Coverage Report

Created: 2026-11-12 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib/gio/glocalfile.c
Line
Count
Source
1
/* GIO - GLib Input, Output and Streaming Library
2
 * 
3
 * Copyright (C) 2006-2007 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
18
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * Author: Alexander Larsson <alexl@redhat.com>
21
 */
22
23
#include "config.h"
24
25
#include <sys/types.h>
26
#include <sys/stat.h>
27
#include <string.h>
28
#include <errno.h>
29
#include <fcntl.h>
30
#if G_OS_UNIX
31
#include <dirent.h>
32
#include <unistd.h>
33
#endif
34
35
#if HAVE_SYS_STATFS_H
36
#include <sys/statfs.h>
37
#endif
38
#if HAVE_SYS_STATVFS_H
39
#include <sys/statvfs.h>
40
#endif
41
#if HAVE_SYS_VFS_H
42
#include <sys/vfs.h>
43
#elif HAVE_SYS_MOUNT_H
44
#if HAVE_SYS_PARAM_H
45
#include <sys/param.h>
46
#endif
47
#include <sys/mount.h>
48
#endif
49
50
#ifndef O_BINARY
51
0
#define O_BINARY 0
52
#endif
53
54
#ifndef O_CLOEXEC
55
#define O_CLOEXEC 0
56
#endif
57
58
#include "gfileattribute.h"
59
#include "glocalfile.h"
60
#include "glocalfileinfo.h"
61
#include "glocalfileenumerator.h"
62
#include "glocalfileinputstream.h"
63
#include "glocalfileoutputstream.h"
64
#include "glocalfileiostream.h"
65
#include "glocalfilemonitor.h"
66
#include "gmountprivate.h"
67
#include "gunixmounts.h"
68
#include "gioerror.h"
69
#include <glib/gstdio.h>
70
#include <glib/gstdioprivate.h>
71
#include "glibintl.h"
72
#ifdef G_OS_UNIX
73
#include "glib-unix.h"
74
#include "gportalsupport.h"
75
#include "gtrashportal.h"
76
#endif
77
78
#include "glib-private.h"
79
80
#ifdef G_OS_WIN32
81
#include <windows.h>
82
#include <io.h>
83
#include <direct.h>
84
85
#ifndef FILE_READ_ONLY_VOLUME
86
#define FILE_READ_ONLY_VOLUME           0x00080000
87
#endif
88
89
#ifndef S_ISREG
90
#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
91
#endif
92
#ifndef S_ISDIR
93
#define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
94
#endif
95
#ifndef S_ISLNK
96
#define S_ISLNK(m) (0)
97
#endif
98
99
#ifndef ECANCELED
100
#define ECANCELED 105
101
#endif
102
#ifndef ERROR_CANCELLED
103
#define ERROR_CANCELLED 1223
104
#endif
105
#endif
106
107
108
static void g_local_file_file_iface_init (GFileIface *iface);
109
110
static GFileAttributeInfoList *local_writable_attributes = NULL;
111
static GFileAttributeInfoList *local_writable_namespaces = NULL;
112
113
struct _GLocalFile
114
{
115
  GObject parent_instance;
116
117
  char *filename;
118
};
119
120
#define g_local_file_get_type _g_local_file_get_type
121
0
G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
122
0
                         G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
123
0
                                                g_local_file_file_iface_init))
