Coverage Report

Created: 2026-10-14 08:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib-2.88.3/glib/gfileutils.c
Line
Count
Source
1
/* gfileutils.c - File utility functions
2
 *
3
 *  Copyright 2000 Red Hat, Inc.
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "config.h"
22
#include "glibconfig.h"
23
24
#include <sys/stat.h>
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <stdarg.h>
28
#include <stdint.h>
29
#include <string.h>
30
#include <errno.h>
31
#include <sys/types.h>
32
#include <sys/stat.h>
33
#include <fcntl.h>
34
#include <stdlib.h>
35
36
#ifdef G_OS_UNIX
37
#include <unistd.h>
38
#endif
39
#ifdef G_OS_WIN32
40
#include <windows.h>
41
#include <io.h>
42
#endif /* G_OS_WIN32 */
43
44
#ifndef S_ISLNK
45
#define S_ISLNK(x) 0
46
#endif
47
48
#ifndef O_BINARY
49
0
#define O_BINARY 0
50
#endif
51
52
#ifndef O_CLOEXEC
53
#define O_CLOEXEC 0
54
#endif
55
56
#include "gfileutils.h"
57
58
#include "gstdio.h"
59
#include "gstdioprivate.h"
60
#include "glibintl.h"
61
62
63
/**
64
 * GFileError:
65
 * @G_FILE_ERROR_EXIST: Operation not permitted; only the owner of
66
 *     the file (or other resource) or processes with special privileges
67
 *     can perform the operation.
68
 * @G_FILE_ERROR_ISDIR: File is a directory; you cannot open a directory
69
 *     for writing, or create or remove hard links to it.
70
 * @G_FILE_ERROR_ACCES: Permission denied; the file permissions do not
71
 *     allow the attempted operation.
72
 * @G_FILE_ERROR_NAMETOOLONG: Filename too long.
73
 * @G_FILE_ERROR_NOENT: No such file or directory. This is a "file
74
 *     doesn't exist" error for ordinary files that are referenced in
75
 *     contexts where they are expected to already exist.
76
 * @G_FILE_ERROR_NOTDIR: A file that isn't a directory was specified when
77
 *     a directory is required.
78
 * @G_FILE_ERROR_NXIO: No such device or address. The system tried to
79
 *     use the device represented by a file you specified, and it
80
 *     couldn't find the device. This can mean that the device file was
81
 *     installed incorrectly, or that the physical device is missing or
82
 *     not correctly attached to the computer.
83
 * @G_FILE_ERROR_NODEV: The underlying file system of the specified file
84
 *     does not support memory mapping.
85
 * @G_FILE_ERROR_ROFS: The directory containing the new link can't be
86
 *     modified because it's on a read-only file system.
87
 * @G_FILE_ERROR_TXTBSY: Text file busy.
88
 * @G_FILE_ERROR_FAULT: You passed in a pointer to bad memory.
89
 *     (GLib won't reliably return this, don't pass in pointers to bad
90
 *     memory.)
91
 * @G_FILE_ERROR_LOOP: Too many levels of symbolic links were encountered
92
 *     in looking up a file name. This often indicates a cycle of symbolic
93
 *     links.
94
 * @G_FILE_ERROR_NOSPC: No space left on device; write operation on a
95
 *     file failed because the disk is full.
96
 * @G_FILE_ERROR_NOMEM: No memory available. The system cannot allocate
97
 *     more virtual memory because its capacity is full.
98
 * @G_FILE_ERROR_MFILE: The current process has too many files open and
99
 *     can't open any more. Duplicate descriptors do count toward this
100
 *     limit.
101
 * @G_FILE_ERROR_NFILE: There are too many distinct file openings in the
102
 *     entire system.
103
 * @G_FILE_ERROR_BADF: Bad file descriptor; for example, I/O on a
104
 *     descriptor that has been closed or reading from a descriptor open
105
 *     only for writing (or vice versa).
106
 * @G_FILE_ERROR_INVAL: Invalid argument. This is used to indicate
107
 *     various kinds of problems with passing the wrong argument to a
108
 *     library function.
109
 * @G_FILE_ERROR_PIPE: Broken pipe; there is no process reading from the
110
 *     other end of a pipe. Every library function that returns this
111
 *     error code also generates a 'SIGPIPE' signal; this signal
112
 *     terminates the program if not handled or blocked. Thus, your
113
 *     program will never actually see this code unless it has handled
114
 *     or blocked 'SIGPIPE'.
115
 * @G_FILE_ERROR_AGAIN: Resource temporarily unavailable; the call might
116
 *     work if you try again later.
117
 * @G_FILE_ERROR_INTR: Interrupted function call; an asynchronous signal
118
 *     occurred and prevented completion of the call. When this
119
 *     happens, you should try the call again.
120
 * @G_FILE_ERROR_IO: Input/output error; usually used for physical read
121
 *    or write errors. i.e. the disk or other physical device hardware
122
 *    is returning errors.
123
 * @G_FILE_ERROR_PERM: Operation not permitted; only the owner of the
124
 *    file (or other resource) or processes with special privileges can
125
 *    perform the operation.
126
 * @G_FILE_ERROR_NOSYS: Function not implemented; this indicates that
127
 *    the system is missing some functionality.
128
 * @G_FILE_ERROR_FAILED: Does not correspond to a UNIX error code; this
129
 *    is the standard "failed for unspecified reason" error code present
130
 *    in all #GError error code enumerations. Returned if no specific
131
 *    code applies.
132
 *
133
 * Values corresponding to @errno codes returned from file operations
134
 * on UNIX. Unlike @errno codes, GFileError values are available on
135
 * all systems, even Windows. The exact meaning of each code depends
136
 * on what sort of file operation you were performing; the UNIX
137
 * documentation gives more details. The following error code descriptions
138
 * come from the GNU C Library manual, and are under the copyright
139
 * of that manual.
140
 *
141
 * It's not very portable to make detailed assumptions about exactly
142
 * which errors will be returned from a given operation. Some errors
143
 * don't occur on some systems, etc., sometimes there are subtle
144
 * differences in when a system will report a given error, etc.
145
 */
146
147
/**
148
 * G_FILE_ERROR:
149
 *
150
 * Error domain for file operations. Errors in this domain will
151
 * be from the #GFileError enumeration. See #GError for information
152
 * on error domains.
153
 */
154
155
/**
156
 * GFileTest:
157
 * @G_FILE_TEST_IS_REGULAR: %TRUE if the file is a regular file
158
 *     (not a directory). Note that this test will also return %TRUE
159
 *     if the tested file is a symlink to a regular file.
160
 * @G_FILE_TEST_IS_SYMLINK: %TRUE if the file is a symlink.
161
 * @G_FILE_TEST_IS_DIR: %TRUE if the file is a directory.
162
 * @G_FILE_TEST_IS_EXECUTABLE: %TRUE if the file is executable.
163
 * @G_FILE_TEST_EXISTS: %TRUE if the file exists. It may or may not
164
 *     be a regular file.
165
 *
166
 * A test to perform on a file using g_file_test().
167
 */
168
169
/**
170
 * g_mkdir_with_parents:
171
 * @pathname: (type filename): a pathname in the GLib file name encoding
172
 * @mode: permissions to use for newly created directories
173
 *
174
 * Create a directory if it doesn't already exist. Create intermediate
175
 * parent directories as needed, too.
176
 *
177
 * Returns: 0 if the directory already exists, or was successfully
178
 * created. Returns -1 if an error occurred, with errno set.
179
 *
180
 * Since: 2.8
181
 */
182
int
183
g_mkdir_with_parents (const gchar *pathname,
184
          int          mode)
185
0
{
186
0
  gchar *fn, *p;
187
188
0
  if (pathname == NULL || *pathname == '\0')
189
0
    {
190
0
      errno = EINVAL;
191
0
      return -1;
192
0
    }
193
194
  /* try to create the full path first */
195
0
  if (g_mkdir (pathname, mode) == 0)
196
0
    return 0;
197
0
  else if (errno == EEXIST)
198
0
    {
199
0
      if (!g_file_test (pathname, G_FILE_TEST_IS_DIR))
200
0
        {
201
0
          errno = ENOTDIR;
202
0
          return -1;
203
0
        }
204
0
      return 0;
205
0
    }
206
207
  /* walk the full path and try creating each element */
208
0
  fn = g_strdup (pathname);
209
210
0
  if (g_path_is_absolute (fn))
211
0
    p = (gchar *) g_path_skip_root (fn);
212
0
  else
213
0
    p = fn;
214
215
0
  do
216
0
    {
217
0
      while (*p && !G_IS_DIR_SEPARATOR (*p))
218
0
  p++;
219
      
220
0
      if (!*p)
221
0
  p = NULL;
222
0
      else
223
0
  *p = '\0';
224
      
225
0
      if (!g_file_test (fn, G_FILE_TEST_EXISTS))
226
0
  {
227
0
    if (g_mkdir (fn, mode) == -1 && errno != EEXIST)
228
0
      {
229
0
        int errno_save = errno;
230
0
        if (errno != ENOENT || !p)
231
0
                {
232
0
            g_free (fn);
233
0
            errno = errno_save;
234
0
            return -1;
235
0
    }
236
0
      }
237
0
  }
238
0
      else if (!g_file_test (fn, G_FILE_TEST_IS_DIR))
239
0
  {
240
0
    g_free (fn);
241
0
    errno = ENOTDIR;
242
0
    return -1;
243
0
  }
244
0
      if (p)
245
0
  {
246
0
    *p++ = G_DIR_SEPARATOR;
247
0
    while (*p && G_IS_DIR_SEPARATOR (*p))
248
0
      p++;
249
0
  }
250
0
    }
251
0
  while (p);
252
253
0
  g_free (fn);
254
255
0
  return 0;
256
0
}
257
258
/**
259
 * g_file_test:
260
 * @filename: (type filename): a filename to test in the
261
 *     GLib file name encoding
262
 * @test: bitfield of #GFileTest flags
263
 *
264
 * Returns %TRUE if any of the tests in the bitfield @test are
265
 * %TRUE. For example, `(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)`
266
 * will return %TRUE if the file exists; the check whether it's a
267
 * directory doesn't matter since the existence test is %TRUE. With
268
 * the current set of available tests, there's no point passing in
269
 * more than one test at a time.
270
 *
271
 * Apart from %G_FILE_TEST_IS_SYMLINK all tests follow symbolic links,
272
 * so for a symbolic link to a regular file g_file_test() will return
273
 * %TRUE for both %G_FILE_TEST_IS_SYMLINK and %G_FILE_TEST_IS_REGULAR.
274
 *
275
 * Note, that for a dangling symbolic link g_file_test() will return
276
 * %TRUE for %G_FILE_TEST_IS_SYMLINK and %FALSE for all other flags.
277
 *
278
 * You should never use g_file_test() to test whether it is safe
279
 * to perform an operation, because there is always the possibility
280
 * of the condition changing before you actually perform the operation,
281
 * see [TOCTOU](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use).
282
 *
283
 * For example, you might think you could use %G_FILE_TEST_IS_SYMLINK
284
 * to know whether it is safe to write to a file without being
285
 * tricked into writing into a different location. It doesn't work!
286
 *
287
 * |[<!-- language="C" -->
288
 *  // DON'T DO THIS
289
 *  if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK)) 
290
 *    {
291
 *      fd = g_open (filename, O_WRONLY);
292
 *      // write to fd
293
 *    }
294
 *
295
 *  // DO THIS INSTEAD
296
 *  fd = g_open (filename, O_WRONLY | O_NOFOLLOW | O_CLOEXEC);
297
 *  if (fd == -1)
298
 *    {
299
 *      // check error
300
 *      if (errno == ELOOP)
301
 *        // file is a symlink and can be ignored
302
 *      else
303
 *        // handle errors as before
304
 *    }
305
 *  else
306
 *    {
307
 *      // write to fd
308
 *    }
309
 * ]|
310
 *
311
 * Another thing to note is that %G_FILE_TEST_EXISTS and
312
 * %G_FILE_TEST_IS_EXECUTABLE are implemented using the access()
313
 * system call. This usually doesn't matter, but if your program
314
 * is setuid or setgid it means that these tests will give you
315
 * the answer for the real user ID and group ID, rather than the
316
 * effective user ID and group ID.
317
 *
318
 * On Windows, there are no symlinks, so testing for
319
 * %G_FILE_TEST_IS_SYMLINK will always return %FALSE. Testing for
320
 * %G_FILE_TEST_IS_EXECUTABLE will just check that the file exists and
321
 * its name indicates that it is executable, checking for well-known
322
 * extensions and those listed in the `PATHEXT` environment variable.
323
 *
324
 * Returns: whether a test was %TRUE
325
 **/
326
gboolean
327
g_file_test (const gchar *filename,
328
             GFileTest    test)
329
0
{
330
#ifdef G_OS_WIN32
331
  DWORD attributes;
332
  wchar_t *wfilename;
333
#endif
334
335
0
  g_return_val_if_fail (filename != NULL, FALSE);
336
337
#ifdef G_OS_WIN32
338
/* stuff missing in std vc6 api */
339
#  ifndef INVALID_FILE_ATTRIBUTES
340
#    define INVALID_FILE_ATTRIBUTES -1
341
#  endif
342
#  ifndef FILE_ATTRIBUTE_DEVICE
343
#    define FILE_ATTRIBUTE_DEVICE 64
344
#  endif
345
  wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
346
347
  if (wfilename == NULL)
348
    return FALSE;
349
350
  attributes = GetFileAttributesW (wfilename);
351
352
  g_free (wfilename);
353
354
  if (attributes == INVALID_FILE_ATTRIBUTES)
355
    return FALSE;
356
357
  if (test & G_FILE_TEST_EXISTS)
358
    return TRUE;
359
      
360
  if (test & G_FILE_TEST_IS_REGULAR)
361
    {
362
      if ((attributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)) == 0)
363
  return TRUE;
364
    }
365
366
  if (test & G_FILE_TEST_IS_DIR)
367
    {
368
      if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
369
  return TRUE;
370
    }
371
372
  /* "while" so that we can exit this "loop" with a simple "break" */
373
  while (test & G_FILE_TEST_IS_EXECUTABLE)
