Report an error if the file is too large. (#315275, Kjartan Maraas)
[platform/upstream/glib.git] / glib / gmappedfile.c
1 /* GLIB - Library of useful routines for C programming
2  * gmappedfile.c: Simplified wrapper around the mmap() function.
3  *
4  * Copyright 2005 Matthias Clasen
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include <errno.h>
25 #include <sys/types.h> 
26 #include <sys/stat.h> 
27 #include <fcntl.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #ifdef HAVE_MMAP
32 #include <sys/mman.h>
33 #endif
34
35 #include "glibconfig.h"
36
37 #ifdef G_OS_WIN32
38 #include <windows.h>
39 #include <io.h>
40 #endif
41
42 #include "gconvert.h"
43 #include "gerror.h"
44 #include "gfileutils.h"
45 #include "gmappedfile.h"
46 #include "gmem.h"
47 #include "gmessages.h"
48 #include "gstdio.h"
49 #include "gstrfuncs.h"
50
51 #include "glibintl.h"
52
53 #include "galias.h"
54
55 #ifndef _O_BINARY
56 #define _O_BINARY 0
57 #endif
58
59 #ifndef MAP_FAILED
60 #define MAP_FAILED ((void *) -1)
61 #endif
62
63 struct _GMappedFile 
64 {
65   gsize  length;
66   gchar *contents;
67 #ifdef G_OS_WIN32
68   HANDLE mapping;
69 #endif
70 };
71
72 /**
73  * g_mapped_file_new:
74  * @filename: The path of the file to load, in the GLib filename encoding
75  * @writable: wether the mapping should be writable
76  * @error: return location for a #GError, or %NULL
77  *
78  * Maps a file into memory. On UNIX, this is using the mmap() function.
79  *
80  * If @writable is %TRUE, the mapped buffer may be modified, otherwise
81  * it is an error to modify the mapped buffer. Modifications to the buffer 
82  * are not visible to other processes mapping the same file, and are not 
83  * written back to the file.
84  *
85  * Note that modifications of the underlying file might affect the contents
86  * of the #GMappedFile. Therefore, mapping should only be used if the file 
87  * will not be modified, or if all modifications of the file are done
88  * atomically (e.g. using g_file_set_contents()). 
89  *
90  * Return value: a newly allocated #GMappedFile which must be freed
91  *    with g_mapped_file_free(), or %NULL if the mapping failed. 
92  *
93  * Since: 2.8
94  */
95 GMappedFile *
96 g_mapped_file_new (const gchar  *filename,
97                    gboolean      writable,
98                    GError      **error)
99 {
100   GMappedFile *file;
101   int fd;
102   struct stat st;
103
104   g_return_val_if_fail (filename != NULL, NULL);
105   g_return_val_if_fail (!error || *error == NULL, NULL);
106
107   fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
108   if (fd == -1)
109     {
110       int save_errno = errno;
111       gchar *display_filename = g_filename_display_name (filename);
112       
113       g_set_error (error,
114                    G_FILE_ERROR,
115                    g_file_error_from_errno (save_errno),
116                    _("Failed to open file '%s': open() failed: %s"),
117                    display_filename, 
118                    g_strerror (save_errno));
119       g_free (display_filename);
120       return NULL;
121     }
122
123   file = g_new0 (GMappedFile, 1);
124
125   if (fstat (fd, &st) == -1)
126     {
127       int save_errno = errno;
128       gchar *display_filename = g_filename_display_name (filename);
129
130       g_set_error (error,
131                    G_FILE_ERROR,
132                    g_file_error_from_errno (save_errno),
133                    _("Failed to get attributes of file '%s': fstat() failed: %s"),
134                    display_filename, 
135                    g_strerror (save_errno));
136       g_free (display_filename);
137       goto out;
138     }
139
140   file->contents = MAP_FAILED;
141
142 #ifdef HAVE_MMAP
143   if (st.st_size > G_MAXSIZE)
144     {
145       errno = EINVAL;
146     }
147   else
148     {      
149       file->length = (gsize) st.st_size;
150       file->contents = (gchar *) mmap (NULL,  file->length,
151                                        writable ? PROT_READ|PROT_WRITE : PROT_READ,
152                                        MAP_PRIVATE, fd, 0);
153     }
154 #endif
155 #ifdef G_OS_WIN32
156   file->length = st.st_size;
157   file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
158                                      writable ? PAGE_WRITECOPY : PAGE_READONLY,
159                                      0, 0,
160                                      NULL);
161   if (file->mapping != NULL)
162     {
163       file->contents = MapViewOfFile (file->mapping,
164                                       writable ? FILE_MAP_COPY : FILE_MAP_READ,
165                                       0, 0,
166                                       0);
167       if (file->contents == NULL)
168         {
169           file->contents = MAP_FAILED;
170           CloseHandle (file->mapping);
171           file->mapping = NULL;
172         }
173     }
174 #endif
175
176   
177   if (file->contents == MAP_FAILED)
178     {
179       int save_errno = errno;
180       gchar *display_filename = g_filename_display_name (filename);
181       
182       g_set_error (error,
183                    G_FILE_ERROR,
184                    g_file_error_from_errno (save_errno),
185                    _("Failed to map file '%s': mmap() failed: %s"),
186                    display_filename,
187                    g_strerror (save_errno));
188       g_free (display_filename);
189       goto out;
190     }
191
192   close (fd);
193   return file;
194
195  out:
196   close (fd);
197   g_free (file);
198
199   return NULL;
200 }
201
202 /**
203  * g_mapped_file_get_length:
204  * @file: a #GMappedFile
205  *
206  * Returns the length of the contents of a #GMappedFile.
207  *
208  * Returns: the length of the contents of @file.
209  *
210  * Since: 2.8
211  */
212 gsize
213 g_mapped_file_get_length (GMappedFile *file)
214 {
215   g_return_val_if_fail (file != NULL, 0);
216
217   return file->length;
218 }
219
220 /**
221  * g_mapped_file_get_contents:
222  * @file: a #GMappedFile
223  *
224  * Returns the contents of a #GMappedFile. 
225  *
226  * Note that the contents may not be zero-terminated,
227  * even if the #GMappedFile is backed by a text file.
228  *
229  * Returns: the contents of @file.
230  *
231  * Since: 2.8
232  */
233 gchar *
234 g_mapped_file_get_contents (GMappedFile *file)
235 {
236   g_return_val_if_fail (file != NULL, NULL);
237
238   return file->contents;
239 }
240
241 /**
242  * g_mapped_file_free:
243  * @file: a #GMappedFile
244  *
245  * Unmaps the buffer of @file and frees it. 
246  *
247  * Since: 2.8
248  */
249 void
250 g_mapped_file_free (GMappedFile *file)
251 {
252   g_return_if_fail (file != NULL);
253
254 #ifdef HAVE_MMAP
255   munmap (file->contents, file->length);
256 #endif
257 #ifdef G_OS_WIN32
258   UnmapViewOfFile (file->contents);
259   CloseHandle (file->mapping);
260 #endif
261
262   g_free (file);
263 }
264
265
266 #define __G_MAPPED_FILE_C__
267 #include "galiasdef.c"