More updates
[glib.git] / glib / gmappedfile.c
blob2af6630a331d411d6607eea0173f22078cbaa561
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.
22 #include "config.h"
24 #include <errno.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #ifdef HAVE_MMAP
32 #include <sys/mman.h>
33 #endif
35 #include "glibconfig.h"
37 #ifdef G_OS_WIN32
38 #include <windows.h>
39 #include <io.h>
41 #define fstat(a,b) _fstati64(a,b)
42 #define stat _stati64
44 #endif
46 #include "gconvert.h"
47 #include "gerror.h"
48 #include "gfileutils.h"
49 #include "gmappedfile.h"
50 #include "gmem.h"
51 #include "gmessages.h"
52 #include "gstdio.h"
53 #include "gstrfuncs.h"
54 #include "gatomic.h"
55 #include "gbuffer.h"
57 #include "glibintl.h"
60 #ifndef _O_BINARY
61 #define _O_BINARY 0
62 #endif
64 #ifndef MAP_FAILED
65 #define MAP_FAILED ((void *) -1)
66 #endif
68 struct _GMappedFile
70 gchar *contents;
71 gsize length;
72 gpointer free_func;
73 int ref_count;
74 #ifdef G_OS_WIN32
75 HANDLE mapping;
76 #endif
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));
89 static void
90 g_mapped_file_destroy (GMappedFile *file)
92 if (file->length)
94 #ifdef HAVE_MMAP
95 munmap (file->contents, file->length);
96 #endif
97 #ifdef G_OS_WIN32
98 UnmapViewOfFile (file->contents);
99 CloseHandle (file->mapping);
100 #endif
103 g_slice_free (GMappedFile, file);
107 * g_mapped_file_new:
108 * @filename: The path of the file to load, in the GLib filename encoding
109 * @writable: whether the mapping should be writable
110 * @error: return location for a #GError, or %NULL
112 * Maps a file into memory. On UNIX, this is using the mmap() function.
114 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
115 * it is an error to modify the mapped buffer. Modifications to the buffer
116 * are not visible to other processes mapping the same file, and are not
117 * written back to the file.
119 * Note that modifications of the underlying file might affect the contents
120 * of the #GMappedFile. Therefore, mapping should only be used if the file
121 * will not be modified, or if all modifications of the file are done
122 * atomically (e.g. using g_file_set_contents()).
124 * Return value: a newly allocated #GMappedFile which must be unref'd
125 * with g_mapped_file_unref(), or %NULL if the mapping failed.
127 * Since: 2.8
129 GMappedFile *
130 g_mapped_file_new (const gchar *filename,
131 gboolean writable,
132 GError **error)
134 GMappedFile *file;
135 int fd;
136 struct stat st;
138 g_return_val_if_fail (filename != NULL, NULL);
139 g_return_val_if_fail (!error || *error == NULL, NULL);
141 fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
142 if (fd == -1)
144 int save_errno = errno;
145 gchar *display_filename = g_filename_display_name (filename);
147 g_set_error (error,
148 G_FILE_ERROR,
149 g_file_error_from_errno (save_errno),
150 _("Failed to open file '%s': open() failed: %s"),
151 display_filename,
152 g_strerror (save_errno));
153 g_free (display_filename);
154 return NULL;
157 file = g_slice_new0 (GMappedFile);
158 file->ref_count = 1;
159 file->free_func = g_mapped_file_destroy;
161 if (fstat (fd, &st) == -1)
163 int save_errno = errno;
164 gchar *display_filename = g_filename_display_name (filename);
166 g_set_error (error,
167 G_FILE_ERROR,
168 g_file_error_from_errno (save_errno),
169 _("Failed to get attributes of file '%s': fstat() failed: %s"),
170 display_filename,
171 g_strerror (save_errno));
172 g_free (display_filename);
173 goto out;
176 if (st.st_size == 0)
178 file->length = 0;
179 file->contents = NULL;
180 close (fd);
181 return file;
184 file->contents = MAP_FAILED;
186 #ifdef HAVE_MMAP
187 if (st.st_size > G_MAXSIZE)
189 errno = EINVAL;
191 else
193 file->length = (gsize) st.st_size;
194 file->contents = (gchar *) mmap (NULL, file->length,
195 writable ? PROT_READ|PROT_WRITE : PROT_READ,
196 MAP_PRIVATE, fd, 0);
198 #endif
199 #ifdef G_OS_WIN32
200 file->length = st.st_size;
201 file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
202 writable ? PAGE_WRITECOPY : PAGE_READONLY,
203 0, 0,
204 NULL);
205 if (file->mapping != NULL)
207 file->contents = MapViewOfFile (file->mapping,
208 writable ? FILE_MAP_COPY : FILE_MAP_READ,
209 0, 0,
211 if (file->contents == NULL)
213 file->contents = MAP_FAILED;
214 CloseHandle (file->mapping);
215 file->mapping = NULL;
218 #endif
221 if (file->contents == MAP_FAILED)
223 int save_errno = errno;
224 gchar *display_filename = g_filename_display_name (filename);
226 g_set_error (error,
227 G_FILE_ERROR,
228 g_file_error_from_errno (save_errno),
229 _("Failed to map file '%s': mmap() failed: %s"),
230 display_filename,
231 g_strerror (save_errno));
232 g_free (display_filename);
233 goto out;
236 close (fd);
237 return file;
239 out:
240 close (fd);
241 g_slice_free (GMappedFile, file);
243 return NULL;
247 * g_mapped_file_get_length:
248 * @file: a #GMappedFile
250 * Returns the length of the contents of a #GMappedFile.
252 * Returns: the length of the contents of @file.
254 * Since: 2.8
256 gsize
257 g_mapped_file_get_length (GMappedFile *file)
259 g_return_val_if_fail (file != NULL, 0);
261 return file->length;
265 * g_mapped_file_get_contents:
266 * @file: a #GMappedFile
268 * Returns the contents of a #GMappedFile.
270 * Note that the contents may not be zero-terminated,
271 * even if the #GMappedFile is backed by a text file.
273 * If the file is empty then %NULL is returned.
275 * Returns: the contents of @file, or %NULL.
277 * Since: 2.8
279 gchar *
280 g_mapped_file_get_contents (GMappedFile *file)
282 g_return_val_if_fail (file != NULL, NULL);
284 return file->contents;
288 * g_mapped_file_free:
289 * @file: a #GMappedFile
291 * This call existed before #GMappedFile had refcounting and is currently
292 * exactly the same as g_mapped_file_unref().
294 * Since: 2.8
295 * Deprecated:2.22: Use g_mapped_file_unref() instead.
297 void
298 g_mapped_file_free (GMappedFile *file)
300 g_mapped_file_unref (file);
304 * g_mapped_file_ref:
305 * @file: a #GMappedFile
307 * Increments the reference count of @file by one. It is safe to call
308 * this function from any thread.
310 * Return value: the passed in #GMappedFile.
312 * Since: 2.22
314 GMappedFile *
315 g_mapped_file_ref (GMappedFile *file)
317 g_return_val_if_fail (file != NULL, NULL);
318 g_return_val_if_fail (file->ref_count > 0, file);
320 g_atomic_int_inc (&file->ref_count);
322 return file;
326 * g_mapped_file_unref:
327 * @file: a #GMappedFile
329 * Decrements the reference count of @file by one. If the reference count
330 * drops to 0, unmaps the buffer of @file and frees it.
332 * It is safe to call this function from any thread.
334 * Since 2.22
336 void
337 g_mapped_file_unref (GMappedFile *file)
339 g_return_if_fail (file != NULL);
340 g_return_if_fail (file->ref_count > 0);
342 if (g_atomic_int_dec_and_test (&file->ref_count))
343 g_mapped_file_destroy (file);