Coverage Report

Created: 2025-08-08 06:10

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