374
    {
375
      const gchar *lastdot = strrchr (filename, '.');
376
      const gchar *pathext = NULL, *p;
377
      size_t extlen;
378
379
      if (lastdot == NULL)
380
        break;
381
382
      if (_stricmp (lastdot, ".exe") == 0 ||
383
    _stricmp (lastdot, ".cmd") == 0 ||
384
    _stricmp (lastdot, ".bat") == 0 ||
385
    _stricmp (lastdot, ".com") == 0)
386
  return TRUE;
387
388
      /* Check if it is one of the types listed in %PATHEXT% */
389
390
      pathext = g_getenv ("PATHEXT");
391
      if (pathext == NULL)
392
        break;
393
394
      pathext = g_utf8_casefold (pathext, -1);
395
396
      lastdot = g_utf8_casefold (lastdot, -1);
397
      extlen = strlen (lastdot);
398
399
      p = pathext;
400
      while (TRUE)
401
  {
402
    const gchar *q = strchr (p, ';');
403
    if (q == NULL)
404
      q = p + strlen (p);
405
    if (extlen == (size_t) (q - p) &&
406
        memcmp (lastdot, p, extlen) == 0)
407
      {
408
        g_free ((gchar *) pathext);
409
        g_free ((gchar *) lastdot);
410
        return TRUE;
411
      }
412
    if (*q)
413
      p = q + 1;
414
    else
415
      break;
416
  }
417
418
      g_free ((gchar *) pathext);
419
      g_free ((gchar *) lastdot);
420
      break;
421
    }
422
423
  return FALSE;
424
#else
425
0
  if ((test & G_FILE_TEST_EXISTS) && (access (filename, F_OK) == 0))
426
0
    return TRUE;
427
  
428
0
  if ((test & G_FILE_TEST_IS_EXECUTABLE) && (access (filename, X_OK) == 0))
429
0
    {
430
0
      if (getuid () != 0)
431
0
  return TRUE;
432
433
      /* For root, on some POSIX systems, access (filename, X_OK)
434
       * will succeed even if no executable bits are set on the
435
       * file. We fall through to a stat test to avoid that.
436
       */
437
0
    }
438
0
  else
439
0
    test &= ~G_FILE_TEST_IS_EXECUTABLE;
440
441
0
  if (test & G_FILE_TEST_IS_SYMLINK)
442
0
    {
443
0
      struct stat s;
444
445
0
      if ((lstat (filename, &s) == 0) && S_ISLNK (s.st_mode))
446
0
        return TRUE;
447
0
    }
448
  
449
0
  if (test & (G_FILE_TEST_IS_REGULAR |
450
0
        G_FILE_TEST_IS_DIR |
451
0
        G_FILE_TEST_IS_EXECUTABLE))
452
0
    {
453
0
      struct stat s;
454
      
455
0
      if (stat (filename, &s) == 0)
456
0
  {
457
0
    if ((test & G_FILE_TEST_IS_REGULAR) && S_ISREG (s.st_mode))
458
0
      return TRUE;
459
    
460
0
    if ((test & G_FILE_TEST_IS_DIR) && S_ISDIR (s.st_mode))
461
0
      return TRUE;
462
463
    /* The extra test for root when access (file, X_OK) succeeds.
464
     */
465
0
    if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
466
0
        ((s.st_mode & S_IXOTH) ||
467
0
         (s.st_mode & S_IXUSR) ||
468
0
         (s.st_mode & S_IXGRP)))
469
0
      return TRUE;
470
0
  }
471
0
    }
472
473
0
  return FALSE;
474
0
#endif
475
0
}
476
477
G_DEFINE_QUARK (g-file-error-quark, g_file_error)
478
479
/**
480
 * g_file_error_from_errno:
481
 * @err_no: an "errno" value
482
 *
483
 * Gets a #GFileError constant based on the passed-in @err_no.
484
 *
485
 * For example, if you pass in `EEXIST` this function returns
486
 * %G_FILE_ERROR_EXIST. Unlike `errno` values, you can portably
487
 * assume that all #GFileError values will exist.
488
 *
489
 * Normally a #GFileError value goes into a #GError returned
490
 * from a function that manipulates files. So you would use
491
 * g_file_error_from_errno() when constructing a #GError.
492
 *
493
 * Returns: #GFileError corresponding to the given @err_no
494
 **/
495
GFileError
496
g_file_error_from_errno (gint err_no)
497
0
{
498
0
  switch (err_no)
499
0
    {
500
0
#ifdef EEXIST
501
0
    case EEXIST:
502
0
      return G_FILE_ERROR_EXIST;
503
0
#endif
504
505
0
#ifdef EISDIR
506
0
    case EISDIR:
507
0
      return G_FILE_ERROR_ISDIR;
508
0
#endif
509
510
0
#ifdef EACCES
511
0
    case EACCES:
512
0
      return G_FILE_ERROR_ACCES;
513
0
#endif
514
515
0
#ifdef ENAMETOOLONG
516
0
    case ENAMETOOLONG:
517
0
      return G_FILE_ERROR_NAMETOOLONG;
518
0
#endif
519
520
0
#ifdef ENOENT
521
0
    case ENOENT:
522
0
      return G_FILE_ERROR_NOENT;
523
0
#endif
524
525
0
#ifdef ENOTDIR
526
0
    case ENOTDIR:
527
0
      return G_FILE_ERROR_NOTDIR;
528
0
#endif
529
530
0
#ifdef ENXIO
531
0
    case ENXIO:
532
0
      return G_FILE_ERROR_NXIO;
533
0
#endif
534
535
0
#ifdef ENODEV
536
0
    case ENODEV:
537
0
      return G_FILE_ERROR_NODEV;
538
0
#endif
539
540
0
#ifdef EROFS
541
0
    case EROFS:
542
0
      return G_FILE_ERROR_ROFS;
543
0
#endif
544
545
0
#ifdef ETXTBSY
546
0
    case ETXTBSY:
547
0
      return G_FILE_ERROR_TXTBSY;
548
0
#endif
549
550
0
#ifdef EFAULT
551
0
    case EFAULT:
552
0
      return G_FILE_ERROR_FAULT;
553
0
#endif
554
555
0
#ifdef ELOOP
556
0
    case ELOOP:
557
0
      return G_FILE_ERROR_LOOP;
558
0
#endif
559
560
0
#ifdef ENOSPC
561
0
    case ENOSPC:
562
0
      return G_FILE_ERROR_NOSPC;
563
0
#endif
564
565
0
#ifdef ENOMEM
566
0
    case ENOMEM:
567
0
      return G_FILE_ERROR_NOMEM;
568
0
#endif
569
570
0
#ifdef EMFILE
571
0
    case EMFILE:
572
0
      return G_FILE_ERROR_MFILE;
573
0
#endif
574
575
0
#ifdef ENFILE
576
0
    case ENFILE:
577
0
      return G_FILE_ERROR_NFILE;
578
0
#endif
579
580
0
#ifdef EBADF
581
0
    case EBADF:
582
0
      return G_FILE_ERROR_BADF;
583
0
#endif
584
585
0
#ifdef EINVAL
586
0
    case EINVAL:
587
0
      return G_FILE_ERROR_INVAL;
588
0
#endif
589
590
0
#ifdef EPIPE
591
0
    case EPIPE:
592
0
      return G_FILE_ERROR_PIPE;
593
0
#endif
594
595
0
#ifdef EAGAIN
596
0
    case EAGAIN:
597
0
      return G_FILE_ERROR_AGAIN;
598
0
#endif
599
600
0
#ifdef EINTR
601
0
    case EINTR:
602
0
      return G_FILE_ERROR_INTR;
603
0
#endif
604
605
0
#ifdef EIO
606
0
    case EIO:
607
0
      return G_FILE_ERROR_IO;
608
0
#endif
609
610
0
#ifdef EPERM
611
0
    case EPERM:
612
0
      return G_FILE_ERROR_PERM;
613
0
#endif
614
615
0
#ifdef ENOSYS
616
0
    case ENOSYS:
617
0
      return G_FILE_ERROR_NOSYS;
618
0
#endif
619
620
0
    default:
621
0
      return G_FILE_ERROR_FAILED;
622
0
    }
623
0
}
624
625
static char *
626
format_error_message (const gchar  *filename,
627
                      const gchar  *format_string,
628
                      int           saved_errno) G_GNUC_FORMAT(2);
629
630
#pragma GCC diagnostic push
631
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
632
633
static char *
634
format_error_message (const gchar  *filename,
635
                      const gchar  *format_string,
636
                      int           saved_errno)
637
0
{
638
0
  gchar *display_name;
639
0
  gchar *msg;
640
641
0
  display_name = g_filename_display_name (filename);
642
0
  msg = g_strdup_printf (format_string, display_name, g_strerror (saved_errno));
643
0
  g_free (display_name);
644
645
0
  return msg;
646
0
}
647
648
#pragma GCC diagnostic pop
649
650
/* format string must have two '%s':
651
 *
652
 *   - the place for the filename
653
 *   - the place for the strerror
654
 */
655
static void
656
set_file_error (GError      **error,
657
                const gchar  *filename,
658
                const gchar  *format_string,
659
                int           saved_errno)
660
0
{
661
0
  char *msg = format_error_message (filename, format_string, saved_errno);
662
663
0
  g_set_error_literal (error, G_FILE_ERROR, g_file_error_from_errno (saved_errno),
664
0
                       msg);
665
0
  g_free (msg);
666
0
}
667
668
static gboolean
669
get_contents_stdio (const gchar  *filename,
670
                    FILE         *f,
671
                    gchar       **contents,
672
                    gsize        *length,
673
                    GError      **error)
674
0
{
675
0
  gchar buf[4096];
676
0
  gsize bytes;  /* always <= sizeof(buf) */
677
0
  gchar *str = NULL;
678
0
  gsize total_bytes = 0;
679
0
  gsize total_allocated = 0;
680
0
  gchar *tmp;
681
0
  gchar *display_filename;
682
683
0
  g_assert (f != NULL);
684
685
0
  while (!feof (f))
686
0
    {
687
0
      gint save_errno;
688
689
0
      bytes = fread (buf, 1, sizeof (buf), f);
690
0
      save_errno = errno;
691
692
0
      if (total_bytes > G_MAXSIZE - bytes)
693
0
          goto file_too_large;
694
695
      /* Possibility of overflow eliminated above. */
696
0
      while (total_bytes + bytes >= total_allocated)
697
0
        {
698
0
          if (str)
699
0
            {
700
0
              if (total_allocated > G_MAXSIZE / 2)
701
0
                  goto file_too_large;
702
0
              total_allocated *= 2;
703
0
            }
704
0
          else
705
0
            {
706
0
              total_allocated = MIN (bytes + 1, sizeof (buf));
707
0
            }
708
709
0
          tmp = g_try_realloc (str, total_allocated);
710
711
0
          if (tmp == NULL)
712
0
            {
713
0
              char *display_size = g_format_size_full (total_allocated, G_FORMAT_SIZE_LONG_FORMAT);
714
0
              display_filename = g_filename_display_name (filename);
715
0
              g_set_error (error,
716
0
                           G_FILE_ERROR,
717
0
                           G_FILE_ERROR_NOMEM,
718
                           /* Translators: the first %s contains the file size
719
                            * (already formatted with units), and the second %s
720
                            * contains the file name */
721
0
                           _("Could not allocate %s to read file “%s”"),
722
0
                           display_size,
723
0
                           display_filename);
724
0
              g_free (display_filename);
725
0
              g_free (display_size);
726
727
0
              goto error;
728
0
            }
729
730
0
    str = tmp;
731
0
        }
732
733
0
      if (ferror (f))
734
0
        {
735
0
          display_filename = g_filename_display_name (filename);
736
0
          g_set_error (error,
737
0
                       G_FILE_ERROR,
738
0
                       g_file_error_from_errno (save_errno),
739
0
                       _("Error reading file “%s”: %s"),
740
0
                       display_filename,
741
0
           g_strerror (save_errno));
742
0
          g_free (display_filename);
743
744
0
          goto error;
745
0
        }
746
747
0
      g_assert (str != NULL);
748
0
      memcpy (str + total_bytes, buf, bytes);
749
750
0
      total_bytes += bytes;
751
0
    }
752
753
0
  fclose (f);
754
755
0
  if (total_allocated == 0)
756
0
    {
757
0
      str = g_new (gchar, 1);
758
0
      total_bytes = 0;
759
0
    }
760
761
0
  str[total_bytes] = '\0';
762
763
0
  if (length)
764
0
    *length = total_bytes;
765
766
0
  *contents = str;
767
768
0
  return TRUE;
769
770
0
 file_too_large:
771
0
  display_filename = g_filename_display_name (filename);
772
0
  g_set_error (error,
773
0
               G_FILE_ERROR,
774
0
               G_FILE_ERROR_FAILED,
775
0
               _("File “%s” is too large"),
776
0
               display_filename);
777
0
  g_free (display_filename);
778
779
0
 error:
780
781
0
  g_free (str);
782
0
  fclose (f);
783
784
0
  return FALSE;
785
0
}
786
787
#ifndef G_OS_WIN32
788
789
static gboolean
790
get_contents_regfile (const gchar  *filename,
791
                      struct stat  *stat_buf,
792
                      gint          fd,
793
                      gchar       **contents,
794
                      gsize        *length,
795
                      GError      **error)
