Lumiera  0.pre.03
»edityourfreedom«
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
file-handling.c
Go to the documentation of this file.
1 /*
2  FILE-HANDLING - file access and handling
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 
29 #include "include/logging.h"
30 #include "lib/mutex.h"
31 #include "lib/safeclib.h"
32 
33 #include "backend/file.h"
35 
36 #include <limits.h>
37 #include <unistd.h>
38 
39 
40 LUMIERA_ERROR_DEFINE (FILE_CHANGED, "File changed unexpected");
41 LUMIERA_ERROR_DEFINE (FILE_NOMMAPINGS, "MMapings (chunksize/bias) not initialised");
42 
43 
44 
46 lumiera_file_init (LumieraFile self, const char* name, int flags)
47 {
48  TRACE (file_dbg);
49 
50  if (self)
51  {
52  llist_init (&self->node);
53 
54  if (!(self->descriptor = lumiera_filedescriptor_acquire (name, flags, &self->node)))
55  return NULL;
56 
57  self->name = lumiera_strndup (name, PATH_MAX);
58  }
59 
60  return self;
61 }
62 
64 lumiera_file_destroy (LumieraFile self, int do_unlink)
65 {
66  TRACE (file_dbg);
67 
68  lumiera_filedescriptor_release (self->descriptor, self->name, &self->node);
69  if (do_unlink)
70  unlink (self->name);
71 
72  lumiera_free (self->name);
73  return self;
74 }
75 
76 
78 lumiera_file_new (const char* name, int flags)
79 {
80  TRACE (file_dbg);
81  TRACE (file, "opening file '%s' with flags '%x'", name, flags);
82 
83  LumieraFile self = lumiera_malloc (sizeof (lumiera_file));
84  if (!lumiera_file_init (self, name, flags))
85  {
86  lumiera_free (self);
87  self = NULL;
88  }
89 
90  return self;
91 }
92 
93 void
95 {
96  TRACE (file_dbg);
97  TRACE (file, "close file '%s'", self->name);
99 }
100 
101 
102 void
104 {
105  TRACE (file_dbg);
106  TRACE (file, "close and unlink file '%s'", self->name);
108 }
109 
110 
111 int
113 {
114  TRACE (file_dbg);
115  REQUIRE (self);
116  REQUIRE (self->descriptor);
117 
118  return lumiera_filedescriptor_handle_acquire (self->descriptor);
119 }
120 
121 
122 void
124 {
125  TRACE (file_dbg);
126  REQUIRE (self);
127  REQUIRE (self->descriptor);
128 
129  return lumiera_filedescriptor_handle_release (self->descriptor);
130 }
131 
132 
133 size_t
134 lumiera_file_set_chunksize_bias (LumieraFile self, size_t chunksize, size_t bias)
135 {
136  if (chunksize && !self->descriptor->mmapings)
137  self->descriptor->mmapings = lumiera_mmapings_new (self, chunksize, bias);
138 
139  return self->descriptor->mmapings->chunksize;
140 }
141 
142 
143 size_t
145 {
146  if (!self->descriptor->mmapings)
147  {
148  LUMIERA_ERROR_SET (file, FILE_NOMMAPINGS, lumiera_filedescriptor_name (self->descriptor));
149  return 0;
150  }
151 
152  return self->descriptor->mmapings->chunksize;
153 }
154 
155 
156 size_t
158 {
159  if (!self->descriptor->mmapings)
160  {
161  LUMIERA_ERROR_SET (file, FILE_NOMMAPINGS, lumiera_filedescriptor_name (self->descriptor));
162  return 0;
163  }
164 
165  return self->descriptor->mmapings->bias;
166 }
167 
168 
171 {
172  if (!self->descriptor->mmapings)
173  LUMIERA_ERROR_SET (file, FILE_NOMMAPINGS, lumiera_filedescriptor_name (self->descriptor));
174 
175  return self->descriptor->mmapings;
176 }
177 
178 
179 int
181 {
182  return self->descriptor->flags & flags;
183 }
184 
185 
187 lumiera_file_mmap_acquire (LumieraFile self, off_t start, size_t size)
188 {
189  TRACE (file_dbg);
190  return lumiera_mmapings_mmap_acquire (lumiera_file_mmapings (self), self, start, size);
191 }
192 
193 
194 void
196 {
197  TRACE (file_dbg);
199 }
200 
201 
202 
205 {
206  if (self && !lumiera_filedescriptor_rdlock (self->descriptor))
207  return NULL;
208 
209  return self;
210 }
211 
212 
215 {
216  if (self && !lumiera_filedescriptor_wrlock (self->descriptor))
217  return NULL;
218 
219  return self;
220 }
221 
222 
225 {
226  if (self && !lumiera_filedescriptor_unlock (self->descriptor))
227  return NULL;
228 
229  return self;
230 }
231 
232 
233 
234 
235 /*
236 // Local Variables:
237 // mode: C
238 // c-file-style: "gnu"
239 // indent-tabs-mode: nil
240 // End:
241 */
Portable and safe wrappers around some C-Lib functions.
LumieraFile lumiera_file_destroy(LumieraFile self, int do_unlink)
Destroy a file structure.
Definition: file-handling.c:64
#define LUMIERA_ERROR_SET(flag, err, extra)
Helper macro to raise an error for the current thread.
Definition: error.h:91
void lumiera_file_handle_release(LumieraFile self)
Put filehandle back into cache aging.
LumieraMMapings lumiera_file_mmapings(LumieraFile self)
Query the underlying mmapings object from a file The MMapings only exists after a chunksize got set w...
LumieraFile lumiera_file_wrlock(LumieraFile self)
lumiera_file * LumieraFile
Definition: file.h:49
lumiera_mmap * LumieraMMap
Definition: mmap.h:36
LumieraFiledescriptor lumiera_filedescriptor_wrlock(LumieraFiledescriptor self)
LumieraFile lumiera_file_unlock(LumieraFile self)
void lumiera_file_delete_unlink(LumieraFile self)
Frees a file structure and deletes the associated file name from disk.
File management.
This header is for including and configuring NoBug.
static void lumiera_free(void *mem)
Free previously allocated memory.
Definition: safeclib.h:82
LumieraMMapings lumiera_mmapings_new(LumieraFile file, size_t chunksize, size_t bias)
allocate and initialise new mmapings container
Definition: mmapings.c:79
int lumiera_file_checkflags(LumieraFile self, int flags)
Query the flags effective for a file.
TRACE(test,"inserted %d", data)
LumieraFile lumiera_file_new(const char *name, int flags)
Allocate a new file structure.
Definition: file-handling.c:78
LumieraMMap lumiera_mmapings_mmap_acquire(LumieraMMapings self, LumieraFile file, off_t start, size_t size)
acquire a mmap which covers the given range
Definition: mmapings.c:96
int lumiera_filedescriptor_handle_acquire(LumieraFiledescriptor self)
LumieraFiledescriptor lumiera_filedescriptor_rdlock(LumieraFiledescriptor self)
return NULL
Definition: llist.h:596
LumieraFile lumiera_file_init(LumieraFile self, const char *name, int flags)
Initialise a file structure.
Definition: file-handling.c:46
void lumiera_mmapings_release_mmap(LumieraMMapings self, LumieraMMap map)
release a previously acquired MMap object
Definition: mmapings.c:147
size_t lumiera_file_set_chunksize_bias(LumieraFile self, size_t chunksize, size_t bias)
Set the chunksize for mapping operations can only set once for a file descriptor, subsequent calls ar...
void lumiera_filedescriptor_release(LumieraFiledescriptor self, const char *name, LList filenode)
Release a filedescriptor.
LumieraFiledescriptor lumiera_filedescriptor_unlock(LumieraFiledescriptor self)
LumieraFiledescriptor lumiera_filedescriptor_acquire(const char *name, int flags, LList filenode)
Find existing filedescriptor or create one.
void * lumiera_malloc(size_t size)
Allocate memory.
Definition: safeclib.c:67
Mutual exclusion locking, header.
size_t lumiera_file_bias_get(LumieraFile self)
Get the bias for mapping operations.
int lumiera_file_handle_acquire(LumieraFile self)
Get a POSIX filehandle for a file.
size_t lumiera_file_chunksize_get(LumieraFile self)
Get the chunksize for mapping operations.
void lumiera_filedescriptor_handle_release(LumieraFiledescriptor self)
lumiera_mmapings * LumieraMMapings
Definition: mmapings.h:35
LumieraFile lumiera_file_rdlock(LumieraFile self)
Place and remove locks on a file.
hard wired safety limits.
void lumiera_file_delete(LumieraFile self)
Frees a file structure.
Definition: file-handling.c:94
LUMIERA_ERROR_DEFINE(FILE_CHANGED,"File changed unexpected")
const char * lumiera_filedescriptor_name(LumieraFiledescriptor self)
Caching and management of filehandles.
void lumiera_file_release_mmap(LumieraFile self, LumieraMMap map)
release a previously acquired MMap object
char * lumiera_strndup(const char *str, size_t len)
Duplicate a C string.
Definition: safeclib.c:114
LumieraMMap lumiera_file_mmap_acquire(LumieraFile self, off_t start, size_t size)
acquire a mmap which covers the given range