Include glibintl.h, not gi18n.h, noticed by Dan Winship.
[platform/upstream/glib.git] / glib / gmappedfile.c
1 /* GLIB - Library of useful routines for C programming
2  * gmappedfile.c: Simplified wrapper around the mmap() function.
3  *
4  * Copyright 2005 Matthias Clasen
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "config.h"
23
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
34
35 #include "glibconfig.h"
36
37 #ifdef G_OS_WIN32
38 #include <windows.h>
39 #include <io.h>
40 #endif
41
42 #include "gconvert.h"
43 #include "gerror.h"
44 #include "gfileutils.h"
45 #include "gmappedfile.h"
46 #include "gmem.h"
47 #include "gmessages.h"
48 #include "gstdio.h"
49 #include "gstrfuncs.h"
50
51 #include "glibintl.h"
52
53 #include "galias.h"
54
55 #ifndef _O_BINARY
56 #define _O_BINARY 0
57 #endif
58
59 #ifndef MAP_FAILED
60 #define MAP_FAILED ((void *) -1)
61 #endif
62
63 struct _GMappedFile 
64 {
65   gsize  length;
66   gchar *contents;
67 #ifdef G_OS_WIN32
68   HANDLE mapping;
69 #endif
70 };
71
72 /**
73  * g_mapped_file_new:
74  * @filename: The path of the file to load, in the GLib filename encoding
75  * @writable: wether the mapping should be writable
76  * @error: return location for a #GError, or %NULL
77  *
78  * Maps a file into memory. On UNIX, this is using the mmap() function.
79  *
80  * If @writable is %TRUE, the mapped buffer may be modified, otherwise
81  * it is an error to modify the mapped buffer. Modifications to the buffer 
82  * are not visible to other processes mapping the same file, and are not 
83  * written back to the file.
84  *
85  * Note that modifications of the underlying file might affect the contents
86  * of the #GMappedFile. Therefore, mapping should only be used if the file 
87  * will not be modified, or if all modifications of the file are done
88  * atomically (e.g. using g_file_set_contents()). 
89  *
90  * Return value: a newly allocated #GMappedFile which must be freed
91  *    with g_mapped_file_free(), or %NULL if the mapping failed. 
92  *
93  * Since: 2.8
94  */
95 GMappedFile *
96 g_mapped_file_new (const gchar  *filename,
97                    gboolean      writable,
98                    GError      **error)
99 {
100   GMappedFile *file;
101   int fd;
102   struct stat st;
103
104   g_return_val_if_fail (filename != NULL, NULL);
105   g_return_val_if_fail (!error || *error == NULL, NULL);
106
107   fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
108   if (fd == -1)
109     {
110       int save_errno = errno;
111       gchar *display_filename = g_filename_display_name (filename);
112       
113       g_set_error (error,
114                    G_FILE_ERROR,
115                    g_file_error_from_errno (save_errno),
116                    _("Failed to open file '%s': open() failed: %s"),
117                    display_filename, 
118                    g_strerror (save_errno));
119       g_free (display_filename);
120       return NULL;
121     }
122
123   file = g_new0 (GMappedFile, 1);
124
125   if (fstat (fd, &st) == -1)
126     {
127       int save_errno = errno;
128       gchar *display_filename = g_filename_display_name (filename);
129
130       g_set_error (error,
131                    G_FILE_ERROR,
132                    g_file_error_from_errno (save_errno),
133                    _("Failed to get attributes of file '%s': fstat() failed: %s"),
134                    display_filename, 
135                    g_strerror (save_errno));
136       g_free (display_filename);
137       goto out;
138     }
139
140   file->contents = MAP_FAILED;
141
142 #ifdef HAVE_MMAP
143   file->length = st.st_size;
144   file->contents = (gchar *) mmap (NULL, st.st_size,
145                                    writable ? PROT_READ|PROT_WRITE : PROT_READ,
146                                    MAP_PRIVATE, fd, 0);
147 #endif
148 #ifdef G_OS_WIN32
149   file->length = st.st_size;
150   file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
151                                      writable ? PAGE_WRITECOPY : PAGE_READONLY,
152                                      0, 0,
153                                      NULL);
154   if (file->mapping != NULL)
155     {
156       file->contents = MapViewOfFile (file->mapping,
157                                       writable ? FILE_MAP_COPY : FILE_MAP_READ,
158                                       0, 0,
159                                       0);
160       if (file->contents == NULL)
161         {
162           file->contents = MAP_FAILED;
163           CloseHandle (file->mapping);
164           file->mapping = NULL;
165         }
166     }
167 #endif
168
169   
170   if (file->contents == MAP_FAILED)
171     {
172       int save_errno = errno;
173       gchar *display_filename = g_filename_display_name (filename);
174       
175       g_set_error (error,
176                    G_FILE_ERROR,
177                    g_file_error_from_errno (save_errno),
178                    _("Failed to map file '%s': mmap() failed: %s"),
179                    display_filename,
180                    g_strerror (save_errno));
181       g_free (display_filename);
182       goto out;
183     }
184
185   close (fd);
186   return file;
187
188  out:
189   close (fd);
190   g_free (file);
191
192   return NULL;
193 }
194
195 /**
196  * g_mapped_file_get_length:
197  * @file: a #GMappedFile
198  *
199  * Returns the length of the contents of a #GMappedFile.
200  *
201  * Returns: the length of the contents of @file.
202  *
203  * Since: 2.8
204  */
205 gsize
206 g_mapped_file_get_length (GMappedFile *file)
207 {
208   g_return_val_if_fail (file != NULL, 0);
209
210   return file->length;
211 }
212
213 /**
214  * g_mapped_file_get_contents:
215  * @file: a #GMappedFile
216  *
217  * Returns the contents of a #GMappedFile. 
218  *
219  * Note that the contents may not be zero-terminated,
220  * even if the #GMappedFile is backed by a text file.
221  *
222  * Returns: the contents of @file.
223  *
224  * Since: 2.8
225  */
226 gchar *
227 g_mapped_file_get_contents (GMappedFile *file)
228 {
229   g_return_val_if_fail (file != NULL, NULL);
230
231   return file->contents;
232 }
233
234 /**
235  * g_mapped_file_free:
236  * @file: a #GMappedFile
237  *
238  * Unmaps the buffer of @file and frees it. 
239  *
240  * Since: 2.8
241  */
242 void
243 g_mapped_file_free (GMappedFile *file)
244 {
245   g_return_if_fail (file != NULL);
246
247 #ifdef HAVE_MMAP
248   munmap (file->contents, file->length);
249 #endif
250 #ifdef G_OS_WIN32
251   UnmapViewOfFile (file->contents);
252   CloseHandle (file->mapping);
253 #endif
254
255   g_free (file);
256 }
257
258
259 #define __G_MAPPED_FILE_C__
260 #include "galiasdef.c"