00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "lib/llist.h"
00023 #include "lib/safeclib.h"
00024
00025 #include "backend/file.h"
00026 #include "backend/filehandle.h"
00027 #include "backend/filedescriptor.h"
00028
00029 #include <unistd.h>
00030
00031 LumieraFilehandle
00032 lumiera_filehandle_init (LumieraFilehandle self, LumieraFiledescriptor desc)
00033 {
00034 TRACE (filehandle, "%p", self);
00035 if (self)
00036 {
00037 llist_init (&self->cachenode);
00038 self->fd = -1;
00039 self->use_cnt = 1;
00040 self->descriptor = desc;
00041 }
00042 return self;
00043 }
00044
00045
00046 LumieraFilehandle
00047 lumiera_filehandle_new (LumieraFiledescriptor desc)
00048 {
00049 LumieraFilehandle self = lumiera_malloc (sizeof (*self));
00050 return lumiera_filehandle_init (self, desc);
00051 }
00052
00053
00054 void*
00055 lumiera_filehandle_destroy_node (LList node)
00056 {
00057 TRACE (filehandle);
00058 REQUIRE (llist_is_empty (node));
00059 LumieraFilehandle self = (LumieraFilehandle)node;
00060 REQUIRE (self->use_cnt == 0);
00061
00062 if (self->fd >= 0)
00063 close (self->fd);
00064 return self;
00065 }
00066
00067
00068 int
00069 lumiera_filehandle_get (LumieraFilehandle self)
00070 {
00071 REQUIRE (self->descriptor);
00072 return -1;
00073 }
00074
00075
00076 int
00077 lumiera_filehandle_handle (LumieraFilehandle self)
00078 {
00079 TRACE (filehandle);
00080
00081 int fd = -1;
00082 if (self->fd == -1)
00083 {
00084 fd = open (lumiera_filedescriptor_name (self->descriptor), lumiera_filedescriptor_flags (self->descriptor) & LUMIERA_FILE_MASK);
00085 if (fd == -1)
00086 {
00087 LUMIERA_ERROR_SET (filehandle, ERRNO);
00088 }
00089 else
00090 {
00091 struct stat st;
00092 if (fstat (fd, &st) == -1)
00093 {
00094 close (fd);
00095 LUMIERA_ERROR_SET (filehandle, ERRNO);
00096 }
00097 else if (!lumiera_filedescriptor_samestat (self->descriptor, &st))
00098 {
00099 close (fd);
00100
00101 LUMIERA_ERROR_SET (filehandle, FILE_CHANGED);
00102 }
00103 }
00104 self->fd = fd;
00105 }
00106
00107 return self->fd;
00108 }