If a character can't be converted, don't replace it with a NUL byte, but
[glib.git] / glib / gmappedfile.c
blobd3d4de8bc2b2a309f0e6c7a97dca7d27408652e1
1 /* GLIB - Library of useful routines for C programming
2 * gmappedfile.c: Simplified wrapper around the mmap() function.
4 * Copyright 2005 Matthias Clasen
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.
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.
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.
22 #include "config.h"
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
35 #include "glibconfig.h"
37 #ifdef G_OS_WIN32
38 #include <windows.h>
39 #include <io.h>
40 #endif
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"
51 #include "glibintl.h"
53 #include "galias.h"
55 #ifndef _O_BINARY
56 #define _O_BINARY 0
57 #endif
59 #ifndef MAP_FAILED
60 #define MAP_FAILED ((void *) -1)
61 #endif
63 struct _GMappedFile
65 gsize length;
66 gchar *contents;
67 #ifdef G_OS_WIN32
68 HANDLE mapping;
69 #endif
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
78 * Maps a file into memory. On UNIX, this is using the mmap() function.
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.
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()).
90 * Return value: a newly allocated #GMappedFile which must be freed
91 * with g_mapped_file_free(), or %NULL if the mapping failed.
93 * Since: 2.8
95 GMappedFile *
96 g_mapped_file_new (const gchar *filename,
97 gboolean writable,
98 GError **error)
100 GMappedFile *file;
101 int fd;
102 struct stat st;
104 g_return_val_if_fail (filename != NULL, NULL);
105 g_return_val_if_fail (!error || *error == NULL, NULL);
107 fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
108 if (fd == -1)
110 int save_errno = errno;
111 gchar *display_filename = g_filename_display_name (filename);
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;
123 file = g_new0 (GMappedFile, 1);
125 if (fstat (fd, &st) == -1)
127 int save_errno = errno;
128 gchar *display_filename = g_filename_display_name (filename);
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;
140 if (st.st_size == 0)
142 file->length = 0;
143 file->contents = "";
144 close (fd);
145 return file;
148 file->contents = MAP_FAILED;
150 #ifdef HAVE_MMAP
151 if (st.st_size > G_MAXSIZE)
153 errno = EINVAL;
155 else
157 file->length = (gsize) st.st_size;
158 file->contents = (gchar *) mmap (NULL, file->length,
159 writable ? PROT_READ|PROT_WRITE : PROT_READ,
160 MAP_PRIVATE, fd, 0);
162 #endif
163 #ifdef G_OS_WIN32
164 file->length = st.st_size;
165 file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
166 writable ? PAGE_WRITECOPY : PAGE_READONLY,
167 0, 0,
168 NULL);
169 if (file->mapping != NULL)
171 file->contents = MapViewOfFile (file->mapping,
172 writable ? FILE_MAP_COPY : FILE_MAP_READ,
173 0, 0,
175 if (file->contents == NULL)
177 file->contents = MAP_FAILED;
178 CloseHandle (file->mapping);
179 file->mapping = NULL;
182 #endif
185 if (file->contents == MAP_FAILED)
187 int save_errno = errno;
188 gchar *display_filename = g_filename_display_name (filename);
190 g_set_error (error,
191 G_FILE_ERROR,
192 g_file_error_from_errno (save_errno),
193 _("Failed to map file '%s': mmap() failed: %s"),
194 display_filename,
195 g_strerror (save_errno));
196 g_free (display_filename);
197 goto out;
200 close (fd);
201 return file;
203 out:
204 close (fd);
205 g_free (file);
207 return NULL;
211 * g_mapped_file_get_length:
212 * @file: a #GMappedFile
214 * Returns the length of the contents of a #GMappedFile.
216 * Returns: the length of the contents of @file.
218 * Since: 2.8
220 gsize
221 g_mapped_file_get_length (GMappedFile *file)
223 g_return_val_if_fail (file != NULL, 0);
225 return file->length;
229 * g_mapped_file_get_contents:
230 * @file: a #GMappedFile
232 * Returns the contents of a #GMappedFile.
234 * Note that the contents may not be zero-terminated,
235 * even if the #GMappedFile is backed by a text file.
237 * Returns: the contents of @file.
239 * Since: 2.8
241 gchar *
242 g_mapped_file_get_contents (GMappedFile *file)
244 g_return_val_if_fail (file != NULL, NULL);
246 return file->contents;
250 * g_mapped_file_free:
251 * @file: a #GMappedFile
253 * Unmaps the buffer of @file and frees it.
255 * Since: 2.8
257 void
258 g_mapped_file_free (GMappedFile *file)
260 g_return_if_fail (file != NULL);
262 if (file->length)
264 #ifdef HAVE_MMAP
265 munmap (file->contents, file->length);
266 #endif
267 #ifdef G_OS_WIN32
268 UnmapViewOfFile (file->contents);
269 CloseHandle (file->mapping);
270 #endif
273 g_free (file);
277 #define __G_MAPPED_FILE_C__
278 #include "galiasdef.c"