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>
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 (st.st_size > 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: The path of the file to load, in the GLib filename encoding
218 * @writable: whether the mapping should be writable
219 * @error: return location for a #GError, or %NULL
221 * Maps a file into memory. On UNIX, this is using the mmap() function.
223 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
224 * it is an error to modify the mapped buffer. Modifications to the buffer
225 * are not visible to other processes mapping the same file, and are not
226 * written back to the file.
228 * Note that modifications of the underlying file might affect the contents
229 * of the #GMappedFile. Therefore, mapping should only be used if the file
230 * will not be modified, or if all modifications of the file are done
231 * atomically (e.g. using g_file_set_contents()).
233 * If @filename is the name of an empty, regular file, the function
234 * will successfully return an empty #GMappedFile. In other cases of
235 * size 0 (e.g. device files such as /dev/null), @error will be set
236 * to the #GFileError value #G_FILE_ERROR_INVAL.
238 * Return value: a newly allocated #GMappedFile which must be unref'd
239 * with g_mapped_file_unref(), or %NULL if the mapping failed.
244 g_mapped_file_new (const gchar *filename,
251 g_return_val_if_fail (filename != NULL, NULL);
252 g_return_val_if_fail (!error || *error == NULL, NULL);
254 fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
257 int save_errno = errno;
258 gchar *display_filename = g_filename_display_name (filename);
262 g_file_error_from_errno (save_errno),
263 _("Failed to open file '%s': open() failed: %s"),
265 g_strerror (save_errno));
266 g_free (display_filename);
270 file = mapped_file_new_from_fd (fd, writable, filename, error);
279 * g_mapped_file_new_from_fd:
280 * @fd: The file descriptor of the file to load
281 * @writable: whether the mapping should be writable
282 * @error: return location for a #GError, or %NULL
284 * Maps a file into memory. On UNIX, this is using the mmap() function.
286 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
287 * it is an error to modify the mapped buffer. Modifications to the buffer
288 * are not visible to other processes mapping the same file, and are not
289 * written back to the file.
291 * Note that modifications of the underlying file might affect the contents
292 * of the #GMappedFile. Therefore, mapping should only be used if the file
293 * will not be modified, or if all modifications of the file are done
294 * atomically (e.g. using g_file_set_contents()).
296 * Return value: a newly allocated #GMappedFile which must be unref'd
297 * with g_mapped_file_unref(), or %NULL if the mapping failed.
302 g_mapped_file_new_from_fd (gint fd,
306 return mapped_file_new_from_fd (fd, writable, NULL, error);
310 * g_mapped_file_get_length:
311 * @file: a #GMappedFile
313 * Returns the length of the contents of a #GMappedFile.
315 * Returns: the length of the contents of @file.
320 g_mapped_file_get_length (GMappedFile *file)
322 g_return_val_if_fail (file != NULL, 0);
328 * g_mapped_file_get_contents:
329 * @file: a #GMappedFile
331 * Returns the contents of a #GMappedFile.
333 * Note that the contents may not be zero-terminated,
334 * even if the #GMappedFile is backed by a text file.
336 * If the file is empty then %NULL is returned.
338 * Returns: the contents of @file, or %NULL.
343 g_mapped_file_get_contents (GMappedFile *file)
345 g_return_val_if_fail (file != NULL, NULL);
347 return file->contents;
351 * g_mapped_file_free:
352 * @file: a #GMappedFile
354 * This call existed before #GMappedFile had refcounting and is currently
355 * exactly the same as g_mapped_file_unref().
358 * Deprecated:2.22: Use g_mapped_file_unref() instead.
361 g_mapped_file_free (GMappedFile *file)
363 g_mapped_file_unref (file);
368 * @file: a #GMappedFile
370 * Increments the reference count of @file by one. It is safe to call
371 * this function from any thread.
373 * Return value: the passed in #GMappedFile.
378 g_mapped_file_ref (GMappedFile *file)
380 g_return_val_if_fail (file != NULL, NULL);
382 g_atomic_int_inc (&file->ref_count);
388 * g_mapped_file_unref:
389 * @file: a #GMappedFile
391 * Decrements the reference count of @file by one. If the reference count
392 * drops to 0, unmaps the buffer of @file and frees it.
394 * It is safe to call this function from any thread.
399 g_mapped_file_unref (GMappedFile *file)
401 g_return_if_fail (file != NULL);
403 if (g_atomic_int_dec_and_test (&file->ref_count))
404 g_mapped_file_destroy (file);
408 * g_mapped_file_get_bytes:
409 * @file: a #GMappedFile
411 * Creates a new #GBytes which references the data mapped from @file.
412 * The mapped contents of the file must not be modified after creating this
413 * bytes object, because a #GBytes should be immutable.
415 * Returns: (transfer full): A newly allocated #GBytes referencing data
421 g_mapped_file_get_bytes (GMappedFile *file)
423 g_return_val_if_fail (file != NULL, NULL);
425 return g_bytes_new_with_free_func (file->contents,
427 (GDestroyNotify) g_mapped_file_unref,
428 g_mapped_file_ref (file));