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