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 255 - (hide annotations) (download) (as text)
Sun Sep 6 15:50:15 2009 UTC (15 years, 7 months ago) by amb
File MIME type: text/x-csrc
File size: 6860 byte(s)
Remove the delete option from UnmapFile() and make it return NULL.

1 amb 2 /***************************************
2 amb 255 $Header: /home/amb/CVS/routino/src/files.c,v 1.12 2009-09-06 15:50:15 amb Exp $
3 amb 2
4     Functions to map a file into memory.
5 amb 151
6     Part of the Routino routing software.
7 amb 2 ******************/ /******************
8 amb 151 This file Copyright 2008,2009 Andrew M. Bishop
9 amb 2
10 amb 151 This program is free software: you can redistribute it and/or modify
11     it under the terms of the GNU Affero General Public License as published by
12     the Free Software Foundation, either version 3 of the License, or
13     (at your option) any later version.
14    
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18     GNU Affero General Public License for more details.
19    
20     You should have received a copy of the GNU Affero General Public License
21     along with this program. If not, see <http://www.gnu.org/licenses/>.
22 amb 2 ***************************************/
23    
24    
25     #include <unistd.h>
26     #include <stdlib.h>
27 amb 162 #include <string.h>
28 amb 2 #include <fcntl.h>
29     #include <sys/stat.h>
30     #include <sys/mman.h>
31 amb 251 #include <sys/types.h>
32 amb 2
33     #include "functions.h"
34    
35    
36 amb 248 struct mmapinfo
37     {
38     const char *filename;
39     int fd;
40     void *address;
41     size_t length;
42     };
43    
44     struct mmapinfo *mappedfiles;
45     int nmappedfiles=0;
46    
47    
48 amb 2 /*++++++++++++++++++++++++++++++++++++++
49 amb 162 Return a filename composed of the dirname, prefix and filename.
50    
51     char *FileName Returns an allocated filename.
52    
53     const char *dirname The directory name.
54    
55     const char *prefix The file prefix.
56    
57     const char *name The filename.
58     ++++++++++++++++++++++++++++++++++++++*/
59    
60     char *FileName(const char *dirname,const char *prefix, const char *name)
61     {
62     char *filename=(char*)malloc((dirname?strlen(dirname):0)+1+(prefix?strlen(prefix):0)+1+strlen(name));
63    
64     sprintf(filename,"%s%s%s%s%s",dirname?dirname:"",dirname?"/":"",prefix?prefix:"",prefix?"-":"",name);
65    
66     return(filename);
67     }
68    
69    
70     /*++++++++++++++++++++++++++++++++++++++
71 amb 250 Open a file and map it into memory.
72 amb 2
73     void *MapFile Returns the address of the file.
74    
75     const char *filename The name of the file to open.
76     ++++++++++++++++++++++++++++++++++++++*/
77    
78 amb 250 void *MapFile(const char *filename)
79 amb 2 {
80     int fd;
81     struct stat buf;
82     void *address;
83    
84     /* Open the file */
85    
86     fd=open(filename,O_RDONLY);
87    
88 amb 177 if(fd<0)
89     {
90     fprintf(stderr,"Cannot open file '%s' to read.\n",filename);
91     exit(EXIT_FAILURE);
92     }
93 amb 2
94     /* Get the length of the file */
95    
96     if(fstat(fd,&buf))
97     {
98     close(fd);
99 amb 177
100     fprintf(stderr,"Cannot stat file '%s'.\n",filename);
101     exit(EXIT_FAILURE);
102 amb 2 }
103    
104     /* Map the file */
105    
106 amb 248 address=mmap(NULL,buf.st_size,PROT_READ,MAP_SHARED,fd,0);
107 amb 2
108 amb 245 if(address==MAP_FAILED)
109 amb 2 {
110     close(fd);
111 amb 177
112     fprintf(stderr,"Cannot mmap file '%s'.\n",filename);
113     exit(EXIT_FAILURE);
114 amb 2 }
115    
116 amb 248 mappedfiles=(struct mmapinfo*)realloc((void*)mappedfiles,(nmappedfiles+1)*sizeof(struct mmapinfo));
117    
118     mappedfiles[nmappedfiles].filename=filename;
119     mappedfiles[nmappedfiles].fd=fd;
120     mappedfiles[nmappedfiles].address=address;
121     mappedfiles[nmappedfiles].length=buf.st_size;
122    
123     nmappedfiles++;
124    
125 amb 2 return(address);
126     }
127    
128    
129     /*++++++++++++++++++++++++++++++++++++++
130 amb 248 Unmap a file and optionally delete it.
131 amb 2
132 amb 255 Returns NULL (for similarity to the MapFile function).
133    
134 amb 248 const char *filename The name of the file when it was opened.
135     ++++++++++++++++++++++++++++++++++++++*/
136    
137 amb 255 void *UnmapFile(const char *filename)
138 amb 248 {
139     int i;
140    
141     for(i=0;i<nmappedfiles;i++)
142     if(!strcmp(mappedfiles[i].filename,filename))
143     break;
144    
145     if(i==nmappedfiles)
146     {
147     fprintf(stderr,"Cannot find file '%s' to unmap.\n",filename);
148     exit(EXIT_FAILURE);
149     }
150    
151     /* Close the file */
152    
153     close(mappedfiles[i].fd);
154    
155     /* Unmap the file */
156    
157     munmap(mappedfiles[i].address,mappedfiles[i].length);
158    
159     /* Shuffle the list of files */
160    
161     nmappedfiles--;
162    
163     if(nmappedfiles>i)
164     memmove(&mappedfiles[i],&mappedfiles[i+1],(nmappedfiles-i)*sizeof(struct mmapinfo));
165 amb 255
166     return(NULL);
167 amb 248 }
168    
169    
170     /*++++++++++++++++++++++++++++++++++++++
171     Open a new file on disk for writing to.
172    
173     int OpenFile Returns the file descriptor if OK or something negative in case of an error.
174    
175 amb 97 const char *filename The name of the file to create.
176 amb 2 ++++++++++++++++++++++++++++++++++++++*/
177    
178 amb 97 int OpenFile(const char *filename)
179 amb 2 {
180 amb 97 int fd;
181 amb 2
182 amb 97 /* Open the file */
183 amb 2
184 amb 248 fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
185 amb 2
186 amb 97 if(fd<0)
187 amb 177 {
188     fprintf(stderr,"Cannot open file '%s' to write.\n",filename);
189     exit(EXIT_FAILURE);
190     }
191 amb 2
192 amb 97 return(fd);
193 amb 2 }
194    
195    
196     /*++++++++++++++++++++++++++++++++++++++
197 amb 251 Open an existing file on disk for reading from.
198    
199     int OpenFile Returns the file descriptor if OK or something negative in case of an error.
200    
201     const char *filename The name of the file to open.
202     ++++++++++++++++++++++++++++++++++++++*/
203    
204     int ReOpenFile(const char *filename)
205     {
206     int fd;
207    
208     /* Open the file */
209    
210     fd=open(filename,O_RDONLY);
211    
212     if(fd<0)
213     {
214     fprintf(stderr,"Cannot open file '%s' to read.\n",filename);
215     exit(EXIT_FAILURE);
216     }
217    
218     return(fd);
219     }
220    
221    
222     /*++++++++++++++++++++++++++++++++++++++
223 amb 97 Write data to a file on disk.
224 amb 2
225     int WriteFile Returns 0 if OK or something else in case of an error.
226    
227 amb 97 int fd The file descriptor to write to.
228 amb 2
229 amb 251 void *address The address of the data to be written from.
230 amb 2
231     size_t length The length of data to write.
232     ++++++++++++++++++++++++++++++++++++++*/
233    
234 amb 97 int WriteFile(int fd,void *address,size_t length)
235 amb 2 {
236     /* Write the data */
237    
238     if(write(fd,address,length)!=length)
239     return(-1);
240    
241     return(0);
242     }
243 amb 87
244    
245     /*++++++++++++++++++++++++++++++++++++++
246 amb 251 Read data from a file on disk.
247    
248     int ReadFile Returns 0 if OK or something else in case of an error.
249    
250     int fd The file descriptor to read from.
251    
252     void *address The address of the data to be read into.
253    
254     size_t length The length of data to read.
255     ++++++++++++++++++++++++++++++++++++++*/
256    
257     int ReadFile(int fd,void *address,size_t length)
258     {
259     /* Read the data */
260    
261     if(read(fd,address,length)!=length)
262     return(-1);
263    
264     return(0);
265     }
266    
267    
268     /*++++++++++++++++++++++++++++++++++++++
269     Seek to a position in a file on disk.
270    
271     int SeekFile Returns 0 if OK or something else in case of an error.
272    
273     int fd The file descriptor to seek within.
274    
275     size_t position The position to seek to.
276     ++++++++++++++++++++++++++++++++++++++*/
277    
278     int SeekFile(int fd,size_t position)
279     {
280     /* Seek the data */
281    
282     if(lseek(fd,position,SEEK_SET)!=position)
283     return(-1);
284    
285     return(0);
286     }
287    
288    
289     /*++++++++++++++++++++++++++++++++++++++
290 amb 97 Close a file on disk.
291 amb 87
292 amb 97 int fd The file descriptor to close.
293 amb 87 ++++++++++++++++++++++++++++++++++++++*/
294    
295 amb 97 void CloseFile(int fd)
296 amb 87 {
297 amb 97 close(fd);
298 amb 87 }
299 amb 251
300    
301     /*++++++++++++++++++++++++++++++++++++++
302     Delete a file from disk.
303    
304     int DeleteFile Returns 0 if OK or something else in case of an error.
305    
306     char *filename The name of the file to delete.
307     ++++++++++++++++++++++++++++++++++++++*/
308    
309     int DeleteFile(char *filename)
310     {
311     unlink(filename);
312    
313     return(0);
314     }

Properties

Name Value
cvs:description File handling functions.