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"
41 #define fstat(a,b) _fstati64(a,b)
48 #include "gfileutils.h"
49 #include "gmappedfile.h"
51 #include "gmessages.h"
53 #include "gstrfuncs.h"
55 #include "gbufferprivate.h"
65 #define MAP_FAILED ((void *) -1)
79 /* Make sure the layout of GMappedFile is the same as GBuffer's */
80 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, contents) ==
81 G_STRUCT_OFFSET (GBuffer, data));
82 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, length) ==
83 G_STRUCT_OFFSET (GBuffer, size));
84 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, ref_count) ==
85 G_STRUCT_OFFSET (GBuffer, ref_count));
86 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, free_func) ==
87 G_STRUCT_OFFSET (GBuffer, free_func));
90 g_mapped_file_destroy (GMappedFile *file)
95 munmap (file->contents, file->length);
98 UnmapViewOfFile (file->contents);
99 CloseHandle (file->mapping);
103 g_slice_free (GMappedFile, file);
108 * @filename: The path of the file to load, in the GLib filename encoding
109 * @writable: whether the mapping should be writable
110 * @error: return location for a #GError, or %NULL
112 * Maps a file into memory. On UNIX, this is using the mmap() function.
114 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
115 * it is an error to modify the mapped buffer. Modifications to the buffer
116 * are not visible to other processes mapping the same file, and are not
117 * written back to the file.
119 * Note that modifications of the underlying file might affect the contents
120 * of the #GMappedFile. Therefore, mapping should only be used if the file
121 * will not be modified, or if all modifications of the file are done
122 * atomically (e.g. using g_file_set_contents()).
124 * If @filename is the name of an empty, regular file, the function
125 * will successfully return an empty #GMappedFile. In other cases of
126 * size 0 (e.g. device files such as /dev/null), @error will be set
127 * to the #GFileError value #G_FILE_ERROR_INVAL.
129 * Return value: a newly allocated #GMappedFile which must be unref'd
130 * with g_mapped_file_unref(), or %NULL if the mapping failed.
135 g_mapped_file_new (const gchar *filename,
143 g_return_val_if_fail (filename != NULL, NULL);
144 g_return_val_if_fail (!error || *error == NULL, NULL);
146 fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
149 int save_errno = errno;
150 gchar *display_filename = g_filename_display_name (filename);
154 g_file_error_from_errno (save_errno),
155 _("Failed to open file '%s': open() failed: %s"),
157 g_strerror (save_errno));
158 g_free (display_filename);
162 file = g_slice_new0 (GMappedFile);
164 file->free_func = g_mapped_file_destroy;
166 if (fstat (fd, &st) == -1)
168 int save_errno = errno;
169 gchar *display_filename = g_filename_display_name (filename);
173 g_file_error_from_errno (save_errno),
174 _("Failed to get attributes of file '%s': fstat() failed: %s"),
176 g_strerror (save_errno));
177 g_free (display_filename);
181 /* mmap() on size 0 will fail with EINVAL, so we avoid calling mmap()
182 * in that case -- but only if we have a regular file; we still want
183 * attempts to mmap a character device to fail, for example.
185 if (st.st_size == 0 && S_ISREG (st.st_mode))
188 file->contents = NULL;
193 file->contents = MAP_FAILED;
196 if (st.st_size > G_MAXSIZE)
202 file->length = (gsize) st.st_size;
203 file->contents = (gchar *) mmap (NULL, file->length,
204 writable ? PROT_READ|PROT_WRITE : PROT_READ,
209 file->length = st.st_size;
210 file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
211 writable ? PAGE_WRITECOPY : PAGE_READONLY,
214 if (file->mapping != NULL)
216 file->contents = MapViewOfFile (file->mapping,
217 writable ? FILE_MAP_COPY : FILE_MAP_READ,
220 if (file->contents == NULL)
222 file->contents = MAP_FAILED;
223 CloseHandle (file->mapping);
224 file->mapping = NULL;
230 if (file->contents == MAP_FAILED)
232 int save_errno = errno;
233 gchar *display_filename = g_filename_display_name (filename);
237 g_file_error_from_errno (save_errno),
238 _("Failed to map file '%s': mmap() failed: %s"),
240 g_strerror (save_errno));
241 g_free (display_filename);
250 g_slice_free (GMappedFile, file);
256 * g_mapped_file_get_length:
257 * @file: a #GMappedFile
259 * Returns the length of the contents of a #GMappedFile.
261 * Returns: the length of the contents of @file.
266 g_mapped_file_get_length (GMappedFile *file)
268 g_return_val_if_fail (file != NULL, 0);
274 * g_mapped_file_get_contents:
275 * @file: a #GMappedFile
277 * Returns the contents of a #GMappedFile.
279 * Note that the contents may not be zero-terminated,
280 * even if the #GMappedFile is backed by a text file.
282 * If the file is empty then %NULL is returned.
284 * Returns: the contents of @file, or %NULL.
289 g_mapped_file_get_contents (GMappedFile *file)
291 g_return_val_if_fail (file != NULL, NULL);
293 return file->contents;
297 * g_mapped_file_free:
298 * @file: a #GMappedFile
300 * This call existed before #GMappedFile had refcounting and is currently
301 * exactly the same as g_mapped_file_unref().
304 * Deprecated:2.22: Use g_mapped_file_unref() instead.
307 g_mapped_file_free (GMappedFile *file)
309 g_mapped_file_unref (file);
314 * @file: a #GMappedFile
316 * Increments the reference count of @file by one. It is safe to call
317 * this function from any thread.
319 * Return value: the passed in #GMappedFile.
324 g_mapped_file_ref (GMappedFile *file)
326 g_return_val_if_fail (file != NULL, NULL);
328 g_atomic_int_inc (&file->ref_count);
334 * g_mapped_file_unref:
335 * @file: a #GMappedFile
337 * Decrements the reference count of @file by one. If the reference count
338 * drops to 0, unmaps the buffer of @file and frees it.
340 * It is safe to call this function from any thread.
345 g_mapped_file_unref (GMappedFile *file)
347 g_return_if_fail (file != NULL);
349 if (g_atomic_int_dec_and_test (&file->ref_count))
350 g_mapped_file_destroy (file);