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"
44 #include "gfileutils.h"
45 #include "gmappedfile.h"
47 #include "gmessages.h"
49 #include "gstrfuncs.h"
60 #define MAP_FAILED ((void *) -1)
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.
96 g_mapped_file_new (const gchar *filename,
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);
110 int save_errno = errno;
111 gchar *display_filename = g_filename_display_name (filename);
115 g_file_error_from_errno (save_errno),
116 _("Failed to open file '%s': open() failed: %s"),
118 g_strerror (save_errno));
119 g_free (display_filename);
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);
132 g_file_error_from_errno (save_errno),
133 _("Failed to get attributes of file '%s': fstat() failed: %s"),
135 g_strerror (save_errno));
136 g_free (display_filename);
148 file->contents = MAP_FAILED;
151 if (st.st_size > G_MAXSIZE)
157 file->length = (gsize) st.st_size;
158 file->contents = (gchar *) mmap (NULL, file->length,
159 writable ? PROT_READ|PROT_WRITE : PROT_READ,
164 file->length = st.st_size;
165 file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
166 writable ? PAGE_WRITECOPY : PAGE_READONLY,
169 if (file->mapping != NULL)
171 file->contents = MapViewOfFile (file->mapping,
172 writable ? FILE_MAP_COPY : FILE_MAP_READ,
175 if (file->contents == NULL)
177 file->contents = MAP_FAILED;
178 CloseHandle (file->mapping);
179 file->mapping = NULL;
185 if (file->contents == MAP_FAILED)
187 int save_errno = errno;
188 gchar *display_filename = g_filename_display_name (filename);
192 g_file_error_from_errno (save_errno),
193 _("Failed to map file '%s': mmap() failed: %s"),
195 g_strerror (save_errno));
196 g_free (display_filename);
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.
221 g_mapped_file_get_length (GMappedFile *file)
223 g_return_val_if_fail (file != NULL, 0);
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.
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.
258 g_mapped_file_free (GMappedFile *file)
260 g_return_if_fail (file != NULL);
265 munmap (file->contents, file->length);
268 UnmapViewOfFile (file->contents);
269 CloseHandle (file->mapping);
277 #define __G_MAPPED_FILE_C__
278 #include "galiasdef.c"