Coverage Report

Created: 2026-10-07 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gstreamer/subprojects/glib-2.86.3/glib/gmappedfile.c
Line
Count
Source
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
 * SPDX-License-Identifier: LGPL-2.1-or-later
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
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_MMAP
29
#include <sys/mman.h>
30
#endif
31
32
#include "glibconfig.h"
33
34
#ifdef G_OS_UNIX
35
#include <unistd.h>
36
#endif
37
38
#ifdef G_OS_WIN32
39
#include <windows.h>
40
#include <io.h>
41
42
#undef fstat
43
#define fstat(a,b) _fstati64(a,b)
44
#undef stat
45
#define stat _stati64
46
47
#ifndef S_ISREG
48
#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
49
#endif
50
51
#endif
52
53
#ifndef O_CLOEXEC
54
#define O_CLOEXEC 0
55
#endif
56
57
#include "gconvert.h"
58
#include "gerror.h"
59
#include "gfileutils.h"
60
#include "gmappedfile.h"
61
#include "gmem.h"
62
#include "gmessages.h"
63
#include "gstdio.h"
64
#include "gstrfuncs.h"
65
#include "gatomic.h"
66
67
#include "glibintl.h"
68
69
70
#ifndef _O_BINARY
71
13
#define _O_BINARY 0
72
#endif
73
74
#ifndef MAP_FAILED
75
#define MAP_FAILED ((void *) -1)
76
#endif
77
78
/**
79
 * GMappedFile:
80
 *
81
 * The #GMappedFile represents a file mapping created with
82
 * g_mapped_file_new(). It has only private members and should
83
 * not be accessed directly.
84
 */
85
86
struct _GMappedFile
87
{
88
  gchar *contents;
89
  gsize  length;
90
  gpointer free_func;
91
  int    ref_count;
92
#ifdef G_OS_WIN32
93
  HANDLE mapping;
94
#endif
95
};
96
97
static void
98
g_mapped_file_destroy (GMappedFile *file)
99
2
{
100
2
  if (file->length)
101
2
    {
102
2
#ifdef HAVE_MMAP
103
2
      munmap (file->contents, file->length);
104
2
#endif
105
#ifdef G_OS_WIN32
106
      UnmapViewOfFile (file->contents);
107
      CloseHandle (file->mapping);
108
#endif
109
2
    }
110
111
2
  g_slice_free (GMappedFile, file);
112
2
}
113
114
static GMappedFile*
115
mapped_file_new_from_fd (int           fd,
116
       gboolean      writable,
117
                         const gchar  *filename,
118
                         GError      **error)
119
2
{
120
2
  GMappedFile *file;
121
2
  struct stat st;
122
123
2
  file = g_slice_new0 (GMappedFile);
124
2
  file->ref_count = 1;
125
2
  file->free_func = g_mapped_file_destroy;
126
127
2
  if (fstat (fd, &st) == -1)
128
0
    {
129
0
      if (error != NULL)
130
0
        {
131
0
          int save_errno = errno;
132
0
          gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
133
134
0
          g_set_error (error,
135
0
                       G_FILE_ERROR,
136
0
                       g_file_error_from_errno (save_errno),
137
0
                       _("Failed to get attributes of file ā€œ%s%s%s%sā€: fstat() failed: %s"),
138
0
                       display_filename ? display_filename : "fd",
139
0
                       display_filename ? "' " : "",
140
0
                       display_filename ? display_filename : "",
141
0
                       display_filename ? "'" : "",
142
0
                       g_strerror (save_errno));
143
0
          g_free (display_filename);
144
0
        }
145
0
      goto out;
146
0
    }
147
148
  /* mmap() on size 0 will fail with EINVAL, so we avoid calling mmap()
149
   * in that case -- but only if we have a regular file; we still want
150
   * attempts to mmap a character device to fail, for example.
151
   */
152
2
  if (st.st_size == 0 && S_ISREG (st.st_mode))
153
0
    {
154
0
      file->length = 0;
155
0
      file->contents = NULL;
156
0
      return file;
157
0
    }
158
159
2
  file->contents = MAP_FAILED;
160
161
2
#ifdef HAVE_MMAP
162
2
  if (sizeof (st.st_size) > sizeof (gsize) && st.st_size > (off_t) G_MAXSIZE)
163
0
    {
164
0
      errno = EINVAL;
165
0
    }
166
2
  else
167
2
    {      
168
2
      file->length = (gsize) st.st_size;
169
2
      file->contents = (gchar *) mmap (NULL,  file->length,
170
2
               writable ? PROT_READ|PROT_WRITE : PROT_READ,
171
2
               MAP_PRIVATE, fd, 0);
172
2
    }
173
2
#endif
174
#ifdef G_OS_WIN32
175
  file->length = st.st_size;
176
  file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
177
             writable ? PAGE_WRITECOPY : PAGE_READONLY,
178
             0, 0,
179
             NULL);
