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