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, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
30 #include "glibconfig.h"
41 #define fstat(a,b) _fstati64(a,b)
46 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
53 #include "gfileutils.h"
54 #include "gmappedfile.h"
56 #include "gmessages.h"
58 #include "gstrfuncs.h"
69 #define MAP_FAILED ((void *) -1)
75 * The #GMappedFile represents a file mapping created with
76 * g_mapped_file_new(). It has only private members and should
77 * not be accessed directly.
92 g_mapped_file_destroy (GMappedFile *file)
97 munmap (file->contents, file->length);
100 UnmapViewOfFile (file->contents);
101 CloseHandle (file->mapping);
105 g_slice_free (GMappedFile, file);
109 mapped_file_new_from_fd (int fd,
111 const gchar *filename,
117 file = g_slice_new0 (GMappedFile);
119 file->free_func = g_mapped_file_destroy;
121 if (fstat (fd, &st) == -1)
123 int save_errno = errno;
124 gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
128 g_file_error_from_errno (save_errno),
129 _("Failed to get attributes of file '%s%s%s%s': fstat() failed: %s"),
130 display_filename ? display_filename : "fd",
131 display_filename ? "' " : "",
132 display_filename ? display_filename : "",
133 display_filename ? "'" : "",
134 g_strerror (save_errno));
135 g_free (display_filename);
139 /* mmap() on size 0 will fail with EINVAL, so we avoid calling mmap()
140 * in that case -- but only if we have a regular file; we still want
141 * attempts to mmap a character device to fail, for example.
143 if (st.st_size == 0 && S_ISREG (st.st_mode))
146 file->contents = NULL;
150 file->contents = MAP_FAILED;
153 if (st.st_size > G_MAXSIZE)
159 file->length = (gsize) st.st_size;
160 file->contents = (gchar *) mmap (NULL, file->length,
161 writable ? PROT_READ|PROT_WRITE : PROT_READ,
166 file->length = st.st_size;
167 file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
168 writable ? PAGE_WRITECOPY : PAGE_READONLY,
171 if (file->mapping != NULL)
173 file->contents = MapViewOfFile (file->mapping,
174 writable ? FILE_MAP_COPY : FILE_MAP_READ,
177 if (file->contents == NULL)
179 file->contents = MAP_FAILED;
180 CloseHandle (file->mapping);
181 file->mapping = NULL;
187 if (file->contents == MAP_FAILED)
189 int save_errno = errno;
190 gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
194 g_file_error_from_errno (save_errno),
195 _("Failed to map %s%s%s%s: mmap() failed: %s"),
196 display_filename ? display_filename : "fd",
197 display_filename ? "' " : "",
198 display_filename ? display_filename : "",
199 display_filename ? "'" : "",
200 g_strerror (save_errno));
201 g_free (display_filename);
208 g_slice_free (GMappedFile, file);
215 * @filename: The path of the file to load, in the GLib filename encoding
216 * @writable: whether the mapping should be writable
217 * @error: return location for a #GError, or %NULL
219 * Maps a file into memory. On UNIX, this is using the mmap() function.
221 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
222 * it is an error to modify the mapped buffer. Modifications to the buffer
223 * are not visible to other processes mapping the same file, and are not
224 * written back to the file.
226 * Note that modifications of the underlying file might affect the contents
227 * of the #GMappedFile. Therefore, mapping should only be used if the file
228 * will not be modified, or if all modifications of the file are done
229 * atomically (e.g. using g_file_set_contents()).
231 * If @filename is the name of an empty, regular file, the function
232 * will successfully return an empty #GMappedFile. In other cases of
233 * size 0 (e.g. device files such as /dev/null), @error will be set
234 * to the #GFileError value #G_FILE_ERROR_INVAL.
236 * Returns: a newly allocated #GMappedFile which must be unref'd
237 * with g_mapped_file_unref(), or %NULL if the mapping failed.
242 g_mapped_file_new (const gchar *filename,
249 g_return_val_if_fail (filename != NULL, NULL);
250 g_return_val_if_fail (!error || *error == NULL, NULL);
252 fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
255 int save_errno = errno;
256 gchar *display_filename = g_filename_display_name (filename);
260 g_file_error_from_errno (save_errno),
261 _("Failed to open file '%s': open() failed: %s"),
263 g_strerror (save_errno));
264 g_free (display_filename);
268 file = mapped_file_new_from_fd (fd, writable, filename, error);
277 * g_mapped_file_new_from_fd:
278 * @fd: The file descriptor of the file to load
279 * @writable: whether the mapping should be writable
280 * @error: return location for a #GError, or %NULL
282 * Maps a file into memory. On UNIX, this is using the mmap() function.
284 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
285 * it is an error to modify the mapped buffer. Modifications to the buffer
286 * are not visible to other processes mapping the same file, and are not
287 * written back to the file.
289 * Note that modifications of the underlying file might affect the contents
290 * of the #GMappedFile. Therefore, mapping should only be used if the file
291 * will not be modified, or if all modifications of the file are done
292 * atomically (e.g. using g_file_set_contents()).
294 * Returns: a newly allocated #GMappedFile which must be unref'd
295 * with g_mapped_file_unref(), or %NULL if the mapping failed.
300 g_mapped_file_new_from_fd (gint fd,
304 return mapped_file_new_from_fd (fd, writable, NULL, error);
308 * g_mapped_file_get_length:
309 * @file: a #GMappedFile
311 * Returns the length of the contents of a #GMappedFile.
313 * Returns: the length of the contents of @file.
318 g_mapped_file_get_length (GMappedFile *file)
320 g_return_val_if_fail (file != NULL, 0);
326 * g_mapped_file_get_contents:
327 * @file: a #GMappedFile
329 * Returns the contents of a #GMappedFile.
331 * Note that the contents may not be zero-terminated,
332 * even if the #GMappedFile is backed by a text file.
334 * If the file is empty then %NULL is returned.
336 * Returns: the contents of @file, or %NULL.
341 g_mapped_file_get_contents (GMappedFile *file)
343 g_return_val_if_fail (file != NULL, NULL);
345 return file->contents;
349 * g_mapped_file_free:
350 * @file: a #GMappedFile
352 * This call existed before #GMappedFile had refcounting and is currently
353 * exactly the same as g_mapped_file_unref().
356 * Deprecated:2.22: Use g_mapped_file_unref() instead.
359 g_mapped_file_free (GMappedFile *file)
361 g_mapped_file_unref (file);
366 * @file: a #GMappedFile
368 * Increments the reference count of @file by one. It is safe to call
369 * this function from any thread.
371 * Returns: the passed in #GMappedFile.
376 g_mapped_file_ref (GMappedFile *file)
378 g_return_val_if_fail (file != NULL, NULL);
380 g_atomic_int_inc (&file->ref_count);
386 * g_mapped_file_unref:
387 * @file: a #GMappedFile
389 * Decrements the reference count of @file by one. If the reference count
390 * drops to 0, unmaps the buffer of @file and frees it.
392 * It is safe to call this function from any thread.
397 g_mapped_file_unref (GMappedFile *file)
399 g_return_if_fail (file != NULL);
401 if (g_atomic_int_dec_and_test (&file->ref_count))
402 g_mapped_file_destroy (file);
406 * g_mapped_file_get_bytes:
407 * @file: a #GMappedFile
409 * Creates a new #GBytes which references the data mapped from @file.
410 * The mapped contents of the file must not be modified after creating this
411 * bytes object, because a #GBytes should be immutable.
413 * Returns: (transfer full): A newly allocated #GBytes referencing data
419 g_mapped_file_get_bytes (GMappedFile *file)
421 g_return_val_if_fail (file != NULL, NULL);
423 return g_bytes_new_with_free_func (file->contents,
425 (GDestroyNotify) g_mapped_file_unref,
426 g_mapped_file_ref (file));