796
0
{
797
0
  gchar *buf;
798
0
  gsize bytes_read;
799
0
  gsize size;
800
0
  gsize alloc_size;
801
0
  gchar *display_filename;
802
803
0
  if ((G_MAXOFFSET >= G_MAXSIZE) && (stat_buf->st_size > (goffset) (G_MAXSIZE - 1)))
804
0
    {
805
0
      display_filename = g_filename_display_name (filename);
806
0
      g_set_error (error,
807
0
                   G_FILE_ERROR,
808
0
                   G_FILE_ERROR_FAILED,
809
0
                   _("File “%s” is too large"),
810
0
                   display_filename);
811
0
      g_free (display_filename);
812
0
      goto error;
813
0
    }
814
815
0
  size = stat_buf->st_size;
816
817
0
  alloc_size = size + 1;
818
0
  buf = g_try_malloc (alloc_size);
819
820
0
  if (buf == NULL)
821
0
    {
822
0
      char *display_size = g_format_size_full (alloc_size, G_FORMAT_SIZE_LONG_FORMAT);
823
0
      display_filename = g_filename_display_name (filename);
824
0
      g_set_error (error,
825
0
                   G_FILE_ERROR,
826
0
                   G_FILE_ERROR_NOMEM,
827
                   /* Translators: the first %s contains the file size
828
                    * (already formatted with units), and the second %s
829
                    * contains the file name */
830
0
                   _("Could not allocate %s to read file “%s”"),
831
0
                   display_size,
832
0
                   display_filename);
833
0
      g_free (display_filename);
834
0
      g_free (display_size);
835
0
      goto error;
836
0
    }
837
  
838
0
  bytes_read = 0;
839
0
  while (bytes_read < size)
840
0
    {
841
0
      gssize rc;
842
          
843
0
      rc = read (fd, buf + bytes_read, size - bytes_read);
844
845
0
      if (rc < 0)
846
0
        {
847
0
          if (errno != EINTR) 
848
0
            {
849
0
        int save_errno = errno;
850
851
0
              g_free (buf);
852
0
              display_filename = g_filename_display_name (filename);
853
0
              g_set_error (error,
854
0
                           G_FILE_ERROR,
855
0
                           g_file_error_from_errno (save_errno),
856
0
                           _("Failed to read from file “%s”: %s"),
857
0
                           display_filename, 
858
0
         g_strerror (save_errno));
859
0
              g_free (display_filename);
860
0
        goto error;
861
0
            }
862
0
        }
863
0
      else if (rc == 0)
864
0
        break;
865
0
      else
866
0
        bytes_read += rc;
867
0
    }
868
      
869
0
  buf[bytes_read] = '\0';
870
871
0
  if (length)
872
0
    *length = bytes_read;
873
  
874
0
  *contents = buf;
875
876
0
  close (fd);
877
878
0
  return TRUE;
879
880
0
error:
881
882
0
  close (fd);
883
  
884
0
  return FALSE;
885
0
}
886
887
static gboolean
888
get_contents_posix (const gchar  *filename,
889
                    gchar       **contents,
890
                    gsize        *length,
891
                    GError      **error)
892
0
{
893
0
  struct stat stat_buf;
894
0
  gint fd;
895
896
  /* O_BINARY useful on Cygwin */
897
0
  fd = open (filename, O_RDONLY | O_BINARY | O_CLOEXEC);
898
899
0
  if (fd < 0)
900
0
    {
901
0
      int saved_errno = errno;
902
903
0
      if (error)
904
0
        set_file_error (error,
905
0
                        filename,
906
0
                        _("Failed to open file “%s”: %s"),
907
0
                        saved_errno);
908
909
0
      return FALSE;
910
0
    }
911
912
  /* I don't think this will ever fail, aside from ENOMEM, but. */
913
0
  if (fstat (fd, &stat_buf) < 0)
914
0
    {
915
0
      int saved_errno = errno;
916
0
      if (error)
917
0
        set_file_error (error,
918
0
                        filename,
919
0
                        _("Failed to get attributes of file “%s”: fstat() failed: %s"),
920
0
                        saved_errno);
921
0
      close (fd);
922
923
0
      return FALSE;
924
0
    }
925
926
0
  if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
927
0
    {
928
0
      gboolean retval = get_contents_regfile (filename,
929
0
                &stat_buf,
930
0
                fd,
931
0
                contents,
932
0
                length,
933
0
                error);
934
935
0
      return retval;
936
0
    }
937
0
  else
938
0
    {
939
0
      FILE *f;
940
0
      gboolean retval;
941
942
0
      f = fdopen (fd, "re");
943
      
944
0
      if (f == NULL)
945
0
        {
946
0
          int saved_errno = errno;
947
0
          if (error)
948
0
            set_file_error (error,
949
0
                            filename,
950
0
                            _("Failed to open file “%s”: fdopen() failed: %s"),
951
0
                            saved_errno);
952
953
0
          return FALSE;
954
0
        }
955
  
956
0
      retval = get_contents_stdio (filename, f, contents, length, error);
957
958
0
      return retval;
959
0
    }
960
0
}
961
962
#else  /* G_OS_WIN32 */
963
964
static gboolean
965
get_contents_win32 (const gchar  *filename,
966
        gchar       **contents,
967
        gsize        *length,
968
        GError      **error)
969
{
970
  FILE *f;
971
  gboolean retval;
972
  
973
  f = g_fopen (filename, "rbe");
974
975
  if (f == NULL)
976
    {
977
      int saved_errno = errno;
978
      if (error)
979
        set_file_error (error,
980
                        filename,
981
                        _("Failed to open file “%s”: %s"),
982
                        saved_errno);
983
984
      return FALSE;
985
    }
986
  
987
  retval = get_contents_stdio (filename, f, contents, length, error);
988
989
  return retval;
990
}
991
992
#endif
993
994
/**
995
 * g_file_get_contents:
996
 * @filename: (type filename): name of a file to read contents from, in the GLib file name encoding
997
 * @contents: (out) (array length=length) (element-type guint8): location to store an allocated string, use g_free() to free
998
 *     the returned string
999
 * @length: (nullable): location to store length in bytes of the contents, or %NULL
1000
 * @error: return location for a #GError, or %NULL
1001
 *
1002
 * Reads an entire file into allocated memory, with good error
1003
 * checking.
1004
 *
1005
 * If the call was successful, it returns %TRUE and sets @contents to the file
1006
 * contents and @length to the length of the file contents in bytes. The string
1007
 * stored in @contents will be nul-terminated, so for text files you can pass
1008
 * %NULL for the @length argument. If the call was not successful, it returns
1009
 * %FALSE and sets @error. The error domain is %G_FILE_ERROR. Possible error
1010
 * codes are those in the #GFileError enumeration. In the error case,
1011
 * @contents is set to %NULL and @length is set to zero.
1012
 *
1013
 * Returns: %TRUE on success, %FALSE if an error occurred
1014
 **/
1015
gboolean
1016
g_file_get_contents (const gchar  *filename,
1017
                     gchar       **contents,
1018
                     gsize        *length,
1019
                     GError      **error)
1020
0
{  
1021
0
  g_return_val_if_fail (filename != NULL, FALSE);
1022
0
  g_return_val_if_fail (contents != NULL, FALSE);
1023
1024
0
  *contents = NULL;
1025
0
  if (length)
1026
0
    *length = 0;
1027
1028
#ifdef G_OS_WIN32
1029
  return get_contents_win32 (filename, contents, length, error);
1030
#else
1031
0
  return get_contents_posix (filename, contents, length, error);
1032
0
#endif
1033
0
}
1034
1035
static gboolean
1036
rename_file (const char  *old_name,
1037
             const char  *new_name,
1038
             gboolean     do_fsync,
1039
             GError     **err)
1040
0
{
1041
0
  errno = 0;
1042
0
  if (g_rename (old_name, new_name) == -1)
1043
0
    {
1044
0
      int save_errno = errno;
1045
0
      gchar *display_old_name = g_filename_display_name (old_name);
1046
0
      gchar *display_new_name = g_filename_display_name (new_name);
1047
1048
0
      g_set_error (err,
1049
0
       G_FILE_ERROR,
1050
0
       g_file_error_from_errno (save_errno),
1051
0
       _("Failed to rename file “%s” to “%s”: g_rename() failed: %s"),
1052
0
       display_old_name,
1053
0
       display_new_name,
1054
0
       g_strerror (save_errno));
1055
1056
0
      g_free (display_old_name);
1057
0
      g_free (display_new_name);
1058
      
1059
0
      return FALSE;
1060
0
    }
1061
1062
  /* In order to guarantee that the *new* contents of the file are seen in
1063
   * future, fsync() the directory containing the file. Otherwise if the file
1064
   * system was unmounted cleanly now, it would be undefined whether the old
1065
   * or new contents of the file were visible after recovery.
1066
   *
1067
   * This assumes the @old_name and @new_name are in the same directory. */
1068
0
#ifdef HAVE_FSYNC
1069
0
  if (do_fsync)
1070
0
    {
1071
0
      gchar *dir = g_path_get_dirname (new_name);
1072
0
      int dir_fd = g_open (dir, O_RDONLY | O_CLOEXEC, 0);
1073
1074
0
      if (dir_fd >= 0)
1075
0
        {
1076
0
          g_fsync (dir_fd);
1077
0
          g_close (dir_fd, NULL);
1078
0
        }
1079
1080
0
      g_free (dir);
1081
0
    }
1082
0
#endif  /* HAVE_FSYNC */
1083
1084
0
  return TRUE;
1085
0
}
1086
1087
static gboolean
1088
fd_should_be_fsynced (int                    fd,
1089
                      const gchar           *test_file,
1090
                      GFileSetContentsFlags  flags)
1091
0
{
1092
0
#ifdef HAVE_FSYNC
1093
0
  struct stat statbuf;
1094
1095
  /* If the final destination exists and is > 0 bytes, we want to sync the
1096
   * newly written file to ensure the data is on disk when we rename over
1097
   * the destination. Otherwise if we get a system crash we can lose both
1098
   * the new and the old file on some filesystems. (I.E. those that don't
1099
   * guarantee the data is written to the disk before the metadata.)
1100
   *
1101
   * There is no difference (in file system terms) if the old file doesn’t
1102
   * already exist, apart from the fact that if the system crashes and the new
1103
   * data hasn’t been fsync()ed, there is only one bit of old data to lose (that
1104
   * the file didn’t exist in the first place). In some situations, such as
1105
   * trashing files, the old file never exists, so it seems reasonable to avoid
1106
   * the fsync(). This is not a widely applicable optimisation though.
1107
   */
1108
0
  if ((flags & (G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_DURABLE)) &&
1109
0
      (flags & G_FILE_SET_CONTENTS_ONLY_EXISTING))
1110
0
    {
1111
0
      errno = 0;
1112
0
      if (g_lstat (test_file, &statbuf) == 0)
1113
0
        return (statbuf.st_size > 0);
1114
0
      else if (errno == ENOENT)
1115
0
        return FALSE;
1116
0
      else
1117
0
        return TRUE;  /* lstat() failed; be cautious */
1118
0
    }
1119
0
  else
1120
0
    {
1121
0
      return (flags & (G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_DURABLE));
1122
0
    }
1123
#else  /* if !HAVE_FSYNC */
1124
  return FALSE;
1125
#endif  /* !HAVE_FSYNC */
1126
0
}
1127
1128
static gboolean
1129
truncate_file (int          fd,
1130
               off_t        length,
1131
               const char  *dest_file,
1132
               GError     **error)
1133
0
{
1134
0
  while (
1135
#ifdef G_OS_WIN32
1136
    g_win32_ftruncate (fd, length) < 0
1137
#else
1138
0
    ftruncate (fd, length) < 0
1139
0
#endif
1140
0
    )
1141
0
    {
1142
0
      int saved_errno = errno;
1143
1144
0
      if (saved_errno == EINTR)
1145
0
        continue;
1146
1147
0
      if (error != NULL)
1148
0
        set_file_error (error,
1149
0
                        dest_file,
1150
0
                        _("Failed to write file “%s”: ftruncate() failed: %s"),
1151
0
                        saved_errno);
1152
0
      return FALSE;
1153
0
    }
1154
1155
0
  return TRUE;
1156
0
}
1157
1158
/* closes @fd once it’s finished (on success or error) */
1159
static gboolean
1160
write_to_file (const gchar  *contents,
1161
               gsize         length,
1162
               int           fd,
1163
               const gchar  *dest_file,
1164
               gboolean      do_fsync,
1165
               GError      **err)
1166
0
{
1167
0
#ifdef HAVE_FALLOCATE
1168
0
  if (length > 0)
1169
0
    {
1170
      /* We do this on a 'best effort' basis... It may not be supported
1171
       * on the underlying filesystem.
1172
       */
1173
0
      (void) fallocate (fd, 0, 0, length);
1174
0
    }
1175
0
#endif
1176
0
  while (length > 0)
1177
0
    {
1178
0
      gssize s;
1179
1180
#ifdef G_OS_WIN32
1181
      /* 'write' on windows uses int types, so limit count to G_MAXINT */
1182
      s = write (fd, contents, MIN (length, (gsize) G_MAXINT));
1183
#else
1184
      /* Limit count to G_MAXSSIZE to fit into the return value. */
1185
0
      s = write (fd, contents, MIN (length, (gsize) G_MAXSSIZE));
1186
0
#endif
1187
0
      if (s < 0)
1188
0
        {
1189
0
          int saved_errno = errno;
1190
0
          if (saved_errno == EINTR)
1191
0
            continue;
1192
1193
0
          if (err)
1194
0
            set_file_error (err,
1195
0
                            dest_file, _("Failed to write file “%s”: write() failed: %s"),
1196
0
                            saved_errno);
1197
0
          close (fd);
1198
1199
0
          return FALSE;
1200
0
        }
1201
1202
0
      g_assert ((gsize) s <= length);
1203
1204
0
      contents += s;
1205
0
      length -= s;
1206
0
    }
1207
1208
1209
0
#ifdef HAVE_FSYNC
1210
0
  errno = 0;
1211
0
  if (do_fsync && g_fsync (fd) != 0)
1212
0
    {
1213
0
      int saved_errno = errno;
1214
0
      if (err)
1215
0
        set_file_error (err,
1216
0
                        dest_file, _("Failed to write file “%s”: fsync() failed: %s"),
1217
0
                        saved_errno);
1218
0
      close (fd);
1219
1220
0
      return FALSE;
1221
0
    }
1222
0
#endif
1223
1224
0
  errno = 0;
1225
0
  if (!g_close (fd, err))
1226
0
    return FALSE;
1227
1228
0
  return TRUE;
1229
0
}
1230
1231
/**
1232
 * g_file_set_contents:
1233
 * @filename: (type filename): name of a file to write @contents to, in the GLib file name
1234
 *   encoding
1235
 * @contents: (array length=length) (element-type guint8): string to write to the file
1236
 * @length: length of @contents, or -1 if @contents is a nul-terminated string
1237
 * @error: return location for a #GError, or %NULL
1238
 *
1239
 * Writes all of @contents to a file named @filename. This is a convenience
1240
 * wrapper around calling g_file_set_contents_full() with `flags` set to
1241
 * `G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING` and
1242
 * `mode` set to `0666`.
1243
 *
1244
 * Returns: %TRUE on success, %FALSE if an error occurred
1245
 *
1246
 * Since: 2.8
1247
 */
