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