124
0
125
0
static char *find_mountpoint_for (const char *file, dev_t dev, gboolean resolve_basename_symlink);
126
0
127
0
#ifndef G_OS_WIN32
128
0
static gboolean is_remote_fs_type (const gchar *fsname);
129
0
#endif
130
0
131
0
static void
132
0
g_local_file_finalize (GObject *object)
133
0
{
134
0
  GLocalFile *local;
135
136
0
  local = G_LOCAL_FILE (object);
137
138
0
  g_free (local->filename);
139
140
0
  G_OBJECT_CLASS (g_local_file_parent_class)->finalize (object);
141
0
}
142
143
static void
144
g_local_file_class_init (GLocalFileClass *klass)
145
0
{
146
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
147
0
  GFileAttributeInfoList *list;
148
149
0
  gobject_class->finalize = g_local_file_finalize;
150
151
  /* Set up attribute lists */
152
153
  /* Writable attributes: */
154
155
0
  list = g_file_attribute_info_list_new ();
156
157
0
  g_file_attribute_info_list_add (list,
158
0
                                  G_FILE_ATTRIBUTE_UNIX_MODE,
159
0
                                  G_FILE_ATTRIBUTE_TYPE_UINT32,
160
0
                                  G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
161
0
                                  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
162
  
163
0
#ifdef G_OS_UNIX
164
0
  g_file_attribute_info_list_add (list,
165
0
                                  G_FILE_ATTRIBUTE_UNIX_UID,
166
0
                                  G_FILE_ATTRIBUTE_TYPE_UINT32,
167
0
                                  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
168
0
  g_file_attribute_info_list_add (list,
169
0
                                  G_FILE_ATTRIBUTE_UNIX_GID,
170
0
                                  G_FILE_ATTRIBUTE_TYPE_UINT32,
171
0
                                  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
172
0
#endif
173
  
174
0
#ifdef HAVE_SYMLINK
175
0
  g_file_attribute_info_list_add (list,
176
0
                                  G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
177
0
                                  G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
178
0
                                  0);
179
0
#endif
180
  
181
0
#if defined(HAVE_UTIMES) || defined(HAVE_UTIMENSAT)
182
0
  g_file_attribute_info_list_add (list,
183
0
                                  G_FILE_ATTRIBUTE_TIME_MODIFIED,
184
0
                                  G_FILE_ATTRIBUTE_TYPE_UINT64,
185
0
                                  G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
186
0
                                  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
187
0
  g_file_attribute_info_list_add (list,
188
0
                                  G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
189
0
                                  G_FILE_ATTRIBUTE_TYPE_UINT32,
190
0
                                  G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
191
0
                                  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
192
  /* When copying, the target file is accessed. Replicating
193
   * the source access time does not make sense in this case.
194
   */
195
0
  g_file_attribute_info_list_add (list,
196
0
                                  G_FILE_ATTRIBUTE_TIME_ACCESS,
197
0
                                  G_FILE_ATTRIBUTE_TYPE_UINT64,
198
0
                                  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
199
0
  g_file_attribute_info_list_add (list,
200
0
                                  G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
201
0
                                  G_FILE_ATTRIBUTE_TYPE_UINT32,
202
0
                                  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
203
0
#endif  /* HAVE_UTIMES || HAVE_UTIMENSAT */
204
205
0
#ifdef HAVE_UTIMENSAT
206
0
  g_file_attribute_info_list_add (list,
207
0
                                  G_FILE_ATTRIBUTE_TIME_MODIFIED_NSEC,
208
0
                                  G_FILE_ATTRIBUTE_TYPE_UINT32,
209
0
                                  G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
210
0
                                  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
211
0
  g_file_attribute_info_list_add (list,
212
0
                                  G_FILE_ATTRIBUTE_TIME_ACCESS_NSEC,
213
0
                                  G_FILE_ATTRIBUTE_TYPE_UINT32,
214
0
                                  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
215
0
#endif
216
217
0
  local_writable_attributes = list;
218
0
}
219
220
static void
221
g_local_file_init (GLocalFile *local)
222
0
{
223
0
}
224
225
const char *
226
_g_local_file_get_filename (GLocalFile *file)
227
0
{
228
0
  return file->filename;
229
0
}
230
231
GFile *
232
_g_local_file_new (const char *filename)
233
0
{
234
0
  GLocalFile *local;
235
236
0
  local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
237
0
  local->filename = g_canonicalize_filename (filename, NULL);
238
  
239
0
  return G_FILE (local);
240
0
}
241
242
/*< internal >
243
 * g_local_file_new_from_dirname_and_basename:
244
 * @dirname: an absolute, canonical directory name
245
 * @basename: the name of a child inside @dirname
246
 *
247
 * Creates a #GFile from @dirname and @basename.
248
 *
249
 * This is more efficient than pasting the fields together for yourself
250
 * and creating a #GFile from the result, and also more efficient than
251
 * creating a #GFile for the dirname and using g_file_get_child().
252
 *
253
 * @dirname must be canonical, as per GLocalFile's opinion of what
254
 * canonical means.  This means that you should only pass strings that
255
 * were returned by _g_local_file_get_filename().
256
 *
257
 * Returns: a #GFile
258
 */
259
GFile *
260
g_local_file_new_from_dirname_and_basename (const gchar *dirname,
261
                                            const gchar *basename)
262
0
{
263
0
  GLocalFile *local;
264
265
0
  g_return_val_if_fail (dirname != NULL, NULL);
266
0
  g_return_val_if_fail (basename && basename[0] && !strchr (basename, '/'), NULL);
267
268
0
  local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
269
0
  local->filename = g_build_filename (dirname, basename, NULL);
270
271
0
  return G_FILE (local);
272
0
}
273
274
static gboolean
275
g_local_file_is_native (GFile *file)
276
0
{
277
0
  return TRUE;
278
0
}
279
280
static gboolean
281
g_local_file_has_uri_scheme (GFile      *file,
282
                             const char *uri_scheme)
283
0
{
284
0
  return g_ascii_strcasecmp (uri_scheme, "file") == 0;
285
0
}
286
287
static char *
288
g_local_file_get_uri_scheme (GFile *file)
289
0
{
290
0
  return g_strdup ("file");
291
0
}
292
293
static char *
294
g_local_file_get_basename (GFile *file)
295
0
{
296
0
  return g_path_get_basename (G_LOCAL_FILE (file)->filename);
297
0
}
298
299
static char *
300
g_local_file_get_path (GFile *file)
301
0
{
302
0
  return g_strdup (G_LOCAL_FILE (file)->filename);
303
0
}
304
305
static char *
306
g_local_file_get_uri (GFile *file)
307
0
{
308
0
  return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
309
0
}
310
311
static gboolean
312
get_filename_charset (const gchar **filename_charset)
313
0
{
314
0
  const gchar **charsets;
315
0
  gboolean is_utf8;
316
  
317
0
  is_utf8 = g_get_filename_charsets (&charsets);
318
319
0
  if (filename_charset)
320
0
    *filename_charset = charsets[0];
321
  
322
0
  return is_utf8;
323
0
}
324
325
static gboolean
326
name_is_valid_for_display (const char *string,
327
                           gboolean    is_valid_utf8)
328
0
{
329
0
  char c;
330
331
0
  if (!is_valid_utf8 &&
332
0
      !g_utf8_validate (string, -1, NULL))
333
0
    return FALSE;
334
335
0
  while ((c = *string++) != 0)
336
0
    {
337
0
      if (g_ascii_iscntrl (c))
338
0
        return FALSE;
339
0
    }
340
341
0
  return TRUE;
342
0
}
343
344
static char *
345
g_local_file_get_parse_name (GFile *file)
346
0
{
347
0
  const char *filename;
348
0
  char *parse_name;
349
0
  const gchar *charset;
350
0
  char *utf8_filename;
351
0
  char *roundtripped_filename;
352
0
  gboolean free_utf8_filename;
353
0
  gboolean is_valid_utf8;
354
0
  char *escaped_path;
355
  
356
0
  filename = G_LOCAL_FILE (file)->filename;
357
0
  if (get_filename_charset (&charset))
358
0
    {
359
0
      utf8_filename = (char *)filename;
360
0
      free_utf8_filename = FALSE;
361
0
      is_valid_utf8 = FALSE; /* Can't guarantee this */
362
0
    }
363
0
  else
364
0
    {
365
0
      utf8_filename = g_convert (filename, -1, 
366
0
                                 "UTF-8", charset, NULL, NULL, NULL);
367
0
      free_utf8_filename = TRUE;
368
0
      is_valid_utf8 = TRUE;
369
370
0
      if (utf8_filename != NULL)
371
0
        {
372
          /* Make sure we can roundtrip: */
373
0
          roundtripped_filename = g_convert (utf8_filename, -1,
374
0
                                             charset, "UTF-8", NULL, NULL, NULL);
375
          
376
0
          if (roundtripped_filename == NULL ||
377
0
              strcmp (filename, roundtripped_filename) != 0)
378
0
            {
379
0
              g_free (utf8_filename);
380
0
              utf8_filename = NULL;
381
0
            }
382
383
0
          g_free (roundtripped_filename);
384
0
        }
385
0
    }
386
387
0
  if (utf8_filename != NULL &&
388
0
      name_is_valid_for_display (utf8_filename, is_valid_utf8))
389
0
    {
390
0
      if (free_utf8_filename)
391
0
        parse_name = utf8_filename;
392
0
      else
393
0
        parse_name = g_strdup (utf8_filename);
394
0
    }
395
0
  else
396
0
    {
397
#ifdef G_OS_WIN32
398
      char *dup_filename, *p, *backslash;
399
400
      /* Turn backslashes into forward slashes like
401
       * g_filename_to_uri() would do (but we can't use that because
402
       * it doesn't output IRIs).
403
       */
404
      dup_filename = g_strdup (filename);
405
      filename = p = dup_filename;
406
407
      while ((backslash = strchr (p, '\\')) != NULL)
408
        {
409
          *backslash = '/';
410
          p = backslash + 1;
411
        }
412
#endif
413
414
0
      escaped_path = g_uri_escape_string (filename,
415
0
                                          G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
416
0
                                          TRUE);
417
0
      parse_name = g_strconcat ("file://",
418
0
                                (*escaped_path != '/') ? "/" : "",
419
0
                                escaped_path,
420
0
                                NULL);
421
      
422
0
      g_free (escaped_path);
423
#ifdef G_OS_WIN32
424
      g_free (dup_filename);
425
#endif
426
0
      if (free_utf8_filename)
427
0
        g_free (utf8_filename);
428
0
    }
429
  
430
0
  return parse_name;
431
0
}
432
433
static GFile *
434
g_local_file_get_parent (GFile *file)
435
0
{
436
0
  GLocalFile *local = G_LOCAL_FILE (file);
437
0
  const char *non_root;
438
0
  char *dirname;
439
0
  GFile *parent;
440
441
  /* Check for root; local->filename is guaranteed to be absolute, so
442
   * g_path_skip_root() should never return NULL. */
443
0
  non_root = g_path_skip_root (local->filename);
444
0
  g_assert (non_root != NULL);
445
446
0
  if (*non_root == 0)
447
0
    return NULL;
448
449
0
  dirname = g_path_get_dirname (local->filename);
450
0
  parent = _g_local_file_new (dirname);
451
0
  g_free (dirname);
452
0
  return parent;
453
0
}
454
455
static GFile *
456
g_local_file_dup (GFile *file)
457
0
{
458
0
  GLocalFile *local = G_LOCAL_FILE (file);
459
460
0
  return _g_local_file_new (local->filename);
461
0
}
462
463
static guint
464
g_local_file_hash (GFile *file)
465
0
{
466
0
  GLocalFile *local = G_LOCAL_FILE (file);
467
  
468
0
  return g_str_hash (local->filename);
469
0
}
470
471
static gboolean
472
g_local_file_equal (GFile *file1,
473
                    GFile *file2)
474
0
{
475
0
  GLocalFile *local1 = G_LOCAL_FILE (file1);
476
0
  GLocalFile *local2 = G_LOCAL_FILE (file2);
477
478
0
  return g_str_equal (local1->filename, local2->filename);
479
0
}
480
481
static const char *
482
match_prefix (const char *path, 
483
              const char *prefix)
484
0
{
485
0
  size_t prefix_len;
486
487
0
  prefix_len = strlen (prefix);
488
0
  if (strncmp (path, prefix, prefix_len) != 0)
489
0
    return NULL;
490
  
491
  /* Handle the case where prefix is the root, so that
492
   * the IS_DIR_SEPRARATOR check below works */
493
0
  if (prefix_len > 0 &&
494
0
      G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
495
0
    prefix_len--;
496
  
497
0
  return path + prefix_len;
498
0
}
499
500
static gboolean
501
g_local_file_prefix_matches (GFile *parent,
502
                             GFile *descendant)
503
0
{
504
0
  GLocalFile *parent_local = G_LOCAL_FILE (parent);
505
0
  GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
506
0
  const char *remainder;
507
508
0
  remainder = match_prefix (descendant_local->filename, parent_local->filename);
509
0
  if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
510
0
    return TRUE;
511
0
  return FALSE;
512
0
}
513
514
static char *
515
g_local_file_get_relative_path (GFile *parent,
516
                                GFile *descendant)
517
0
{
518
0
  GLocalFile *parent_local = G_LOCAL_FILE (parent);
519
0
  GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
520
0
  const char *remainder;
521
522
0
  remainder = match_prefix (descendant_local->filename, parent_local->filename);
523
  
524
0
  if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
525
0
    return g_strdup (remainder + 1);
526
0
  return NULL;
527
0
}
528
529
static GFile *
530
g_local_file_resolve_relative_path (GFile      *file,
531
                                    const char *relative_path)
532
0
{
533
0
  GLocalFile *local = G_LOCAL_FILE (file);
534
0
  char *filename;
535
0
  GFile *child;
536
537
0
  if (g_path_is_absolute (relative_path))
538
0
    return _g_local_file_new (relative_path);
539
  
540
0
  filename = g_build_filename (local->filename, relative_path, NULL);
541
0
  child = _g_local_file_new (filename);
542
0
  g_free (filename);
543
  
544
0
  return child;
545
0
}
546
547
static GFileEnumerator *
548
g_local_file_enumerate_children (GFile                *file,
549
                                 const char           *attributes,
550
                                 GFileQueryInfoFlags   flags,
551
                                 GCancellable         *cancellable,
552
                                 GError              **error)
553
0
{
554
0
  GLocalFile *local = G_LOCAL_FILE (file);
555
0
  return _g_local_file_enumerator_new (local,
556
0
                                       attributes, flags,
557
0
                                       cancellable, error);
558
0
}
559
560
static GFile *
561
g_local_file_get_child_for_display_name (GFile        *file,
562
                                         const char   *display_name,
563
                                         GError      **error)
564
0
{
565
0
  GFile *new_file;
566
0
  char *basename;
567
568
0
  basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
569
0
  if (basename == NULL)
570
0
    {
571
0
      g_set_error (error, G_IO_ERROR,
572
0
                   G_IO_ERROR_INVALID_FILENAME,
573
0
                   _("Invalid filename %s"), display_name);
574
0
      return NULL;
575
0
    }
576
577
0
  new_file = g_file_get_child (file, basename);
578
0
  g_free (basename);
579
  
580
0
  return new_file;
581
0
}
582
583
#if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
584
typedef struct {
585
  unsigned long f_type;
586
  const char *fs_type;  /* file system ID taken from Linux manpage */
587
} FsTypeMapping;
588
589
static const FsTypeMapping fs_type_map[] =
590
  {
591
    /* Keep this list sorted by f_type to allow for binary search */
592
    { 0x0000002ful, "qnx4" },
593
    { 0x00000187ul, "autofs" },
594
    { 0x00001373ul, "devfs" },
595
    { 0x0000137Dul, "ext" },
596
    { 0x0000137Ful, "minix" },
597
    { 0x0000138Ful, "minix2" },
598
    { 0x00001cd1ul, "devpts" },
599
    { 0x00002468ul, "minix2" },
600
    { 0x00002478ul, "minix22" },
601
    { 0x00003434ul, "nilfs" },
602
    { 0x00004244ul, "hfs" },
603
    { 0x00004d44ul, "msdos" },
604
    { 0x00004d5aul, "minix3" },
605
    { 0x0000517Bul, "smb" },
606
    { 0x0000564cul, "ncp" },
607
    { 0x00006969ul, "nfs" },
608
    { 0x00007275ul, "romfs" },
609
    { 0x000072b6ul, "jffs2" },
610
    { 0x00009660ul, "isofs" },
611
    { 0x00009fa0ul, "proc" },
612
    { 0x00009fa1ul, "openprom" },
613
    { 0x00009fa2ul, "usbdevice" },
614
    { 0x0000adf5ul, "adfs" },
615
    { 0x0000ADFFul, "affs" },
616
    { 0x0000EF51ul, "ext2" },
617
    { 0x0000EF53ul, "ext3/ext4" },
618
    { 0x0000f15ful, "ecryptfs" },
619
    { 0x00011954ul, "ufs" },
620
    { 0x0027e0ebul, "cgroup" },
621
    { 0x00414A53ul, "efs" },
622
    { 0x00c0ffeeul, "hostfs" },
623
    { 0x01021994ul, "tmpfs" },
624
    { 0x01021997ul, "v9fs" },
625
    { 0x012FD16Dul, "xiafs" },
626
    { 0x012FF7B4ul, "xenix" },
627
    { 0x012FF7B5ul, "sysv4" },
628
    { 0x012FF7B6ul, "sysv2" },
629
    { 0x012FF7B7ul, "coh" },
630
    { 0x0BAD1DEAul, "futexfs" },
631
    { 0x15013346ul, "udf" },
632
    { 0x19800202ul, "mqueue" },
633
    { 0x1BADFACEul, "bfs" },
634
    { 0x2011BAB0ul, "exfat" },
635
    { 0x28cd3d45ul, "cramfs" },
636
    { 0x3153464aul, "jfs" },
637
    { 0x42465331ul, "befs" },
638
    { 0x42494e4dul, "binfmt_misc" },
639
    { 0x43415d53ul, "smackfs" },
640
    { 0x50495045ul, "pipefs" },
641
    { 0x52345362ul, "reiser4" },
642
    { 0x52654973ul, "reiserfs" },
643
    { 0x5346414ful, "afs" },
644
    { 0x5346544eul, "ntfs" },
645
    { 0x534F434Bul, "sockfs" },
646
    { 0x58465342ul, "xfs" },
647
    { 0x6165676Cul, "pstore" },
648
    { 0x62646576ul, "bdevfs" },
649
    { 0x62656570ul, "configfs" },
650
    { 0x62656572ul, "sysfs" },
651
    { 0x63677270ul, "cgroup2" },
652
    { 0x64626720ul, "debugfs" },
653
    { 0x65735543ul, "fusectl" },
654
    { 0x65735546ul, "fuse" },
655
    { 0x67596969ul, "rpc_pipefs" },
656
    { 0x68191122ul, "qnx6" },
657
    { 0x6e736673ul, "nsfs" },
658
    { 0x73636673ul, "securityfs" },
659
    { 0x7366746eul, "ntfs3" },
660
    { 0x73717368ul, "squashfs" },
661
    { 0x73727279ul, "btrfs_test_fs" },
662
    { 0x73757245ul, "coda" },
663
    { 0x7461636ful, "ocfs2" },
664
    { 0x74726163ul, "tracefs" },
665
    { 0x794c7630ul, "overlay" },
666
    { 0x858458f6ul, "ramfs" },
667
    { 0x9123683Eul, "btrfs" },
668
    { 0x958458f6ul, "hugetlbfs" },
669
    { 0xa501FCF5ul, "vxfs" },
670
    { 0xabba1974ul, "xenfs" },
671
    { 0xde5e81e4ul, "efivarfs" },
672
    { 0xF2F52010ul, "f2fs" },
673
    { 0xf97cff8cul, "selinuxfs" },
674
    { 0xF995E849ul, "hpfs" },
675
    { 0xfe534d42ul, "smb2" },
676
    { 0xFF534D42ul, "cifs" },
677
  };
678
679
static int
680
fs_type_mapping_compare (const void *a,
681
                         const void *b)
682
0
{
683
0
  const FsTypeMapping *type_a = a;
684
0
  const FsTypeMapping *type_b = b;
685
686
0
  if (type_a->f_type < type_b->f_type)
687
0
    return -1;
688
0
  else if (type_a->f_type > type_b->f_type)
689
0
    return 1;
690
0
  else
691
0
    return 0;
692
0
}
693
694
static const char *
695
get_fs_type (long f_type)
696
0
{
697
0
  FsTypeMapping key = { (unsigned long) f_type, NULL };
698
0
  const FsTypeMapping *needle;
699
700
0
  needle = bsearch (&key, fs_type_map,
701
0
                    G_N_ELEMENTS (fs_type_map), sizeof (fs_type_map[0]),
702
0
                    fs_type_mapping_compare);
703
704
0
  return (needle != NULL) ? needle->fs_type : NULL;
705
0
}
706
#endif
707
708
#ifndef G_OS_WIN32
709
710
G_LOCK_DEFINE_STATIC(mount_info_hash);
711
static GHashTable *mount_info_hash = NULL;
712
static guint64 mount_info_hash_cache_time = 0;
713
714
typedef enum {
715
  MOUNT_INFO_READONLY = 1<<0
716
} G_GNUC_FLAG_ENUM MountInfo;
717
718
static gboolean
719
device_equal (gconstpointer v1,
720
              gconstpointer v2)
721
0
{
722
0
  return *(dev_t *)v1 == *(dev_t *)v2;
723
0
}
724
725
static guint
726
device_hash (gconstpointer v)
727
0
{
728
0
  return (guint) *(dev_t *)v;
729
0
}
730
731
static void
732
get_mount_info (GFileInfo             *fs_info,
733
                const char            *path,
734
                GFileAttributeMatcher *matcher)
735
0
{
736
0
  GStatBuf buf;
737
0
  gboolean got_info;
738
0
  gpointer info_as_ptr;
739
0
  guint mount_info;
740
0
  char *mountpoint;
741
0
  dev_t *dev;
742
0
  GUnixMountEntry *mount;
743
0
  guint64 cache_time;
744
0
  gboolean is_remote = FALSE;
745
746
0
  if (g_lstat (path, &buf) != 0)
747
0
    return;
748
749
0
  G_LOCK (mount_info_hash);
750
751
0
  if (mount_info_hash == NULL)
752
0
    mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
753
0
                                             g_free, NULL);
754
755
756
0
  if (g_unix_mount_entries_changed_since (mount_info_hash_cache_time))
757
0
    g_hash_table_remove_all (mount_info_hash);
758
  
759
0
  got_info = g_hash_table_lookup_extended (mount_info_hash,
760
0
                                           &buf.st_dev,
761
0
                                           NULL,
762
0
                                           &info_as_ptr);
763
  
764
0
  G_UNLOCK (mount_info_hash);
765
  
766
0
  mount_info = GPOINTER_TO_UINT (info_as_ptr);
767
  
768
0
  if (!got_info)
769
0
    {
770
0
      mount_info = 0;
771
772
0
      mountpoint = find_mountpoint_for (path, buf.st_dev, FALSE);
773
0
      if (mountpoint == NULL)
774
0
        mountpoint = g_strdup ("/");
775
776
0
      mount = g_unix_mount_entry_at (mountpoint, &cache_time);
777
0
      if (mount)
778
0
        {
779
0
          if (g_unix_mount_entry_is_readonly (mount))
780
0
            mount_info |= MOUNT_INFO_READONLY;
781
0
          if (is_remote_fs_type (g_unix_mount_entry_get_fs_type (mount)))
782
0
            is_remote = TRUE;
783
          
784
0
          g_unix_mount_entry_free (mount);
785
0
        }
786
787
0
      g_free (mountpoint);
788
789
0
      dev = g_new0 (dev_t, 1);
790
0
      *dev = buf.st_dev;
791
      
792
0
      G_LOCK (mount_info_hash);
793
0
      mount_info_hash_cache_time = cache_time;
794
0
      g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
795
0
      G_UNLOCK (mount_info_hash);
796
0
    }
797
798
0
  if (g_file_attribute_matcher_matches (matcher,
799
0
                                        G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
800
0
    g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
801
0
                                       (mount_info & MOUNT_INFO_READONLY));
802
803
0
  if (g_file_attribute_matcher_matches (matcher,
804
0
                                        G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE))
805
0
    g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE, is_remote);
806
0
}
807
808
#endif
809
810
#ifdef G_OS_WIN32
811
812
static wchar_t *
813
get_volume_for_path (const char *path)
814
{
815
  long len;
816
  wchar_t *wpath;
817
  wchar_t *result;
818
819
  wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
820
  result = g_new (wchar_t, MAX_PATH);
821
822
  if (!GetVolumePathNameW (wpath, result, MAX_PATH))
823
    {
824
      char *msg = g_win32_error_message (GetLastError ());
825
      g_critical ("GetVolumePathName failed: %s", msg);
826
      g_free (msg);
827
      g_free (result);
828
      g_free (wpath);
829
      return NULL;
830
    }
831
832
  len = wcslen (result);
833
  if (len > 0 && result[len-1] != L'\\')
834
    {
835
      result = g_renew (wchar_t, result, len + 2);
836
      result[len] = L'\\';
837
      result[len + 1] = 0;
838
    }
839
840
  g_free (wpath);
841
  return result;
842
}
843
844
static char *
845
find_mountpoint_for (const char *file, dev_t dev, gboolean resolve_basename_symlink)
846
{
847
  wchar_t *wpath;
848
  char *utf8_path;
849
850
  wpath = get_volume_for_path (file);
851
  if (!wpath)
852
    return NULL;
853
854
  utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
855
856
  g_free (wpath);
857
  return utf8_path;
858
}
859
860
static void
861
get_filesystem_readonly (GFileInfo  *info,
862
                         const char *path)
863
{
864
  wchar_t *rootdir;
865
866
  rootdir = get_volume_for_path (path);
867
868
  if (rootdir)
869
    {
870
      DWORD flags;
871
      if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
872
        g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
873
                                           (flags & FILE_READ_ONLY_VOLUME) != 0);
874
    }
875
876
  g_free (rootdir);
877
}
878
879
#endif /* G_OS_WIN32 */
880
881
#pragma GCC diagnostic push
882
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
883
static void
884
g_set_io_error (GError      **error,
885
                const gchar  *msg,
886
                GFile        *file,
887
                gint          errsv)
888
0
{
889
0
  GLocalFile *local = G_LOCAL_FILE (file);
890
0
  gchar *display_name;
891
892
0
  display_name = g_filename_display_name (local->filename);
893
0
  g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
894
0
               msg, display_name, g_strerror (errsv));
895
0
  g_free (display_name);
896
0
}
897
#pragma GCC diagnostic pop
898
899
static GFileInfo *
900
g_local_file_query_filesystem_info (GFile         *file,
901
                                    const char    *attributes,
902
                                    GCancellable  *cancellable,
903
                                    GError       **error)
904
0
{
905
0
  GLocalFile *local = G_LOCAL_FILE (file);
906
0
  GFileInfo *info;
907
0
  int statfs_result = 0;
908
0
  gboolean no_size;
909
0
#ifndef G_OS_WIN32
910
0
  const char *fstype;
911
0
#ifdef USE_STATFS
912
0
  guint64 block_size;
913
0
  struct statfs statfs_buffer;
914
#elif defined(USE_STATVFS)
915
  guint64 block_size;
916
  struct statvfs statfs_buffer;
917
#endif /* USE_STATFS */
918
0
#endif /* G_OS_WIN32 */
919
0
  GFileAttributeMatcher *attribute_matcher;
920
        
921
0
  no_size = FALSE;
922
  
923
0
#ifdef USE_STATFS
924
  
925
0
#if STATFS_ARGS == 2
926
0
  statfs_result = statfs (local->filename, &statfs_buffer);
927
#elif STATFS_ARGS == 4
928
  statfs_result = statfs (local->filename, &statfs_buffer,
929
                          sizeof (statfs_buffer), 0);
930
#endif /* STATFS_ARGS == 2 */
931
0
  block_size = statfs_buffer.f_bsize;
932
  
933
  /* Many backends can't report free size (for instance the gvfs fuse
934
   * backend for backend not supporting this), and set f_bfree to 0,
935
   *  but it can be 0 for real too. We treat the available == 0 and
936
   * free == 0 case as "both of these are invalid", but only on file systems
937
   * which are known to not support this (otherwise we can omit metadata for
938
   * systems which are legitimately full). */
939
0
#if defined(__linux__)
940
0
  if (statfs_result == 0 &&
941
0
      statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0 &&
942
0
      (/* linux/ncp_fs.h: NCP_SUPER_MAGIC == 0x564c */
943
0
       statfs_buffer.f_type == 0x564c ||
944
       /* man statfs: FUSE_SUPER_MAGIC == 0x65735546 */
945
0
       statfs_buffer.f_type == 0x65735546))
946
0
    no_size = TRUE;
947
0
#endif  /* __linux__ */
948
  
949
#elif defined(USE_STATVFS)
950
  statfs_result = statvfs (local->filename, &statfs_buffer);
951
  block_size = statfs_buffer.f_frsize; 
952
#endif /* USE_STATFS */
953
954
0
  if (statfs_result == -1)
955
0
    {
956
0
      int errsv = errno;
957
958
0
      g_set_io_error (error,
959
0
                      _("Error getting filesystem info for %s: %s"),
960
0
                      file, errsv);
961
0
      return NULL;
962
0
    }
963
964
0
  info = g_file_info_new ();
965
966
0
  attribute_matcher = g_file_attribute_matcher_new (attributes);
967
  
968
0
  if (!no_size &&
969
0
      g_file_attribute_matcher_matches (attribute_matcher,
970
0
                                        G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
971
0
    {
972
#ifdef G_OS_WIN32
973
      gchar *localdir = g_path_get_dirname (local->filename);
974
      wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
975
      ULARGE_INTEGER li;
976
      
977
      g_free (localdir);
978
      if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
979
        g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
980
      g_free (wdirname);
981
#else
982
0
#if defined(USE_STATFS) || defined(USE_STATVFS)
983
0
      g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
984
0
#endif
985
0
#endif
986
0
    }
987
0
  if (!no_size &&
988
0
      g_file_attribute_matcher_matches (attribute_matcher,
989
0
                                        G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
990
0
    {
991
#ifdef G_OS_WIN32
992
      gchar *localdir = g_path_get_dirname (local->filename);
993
      wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
994
      ULARGE_INTEGER li;
995
      
996
      g_free (localdir);
997
      if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
998
        g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE,  (guint64)li.QuadPart);
999
      g_free (wdirname);
1000
#else
1001
0
#if defined(USE_STATFS) || defined(USE_STATVFS)
1002
0
      g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
1003
0
#endif
1004
0
#endif /* G_OS_WIN32 */
1005
0
    }
1006
1007
0
  if (!no_size &&
1008
0
      g_file_attribute_matcher_matches (attribute_matcher,
1009
0
                                        G_FILE_ATTRIBUTE_FILESYSTEM_USED))
1010
0
    {
1011
#ifdef G_OS_WIN32
1012
      gchar *localdir = g_path_get_dirname (local->filename);
1013
      wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1014
      ULARGE_INTEGER li_free;
1015
      ULARGE_INTEGER li_total;
1016
1017
      g_free (localdir);
1018
      if (GetDiskFreeSpaceExW (wdirname, &li_free, &li_total, NULL))
1019
        g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED,  (guint64)li_total.QuadPart - (guint64)li_free.QuadPart);
1020
      g_free (wdirname);
1021
#else
1022
0
      g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, block_size * (statfs_buffer.f_blocks - statfs_buffer.f_bfree));
1023
0
#endif /* G_OS_WIN32 */
1024
0
    }
1025
1026
0
#ifndef G_OS_WIN32
1027
0
#ifdef USE_STATFS
1028
#if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1029
  fstype = statfs_buffer.f_fstypename;
1030
#else
1031
0
  fstype = get_fs_type (statfs_buffer.f_type);
1032
0
#endif
1033
1034
#elif defined(USE_STATVFS)
1035
#if defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
1036
  fstype = statfs_buffer.f_fstypename;
1037
#elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1038
  fstype = statfs_buffer.f_basetype;
1039
#elif defined(HAVE_STRUCT_STATVFS_F_TYPE)
1040
  fstype = get_fs_type (statfs_buffer.f_type);
1041
#else
1042
  fstype = NULL;
1043
#endif
1044
#endif /* USE_STATFS */
1045
1046
0
  if (fstype &&
1047
0
      g_file_attribute_matcher_matches (attribute_matcher,
1048
0
                                        G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1049
0
    g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1050
0
#endif /* G_OS_WIN32 */
1051
1052
0
  if (g_file_attribute_matcher_matches (attribute_matcher,
1053
0
                                        G_FILE_ATTRIBUTE_FILESYSTEM_READONLY) ||
1054
0
      g_file_attribute_matcher_matches (attribute_matcher,
1055
0
                                        G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE))
1056
0
    {
1057
#ifdef G_OS_WIN32
1058
      get_filesystem_readonly (info, local->filename);
1059
#else
1060
0
      get_mount_info (info, local->filename, attribute_matcher);
1061
0
#endif /* G_OS_WIN32 */
1062
0
    }
1063
1064
0
  g_file_attribute_matcher_unref (attribute_matcher);
1065
  
1066
0
  return info;
1067
0
}
1068
1069
static GMount *
1070
g_local_file_find_enclosing_mount (GFile         *file,
1071
                                   GCancellable  *cancellable,
1072
                                   GError       **error)
1073
0
{
1074
0
  GLocalFile *local = G_LOCAL_FILE (file);
1075
0
  GStatBuf buf;
1076
0
  char *mountpoint;
1077
0
  GMount *mount;
1078
1079
0
  if (g_lstat (local->filename, &buf) != 0)
1080
0
    goto error;
1081
1082
0
  mountpoint = find_mountpoint_for (local->filename, buf.st_dev, FALSE);
1083
0
  if (mountpoint == NULL)
1084
0
    goto error;
1085
1086
0
  mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1087
0
  g_free (mountpoint);
1088
0
  if (mount)
1089
0
    return mount;
1090
1091
0
error:
1092
0
  g_set_io_error (error,
1093
                  /* Translators: This is an error message when trying to find
1094
                   * the enclosing (user visible) mount of a file, but none
1095
                   * exists.
1096
                   */
1097
0
                  _("Containing mount for file %s not found"),
1098
0
                  file, 0);
1099
1100
0
  return NULL;
1101
0
}
1102
1103
static GFile *
1104
g_local_file_set_display_name (GFile         *file,
1105
                               const char    *display_name,
1106
                               GCancellable  *cancellable,
1107
                               GError       **error)
1108
0
{
1109
0
  GLocalFile *local, *new_local;
1110
0
  GFile *new_file, *parent;
1111
0
  GStatBuf statbuf;
1112
0
  GVfsClass *class;
1113
0
  GVfs *vfs;
1114
0
  int errsv;
1115
1116
0
  parent = g_file_get_parent (file);
1117
0
  if (parent == NULL)
1118
0
    {
1119
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1120
0
                           _("Can’t rename root directory"));
1121
0
      return NULL;
1122
0
    }
1123
  
1124
0
  new_file = g_file_get_child_for_display_name (parent, display_name, error);
1125
0
  g_object_unref (parent);
1126
  
1127
0
  if (new_file == NULL)
1128
0
    return NULL;
1129
0
  local = G_LOCAL_FILE (file);
1130
0
  new_local = G_LOCAL_FILE (new_file);
1131
1132
0
  if (g_lstat (new_local->filename, &statbuf) == -1) 
1133
0
    {
1134
0
      errsv = errno;
1135
1136
0
      if (errsv != ENOENT)
1137
0
        {
1138
0
          g_set_io_error (error, _("Error renaming file %s: %s"), new_file, errsv);
1139
0
          g_object_unref (new_file);
1140
0
          return NULL;
1141
0
        }
1142
0
    }
1143
0
  else
1144
0
    {
1145
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1146
0
                           _("Can’t rename file, filename already exists"));
1147
0
      g_object_unref (new_file);
1148
0
      return NULL;
1149
0
    }
1150
1151
0
  if (g_rename (local->filename, new_local->filename) == -1)
1152
0
    {
1153
0
      errsv = errno;
1154
1155
0
      if (errsv == EINVAL)
1156
        /* We can't get a rename file into itself error here,
1157
         * so this must be an invalid filename, on e.g. FAT
1158
         */
1159
0
        g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_FILENAME,
1160
0
                             _("Invalid filename"));
1161
0
      else
1162
0
        g_set_io_error (error,
1163
0
                        _("Error renaming file %s: %s"),
1164
0
                        file, errsv);
1165
0
      g_object_unref (new_file);
1166
0
      return NULL;
1167
0
    }
1168
1169
0
  vfs = g_vfs_get_default ();
1170
0
  class = G_VFS_GET_CLASS (vfs);
1171
0
  if (class->local_file_moved)
1172
0
    class->local_file_moved (vfs, local->filename, new_local->filename);
1173
1174
0
  return new_file;
1175
0
}
1176
1177
static GFileInfo *
1178
g_local_file_query_info (GFile                *file,
1179
                         const char           *attributes,
1180
                         GFileQueryInfoFlags   flags,
1181
                         GCancellable         *cancellable,
1182
                         GError              **error)
1183
0
{
1184
0
  GLocalFile *local = G_LOCAL_FILE (file);
1185
0
  GFileInfo *info;
1186
0
  GFileAttributeMatcher *matcher;
1187
0
  char *basename, *dirname;
1188
0
  GLocalParentFileInfo parent_info;
1189
1190
0
  matcher = g_file_attribute_matcher_new (attributes);
1191
  
1192
0
  basename = g_path_get_basename (local->filename);
1193
  
1194
0
  dirname = g_path_get_dirname (local->filename);
1195
0
  _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1196
0
  g_free (dirname);
1197
  
1198
0
  info = _g_local_file_info_get (basename, local->filename,
1199
0
                                 matcher, flags, &parent_info,
1200
0
                                 error);
1201
  
1202
1203
0
  _g_local_file_info_free_parent_info (&parent_info);
1204
0
  g_free (basename);
1205
1206
0
  g_file_attribute_matcher_unref (matcher);
1207
1208
0
  return info;
1209
0
}
1210
1211
/* FIXME: faccessat() is available on FreeBSD but appears to not work correctly
1212
 * here. This needs diagnosing; https://gitlab.gnome.org/GNOME/glib/-/issues/3495
1213
 *
1214
 * On Android (bionic as of 2015-02-24), faccess() returns EINVAL if any flags are set,
1215
 * so we have to use the fallback path. See
1216
 * https://cs.android.com/android/_/android/platform/bionic/+/35778253a5ed71e87a608ca590b63729d9f88567
1217
 * 
1218
 * On Solaris, combining AT_EACCESS and AT_SYMLINK_NOFOLLOW results in EINVAL,
1219
 * since only AT_EACCESS is supported for faccessat()
1220
 * https://docs.oracle.com/cd/E86824_01/html/E54765/faccessat-2.html
1221
 */
1222
#if defined(HAVE_FACCESSAT) && !defined(__FreeBSD__) && !defined(__ANDROID__) && \
1223
    !defined(__OpenBSD__) && !defined(__sun__)
1224
static gboolean
1225
g_local_file_query_exists (GFile        *file,
1226
                           GCancellable *cancellable)
1227
0
{
1228
0
  GLocalFile *local = G_LOCAL_FILE (file);
1229
1230
0
  if (faccessat (AT_FDCWD, local->filename, F_OK, AT_EACCESS | AT_SYMLINK_NOFOLLOW) == 0)
1231
0
    return TRUE;
1232
1233
0
  if G_UNLIKELY (errno == EBADF)
1234
0
    {
1235
0
      g_critical ("g_local_file_query_exists: faccessat didn't accept supplied dirfd");
1236
0
    }
1237
0
  else if G_UNLIKELY (errno == EINVAL)
1238
0
    {
1239
0
      g_critical ("g_local_file_query_exists: faccessat doesn't support supplied flags");
1240
0
    }
1241
1242
0
  return FALSE;
1243
0
}
1244
#endif
1245
1246
static GFileAttributeInfoList *
1247
g_local_file_query_settable_attributes (GFile         *file,
1248
                                        GCancellable  *cancellable,
1249
                                        GError       **error)
1250
0
{
1251
0
  return g_file_attribute_info_list_ref (local_writable_attributes);
1252
0
}
1253
1254
static GFileAttributeInfoList *
1255
g_local_file_query_writable_namespaces (GFile         *file,
1256
                                        GCancellable  *cancellable,
1257
                                        GError       **error)
1258
0
{
1259
0
  GFileAttributeInfoList *list;
1260
0
  GVfsClass *class;
1261
0
  GVfs *vfs;
1262
1263
0
  if (g_once_init_enter_pointer (&local_writable_namespaces))
1264
0
    {
1265
      /* Writable namespaces: */
1266
1267
0
      list = g_file_attribute_info_list_new ();
1268
1269
0
#ifdef HAVE_XATTR
1270
0
      g_file_attribute_info_list_add (list,
1271
0
                                      "xattr",
1272
0
                                      G_FILE_ATTRIBUTE_TYPE_STRING,
1273
0
                                      G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
1274
0
                                      G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1275
0
      g_file_attribute_info_list_add (list,
1276
0
                                      "xattr-sys",
1277
0
                                      G_FILE_ATTRIBUTE_TYPE_STRING,
1278
0
                                      G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1279
0
#endif
1280
1281
0
      vfs = g_vfs_get_default ();
1282
0
      class = G_VFS_GET_CLASS (vfs);
1283
0
      if (class->add_writable_namespaces)
1284
0
        class->add_writable_namespaces (vfs, list);
1285
1286
0
      g_once_init_leave_pointer (&local_writable_namespaces, list);
1287
0
    }
1288
0
  list = (GFileAttributeInfoList *)local_writable_namespaces;
1289
1290
0
  return g_file_attribute_info_list_ref (list);
1291
0
}
1292
1293
static gboolean
1294
g_local_file_set_attribute (GFile                *file,
1295
                            const char           *attribute,
1296
                            GFileAttributeType    type,
1297
                            gpointer              value_p,
1298
                            GFileQueryInfoFlags   flags,
1299
                            GCancellable         *cancellable,
1300
                            GError              **error)
1301
0
{
1302
0
  GLocalFile *local = G_LOCAL_FILE (file);
1303
1304
0
  return _g_local_file_info_set_attribute (local->filename,
1305
0
                                           attribute,
1306
0
                                           type,
1307
0
                                           value_p,
1308
0
                                           flags,
1309
0
                                           cancellable,
1310
0
                                           error);
1311
0
}
1312
1313
static gboolean
1314
g_local_file_set_attributes_from_info (GFile                *file,
1315
                                       GFileInfo            *info,
1316
                                       GFileQueryInfoFlags   flags,
1317
                                       GCancellable         *cancellable,
1318
                                       GError              **error)
1319
0
{
1320
0
  GLocalFile *local = G_LOCAL_FILE (file);
1321
0
  int res, chained_res;
1322
0
  GFileIface *default_iface;
1323
1324
0
  res = _g_local_file_info_set_attributes (local->filename,
1325
0
                                           info, flags, 
1326
0
                                           cancellable,
1327
0
                                           error);
1328
1329
0
  if (!res)
1330
0
    error = NULL; /* Don't write over error if further errors */
1331
1332
0
  default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1333
1334
0
  chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1335
  
1336
0
  return res && chained_res;
1337
0
}
1338
1339
static GFileInputStream *
1340
g_local_file_read (GFile         *file,
1341
                   GCancellable  *cancellable,
1342
                   GError       **error)
1343
0
{
1344
0
  GLocalFile *local = G_LOCAL_FILE (file);
1345
0
  int fd, ret;
1346
0
  GLocalFileStat buf;
1347
  
1348
0
  fd = g_open (local->filename, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
1349
0
  if (fd == -1)
1350
0
    {
1351
0
      int errsv = errno;
1352
1353
#ifdef G_OS_WIN32
1354
      if (errsv == EACCES)
1355
        {
1356
          /* Exploit the fact that on W32 the glib filename encoding is UTF8 */
1357
          ret = GLIB_PRIVATE_CALL (g_win32_stat_utf8) (local->filename, &buf);
1358
          if (ret == 0 && S_ISDIR (buf.st_mode))
1359
            errsv = EISDIR;
1360
        }
1361
#endif
1362
0
      g_set_io_error (error,
1363
0
                      _("Error opening file %s: %s"),
1364
0
                      file, errsv);
1365
0
      return NULL;
1366
0
    }
1367
1368
0
  ret = g_local_file_fstat (fd, G_LOCAL_FILE_STAT_FIELD_TYPE, G_LOCAL_FILE_STAT_FIELD_ALL, &buf);
1369
1370
0
  if (ret == 0 && S_ISDIR (_g_stat_mode (&buf)))
1371
0
    {
1372
0
      (void) g_close (fd, NULL);
1373
0
      g_set_io_error (error,
1374
0
                      _("Error opening file %s: %s"),
1375
0
                      file, EISDIR);
1376
0
      return NULL;
1377
0
    }
1378
  
1379
0
  return _g_local_file_input_stream_new (fd);
1380
0
}
1381
1382
static GFileOutputStream *
1383
g_local_file_append_to (GFile             *file,
1384
                        GFileCreateFlags   flags,
1385
                        GCancellable      *cancellable,
1386
                        GError           **error)
1387
0
{
1388
0
  return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1389
0
                                             flags, cancellable, error);
1390
0
}
1391
1392
static GFileOutputStream *
1393
g_local_file_create (GFile             *file,
1394
                     GFileCreateFlags   flags,
1395
                     GCancellable      *cancellable,
1396
                     GError           **error)
1397
0
{
1398
0
  return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1399
0
                                             FALSE, flags, NULL,
1400
0
                                             cancellable, error);
1401
0
}
1402
1403
static GFileOutputStream *
1404
g_local_file_replace (GFile             *file,
1405
                      const char        *etag,
1406
                      gboolean           make_backup,
1407
                      GFileCreateFlags   flags,
1408
                      GCancellable      *cancellable,
1409
                      GError           **error)
1410
0
{
1411
0
  return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1412
0
                                              FALSE,
1413
0
                                              etag, make_backup, flags, NULL,
1414
0
                                              cancellable, error);
1415
0
}
1416
1417
static GFileIOStream *
1418
g_local_file_open_readwrite (GFile                      *file,
1419
                             GCancellable               *cancellable,
1420
                             GError                    **error)
1421
0
{
1422
0
  GFileOutputStream *output;
1423
0
  GFileIOStream *res;
1424
1425
0
  output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
1426
0
                                             TRUE,
1427
0
                                             cancellable, error);
1428
0
  if (output == NULL)
1429
0
    return NULL;
1430
1431
0
  res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1432
0
  g_object_unref (output);
1433
0
  return res;
1434
0
}
1435
1436
static GFileIOStream *
1437
g_local_file_create_readwrite (GFile                      *file,
1438
                               GFileCreateFlags            flags,
1439
                               GCancellable               *cancellable,
1440
                               GError                    **error)
1441
0
{
1442
0
  GFileOutputStream *output;
1443
0
  GFileIOStream *res;
1444
1445
0
  output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1446
0
                                               TRUE, flags, NULL,
1447
0
                                               cancellable, error);
1448
0
  if (output == NULL)
1449
0
    return NULL;
1450
1451
0
  res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1452
0
  g_object_unref (output);
1453
0
  return res;
1454
0
}
1455
1456
static GFileIOStream *
1457
g_local_file_replace_readwrite (GFile                      *file,
1458
                                const char                 *etag,
1459
                                gboolean                    make_backup,
1460
                                GFileCreateFlags            flags,
1461
                                GCancellable               *cancellable,
1462
                                GError                    **error)
1463
0
{
1464
0
  GFileOutputStream *output;
1465
0
  GFileIOStream *res;
1466
1467
0
  output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1468
0
                                                TRUE,
1469
0
                                                etag, make_backup, flags, NULL,
1470
0
                                                cancellable, error);
1471
0
  if (output == NULL)
1472
0
    return NULL;
1473
1474
0
  res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1475
0
  g_object_unref (output);
1476
0
  return res;
1477
0
}
1478
1479
static gboolean
1480
g_local_file_delete (GFile         *file,
1481
                     GCancellable  *cancellable,
1482
                     GError       **error)
1483
0
{
1484
0
  GLocalFile *local = G_LOCAL_FILE (file);
1485
0
  GVfsClass *class;
1486
0
  GVfs *vfs;
1487
1488
0
  if (g_remove (local->filename) == -1)
1489
0
    {
1490
0
      int errsv = errno;
1491
1492
      /* Posix allows EEXIST too, but the clearer error
1493
         is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1494
         expects */
1495
0
      if (errsv == EEXIST)
1496
0
        errsv = ENOTEMPTY;
1497
1498
0
      g_set_io_error (error,
1499
0
                      _("Error removing file %s: %s"),
1500
0
                      file, errsv);
1501
0
      return FALSE;
1502
0
    }
1503
1504
0
  vfs = g_vfs_get_default ();
1505
0
  class = G_VFS_GET_CLASS (vfs);
1506
0
  if (class->local_file_removed)
1507
0
    class->local_file_removed (vfs, local->filename);
1508
1509
0
  return TRUE;
1510
0
}
1511
1512
#ifndef G_OS_WIN32
1513
1514
static char *
1515
strip_trailing_slashes (const char *path)
1516
0
{
1517
0
  char *path_copy;
1518
0
  size_t len;
1519
1520
0
  path_copy = g_strdup (path);
1521
0
  len = strlen (path_copy);
1522
0
  while (len > 1 && path_copy[len-1] == '/')
1523
0
    path_copy[--len] = 0;
1524
1525
0
  return path_copy;
1526
0
 }
1527
1528
static char *
1529
expand_symlink (const char *link)
1530
0
{
1531
0
  char *resolved, *canonical, *parent, *link2;
1532
0
  char symlink_value[4096];
1533
#ifdef G_OS_WIN32
1534
#else
1535
0
  gssize res;
1536
0
#endif
1537
  
1538
#ifdef G_OS_WIN32
1539
#else
1540
0
  res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1541
  
1542
0
  if (res == -1)
1543
0
    return g_strdup (link);
1544
0
  symlink_value[res] = 0;
1545
0
#endif
1546
  
1547
0
  if (g_path_is_absolute (symlink_value))
1548
0
    return g_canonicalize_filename (symlink_value, NULL);
1549
0
  else
1550
0
    {
1551
0
      link2 = strip_trailing_slashes (link);
1552
0
      parent = g_path_get_dirname (link2);
1553
0
      g_free (link2);
1554
      
1555
0
      resolved = g_build_filename (parent, symlink_value, NULL);
1556
0
      g_free (parent);
1557
      
1558
0
      canonical = g_canonicalize_filename (resolved, NULL);
1559
      
1560
0
      g_free (resolved);
1561
1562
0
      return canonical;
1563
0
    }
1564
0
}
1565
1566
static char *
1567
expand_symlinks (const char *path,
1568
                 dev_t      *dev)
1569
0
{
1570
0
  char *tmp, *target;
1571
0
  GStatBuf target_stat;
1572
0
  int num_recursions;
1573
1574
0
  target = g_strdup (path);
1575
1576
0
  num_recursions = 0;
1577
0
  do
1578
0
    {
1579
0
      if (g_lstat (target, &target_stat) != 0)
1580
0
        {
1581
0
          g_free (target);
1582
0
          return NULL;
1583
0
        }
1584
1585
0
      if (S_ISLNK (target_stat.st_mode))
1586
0
        {
1587
0
          tmp = target;
1588
0
          target = expand_symlink (target);
1589
0
          g_free (tmp);
1590
0
        }
1591
1592
0
      num_recursions++;
1593
1594
#ifdef MAXSYMLINKS
1595
      if (num_recursions > MAXSYMLINKS)
1596
#else
1597
      /* 40 is used in kernel sources currently:
1598
       * https://github.com/torvalds/linux/include/linux/namei.h
1599
       */
1600
0
      if (num_recursions > 40)
1601
0
#endif
1602
0
        {
1603
0
          g_free (target);
1604
0
          return NULL;
1605
0
        }
1606
0
    }
1607
0
  while (S_ISLNK (target_stat.st_mode));
1608
1609
0
  if (dev)
1610
0
    *dev = target_stat.st_dev;
1611
1612
0
  return target;
1613
0
}
1614
1615
static char *
1616
get_parent (const char *path,
1617
            dev_t      *parent_dev)
1618
0
{
1619
0
  char *parent, *res;
1620
0
  char *path_copy;
1621
1622
0
  path_copy = strip_trailing_slashes (path);
1623
  
1624
0
  parent = g_path_get_dirname (path_copy);
1625
0
  if (strcmp (parent, ".") == 0)
1626
0
    {
1627
0
      g_free (parent);
1628
0
      g_free (path_copy);
1629
0
      return NULL;
1630
0
    }
1631
0
  g_free (path_copy);
1632
1633
0
  res = expand_symlinks (parent, parent_dev);
1634
0
  g_free (parent);
1635
1636
0
  return res;
1637
0
}
1638
1639
#ifndef HAVE_COCOA
1640
static char *
1641
expand_all_symlinks (const char *path)
1642
0
{
1643
0
  char *parent, *parent_expanded;
1644
0
  char *basename, *res;
1645
0
  dev_t parent_dev;
1646
1647
0
  parent = get_parent (path, &parent_dev);
1648
0
  if (parent == NULL)
1649
0
    return NULL;
1650
1651
0
  if (g_strcmp0 (parent, "/") != 0)
1652
0
    {
1653
0
      parent_expanded = expand_all_symlinks (parent);
1654
0
      basename = g_path_get_basename (path);
1655
0
      res = g_build_filename (parent_expanded, basename, NULL);
1656
0
      g_free (basename);
1657
0
      g_free (parent_expanded);
1658
0
    }
1659
0
  else
1660
0
    res = g_strdup (path);
1661
1662
0
  g_free (parent);
1663
1664
0
  return res;
1665
0
}
1666
#endif /* HAVE_COCOA */
1667
1668
static char *
1669
find_mountpoint_for (const char *file,
1670
                     dev_t       dev,
1671
                     gboolean    resolve_basename_symlink)
1672
0
{
1673
0
  char *dir, *parent;
1674
0
  dev_t dir_dev, parent_dev;
1675
1676
0
  if (resolve_basename_symlink)
1677
0
    {
1678
0
      dir = expand_symlinks (file, NULL);
1679
0
      if (dir == NULL)
1680
0
        return NULL;
1681
0
    }
1682
0
  else
1683
0
    dir = g_strdup (file);
1684
1685
0
  dir_dev = dev;
1686
1687
0
  while (g_strcmp0 (dir, "/") != 0)
1688
0
    {
1689
0
      parent = get_parent (dir, &parent_dev);
1690
0
      if (parent == NULL)
1691
0
        {
1692
0
          g_free (dir);
1693
0
          return NULL;
1694
0
        }
1695
1696
0
      if (parent_dev != dir_dev)
1697
0
        {
1698
0
          g_free (parent);
1699
0
          return dir;
1700
0
        }
1701
    
1702
0
      g_free (dir);
1703
0
      dir = parent;
1704
0
    }
1705
1706
0
  return dir;
1707
0
}
1708
1709
char *
1710
_g_local_file_find_topdir_for (const char *file)
1711
0
{
1712
0
  char *dir;
1713
0
  char *mountpoint = NULL;
1714
0
  dev_t dir_dev;
1715
1716
0
  dir = get_parent (file, &dir_dev);
1717
0
  if (dir == NULL)
1718
0
    return NULL;
1719
1720
0
  mountpoint = find_mountpoint_for (dir, dir_dev, TRUE);
1721
0
  g_free (dir);
1722
1723
0
  return mountpoint;
1724
0
}
1725
1726
#ifndef HAVE_COCOA
1727
static char *
1728
get_unique_filename (const char *basename, 
1729
                     int         id)
1730
0
{
1731
0
  const char *dot;
1732
      
1733
0
  if (id == 1)
1734
0
    return g_strdup (basename);
1735
1736
0
  dot = strchr (basename, '.');
1737
0
  if (dot)
1738
0
    return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1739
0
  else
1740
0
    return g_strdup_printf ("%s.%d", basename, id);
1741
0
}
1742
1743
/*
1744
 * Truncate @basename from the front so that @suffix_len more bytes (e.g. the
1745
 * ".trashinfo" suffix) can still be appended without exceeding NAME_MAX, while
1746
 * keeping the trailing part of the original name (its extension, if any).
1747
 *
1748
 * When the filename encoding is UTF-8, g_utf8_next_char() is used to skip
1749
 * whole characters from the front so the cut never lands in the middle of a
1750
 * multi-byte sequence, which would yield invalid UTF-8 (a garbled name) in
1751
 * the trash.
1752
 *
1753
 * For other (typically single-byte) encodings the cut is performed on a plain
1754
 * byte boundary, which is always safe for fixed-width encodings.  Rare
1755
 * multi-byte non-UTF-8 encodings (e.g. Shift-JIS) are handled best-effort, in
1756
 * line with GLib's general treatment of filename encodings.
1757
 *
1758
 * Returns the new length (always > 0) on success, or 0 when the name is too
1759
 * short to be truncated safely (the caller should then fail with ENAMETOOLONG).
1760
 */
1761
static size_t
1762
truncate_basename_front (char   *basename,
1763
                         size_t  basename_len,
1764
                         size_t  suffix_len)
1765
0
{
1766
0
  const char *start, *end;
1767
1768
0
  if (basename_len <= suffix_len)
1769
0
    return 0;
1770
1771
0
  if (g_get_filename_charsets (NULL))
1772
0
    {
1773
      /* UTF-8: skip whole characters from the front so the cut never lands
1774
       * in the middle of a multi-byte sequence. */
1775
0
      start = basename;
1776
0
      end = basename + basename_len;
1777
1778
0
      while ((gsize) (start - basename) < suffix_len)
1779
0
        start = g_utf8_next_char (start);
1780
1781
0
      if (start >= end)
1782
0
        return 0;
1783
1784
0
      basename_len = end - start;
1785
0
    }
1786
0
  else
1787
0
    {
1788
      /* Non-UTF-8 (single-byte or best-effort): plain front cut. */
1789
0
      basename_len -= suffix_len;
1790
0
      start = basename + suffix_len;
1791
0
    }
1792
1793
0
  memmove (basename, start, basename_len);
1794
0
  basename[basename_len] = '\0';
1795
0
  return basename_len;
1796
0
}
1797
#endif /* HAVE_COCOA */
1798
1799
static gboolean
1800
path_has_prefix (const char *path, 
1801
                 const char *prefix)
1802
0
{
1803
0
  int prefix_len;
1804
1805
0
  if (prefix == NULL)
1806
0
    return TRUE;
1807
1808
0
  prefix_len = strlen (prefix);
1809
  
1810
0
  if (strncmp (path, prefix, prefix_len) == 0 &&
1811
0
      (prefix_len == 0 || /* empty prefix always matches */
1812
0
       prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1813
0
       path[prefix_len] == 0 ||
1814
0
       path[prefix_len] == '/'))
1815
0
    return TRUE;
1816
  
1817
0
  return FALSE;
1818
0
}
1819
1820
#ifndef HAVE_COCOA
1821
static char *
1822
try_make_relative (const char *path, 
1823
                   const char *base)
1824
0
{
1825
0
  char *path2, *base2;
1826
0
  char *relative;
1827
1828
0
  path2 = expand_all_symlinks (path);
1829
0
  base2 = expand_all_symlinks (base);
1830
1831
0
  relative = NULL;
1832
0
  if (path2 != NULL && base2 != NULL && path_has_prefix (path2, base2))
1833
0
    {
1834
0
      relative = path2 + strlen (base2);
1835
0
      while (*relative == '/')
1836
0
        relative ++;
1837
0
      relative = g_strdup (relative);
1838
0
    }
1839
0
  g_free (path2);
1840
0
  g_free (base2);
1841
1842
0
  if (relative)
1843
0
    return relative;
1844
  
1845
  /* Failed, use abs path */
1846
0
  return g_strdup (path);
1847
0
}
1848
#endif /* HAVE_COCOA */
1849
1850
static gboolean
1851
ignore_trash_mount (GUnixMountEntry *mount)
1852
0
{
1853
0
  const gchar *mount_options;
1854
0
  gboolean is_system_internal;
1855
1856
0
  mount_options = g_unix_mount_entry_get_options (mount);
1857
1858
0
  if (mount_options != NULL)
1859
0
    {
1860
0
      if (strstr (mount_options, "x-gvfs-trash") != NULL)
1861
0
        return FALSE;
1862
1863
0
      if (strstr (mount_options, "x-gvfs-notrash") != NULL)
1864
0
        return TRUE;
1865
0
    }
1866
1867
0
  is_system_internal = g_unix_mount_entry_is_system_internal (mount);
1868
1869
0
  if (mount_options == NULL || is_system_internal)
1870
0
    {
1871
0
      GUnixMountPoint *mount_point = NULL;
1872
0
      const gchar *fstab_options = NULL;
1873
0
      gboolean fstab_trash = FALSE;
1874
0
      gboolean fstab_notrash = FALSE;
1875
1876
      /* The x-gvfs-* options are userspace-only mount options: the kernel does
1877
       * not know about them, so they never appear in /proc/self/mountinfo.
1878
       * libmount can only report them from /run/mount/utab, which requires the
1879
       * filesystem to have been mounted by mount(8) and that file to have
1880
       * survived since boot; filesystems mounted by systemd, by the initrd or
1881
       * by an image-based OS carry no utab entry at all. So fall back to the
1882
       * fstab entry for this mount path.
1883
       *
1884
       * The mount_options == NULL case is the pre-existing fallback path, kept
1885
       * unchanged for platforms whose mount entries carry no options at all.
1886
       * The system-internal case is the new one, and is deliberately limited to
1887
       * that branch, which would refuse trashing anyway: g_unix_mount_point_at()
1888
       * re-parses fstab and has no cache, while this function is on the hot path
1889
       * of the access::can-trash attribute, which is queried for every file of
1890
       * an enumeration.
1891
       */
1892
0
      mount_point = g_unix_mount_point_at (g_unix_mount_entry_get_mount_path (mount),
1893
0
                                           NULL);
1894
0
      if (mount_point != NULL)
1895
0
        fstab_options = g_unix_mount_point_get_options (mount_point);
1896
1897
0
      if (fstab_options != NULL)
1898
0
        {
1899
0
          fstab_trash = strstr (fstab_options, "x-gvfs-trash") != NULL;
1900
0
          fstab_notrash = strstr (fstab_options, "x-gvfs-notrash") != NULL;
1901
0
        }
1902
1903
0
      g_clear_pointer (&mount_point, g_unix_mount_point_free);
1904
1905
0
      if (fstab_trash)
1906
0
        return FALSE;
1907
1908
0
      if (fstab_notrash)
1909
0
        return TRUE;
1910
0
    }
1911
1912
0
  return is_system_internal;
1913
0
}
1914
1915
static gboolean
1916
ignore_trash_path (const gchar *topdir)
1917
0
{
1918
0
  GUnixMountEntry *mount;
1919
0
  gboolean retval = TRUE;
1920
1921
0
  mount = g_unix_mount_entry_at (topdir, NULL);
1922
0
  if (mount == NULL)
1923
0
    goto out;
1924
1925
0
  retval = ignore_trash_mount (mount);
1926
1927
0
 out:
1928
0
  g_clear_pointer (&mount, g_unix_mount_entry_free);
1929
1930
0
  return retval;
1931
0
}
1932
1933
gboolean
1934
_g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1935
0
{
1936
0
  static gsize home_dev_set = 0;
1937
0
  static dev_t home_dev;
1938
0
  static gboolean home_dev_valid = FALSE;
1939
0
  char *topdir, *globaldir, *trashdir, *tmpname;
1940
0
  uid_t uid;
1941
0
  char uid_str[32];
1942
0
  GStatBuf global_stat, trash_stat;
1943
0
  gboolean res;
1944
1945
0
  if (g_once_init_enter (&home_dev_set))
1946
0
    {
1947
0
      GStatBuf home_stat;
1948
1949
0
      if (g_stat (g_get_home_dir (), &home_stat) == 0)
1950
0
        {
1951
0
          home_dev = home_stat.st_dev;
1952
0
          home_dev_valid = TRUE;
1953
0
        }
1954
0
      else
1955
0
        {
1956
0
          home_dev_valid = FALSE;
1957
0
        }
1958
1959
0
      g_once_init_leave (&home_dev_set, 1);
1960
0
    }
1961
1962
  /* Assume we can trash to the home */
1963
0
  if (!home_dev_valid)
1964
0
    return FALSE;
1965
0
  else if (dir_dev == home_dev)
1966
0
    return TRUE;
1967
1968
0
  topdir = find_mountpoint_for (dirname, dir_dev, TRUE);
1969
0
  if (topdir == NULL)
1970
0
    return FALSE;
1971
1972
0
  if (ignore_trash_path (topdir))
1973
0
    {
1974
0
      g_free (topdir);
1975
1976
0
      return FALSE;
1977
0
    }
1978
1979
0
  globaldir = g_build_filename (topdir, ".Trash", NULL);
1980
0
  if (g_lstat (globaldir, &global_stat) == 0 &&
1981
0
      S_ISDIR (global_stat.st_mode) &&
1982
0
      (global_stat.st_mode & S_ISVTX) != 0)
1983
0
    {
1984
      /* got a toplevel sysadmin created dir, assume we
1985
       * can trash to it (we should be able to create a dir)
1986
       * This fails for the FAT case where the ownership of
1987
       * that dir would be wrong though..
1988
       */
1989
0
      g_free (globaldir);
1990
0
      g_free (topdir);
1991
0
      return TRUE;
1992
0
    }
1993
0
  g_free (globaldir);
1994
1995
  /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1996
0
  uid = geteuid ();
1997
0
  g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1998
1999
0
  tmpname = g_strdup_printf (".Trash-%s", uid_str);
2000
0
  trashdir = g_build_filename (topdir, tmpname, NULL);
2001
0
  g_free (tmpname);
2002
2003
0
  if (g_lstat (trashdir, &trash_stat) == 0)
2004
0
    {
2005
0
      g_free (topdir);
2006
0
      g_free (trashdir);
2007
0
      return S_ISDIR (trash_stat.st_mode) &&
2008
0
             trash_stat.st_uid == uid;
2009
0
    }
2010
0
  g_free (trashdir);
2011
2012
  /* User specific trash didn't exist, can we create it? */
2013
0
  res = g_access (topdir, W_OK) == 0;
2014
0
  g_free (topdir);
2015
2016
0
  return res;
2017
0
}
2018
2019
#ifndef G_OS_WIN32
2020
gboolean
2021
_g_local_file_is_lost_found_dir (const char *path, dev_t path_dev)
2022
0
{
2023
0
  gboolean ret = FALSE;
2024
0
  gchar *mount_dir = NULL;
2025
0
  size_t mount_dir_len;
2026
0
  GStatBuf statbuf;
2027
2028
0
  if (!g_str_has_suffix (path, "/lost+found"))
2029
0
    goto out;
2030
2031
0
  mount_dir = find_mountpoint_for (path, path_dev, FALSE);
2032
0
  if (mount_dir == NULL)
2033
0
    goto out;
2034
2035
0
  mount_dir_len = strlen (mount_dir);
2036
  /* We special-case rootfs ('/') since it's the only case where
2037
   * mount_dir ends in '/'
2038
   */
2039
0
  if (mount_dir_len == 1)
2040
0
    mount_dir_len--;
2041
0
  if (mount_dir_len + strlen ("/lost+found") != strlen (path))
2042
0
    goto out;
2043
2044
0
  if (g_lstat (path, &statbuf) != 0)
2045
0
    goto out;
2046
2047
0
  if (!(S_ISDIR (statbuf.st_mode) &&
2048
0
        statbuf.st_uid == 0 &&
2049
0
        statbuf.st_gid == 0))
2050
0
    goto out;
2051
2052
0
  ret = TRUE;
2053
2054
0
 out:
2055
0
  g_free (mount_dir);
2056
0
  return ret;
2057
0
}
2058
#endif
2059
2060
/* Check whether subsequently deleting the original file from the trash
2061
 * (in the gvfsd-trash process) will succeed. If we think it won’t, return
2062
 * an error, as the trash spec says trashing should not be allowed.
2063
 * https://specifications.freedesktop.org/trash-spec/latest/#implementation-notes
2064
 *
2065
 * Check ownership to see if we can delete. gvfsd will automatically chmod
2066
 * a file to allow it to be deleted, so checking the permissions bitfield isn’t
2067
 * relevant.
2068
 */
2069
#ifndef HAVE_COCOA
2070
static gboolean
2071
check_removing_recursively (GFile        *file,
2072
                            gboolean      user_owned,
2073
                            uid_t         uid,
2074
                            GCancellable *cancellable,
2075
                            GError       **error)
2076
0
{
2077
0
  GFileEnumerator *enumerator;
2078
2079
0
  enumerator = g_file_enumerate_children (file,
2080
0
                                          G_FILE_ATTRIBUTE_STANDARD_NAME ","
2081
0
                                          G_FILE_ATTRIBUTE_STANDARD_TYPE ","
2082
0
                                          G_FILE_ATTRIBUTE_UNIX_UID,
2083
0
                                          G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2084
0
                                          cancellable,
2085
0
                                          error);
2086
2087
0
  if (!enumerator)
2088
0
    return FALSE;
2089
2090
0
  while (TRUE)
2091
0
    {
2092
0
      GFileInfo *info;
2093
0
      GFile *child;
2094
2095
0
      if (!g_file_enumerator_iterate (enumerator, &info, &child, cancellable, error))
2096
0
        {
2097
0
          g_object_unref (enumerator);
2098
0
          return FALSE;
2099
0
        }
2100
2101
0
      if (!info)
2102
0
        break;
2103
2104
0
      if (!user_owned)
2105
0
        {
2106
0
          GLocalFile *local = G_LOCAL_FILE (child);
2107
2108
0
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
2109
0
                       _("Unable to trash child file %s"), local->filename);
2110
0
          g_object_unref (enumerator);
2111
0
          return FALSE;
2112
0
        }
2113
2114
0
      if ((g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY))
2115
0
        {
2116
0
          uid_t fuid;
2117
2118
0
          fuid = g_file_info_get_attribute_uint32 (info,
2119
0
                                                   G_FILE_ATTRIBUTE_UNIX_UID);
2120
0
          if (!check_removing_recursively (child,
2121
0
                                           fuid == uid,
2122
0
                                           uid,
2123
0
                                           cancellable,
2124
0
                                           error))
2125
0
            {
2126
0
              g_object_unref (enumerator);
2127
0
              return FALSE;
2128
0
            }
2129
0
        }
2130
0
    }
2131
0
  g_object_unref (enumerator);
2132
0
  return TRUE;
2133
0
}
2134
#endif /* HAVE_COCOA */
2135
2136
static gboolean
2137
g_local_file_trash (GFile         *file,
2138
                    GCancellable  *cancellable,
2139
                    GError       **error)
2140
0
{
2141
0
  GLocalFile *local = G_LOCAL_FILE (file);
2142
#ifdef HAVE_COCOA
2143
  return _g_local_file_trash_macos (local->filename, cancellable, error);
2144
#else
2145
0
  GStatBuf file_stat, home_stat;
2146
0
  dev_t checked_st_dev;
2147
0
  const char *homedir;
2148
0
  char *trashdir, *topdir, *infodir, *filesdir;
2149
0
  char *basename, *trashname, *trashfile, *infoname, *infofile;
2150
0
  char *original_name, *original_name_escaped;
2151
0
  int i;
2152
0
  char *data;
2153
0
  char *path;
2154
0
  gboolean is_homedir_trash;
2155
0
  char *delete_time = NULL;
2156
0
  int fd;
2157
0
  GStatBuf trash_stat, global_stat;
2158
0
  char *dirname, *globaldir;
2159
0
  GVfsClass *class;
2160
0
  GVfs *vfs;
2161
0
  int errsv;
2162
0
  size_t basename_len;
2163
0
  GError *my_error = NULL;
2164
2165
0
  if (glib_should_use_portal ())
2166
0
    return g_trash_portal_trash_file (file, error);
2167
2168
0
  if (g_lstat (local->filename, &file_stat) != 0)
2169
0
    {
2170
0
      errsv = errno;
2171
2172
0
      g_set_io_error (error,
2173
0
                      _("Error trashing file %s: %s"),
2174
0
                      file, errsv);
2175
0
      return FALSE;
2176
0
    }
2177
    
2178
0
  homedir = g_get_home_dir ();
2179
0
  if (g_stat (homedir, &home_stat) != 0)
2180
0
    {
2181
0
      errsv = errno;
2182
2183
0
      g_set_io_error (error,
2184
0
                      _("Error trashing file %s: %s"),
2185
0
                      file, errsv);
2186
0
      return FALSE;
2187
0
    }
2188
2189
0
  is_homedir_trash = FALSE;
2190
0
  trashdir = NULL;
2191
2192
0
  checked_st_dev = file_stat.st_dev;
2193
2194
  /* On overlayfs, a file's st_dev will be different to the home directory's.
2195
   * We still want to create our trash directory under the home directory, so
2196
   * instead we should stat the directory that the file we're deleting is in as
2197
   * this will have the same st_dev.
2198
   */
2199
0
  if (!S_ISDIR (file_stat.st_mode))
2200
0
    {
2201
0
      GStatBuf parent_stat;
2202
0
      path = g_path_get_dirname (local->filename);
2203
      /* If the parent is a symlink to a different device then it might have
2204
       * st_dev equal to the home directory's, in which case we will end up
2205
       * trying to rename across a filesystem boundary, which doesn't work. So
2206
       * we use g_stat here instead of g_lstat, to know where the symlink
2207
       * points to. */
2208
0
      if (g_stat (path, &parent_stat))
2209
0
        {
2210
0
          errsv = errno;
2211
0
          g_free (path);
2212
2213
0
          g_set_io_error (error,
2214
0
                          _("Error trashing file %s: %s"),
2215
0
                          file, errsv);
2216
0
          return FALSE;
2217
0
        }
2218
0
      checked_st_dev = parent_stat.st_dev;
2219
0
      g_free (path);
2220
0
    }
2221
2222
0
  if (checked_st_dev == home_stat.st_dev)
2223
0
    {
2224
0
      is_homedir_trash = TRUE;
2225
0
      errno = 0;
2226
0
      trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
2227
0
      if (g_mkdir_with_parents (trashdir, 0700) < 0)
2228
0
        {
2229
0
          char *display_name;
2230
0
          errsv = errno;
2231
2232
0
          display_name = g_filename_display_name (trashdir);
2233
0
          g_set_error (error, G_IO_ERROR,
2234
0
                       g_io_error_from_errno (errsv),
2235
0
                       _("Unable to create trash directory %s: %s"),
2236
0
                       display_name, g_strerror (errsv));
2237
0
          g_free (display_name);
2238
0
          g_free (trashdir);
2239
0
          return FALSE;
2240
0
        }
2241
0
      topdir = g_strdup (g_get_user_data_dir ());
2242
0
    }
2243
0
  else
2244
0
    {
2245
0
      uid_t uid;
2246
0
      char uid_str[32];
2247
0
      gboolean success = FALSE;
2248
2249
0
      uid = geteuid ();
2250
0
      g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
2251
2252
0
      topdir = _g_local_file_find_topdir_for (local->filename);
2253
0
      if (topdir == NULL)
2254
0
        {
2255
0
          g_set_io_error (error,
2256
0
                          _("Unable to find toplevel directory to trash %s"),
2257
0
                          file, ENOTSUP);
2258
0
          return FALSE;
2259
0
        }
2260
2261
0
      if (ignore_trash_path (topdir))
2262
0
        {
2263
0
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2264
0
                       _("Trashing on system internal mounts is not supported"));
2265
0
          g_free (topdir);
2266
2267
0
          return FALSE;
2268
0
        }
2269
2270
      /* Try looking for global trash dir $topdir/.Trash/$uid */
2271
0
      globaldir = g_build_filename (topdir, ".Trash", NULL);
2272
0
      if (g_lstat (globaldir, &global_stat) == 0 &&
2273
0
          S_ISDIR (global_stat.st_mode) &&
2274
0
          (global_stat.st_mode & S_ISVTX) != 0)
2275
0
        {
2276
0
          trashdir = g_build_filename (globaldir, uid_str, NULL);
2277
0
          success = TRUE;
2278
2279
0
          if (g_lstat (trashdir, &trash_stat) == 0)
2280
0
            {
2281
0
              if (!S_ISDIR (trash_stat.st_mode) ||
2282
0
                  trash_stat.st_uid != uid)
2283
0
                {
2284
                  /* Not a directory or not owned by user, ignore */
2285
0
                  g_free (trashdir);
2286
0
                  trashdir = NULL;
2287
0
                  success = FALSE;
2288
0
                }
2289
0
            }
2290
0
          else if (g_mkdir (trashdir, 0700) == -1)
2291
0
            {
2292
0
              g_free (trashdir);
2293
0
              trashdir = NULL;
2294
0
              success = FALSE;
2295
0
            }
2296
0
        }
2297
0
      g_free (globaldir);
2298
2299
0
      if (trashdir == NULL)
2300
0
        {
2301
0
          gboolean tried_create;
2302
          
2303
          /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
2304
0
          dirname = g_strdup_printf (".Trash-%s", uid_str);
2305
0
          trashdir = g_build_filename (topdir, dirname, NULL);
2306
0
          success = TRUE;
2307
0
          g_free (dirname);
2308
2309
0
          tried_create = FALSE;
2310
2311
0
        retry:
2312
0
          if (g_lstat (trashdir, &trash_stat) == 0)
2313
0
            {
2314
0
              if (!S_ISDIR (trash_stat.st_mode) ||
2315
0
                  trash_stat.st_uid != uid)
2316
0
                {
2317
                  /* Remove the failed directory */
2318
0
                  if (tried_create)
2319
0
                    g_remove (trashdir);
2320
                  
2321
                  /* Not a directory or not owned by user, ignore */
2322
0
                  success = FALSE;
2323
0
                }
2324
0
            }
2325
0
          else
2326
0
            {
2327
0
              if (!tried_create &&
2328
0
                  g_mkdir (trashdir, 0700) != -1)
2329
0
                {
2330
                  /* Ensure that the created dir has the right uid etc.
2331
                     This might fail on e.g. a FAT dir */
2332
0
                  tried_create = TRUE;
2333
0
                  goto retry;
2334
0
                }
2335
0
              else
2336
0
                {
2337
0
                  success = FALSE;
2338
0
                }
2339
0
            }
2340
0
        }
2341
2342
0
      if (!success)
2343
0
        {
2344
0
          gchar *trashdir_display_name = NULL, *file_display_name = NULL;
2345
2346
0
          trashdir_display_name = g_filename_display_name (trashdir);
2347
0
          file_display_name = g_filename_display_name (local->filename);
2348
0
          g_set_error (error, G_IO_ERROR,
2349
0
                       G_IO_ERROR_NOT_SUPPORTED,
2350
0
                       _("Unable to find or create trash directory %s to trash %s"),
2351
0
                       trashdir_display_name, file_display_name);
2352
2353
0
          g_free (trashdir_display_name);
2354
0
          g_free (file_display_name);
2355
2356
0
          g_free (topdir);
2357
0
          g_free (trashdir);
2358
2359
0
          return FALSE;
2360
0
        }
2361
0
    }
2362
2363
  /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
2364
2365
0
  infodir = g_build_filename (trashdir, "info", NULL);
2366
0
  filesdir = g_build_filename (trashdir, "files", NULL);
2367
2368
  /* Make sure we have the subdirectories */
2369
0
  if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
2370
0
      (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
2371
0
    {
2372
0
      gchar *trashdir_display_name = NULL, *file_display_name = NULL;
2373
2374
0
      trashdir_display_name = g_filename_display_name (trashdir);
2375
0
      file_display_name = g_filename_display_name (local->filename);
2376
0
      g_set_error (error, G_IO_ERROR,
2377
0
                   G_IO_ERROR_NOT_SUPPORTED,
2378
0
                   _("Unable to find or create trash directory %s to trash %s"),
2379
0
                   trashdir_display_name, file_display_name);
2380
2381
0
      g_free (trashdir_display_name);
2382
0
      g_free (file_display_name);
2383
2384
0
      g_free (topdir);
2385
0
      g_free (trashdir);
2386
0
      g_free (infodir);
2387
0
      g_free (filesdir);
2388
2389
0
      return FALSE;
2390
0
    }
2391
2392
0
  g_free (trashdir);
2393
2394
0
  basename = g_path_get_basename (local->filename);
2395
0
  basename_len = strlen (basename);
2396
0
  i = 1;
2397
0
  trashname = NULL;
2398
0
  infofile = NULL;
2399
0
  while (TRUE)
2400
0
    {
2401
0
      g_free (trashname);
2402
0
      g_free (infofile);
2403
2404
      /* Make sure we can create a unique info file */
2405
0
      trashname = get_unique_filename (basename, i++);
2406
0
      infoname = g_strconcat (trashname, ".trashinfo", NULL);
2407
0
      infofile = g_build_filename (infodir, infoname, NULL);
2408
0
      g_free (infoname);
2409
2410
0
      fd = g_open (infofile, O_CREAT | O_EXCL | O_CLOEXEC, 0666);
2411
0
      errsv = errno;
2412
2413
0
      if (fd == -1)
2414
0
        {
2415
0
          if (errsv == EEXIST)
2416
0
            continue;
2417
0
          else if (errsv == ENAMETOOLONG)
2418
0
            {
2419
0
              basename_len = truncate_basename_front (basename, basename_len, strlen (".trashinfo"));
2420
0
              if (basename_len == 0)
2421
0
                break; /* fail with ENAMETOOLONG */
2422
0
              i = 1;
2423
0
              continue;
2424
0
            }
2425
0
          else
2426
0
            break; /* fail with other error */
2427
0
        }
2428
2429
0
      (void) g_close (fd, NULL);
2430
2431
      /* Make sure we can write the info file */
2432
0
      if (!g_file_set_contents_full (infofile, NULL, 0,
2433
0
                                     G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING,
2434
0
                                     0600, &my_error))
2435
0
        {
2436
0
          g_unlink (infofile);
2437
0
          if (g_error_matches (my_error,
2438
0
                               G_FILE_ERROR,
2439
0
                               G_FILE_ERROR_NAMETOOLONG))
2440
0
            {
2441
0
              basename_len = truncate_basename_front (basename, basename_len, strlen (".XXXXXX"));
2442
0
              if (basename_len == 0)
2443
0
                break; /* fail with ENAMETOOLONG */
2444
0
              i = 1;
2445
0
              g_clear_error (&my_error);
2446
0
              continue;
2447
0
            }
2448
0
          else
2449
0
            break; /* fail with other error */
2450
0
        }
2451
2452
      /* file created */
2453
0
      break;
2454
0
    }
2455
2456
0
  g_free (basename);
2457
0
  g_free (infodir);
2458
2459
0
  if (fd == -1 || my_error)
2460
0
    {
2461
0
      g_free (filesdir);
2462
0
      g_free (topdir);
2463
0
      g_free (trashname);
2464
0
      g_free (infofile);
2465
2466
0
      if (my_error)
2467
0
        g_propagate_error (error, my_error);
2468
0
      else
2469
0
        {
2470
0
          g_set_io_error (error,
2471
0
                          _("Unable to create trashing info file for %s: %s"),
2472
0
                          file, errsv);
2473
0
        }
2474
2475
0
      return FALSE;
2476
0
    }
2477
2478
  /* Write the full content of the info file before trashing to make
2479
   * sure someone doesn't read an empty file.  See #749314
2480
   */
2481
2482
  /* Use absolute names for homedir */
2483
0
  if (is_homedir_trash)
2484
0
    original_name = g_strdup (local->filename);
2485
0
  else
2486
0
    original_name = try_make_relative (local->filename, topdir);
2487
0
  original_name_escaped = g_uri_escape_string (original_name, "/", FALSE);
2488
  
2489
0
  g_free (original_name);
2490
0
  g_free (topdir);
2491
  
2492
0
  {
2493
0
    GDateTime *now = g_date_time_new_now_local ();
2494
0
    if (now != NULL)
2495
0
      delete_time = g_date_time_format (now, "%Y-%m-%dT%H:%M:%S");
2496
0
    else
2497
0
      delete_time = g_strdup ("9999-12-31T23:59:59");
2498
0
    g_date_time_unref (now);
2499
0
  }
2500
2501
0
  data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2502
0
                          original_name_escaped, delete_time);
2503
0
  g_free (delete_time);
2504
0
  g_clear_pointer (&original_name_escaped, g_free);
2505
2506
0
  if (!g_file_set_contents_full (infofile, data, -1,
2507
0
                            G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING,
2508
0
                            0600, error))
2509
0
    {
2510
0
      g_unlink (infofile);
2511
2512
0
      g_free (data);
2513
0
      g_free (filesdir);
2514
0
      g_free (trashname);
2515
0
      g_free (infofile);
2516
2517
0
      return FALSE;
2518
0
    }
2519
2520
0
  g_clear_pointer (&data, g_free);
2521
2522
0
  if (S_ISDIR (file_stat.st_mode))
2523
0
    {
2524
0
      uid_t uid = geteuid ();
2525
2526
0
      if (file_stat.st_uid == uid &&
2527
0
          !check_removing_recursively (file, TRUE, uid, cancellable, error))
2528
0
        {
2529
0
          g_unlink (infofile);
2530
2531
0
          g_free (filesdir);
2532
0
          g_free (trashname);
2533
0
          g_free (infofile);
2534
2535
0
          return FALSE;
2536
0
        }
2537
0
    }
2538
2539
0
  trashfile = g_build_filename (filesdir, trashname, NULL);
2540
2541
0
  g_free (filesdir);
2542
2543
0
  if (g_rename (local->filename, trashfile) == -1)
2544
0
    {
2545
0
      errsv = errno;
2546
2547
0
      g_unlink (infofile);
2548
2549
0
      g_free (trashname);
2550
0
      g_free (infofile);
2551
0
      g_free (trashfile);
2552
2553
0
      if (errsv == EXDEV)
2554
        /* The trash dir was actually on another fs anyway!?
2555
         * This can happen when the same device is mounted multiple
2556
         * times, or with bind mounts of the same fs.
2557
         */
2558
0
        g_set_io_error (error,
2559
0
                        _("Unable to trash file %s across filesystem boundaries"),
2560
0
                        file, ENOTSUP);
2561
0
      else
2562
0
        g_set_io_error (error,
2563
0
                        _("Unable to trash file %s: %s"),
2564
0
                        file, errsv);
2565
0
      return FALSE;
2566
0
    }
2567
2568
0
  vfs = g_vfs_get_default ();
2569
0
  class = G_VFS_GET_CLASS (vfs);
2570
0
  if (class->local_file_moved)
2571
0
    class->local_file_moved (vfs, local->filename, trashfile);
2572
2573
0
  g_free (trashfile);
2574
2575
  /* TODO: Do we need to update mtime/atime here after the move? */
2576
2577
0
  g_free (infofile);
2578
0
  g_free (trashname);
2579
  
2580
0
  return TRUE;
2581
0
#endif
2582
0
}
2583
#else /* G_OS_WIN32 */
2584
gboolean
2585
_g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
2586
{
2587
  return FALSE;                 /* XXX ??? */
2588
}
2589
2590
static gboolean
2591
g_local_file_trash (GFile         *file,
2592
                    GCancellable  *cancellable,
2593
                    GError       **error)
2594
{
2595
  GLocalFile *local = G_LOCAL_FILE (file);
2596
  SHFILEOPSTRUCTW op = {0};
2597
  gboolean success;
2598
  wchar_t *wfilename;
2599
  long len;
2600
  int errcode;
2601
2602
  wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
2603
  /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2604
  wfilename = g_renew (wchar_t, wfilename, len + 2);
2605
  wfilename[len + 1] = 0;
2606
2607
  op.wFunc = FO_DELETE;
2608
  op.pFrom = wfilename;
2609
  op.fFlags = FOF_ALLOWUNDO;
2610
2611
  errcode = SHFileOperationW (&op);
2612
  success = errcode == 0;
2613
2614
  if ((success || errcode == ERROR_CANCELLED) && op.fAnyOperationsAborted)
2615
    {
2616
      if (cancellable && !g_cancellable_is_cancelled (cancellable))
2617
        g_cancellable_cancel (cancellable);
2618
      g_set_io_error (error,
2619
                      _("Unable to trash file %s: %s"),
2620
                      file, ECANCELED);
2621
      success = FALSE;
2622
    }
2623
  else if (!success)
2624
    g_set_io_error (error,
2625
                    _("Unable to trash file %s"),
2626
                    file, 0);
2627
2628
  g_free (wfilename);
2629
  return success;
2630
}
2631
#endif /* G_OS_WIN32 */
2632
2633
static gboolean
2634
g_local_file_make_directory (GFile         *file,
2635
                             GCancellable  *cancellable,
2636
                             GError       **error)
2637
0
{
2638
0
  GLocalFile *local = G_LOCAL_FILE (file);
2639
  
2640
0
  if (g_mkdir (local->filename, 0777) == -1)
2641
0
    {
2642
0
      int errsv = errno;
2643
2644
0
      if (errsv == EINVAL)
2645
        /* This must be an invalid filename, on e.g. FAT */
2646
0
        g_set_error_literal (error, G_IO_ERROR,
2647
0
                             G_IO_ERROR_INVALID_FILENAME,
2648
0
                             _("Invalid filename"));
2649
0
      else
2650
0
        g_set_io_error (error,
2651
0
                        _("Error creating directory %s: %s"),
2652
0
                        file, errsv);
2653
0
      return FALSE;
2654
0
    }
2655
  
2656
0
  return TRUE;
2657
0
}
2658
2659
#ifdef HAVE_SYMLINK
2660
static gboolean
2661
g_local_file_make_symbolic_link (GFile         *file,
2662
                                 const char    *symlink_value,
2663
                                 GCancellable  *cancellable,
2664
                                 GError       **error)
2665
0
{
2666
0
  GLocalFile *local = G_LOCAL_FILE (file);
2667
  
2668
0
  if (symlink (symlink_value, local->filename) == -1)
2669
0
    {
2670
0
      int errsv = errno;
2671
2672
0
      if (errsv == EINVAL)
2673
        /* This must be an invalid filename, on e.g. FAT */
2674
0
        g_set_error_literal (error, G_IO_ERROR,
2675
0
                             G_IO_ERROR_INVALID_FILENAME,
2676
0
                             _("Invalid filename"));
2677
0
      else if (errsv == EPERM)
2678
0
        g_set_error (error, G_IO_ERROR,
2679
0
                     G_IO_ERROR_NOT_SUPPORTED,
2680
0
                     _("Filesystem does not support symbolic links"));
2681
0
      else
2682
0
        g_set_io_error (error,
2683
0
                        _("Error making symbolic link %s: %s"),
2684
0
                        file, errsv);
2685
0
      return FALSE;
2686
0
    }
2687
0
  return TRUE;
2688
0
}
2689
#endif
2690
2691
static gboolean
2692
g_local_file_move (GFile                  *source,
2693
                   GFile                  *destination,
2694
                   GFileCopyFlags          flags,
2695
                   GCancellable           *cancellable,
2696
                   GFileProgressCallback   progress_callback,
2697
                   gpointer                progress_callback_data,
2698
                   GError                **error)
2699
0
{
2700
0
  GLocalFile *local_source, *local_destination;
2701
0
  GStatBuf statbuf;
2702
0
  gboolean destination_exist, source_is_dir;
2703
0
  char *backup_name;
2704
0
  int res;
2705
0
  off_t source_size;
2706
0
  GVfsClass *class;
2707
0
  GVfs *vfs;
2708
2709
0
  if (!G_IS_LOCAL_FILE (source) ||
2710
0
      !G_IS_LOCAL_FILE (destination))
2711
0
    {
2712
      /* Fall back to default move */
2713
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2714
0
      return FALSE;
2715
0
    }
2716
  
2717
0
  local_source = G_LOCAL_FILE (source);
2718
0
  local_destination = G_LOCAL_FILE (destination);
2719
  
2720
0
  res = g_lstat (local_source->filename, &statbuf);
2721
0
  if (res == -1)
2722
0
    {
2723
0
      int errsv = errno;
2724
2725
0
      g_set_io_error (error,
2726
0
                      _("Error moving file %s: %s"),
2727
0
                      source, errsv);
2728
0
      return FALSE;
2729
0
    }
2730
2731
0
  source_is_dir = S_ISDIR (statbuf.st_mode);
2732
0
  source_size = statbuf.st_size;
2733
  
2734
0
  destination_exist = FALSE;
2735
0
  res = g_lstat (local_destination->filename, &statbuf);
2736
0
  if (res == 0)
2737
0
    {
2738
0
      destination_exist = TRUE; /* Target file exists */
2739
2740
0
      if (flags & G_FILE_COPY_OVERWRITE)
2741
0
        {
2742
          /* Always fail on dirs, even with overwrite */
2743
0
          if (S_ISDIR (statbuf.st_mode))
2744
0
            {
2745
0
              if (source_is_dir)
2746
0
                g_set_error_literal (error,
2747
0
                                     G_IO_ERROR,
2748
0
                                     G_IO_ERROR_WOULD_MERGE,
2749
0
                                     _("Can’t move directory over directory"));
2750
0
              else
2751
0
                g_set_error_literal (error,
2752
0
                                     G_IO_ERROR,
2753
0
                                     G_IO_ERROR_IS_DIRECTORY,
2754
0
                                     _("Can’t copy over directory"));
2755
0
              return FALSE;
2756
0
            }
2757
0
        }
2758
0
      else
2759
0
        {
2760
0
          g_set_io_error (error,
2761
0
                          _("Error moving file %s: %s"),
2762
0
                          source, EEXIST);
2763
0
          return FALSE;
2764
0
        }
2765
0
    }
2766
2767
0
  if (flags & G_FILE_COPY_BACKUP && destination_exist)
2768
0
    {
2769
0
      backup_name = g_strconcat (local_destination->filename, "~", NULL);
2770
0
      if (g_rename (local_destination->filename, backup_name) == -1)
2771
0
        {
2772
0
          g_set_error_literal (error,
2773
0
                               G_IO_ERROR,
2774
0
                               G_IO_ERROR_CANT_CREATE_BACKUP,
2775
0
                               _("Backup file creation failed"));
2776
0
          g_free (backup_name);
2777
0
          return FALSE;
2778
0
        }
2779
0
      g_free (backup_name);
2780
0
      destination_exist = FALSE; /* It did, but no more */
2781
0
    }
2782
2783
0
  if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2784
0
    {
2785
      /* Source is a dir, destination exists (and is not a dir, because that would have failed
2786
         earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2787
0
      res = g_unlink (local_destination->filename);
2788
0
      if (res == -1)
2789
0
        {
2790
0
          int errsv = errno;
2791
2792
0
          g_set_error (error, G_IO_ERROR,
2793
0
                       g_io_error_from_errno (errsv),
2794
0
                       _("Error removing target file: %s"),
2795
0
                       g_strerror (errsv));
2796
0
          return FALSE;
2797
0
        }
2798
0
    }
2799
  
2800
0
  if (g_rename (local_source->filename, local_destination->filename) == -1)
2801
0
    {
2802
0
      int errsv = errno;
2803
2804
0
      if (errsv == EXDEV)
2805
        /* This will cause the fallback code to run */
2806
0
        g_set_error_literal (error, G_IO_ERROR,
2807
0
                             G_IO_ERROR_NOT_SUPPORTED,
2808
0
                             _("Move between mounts not supported"));
2809
0
      else if (errsv == EINVAL)
2810
        /* This must be an invalid filename, on e.g. FAT, or
2811
           we're trying to move the file into itself...
2812
           We return invalid filename for both... */
2813
0
        g_set_error_literal (error, G_IO_ERROR,
2814
0
                             G_IO_ERROR_INVALID_FILENAME,
2815
0
                             _("Invalid filename"));
2816
0
      else
2817
0
        g_set_io_error (error,
2818
0
                        _("Error moving file %s: %s"),
2819
0
                        source, errsv);
2820
0
      return FALSE;
2821
0
    }
2822
2823
0
  vfs = g_vfs_get_default ();
2824
0
  class = G_VFS_GET_CLASS (vfs);
2825
0
  if (class->local_file_moved)
2826
0
    class->local_file_moved (vfs, local_source->filename, local_destination->filename);
2827
2828
  /* Make sure we send full copied size */
2829
0
  if (progress_callback)
2830
0
    progress_callback (source_size, source_size, progress_callback_data);
2831
  
2832
0
  return TRUE;
2833
0
}
2834
2835
#ifdef G_OS_WIN32
2836
2837
gboolean
2838
g_local_file_is_nfs_home (const gchar *filename)
2839
{
2840
  return FALSE;
2841
}
2842
2843
#else
2844
2845
static gboolean
2846
is_remote_fs_type (const gchar *fsname)
2847
0
{
2848
0
  if (fsname != NULL)
2849
0
    {
2850
0
      if (strcmp (fsname, "nfs") == 0)
2851
0
        return TRUE;
2852
0
      if (strcmp (fsname, "nfs4") == 0)
2853
0
        return TRUE;
2854
0
      if (strcmp (fsname, "cifs") == 0)
2855
0
        return TRUE;
2856
0
      if (strcmp (fsname, "smb") == 0)
2857
0
        return TRUE;
2858
0
      if (strcmp (fsname, "smb2") == 0)
2859
0
        return TRUE;
2860
0
      if (strcmp (fsname, "fuse.sshfs") == 0)
2861
0
        return TRUE;
2862
0
    }
2863
2864
0
  return FALSE;
2865
0
}
2866
2867
gboolean
2868
g_local_file_is_nfs_home (const gchar *filename)
2869
0
{
2870
0
  static gboolean remote_home = FALSE;
2871
0
  static gsize initialized;
2872
0
  const gchar *home;
2873
2874
0
  home = g_get_home_dir ();
2875
0
  if (path_has_prefix (filename, home))
2876
0
    {
2877
0
      if (g_once_init_enter (&initialized))
2878
0
        {
2879
0
          GFile *file;
2880
0
          GFileInfo *info;
2881
0
          const gchar *fs_type = NULL;
2882
2883
0
          file = _g_local_file_new (home);
2884
0
          info = g_local_file_query_filesystem_info (file, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, NULL, NULL);
2885
0
          if (info != NULL)
2886
0
            fs_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE);
2887
0
          if (g_strcmp0 (fs_type, "nfs") == 0 || g_strcmp0 (fs_type, "nfs4") == 0)
2888
0
            remote_home = TRUE;
2889
0
          g_clear_object (&info);
2890
0
          g_object_unref (file);
2891
2892
0
          g_once_init_leave (&initialized, TRUE);
2893
0
        }
2894
0
      return remote_home;
2895
0
    }
2896
2897
0
  return FALSE;
2898
0
}
2899
#endif /* !G_OS_WIN32 */
2900
2901
static GFileMonitor*
2902
g_local_file_monitor_dir (GFile             *file,
2903
                          GFileMonitorFlags  flags,
2904
                          GCancellable      *cancellable,
2905
                          GError           **error)
2906
0
{
2907
0
  GLocalFile *local_file = G_LOCAL_FILE (file);
2908
2909
0
  return g_local_file_monitor_new_for_path (local_file->filename, TRUE, flags, error);
2910
0
}
2911
2912
static GFileMonitor*
2913
g_local_file_monitor_file (GFile             *file,
2914
                           GFileMonitorFlags  flags,
2915
                           GCancellable      *cancellable,
2916
                           GError           **error)
2917
0
{
2918
0
  GLocalFile *local_file = G_LOCAL_FILE (file);
2919
2920
0
  return g_local_file_monitor_new_for_path (local_file->filename, FALSE, flags, error);
2921
0
}
2922
2923
/* Here is the GLocalFile implementation of g_file_measure_disk_usage().
2924
 *
2925
 * If available, we use fopenat() in preference to filenames for
2926
 * efficiency and safety reasons.  We know that fopenat() is available
2927
 * based on if AT_FDCWD is defined.  POSIX guarantees that this will be
2928
 * defined as a macro.
2929
 *
2930
 * We use a linked list of stack-allocated GSList nodes in order to be
2931
 * able to reconstruct the filename for error messages.  We actually
2932
 * pass the filename to operate on through the top node of the list.
2933
 *
2934
 * In case we're using openat(), this top filename will be a basename
2935
 * which should be opened in the directory which has also had its fd
2936
 * passed along.  If we're not using openat() then it will be a full
2937
 * absolute filename.
2938
 */
2939
2940
static gboolean
2941
g_local_file_measure_size_error (GFileMeasureFlags   flags,
2942
                                 gint                saved_errno,
2943
                                 GSList             *name,
2944
                                 GError            **error)
2945
0
{
2946
  /* Only report an error if we were at the toplevel or if the caller
2947
   * requested reporting of all errors.
2948
   */
2949
0
  if ((name->next == NULL) || (flags & G_FILE_MEASURE_REPORT_ANY_ERROR))
2950
0
    {
2951
0
      GString *filename;
2952
0
      GSList *node;
2953
2954
      /* Skip some work if there is no error return */
2955
0
      if (!error)
2956
0
        return FALSE;
2957
2958
0
#ifdef AT_FDCWD
2959
      /* If using openat() we need to rebuild the filename for the message */
2960
0
      filename = g_string_new (name->data);
2961
0
      for (node = name->next; node; node = node->next)
2962
0
        {
2963
0
          gchar *utf8;
2964
2965
0
          g_string_prepend_c (filename, G_DIR_SEPARATOR);
2966
0
          utf8 = g_filename_display_name (node->data);
2967
0
          g_string_prepend (filename, utf8);
2968
0
          g_free (utf8);
2969
0
        }
2970
#else
2971
      {
2972
        gchar *utf8;
2973
2974
        /* Otherwise, we already have it, so just use it. */
2975
        node = name;
2976
        filename = g_string_new (NULL);
2977
        utf8 = g_filename_display_name (node->data);
2978
        g_string_append (filename, utf8);
2979
        g_free (utf8);
2980
      }
2981
#endif
2982
2983
0
      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (saved_errno),
2984
0
                   _("Could not determine the disk usage of %s: %s"),
2985
0
                   filename->str, g_strerror (saved_errno));
2986
2987
0
      g_string_free (filename, TRUE);
2988
2989
0
      return FALSE;
2990
0
    }
2991
2992
0
  else
2993
    /* We're not reporting this error... */
2994
0
    return TRUE;
2995
0
}
2996
2997
typedef struct
2998
{
2999
  GFileMeasureFlags  flags;
3000
  dev_t              contained_on;
3001
  GCancellable      *cancellable;
3002
3003
  GFileMeasureProgressCallback progress_callback;
3004
  gpointer                     progress_data;
3005
3006
  guint64 disk_usage;
3007
  guint64 num_dirs;
3008
  guint64 num_files;
3009
3010
  guint64 last_progress_report;
3011
} MeasureState;
3012
3013
static gboolean
3014
g_local_file_measure_size_of_contents (gint           fd,
3015
                                       GSList        *dir_name,
3016
                                       MeasureState  *state,
3017
                                       GError       **error);