1248
gboolean
1249
g_file_set_contents (const gchar  *filename,
1250
                     const gchar  *contents,
1251
                     gssize        length,
1252
                     GError      **error)
1253
0
{
1254
0
  return g_file_set_contents_full (filename, contents, length,
1255
0
                                   G_FILE_SET_CONTENTS_CONSISTENT |
1256
0
                                   G_FILE_SET_CONTENTS_ONLY_EXISTING,
1257
0
                                   0666, error);
1258
0
}
1259
1260
/**
1261
 * g_file_set_contents_full:
1262
 * @filename: (type filename): name of a file to write @contents to, in the GLib file name
1263
 *   encoding
1264
 * @contents: (array length=length) (element-type guint8): string to write to the file
1265
 * @length: length of @contents, or -1 if @contents is a nul-terminated string
1266
 * @flags: flags controlling the safety vs speed of the operation
1267
 * @mode: file mode, as passed to `open()`; typically this will be `0666`
1268
 * @error: return location for a #GError, or %NULL
1269
 *
1270
 * Writes all of @contents to a file named @filename, with good error checking.
1271
 * If a file called @filename already exists it will be overwritten.
1272
 *
1273
 * @flags control the properties of the write operation: whether it’s atomic,
1274
 * and what the tradeoff is between returning quickly or being resilient to
1275
 * system crashes.
1276
 *
1277
 * As this function performs file I/O, it is recommended to not call it anywhere
1278
 * where blocking would cause problems, such as in the main loop of a graphical
1279
 * application. In particular, if @flags has any value other than
1280
 * %G_FILE_SET_CONTENTS_NONE then this function may call `fsync()`.
1281
 *
1282
 * If %G_FILE_SET_CONTENTS_CONSISTENT is set in @flags, the operation is atomic
1283
 * in the sense that it is first written to a temporary file which is then
1284
 * renamed to the final name.
1285
 *
1286
 * Notes:
1287
 *
1288
 * - On UNIX, if @filename already exists hard links to @filename will break.
1289
 *   Also since the file is recreated, existing permissions, access control
1290
 *   lists, metadata etc. may be lost. If @filename is a symbolic link,
1291
 *   the link itself will be replaced, not the linked file.
1292
 *
1293
 * - On UNIX, if @filename already exists and is non-empty, and if the system
1294
 *   supports it (via a journalling filesystem or equivalent), and if
1295
 *   %G_FILE_SET_CONTENTS_CONSISTENT is set in @flags, the `fsync()` call (or
1296
 *   equivalent) will be used to ensure atomic replacement: @filename
1297
 *   will contain either its old contents or @contents, even in the face of
1298
 *   system power loss, the disk being unsafely removed, etc.
1299
 *
1300
 * - On UNIX, if @filename does not already exist or is empty, there is a
1301
 *   possibility that system power loss etc. after calling this function will
1302
 *   leave @filename empty or full of NUL bytes, depending on the underlying
1303
 *   filesystem, unless %G_FILE_SET_CONTENTS_DURABLE and
1304
 *   %G_FILE_SET_CONTENTS_CONSISTENT are set in @flags.
1305
 *
1306
 * - On Windows renaming a file will not remove an existing file with the
1307
 *   new name, so on Windows there is a race condition between the existing
1308
 *   file being removed and the temporary file being renamed.
1309
 *
1310
 * - On Windows there is no way to remove a file that is open to some
1311
 *   process, or mapped into memory. Thus, this function will fail if
1312
 *   @filename already exists and is open.
1313
 *
1314
 * If the call was successful, it returns %TRUE. If the call was not successful,
1315
 * it returns %FALSE and sets @error. The error domain is %G_FILE_ERROR.
1316
 * Possible error codes are those in the #GFileError enumeration.
1317
 *
1318
 * Note that the name for the temporary file is constructed by appending up
1319
 * to 7 characters to @filename.
1320
 *
1321
 * If the file didn’t exist before and is created, it will be given the
1322
 * permissions from @mode. Otherwise, the permissions of the existing file will
1323
 * remain unchanged.
1324
 *
1325
 * Returns: %TRUE on success, %FALSE if an error occurred
1326
 *
1327
 * Since: 2.66
1328
 */
1329
gboolean
1330
g_file_set_contents_full (const gchar            *filename,
1331
                          const gchar            *contents,
1332
                          gssize                  length,
1333
                          GFileSetContentsFlags   flags,
1334
                          int                     mode,
1335
                          GError                **error)
1336
0
{
1337
0
  g_return_val_if_fail (filename != NULL, FALSE);
1338
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1339
0
  g_return_val_if_fail (contents != NULL || length == 0, FALSE);
1340
0
  g_return_val_if_fail (length >= -1, FALSE);
1341
1342
  /* @flags are handled as follows:
1343
   *  - %G_FILE_SET_CONTENTS_NONE: write directly to @filename, no fsync()s
1344
   *  - %G_FILE_SET_CONTENTS_CONSISTENT: write to temp file, fsync() it, rename()
1345
   *  - %G_FILE_SET_CONTENTS_CONSISTENT | ONLY_EXISTING: as above, but skip the
1346
   *    fsync() if @filename doesn’t exist or is empty
1347
   *  - %G_FILE_SET_CONTENTS_DURABLE: write directly to @filename, fsync() it
1348
   *  - %G_FILE_SET_CONTENTS_DURABLE | ONLY_EXISTING: as above, but skip the
1349
   *    fsync() if @filename doesn’t exist or is empty
1350
   *  - %G_FILE_SET_CONTENTS_CONSISTENT | DURABLE: write to temp file, fsync()
1351
   *    it, rename(), fsync() containing directory
1352
   *  - %G_FILE_SET_CONTENTS_CONSISTENT | DURABLE | ONLY_EXISTING: as above, but
1353
   *    skip both fsync()s if @filename doesn’t exist or is empty
1354
   */
1355
1356
0
  if (length < 0)
1357
0
    length = strlen (contents);
1358
1359
0
  if (flags & G_FILE_SET_CONTENTS_CONSISTENT)
1360
0
    {
1361
0
      gchar *tmp_filename = NULL;
1362
0
      GError *rename_error = NULL;
1363
0
      gboolean retval;
1364
0
      int fd;
1365
0
      gboolean do_fsync;
1366
0
      GStatBuf old_stat;
1367
1368
0
      tmp_filename = g_strdup_printf ("%s.XXXXXX", filename);
1369
1370
0
      errno = 0;
1371
0
      fd = g_mkstemp_full (tmp_filename, O_RDWR | O_BINARY | O_CLOEXEC, mode);
1372
1373
0
      if (fd == -1)
1374
0
        {
1375
0
          int saved_errno = errno;
1376
0
          if (error)
1377
0
            set_file_error (error,
1378
0
                            tmp_filename, _("Failed to create file “%s”: %s"),
1379
0
                            saved_errno);
1380
0
          retval = FALSE;
1381
0
          goto consistent_out;
1382
0
        }
1383
1384
      /* Maintain the permissions of the file if it exists */
1385
0
      if (!g_stat (filename, &old_stat))
1386
0
        {
1387
0
#ifndef G_OS_WIN32
1388
0
          if (fchmod (fd, old_stat.st_mode))
1389
#else  /* G_OS_WIN32 */
1390
          if (g_chmod (tmp_filename, old_stat.st_mode))
1391
#endif /* G_OS_WIN32 */
1392
0
            {
1393
0
              int saved_errno = errno;
1394
0
              if (error)
1395
0
                set_file_error (error,
1396
0
                                tmp_filename, _ ("Failed to set permissions of “%s”: %s"),
1397
0
                                saved_errno);
1398
0
              g_unlink (tmp_filename);
1399
0
              retval = FALSE;
1400
0
              goto consistent_out;
1401
0
            }
1402
0
        }
1403
1404
0
      do_fsync = fd_should_be_fsynced (fd, filename, flags);
1405
0
      if (!write_to_file (contents, length, g_steal_fd (&fd), tmp_filename, do_fsync, error))
1406
0
        {
1407
0
          g_unlink (tmp_filename);
1408
0
          retval = FALSE;
1409
0
          goto consistent_out;
1410
0
        }
1411
1412
0
      if (!rename_file (tmp_filename, filename, do_fsync, &rename_error))
1413
0
        {
1414
0
#ifndef G_OS_WIN32
1415
1416
0
          g_unlink (tmp_filename);
1417
0
          g_propagate_error (error, rename_error);
1418
0
          retval = FALSE;
1419
0
          goto consistent_out;
1420
1421
#else /* G_OS_WIN32 */
1422
1423
          /* Renaming failed, but on Windows this may just mean
1424
           * the file already exists. So if the target file
1425
           * exists, try deleting it and do the rename again.
1426
           */
1427
          if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1428
            {
1429
              g_unlink (tmp_filename);
1430
              g_propagate_error (error, rename_error);
1431
              retval = FALSE;
1432
              goto consistent_out;
1433
            }
1434
1435
          g_error_free (rename_error);
1436
1437
          if (g_unlink (filename) == -1)
1438
            {
1439
              int saved_errno = errno;
1440
              if (error)
1441
                set_file_error (error,
1442
                                filename,
1443
                                _("Existing file “%s” could not be removed: g_unlink() failed: %s"),
1444
                                saved_errno);
1445
              g_unlink (tmp_filename);
1446
              retval = FALSE;
1447
              goto consistent_out;
1448
            }
1449
1450
          if (!rename_file (tmp_filename, filename, flags, error))
1451
            {
1452
              g_unlink (tmp_filename);
1453
              retval = FALSE;
1454
              goto consistent_out;
1455
            }
1456
1457
#endif  /* G_OS_WIN32 */
1458
0
        }
1459
1460
0
      retval = TRUE;
1461
1462
0
consistent_out:
1463
0
      g_free (tmp_filename);
1464
0
      return retval;
1465
0
    }
1466
0
  else
1467
0
    {
1468
0
      int direct_fd;
1469
0
      int open_flags;
1470
0
      gboolean do_fsync;
1471
1472
0
      open_flags = O_RDWR | O_BINARY | O_CREAT | O_CLOEXEC;
1473
0
#ifdef O_NOFOLLOW
1474
      /* Windows doesn’t have symlinks, so O_NOFOLLOW is unnecessary there. */
1475
0
      open_flags |= O_NOFOLLOW;
1476
0
#endif
1477
1478
0
      errno = 0;
1479
0
      direct_fd = g_open (filename, open_flags, mode);
1480
1481
0
      if (direct_fd < 0)
1482
0
        {
1483
0
          int saved_errno = errno;
1484
1485
0
#ifdef O_NOFOLLOW
1486
          /* ELOOP indicates that @filename is a symlink, since we used
1487
           * O_NOFOLLOW (alternately it could indicate that @filename contains
1488
           * looping or too many symlinks). In either case, try again on the
1489
           * %G_FILE_SET_CONTENTS_CONSISTENT code path.
1490
           *
1491
           * FreeBSD uses EMLINK instead of ELOOP
1492
           * (https://www.freebsd.org/cgi/man.cgi?query=open&sektion=2#STANDARDS),
1493
           * and NetBSD uses EFTYPE
1494
           * (https://netbsd.gw.com/cgi-bin/man-cgi?open+2+NetBSD-current). */
1495
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1496
          if (saved_errno == EMLINK)
1497
#elif defined(__NetBSD__)
1498
          if (saved_errno == EFTYPE)
1499
#else
1500
0
          if (saved_errno == ELOOP)
1501
0
#endif
1502
0
            return g_file_set_contents_full (filename, contents, length,
1503
0
                                             flags | G_FILE_SET_CONTENTS_CONSISTENT,
1504
0
                                             mode, error);
1505
0
#endif  /* O_NOFOLLOW */
1506
1507
0
          if (error)
1508
0
            set_file_error (error,
1509
0
                            filename, _("Failed to open file “%s”: %s"),
1510
0
                            saved_errno);
1511
0
          return FALSE;
1512
0
        }
1513
1514
0
      do_fsync = fd_should_be_fsynced (direct_fd, filename, flags);
1515
0
      if (!truncate_file (direct_fd, 0, filename, error))
1516
0
        return FALSE;
1517
0
      if (!write_to_file (contents, length, g_steal_fd (&direct_fd), filename,
1518
0
                          do_fsync, error))
1519
0
        return FALSE;
1520
0
    }
1521
1522
0
  return TRUE;
1523
0
}
1524
1525
/*
1526
 * get_tmp_file based on the mkstemp implementation from the GNU C library.
1527
 * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
1528
 */
1529
typedef gint (*GTmpFileCallback) (const gchar *, gint, gint);
1530
1531
static gint
1532
get_tmp_file (gchar            *tmpl,
1533
              GTmpFileCallback  f,
1534
              int               flags,
1535
              int               mode)
