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