180
  if (file->mapping != NULL)
181
    {
182
      file->contents = MapViewOfFile (file->mapping,
183
              writable ? FILE_MAP_COPY : FILE_MAP_READ,
184
              0, 0,
185
              0);
186
      if (file->contents == NULL)
187
  {
188
    file->contents = MAP_FAILED;
189
    CloseHandle (file->mapping);
190
    file->mapping = NULL;
191
  }
192
    }
193
#endif
194
195
  
196
2
  if (file->contents == MAP_FAILED)
197
0
    {
198
0
      if (error != NULL)
199
0
        {
200
0
          int save_errno = errno;
201
0
          gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
202
203
0
          g_set_error (error,
204
0
                       G_FILE_ERROR,
205
0
                       g_file_error_from_errno (save_errno),
206
0
                       _("Failed to map %s%s%s%s: mmap() failed: %s"),
207
0
                       display_filename ? display_filename : "fd",
208
0
                       display_filename ? "' " : "",
209
0
                       display_filename ? display_filename : "",
210
0
                       display_filename ? "'" : "",
211
0
                       g_strerror (save_errno));
212
0
          g_free (display_filename);
213
0
        }
214
0
      goto out;
215
0
    }
216
217
2
  return file;
218
219
0
 out:
220
0
  g_slice_free (GMappedFile, file);
221
222
0
  return NULL;
223
2
}
224
225
/**
226
 * g_mapped_file_new:
227
 * @filename: (type filename): The path of the file to load, in the GLib
228
 *     filename encoding
229
 * @writable: whether the mapping should be writable
230
 * @error: return location for a #GError, or %NULL
231
 *
232
 * Maps a file into memory. On UNIX, this is using the mmap() function.
233
 *
234
 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
235
 * it is an error to modify the mapped buffer. Modifications to the buffer
236
 * are not visible to other processes mapping the same file, and are not
237
 * written back to the file.
238
 *
239
 * Note that modifications of the underlying file might affect the contents
240
 * of the #GMappedFile. Therefore, mapping should only be used if the file
241
 * will not be modified, or if all modifications of the file are done
242
 * atomically (e.g. using g_file_set_contents()).
243
 *
244
 * If @filename is the name of an empty, regular file, the function
245
 * will successfully return an empty #GMappedFile. In other cases of
246
 * size 0 (e.g. device files such as /dev/null), @error will be set
247
 * to the #GFileError value %G_FILE_ERROR_INVAL.
248
 *
249
 * Returns: a newly allocated #GMappedFile which must be unref'd
250
 *    with g_mapped_file_unref(), or %NULL if the mapping failed.
251
 *
252
 * Since: 2.8
253
 */
254
GMappedFile *
255
g_mapped_file_new (const gchar  *filename,
256
       gboolean      writable,
257
       GError      **error)
258
13
{
259
13
  GMappedFile *file;
260
13
  int fd;
261
262
13
  g_return_val_if_fail (filename != NULL, NULL);
263
13
  g_return_val_if_fail (!error || *error == NULL, NULL);
264
265
13
  fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY | O_CLOEXEC, 0);
266
13
  if (fd == -1)
267
11
    {
268
11
      if (error != NULL)
269
11
        {
270
11
          int save_errno = errno;
271
11
          gchar *display_filename = g_filename_display_name (filename);
272
273
11
          g_set_error (error,
274
11
                       G_FILE_ERROR,
275
11
                       g_file_error_from_errno (save_errno),
276
11
                       _("Failed to open file ā€œ%sā€: open() failed: %s"),
277
11
                       display_filename,
278
11
                       g_strerror (save_errno));
279
11
          g_free (display_filename);
280
11
        }
281
282
11
      return NULL;
283
11
    }
284
285
2
  file = mapped_file_new_from_fd (fd, writable, filename, error);
286
287
2
  close (fd);
288
289
2
  return file;