1536
0
{
1537
0
  char *XXXXXX;
1538
0
  int count, fd;
1539
0
  static const char letters[] =
1540
0
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1541
0
  static const int NLETTERS = sizeof (letters) - 1;
1542
0
  guint64 value;
1543
0
  guint64 now_us;
1544
0
  static guint counter = 0;
1545
1546
0
  g_return_val_if_fail (tmpl != NULL, -1);
1547
1548
  /* find the last occurrence of "XXXXXX" */
1549
0
  XXXXXX = g_strrstr (tmpl, "XXXXXX");
1550
1551
0
  if (!XXXXXX || strncmp (XXXXXX, "XXXXXX", 6))
1552
0
    {
1553
0
      errno = EINVAL;
1554
0
      return -1;
1555
0
    }
1556
1557
  /* Get some more or less random data.  */
1558
0
  now_us = g_get_real_time ();
1559
0
  value = ((now_us % G_USEC_PER_SEC) ^ (now_us / G_USEC_PER_SEC)) + counter++;
1560
1561
0
  for (count = 0; count < 100; value += 7777, ++count)
1562
0
    {
1563
0
      guint64 v = value;
1564
1565
      /* Fill in the random bits.  */
1566
0
      XXXXXX[0] = letters[v % NLETTERS];
1567
0
      v /= NLETTERS;
1568
0
      XXXXXX[1] = letters[v % NLETTERS];
1569
0
      v /= NLETTERS;
1570
0
      XXXXXX[2] = letters[v % NLETTERS];
1571
0
      v /= NLETTERS;
1572
0
      XXXXXX[3] = letters[v % NLETTERS];
1573
0
      v /= NLETTERS;
1574
0
      XXXXXX[4] = letters[v % NLETTERS];
1575
0
      v /= NLETTERS;
1576
0
      XXXXXX[5] = letters[v % NLETTERS];
1577
1578
0
      fd = f (tmpl, flags, mode);
1579
1580
0
      if (fd >= 0)
1581
0
        return fd;
1582
0
      else if (errno != EEXIST)
1583
        /* Any other error will apply also to other names we might
1584
         *  try, and there are 2^32 or so of them, so give up now.
1585
         */
1586
0
        return -1;
1587
0
    }
1588
1589
  /* We got out of the loop because we ran out of combinations to try.  */
1590
0
  errno = EEXIST;
1591
0
  return -1;
1592
0
}
1593
1594
/* Some GTmpFileCallback implementations.
1595
 *
1596
 * Note: we cannot use open() or g_open() directly because even though
1597
 * they appear compatible, they may be vararg functions and calling
1598
 * varargs functions through a non-varargs type is undefined.
1599
 */
1600
static gint
1601
wrap_g_mkdir (const gchar *filename,
1602
              int          flags G_GNUC_UNUSED,
1603
              int          mode)
1604
0
{
1605
  /* tmpl is in UTF-8 on Windows, thus use g_mkdir() */
1606
0
  return g_mkdir (filename, mode);
1607
0
}
1608
1609
static gint
1610
wrap_g_open (const gchar *filename,
1611
                int          flags,
1612
                int          mode)
1613
0
{
1614
0
  return g_open (filename, flags, mode);
1615
0
}
1616
1617
/**
1618
 * g_mkdtemp_full: (skip)
1619
 * @tmpl: (type filename): template directory name
1620
 * @mode: permissions to create the temporary directory with
1621
 *
1622
 * Creates a temporary directory in the current directory.
1623
 *
1624
 * See the [`mkdtemp()`](man:mkdtemp(3)) documentation on most UNIX-like systems.
1625
 *
1626
 * The parameter is a string that should follow the rules for
1627
 * mkdtemp() templates, i.e. contain the string "XXXXXX".
1628
 * g_mkdtemp_full() is slightly more flexible than mkdtemp() in that the
1629
 * sequence does not have to occur at the very end of the template
1630
 * and you can pass a @mode. The X string will be modified to form
1631
 * the name of a directory that didn't exist. The string should be
1632
 * in the GLib file name encoding. Most importantly, on Windows it
1633
 * should be in UTF-8.
1634
 *
1635
 * If you are going to be creating a temporary directory inside the
1636
 * directory returned by g_get_tmp_dir(), you might want to use
1637
 * g_dir_make_tmp() instead.
1638
 *
1639
 * Returns: (nullable) (type filename): A pointer to @tmpl, which has been
1640
 *   modified to hold the directory name. In case of errors, %NULL is
1641
 *   returned, and %errno will be set.
1642
 *
1643
 * Since: 2.30
1644
 */
1645
gchar *
1646
g_mkdtemp_full (gchar *tmpl,
1647
                gint   mode)
1648
0
{
1649
0
  if (get_tmp_file (tmpl, wrap_g_mkdir, 0, mode) == -1)
1650
0
    return NULL;
1651
0
  else
1652
0
    return tmpl;
1653
0
}
1654
1655
/**
1656
 * g_mkdtemp: (skip)
1657
 * @tmpl: (type filename): template directory name
1658
 *
1659
 * Creates a temporary directory in the current directory.
1660
 *
1661
 * See the [`mkdtemp()`](man:mkdtemp(3)) documentation on most UNIX-like systems.
1662
 *
1663
 * The parameter is a string that should follow the rules for
1664
 * mkdtemp() templates, i.e. contain the string "XXXXXX".
1665
 * g_mkdtemp() is slightly more flexible than mkdtemp() in that the
1666
 * sequence does not have to occur at the very end of the template.
1667
 * The X string will be modified to form the name of a directory that
1668
 * didn't exist.
1669
 * The string should be in the GLib file name encoding. Most importantly,
1670
 * on Windows it should be in UTF-8.
1671
 *
1672
 * If you are going to be creating a temporary directory inside the
1673
 * directory returned by g_get_tmp_dir(), you might want to use
1674
 * g_dir_make_tmp() instead.
1675
 *
1676
 * Returns: (nullable) (type filename): A pointer to @tmpl, which has been
1677
 *   modified to hold the directory name.  In case of errors, %NULL is
1678
 *   returned and %errno will be set.
1679
 *
1680
 * Since: 2.30
1681
 */
1682
gchar *
1683
g_mkdtemp (gchar *tmpl)
1684
0
{
1685
0
  return g_mkdtemp_full (tmpl, 0700);
1686
0
}
1687
1688
/**
1689
 * g_mkstemp_full: (skip)
1690
 * @tmpl: (type filename): template filename
1691
 * @flags: flags to pass to an open() call in addition to O_EXCL
1692
 *   and O_CREAT, which are passed automatically
1693
 * @mode: permissions to create the temporary file with
1694
 *
1695
 * Opens a temporary file in the current directory.
1696
 *
1697
 * See the [`mkstemp()`](man:mkstemp(3)) documentation on most UNIX-like systems.
1698
 *
1699
 * The parameter is a string that should follow the rules for
1700
 * mkstemp() templates, i.e. contain the string "XXXXXX".
1701
 * g_mkstemp_full() is slightly more flexible than mkstemp()
1702
 * in that the sequence does not have to occur at the very end of the
1703
 * template and you can pass a @mode and additional @flags. The X
1704
 * string will be modified to form the name of a file that didn't exist.
1705
 * The string should be in the GLib file name encoding. Most importantly,
1706
 * on Windows it should be in UTF-8.
1707
 *
1708
 * Returns: A file handle (as from open()) to the file
1709
 *   opened for reading and writing. The file handle should be
1710
 *   closed with close(). In case of errors, -1 is returned
1711
 *   and %errno will be set.
1712
 *
1713
 * Since: 2.22
1714
 */
1715
gint
1716
g_mkstemp_full (gchar *tmpl,
1717
                gint   flags,
1718
                gint   mode)
1719
0
{
1720
  /* tmpl is in UTF-8 on Windows, thus use g_open() */
1721
0
  return get_tmp_file (tmpl, wrap_g_open,
1722
0
                       flags | O_CREAT | O_EXCL, mode);
1723
0
}
1724
1725
/**
1726
 * g_mkstemp: (skip)
1727
 * @tmpl: (type filename): template filename
1728
 *
1729
 * Opens a temporary file in the current directory.
1730
 *
1731
 * See the [`mkstemp()`](man:mkstemp(3)) documentation on most UNIX-like systems.
1732
 *
1733
 * The parameter is a string that should follow the rules for
1734
 * mkstemp() templates, i.e. contain the string "XXXXXX".
1735
 * g_mkstemp() is slightly more flexible than mkstemp() in that the
1736
 * sequence does not have to occur at the very end of the template.
1737
 * The X string will be modified to form the name of a file that
1738
 * didn't exist. The string should be in the GLib file name encoding.
1739
 * Most importantly, on Windows it should be in UTF-8.
1740
 *
1741
 * Returns: A file handle (as from open()) to the file
1742
 *   opened for reading and writing. The file is opened in binary
1743
 *   mode on platforms where there is a difference. The file handle
1744
 *   should be closed with close(). In case of errors, -1 is
1745
 *   returned and %errno will be set.
1746
 */
1747
gint
1748
g_mkstemp (gchar *tmpl)
1749
0
{
1750
0
  return g_mkstemp_full (tmpl, O_RDWR | O_BINARY | O_CLOEXEC, 0600);
1751
0
}
1752
1753
static gint
1754
g_get_tmp_name (const gchar      *tmpl,
1755
                gchar           **name_used,
1756
                GTmpFileCallback  f,
1757
                gint              flags,
1758
                gint              mode,
1759
                GError          **error)
1760
0
{
1761
0
  int retval;
1762
0
  const char *tmpdir;
1763
0
  const char *sep;
1764
0
  char *fulltemplate;
1765
0
  const char *slash;
1766
1767
0
  if (tmpl == NULL)
1768
0
    tmpl = ".XXXXXX";
1769
1770
0
  if ((slash = strchr (tmpl, G_DIR_SEPARATOR)) != NULL
1771
#ifdef G_OS_WIN32
1772
      || (strchr (tmpl, '/') != NULL && (slash = "/"))
1773
#endif
1774
0
      )
1775
0
    {
1776
0
      gchar *display_tmpl = g_filename_display_name (tmpl);
1777
0
      char c[2];
1778
0
      c[0] = *slash;
1779
0
      c[1] = '\0';
1780
1781
0
      g_set_error (error,
1782
0
                   G_FILE_ERROR,
1783
0
                   G_FILE_ERROR_FAILED,
1784
0
                   _("Template “%s” invalid, should not contain a “%s”"),
1785
0
                   display_tmpl, c);
1786
0
      g_free (display_tmpl);
1787
1788
0
      return -1;
1789
0
    }
1790
1791
0
  if (strstr (tmpl, "XXXXXX") == NULL)
1792
0
    {
1793
0
      gchar *display_tmpl = g_filename_display_name (tmpl);
1794
0
      g_set_error (error,
1795
0
                   G_FILE_ERROR,
1796
0
                   G_FILE_ERROR_FAILED,
1797
0
                   _("Template “%s” doesn’t contain XXXXXX"),
1798
0
                   display_tmpl);
1799
0
      g_free (display_tmpl);
1800
0
      return -1;
1801
0
    }
1802
1803
0
  tmpdir = g_get_tmp_dir ();
1804
1805
0
  if (G_IS_DIR_SEPARATOR (tmpdir [strlen (tmpdir) - 1]))
1806
0
    sep = "";
1807
0
  else
1808
0
    sep = G_DIR_SEPARATOR_S;
1809
1810
0
  fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
1811
1812
0
  retval = get_tmp_file (fulltemplate, f, flags, mode);
1813
0
  if (retval == -1)
1814
0
    {
1815
0
      int saved_errno = errno;
1816
0
      if (error)
1817
0
        set_file_error (error,
1818
0
                        fulltemplate,
1819
0
                        _("Failed to create file “%s”: %s"),
1820
0
                        saved_errno);
1821
0
      g_free (fulltemplate);
1822
0
      return -1;
1823
0
    }
1824
1825
0
  *name_used = fulltemplate;
1826
1827
0
  return retval;
1828
0
}
1829
1830
/**
1831
 * g_file_open_tmp:
1832
 * @tmpl: (type filename) (nullable): Template for file name, as in
1833
 *   g_mkstemp(), basename only, or %NULL for a default template
1834
 * @name_used: (out) (type filename): location to store actual name used,
1835
 *   or %NULL
1836
 * @error: return location for a #GError
1837
 *
1838
 * Opens a file for writing in the preferred directory for temporary
1839
 * files (as returned by g_get_tmp_dir()).
1840
 *
1841
 * @tmpl should be a string in the GLib file name encoding containing
1842
 * a sequence of six 'X' characters, as the parameter to g_mkstemp().
1843
 * However, unlike these functions, the template should only be a
1844
 * basename, no directory components are allowed. If template is
1845
 * %NULL, a default template is used.
1846
 *
1847
 * Note that in contrast to g_mkstemp() (and mkstemp()) @tmpl is not
1848
 * modified, and might thus be a read-only literal string.
1849
 *
1850
 * Upon success, and if @name_used is non-%NULL, the actual name used
1851
 * is returned in @name_used. This string should be freed with g_free()
1852
 * when not needed any longer. The returned name is in the GLib file
1853
 * name encoding.
1854
 *
1855
 * Returns: A file handle (as from open()) to the file opened for
1856
 *   reading and writing. The file is opened in binary mode on platforms
1857
 *   where there is a difference. The file handle should be closed with
1858
 *   close(). In case of errors, -1 is returned and @error will be set.
1859
 */
1860
gint
1861
g_file_open_tmp (const gchar  *tmpl,
1862
                 gchar       **name_used,
1863
                 GError      **error)
1864
0
{
1865
0
  gchar *fulltemplate;
1866
0
  gint result;
1867
1868
0
  g_return_val_if_fail (error == NULL || *error == NULL, -1);
1869
1870
0
  result = g_get_tmp_name (tmpl, &fulltemplate,
1871
0
                           wrap_g_open,
1872
0
                           O_CREAT | O_EXCL | O_RDWR | O_BINARY | O_CLOEXEC,
1873
0
                           0600,
1874
0
                           error);
1875
0
  if (result != -1)
1876
0
    {
1877
0
      if (name_used)
1878
0
        *name_used = fulltemplate;
1879
0
      else
1880
0
        g_free (fulltemplate);
1881
0
    }
1882
1883
0
  return result;
1884
0
}
1885
1886
/**
1887
 * g_dir_make_tmp:
1888
 * @tmpl: (type filename) (nullable): Template for directory name,
1889
 *   as in g_mkdtemp(), basename only, or %NULL for a default template
1890
 * @error: return location for a #GError
1891
 *
1892
 * Creates a subdirectory in the preferred directory for temporary
1893
 * files (as returned by g_get_tmp_dir()).
1894
 *
1895
 * @tmpl should be a string in the GLib file name encoding containing
1896
 * a sequence of six 'X' characters, as the parameter to g_mkstemp().
1897
 * However, unlike these functions, the template should only be a
1898
 * basename, no directory components are allowed. If template is
1899
 * %NULL, a default template is used.
1900
 *
1901
 * Note that in contrast to g_mkdtemp() (and mkdtemp()) @tmpl is not
1902
 * modified, and might thus be a read-only literal string.
1903
 *
1904
 * Returns: (type filename) (transfer full): The actual name used. This string
1905
 *   should be freed with g_free() when not needed any longer and is
1906
 *   is in the GLib file name encoding. In case of errors, %NULL is
1907
 *   returned and @error will be set.
1908
 *
1909
 * Since: 2.30
1910
 */
