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 449 - (show annotations) (download) (as text)
Mon Jul 12 17:59:42 2010 UTC (14 years, 8 months ago) by amb
File MIME type: text/x-csrc
File size: 7061 byte(s)
Create a files.h header and put some of the most heavily used files.c functions
into it and make them inline.

1 /***************************************
2 $Header: /home/amb/CVS/routino/src/files.c,v 1.20 2010-07-12 17:59:41 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 <stdio.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <sys/stat.h>
32 #include <sys/mman.h>
33 #include <sys/types.h>
34
35 #include "files.h"
36
37
38 /*+ A structure to contain the list of memory mapped files. +*/
39 struct mmapinfo
40 {
41 const char *filename; /*+ The name of the file (the index of the list). +*/
42 int fd; /*+ The file descriptor used when it was opened. +*/
43 void *address; /*+ The address the file was mapped to. +*/
44 size_t length; /*+ The length of the file. +*/
45 };
46
47 /*+ The list of memory mapped files. +*/
48 static struct mmapinfo *mappedfiles;
49
50 /*+ The number of mapped files. +*/
51 static int nmappedfiles=0;
52
53
54 /*++++++++++++++++++++++++++++++++++++++
55 Return a filename composed of the dirname, prefix and filename.
56
57 char *FileName Returns an allocated filename.
58
59 const char *dirname The directory name.
60
61 const char *prefix The file prefix.
62
63 const char *name The filename.
64 ++++++++++++++++++++++++++++++++++++++*/
65
66 char *FileName(const char *dirname,const char *prefix, const char *name)
67 {
68 char *filename=(char*)malloc((dirname?strlen(dirname):0)+1+(prefix?strlen(prefix):0)+1+strlen(name));
69
70 sprintf(filename,"%s%s%s%s%s",dirname?dirname:"",dirname?"/":"",prefix?prefix:"",prefix?"-":"",name);
71
72 return(filename);
73 }
74
75
76 /*++++++++++++++++++++++++++++++++++++++
77 Open a file and map it into memory.
78
79 void *MapFile Returns the address of the file or exits in case of an error.
80
81 const char *filename The name of the file to open.
82 ++++++++++++++++++++++++++++++++++++++*/
83
84 void *MapFile(const char *filename)
85 {
86 int fd;
87 off_t size;
88 void *address;
89
90 /* Open the file and get its size */
91
92 fd=ReOpenFile(filename);
93
94 size=SizeFile(filename);
95
96 /* Map the file */
97
98 address=mmap(NULL,size,PROT_READ,MAP_SHARED,fd,0);
99
100 if(address==MAP_FAILED)
101 {
102 close(fd);
103
104 fprintf(stderr,"Cannot mmap file '%s' [%s].\n",filename,strerror(errno));
105 exit(EXIT_FAILURE);
106 }
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 Unmap a file.
123
124 void *UnmapFile Returns NULL (for similarity to the MapFile function).
125
126 const char *filename The name of the file when it was opened.
127 ++++++++++++++++++++++++++++++++++++++*/
128
129 void *UnmapFile(const char *filename)
130 {
131 int i;
132
133 for(i=0;i<nmappedfiles;i++)
134 if(!strcmp(mappedfiles[i].filename,filename))
135 break;
136
137 if(i==nmappedfiles)
138 {
139 fprintf(stderr,"The file '%s' was not mapped using MapFile().\n",filename);
140 exit(EXIT_FAILURE);
141 }
142
143 /* Close the file */
144
145 close(mappedfiles[i].fd);
146
147 /* Unmap the file */
148
149 munmap(mappedfiles[i].address,mappedfiles[i].length);
150
151 /* Shuffle the list of files */
152
153 nmappedfiles--;
154
155 if(nmappedfiles>i)
156 memmove(&mappedfiles[i],&mappedfiles[i+1],(nmappedfiles-i)*sizeof(struct mmapinfo));
157
158 return(NULL);
159 }
160
161
162 /*++++++++++++++++++++++++++++++++++++++
163 Open a new file on disk for writing to.
164
165 int OpenFile Returns the file descriptor if OK or exits in case of an error.
166
167 const char *filename The name of the file to create.
168 ++++++++++++++++++++++++++++++++++++++*/
169
170 int OpenFile(const char *filename)
171 {
172 int fd;
173
174 /* Open the file */
175
176 fd=open(filename,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
177
178 if(fd<0)
179 {
180 fprintf(stderr,"Cannot open file '%s' for writing [%s].\n",filename,strerror(errno));
181 exit(EXIT_FAILURE);
182 }
183
184 return(fd);
185 }
186
187
188 /*++++++++++++++++++++++++++++++++++++++
189 Open a new file on disk for reading and appending.
190
191 int AppendFile Returns the file descriptor if OK or exits in case of an error.
192
193 const char *filename The name of the file to create.
194 ++++++++++++++++++++++++++++++++++++++*/
195
196 int AppendFile(const char *filename)
197 {
198 int fd;
199
200 /* Open the file */
201
202 fd=open(filename,O_RDWR|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
203
204 if(fd<0)
205 {
206 fprintf(stderr,"Cannot open file '%s' for appending [%s].\n",filename,strerror(errno));
207 exit(EXIT_FAILURE);
208 }
209
210 return(fd);
211 }
212
213
214 /*++++++++++++++++++++++++++++++++++++++
215 Open an existing file on disk for reading from.
216
217 int ReOpenFile Returns the file descriptor if OK or exits in case of an error.
218
219 const char *filename The name of the file to open.
220 ++++++++++++++++++++++++++++++++++++++*/
221
222 int ReOpenFile(const char *filename)
223 {
224 int fd;
225
226 /* Open the file */
227
228 fd=open(filename,O_RDONLY);
229
230 if(fd<0)
231 {
232 fprintf(stderr,"Cannot open file '%s' for reading [%s].\n",filename,strerror(errno));
233 exit(EXIT_FAILURE);
234 }
235
236 return(fd);
237 }
238
239
240 /*++++++++++++++++++++++++++++++++++++++
241 Get the size of a file.
242
243 off_t SizeFile Returns the file size.
244
245 const char *filename The name of the file to check.
246 ++++++++++++++++++++++++++++++++++++++*/
247
248 off_t SizeFile(const char *filename)
249 {
250 struct stat buf;
251
252 if(stat(filename,&buf))
253 {
254 fprintf(stderr,"Cannot stat file '%s' [%s].\n",filename,strerror(errno));
255 exit(EXIT_FAILURE);
256 }
257
258 return(buf.st_size);
259 }
260
261
262 /*++++++++++++++++++++++++++++++++++++++
263 Check if a file exists.
264
265 int ExistsFile Returns 1 if the file exists and 0 if not.
266
267 const char *filename The name of the file to check.
268 ++++++++++++++++++++++++++++++++++++++*/
269
270 int ExistsFile(const char *filename)
271 {
272 struct stat buf;
273
274 if(stat(filename,&buf))
275 return(0);
276 else
277 return(1);
278 }
279
280
281 /*++++++++++++++++++++++++++++++++++++++
282 Close a file on disk.
283
284 int fd The file descriptor to close.
285 ++++++++++++++++++++++++++++++++++++++*/
286
287 void CloseFile(int fd)
288 {
289 close(fd);
290 }
291
292
293 /*++++++++++++++++++++++++++++++++++++++
294 Delete a file from disk.
295
296 int DeleteFile Returns 0 if OK or something else in case of an error.
297
298 char *filename The name of the file to delete.
299 ++++++++++++++++++++++++++++++++++++++*/
300
301 int DeleteFile(char *filename)
302 {
303 unlink(filename);
304
305 return(0);
306 }

Properties

Name Value
cvs:description File handling functions.