Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Contents of /trunk/src/files.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1162 - (show annotations) (download) (as text)
Tue Nov 20 14:21:45 2012 UTC (12 years, 3 months ago) by amb
File MIME type: text/x-csrc
File size: 9263 byte(s)
Tidy up all of the recent code changes - Fix comment.

1 /***************************************
2 Functions to handle files.
3
4 Part of the Routino routing software.
5 ******************/ /******************
6 This file Copyright 2008-2012 Andrew M. Bishop
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ***************************************/
21
22
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31 #include <sys/types.h>
32
33 #include "files.h"
34
35
36 /*+ A structure to contain the list of memory mapped files. +*/
37 struct mmapinfo
38 {
39 const char *filename; /*+ The name of the file (the index of the list). +*/
40 int fd; /*+ The file descriptor used when it was opened. +*/
41 void *address; /*+ The address the file was mapped to. +*/
42 size_t length; /*+ The length of the file. +*/
43 };
44
45 /*+ The list of memory mapped files. +*/
46 static struct mmapinfo *mappedfiles;
47
48 /*+ The number of mapped files. +*/
49 static int nmappedfiles=0;
50
51
52 /*++++++++++++++++++++++++++++++++++++++
53 Return a filename composed of the dirname, prefix and name.
54
55 char *FileName Returns a pointer to memory allocated to the filename.
56
57 const char *dirname The directory name.
58
59 const char *prefix The file prefix.
60
61 const char *name The main part of the name.
62 ++++++++++++++++++++++++++++++++++++++*/
63
64 char *FileName(const char *dirname,const char *prefix, const char *name)
65 {
66 char *filename=(char*)malloc((dirname?strlen(dirname):0)+1+(prefix?strlen(prefix):0)+1+strlen(name)+1);
67
68 sprintf(filename,"%s%s%s%s%s",dirname?dirname:"",dirname?"/":"",prefix?prefix:"",prefix?"-":"",name);
69
70 return(filename);
71 }
72
73
74 /*++++++++++++++++++++++++++++++++++++++
75 Open a file read-only and map it into memory.
76
77 void *MapFile Returns the address of the file or exits in case of an error.
78
79 const char *filename The name of the file to open.
80 ++++++++++++++++++++++++++++++++++++++*/
81
82 void *MapFile(const char *filename)
83 {
84 int fd;
85 off_t size;
86 void *address;
87
88 /* Open the file and get its size */
89
90 fd=ReOpenFile(filename);
91
92 size=SizeFile(filename);
93
94 /* Map the file */
95
96 address=mmap(NULL,size,PROT_READ,MAP_SHARED,fd,0);
97
98 if(address==MAP_FAILED)
99 {
100 close(fd);
101
102 fprintf(stderr,"Cannot mmap file '%s' for reading [%s].\n",filename,strerror(errno));
103 exit(EXIT_FAILURE);
104 }
105
106 /* Store the information about the mapped file */
107
108 mappedfiles=(struct mmapinfo*)realloc((void*)mappedfiles,(nmappedfiles+1)*sizeof(struct mmapinfo));
109
110 mappedfiles[nmappedfiles].filename=filename;
111 mappedfiles[nmappedfiles].fd=fd;
112 mappedfiles[nmappedfiles].address=address;
113 mappedfiles[nmappedfiles].length=size;
114
115 nmappedfiles++;
116
117 return(address);
118 }
119
120
121 /*++++++++++++++++++++++++++++++++++++++
122 Open a file read-write and map it into memory.
123
124 void *MapFileWriteable Returns the address of the file or exits in case of an error.
125
126 const char *filename The name of the file to open.
127 ++++++++++++++++++++++++++++++++++++++*/
128
129 void *MapFileWriteable(const char *filename)
130 {
131 int fd;
132 off_t size;
133 void *address;
134
135 /* Open the file and get its size */
136
137 fd=ReOpenFileWriteable(filename);
138
139 size=SizeFile(filename);
140
141 /* Map the file */
142
143 address=mmap(NULL,size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
144
145 if(address==MAP_FAILED)
146 {
147 close(fd);
148
149 fprintf(stderr,"Cannot mmap file '%s' for reading and writing [%s].\n",filename,strerror(errno));
150 exit(EXIT_FAILURE);
151 }
152
153 /* Store the information about the mapped file */
154
155 mappedfiles=(struct mmapinfo*)realloc((void*)mappedfiles,(nmappedfiles+1)*sizeof(struct mmapinfo));
156
157 mappedfiles[nmappedfiles].filename=filename;
158 mappedfiles[nmappedfiles].fd=fd;
159 mappedfiles[nmappedfiles].address=address;
160 mappedfiles[nmappedfiles].length=size;
161
162 nmappedfiles++;
163
164 return(address);
165 }
166
167
168 /*++++++++++++++++++++++++++++++++++++++
169 Unmap a file and close it.
170
171 void *UnmapFile Returns NULL (for similarity to the MapFile function).
172
173 const void *address The address of the mapped file in memory.
174 ++++++++++++++++++++++++++++++++++++++*/
175
176 void *UnmapFile(const void *address)
177 {
178 int i;
179
180 for(i=0;i<nmappedfiles;i++)
181 if(mappedfiles[i].address==address)
182 break;
183
184 if(i==nmappedfiles)
185 {
186 fprintf(stderr,"The data at address %p was not mapped using MapFile().\n",address);
187 exit(EXIT_FAILURE);
188 }
189
190 /* Close the file */
191
192 close(mappedfiles[i].fd);
193
194 /* Unmap the file */
195
196 munmap(mappedfiles[i].address,mappedfiles[i].length);
197
198 /* Shuffle the list of files */
199
200 nmappedfiles--;
201
202 if(nmappedfiles>i)
203 memmove(&mappedfiles[i],&mappedfiles[i+1],(nmappedfiles-i)*sizeof(struct mmapinfo));
204
205 return(NULL);
206 }
207
208
209 /*++++++++++++++++++++++++++++++++++++++
210 Open a new file on disk for writing.
211
212 int OpenFileNew Returns the file descriptor if OK or exits in case of an error.
213
214 const char *filename The name of the file to create.
215 ++++++++++++++++++++++++++++++++++++++*/
216
217 int OpenFileNew(const char *filename)
218 {
219 int fd;
220
221 /* Open the file */
222
223 fd=open(filename,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
224
225 if(fd<0)
226 {
227 fprintf(stderr,"Cannot open file '%s' for writing [%s].\n",filename,strerror(errno));
228 exit(EXIT_FAILURE);
229 }
230
231 return(fd);
232 }
233
234
235 /*++++++++++++++++++++++++++++++++++++++
236 Open a new or existing file on disk for reading and appending.
237
238 int OpenFileAppend Returns the file descriptor if OK or exits in case of an error.
239
240 const char *filename The name of the file to create or open.
241 ++++++++++++++++++++++++++++++++++++++*/
242
243 int OpenFileAppend(const char *filename)
244 {
245 int fd;
246
247 /* Open the file */
248
249 fd=open(filename,O_RDWR|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
250
251 if(fd<0)
252 {
253 fprintf(stderr,"Cannot open file '%s' for appending [%s].\n",filename,strerror(errno));
254 exit(EXIT_FAILURE);
255 }
256
257 return(fd);
258 }
259
260
261 /*++++++++++++++++++++++++++++++++++++++
262 Open an existing file on disk for reading.
263
264 int ReOpenFile Returns the file descriptor if OK or exits in case of an error.
265
266 const char *filename The name of the file to open.
267 ++++++++++++++++++++++++++++++++++++++*/
268
269 int ReOpenFile(const char *filename)
270 {
271 int fd;
272
273 /* Open the file */
274
275 fd=open(filename,O_RDONLY);
276
277 if(fd<0)
278 {
279 fprintf(stderr,"Cannot open file '%s' for reading [%s].\n",filename,strerror(errno));
280 exit(EXIT_FAILURE);
281 }
282
283 return(fd);
284 }
285
286
287 /*++++++++++++++++++++++++++++++++++++++
288 Open an existing file on disk for reading or writing.
289
290 int ReOpenFileWriteable Returns the file descriptor if OK or exits in case of an error.
291
292 const char *filename The name of the file to open.
293 ++++++++++++++++++++++++++++++++++++++*/
294
295 int ReOpenFileWriteable(const char *filename)
296 {
297 int fd;
298
299 /* Open the file */
300
301 fd=open(filename,O_RDWR);
302
303 if(fd<0)
304 {
305 fprintf(stderr,"Cannot open file '%s' for reading and writing [%s].\n",filename,strerror(errno));
306 exit(EXIT_FAILURE);
307 }
308
309 return(fd);
310 }
311
312
313 /*++++++++++++++++++++++++++++++++++++++
314 Get the size of a file.
315
316 off_t SizeFile Returns the file size if OK or exits in case of an error.
317
318 const char *filename The name of the file to check.
319 ++++++++++++++++++++++++++++++++++++++*/
320
321 off_t SizeFile(const char *filename)
322 {
323 struct stat buf;
324
325 if(stat(filename,&buf))
326 {
327 fprintf(stderr,"Cannot stat file '%s' [%s].\n",filename,strerror(errno));
328 exit(EXIT_FAILURE);
329 }
330
331 return(buf.st_size);
332 }
333
334
335 /*++++++++++++++++++++++++++++++++++++++
336 Check if a file exists.
337
338 int ExistsFile Returns 1 if the file exists and 0 if not.
339
340 const char *filename The name of the file to check.
341 ++++++++++++++++++++++++++++++++++++++*/
342
343 int ExistsFile(const char *filename)
344 {
345 struct stat buf;
346
347 if(stat(filename,&buf))
348 return(0);
349 else
350 return(1);
351 }
352
353
354 /*++++++++++++++++++++++++++++++++++++++
355 Close a file on disk.
356
357 int CloseFile returns -1 (for similarity to the *OpenFile* functions).
358
359 int fd The file descriptor to close.
360 ++++++++++++++++++++++++++++++++++++++*/
361
362 int CloseFile(int fd)
363 {
364 close(fd);
365
366 return(-1);
367 }
368
369
370 /*++++++++++++++++++++++++++++++++++++++
371 Delete a file from disk.
372
373 int DeleteFile Returns 0 if OK.
374
375 const char *filename The name of the file to delete.
376 ++++++++++++++++++++++++++++++++++++++*/
377
378 int DeleteFile(const char *filename)
379 {
380 unlink(filename);
381
382 return(0);
383 }
384
385
386 /*++++++++++++++++++++++++++++++++++++++
387 Rename a file on disk.
388
389 int RenameFile Returns 0 if OK.
390
391 const char *oldfilename The old name of the file before renaming.
392
393 const char *newfilename The new name of the file after renaming.
394 ++++++++++++++++++++++++++++++++++++++*/
395
396 int RenameFile(const char *oldfilename,const char *newfilename)
397 {
398 rename(oldfilename,newfilename);
399
400 return(0);
401 }

Properties

Name Value
cvs:description File handling functions.