1911
gchar *
1912
g_dir_make_tmp (const gchar  *tmpl,
1913
                GError      **error)
1914
0
{
1915
0
  gchar *fulltemplate;
1916
1917
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1918
1919
0
  if (g_get_tmp_name (tmpl, &fulltemplate, wrap_g_mkdir, 0, 0700, error) == -1)
1920
0
    return NULL;
1921
0
  else
1922
0
    return fulltemplate;
1923
0
}
1924
1925
static gchar *
1926
g_build_path_va (const gchar  *separator,
1927
     const gchar  *first_element,
1928
     va_list      *args,
1929
     gchar       **str_array)
1930
0
{
1931
0
  GString *result;
1932
0
  size_t separator_len = strlen (separator);
1933
0
  gboolean is_first = TRUE;
1934
0
  gboolean have_leading = FALSE;
1935
0
  const gchar *single_element = NULL;
1936
0
  const gchar *next_element;
1937
0
  const gchar *last_trailing = NULL;
1938
0
  size_t i = 0;
1939
1940
0
  result = g_string_new (NULL);
1941
1942
0
  if (str_array)
1943
0
    next_element = str_array[i++];
1944
0
  else
1945
0
    next_element = first_element;
1946
1947
0
  while (TRUE)
1948
0
    {
1949
0
      const gchar *element;
1950
0
      const gchar *start;
1951
0
      const gchar *end;
1952
1953
0
      if (next_element)
1954
0
  {
1955
0
    element = next_element;
1956
0
    if (str_array)
1957
0
      next_element = str_array[i++];
1958
0
    else
1959
0
      next_element = va_arg (*args, gchar *);
1960
0
  }
1961
0
      else
1962
0
  break;
1963
1964
      /* Ignore empty elements */
1965
0
      if (!*element)
1966
0
  continue;
1967
      
1968
0
      start = element;
1969
1970
0
      if (separator_len)
1971
0
  {
1972
0
    while (strncmp (start, separator, separator_len) == 0)
1973
0
      start += separator_len;
1974
0
        }
1975
1976
0
      end = start + strlen (start);
1977
      
1978
0
      if (separator_len)
1979
0
  {
1980
0
    while (end >= start + separator_len &&
1981
0
     strncmp (end - separator_len, separator, separator_len) == 0)
1982
0
      end -= separator_len;
1983
    
1984
0
    last_trailing = end;
1985
0
    while (last_trailing >= element + separator_len &&
1986
0
     strncmp (last_trailing - separator_len, separator, separator_len) == 0)
1987
0
      last_trailing -= separator_len;
1988
1989
0
    if (!have_leading)
1990
0
      {
1991
        /* If the leading and trailing separator strings are in the
1992
         * same element and overlap, the result is exactly that element
1993
         */
1994
0
        if (last_trailing <= start)
1995
0
    single_element = element;
1996
      
1997
0
        g_string_append_len (result, element, start - element);
1998
0
        have_leading = TRUE;
1999
0
      }
2000
0
    else
2001
0
      single_element = NULL;
2002
0
  }
2003
2004
0
      if (end == start)
2005
0
  continue;
2006
2007
0
      if (!is_first)
2008
0
  g_string_append (result, separator);
2009
      
2010
0
      g_string_append_len (result, start, end - start);
2011
0
      is_first = FALSE;
2012
0
    }
2013
2014
0
  if (single_element)
2015
0
    {
2016
0
      g_string_free (result, TRUE);
2017
0
      return g_strdup (single_element);
2018
0
    }
2019
0
  else
2020
0
    {
2021
0
      if (last_trailing)
2022
0
  g_string_append (result, last_trailing);
2023
  
2024
0
      return g_string_free (result, FALSE);
2025
0
    }
2026
0
}
2027
2028
/**
2029
 * g_build_pathv:
2030
 * @separator: a string used to separate the elements of the path.
2031
 * @args: (array zero-terminated=1) (element-type filename): %NULL-terminated
2032
 *   array of strings containing the path elements.
2033
 * 
2034
 * Behaves exactly like g_build_path(), but takes the path elements
2035
 * as a string array, instead of variadic arguments.
2036
 *
2037
 * This function is mainly meant for language bindings.
2038
 *
2039
 * Returns: (type filename) (transfer full): a newly-allocated string that
2040
 *     must be freed with g_free().
2041
 *
2042
 * Since: 2.8
2043
 */
2044
gchar *
2045
g_build_pathv (const gchar  *separator,
2046
         gchar       **args)
2047
0
{
2048
0
  if (!args)
2049
0
    return NULL;
2050
2051
0
  return g_build_path_va (separator, NULL, NULL, args);
2052
0
}
2053
2054
2055
/**
2056
 * g_build_path:
2057
 * @separator: (type filename): a string used to separate the elements of the path.
2058
 * @first_element: (type filename): the first element in the path
2059
 * @...: remaining elements in path, terminated by %NULL
2060
 * 
2061
 * Creates a path from a series of elements using @separator as the
2062
 * separator between elements.
2063
 *
2064
 * At the boundary between two elements, any trailing occurrences of
2065
 * separator in the first element, or leading occurrences of separator
2066
 * in the second element are removed and exactly one copy of the
2067
 * separator is inserted.
2068
 *
2069
 * Empty elements are ignored.
2070
 *
2071
 * The number of leading copies of the separator on the result is
2072
 * the same as the number of leading copies of the separator on
2073
 * the first non-empty element.
2074
 *
2075
 * The number of trailing copies of the separator on the result is
2076
 * the same as the number of trailing copies of the separator on
2077
 * the last non-empty element. (Determination of the number of
2078
 * trailing copies is done without stripping leading copies, so
2079
 * if the separator is `ABA`, then `ABABA` has 1 trailing copy.)
2080
 *
2081
 * However, if there is only a single non-empty element, and there
2082
 * are no characters in that element not part of the leading or
2083
 * trailing separators, then the result is exactly the original value
2084
 * of that element.
2085
 *
2086
 * Other than for determination of the number of leading and trailing
2087
 * copies of the separator, elements consisting only of copies
2088
 * of the separator are ignored.
2089
 *
2090
 * Returns: (type filename) (transfer full): the newly allocated path
2091
 **/
2092
gchar *
2093
g_build_path (const gchar *separator,
2094
        const gchar *first_element,
2095
        ...)
2096
0
{
2097
0
  gchar *str;
2098
0
  va_list args;
2099
2100
0
  g_return_val_if_fail (separator != NULL, NULL);
2101
2102
0
  va_start (args, first_element);
2103
0
  str = g_build_path_va (separator, first_element, &args, NULL);
2104
0
  va_end (args);
2105
2106
0
  return str;
2107
0
}
2108
2109
#ifdef G_OS_WIN32
2110
2111
static gchar *
2112
g_build_pathname_va (const gchar  *first_element,
2113
         va_list      *args,
2114
         gchar       **str_array)
2115
{
2116
  /* Code copied from g_build_pathv(), and modified to use two
2117
   * alternative single-character separators.
2118
   */
2119
  GString *result;
2120
  gboolean is_first = TRUE;
2121
  gboolean have_leading = FALSE;
2122
  const gchar *single_element = NULL;
2123
  const gchar *next_element;
2124
  const gchar *last_trailing = NULL;
2125
  gchar current_separator = '\\';
2126
  size_t i = 0;
2127
2128
  result = g_string_new (NULL);
2129
2130
  if (str_array)
2131
    next_element = str_array[i++];
2132
  else
2133
    next_element = first_element;
2134
  
2135
  while (TRUE)
2136
    {
2137
      const gchar *element;
2138
      const gchar *start;
2139
      const gchar *end;
2140
2141
      if (next_element)
2142
  {
2143
    element = next_element;
2144
    if (str_array)
2145
      next_element = str_array[i++];
2146
    else
2147
      next_element = va_arg (*args, gchar *);
2148
  }
2149
      else
2150
  break;
2151
2152
      /* Ignore empty elements */
2153
      if (!*element)
2154
  continue;
2155
      
2156
      start = element;
2157
2158
      if (TRUE)
2159
  {
2160
    while (start &&
2161
     (*start == '\\' || *start == '/'))
2162
      {
2163
        current_separator = *start;
2164
        start++;
2165
      }
2166
  }
2167
2168
      end = start + strlen (start);
2169
      
2170
      if (TRUE)
2171
  {
2172
    while (end >= start + 1 &&
2173
     (end[-1] == '\\' || end[-1] == '/'))
2174
      {
2175
        current_separator = end[-1];
2176
        end--;
2177
      }
2178
    
2179
    last_trailing = end;
2180
    while (last_trailing >= element + 1 &&
2181
     (last_trailing[-1] == '\\' || last_trailing[-1] == '/'))
2182
      last_trailing--;
2183
2184
    if (!have_leading)
2185
      {
2186
        /* If the leading and trailing separator strings are in the
2187
         * same element and overlap, the result is exactly that element
2188
         */
2189
        if (last_trailing <= start)
2190
    single_element = element;
2191
      
2192
        g_string_append_len (result, element, start - element);
2193
        have_leading = TRUE;
2194
      }
2195
    else
2196
      single_element = NULL;
2197
  }
2198
2199
      if (end == start)
2200
  continue;
2201
2202
      if (!is_first)
2203
  g_string_append_len (result, &current_separator, 1);
2204
      
2205
      g_string_append_len (result, start, end - start);
2206
      is_first = FALSE;
2207
    }
2208
2209
  if (single_element)
2210
    {
2211
      g_string_free (result, TRUE);
2212
      return g_strdup (single_element);
2213
    }
2214
  else
2215
    {
2216
      if (last_trailing)
2217
  g_string_append (result, last_trailing);
2218
  
2219
      return g_string_free (result, FALSE);
2220
    }
2221
}
2222
2223
#endif
2224
2225
static gchar *
2226
g_build_filename_va (const gchar  *first_argument,
2227
                     va_list      *args,
2228
                     gchar       **str_array)
2229
0
{
2230
0
  gchar *str;
2231
2232
0
#ifndef G_OS_WIN32
2233
0
  str = g_build_path_va (G_DIR_SEPARATOR_S, first_argument, args, str_array);
2234
#else
2235
  str = g_build_pathname_va (first_argument, args, str_array);
2236
#endif
2237
2238
0
  return str;
2239
0
}
2240
2241
/**
2242
 * g_build_filename_valist:
2243
 * @first_element: (type filename): the first element in the path
2244
 * @args: va_list of remaining elements in path
2245
 *
2246
 * Creates a filename from a list of elements using the correct
2247
 * separator for the current platform.
2248
 *
2249
 * Behaves exactly like g_build_filename(), but takes the path elements
2250
 * as a va_list.
2251
 *
2252
 * This function is mainly meant for implementing other variadic arguments
2253
 * functions.
2254
 *
2255
 * Returns: (type filename) (transfer full): the newly allocated path
2256
 *
2257
 * Since: 2.56
2258
 */
2259
gchar *
2260
g_build_filename_valist (const gchar  *first_element,
2261
                         va_list      *args)
2262
0
{
2263
0
  g_return_val_if_fail (first_element != NULL, NULL);
2264
2265
0
  return g_build_filename_va (first_element, args, NULL);
2266
0
}
2267
2268
/**
2269
 * g_build_filenamev:
2270
 * @args: (array zero-terminated=1) (element-type filename): %NULL-terminated
2271
 *   array of strings containing the path elements.
2272
 * 
2273
 * Creates a filename from a vector of elements using the correct
2274
 * separator for the current platform.
2275
 *
2276
 * This function behaves exactly like g_build_filename(), but takes the path
2277
 * elements as a string array, instead of varargs. This function is mainly
2278
 * meant for language bindings.
2279
 *
2280
 * If you are building a path programmatically you may want to use
2281
 * #GPathBuf instead.
2282
 *
2283
 * Returns: (type filename) (transfer full): the newly allocated path
2284
 *
2285
 * Since: 2.8
2286
 */
2287
gchar *
2288
g_build_filenamev (gchar **args)
2289
0
{
2290
0
  return g_build_filename_va (NULL, NULL, args);
2291
0
}
2292
2293
/**
2294
 * g_build_filename:
2295
 * @first_element: (type filename): the first element in the path
2296
 * @...: remaining elements in path, terminated by %NULL
2297
 * 
2298
 * Creates a filename from a series of elements using the correct
2299
 * separator for the current platform.
2300
 *
2301
 * On Unix, this function behaves identically to `g_build_path
2302
 * (G_DIR_SEPARATOR_S, first_element, ....)`.
2303
 *
2304
 * On Windows, it takes into account that either the backslash
2305
 * (`\` or slash (`/`) can be used as separator in filenames, but
2306
 * otherwise behaves as on UNIX. When file pathname separators need
2307
 * to be inserted, the one that last previously occurred in the
2308
 * parameters (reading from left to right) is used.
2309
 *
2310
 * No attempt is made to force the resulting filename to be an absolute
2311
 * path. If the first element is a relative path, the result will
2312
 * be a relative path.
2313
 *
2314
 * If you are building a path programmatically you may want to use
2315
 * #GPathBuf instead.
2316
 *
2317
 * Returns: (type filename) (transfer full): the newly allocated path
2318
 */
2319
gchar *
2320
g_build_filename (const gchar *first_element, 
2321
      ...)
