Coverage Report

Created: 2026-07-11 07:00

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