Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Annotation of /trunk/src/files.c
Parent Directory
|
Revision Log
Revision 326 -
(hide annotations)
(download)
(as text)
Fri Mar 19 19:47:10 2010 UTC (15 years ago) by amb
File MIME type: text/x-csrc
File size: 7835 byte(s)
Fri Mar 19 19:47:10 2010 UTC (15 years ago) by amb
File MIME type: text/x-csrc
File size: 7835 byte(s)
Allow planetsplitter to be run with a --parse-only or --process-only option and append to existing file or read from existing file.
1 | amb | 2 | /*************************************** |
2 | amb | 326 | $Header: /home/amb/CVS/routino/src/files.c,v 1.15 2010-03-19 19:47:09 amb Exp $ |
3 | amb | 2 | |
4 | amb | 326 | Functions to handle files. |
5 | amb | 151 | |
6 | Part of the Routino routing software. | ||
7 | amb | 2 | ******************/ /****************** |
8 | amb | 326 | This file Copyright 2008-2010 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 | 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 | 162 | Return a filename composed of the dirname, prefix and filename. |
54 | |||
55 | char *FileName Returns an allocated filename. | ||
56 | |||
57 | const char *dirname The directory name. | ||
58 | |||
59 | const char *prefix The file prefix. | ||
60 | |||
61 | const char *name The filename. | ||
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)); | ||
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 | amb | 250 | Open a file and map it into memory. |
76 | amb | 2 | |
77 | void *MapFile Returns the address of the file. | ||
78 | |||
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 | struct stat buf; | ||
86 | void *address; | ||
87 | |||
88 | /* Open the file */ | ||
89 | |||
90 | fd=open(filename,O_RDONLY); | ||
91 | |||
92 | amb | 177 | if(fd<0) |
93 | { | ||
94 | fprintf(stderr,"Cannot open file '%s' to read.\n",filename); | ||
95 | exit(EXIT_FAILURE); | ||
96 | } | ||
97 | amb | 2 | |
98 | /* Get the length of the file */ | ||
99 | |||
100 | if(fstat(fd,&buf)) | ||
101 | { | ||
102 | close(fd); | ||
103 | amb | 177 | |
104 | fprintf(stderr,"Cannot stat file '%s'.\n",filename); | ||
105 | exit(EXIT_FAILURE); | ||
106 | amb | 2 | } |
107 | |||
108 | /* Map the file */ | ||
109 | |||
110 | amb | 248 | address=mmap(NULL,buf.st_size,PROT_READ,MAP_SHARED,fd,0); |
111 | amb | 2 | |
112 | amb | 245 | if(address==MAP_FAILED) |
113 | amb | 2 | { |
114 | close(fd); | ||
115 | amb | 177 | |
116 | fprintf(stderr,"Cannot mmap file '%s'.\n",filename); | ||
117 | exit(EXIT_FAILURE); | ||
118 | amb | 2 | } |
119 | |||
120 | amb | 248 | mappedfiles=(struct mmapinfo*)realloc((void*)mappedfiles,(nmappedfiles+1)*sizeof(struct mmapinfo)); |
121 | |||
122 | mappedfiles[nmappedfiles].filename=filename; | ||
123 | mappedfiles[nmappedfiles].fd=fd; | ||
124 | mappedfiles[nmappedfiles].address=address; | ||
125 | mappedfiles[nmappedfiles].length=buf.st_size; | ||
126 | |||
127 | nmappedfiles++; | ||
128 | |||
129 | amb | 2 | return(address); |
130 | } | ||
131 | |||
132 | |||
133 | /*++++++++++++++++++++++++++++++++++++++ | ||
134 | amb | 248 | Unmap a file and optionally delete it. |
135 | amb | 2 | |
136 | amb | 289 | void *UnmapFile Returns NULL (for similarity to the MapFile function). |
137 | amb | 255 | |
138 | amb | 248 | const char *filename The name of the file when it was opened. |
139 | ++++++++++++++++++++++++++++++++++++++*/ | ||
140 | |||
141 | amb | 255 | void *UnmapFile(const char *filename) |
142 | amb | 248 | { |
143 | int i; | ||
144 | |||
145 | for(i=0;i<nmappedfiles;i++) | ||
146 | if(!strcmp(mappedfiles[i].filename,filename)) | ||
147 | break; | ||
148 | |||
149 | if(i==nmappedfiles) | ||
150 | { | ||
151 | fprintf(stderr,"Cannot find file '%s' to unmap.\n",filename); | ||
152 | exit(EXIT_FAILURE); | ||
153 | } | ||
154 | |||
155 | /* Close the file */ | ||
156 | |||
157 | close(mappedfiles[i].fd); | ||
158 | |||
159 | /* Unmap the file */ | ||
160 | |||
161 | munmap(mappedfiles[i].address,mappedfiles[i].length); | ||
162 | |||
163 | /* Shuffle the list of files */ | ||
164 | |||
165 | nmappedfiles--; | ||
166 | |||
167 | if(nmappedfiles>i) | ||
168 | memmove(&mappedfiles[i],&mappedfiles[i+1],(nmappedfiles-i)*sizeof(struct mmapinfo)); | ||
169 | amb | 255 | |
170 | return(NULL); | ||
171 | amb | 248 | } |
172 | |||
173 | |||
174 | /*++++++++++++++++++++++++++++++++++++++ | ||
175 | Open a new file on disk for writing to. | ||
176 | |||
177 | int OpenFile Returns the file descriptor if OK or something negative in case of an error. | ||
178 | |||
179 | amb | 97 | const char *filename The name of the file to create. |
180 | amb | 2 | ++++++++++++++++++++++++++++++++++++++*/ |
181 | |||
182 | amb | 97 | int OpenFile(const char *filename) |
183 | amb | 2 | { |
184 | amb | 97 | int fd; |
185 | amb | 2 | |
186 | amb | 97 | /* Open the file */ |
187 | amb | 2 | |
188 | amb | 248 | fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); |
189 | amb | 2 | |
190 | amb | 97 | if(fd<0) |
191 | amb | 177 | { |
192 | fprintf(stderr,"Cannot open file '%s' to write.\n",filename); | ||
193 | exit(EXIT_FAILURE); | ||
194 | } | ||
195 | amb | 2 | |
196 | amb | 97 | return(fd); |
197 | amb | 2 | } |
198 | |||
199 | |||
200 | /*++++++++++++++++++++++++++++++++++++++ | ||
201 | amb | 326 | Open a new file on disk for reading and appending. |
202 | |||
203 | int AppendFile Returns the file descriptor if OK or something negative in case of an error. | ||
204 | |||
205 | const char *filename The name of the file to create. | ||
206 | ++++++++++++++++++++++++++++++++++++++*/ | ||
207 | |||
208 | int AppendFile(const char *filename) | ||
209 | { | ||
210 | int fd; | ||
211 | |||
212 | /* Open the file */ | ||
213 | |||
214 | fd=open(filename,O_RDWR|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); | ||
215 | |||
216 | if(fd<0) | ||
217 | { | ||
218 | fprintf(stderr,"Cannot open file '%s' to read and append.\n",filename); | ||
219 | exit(EXIT_FAILURE); | ||
220 | } | ||
221 | |||
222 | return(fd); | ||
223 | } | ||
224 | |||
225 | |||
226 | /*++++++++++++++++++++++++++++++++++++++ | ||
227 | amb | 251 | Open an existing file on disk for reading from. |
228 | |||
229 | amb | 289 | int ReOpenFile Returns the file descriptor if OK or something negative in case of an error. |
230 | amb | 251 | |
231 | const char *filename The name of the file to open. | ||
232 | ++++++++++++++++++++++++++++++++++++++*/ | ||
233 | |||
234 | int ReOpenFile(const char *filename) | ||
235 | { | ||
236 | int fd; | ||
237 | |||
238 | /* Open the file */ | ||
239 | |||
240 | fd=open(filename,O_RDONLY); | ||
241 | |||
242 | if(fd<0) | ||
243 | { | ||
244 | fprintf(stderr,"Cannot open file '%s' to read.\n",filename); | ||
245 | exit(EXIT_FAILURE); | ||
246 | } | ||
247 | |||
248 | return(fd); | ||
249 | } | ||
250 | |||
251 | |||
252 | /*++++++++++++++++++++++++++++++++++++++ | ||
253 | amb | 97 | Write data to a file on disk. |
254 | amb | 2 | |
255 | int WriteFile Returns 0 if OK or something else in case of an error. | ||
256 | |||
257 | amb | 97 | int fd The file descriptor to write to. |
258 | amb | 2 | |
259 | amb | 259 | const void *address The address of the data to be written from. |
260 | amb | 2 | |
261 | size_t length The length of data to write. | ||
262 | ++++++++++++++++++++++++++++++++++++++*/ | ||
263 | |||
264 | amb | 259 | int WriteFile(int fd,const void *address,size_t length) |
265 | amb | 2 | { |
266 | /* Write the data */ | ||
267 | |||
268 | if(write(fd,address,length)!=length) | ||
269 | return(-1); | ||
270 | |||
271 | return(0); | ||
272 | } | ||
273 | amb | 87 | |
274 | |||
275 | /*++++++++++++++++++++++++++++++++++++++ | ||
276 | amb | 251 | Read data from a file on disk. |
277 | |||
278 | int ReadFile Returns 0 if OK or something else in case of an error. | ||
279 | |||
280 | int fd The file descriptor to read from. | ||
281 | |||
282 | void *address The address of the data to be read into. | ||
283 | |||
284 | size_t length The length of data to read. | ||
285 | ++++++++++++++++++++++++++++++++++++++*/ | ||
286 | |||
287 | int ReadFile(int fd,void *address,size_t length) | ||
288 | { | ||
289 | /* Read the data */ | ||
290 | |||
291 | if(read(fd,address,length)!=length) | ||
292 | return(-1); | ||
293 | |||
294 | return(0); | ||
295 | } | ||
296 | |||
297 | |||
298 | /*++++++++++++++++++++++++++++++++++++++ | ||
299 | Seek to a position in a file on disk. | ||
300 | |||
301 | int SeekFile Returns 0 if OK or something else in case of an error. | ||
302 | |||
303 | int fd The file descriptor to seek within. | ||
304 | |||
305 | size_t position The position to seek to. | ||
306 | ++++++++++++++++++++++++++++++++++++++*/ | ||
307 | |||
308 | int SeekFile(int fd,size_t position) | ||
309 | { | ||
310 | /* Seek the data */ | ||
311 | |||
312 | if(lseek(fd,position,SEEK_SET)!=position) | ||
313 | return(-1); | ||
314 | |||
315 | return(0); | ||
316 | } | ||
317 | |||
318 | |||
319 | /*++++++++++++++++++++++++++++++++++++++ | ||
320 | amb | 97 | Close a file on disk. |
321 | amb | 87 | |
322 | amb | 97 | int fd The file descriptor to close. |
323 | amb | 87 | ++++++++++++++++++++++++++++++++++++++*/ |
324 | |||
325 | amb | 97 | void CloseFile(int fd) |
326 | amb | 87 | { |
327 | amb | 97 | close(fd); |
328 | amb | 87 | } |
329 | amb | 251 | |
330 | |||
331 | /*++++++++++++++++++++++++++++++++++++++ | ||
332 | Delete a file from disk. | ||
333 | |||
334 | int DeleteFile Returns 0 if OK or something else in case of an error. | ||
335 | |||
336 | char *filename The name of the file to delete. | ||
337 | ++++++++++++++++++++++++++++++++++++++*/ | ||
338 | |||
339 | int DeleteFile(char *filename) | ||
340 | { | ||
341 | unlink(filename); | ||
342 | |||
343 | return(0); | ||
344 | } |
Properties
Name | Value |
---|---|
cvs:description | File handling functions. |