2322
0
{
2323
0
  gchar *str;
2324
0
  va_list args;
2325
2326
0
  va_start (args, first_element);
2327
0
  str = g_build_filename_va (first_element, &args, NULL);
2328
0
  va_end (args);
2329
2330
0
  return str;
2331
0
}
2332
2333
/**
2334
 * g_file_read_link:
2335
 * @filename: (type filename): the symbolic link
2336
 * @error: return location for a #GError
2337
 *
2338
 * Reads the contents of the symbolic link @filename like the POSIX
2339
 * `readlink()` function.
2340
 *
2341
 * The returned string is in the encoding used for filenames. Use
2342
 * g_filename_to_utf8() to convert it to UTF-8.
2343
 *
2344
 * The returned string may also be a relative path. Use g_build_filename()
2345
 * to convert it to an absolute path:
2346
 *
2347
 * |[<!-- language="C" -->
2348
 * g_autoptr(GError) local_error = NULL;
2349
 * g_autofree gchar *link_target = g_file_read_link ("/etc/localtime", &local_error);
2350
 *
2351
 * if (local_error != NULL)
2352
 *   g_error ("Error reading link: %s", local_error->message);
2353
 *
2354
 * if (!g_path_is_absolute (link_target))
2355
 *   {
2356
 *     g_autofree gchar *absolute_link_target = g_build_filename ("/etc", link_target, NULL);
2357
 *     g_free (link_target);
2358
 *     link_target = g_steal_pointer (&absolute_link_target);
2359
 *   }
2360
 * ]|
2361
 *
2362
 * Returns: (type filename) (transfer full): A newly-allocated string with
2363
 *   the contents of the symbolic link, or %NULL if an error occurred.
2364
 *
2365
 * Since: 2.4
2366
 */
2367
gchar *
2368
g_file_read_link (const gchar  *filename,
2369
            GError      **error)
2370
0
{
2371
0
#if defined (HAVE_READLINK)
2372
0
  gchar *buffer;
2373
0
  size_t size;
2374
0
  gssize read_size;
2375
  
2376
0
  g_return_val_if_fail (filename != NULL, NULL);
2377
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2378
2379
0
  size = 256;
2380
0
  buffer = g_malloc (size);
2381
  
2382
0
  while (TRUE) 
2383
0
    {
2384
0
      read_size = readlink (filename, buffer, size);
2385
0
      if (read_size < 0)
2386
0
        {
2387
0
          int saved_errno = errno;
2388
0
          if (error)
2389
0
            set_file_error (error,
2390
0
                            filename,
2391
0
                            _("Failed to read the symbolic link “%s”: %s"),
2392
0
                            saved_errno);
2393
0
          g_free (buffer);
2394
0
          return NULL;
2395
0
        }
2396
    
2397
0
      if ((size_t) read_size < size)
2398
0
        {
2399
0
          buffer[read_size] = 0;
2400
0
          return buffer;
2401
0
        }
2402
      
2403
0
      size *= 2;
2404
0
      buffer = g_realloc (buffer, size);
2405
0
    }
2406
#elif defined (G_OS_WIN32)
2407
  gchar *buffer;
2408
  gssize read_size;
2409
  
2410
  g_return_val_if_fail (filename != NULL, NULL);
2411
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2412
2413
  read_size = g_win32_readlink_utf8 (filename, NULL, 0, &buffer, TRUE);
2414
  if (read_size < 0)
2415
    {
2416
      int saved_errno = errno;
2417
      if (error)
2418
        set_file_error (error,
2419
                        filename,
2420
                        _("Failed to read the symbolic link “%s”: %s"),
2421
                        saved_errno);
2422
      return NULL;
2423
    }
2424
  else if (read_size == 0)
2425
    return strdup ("");
2426
  else
2427
    return buffer;
2428
#else
2429
  g_return_val_if_fail (filename != NULL, NULL);
2430
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2431
2432
  g_set_error_literal (error,
2433
                       G_FILE_ERROR,
2434
                       G_FILE_ERROR_INVAL,
2435
                       _("Symbolic links not supported"));
2436
  
2437
  return NULL;
2438
#endif
2439
0
}
2440
2441
/**
2442
 * g_path_is_absolute:
2443
 * @file_name: (type filename): a file name
2444
 *
2445
 * Returns %TRUE if the given @file_name is an absolute file name.
2446
 * Note that this is a somewhat vague concept on Windows.
2447
 *
2448
 * On POSIX systems, an absolute file name is well-defined. It always
2449
 * starts from the single root directory. For example "/usr/local".
2450
 *
2451
 * On Windows, the concepts of current drive and drive-specific
2452
 * current directory introduce vagueness. This function interprets as
2453
 * an absolute file name one that either begins with a directory
2454
 * separator such as "\Users\tml" or begins with the root on a drive,
2455
 * for example "C:\Windows". The first case also includes UNC paths
2456
 * such as "\\\\myserver\docs\foo". In all cases, either slashes or
2457
 * backslashes are accepted.
2458
 *
2459
 * Note that a file name relative to the current drive root does not
2460
 * truly specify a file uniquely over time and across processes, as
2461
 * the current drive is a per-process value and can be changed.
2462
 *
2463
 * File names relative the current directory on some specific drive,
2464
 * such as "D:foo/bar", are not interpreted as absolute by this
2465
 * function, but they obviously are not relative to the normal current
2466
 * directory as returned by getcwd() or g_get_current_dir()
2467
 * either. Such paths should be avoided, or need to be handled using
2468
 * Windows-specific code.
2469
 *
2470
 * Returns: %TRUE if @file_name is absolute
2471
 */