3018
3019
/*
3020
 * _g_stat_is_size_usable:
3021
 * @buf: a #GLocalFileStat.
3022
 *
3023
 * Checks if the file type is such that the `st_size` field of `struct stat` is
3024
 * well-defined by POSIX.
3025
 * (see https://pubs.opengroup.org/onlinepubs/009696799/basedefs/sys/stat.h.html)
3026
 *
3027
 * This behaviour is aligned with `du` from GNU Coreutils 9.2+
3028
 * (see https://lists.gnu.org/archive/html/bug-coreutils/2023-03/msg00007.html)
3029
 * and makes apparent size sums well-defined; formerly, they depended on the
3030
 * implementation, and could differ across filesystems.
3031
 *
3032
 * Returns: %TRUE if the size field is well-defined, %FALSE otherwise.
3033
 **/
3034
inline static gboolean
3035
_g_stat_is_size_usable (const GLocalFileStat *buf)
3036
0
{
3037
#ifndef HAVE_STATX
3038
  /* Memory objects are defined by POSIX, but are not supported by statx nor Windows */
3039
#ifdef S_TYPEISSHM
3040
  if (S_TYPEISSHM (buf))
3041
    return TRUE;
3042
#endif
3043
#ifdef S_TYPEISTMO
3044
  if (S_TYPEISTMO (buf))
3045
    return TRUE;
3046
#endif
3047
#endif
3048
3049
0
  return S_ISREG (_g_stat_mode (buf)) || S_ISLNK (_g_stat_mode (buf));
3050
0
}
3051
3052
static gboolean
3053
g_local_file_measure_size_of_file (gint           parent_fd,
3054
                                   GSList        *name,
3055
                                   MeasureState  *state,
3056
                                   GError       **error)
