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.
25 #include <sys/types.h>
35 #include "glibconfig.h"
42 #define fstat(a,b) _fstat(a,b)
49 #include "gfileutils.h"
50 #include "gmappedfile.h"
52 #include "gmessages.h"
54 #include "gstrfuncs.h"
65 #define MAP_FAILED ((void *) -1)
79 * @filename: The path of the file to load, in the GLib filename encoding
80 * @writable: whether the mapping should be writable
81 * @error: return location for a #GError, or %NULL
83 * Maps a file into memory. On UNIX, this is using the mmap() function.
85 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
86 * it is an error to modify the mapped buffer. Modifications to the buffer
87 * are not visible to other processes mapping the same file, and are not
88 * written back to the file.
90 * Note that modifications of the underlying file might affect the contents
91 * of the #GMappedFile. Therefore, mapping should only be used if the file
92 * will not be modified, or if all modifications of the file are done
93 * atomically (e.g. using g_file_set_contents()).
95 * Return value: a newly allocated #GMappedFile which must be freed
96 * with g_mapped_file_free(), or %NULL if the mapping failed.
101 g_mapped_file_new (const gchar *filename,
109 g_return_val_if_fail (filename != NULL, NULL);
110 g_return_val_if_fail (!error || *error == NULL, NULL);
112 fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
115 int save_errno = errno;
116 gchar *display_filename = g_filename_display_name (filename);
120 g_file_error_from_errno (save_errno),
121 _("Failed to open file '%s': open() failed: %s"),
123 g_strerror (save_errno));
124 g_free (display_filename);
128 file = g_new0 (GMappedFile, 1);
130 if (fstat (fd, &st) == -1)
132 int save_errno = errno;
133 gchar *display_filename = g_filename_display_name (filename);
137 g_file_error_from_errno (save_errno),
138 _("Failed to get attributes of file '%s': fstat() failed: %s"),
140 g_strerror (save_errno));
141 g_free (display_filename);
153 file->contents = MAP_FAILED;
156 if (st.st_size > G_MAXSIZE)
162 file->length = (gsize) st.st_size;
163 file->contents = (gchar *) mmap (NULL, file->length,
164 writable ? PROT_READ|PROT_WRITE : PROT_READ,
169 file->length = st.st_size;
170 file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
171 writable ? PAGE_WRITECOPY : PAGE_READONLY,
174 if (file->mapping != NULL)
176 file->contents = MapViewOfFile (file->mapping,
177 writable ? FILE_MAP_COPY : FILE_MAP_READ,
180 if (file->contents == NULL)
182 file->contents = MAP_FAILED;
183 CloseHandle (file->mapping);
184 file->mapping = NULL;
190 if (file->contents == MAP_FAILED)
192 int save_errno = errno;
193 gchar *display_filename = g_filename_display_name (filename);
197 g_file_error_from_errno (save_errno),
198 _("Failed to map file '%s': mmap() failed: %s"),
200 g_strerror (save_errno));
201 g_free (display_filename);
216 * g_mapped_file_get_length:
217 * @file: a #GMappedFile
219 * Returns the length of the contents of a #GMappedFile.
221 * Returns: the length of the contents of @file.
226 g_mapped_file_get_length (GMappedFile *file)
228 g_return_val_if_fail (file != NULL, 0);
234 * g_mapped_file_get_contents:
235 * @file: a #GMappedFile
237 * Returns the contents of a #GMappedFile.
239 * Note that the contents may not be zero-terminated,
240 * even if the #GMappedFile is backed by a text file.
242 * Returns: the contents of @file.
247 g_mapped_file_get_contents (GMappedFile *file)
249 g_return_val_if_fail (file != NULL, NULL);
251 return file->contents;
255 * g_mapped_file_free:
256 * @file: a #GMappedFile
258 * Unmaps the buffer of @file and frees it.
263 g_mapped_file_free (GMappedFile *file)
265 g_return_if_fail (file != NULL);
270 munmap (file->contents, file->length);
273 UnmapViewOfFile (file->contents);
274 CloseHandle (file->mapping);
282 #define __G_MAPPED_FILE_C__
283 #include "galiasdef.c"