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 2 - (show annotations) (download) (as text)
Wed Dec 31 12:21:27 2008 UTC (16 years, 2 months ago) by amb
File MIME type: text/x-csrc
File size: 3253 byte(s)
Initial revision

1 /***************************************
2 $Header: /home/amb/CVS/routino/src/files.c,v 1.1 2008-12-31 12:20:07 amb Exp $
3
4 Functions to map a file into memory.
5 ******************/ /******************
6 Written by Andrew M. Bishop
7
8 This file Copyright 2008 Andrew M. Bishop
9 It may be distributed under the GNU Public License, version 2, or
10 any higher version. See section COPYING of the GNU Public license
11 for conditions under which this file may be redistributed.
12 ***************************************/
13
14
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/mman.h>
21
22 #include "functions.h"
23
24 /*+ A structure to contain the file mapping between file descriptor and address. +*/
25 struct filemapping
26 {
27 int fd; /*+ The file descriptor. +*/
28 void *address; /*+ The address the file is mapped to. +*/
29 size_t length; /*+ The length of the mapped file. +*/
30 };
31
32 /*+ The current set of file mappings. +*/
33 static struct filemapping *mappings=NULL;
34
35 /*+ The number of file mappings +*/
36 static int nmappings=0;
37
38
39 /*++++++++++++++++++++++++++++++++++++++
40 Open a file and map it into memory.
41
42 void *MapFile Returns the address of the file.
43
44 const char *filename The name of the file to open.
45 ++++++++++++++++++++++++++++++++++++++*/
46
47 void *MapFile(const char *filename)
48 {
49 int fd;
50 struct stat buf;
51 void *address;
52
53 /* Open the file */
54
55 fd=open(filename,O_RDONLY);
56
57 if(fd==-1)
58 return(NULL);
59
60 /* Get the length of the file */
61
62 if(fstat(fd,&buf))
63 {
64 close(fd);
65 return(NULL);
66 }
67
68 /* Map the file */
69
70 address=mmap(NULL,buf.st_size,PROT_READ,MAP_PRIVATE,fd,0);
71
72 if(!address)
73 {
74 close(fd);
75 return(NULL);
76 }
77
78 /* Store the file mapping */
79
80 nmappings++;
81 mappings=(struct filemapping*)realloc((void*)mappings,nmappings*sizeof(struct filemapping));
82
83 mappings[nmappings-1].fd=fd;
84 mappings[nmappings-1].address=address;
85 mappings[nmappings-1].length=buf.st_size;
86
87 return(address);
88 }
89
90
91 /*++++++++++++++++++++++++++++++++++++++
92 Unmap a file and close it.
93
94 void *address The address of the mapped file.
95 ++++++++++++++++++++++++++++++++++++++*/
96
97 void UnMapFile(void *address)
98 {
99 int i;
100
101 /* Search for the file mapping and unmap it and close it. */
102
103 for(i=0;i<nmappings;i++)
104 {
105 if(address==mappings[i].address)
106 {
107 munmap(mappings[i].address,mappings[i].length);
108
109 close(mappings[i].fd);
110
111 mappings[i].fd=-1;
112 mappings[i].address=NULL;
113 mappings[i].length=0;
114
115 break;
116 }
117 }
118 }
119
120
121 /*++++++++++++++++++++++++++++++++++++++
122 Write a new file to disk.
123
124 int WriteFile Returns 0 if OK or something else in case of an error.
125
126 const char *filename The name of the file to create.
127
128 void *address The address of the data to be written.
129
130 size_t length The length of data to write.
131 ++++++++++++++++++++++++++++++++++++++*/
132
133 int WriteFile(const char *filename,void *address,size_t length)
134 {
135 int fd;
136
137 /* Open the file */
138
139 fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP|S_IROTH);
140
141 if(fd==-1)
142 return(-1);
143
144 /* Write the data */
145
146 if(write(fd,address,length)!=length)
147 return(-1);
148
149 /* Close the file */
150
151 close(fd);
152
153 return(0);
154 }

Properties

Name Value
cvs:description File handling functions.