Lumiera  0.pre.03
»edityourfreedom«
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
filehandle.c
Go to the documentation of this file.
1 /*
2  FileHandle - filehandle management and caching
3 
4  Copyright (C) Lumiera.org
5  2008, Christian Thaeter <ct@pipapo.org>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
30 #include "include/logging.h"
31 #include "lib/llist.h"
32 #include "lib/safeclib.h"
33 
34 #include "backend/file.h"
35 #include "backend/filehandle.h"
36 #include "backend/filedescriptor.h"
37 
38 #include <unistd.h>
39 
40 
41 
44 {
45  TRACE (filehandle_dbg, "%p", self);
46  if (self)
47  {
48  llist_init (&self->cachenode);
49  self->fd = -1;
50  self->use_cnt = 1;
51  self->descriptor = desc;
52  }
53  return self;
54 }
55 
56 
59 {
60  LumieraFilehandle self = lumiera_malloc (sizeof (*self));
61  return lumiera_filehandle_init (self, desc);
62 }
63 
64 
65 void*
67 {
68  TRACE (filehandle_dbg);
69  REQUIRE (llist_is_empty (node));
71  REQUIRE (self->use_cnt == 0);
72 
73  if (self->fd >= 0)
74  close (self->fd);
75  return self;
76 }
77 
78 
79 
80 int
82 {
83  TRACE (filehandle_dbg);
84 
85  int fd = -1;
86  if (self->fd == -1)
87  {
88  fd = open (lumiera_filedescriptor_name (self->descriptor), lumiera_filedescriptor_flags (self->descriptor) & LUMIERA_FILE_MASK);
89  if (fd == -1)
90  {
92  LUMIERA_ERROR_SET_CRITICAL (file, ERRNO, lumiera_filedescriptor_name (self->descriptor));
93  }
94  else
95  {
96  struct stat st;
97  if (fstat (fd, &st) == -1)
98  {
99  close (fd);
100  fd = -1;
101  LUMIERA_ERROR_SET_CRITICAL (file, ERRNO, lumiera_filedescriptor_name (self->descriptor));
102  }
103  else if (!lumiera_filedescriptor_samestat (self->descriptor, &st))
104  {
105  close (fd);
106  fd = -1;
107  /* Woops this is not the file we expected to use */
108  LUMIERA_ERROR_SET_CRITICAL (file, FILE_CHANGED, lumiera_filedescriptor_name (self->descriptor));
109  }
110  }
111  self->fd = fd;
112  }
113 
114  return self->fd;
115 }
Portable and safe wrappers around some C-Lib functions.
LumieraFilehandle lumiera_filehandle_init(LumieraFilehandle self, LumieraFiledescriptor desc)
Initialise filehandle structure.
Definition: filehandle.c:43
File descriptors are the underlying working horse in accessing files.
File management.
This header is for including and configuring NoBug.
lumiera_filehandle * LumieraFilehandle
Definition: filehandle.h:38
int lumiera_filehandle_handle(LumieraFilehandle self)
Definition: filehandle.c:81
int lumiera_filedescriptor_flags(LumieraFiledescriptor self)
TRACE(test,"inserted %d", data)
#define LUMIERA_ERROR_SET_CRITICAL(flag, err, extra)
Helper macro to raise an error for the current thread.
Definition: error.h:121
Filehandles manage the underlying POSIX filehandle for a file descriptor.
void * lumiera_malloc(size_t size)
Allocate memory.
Definition: safeclib.c:67
lumiera_filedescriptor * LumieraFiledescriptor
Intrusive cyclic double linked list There is only one node type which contains a forward and a backwa...
int lumiera_filedescriptor_samestat(LumieraFiledescriptor self, struct stat *stat)
void * lumiera_filehandle_destroy_node(LList node)
destroy the resources associated either a filehandle structure.
Definition: filehandle.c:66
const char * lumiera_filedescriptor_name(LumieraFiledescriptor self)
LumieraFilehandle lumiera_filehandle_new(LumieraFiledescriptor desc)
Allocate a new filehandle structure.
Definition: filehandle.c:58
#define LUMIERA_FILE_MASK
Definition: file.h:70
llist * LList
Definition: llist.h:91