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"
59 #include "gbufferprivate.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.
91 /* Make sure the layout of GMappedFile is the same as GBuffer's */
92 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, contents) ==
93 G_STRUCT_OFFSET (GBuffer, data));
94 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, length) ==
95 G_STRUCT_OFFSET (GBuffer, size));
96 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, ref_count) ==
97 G_STRUCT_OFFSET (GBuffer, ref_count));
98 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, free_func) ==
99 G_STRUCT_OFFSET (GBuffer, free_func));
102 g_mapped_file_destroy (GMappedFile *file)
107 munmap (file->contents, file->length);
110 UnmapViewOfFile (file->contents);
111 CloseHandle (file->mapping);
115 g_slice_free (GMappedFile, file);
119 mapped_file_new_from_fd (int fd,
121 const gchar *filename,
127 file = g_slice_new0 (GMappedFile);
129 file->free_func = g_mapped_file_destroy;
131 if (fstat (fd, &st) == -1)
133 int save_errno = errno;
134 gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
138 g_file_error_from_errno (save_errno),
139 _("Failed to get attributes of file '%s%s%s%s': fstat() failed: %s"),
140 display_filename ? display_filename : "fd",
141 display_filename ? "' " : "",
142 display_filename ? display_filename : "",
143 display_filename ? "'" : "",
144 g_strerror (save_errno));
145 g_free (display_filename);
149 /* mmap() on size 0 will fail with EINVAL, so we avoid calling mmap()
150 * in that case -- but only if we have a regular file; we still want
151 * attempts to mmap a character device to fail, for example.
153 if (st.st_size == 0 && S_ISREG (st.st_mode))
156 file->contents = NULL;
160 file->contents = MAP_FAILED;
163 if (st.st_size > G_MAXSIZE)
169 file->length = (gsize) st.st_size;
170 file->contents = (gchar *) mmap (NULL, file->length,
171 writable ? PROT_READ|PROT_WRITE : PROT_READ,
176 file->length = st.st_size;
177 file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
178 writable ? PAGE_WRITECOPY : PAGE_READONLY,
181 if (file->mapping != NULL)
183 file->contents = MapViewOfFile (file->mapping,
184 writable ? FILE_MAP_COPY : FILE_MAP_READ,
187 if (file->contents == NULL)
189 file->contents = MAP_FAILED;
190 CloseHandle (file->mapping);
191 file->mapping = NULL;
197 if (file->contents == MAP_FAILED)
199 int save_errno = errno;
200 gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
204 g_file_error_from_errno (save_errno),
205 _("Failed to map %s%s%s%s: mmap() failed: %s"),
206 display_filename ? display_filename : "fd",
207 display_filename ? "' " : "",
208 display_filename ? display_filename : "",
209 display_filename ? "'" : "",
210 g_strerror (save_errno));
211 g_free (display_filename);
218 g_slice_free (GMappedFile, file);
225 * @filename: The path of the file to load, in the GLib filename encoding
226 * @writable: whether the mapping should be writable
227 * @error: return location for a #GError, or %NULL
229 * Maps a file into memory. On UNIX, this is using the mmap() function.
231 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
232 * it is an error to modify the mapped buffer. Modifications to the buffer
233 * are not visible to other processes mapping the same file, and are not
234 * written back to the file.
236 * Note that modifications of the underlying file might affect the contents
237 * of the #GMappedFile. Therefore, mapping should only be used if the file
238 * will not be modified, or if all modifications of the file are done
239 * atomically (e.g. using g_file_set_contents()).
241 * If @filename is the name of an empty, regular file, the function
242 * will successfully return an empty #GMappedFile. In other cases of
243 * size 0 (e.g. device files such as /dev/null), @error will be set
244 * to the #GFileError value #G_FILE_ERROR_INVAL.
246 * Return value: a newly allocated #GMappedFile which must be unref'd
247 * with g_mapped_file_unref(), or %NULL if the mapping failed.
252 g_mapped_file_new (const gchar *filename,
259 g_return_val_if_fail (filename != NULL, NULL);
260 g_return_val_if_fail (!error || *error == NULL, NULL);
262 fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
265 int save_errno = errno;
266 gchar *display_filename = g_filename_display_name (filename);
270 g_file_error_from_errno (save_errno),
271 _("Failed to open file '%s': open() failed: %s"),
273 g_strerror (save_errno));
274 g_free (display_filename);
278 file = mapped_file_new_from_fd (fd, writable, filename, error);
287 * g_mapped_file_new_from_fd:
288 * @fd: The file descriptor of the file to load
289 * @writable: whether the mapping should be writable
290 * @error: return location for a #GError, or %NULL
292 * Maps a file into memory. On UNIX, this is using the mmap() function.
294 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
295 * it is an error to modify the mapped buffer. Modifications to the buffer
296 * are not visible to other processes mapping the same file, and are not
297 * written back to the file.
299 * Note that modifications of the underlying file might affect the contents
300 * of the #GMappedFile. Therefore, mapping should only be used if the file
301 * will not be modified, or if all modifications of the file are done
302 * atomically (e.g. using g_file_set_contents()).
304 * Return value: a newly allocated #GMappedFile which must be unref'd
305 * with g_mapped_file_unref(), or %NULL if the mapping failed.
310 g_mapped_file_new_from_fd (gint fd,
314 return mapped_file_new_from_fd (fd, writable, NULL, error);
318 * g_mapped_file_get_length:
319 * @file: a #GMappedFile
321 * Returns the length of the contents of a #GMappedFile.
323 * Returns: the length of the contents of @file.
328 g_mapped_file_get_length (GMappedFile *file)
330 g_return_val_if_fail (file != NULL, 0);
336 * g_mapped_file_get_contents:
337 * @file: a #GMappedFile
339 * Returns the contents of a #GMappedFile.
341 * Note that the contents may not be zero-terminated,
342 * even if the #GMappedFile is backed by a text file.
344 * If the file is empty then %NULL is returned.
346 * Returns: the contents of @file, or %NULL.
351 g_mapped_file_get_contents (GMappedFile *file)
353 g_return_val_if_fail (file != NULL, NULL);
355 return file->contents;
359 * g_mapped_file_free:
360 * @file: a #GMappedFile
362 * This call existed before #GMappedFile had refcounting and is currently
363 * exactly the same as g_mapped_file_unref().
366 * Deprecated:2.22: Use g_mapped_file_unref() instead.
369 g_mapped_file_free (GMappedFile *file)
371 g_mapped_file_unref (file);
376 * @file: a #GMappedFile
378 * Increments the reference count of @file by one. It is safe to call
379 * this function from any thread.
381 * Return value: the passed in #GMappedFile.
386 g_mapped_file_ref (GMappedFile *file)
388 g_return_val_if_fail (file != NULL, NULL);
390 g_atomic_int_inc (&file->ref_count);
396 * g_mapped_file_unref:
397 * @file: a #GMappedFile
399 * Decrements the reference count of @file by one. If the reference count
400 * drops to 0, unmaps the buffer of @file and frees it.
402 * It is safe to call this function from any thread.
407 g_mapped_file_unref (GMappedFile *file)
409 g_return_if_fail (file != NULL);
411 if (g_atomic_int_dec_and_test (&file->ref_count))
412 g_mapped_file_destroy (file);