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)
48 #include "gfileutils.h"
49 #include "gmappedfile.h"
51 #include "gmessages.h"
53 #include "gstrfuncs.h"
55 #include "gbufferprivate.h"
65 #define MAP_FAILED ((void *) -1)
79 /* Make sure the layout of GMappedFile is the same as GBuffer's */
80 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, contents) ==
81 G_STRUCT_OFFSET (GBuffer, data));
82 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, length) ==
83 G_STRUCT_OFFSET (GBuffer, size));
84 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, ref_count) ==
85 G_STRUCT_OFFSET (GBuffer, ref_count));
86 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, free_func) ==
87 G_STRUCT_OFFSET (GBuffer, free_func));
90 g_mapped_file_destroy (GMappedFile *file)
95 munmap (file->contents, file->length);
98 UnmapViewOfFile (file->contents);
99 CloseHandle (file->mapping);
103 g_slice_free (GMappedFile, file);
107 mapped_file_new_from_fd (int fd,
109 const gchar *filename,
115 file = g_slice_new0 (GMappedFile);
117 file->free_func = g_mapped_file_destroy;
119 if (fstat (fd, &st) == -1)
121 int save_errno = errno;
122 gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
126 g_file_error_from_errno (save_errno),
127 _("Failed to get attributes of file '%s%s%s%s': fstat() failed: %s"),
128 display_filename ? display_filename : "fd",
129 display_filename ? "' " : "",
130 display_filename ? display_filename : "",
131 display_filename ? "'" : "",
132 g_strerror (save_errno));
133 g_free (display_filename);
137 /* mmap() on size 0 will fail with EINVAL, so we avoid calling mmap()
138 * in that case -- but only if we have a regular file; we still want
139 * attempts to mmap a character device to fail, for example.
141 if (st.st_size == 0 && S_ISREG (st.st_mode))
144 file->contents = NULL;
148 file->contents = MAP_FAILED;
151 if (st.st_size > G_MAXSIZE)
157 file->length = (gsize) st.st_size;
158 file->contents = (gchar *) mmap (NULL, file->length,
159 writable ? PROT_READ|PROT_WRITE : PROT_READ,
164 file->length = st.st_size;
165 file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
166 writable ? PAGE_WRITECOPY : PAGE_READONLY,
169 if (file->mapping != NULL)
171 file->contents = MapViewOfFile (file->mapping,
172 writable ? FILE_MAP_COPY : FILE_MAP_READ,
175 if (file->contents == NULL)
177 file->contents = MAP_FAILED;
178 CloseHandle (file->mapping);
179 file->mapping = NULL;
185 if (file->contents == MAP_FAILED)
187 int save_errno = errno;
188 gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
192 g_file_error_from_errno (save_errno),
193 _("Failed to map %s%s%s%s: mmap() failed: %s"),
194 display_filename ? display_filename : "fd",
195 display_filename ? "' " : "",
196 display_filename ? display_filename : "",
197 display_filename ? "'" : "",
198 g_strerror (save_errno));
199 g_free (display_filename);
206 g_slice_free (GMappedFile, file);
213 * @filename: The path of the file to load, in the GLib filename encoding
214 * @writable: whether the mapping should be writable
215 * @error: return location for a #GError, or %NULL
217 * Maps a file into memory. On UNIX, this is using the mmap() function.
219 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
220 * it is an error to modify the mapped buffer. Modifications to the buffer
221 * are not visible to other processes mapping the same file, and are not
222 * written back to the file.
224 * Note that modifications of the underlying file might affect the contents
225 * of the #GMappedFile. Therefore, mapping should only be used if the file
226 * will not be modified, or if all modifications of the file are done
227 * atomically (e.g. using g_file_set_contents()).
229 * If @filename is the name of an empty, regular file, the function
230 * will successfully return an empty #GMappedFile. In other cases of
231 * size 0 (e.g. device files such as /dev/null), @error will be set
232 * to the #GFileError value #G_FILE_ERROR_INVAL.
234 * Return value: a newly allocated #GMappedFile which must be unref'd
235 * with g_mapped_file_unref(), or %NULL if the mapping failed.
240 g_mapped_file_new (const gchar *filename,
247 g_return_val_if_fail (filename != NULL, NULL);
248 g_return_val_if_fail (!error || *error == NULL, NULL);
250 fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
253 int save_errno = errno;
254 gchar *display_filename = g_filename_display_name (filename);
258 g_file_error_from_errno (save_errno),
259 _("Failed to open file '%s': open() failed: %s"),
261 g_strerror (save_errno));
262 g_free (display_filename);
266 file = mapped_file_new_from_fd (fd, writable, filename, error);
275 * g_mapped_file_new_from_fd:
276 * @fd: The file descriptor of the file to load
277 * @writable: whether the mapping should be writable
278 * @error: return location for a #GError, or %NULL
280 * Maps a file into memory. On UNIX, this is using the mmap() function.
282 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
283 * it is an error to modify the mapped buffer. Modifications to the buffer
284 * are not visible to other processes mapping the same file, and are not
285 * written back to the file.
287 * Note that modifications of the underlying file might affect the contents
288 * of the #GMappedFile. Therefore, mapping should only be used if the file
289 * will not be modified, or if all modifications of the file are done
290 * atomically (e.g. using g_file_set_contents()).
292 * Return value: a newly allocated #GMappedFile which must be unref'd
293 * with g_mapped_file_unref(), or %NULL if the mapping failed.
298 g_mapped_file_new_from_fd (gint fd,
302 return mapped_file_new_from_fd (fd, writable, NULL, error);
306 * g_mapped_file_get_length:
307 * @file: a #GMappedFile
309 * Returns the length of the contents of a #GMappedFile.
311 * Returns: the length of the contents of @file.
316 g_mapped_file_get_length (GMappedFile *file)
318 g_return_val_if_fail (file != NULL, 0);
324 * g_mapped_file_get_contents:
325 * @file: a #GMappedFile
327 * Returns the contents of a #GMappedFile.
329 * Note that the contents may not be zero-terminated,
330 * even if the #GMappedFile is backed by a text file.
332 * If the file is empty then %NULL is returned.
334 * Returns: the contents of @file, or %NULL.
339 g_mapped_file_get_contents (GMappedFile *file)
341 g_return_val_if_fail (file != NULL, NULL);
343 return file->contents;
347 * g_mapped_file_free:
348 * @file: a #GMappedFile
350 * This call existed before #GMappedFile had refcounting and is currently
351 * exactly the same as g_mapped_file_unref().
354 * Deprecated:2.22: Use g_mapped_file_unref() instead.
357 g_mapped_file_free (GMappedFile *file)
359 g_mapped_file_unref (file);
364 * @file: a #GMappedFile
366 * Increments the reference count of @file by one. It is safe to call
367 * this function from any thread.
369 * Return value: the passed in #GMappedFile.
374 g_mapped_file_ref (GMappedFile *file)
376 g_return_val_if_fail (file != NULL, NULL);
378 g_atomic_int_inc (&file->ref_count);
384 * g_mapped_file_unref:
385 * @file: a #GMappedFile
387 * Decrements the reference count of @file by one. If the reference count
388 * drops to 0, unmaps the buffer of @file and frees it.
390 * It is safe to call this function from any thread.
395 g_mapped_file_unref (GMappedFile *file)
397 g_return_if_fail (file != NULL);
399 if (g_atomic_int_dec_and_test (&file->ref_count))
400 g_mapped_file_destroy (file);