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)
45 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
52 #include "gfileutils.h"
53 #include "gmappedfile.h"
55 #include "gmessages.h"
57 #include "gstrfuncs.h"
68 #define MAP_FAILED ((void *) -1)
74 * The #GMappedFile represents a file mapping created with
75 * g_mapped_file_new(). It has only private members and should
76 * not be accessed directly.
91 g_mapped_file_destroy (GMappedFile *file)
96 munmap (file->contents, file->length);
99 UnmapViewOfFile (file->contents);
100 CloseHandle (file->mapping);
104 g_slice_free (GMappedFile, file);
108 mapped_file_new_from_fd (int fd,
110 const gchar *filename,
116 file = g_slice_new0 (GMappedFile);
118 file->free_func = g_mapped_file_destroy;
120 if (fstat (fd, &st) == -1)
122 int save_errno = errno;
123 gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
127 g_file_error_from_errno (save_errno),
128 _("Failed to get attributes of file '%s%s%s%s': fstat() failed: %s"),
129 display_filename ? display_filename : "fd",
130 display_filename ? "' " : "",
131 display_filename ? display_filename : "",
132 display_filename ? "'" : "",
133 g_strerror (save_errno));
134 g_free (display_filename);
138 /* mmap() on size 0 will fail with EINVAL, so we avoid calling mmap()
139 * in that case -- but only if we have a regular file; we still want
140 * attempts to mmap a character device to fail, for example.
142 if (st.st_size == 0 && S_ISREG (st.st_mode))
145 file->contents = NULL;
149 file->contents = MAP_FAILED;
152 if (st.st_size > G_MAXSIZE)
158 file->length = (gsize) st.st_size;
159 file->contents = (gchar *) mmap (NULL, file->length,
160 writable ? PROT_READ|PROT_WRITE : PROT_READ,
165 file->length = st.st_size;
166 file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
167 writable ? PAGE_WRITECOPY : PAGE_READONLY,
170 if (file->mapping != NULL)
172 file->contents = MapViewOfFile (file->mapping,
173 writable ? FILE_MAP_COPY : FILE_MAP_READ,
176 if (file->contents == NULL)
178 file->contents = MAP_FAILED;
179 CloseHandle (file->mapping);
180 file->mapping = NULL;
186 if (file->contents == MAP_FAILED)
188 int save_errno = errno;
189 gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
193 g_file_error_from_errno (save_errno),
194 _("Failed to map %s%s%s%s: mmap() failed: %s"),
195 display_filename ? display_filename : "fd",
196 display_filename ? "' " : "",
197 display_filename ? display_filename : "",
198 display_filename ? "'" : "",
199 g_strerror (save_errno));
200 g_free (display_filename);
207 g_slice_free (GMappedFile, file);
214 * @filename: The path of the file to load, in the GLib filename encoding
215 * @writable: whether the mapping should be writable
216 * @error: return location for a #GError, or %NULL
218 * Maps a file into memory. On UNIX, this is using the mmap() function.
220 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
221 * it is an error to modify the mapped buffer. Modifications to the buffer
222 * are not visible to other processes mapping the same file, and are not
223 * written back to the file.
225 * Note that modifications of the underlying file might affect the contents
226 * of the #GMappedFile. Therefore, mapping should only be used if the file
227 * will not be modified, or if all modifications of the file are done
228 * atomically (e.g. using g_file_set_contents()).
230 * If @filename is the name of an empty, regular file, the function
231 * will successfully return an empty #GMappedFile. In other cases of
232 * size 0 (e.g. device files such as /dev/null), @error will be set
233 * to the #GFileError value #G_FILE_ERROR_INVAL.
235 * Return value: a newly allocated #GMappedFile which must be unref'd
236 * with g_mapped_file_unref(), or %NULL if the mapping failed.
241 g_mapped_file_new (const gchar *filename,
248 g_return_val_if_fail (filename != NULL, NULL);
249 g_return_val_if_fail (!error || *error == NULL, NULL);
251 fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
254 int save_errno = errno;
255 gchar *display_filename = g_filename_display_name (filename);
259 g_file_error_from_errno (save_errno),
260 _("Failed to open file '%s': open() failed: %s"),
262 g_strerror (save_errno));
263 g_free (display_filename);
267 file = mapped_file_new_from_fd (fd, writable, filename, error);
276 * g_mapped_file_new_from_fd:
277 * @fd: The file descriptor of the file to load
278 * @writable: whether the mapping should be writable
279 * @error: return location for a #GError, or %NULL
281 * Maps a file into memory. On UNIX, this is using the mmap() function.
283 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
284 * it is an error to modify the mapped buffer. Modifications to the buffer
285 * are not visible to other processes mapping the same file, and are not
286 * written back to the file.
288 * Note that modifications of the underlying file might affect the contents
289 * of the #GMappedFile. Therefore, mapping should only be used if the file
290 * will not be modified, or if all modifications of the file are done
291 * atomically (e.g. using g_file_set_contents()).
293 * Return value: a newly allocated #GMappedFile which must be unref'd
294 * with g_mapped_file_unref(), or %NULL if the mapping failed.
299 g_mapped_file_new_from_fd (gint fd,
303 return mapped_file_new_from_fd (fd, writable, NULL, error);
307 * g_mapped_file_get_length:
308 * @file: a #GMappedFile
310 * Returns the length of the contents of a #GMappedFile.
312 * Returns: the length of the contents of @file.
317 g_mapped_file_get_length (GMappedFile *file)
319 g_return_val_if_fail (file != NULL, 0);
325 * g_mapped_file_get_contents:
326 * @file: a #GMappedFile
328 * Returns the contents of a #GMappedFile.
330 * Note that the contents may not be zero-terminated,
331 * even if the #GMappedFile is backed by a text file.
333 * If the file is empty then %NULL is returned.
335 * Returns: the contents of @file, or %NULL.
340 g_mapped_file_get_contents (GMappedFile *file)
342 g_return_val_if_fail (file != NULL, NULL);
344 return file->contents;
348 * g_mapped_file_free:
349 * @file: a #GMappedFile
351 * This call existed before #GMappedFile had refcounting and is currently
352 * exactly the same as g_mapped_file_unref().
355 * Deprecated:2.22: Use g_mapped_file_unref() instead.
358 g_mapped_file_free (GMappedFile *file)
360 g_mapped_file_unref (file);
365 * @file: a #GMappedFile
367 * Increments the reference count of @file by one. It is safe to call
368 * this function from any thread.
370 * Return value: the passed in #GMappedFile.
375 g_mapped_file_ref (GMappedFile *file)
377 g_return_val_if_fail (file != NULL, NULL);
379 g_atomic_int_inc (&file->ref_count);
385 * g_mapped_file_unref:
386 * @file: a #GMappedFile
388 * Decrements the reference count of @file by one. If the reference count
389 * drops to 0, unmaps the buffer of @file and frees it.
391 * It is safe to call this function from any thread.
396 g_mapped_file_unref (GMappedFile *file)
398 g_return_if_fail (file != NULL);
400 if (g_atomic_int_dec_and_test (&file->ref_count))
401 g_mapped_file_destroy (file);
405 * g_mapped_file_get_bytes:
406 * @file: a #GMappedFile
408 * Creates a new #GBytes which references the data mapped from @file.
409 * The mapped contents of the file must not be modified after creating this
410 * bytes object, because a #GBytes should be immutable.
412 * Returns: (transfer full): A newly allocated #GBytes referencing data
418 g_mapped_file_get_bytes (GMappedFile *file)
420 g_return_val_if_fail (file != NULL, NULL);
422 return g_bytes_new_with_free_func (file->contents,
424 (GDestroyNotify) g_mapped_file_unref,
425 g_mapped_file_ref (file));