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>
38 #include "gfileutils.h"
39 #include "gmappedfile.h"
41 #include "gmessages.h"
43 #include "gstrfuncs.h"
50 #define MAP_FAILED ((void *) -1)
61 * @filename: The path of the file to load, in the GLib filename encoding
62 * @writable: wether the mapping should be writable
63 * @error: return location for a #GError, or %NULL
65 * Maps a file into memory. On UNIX, this is using the mmap() function.
67 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
68 * it is an error to modify the mapped buffer. Modifications to the buffer
69 * are not visible to other processes mapping the same file, and are not
70 * written back to the file.
72 * Note that modifications of the underlying file might affect the contents
73 * of the #GMappedFile. Therefore, mapping should only be used if the file
74 * will not be modified, or if all modifications of the file are done
75 * atomically (e.g. using g_file_set_contents()).
77 * Return value: a newly allocated #GMappedFile which must be freed
78 * with g_mapped_file_free(), or %NULL if the mapping failed.
83 g_mapped_file_new (const gchar *filename,
91 g_return_val_if_fail (filename != NULL, NULL);
92 g_return_val_if_fail (!error || *error == NULL, NULL);
94 fd = g_open (filename, writable ? O_RDWR : O_RDONLY);
97 int save_errno = errno;
98 gchar *display_filename = g_filename_display_name (filename);
102 g_file_error_from_errno (save_errno),
103 _("Failed to open file '%s': open() failed: %s"),
105 g_strerror (save_errno));
106 g_free (display_filename);
110 file = g_new0 (GMappedFile, 1);
112 if (fstat (fd, &st) == -1)
114 int save_errno = errno;
115 gchar *display_filename = g_filename_display_name (filename);
119 g_file_error_from_errno (save_errno),
120 _("Failed to get attributes of file '%s': fstat() failed: %s"),
122 g_strerror (save_errno));
123 g_free (display_filename);
127 file->contents = MAP_FAILED;
130 file->length = st.st_size;
131 file->contents = (gchar *) mmap (NULL, st.st_size,
132 writable ? PROT_READ|PROT_WRITE : PROT_READ,
136 if (file->contents == MAP_FAILED)
138 int save_errno = errno;
139 gchar *display_filename = g_filename_display_name (filename);
143 g_file_error_from_errno (save_errno),
144 _("Failed to map file '%s': mmap() failed: %s"),
146 g_strerror (save_errno));
147 g_free (display_filename);
162 * g_mapped_file_get_length:
163 * @file: a #GMappedFile
165 * Returns the length of the contents of a #GMappedFile.
167 * Returns: the length of the contents of @file.
172 g_mapped_file_get_length (GMappedFile *file)
174 g_return_val_if_fail (file != NULL, 0);
180 * g_mapped_file_get_contents:
181 * @file: a #GMappedFile
183 * Returns the contents of a #GMappedFile.
185 * Note that the contents may not be zero-terminated,
186 * even if the #GMappedFile is backed by a text file.
188 * Returns: the contents of @file.
193 g_mapped_file_get_contents (GMappedFile *file)
195 g_return_val_if_fail (file != NULL, NULL);
197 return file->contents;
201 * g_mapped_file_free:
202 * @file: a #GMappedFile
204 * Unmaps the buffer of @file and frees it.
206 * For writable, shared mappings, the contents
207 * will be written back to the file at this point.
212 g_mapped_file_free (GMappedFile *file)
214 g_return_if_fail (file != NULL);
217 munmap (file->contents, file->length);
222 #define __G_MAPPED_FILE_C__
223 #include "galiasdef.c"