290
13
}
291
292
293
/**
294
 * g_mapped_file_new_from_fd:
295
 * @fd: The file descriptor of the file to load
296
 * @writable: whether the mapping should be writable
297
 * @error: return location for a #GError, or %NULL
298
 *
299
 * Maps a file into memory. On UNIX, this is using the mmap() function.
300
 *
301
 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
302
 * it is an error to modify the mapped buffer. Modifications to the buffer
303
 * are not visible to other processes mapping the same file, and are not
304
 * written back to the file.
305
 *
306
 * Note that modifications of the underlying file might affect the contents
307
 * of the #GMappedFile. Therefore, mapping should only be used if the file
308
 * will not be modified, or if all modifications of the file are done
309
 * atomically (e.g. using g_file_set_contents()).
310
 *
311
 * Returns: a newly allocated #GMappedFile which must be unref'd
312
 *    with g_mapped_file_unref(), or %NULL if the mapping failed.
313
 *
314
 * Since: 2.32
315
 */
316
GMappedFile *
317
g_mapped_file_new_from_fd (gint          fd,
318
         gboolean      writable,
319
         GError      **error)
320
0
{
321
0
  return mapped_file_new_from_fd (fd, writable, NULL, error);
322
0
}
323
324
/**
325
 * g_mapped_file_get_length:
326
 * @file: a #GMappedFile
327
 *
328
 * Returns the length of the contents of a #GMappedFile.
329
 *
330
 * Returns: the length of the contents of @file.
331
 *
332
 * Since: 2.8
333
 */
334
gsize
335
g_mapped_file_get_length (GMappedFile *file)
336
2
{
337
2
  g_return_val_if_fail (file != NULL, 0);
338
339
2
  return file->length;
340
2
}
341
342
/**
343
 * g_mapped_file_get_contents:
344
 * @file: a #GMappedFile
345
 *
346
 * Returns the contents of a #GMappedFile. 
347
 *
348
 * Note that the contents may not be zero-terminated,
349
 * even if the #GMappedFile is backed by a text file.
350
 *
351
 * If the file is empty then %NULL is returned.
352
 *
353
 * Returns: (transfer none) (nullable): the contents of @file, or %NULL.
354
 *
355
 * Since: 2.8
356
 */
357
gchar *
358
g_mapped_file_get_contents (GMappedFile *file)
359
2
{
360
2
  g_return_val_if_fail (file != NULL, NULL);
361
362
2
  return file->contents;
363
2
}
364
365
/**
366
 * g_mapped_file_free:
367
 * @file: a #GMappedFile
368
 *
369
 * This call existed before #GMappedFile had refcounting and is currently
370
 * exactly the same as g_mapped_file_unref().
371
 *
372
 * Since: 2.8
373
 * Deprecated:2.22: Use g_mapped_file_unref() instead.
374
 */
375
void
376
g_mapped_file_free (GMappedFile *file)
377
0
{
378
0
  g_mapped_file_unref (file);
379
0
}
380
381
/**
382
 * g_mapped_file_ref:
383
 * @file: a #GMappedFile
384
 *
385
 * Increments the reference count of @file by one.  It is safe to call
386
 * this function from any thread.
387
 *
388
 * Returns: the passed in #GMappedFile.
389
 *
390
 * Since: 2.22
391
 **/
392
GMappedFile *
393
g_mapped_file_ref (GMappedFile *file)
394
2
{
395
2
  g_return_val_if_fail (file != NULL, NULL);
396
397
2
  g_atomic_int_inc (&file->ref_count);
398
399
2
  return file;
400
2
}
401
402
/**
403
 * g_mapped_file_unref:
404
 * @file: a #GMappedFile
405
 *
406
 * Decrements the reference count of @file by one.  If the reference count
407
 * drops to 0, unmaps the buffer of @file and frees it.
408
 *
409
 * It is safe to call this function from any thread.
410
 *
411
 * Since 2.22
412
 **/
413
void
414
g_mapped_file_unref (GMappedFile *file)
415
4
{
416
4
  g_return_if_fail (file != NULL);
417
418
4
  if (g_atomic_int_dec_and_test (&file->ref_count))
419
2
    g_mapped_file_destroy (file);
420
4
}
421
422
/**
423
 * g_mapped_file_get_bytes:
424
 * @file: a #GMappedFile
425
 *
426
 * Creates a new #GBytes which references the data mapped from @file.
427
 * The mapped contents of the file must not be modified after creating this
428
 * bytes object, because a #GBytes should be immutable.
429
 *
430
 * Returns: (transfer full): A newly allocated #GBytes referencing data
431
 *     from @file
432
 *
433
 * Since: 2.34
434
 **/
435
GBytes *
436
g_mapped_file_get_bytes (GMappedFile *file)
437
0
{
438
0
  g_return_val_if_fail (file != NULL, NULL);
439
440
0
  return g_bytes_new_with_free_func (file->contents,
441
0
             file->length,
442
0
             (GDestroyNotify) g_mapped_file_unref,
443
0
             g_mapped_file_ref (file));
444
0
}