1 /* GLIB - Library of useful routines for C programming
2 * gmappedfile.c: Simplified wrapper around the mmap() function.
4 * Copyright 2005 Matthias Clasen
6 * SPDX-License-Identifier: LGPL-2.1-or-later
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include <sys/types.h>
32 #include "glibconfig.h"
43 #define fstat(a,b) _fstati64(a,b)
48 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
55 #include "gfileutils.h"
56 #include "gmappedfile.h"
58 #include "gmessages.h"
60 #include "gstrfuncs.h"
71 #define MAP_FAILED ((void *) -1)
77 * The #GMappedFile represents a file mapping created with
78 * g_mapped_file_new(). It has only private members and should
79 * not be accessed directly.
94 g_mapped_file_destroy (GMappedFile *file)
99 munmap (file->contents, file->length);
102 UnmapViewOfFile (file->contents);
103 CloseHandle (file->mapping);
107 g_slice_free (GMappedFile, file);
111 mapped_file_new_from_fd (int fd,
113 const gchar *filename,
119 file = g_slice_new0 (GMappedFile);
121 file->free_func = g_mapped_file_destroy;
123 if (fstat (fd, &st) == -1)
125 int save_errno = errno;
126 gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
130 g_file_error_from_errno (save_errno),
131 _("Failed to get attributes of file ā%s%s%s%sā: fstat() failed: %s"),
132 display_filename ? display_filename : "fd",
133 display_filename ? "' " : "",
134 display_filename ? display_filename : "",
135 display_filename ? "'" : "",
136 g_strerror (save_errno));
137 g_free (display_filename);
141 /* mmap() on size 0 will fail with EINVAL, so we avoid calling mmap()
142 * in that case -- but only if we have a regular file; we still want
143 * attempts to mmap a character device to fail, for example.
145 if (st.st_size == 0 && S_ISREG (st.st_mode))
148 file->contents = NULL;
152 file->contents = MAP_FAILED;
155 if (sizeof (st.st_size) > sizeof (gsize) && st.st_size > (off_t) G_MAXSIZE)
161 file->length = (gsize) st.st_size;
162 file->contents = (gchar *) mmap (NULL, file->length,
163 writable ? PROT_READ|PROT_WRITE : PROT_READ,
168 file->length = st.st_size;
169 file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
170 writable ? PAGE_WRITECOPY : PAGE_READONLY,
173 if (file->mapping != NULL)
175 file->contents = MapViewOfFile (file->mapping,
176 writable ? FILE_MAP_COPY : FILE_MAP_READ,
179 if (file->contents == NULL)
181 file->contents = MAP_FAILED;
182 CloseHandle (file->mapping);
183 file->mapping = NULL;
189 if (file->contents == MAP_FAILED)
191 int save_errno = errno;
192 gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
196 g_file_error_from_errno (save_errno),
197 _("Failed to map %s%s%s%s: mmap() failed: %s"),
198 display_filename ? display_filename : "fd",
199 display_filename ? "' " : "",
200 display_filename ? display_filename : "",
201 display_filename ? "'" : "",
202 g_strerror (save_errno));
203 g_free (display_filename);
210 g_slice_free (GMappedFile, file);
217 * @filename: (type filename): The path of the file to load, in the GLib
219 * @writable: whether the mapping should be writable
220 * @error: return location for a #GError, or %NULL
222 * Maps a file into memory. On UNIX, this is using the mmap() function.
224 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
225 * it is an error to modify the mapped buffer. Modifications to the buffer
226 * are not visible to other processes mapping the same file, and are not
227 * written back to the file.
229 * Note that modifications of the underlying file might affect the contents
230 * of the #GMappedFile. Therefore, mapping should only be used if the file
231 * will not be modified, or if all modifications of the file are done
232 * atomically (e.g. using g_file_set_contents()).
234 * If @filename is the name of an empty, regular file, the function
235 * will successfully return an empty #GMappedFile. In other cases of
236 * size 0 (e.g. device files such as /dev/null), @error will be set
237 * to the #GFileError value %G_FILE_ERROR_INVAL.
239 * Returns: a newly allocated #GMappedFile which must be unref'd
240 * with g_mapped_file_unref(), or %NULL if the mapping failed.
245 g_mapped_file_new (const gchar *filename,
252 g_return_val_if_fail (filename != NULL, NULL);
253 g_return_val_if_fail (!error || *error == NULL, NULL);
255 fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
258 int save_errno = errno;
259 gchar *display_filename = g_filename_display_name (filename);
263 g_file_error_from_errno (save_errno),
264 _("Failed to open file ā%sā: open() failed: %s"),
266 g_strerror (save_errno));
267 g_free (display_filename);
271 file = mapped_file_new_from_fd (fd, writable, filename, error);
280 * g_mapped_file_new_from_fd:
281 * @fd: The file descriptor of the file to load
282 * @writable: whether the mapping should be writable
283 * @error: return location for a #GError, or %NULL
285 * Maps a file into memory. On UNIX, this is using the mmap() function.
287 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
288 * it is an error to modify the mapped buffer. Modifications to the buffer
289 * are not visible to other processes mapping the same file, and are not
290 * written back to the file.
292 * Note that modifications of the underlying file might affect the contents
293 * of the #GMappedFile. Therefore, mapping should only be used if the file
294 * will not be modified, or if all modifications of the file are done
295 * atomically (e.g. using g_file_set_contents()).
297 * Returns: a newly allocated #GMappedFile which must be unref'd
298 * with g_mapped_file_unref(), or %NULL if the mapping failed.
303 g_mapped_file_new_from_fd (gint fd,
307 return mapped_file_new_from_fd (fd, writable, NULL, error);
311 * g_mapped_file_get_length:
312 * @file: a #GMappedFile
314 * Returns the length of the contents of a #GMappedFile.
316 * Returns: the length of the contents of @file.
321 g_mapped_file_get_length (GMappedFile *file)
323 g_return_val_if_fail (file != NULL, 0);
329 * g_mapped_file_get_contents:
330 * @file: a #GMappedFile
332 * Returns the contents of a #GMappedFile.
334 * Note that the contents may not be zero-terminated,
335 * even if the #GMappedFile is backed by a text file.
337 * If the file is empty then %NULL is returned.
339 * Returns: the contents of @file, or %NULL.
344 g_mapped_file_get_contents (GMappedFile *file)
346 g_return_val_if_fail (file != NULL, NULL);
348 return file->contents;
352 * g_mapped_file_free:
353 * @file: a #GMappedFile
355 * This call existed before #GMappedFile had refcounting and is currently
356 * exactly the same as g_mapped_file_unref().
359 * Deprecated:2.22: Use g_mapped_file_unref() instead.
362 g_mapped_file_free (GMappedFile *file)
364 g_mapped_file_unref (file);
369 * @file: a #GMappedFile
371 * Increments the reference count of @file by one. It is safe to call
372 * this function from any thread.
374 * Returns: the passed in #GMappedFile.
379 g_mapped_file_ref (GMappedFile *file)
381 g_return_val_if_fail (file != NULL, NULL);
383 g_atomic_int_inc (&file->ref_count);
389 * g_mapped_file_unref:
390 * @file: a #GMappedFile
392 * Decrements the reference count of @file by one. If the reference count
393 * drops to 0, unmaps the buffer of @file and frees it.
395 * It is safe to call this function from any thread.
400 g_mapped_file_unref (GMappedFile *file)
402 g_return_if_fail (file != NULL);
404 if (g_atomic_int_dec_and_test (&file->ref_count))
405 g_mapped_file_destroy (file);
409 * g_mapped_file_get_bytes:
410 * @file: a #GMappedFile
412 * Creates a new #GBytes which references the data mapped from @file.
413 * The mapped contents of the file must not be modified after creating this
414 * bytes object, because a #GBytes should be immutable.
416 * Returns: (transfer full): A newly allocated #GBytes referencing data
422 g_mapped_file_get_bytes (GMappedFile *file)
424 g_return_val_if_fail (file != NULL, NULL);
426 return g_bytes_new_with_free_func (file->contents,
428 (GDestroyNotify) g_mapped_file_unref,
429 g_mapped_file_ref (file));