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 251 - (show annotations) (download) (as text)
Sat Sep 5 09:36:57 2009 UTC (15 years, 6 months ago) by amb
File MIME type: text/x-csrc
File size: 6898 byte(s)
Add some more file functions.

1 /***************************************
2 $Header: /home/amb/CVS/routino/src/files.c,v 1.11 2009-09-05 09:36:57 amb Exp $
3
4 Functions to map a file into memory.
5
6 Part of the Routino routing software.
7 ******************/ /******************
8 This file Copyright 2008,2009 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 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 /*++++++++++++++++++++++++++++++++++++++
49 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 Open a file and map it into memory.
72
73 void *MapFile Returns the address of the file.
74
75 const char *filename The name of the file to open.
76 ++++++++++++++++++++++++++++++++++++++*/
77
78 void *MapFile(const char *filename)
79 {
80 int fd;
81 struct stat buf;
82 void *address;
83
84 /* Open the file */
85
86 fd=open(filename,O_RDONLY);
87
88 if(fd<0)
89 {
90 fprintf(stderr,"Cannot open file '%s' to read.\n",filename);
91 exit(EXIT_FAILURE);
92 }
93
94 /* Get the length of the file */
95
96 if(fstat(fd,&buf))
97 {
98 close(fd);
99
100 fprintf(stderr,"Cannot stat file '%s'.\n",filename);
101 exit(EXIT_FAILURE);
102 }
103
104 /* Map the file */
105
106 address=mmap(NULL,buf.st_size,PROT_READ,MAP_SHARED,fd,0);
107
108 if(address==MAP_FAILED)
109 {
110 close(fd);
111
112 fprintf(stderr,"Cannot mmap file '%s'.\n",filename);
113 exit(EXIT_FAILURE);
114 }
115
116 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 return(address);
126 }
127
128
129 /*++++++++++++++++++++++++++++++++++++++
130 Unmap a file and optionally delete it.
131
132 const char *filename The name of the file when it was opened.
133
134 int delete If set then delete the file.
135 ++++++++++++++++++++++++++++++++++++++*/
136
137 void UnmapFile(const char *filename,int delete)
138 {
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 /* Delete the file */
160
161 if(delete)
162 unlink(filename);
163
164 /* Shuffle the list of files */
165
166 nmappedfiles--;
167
168 if(nmappedfiles>i)
169 memmove(&mappedfiles[i],&mappedfiles[i+1],(nmappedfiles-i)*sizeof(struct mmapinfo));
170 }
171
172
173 /*++++++++++++++++++++++++++++++++++++++
174 Open a new file on disk for writing to.
175
176 int OpenFile Returns the file descriptor if OK or something negative in case of an error.
177
178 const char *filename The name of the file to create.
179 ++++++++++++++++++++++++++++++++++++++*/
180
181 int OpenFile(const char *filename)
182 {
183 int fd;
184
185 /* Open the file */
186
187 fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
188
189 if(fd<0)
190 {
191 fprintf(stderr,"Cannot open file '%s' to write.\n",filename);
192 exit(EXIT_FAILURE);
193 }
194
195 return(fd);
196 }
197
198
199 /*++++++++++++++++++++++++++++++++++++++
200 Open an existing file on disk for reading from.
201
202 int OpenFile Returns the file descriptor if OK or something negative in case of an error.
203
204 const char *filename The name of the file to open.
205 ++++++++++++++++++++++++++++++++++++++*/
206
207 int ReOpenFile(const char *filename)
208 {
209 int fd;
210
211 /* Open the file */
212
213 fd=open(filename,O_RDONLY);
214
215 if(fd<0)
216 {
217 fprintf(stderr,"Cannot open file '%s' to read.\n",filename);
218 exit(EXIT_FAILURE);
219 }
220
221 return(fd);
222 }
223
224
225 /*++++++++++++++++++++++++++++++++++++++
226 Write data to a file on disk.
227
228 int WriteFile Returns 0 if OK or something else in case of an error.
229
230 int fd The file descriptor to write to.
231
232 void *address The address of the data to be written from.
233
234 size_t length The length of data to write.
235 ++++++++++++++++++++++++++++++++++++++*/
236
237 int WriteFile(int fd,void *address,size_t length)
238 {
239 /* Write the data */
240
241 if(write(fd,address,length)!=length)
242 return(-1);
243
244 return(0);
245 }
246
247
248 /*++++++++++++++++++++++++++++++++++++++
249 Read data from a file on disk.
250
251 int ReadFile Returns 0 if OK or something else in case of an error.
252
253 int fd The file descriptor to read from.
254
255 void *address The address of the data to be read into.
256
257 size_t length The length of data to read.
258 ++++++++++++++++++++++++++++++++++++++*/
259
260 int ReadFile(int fd,void *address,size_t length)
261 {
262 /* Read the data */
263
264 if(read(fd,address,length)!=length)
265 return(-1);
266
267 return(0);
268 }
269
270
271 /*++++++++++++++++++++++++++++++++++++++
272 Seek to a position in a file on disk.
273
274 int SeekFile Returns 0 if OK or something else in case of an error.
275
276 int fd The file descriptor to seek within.
277
278 size_t position The position to seek to.
279 ++++++++++++++++++++++++++++++++++++++*/
280
281 int SeekFile(int fd,size_t position)
282 {
283 /* Seek the data */
284
285 if(lseek(fd,position,SEEK_SET)!=position)
286 return(-1);
287
288 return(0);
289 }
290
291
292 /*++++++++++++++++++++++++++++++++++++++
293 Close a file on disk.
294
295 int fd The file descriptor to close.
296 ++++++++++++++++++++++++++++++++++++++*/
297
298 void CloseFile(int fd)
299 {
300 close(fd);
301 }
302
303
304 /*++++++++++++++++++++++++++++++++++++++
305 Delete a file from disk.
306
307 int DeleteFile Returns 0 if OK or something else in case of an error.
308
309 char *filename The name of the file to delete.
310 ++++++++++++++++++++++++++++++++++++++*/
311
312 int DeleteFile(char *filename)
313 {
314 unlink(filename);
315
316 return(0);
317 }

Properties

Name Value
cvs:description File handling functions.