2472
gboolean
2473
g_path_is_absolute (const gchar *file_name)
2474
0
{
2475
0
  g_return_val_if_fail (file_name != NULL, FALSE);
2476
2477
0
  if (G_IS_DIR_SEPARATOR (file_name[0]))
2478
0
    return TRUE;
2479
2480
#ifdef G_OS_WIN32
2481
  /* Recognize drive letter on native Windows */
2482
  if (g_ascii_isalpha (file_name[0]) &&
2483
      file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
2484
    return TRUE;
2485
#endif
2486
2487
0
  return FALSE;
2488
0
}
2489
2490
/**
2491
 * g_path_skip_root:
2492
 * @file_name: (type filename): a file name
2493
 *
2494
 * Returns a pointer into @file_name after the root component,
2495
 * i.e. after the "/" in UNIX or "C:\" under Windows. If @file_name
2496
 * is not an absolute path it returns %NULL.
2497
 *
2498
 * Returns: (type filename) (nullable): a pointer into @file_name after the
2499
 *     root component
2500
 */
2501
const gchar *
2502
g_path_skip_root (const gchar *file_name)
2503
0
{
2504
0
  g_return_val_if_fail (file_name != NULL, NULL);
2505
2506
#ifdef G_PLATFORM_WIN32
2507
  /* Skip \\server\share or //server/share */
2508
  if (G_IS_DIR_SEPARATOR (file_name[0]) &&
2509
      G_IS_DIR_SEPARATOR (file_name[1]) &&
2510
      file_name[2] &&
2511
      !G_IS_DIR_SEPARATOR (file_name[2]))
2512
    {
2513
      gchar *p;
2514
      p = strchr (file_name + 2, G_DIR_SEPARATOR);
2515
2516
#ifdef G_OS_WIN32
2517
      {
2518
        gchar *q;
2519
2520
        q = strchr (file_name + 2, '/');
2521
        if (p == NULL || (q != NULL && q < p))
2522
        p = q;
2523
      }
2524
#endif
2525
2526
      if (p && p > file_name + 2 && p[1])
2527
        {
2528
          file_name = p + 1;
2529
2530
          while (file_name[0] && !G_IS_DIR_SEPARATOR (file_name[0]))
2531
            file_name++;
2532
2533
          /* Possibly skip a backslash after the share name */
2534
          if (G_IS_DIR_SEPARATOR (file_name[0]))
2535
            file_name++;
2536
2537
          return (gchar *)file_name;
2538
        }
2539
    }
2540
#endif
2541
2542
  /* Skip initial slashes */
2543
0
  if (G_IS_DIR_SEPARATOR (file_name[0]))
2544
0
    {
2545
0
      while (G_IS_DIR_SEPARATOR (file_name[0]))
2546
0
        file_name++;
2547
0
      return (gchar *)file_name;
2548
0
    }
2549
2550
#ifdef G_OS_WIN32
2551
  /* Skip X:\ */
2552
  if (g_ascii_isalpha (file_name[0]) &&
2553
      file_name[1] == ':' &&
2554
      G_IS_DIR_SEPARATOR (file_name[2]))
2555
    return (gchar *)file_name + 3;
2556
#endif
2557
2558
0
  return NULL;
2559
0
}
2560
2561
/**
2562
 * g_basename:
2563
 * @file_name: (type filename): the name of the file
2564
 *
2565
 * Gets the name of the file without any leading directory
2566
 * components. It returns a pointer into the given file name
2567
 * string.
2568
 *
2569
 * Returns: (type filename): the name of the file without any leading
2570
 *   directory components
2571
 *
2572
 * Deprecated:2.2: Use g_path_get_basename() instead, but notice
2573
 *   that g_path_get_basename() allocates new memory for the
2574
 *   returned string, unlike this function which returns a pointer
2575
 *   into the argument.
2576
 */
2577
const gchar *
2578
g_basename (const gchar *file_name)
2579
0
{
2580
0
  const gchar *base;
2581
2582
0
  g_return_val_if_fail (file_name != NULL, NULL);
2583
2584
0
  base = strrchr (file_name, G_DIR_SEPARATOR);
2585
2586
#ifdef G_OS_WIN32
2587
  {
2588
    const gchar *q;
2589
    q = strrchr (file_name, '/');
2590
    if (base == NULL || (q != NULL && q > base))
2591
      base = q;
2592
  }
2593
#endif
2594
2595
0
  if (base)
2596
0
    return base + 1;
2597
2598
#ifdef G_OS_WIN32
2599
  if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
2600
    return (gchar*) file_name + 2;
2601
#endif
2602
2603
0
  return (gchar*) file_name;
2604
0
}
2605
2606
/**
2607
 * g_path_get_basename:
2608
 * @file_name: (type filename): the name of the file
2609
 *
2610
 * Gets the last component of the filename.
2611
 *
2612
 * If @file_name ends with a directory separator it gets the component
2613
 * before the last slash. If @file_name consists only of directory
2614
 * separators (and on Windows, possibly a drive letter), a single
2615
 * separator is returned. If @file_name is empty, it gets ".".
2616
 *
2617
 * Returns: (type filename) (transfer full): a newly allocated string
2618
 *   containing the last component of the filename
2619
 */
2620
gchar *
2621
g_path_get_basename (const gchar *file_name)
2622
0
{
2623
0
  size_t base;
2624
0
  size_t last_nonslash;
2625
0
  gsize len;
2626
0
  gchar *retval;
2627
2628
0
  g_return_val_if_fail (file_name != NULL, NULL);
2629
2630
0
  if (file_name[0] == '\0')
2631
0
    return g_strdup (".");
2632
2633
0
  last_nonslash = strlen (file_name) - 1;
2634
2635
0
  while (last_nonslash > 0 && G_IS_DIR_SEPARATOR (file_name[last_nonslash]))
2636
0
    last_nonslash--;
2637
2638
0
  if (last_nonslash == 0 && G_IS_DIR_SEPARATOR (file_name[0]))
2639
    /* string only containing slashes */
2640
0
    return g_strdup (G_DIR_SEPARATOR_S);
2641
2642
#ifdef G_OS_WIN32
2643
  if (last_nonslash == 1 &&
2644
      g_ascii_isalpha (file_name[0]) &&
2645
      file_name[1] == ':')
2646
    /* string only containing slashes and a drive */
2647
    return g_strdup (G_DIR_SEPARATOR_S);
2648
#endif
2649
0
  base = last_nonslash;
2650
2651
0
  while (base > 0 && !G_IS_DIR_SEPARATOR (file_name[base]))
2652
0
    base--;
2653
2654
  /* Does the file_name start without a directory separator, with the only
2655
   * directory separators being at the end of the string? e.g. `dir/` */
2656
0
  if (base == 0 && !G_IS_DIR_SEPARATOR (file_name[0]))
2657
0
    {
2658
0
      base = 0;
2659
2660
#ifdef G_OS_WIN32
2661
      /* Does it start with a drive letter? e.g. `C:dir/`
2662
       * If so, skip that. */
2663
      if (g_ascii_isalpha (file_name[0]) &&
2664
          file_name[1] == ':')
2665
        base = 2;
2666
#endif /* G_OS_WIN32 */
2667
0
    }
2668
0
  else
2669
0
    {
2670
      /* Otherwise, `base` now points at the last directory separator character
2671
       * before the component we want as the basename, so increase the index
2672
       * again. */
2673
0
      base += 1;
2674
0
    }
2675
2676
0
  len = last_nonslash - base + 1;
2677
0
  g_assert (len < SIZE_MAX);
2678
0
  retval = g_malloc (len + 1);
2679
0
  memcpy (retval, file_name + base, len);
2680
0
  retval[len] = '\0';
2681
2682
0
  return retval;
2683
0
}
2684
2685
/**
2686
 * g_dirname:
2687
 * @file_name: (type filename): the name of the file
2688
 *
2689
 * Gets the directory components of a file name.
2690
 *
2691
 * If the file name has no directory components "." is returned.
2692
 * The returned string should be freed when no longer needed.
2693
 *
2694
 * Returns: (type filename) (transfer full): the directory components of the file
2695
 *
2696
 * Deprecated: use g_path_get_dirname() instead
2697
 */
2698
2699
/**
2700
 * g_path_get_dirname:
2701
 * @file_name: (type filename): the name of the file
2702
 *
2703
 * Gets the directory components of a file name. For example, the directory
2704
 * component of `/usr/bin/test` is `/usr/bin`. The directory component of `/`
2705
 * is `/`.
2706
 *
2707
 * If the file name has no directory components "." is returned.
2708
 * The returned string should be freed when no longer needed.
2709
 *
2710
 * Returns: (type filename) (transfer full): the directory components of the file
2711
 */
2712
gchar *
2713
g_path_get_dirname (const gchar *file_name)
2714
0
{
2715
0
  const gchar *base;
2716
0
  gchar *base_p;
2717
0
  gsize len;
2718
2719
0
  g_return_val_if_fail (file_name != NULL, NULL);
2720
2721
0
  base = strrchr (file_name, G_DIR_SEPARATOR);
2722
2723
#ifdef G_OS_WIN32
2724
  {
2725
    const gchar *q;
2726
    q = strrchr (file_name, '/');
2727
    if (base == NULL || (q != NULL && q > base))
2728
      base = q;
2729
  }
2730
#endif
2731
2732
0
  if (!base)
2733
0
    {
2734
#ifdef G_OS_WIN32
2735
      if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
2736
        {
2737
          gchar drive_colon_dot[4];
2738
2739
          drive_colon_dot[0] = file_name[0];
2740
          drive_colon_dot[1] = ':';
2741
          drive_colon_dot[2] = '.';
2742
          drive_colon_dot[3] = '\0';
2743
2744
          return g_strdup (drive_colon_dot);
2745
        }
2746
#endif
2747
0
    return g_strdup (".");
2748
0
    }
2749
2750
0
  while (base > file_name && G_IS_DIR_SEPARATOR (*base))
2751
0
    base--;
2752
2753
#ifdef G_OS_WIN32
2754
  /* base points to the char before the last slash.
2755
   *
2756
   * In case file_name is the root of a drive (X:\) or a child of the
2757
   * root of a drive (X:\foo), include the slash.
2758
   *
2759
   * In case file_name is the root share of an UNC path
2760
   * (\\server\share), add a slash, returning \\server\share\ .
2761
   *
2762
   * In case file_name is a direct child of a share in an UNC path
2763
   * (\\server\share\foo), include the slash after the share name,
2764
   * returning \\server\share\ .
2765
   */
2766
  if (base == file_name + 1 &&
2767
      g_ascii_isalpha (file_name[0]) &&
2768
      file_name[1] == ':')
2769
    base++;
2770
  else if (G_IS_DIR_SEPARATOR (file_name[0]) &&
2771
           G_IS_DIR_SEPARATOR (file_name[1]) &&
2772
           file_name[2] &&
2773
           !G_IS_DIR_SEPARATOR (file_name[2]) &&
2774
           base >= file_name + 2)
2775
    {
2776
      const gchar *p = file_name + 2;
2777
      while (*p && !G_IS_DIR_SEPARATOR (*p))
2778
        p++;
2779
      if (p == base + 1)
2780
        {
2781
          len = (guint) strlen (file_name) + 1;
2782
          base_p = g_new (gchar, len + 1);
2783
          strcpy (base_p, file_name);
2784
          base_p[len-1] = G_DIR_SEPARATOR;
2785
          base_p[len] = 0;
2786
          return base_p;
2787
        }
2788
      if (G_IS_DIR_SEPARATOR (*p))
2789
        {
2790
          p++;
2791
          while (*p && !G_IS_DIR_SEPARATOR (*p))
2792
            p++;
2793
          if (p == base + 1)
2794
            base++;
2795
        }
2796
    }
2797
#endif
2798
2799
0
  len = (guint) 1 + base - file_name;
2800
0
  base_p = g_new (gchar, len + 1);
2801
0
  memmove (base_p, file_name, len);
2802
0
  base_p[len] = 0;
2803
2804
0
  return base_p;
2805
0
}
2806
2807
/**
2808
 * g_canonicalize_filename:
2809
 * @filename: (type filename): the name of the file
2810
 * @relative_to: (type filename) (nullable): the relative directory, or %NULL
2811
 * to use the current working directory
2812
 *
2813
 * Gets the canonical file name from @filename. All triple slashes are turned into
2814
 * single slashes, and all `..` and `.`s resolved against @relative_to.
2815
 *
2816
 * Symlinks are not followed, and the returned path is guaranteed to be absolute.
2817
 *
2818
 * If @filename is an absolute path, @relative_to is ignored. Otherwise,
2819
 * @relative_to will be prepended to @filename to make it absolute. @relative_to
2820
 * must be an absolute path, or %NULL. If @relative_to is %NULL, it'll fallback
2821
 * to g_get_current_dir().
2822
 *
2823
 * This function never fails, and will canonicalize file paths even if they don't
2824
 * exist.
2825
 *
2826
 * No file system I/O is done.
2827
 *
2828
 * Returns: (type filename) (transfer full): a newly allocated string with the
2829
 *   canonical file path
2830
 *
2831
 * Since: 2.58
2832
 */
2833
gchar *
2834
g_canonicalize_filename (const gchar *filename,
2835
                         const gchar *relative_to)
2836
0
{
2837
0
  gchar *canon, *input, *output, *after_root, *output_start;
2838
2839
0
  g_return_val_if_fail (relative_to == NULL || g_path_is_absolute (relative_to), NULL);
2840
2841
0
  if (!g_path_is_absolute (filename))
2842
0
    {
2843
0
      gchar *cwd_allocated = NULL;
2844
0
      const gchar  *cwd;
2845
2846
0
      if (relative_to != NULL)
2847
0
        cwd = relative_to;
2848
0
      else
2849
0
        cwd = cwd_allocated = g_get_current_dir ();
2850
2851
0
      canon = g_build_filename (cwd, filename, NULL);
2852
0
      g_free (cwd_allocated);
2853
0
    }
2854
0
  else
2855
0
    {
2856
0
      canon = g_strdup (filename);
2857
0
    }
2858
2859
0
  after_root = (char *)g_path_skip_root (canon);
2860
2861
0
  if (after_root == NULL)
2862
0
    {
2863
      /* This shouldn't really happen, as g_get_current_dir() should
2864
         return an absolute pathname, but bug 573843 shows this is
2865
         not always happening */
2866
0
      g_free (canon);
2867
0
      return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
2868
0
    }
2869
2870
  /* Find the first dir separator and use the canonical dir separator. */
2871
0
  for (output = after_root - 1;
2872
0
       (output >= canon) && G_IS_DIR_SEPARATOR (*output);
2873
0
       output--)
2874
0
    *output = G_DIR_SEPARATOR;
2875
2876
  /* 1 to re-increment after the final decrement above (so that output >= canon),
2877
   * and 1 to skip the first `/`. There might not be a first `/` if
2878
   * the @canon is a Windows `//server/share` style path with no
2879
   * trailing directories. @after_root will be '\0' in that case. */
2880
0
  output++;
2881
0
  if (*output == G_DIR_SEPARATOR)
2882
0
    output++;
2883
2884
  /* POSIX allows double slashes at the start to mean something special
2885
   * (as does windows too). So, "//" != "/", but more than two slashes
2886
   * is treated as "/".
2887
   */
2888
0
  if (after_root - output == 1)
2889
0
    output++;
2890
2891
0
  input = after_root;
2892
0
  output_start = output;
2893
0
  while (*input)
2894
0
    {
2895
      /* input points to the next non-separator to be processed. */
2896
      /* output points to the next location to write to. */
2897
0
      g_assert (input > canon && G_IS_DIR_SEPARATOR (input[-1]));
2898
0
      g_assert (output > canon && G_IS_DIR_SEPARATOR (output[-1]));
2899
0
      g_assert (input >= output);
2900
2901
      /* Ignore repeated dir separators. */
2902
0
      while (G_IS_DIR_SEPARATOR (input[0]))
2903
0
       input++;
2904
2905
      /* Ignore single dot directory components. */
2906
0
      if (input[0] == '.' && (input[1] == 0 || G_IS_DIR_SEPARATOR (input[1])))
2907
0
        {
2908
0
           if (input[1] == 0)
2909
0
             break;
2910
0
           input += 2;
2911
0
        }
2912
      /* Remove double-dot directory components along with the preceding
2913
       * path component. */
2914
0
      else if (input[0] == '.' && input[1] == '.' &&
2915
0
               (input[2] == 0 || G_IS_DIR_SEPARATOR (input[2])))
2916
0
        {
2917
0
          if (output > output_start)
2918
0
            {
2919
0
              do
2920
0
                {
2921
0
                  output--;
2922
0
                }
2923
0
              while (!G_IS_DIR_SEPARATOR (output[-1]) && output > output_start);
2924
0
            }
2925
0
          if (input[2] == 0)
2926
0
            break;
2927
0
          input += 3;
2928
0
        }
2929
      /* Copy the input to the output until the next separator,
2930
       * while converting it to canonical separator */
2931
0
      else
2932
0
        {
2933
0
          while (*input && !G_IS_DIR_SEPARATOR (*input))
2934
0
            *output++ = *input++;
2935
0
          if (input[0] == 0)
2936
0
            break;
2937
0
          input++;
2938
0
          *output++ = G_DIR_SEPARATOR;
2939
0
        }
2940
0
    }
2941
2942
  /* Remove a potentially trailing dir separator */
2943
0
  if (output > output_start && G_IS_DIR_SEPARATOR (output[-1]))
2944
0
    output--;
2945
2946
0
  *output = '\0';
2947
2948
0
  return canon;
2949
0
}
2950
2951
#if defined(MAXPATHLEN)
2952
#define G_PATH_LENGTH MAXPATHLEN
2953
#elif defined(PATH_MAX)
2954
0
#define G_PATH_LENGTH PATH_MAX
2955
#elif defined(_PC_PATH_MAX)
2956
#define G_PATH_LENGTH sysconf(_PC_PATH_MAX)
2957
#else
2958
#define G_PATH_LENGTH 2048
2959
#endif
2960
2961
/**
2962
 * g_get_current_dir:
2963
 *
2964
 * Gets the current directory.
2965
 *
2966
 * The returned string should be freed when no longer needed.
2967
 * The encoding of the returned string is system defined.
2968
 * On Windows, it is always UTF-8.
2969
 *
2970
 * Since GLib 2.40, this function will return the value of the "PWD"
2971
 * environment variable if it is set and it happens to be the same as
2972
 * the current directory.  This can make a difference in the case that
2973
 * the current directory is the target of a symbolic link.
2974
 *
2975
 * Returns: (type filename) (transfer full): the current directory
2976
 */
2977
gchar *
2978
g_get_current_dir (void)
2979
0
{
2980
#ifdef G_OS_WIN32
2981
2982
  gchar *dir = NULL;
2983
  wchar_t dummy[2], *wdir;
2984
  DWORD len;
2985
2986
  len = GetCurrentDirectoryW (2, dummy);
2987
  wdir = g_new (wchar_t, len);
2988
2989
  if (GetCurrentDirectoryW (len, wdir) == len - 1)
2990
    dir = g_utf16_to_utf8 (wdir, -1, NULL, NULL, NULL);
2991
2992
  g_free (wdir);
2993
2994
  if (dir == NULL)
2995
    dir = g_strdup ("\\");
2996
2997
  return dir;
2998
2999
#else
3000
0
  const gchar *pwd;
3001
0
  gchar *buffer = NULL;
3002
0
  gchar *dir = NULL;
3003
0
  static gsize buffer_size = 0;
3004
0
  struct stat pwdbuf, dotbuf;
3005
3006
0
  pwd = g_getenv ("PWD");
3007
0
  if (pwd != NULL &&
3008
0
      g_stat (".", &dotbuf) == 0 && g_stat (pwd, &pwdbuf) == 0 &&
3009
0
      dotbuf.st_dev == pwdbuf.st_dev && dotbuf.st_ino == pwdbuf.st_ino)
3010
0
    return g_strdup (pwd);
3011
3012
0
  if (buffer_size == 0)
3013
0
    buffer_size = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
3014
3015
0
  while (buffer_size < G_MAXSIZE / 2)
3016
0
    {
3017
0
      g_free (buffer);
3018
0
      buffer = g_new (gchar, buffer_size);
3019
0
      *buffer = 0;
3020
0
      dir = getcwd (buffer, buffer_size);
3021
3022
0
      if (dir || errno != ERANGE)
3023
0
        break;
3024
3025
0
      buffer_size *= 2;
3026
0
    }
3027
3028
  /* Check that getcwd() nul-terminated the string. It should do, but the specs
3029
   * don’t actually explicitly state that:
3030
   * https://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html */
3031
0
  g_assert (dir == NULL || strnlen (dir, buffer_size) < buffer_size);
3032
3033
0
  if (!dir || !*buffer)
3034
0
    {
3035
      /* Fallback return value */
3036
0
      g_assert (buffer_size >= 2);
3037
0
      g_assert (buffer != NULL);
3038
0
      buffer[0] = G_DIR_SEPARATOR;
3039
0
      buffer[1] = 0;
3040
0
    }
3041
3042
0
  dir = g_strdup (buffer);
3043
0
  g_free (buffer);
3044
3045
0
  return dir;
3046
3047
0
#endif /* !G_OS_WIN32 */
3048
0
}
3049
3050
#ifdef G_OS_WIN32
3051
3052
/* Binary compatibility versions. Not for newly compiled code. */
3053
3054
_GLIB_EXTERN gboolean g_file_test_utf8         (const gchar  *filename,
3055
                                                GFileTest     test);
3056
_GLIB_EXTERN gboolean g_file_get_contents_utf8 (const gchar  *filename,
3057
                                                gchar       **contents,
3058
                                                gsize        *length,
3059
                                                GError      **error);
3060
_GLIB_EXTERN gint     g_mkstemp_utf8           (gchar        *tmpl);
3061
_GLIB_EXTERN gint     g_file_open_tmp_utf8     (const gchar  *tmpl,
3062
                                                gchar       **name_used,
3063
                                                GError      **error);
3064
_GLIB_EXTERN gchar   *g_get_current_dir_utf8   (void);
3065
3066
3067
gboolean
3068
g_file_test_utf8 (const gchar *filename,
3069
                  GFileTest    test)
3070
{
3071
  return g_file_test (filename, test);
3072
}
3073
3074
gboolean
3075
g_file_get_contents_utf8 (const gchar  *filename,
3076
                          gchar       **contents,
3077
                          gsize        *length,
3078
                          GError      **error)
3079
{
3080
  return g_file_get_contents (filename, contents, length, error);
3081
}
3082
3083
gint
3084
g_mkstemp_utf8 (gchar *tmpl)
3085
{
3086
  return g_mkstemp (tmpl);
3087
}
3088
3089
gint
3090
g_file_open_tmp_utf8 (const gchar  *tmpl,
3091
                      gchar       **name_used,
3092
                      GError      **error)
3093
{
3094
  return g_file_open_tmp (tmpl, name_used, error);
3095
}
3096
3097
gchar *
3098
g_get_current_dir_utf8 (void)
3099
{
3100
  return g_get_current_dir ();
3101
}
3102
3103
#endif