Routino SVN Repository Browser

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

ViewVC logotype

Annotation of /trunk/src/files.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 701 - (hide annotations) (download) (as text)
Sat May 7 13:18:36 2011 UTC (13 years, 10 months ago) by amb
File MIME type: text/x-csrc
File size: 8855 byte(s)
Remove useless assert statement.

1 amb 2 /***************************************
2 amb 326 Functions to handle files.
3 amb 151
4     Part of the Routino routing software.
5 amb 2 ******************/ /******************
6 amb 612 This file Copyright 2008-2011 Andrew M. Bishop
7 amb 2
8 amb 151 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 amb 2 ***************************************/
21    
22 amb 680
23 amb 2 #include <unistd.h>
24     #include <stdlib.h>
25 amb 449 #include <stdio.h>
26 amb 162 #include <string.h>
27 amb 2 #include <fcntl.h>
28 amb 330 #include <errno.h>
29 amb 2 #include <sys/stat.h>
30     #include <sys/mman.h>
31 amb 251 #include <sys/types.h>
32 amb 2
33 amb 449 #include "files.h"
34 amb 2
35    
36 amb 289 /*+ A structure to contain the list of memory mapped files. +*/
37 amb 248 struct mmapinfo
38     {
39 amb 289 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 amb 248 };
44    
45 amb 289 /*+ The list of memory mapped files. +*/
46     static struct mmapinfo *mappedfiles;
47 amb 248
48 amb 289 /*+ The number of mapped files. +*/
49     static int nmappedfiles=0;
50 amb 248
51 amb 289
52 amb 2 /*++++++++++++++++++++++++++++++++++++++
53 amb 680 Return a filename composed of the dirname, prefix and name.
54 amb 162
55 amb 680 char *FileName Returns a pointer to memory allocated to the filename.
56 amb 162
57     const char *dirname The directory name.
58    
59     const char *prefix The file prefix.
60    
61 amb 680 const char *name The main part of the name.
62 amb 162 ++++++++++++++++++++++++++++++++++++++*/
63    
64     char *FileName(const char *dirname,const char *prefix, const char *name)
65     {
66 amb 518 char *filename=(char*)malloc((dirname?strlen(dirname):0)+1+(prefix?strlen(prefix):0)+1+strlen(name)+1);
67 amb 162
68     sprintf(filename,"%s%s%s%s%s",dirname?dirname:"",dirname?"/":"",prefix?prefix:"",prefix?"-":"",name);
69    
70     return(filename);
71     }
72    
73    
74     /*++++++++++++++++++++++++++++++++++++++
75 amb 680 Open a file read-only and map it into memory.
76 amb 2
77 amb 331 void *MapFile Returns the address of the file or exits in case of an error.
78 amb 2
79     const char *filename The name of the file to open.
80     ++++++++++++++++++++++++++++++++++++++*/
81    
82 amb 250 void *MapFile(const char *filename)
83 amb 2 {
84     int fd;
85 amb 331 off_t size;
86 amb 2 void *address;
87    
88 amb 331 /* Open the file and get its size */
89 amb 2
90 amb 331 fd=ReOpenFile(filename);
91 amb 2
92 amb 331 size=SizeFile(filename);
93 amb 2
94     /* Map the file */
95    
96 amb 331 address=mmap(NULL,size,PROT_READ,MAP_SHARED,fd,0);
97 amb 2
98 amb 245 if(address==MAP_FAILED)
99 amb 2 {
100     close(fd);
101 amb 177
102 amb 511 fprintf(stderr,"Cannot mmap file '%s' for reading [%s].\n",filename,strerror(errno));
103 amb 177 exit(EXIT_FAILURE);
104 amb 2 }
105    
106 amb 680 /* Store the information about the mapped file */
107    
108 amb 248 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 amb 331 mappedfiles[nmappedfiles].length=size;
114 amb 248
115     nmappedfiles++;
116    
117 amb 2 return(address);
118     }
119    
120    
121     /*++++++++++++++++++++++++++++++++++++++
122 amb 680 Open a file read-write and map it into memory.
123 amb 507
124 amb 513 void *MapFileWriteable Returns the address of the file or exits in case of an error.
125 amb 507
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 amb 511 fd=ReOpenFileWriteable(filename);
138 amb 507
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 amb 511 fprintf(stderr,"Cannot mmap file '%s' for reading and writing [%s].\n",filename,strerror(errno));
150 amb 507 exit(EXIT_FAILURE);
151     }
152    
153 amb 680 /* Store the information about the mapped file */
154    
155 amb 507 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 amb 680 Unmap a file and close it.
170 amb 2
171 amb 289 void *UnmapFile Returns NULL (for similarity to the MapFile function).
172 amb 255
173 amb 248 const char *filename The name of the file when it was opened.
174     ++++++++++++++++++++++++++++++++++++++*/
175    
176 amb 255 void *UnmapFile(const char *filename)
177 amb 248 {
178     int i;
179    
180     for(i=0;i<nmappedfiles;i++)
181     if(!strcmp(mappedfiles[i].filename,filename))
182     break;
183    
184     if(i==nmappedfiles)
185     {
186 amb 330 fprintf(stderr,"The file '%s' was not mapped using MapFile().\n",filename);
187 amb 248 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 amb 255
205     return(NULL);
206 amb 248 }
207    
208    
209     /*++++++++++++++++++++++++++++++++++++++
210 amb 680 Open a new file on disk for writing.
211 amb 248
212 amb 513 int OpenFileNew Returns the file descriptor if OK or exits in case of an error.
213 amb 248
214 amb 97 const char *filename The name of the file to create.
215 amb 2 ++++++++++++++++++++++++++++++++++++++*/
216    
217 amb 502 int OpenFileNew(const char *filename)
218 amb 2 {
219 amb 97 int fd;
220 amb 2
221 amb 97 /* Open the file */
222 amb 2
223 amb 448 fd=open(filename,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
224 amb 2
225 amb 97 if(fd<0)
226 amb 177 {
227 amb 330 fprintf(stderr,"Cannot open file '%s' for writing [%s].\n",filename,strerror(errno));
228 amb 177 exit(EXIT_FAILURE);
229     }
230 amb 2
231 amb 97 return(fd);
232 amb 2 }
233    
234    
235     /*++++++++++++++++++++++++++++++++++++++
236 amb 680 Open a new or existing file on disk for reading and appending.
237 amb 326
238 amb 502 int OpenFileAppend Returns the file descriptor if OK or exits in case of an error.
239 amb 326
240 amb 680 const char *filename The name of the file to create or open.
241 amb 326 ++++++++++++++++++++++++++++++++++++++*/
242    
243 amb 502 int OpenFileAppend(const char *filename)
244 amb 326 {
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 amb 330 fprintf(stderr,"Cannot open file '%s' for appending [%s].\n",filename,strerror(errno));
254 amb 326 exit(EXIT_FAILURE);
255     }
256    
257     return(fd);
258     }
259    
260    
261     /*++++++++++++++++++++++++++++++++++++++
262 amb 511 Open an existing file on disk for reading.
263 amb 251
264 amb 331 int ReOpenFile Returns the file descriptor if OK or exits in case of an error.
265 amb 251
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 amb 511 fd=open(filename,O_RDONLY);
276 amb 251
277     if(fd<0)
278     {
279 amb 330 fprintf(stderr,"Cannot open file '%s' for reading [%s].\n",filename,strerror(errno));
280 amb 251 exit(EXIT_FAILURE);
281     }
282    
283     return(fd);
284     }
285    
286    
287     /*++++++++++++++++++++++++++++++++++++++
288 amb 680 Open an existing file on disk for reading or writing.
289 amb 511
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 amb 331 Get the size of a file.
315    
316 amb 680 off_t SizeFile Returns the file size if OK or exits in case of an error.
317 amb 331
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 amb 341 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 amb 97 Close a file on disk.
356 amb 87
357 amb 680 int CloseFile returns -1 (for similarity to the *OpenFile* functions).
358 amb 612
359 amb 97 int fd The file descriptor to close.
360 amb 87 ++++++++++++++++++++++++++++++++++++++*/
361    
362 amb 612 int CloseFile(int fd)
363 amb 87 {
364 amb 97 close(fd);
365 amb 612
366     return(-1);
367 amb 87 }
368 amb 251
369    
370     /*++++++++++++++++++++++++++++++++++++++
371     Delete a file from disk.
372    
373 amb 680 int DeleteFile Returns 0 if OK.
374 amb 251
375     char *filename The name of the file to delete.
376     ++++++++++++++++++++++++++++++++++++++*/
377    
378     int DeleteFile(char *filename)
379     {
380     unlink(filename);
381    
382     return(0);
383     }

Properties

Name Value
cvs:description File handling functions.