3057
0
{
3058
0
  GLocalFileStat buf;
3059
3060
0
  if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
3061
0
    return FALSE;
3062
3063
0
#if defined (AT_FDCWD)
3064
0
  if (g_local_file_fstatat (parent_fd, name->data, AT_SYMLINK_NOFOLLOW,
3065
0
                            G_LOCAL_FILE_STAT_FIELD_BASIC_STATS,
3066
0
                            G_LOCAL_FILE_STAT_FIELD_ALL & (~G_LOCAL_FILE_STAT_FIELD_ATIME),
3067
0
                            &buf) != 0)
3068
0
    {
3069
0
      int errsv = errno;
3070
0
      return g_local_file_measure_size_error (state->flags, errsv, name, error);
3071
0
    }
3072
#elif defined (HAVE_LSTAT) || !defined (G_OS_WIN32)
3073
  if (g_lstat (name->data, &buf) != 0)
3074
    {
3075
      int errsv = errno;
3076
      return g_local_file_measure_size_error (state->flags, errsv, name, error);
3077
    }
3078
#else /* !AT_FDCWD && !HAVE_LSTAT && G_OS_WIN32 */
3079
  if (GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (name->data, &buf) != 0)
3080
    {
3081
      int errsv = errno;
3082
      return g_local_file_measure_size_error (state->flags, errsv, name, error);
3083
    }
3084
#endif
3085
3086
0
  if (name->next)
3087
0
    {
3088
      /* If not at the toplevel, check for a device boundary. */
3089
3090
0
      if (state->flags & G_FILE_MEASURE_NO_XDEV)
3091
0
        if (state->contained_on != _g_stat_dev (&buf))
3092
0
          return TRUE;
3093
0
    }
3094
0
  else
3095
0
    {
3096
      /* If, however, this is the toplevel, set the device number so
3097
       * that recursive invocations can compare against it.
3098
       */
3099
0
      state->contained_on = _g_stat_dev (&buf);
3100
0
    }
3101
3102
#if defined (G_OS_WIN32)
3103
  if (~state->flags & G_FILE_MEASURE_APPARENT_SIZE)
3104
    state->disk_usage += buf.allocated_size;
3105
  else
3106
#elif defined (HAVE_STRUCT_STAT_ST_BLOCKS)
3107
0
  if (~state->flags & G_FILE_MEASURE_APPARENT_SIZE)
3108
0
    state->disk_usage += _g_stat_blocks (&buf) * G_GUINT64_CONSTANT (512);
3109
0
  else
3110
0
#endif
3111
0
  if (_g_stat_is_size_usable (&buf))
3112
0
    state->disk_usage += _g_stat_size (&buf);
3113
3114
0
  if (S_ISDIR (_g_stat_mode (&buf)))
3115
0
    state->num_dirs++;
3116
0
  else
3117
0
    state->num_files++;
3118
3119
0
  if (state->progress_callback)
3120
0
    {
3121
      /* We could attempt to do some cleverness here in order to avoid
3122
       * calling clock_gettime() so much, but we're doing stats and opens
3123
       * all over the place already...
3124
       */
3125
0
      if (state->last_progress_report)
3126
0
        {
3127
0
          guint64 now;
3128
3129
0
          now = g_get_monotonic_time ();
3130
3131
0
          if (state->last_progress_report + 200 * G_TIME_SPAN_MILLISECOND < now)
3132
0
            {
3133
0
              (* state->progress_callback) (TRUE,
3134
0
                                            state->disk_usage, state->num_dirs, state->num_files,
3135
0
                                            state->progress_data);
3136
0
              state->last_progress_report = now;
3137
0
            }
3138
0
        }
3139
0
      else
3140
0
        {
3141
          /* We must do an initial report to inform that more reports
3142
           * will be coming.
3143
           */
3144
0
          (* state->progress_callback) (TRUE, 0, 0, 0, state->progress_data);
3145
0
          state->last_progress_report = g_get_monotonic_time ();
3146
0
        }
3147
0
    }
3148
3149
0
  if (S_ISDIR (_g_stat_mode (&buf)))
3150
0
    {
3151
0
      int dir_fd = -1;
3152
0
#ifdef AT_FDCWD
3153
0
      int errsv;
3154
0
#endif
3155
3156
0
      if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
3157
0
        return FALSE;
3158
3159
0
#ifdef AT_FDCWD
3160
0
#ifdef HAVE_OPEN_O_DIRECTORY
3161
0
      dir_fd = openat (parent_fd, name->data, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
3162
#else
3163
      dir_fd = openat (parent_fd, name->data, O_RDONLY | O_CLOEXEC);
3164
#endif
3165
0
      errsv = errno;
3166
0
      if (dir_fd < 0)
3167
0
        return g_local_file_measure_size_error (state->flags, errsv, name, error);
3168
0
#endif
3169
3170
0
      if (!g_local_file_measure_size_of_contents (dir_fd, name, state, error))
3171
0
        return FALSE;
3172
0
    }
3173
3174
0
  return TRUE;
3175
0
}
3176
3177
static gboolean
3178
g_local_file_measure_size_of_contents (gint           fd,
3179
                                       GSList        *dir_name,
3180
                                       MeasureState  *state,
3181
                                       GError       **error)
3182
0
{
3183
0
  gboolean success = TRUE;
3184
0
  const gchar *name;
3185
0
  GDir *dir;
3186
0
  gint saved_errno;
3187
3188
0
#ifdef AT_FDCWD
3189
0
  {
3190
    /* If this fails, we want to preserve the errno from fdopendir() */
3191
0
    DIR *dirp;
3192
0
    dirp = fdopendir (fd);
3193
0
    saved_errno = errno;
3194
0
    dir = dirp ? GLIB_PRIVATE_CALL(g_dir_new_from_dirp) (dirp) : NULL;
3195
0
    g_assert ((dirp == NULL) == (dir == NULL));
3196
0
  }
3197
#else
3198
  dir = GLIB_PRIVATE_CALL(g_dir_open_with_errno) (dir_name->data, 0);
3199
  saved_errno = errno;
3200
#endif
3201
3202
0
  if (dir == NULL)
3203
0
    {
3204
0
#ifdef AT_FDCWD
3205
0
      close (fd);
3206
0
#endif
3207
3208
0
      return g_local_file_measure_size_error (state->flags, saved_errno, dir_name, error);
3209
0
    }
3210
3211
0
  while (success && (name = g_dir_read_name (dir)))
3212
0
    {
3213
0
      GSList node;
3214
3215
0
      node.next = dir_name;
3216
0
#ifdef AT_FDCWD
3217
0
      node.data = (gchar *) name;
3218
#else
3219
      node.data = g_build_filename (dir_name->data, name, NULL);
3220
#endif
3221
3222
0
      success = g_local_file_measure_size_of_file (fd, &node, state, error);
3223
3224
#ifndef AT_FDCWD
3225
      g_free (node.data);
3226
#endif
3227
0
    }
3228
3229
0
  g_dir_close (dir);
3230
3231
0
  return success;
3232
0
}
3233
3234
static gboolean
3235
g_local_file_measure_disk_usage (GFile                         *file,
3236
                                 GFileMeasureFlags              flags,
3237
                                 GCancellable                  *cancellable,
3238
                                 GFileMeasureProgressCallback   progress_callback,
3239
                                 gpointer                       progress_data,
3240
                                 guint64                       *disk_usage,
3241
                                 guint64                       *num_dirs,
3242
                                 guint64                       *num_files,
3243
                                 GError                       **error)
3244
0
{
3245
0
  GLocalFile *local_file = G_LOCAL_FILE (file);
3246
0
  MeasureState state = { 0, };
3247
0
  gint root_fd = -1;
3248
0
  GSList node;
3249
3250
0
  state.flags = flags;
3251
0
  state.cancellable = cancellable;
3252
0
  state.progress_callback = progress_callback;
3253
0
  state.progress_data = progress_data;
3254
3255
0
#ifdef AT_FDCWD
3256
0
  root_fd = AT_FDCWD;
3257
0
#endif
3258
3259
0
  node.data = local_file->filename;
3260
0
  node.next = NULL;
3261
3262
0
  if (!g_local_file_measure_size_of_file (root_fd, &node, &state, error))
3263
0
    return FALSE;
3264
3265
0
  if (disk_usage)
3266
0
    *disk_usage = state.disk_usage;
3267
3268
0
  if (num_dirs)
3269
0
    *num_dirs = state.num_dirs;
3270
3271
0
  if (num_files)
3272
0
    *num_files = state.num_files;
3273
3274
0
  return TRUE;
3275
0
}
3276
3277
static void
3278
g_local_file_file_iface_init (GFileIface *iface)
3279
0
{
3280
0
  iface->dup = g_local_file_dup;
3281
0
  iface->hash = g_local_file_hash;
3282
0
  iface->equal = g_local_file_equal;
3283
0
  iface->is_native = g_local_file_is_native;
3284
0
  iface->has_uri_scheme = g_local_file_has_uri_scheme;
3285
0
  iface->get_uri_scheme = g_local_file_get_uri_scheme;
3286
0
  iface->get_basename = g_local_file_get_basename;
3287
0
  iface->get_path = g_local_file_get_path;
3288
0
  iface->get_uri = g_local_file_get_uri;
3289
0
  iface->get_parse_name = g_local_file_get_parse_name;
3290
0
  iface->get_parent = g_local_file_get_parent;
3291
0
  iface->prefix_matches = g_local_file_prefix_matches;
3292
0
  iface->get_relative_path = g_local_file_get_relative_path;
3293
0
  iface->resolve_relative_path = g_local_file_resolve_relative_path;
3294
0
  iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
3295
0
  iface->set_display_name = g_local_file_set_display_name;
3296
0
  iface->enumerate_children = g_local_file_enumerate_children;
3297
0
  iface->query_info = g_local_file_query_info;
3298
0
  iface->query_filesystem_info = g_local_file_query_filesystem_info;
3299
0
  iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
3300
0
  iface->query_settable_attributes = g_local_file_query_settable_attributes;
3301
0
  iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
3302
0
  iface->set_attribute = g_local_file_set_attribute;
3303
0
  iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
3304
0
  iface->read_fn = g_local_file_read;
3305
0
  iface->append_to = g_local_file_append_to;
3306
0
  iface->create = g_local_file_create;
3307
0
  iface->replace = g_local_file_replace;
3308
0
  iface->open_readwrite = g_local_file_open_readwrite;
3309
0
  iface->create_readwrite = g_local_file_create_readwrite;
3310
0
  iface->replace_readwrite = g_local_file_replace_readwrite;
3311
0
  iface->delete_file = g_local_file_delete;
3312
0
  iface->trash = g_local_file_trash;
3313
0
  iface->make_directory = g_local_file_make_directory;
3314
0
#ifdef HAVE_SYMLINK
3315
0
  iface->make_symbolic_link = g_local_file_make_symbolic_link;
3316
0
#endif
3317
0
  iface->move = g_local_file_move;
3318
0
  iface->monitor_dir = g_local_file_monitor_dir;
3319
0
  iface->monitor_file = g_local_file_monitor_file;
3320
0
  iface->measure_disk_usage = g_local_file_measure_disk_usage;
3321
0
#if defined(HAVE_FACCESSAT) && !defined(__FreeBSD__) && !defined(__ANDROID__) && \
3322
0
    !defined(__OpenBSD__) && !defined(__sun__)
3323
0
  iface->query_exists = g_local_file_query_exists;
3324
0
#endif
3325
3326
0
  iface->supports_thread_contexts = TRUE;
3327
0
}