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 326 - (show 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)
Allow planetsplitter to be run with a --parse-only or --process-only option and
append to existing file or read from existing file.

1 /***************************************
2 $Header: /home/amb/CVS/routino/src/files.c,v 1.15 2010-03-19 19:47:09 amb Exp $
3
4 Functions to handle files.
5
6 Part of the Routino routing software.
7 ******************/ /******************
8 This file Copyright 2008-2010 Andrew M. Bishop
9
10 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 ***************************************/
23
24
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31 #include <sys/types.h>
32
33 #include "functions.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 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 Open a file and map it into memory.
76
77 void *MapFile Returns the address of the file.
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 struct stat buf;
86 void *address;
87
88 /* Open the file */
89
90 fd=open(filename,O_RDONLY);
91
92 if(fd<0)
93 {
94 fprintf(stderr,"Cannot open file '%s' to read.\n",filename);
95 exit(EXIT_FAILURE);
96 }
97
98 /* Get the length of the file */
99
100 if(fstat(fd,&buf))
101 {
102 close(fd);
103
104 fprintf(stderr,"Cannot stat file '%s'.\n",filename);
105 exit(EXIT_FAILURE);
106 }
107
108 /* Map the file */
109
110 address=mmap(NULL,buf.st_size,PROT_READ,MAP_SHARED,fd,0);
111
112 if(address==MAP_FAILED)
113 {
114 close(fd);
115
116 fprintf(stderr,"Cannot mmap file '%s'.\n",filename);
117 exit(EXIT_FAILURE);
118 }
119
120 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 return(address);
130 }
131
132
133 /*++++++++++++++++++++++++++++++++++++++
134 Unmap a file and optionally delete it.
135
136 void *UnmapFile Returns NULL (for similarity to the MapFile function).
137
138 const char *filename The name of the file when it was opened.
139 ++++++++++++++++++++++++++++++++++++++*/
140
141 void *UnmapFile(const char *filename)
142 {
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
170 return(NULL);
171 }
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 const char *filename The name of the file to create.
180 ++++++++++++++++++++++++++++++++++++++*/
181
182 int OpenFile(const char *filename)
183 {
184 int fd;
185
186 /* Open the file */
187
188 fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
189
190 if(fd<0)
191 {
192 fprintf(stderr,"Cannot open file '%s' to write.\n",filename);
193 exit(EXIT_FAILURE);
194 }
195
196 return(fd);
197 }
198
199
200 /*++++++++++++++++++++++++++++++++++++++
201 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 Open an existing file on disk for reading from.
228
229 int ReOpenFile Returns the file descriptor if OK or something negative in case of an error.
230
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 Write data to a file on disk.
254
255 int WriteFile Returns 0 if OK or something else in case of an error.
256
257 int fd The file descriptor to write to.
258
259 const void *address The address of the data to be written from.
260
261 size_t length The length of data to write.
262 ++++++++++++++++++++++++++++++++++++++*/
263
264 int WriteFile(int fd,const void *address,size_t length)
265 {
266 /* Write the data */
267
268 if(write(fd,address,length)!=length)
269 return(-1);
270
271 return(0);
272 }
273
274
275 /*++++++++++++++++++++++++++++++++++++++
276 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 Close a file on disk.
321
322 int fd The file descriptor to close.
323 ++++++++++++++++++++++++++++++++++++++*/
324
325 void CloseFile(int fd)
326 {
327 close(fd);
328 }
329
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.