1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
23 #include <sys/types.h>
34 #include <sys/statfs.h>
36 #if HAVE_SYS_STATVFS_H
37 #include <sys/statvfs.h>
41 #elif HAVE_SYS_MOUNT_H
43 #include <sys/param.h>
45 #include <sys/mount.h>
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"
63 #include <glib/gstdio.h>
64 #include <glib/gstdioprivate.h>
67 #include "glib-unix.h"
68 #include "gportalsupport.h"
69 #include "gtrashportal.h"
72 #include "glib-private.h"
79 #ifndef FILE_READ_ONLY_VOLUME
80 #define FILE_READ_ONLY_VOLUME 0x00080000
84 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
87 #define S_ISLNK(m) (0)
96 static void g_local_file_file_iface_init (GFileIface *iface);
98 static GFileAttributeInfoList *local_writable_attributes = NULL;
99 static /* GFileAttributeInfoList * */ gsize local_writable_namespaces = 0;
103 GObject parent_instance;
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))
113 static char *find_mountpoint_for (const char *file, dev_t dev, gboolean resolve_basename_symlink);
116 static gboolean is_remote_fs_type (const gchar *fsname);
120 g_local_file_finalize (GObject *object)
124 local = G_LOCAL_FILE (object);
126 g_free (local->filename);
128 G_OBJECT_CLASS (g_local_file_parent_class)->finalize (object);
132 g_local_file_class_init (GLocalFileClass *klass)
134 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
135 GFileAttributeInfoList *list;
137 gobject_class->finalize = g_local_file_finalize;
139 /* Set up attribute lists */
141 /* Writable attributes: */
143 list = g_file_attribute_info_list_new ();
145 g_file_attribute_info_list_add (list,
146 G_FILE_ATTRIBUTE_UNIX_MODE,
147 G_FILE_ATTRIBUTE_TYPE_UINT32,
148 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
149 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
152 g_file_attribute_info_list_add (list,
153 G_FILE_ATTRIBUTE_UNIX_UID,
154 G_FILE_ATTRIBUTE_TYPE_UINT32,
155 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
156 g_file_attribute_info_list_add (list,
157 G_FILE_ATTRIBUTE_UNIX_GID,
158 G_FILE_ATTRIBUTE_TYPE_UINT32,
159 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
163 g_file_attribute_info_list_add (list,
164 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
165 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
170 g_file_attribute_info_list_add (list,
171 G_FILE_ATTRIBUTE_TIME_MODIFIED,
172 G_FILE_ATTRIBUTE_TYPE_UINT64,
173 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
174 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
175 g_file_attribute_info_list_add (list,
176 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
177 G_FILE_ATTRIBUTE_TYPE_UINT32,
178 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
179 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.
183 g_file_attribute_info_list_add (list,
184 G_FILE_ATTRIBUTE_TIME_ACCESS,
185 G_FILE_ATTRIBUTE_TYPE_UINT64,
186 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
187 g_file_attribute_info_list_add (list,
188 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
189 G_FILE_ATTRIBUTE_TYPE_UINT32,
190 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
193 local_writable_attributes = list;
197 g_local_file_init (GLocalFile *local)
202 _g_local_file_get_filename (GLocalFile *file)
204 return file->filename;
208 _g_local_file_new (const char *filename)
212 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
213 local->filename = g_canonicalize_filename (filename, NULL);
215 return G_FILE (local);
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
223 * Creates a #GFile from @dirname and @basename.
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().
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().
236 g_local_file_new_from_dirname_and_basename (const gchar *dirname,
237 const gchar *basename)
241 g_return_val_if_fail (dirname != NULL, NULL);
242 g_return_val_if_fail (basename && basename[0] && !strchr (basename, '/'), NULL);
244 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
245 local->filename = g_build_filename (dirname, basename, NULL);
247 return G_FILE (local);
251 g_local_file_is_native (GFile *file)
257 g_local_file_has_uri_scheme (GFile *file,
258 const char *uri_scheme)
260 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
264 g_local_file_get_uri_scheme (GFile *file)
266 return g_strdup ("file");
270 g_local_file_get_basename (GFile *file)
272 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
276 g_local_file_get_path (GFile *file)
278 return g_strdup (G_LOCAL_FILE (file)->filename);
282 g_local_file_get_uri (GFile *file)
284 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
288 get_filename_charset (const gchar **filename_charset)
290 const gchar **charsets;
293 is_utf8 = g_get_filename_charsets (&charsets);
295 if (filename_charset)
296 *filename_charset = charsets[0];
302 name_is_valid_for_display (const char *string,
303 gboolean is_valid_utf8)
307 if (!is_valid_utf8 &&
308 !g_utf8_validate (string, -1, NULL))
311 while ((c = *string++) != 0)
313 if (g_ascii_iscntrl (c))
321 g_local_file_get_parse_name (GFile *file)
323 const char *filename;
325 const gchar *charset;
327 char *roundtripped_filename;
328 gboolean free_utf8_filename;
329 gboolean is_valid_utf8;
332 filename = G_LOCAL_FILE (file)->filename;
333 if (get_filename_charset (&charset))
335 utf8_filename = (char *)filename;
336 free_utf8_filename = FALSE;
337 is_valid_utf8 = FALSE; /* Can't guarantee this */
341 utf8_filename = g_convert (filename, -1,
342 "UTF-8", charset, NULL, NULL, NULL);
343 free_utf8_filename = TRUE;
344 is_valid_utf8 = TRUE;
346 if (utf8_filename != NULL)
348 /* Make sure we can roundtrip: */
349 roundtripped_filename = g_convert (utf8_filename, -1,
350 charset, "UTF-8", NULL, NULL, NULL);
352 if (roundtripped_filename == NULL ||
353 strcmp (filename, roundtripped_filename) != 0)
355 g_free (utf8_filename);
356 utf8_filename = NULL;
359 g_free (roundtripped_filename);
363 if (utf8_filename != NULL &&
364 name_is_valid_for_display (utf8_filename, is_valid_utf8))
366 if (free_utf8_filename)
367 parse_name = utf8_filename;
369 parse_name = g_strdup (utf8_filename);
374 char *dup_filename, *p, *backslash;
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).
380 dup_filename = g_strdup (filename);
381 filename = p = dup_filename;
383 while ((backslash = strchr (p, '\\')) != NULL)
390 escaped_path = g_uri_escape_string (filename,
391 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
393 parse_name = g_strconcat ("file://",
394 (*escaped_path != '/') ? "/" : "",
398 g_free (escaped_path);
400 g_free (dup_filename);
402 if (free_utf8_filename)
403 g_free (utf8_filename);
410 g_local_file_get_parent (GFile *file)
412 GLocalFile *local = G_LOCAL_FILE (file);
413 const char *non_root;
417 /* Check for root; local->filename is guaranteed to be absolute, so
418 * g_path_skip_root() should never return NULL. */
419 non_root = g_path_skip_root (local->filename);
420 g_assert (non_root != NULL);
425 dirname = g_path_get_dirname (local->filename);
426 parent = _g_local_file_new (dirname);
432 g_local_file_dup (GFile *file)
434 GLocalFile *local = G_LOCAL_FILE (file);
436 return _g_local_file_new (local->filename);
440 g_local_file_hash (GFile *file)
442 GLocalFile *local = G_LOCAL_FILE (file);
444 return g_str_hash (local->filename);
448 g_local_file_equal (GFile *file1,
451 GLocalFile *local1 = G_LOCAL_FILE (file1);
452 GLocalFile *local2 = G_LOCAL_FILE (file2);
454 return g_str_equal (local1->filename, local2->filename);
458 match_prefix (const char *path,
463 prefix_len = strlen (prefix);
464 if (strncmp (path, prefix, prefix_len) != 0)
467 /* Handle the case where prefix is the root, so that
468 * the IS_DIR_SEPRARATOR check below works */
469 if (prefix_len > 0 &&
470 G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
473 return path + prefix_len;
477 g_local_file_prefix_matches (GFile *parent,
480 GLocalFile *parent_local = G_LOCAL_FILE (parent);
481 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
482 const char *remainder;
484 remainder = match_prefix (descendant_local->filename, parent_local->filename);
485 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
491 g_local_file_get_relative_path (GFile *parent,
494 GLocalFile *parent_local = G_LOCAL_FILE (parent);
495 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
496 const char *remainder;
498 remainder = match_prefix (descendant_local->filename, parent_local->filename);
500 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
501 return g_strdup (remainder + 1);
506 g_local_file_resolve_relative_path (GFile *file,
507 const char *relative_path)
509 GLocalFile *local = G_LOCAL_FILE (file);
513 if (g_path_is_absolute (relative_path))
514 return _g_local_file_new (relative_path);
516 filename = g_build_filename (local->filename, relative_path, NULL);
517 child = _g_local_file_new (filename);
523 static GFileEnumerator *
524 g_local_file_enumerate_children (GFile *file,
525 const char *attributes,
526 GFileQueryInfoFlags flags,
527 GCancellable *cancellable,
530 GLocalFile *local = G_LOCAL_FILE (file);
531 return _g_local_file_enumerator_new (local,
537 g_local_file_get_child_for_display_name (GFile *file,
538 const char *display_name,
544 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
545 if (basename == NULL)
547 g_set_error (error, G_IO_ERROR,
548 G_IO_ERROR_INVALID_FILENAME,
549 _("Invalid filename %s"), display_name);
553 new_file = g_file_get_child (file, basename);
559 #if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
561 get_fs_type (long f_type)
563 /* filesystem ids taken from linux manpage */
581 return "binfmt_misc";
585 return "btrfs_test_fs";
740 G_LOCK_DEFINE_STATIC(mount_info_hash);
741 static GHashTable *mount_info_hash = NULL;
742 static guint64 mount_info_hash_cache_time = 0;
745 MOUNT_INFO_READONLY = 1<<0
749 device_equal (gconstpointer v1,
752 return *(dev_t *)v1 == *(dev_t *)v2;
756 device_hash (gconstpointer v)
758 return (guint) *(dev_t *)v;
762 get_mount_info (GFileInfo *fs_info,
764 GFileAttributeMatcher *matcher)
768 gpointer info_as_ptr;
772 GUnixMountEntry *mount;
775 if (g_lstat (path, &buf) != 0)
778 G_LOCK (mount_info_hash);
780 if (mount_info_hash == NULL)
781 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
785 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
786 g_hash_table_remove_all (mount_info_hash);
788 got_info = g_hash_table_lookup_extended (mount_info_hash,
793 G_UNLOCK (mount_info_hash);
795 mount_info = GPOINTER_TO_UINT (info_as_ptr);
801 mountpoint = find_mountpoint_for (path, buf.st_dev, FALSE);
802 if (mountpoint == NULL)
803 mountpoint = g_strdup ("/");
805 mount = g_unix_mount_at (mountpoint, &cache_time);
808 if (g_unix_mount_is_readonly (mount))
809 mount_info |= MOUNT_INFO_READONLY;
811 g_unix_mount_free (mount);
816 dev = g_new0 (dev_t, 1);
819 G_LOCK (mount_info_hash);
820 mount_info_hash_cache_time = cache_time;
821 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
822 G_UNLOCK (mount_info_hash);
825 if (mount_info & MOUNT_INFO_READONLY)
826 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
834 get_volume_for_path (const char *path)
840 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
841 result = g_new (wchar_t, MAX_PATH);
843 if (!GetVolumePathNameW (wpath, result, MAX_PATH))
845 char *msg = g_win32_error_message (GetLastError ());
846 g_critical ("GetVolumePathName failed: %s", msg);
853 len = wcslen (result);
854 if (len > 0 && result[len-1] != L'\\')
856 result = g_renew (wchar_t, result, len + 2);
866 find_mountpoint_for (const char *file, dev_t dev, gboolean resolve_basename_symlink)
871 wpath = get_volume_for_path (file);
875 utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
882 get_filesystem_readonly (GFileInfo *info,
887 rootdir = get_volume_for_path (path);
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);
900 #endif /* G_OS_WIN32 */
902 #pragma GCC diagnostic push
903 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
905 g_set_io_error (GError **error,
910 GLocalFile *local = G_LOCAL_FILE (file);
913 display_name = g_filename_display_name (local->filename);
914 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
915 msg, display_name, g_strerror (errsv));
916 g_free (display_name);
918 #pragma GCC diagnostic pop
921 g_local_file_query_filesystem_info (GFile *file,
922 const char *attributes,
923 GCancellable *cancellable,
926 GLocalFile *local = G_LOCAL_FILE (file);
928 int statfs_result = 0;
934 struct statfs statfs_buffer;
935 #elif defined(USE_STATVFS)
937 struct statvfs statfs_buffer;
938 #endif /* USE_STATFS */
939 #endif /* G_OS_WIN32 */
940 GFileAttributeMatcher *attribute_matcher;
947 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 block_size = statfs_buffer.f_bsize;
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 #if defined(__linux__)
961 if (statfs_result == 0 &&
962 statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0 &&
963 (/* linux/ncp_fs.h: NCP_SUPER_MAGIC == 0x564c */
964 statfs_buffer.f_type == 0x564c ||
965 /* man statfs: FUSE_SUPER_MAGIC == 0x65735546 */
966 statfs_buffer.f_type == 0x65735546))
968 #endif /* __linux__ */
970 #elif defined(USE_STATVFS)
971 statfs_result = statvfs (local->filename, &statfs_buffer);
972 block_size = statfs_buffer.f_frsize;
973 #endif /* USE_STATFS */
975 if (statfs_result == -1)
979 g_set_io_error (error,
980 _("Error getting filesystem info for %s: %s"),
985 info = g_file_info_new ();
987 attribute_matcher = g_file_attribute_matcher_new (attributes);
990 g_file_attribute_matcher_matches (attribute_matcher,
991 G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
994 gchar *localdir = g_path_get_dirname (local->filename);
995 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
999 if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
1000 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
1003 #if defined(USE_STATFS) || defined(USE_STATVFS)
1004 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
1009 g_file_attribute_matcher_matches (attribute_matcher,
1010 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
1013 gchar *localdir = g_path_get_dirname (local->filename);
1014 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1018 if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
1019 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, (guint64)li.QuadPart);
1022 #if defined(USE_STATFS) || defined(USE_STATVFS)
1023 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
1025 #endif /* G_OS_WIN32 */
1029 g_file_attribute_matcher_matches (attribute_matcher,
1030 G_FILE_ATTRIBUTE_FILESYSTEM_USED))
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;
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);
1043 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, block_size * (statfs_buffer.f_blocks - statfs_buffer.f_bfree));
1044 #endif /* G_OS_WIN32 */
1049 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1050 fstype = statfs_buffer.f_fstypename;
1052 fstype = get_fs_type (statfs_buffer.f_type);
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;
1063 #endif /* USE_STATFS */
1066 g_file_attribute_matcher_matches (attribute_matcher,
1067 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1068 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1069 #endif /* G_OS_WIN32 */
1071 if (g_file_attribute_matcher_matches (attribute_matcher,
1072 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
1075 get_filesystem_readonly (info, local->filename);
1077 get_mount_info (info, local->filename, attribute_matcher);
1078 #endif /* G_OS_WIN32 */
1082 if (g_file_attribute_matcher_matches (attribute_matcher,
1083 G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE))
1084 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE,
1085 is_remote_fs_type (fstype));
1088 g_file_attribute_matcher_unref (attribute_matcher);
1094 g_local_file_find_enclosing_mount (GFile *file,
1095 GCancellable *cancellable,
1098 GLocalFile *local = G_LOCAL_FILE (file);
1103 if (g_lstat (local->filename, &buf) != 0)
1106 mountpoint = find_mountpoint_for (local->filename, buf.st_dev, FALSE);
1107 if (mountpoint == NULL)
1110 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1111 g_free (mountpoint);
1116 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
1121 _("Containing mount for file %s not found"),
1128 g_local_file_set_display_name (GFile *file,
1129 const char *display_name,
1130 GCancellable *cancellable,
1133 GLocalFile *local, *new_local;
1134 GFile *new_file, *parent;
1140 parent = g_file_get_parent (file);
1143 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1144 _("Can’t rename root directory"));
1148 new_file = g_file_get_child_for_display_name (parent, display_name, error);
1149 g_object_unref (parent);
1151 if (new_file == NULL)
1153 local = G_LOCAL_FILE (file);
1154 new_local = G_LOCAL_FILE (new_file);
1156 if (g_lstat (new_local->filename, &statbuf) == -1)
1160 if (errsv != ENOENT)
1162 g_set_io_error (error, _("Error renaming file %s: %s"), new_file, errsv);
1168 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1169 _("Can’t rename file, filename already exists"));
1173 if (g_rename (local->filename, new_local->filename) == -1)
1177 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
1181 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_FILENAME,
1182 _("Invalid filename"));
1184 g_set_io_error (error,
1185 _("Error renaming file %s: %s"),
1187 g_object_unref (new_file);
1191 vfs = g_vfs_get_default ();
1192 class = G_VFS_GET_CLASS (vfs);
1193 if (class->local_file_moved)
1194 class->local_file_moved (vfs, local->filename, new_local->filename);
1200 g_local_file_query_info (GFile *file,
1201 const char *attributes,
1202 GFileQueryInfoFlags flags,
1203 GCancellable *cancellable,
1206 GLocalFile *local = G_LOCAL_FILE (file);
1208 GFileAttributeMatcher *matcher;
1209 char *basename, *dirname;
1210 GLocalParentFileInfo parent_info;
1212 matcher = g_file_attribute_matcher_new (attributes);
1214 basename = g_path_get_basename (local->filename);
1216 dirname = g_path_get_dirname (local->filename);
1217 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1220 info = _g_local_file_info_get (basename, local->filename,
1221 matcher, flags, &parent_info,
1225 _g_local_file_info_free_parent_info (&parent_info);
1228 g_file_attribute_matcher_unref (matcher);
1233 static GFileAttributeInfoList *
1234 g_local_file_query_settable_attributes (GFile *file,
1235 GCancellable *cancellable,
1238 return g_file_attribute_info_list_ref (local_writable_attributes);
1241 static GFileAttributeInfoList *
1242 g_local_file_query_writable_namespaces (GFile *file,
1243 GCancellable *cancellable,
1246 GFileAttributeInfoList *list;
1250 if (g_once_init_enter (&local_writable_namespaces))
1252 /* Writable namespaces: */
1254 list = g_file_attribute_info_list_new ();
1257 g_file_attribute_info_list_add (list,
1259 G_FILE_ATTRIBUTE_TYPE_STRING,
1260 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
1261 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1262 g_file_attribute_info_list_add (list,
1264 G_FILE_ATTRIBUTE_TYPE_STRING,
1265 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1268 vfs = g_vfs_get_default ();
1269 class = G_VFS_GET_CLASS (vfs);
1270 if (class->add_writable_namespaces)
1271 class->add_writable_namespaces (vfs, list);
1273 g_once_init_leave (&local_writable_namespaces, (gsize)list);
1275 list = (GFileAttributeInfoList *)local_writable_namespaces;
1277 return g_file_attribute_info_list_ref (list);
1281 g_local_file_set_attribute (GFile *file,
1282 const char *attribute,
1283 GFileAttributeType type,
1285 GFileQueryInfoFlags flags,
1286 GCancellable *cancellable,
1289 GLocalFile *local = G_LOCAL_FILE (file);
1291 return _g_local_file_info_set_attribute (local->filename,
1301 g_local_file_set_attributes_from_info (GFile *file,
1303 GFileQueryInfoFlags flags,
1304 GCancellable *cancellable,
1307 GLocalFile *local = G_LOCAL_FILE (file);
1308 int res, chained_res;
1309 GFileIface *default_iface;
1311 res = _g_local_file_info_set_attributes (local->filename,
1317 error = NULL; /* Don't write over error if further errors */
1319 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1321 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1323 return res && chained_res;
1326 static GFileInputStream *
1327 g_local_file_read (GFile *file,
1328 GCancellable *cancellable,
1331 GLocalFile *local = G_LOCAL_FILE (file);
1335 fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1341 if (errsv == EACCES)
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))
1349 g_set_io_error (error,
1350 _("Error opening file %s: %s"),
1355 ret = g_local_file_fstat (fd, G_LOCAL_FILE_STAT_FIELD_TYPE, G_LOCAL_FILE_STAT_FIELD_ALL, &buf);
1357 if (ret == 0 && S_ISDIR (_g_stat_mode (&buf)))
1359 (void) g_close (fd, NULL);
1360 g_set_io_error (error,
1361 _("Error opening file %s: %s"),
1366 return _g_local_file_input_stream_new (fd);
1369 static GFileOutputStream *
1370 g_local_file_append_to (GFile *file,
1371 GFileCreateFlags flags,
1372 GCancellable *cancellable,
1375 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1376 flags, cancellable, error);
1379 static GFileOutputStream *
1380 g_local_file_create (GFile *file,
1381 GFileCreateFlags flags,
1382 GCancellable *cancellable,
1385 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1387 cancellable, error);
1390 static GFileOutputStream *
1391 g_local_file_replace (GFile *file,
1393 gboolean make_backup,
1394 GFileCreateFlags flags,
1395 GCancellable *cancellable,
1398 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1400 etag, make_backup, flags, NULL,
1401 cancellable, error);
1404 static GFileIOStream *
1405 g_local_file_open_readwrite (GFile *file,
1406 GCancellable *cancellable,
1409 GFileOutputStream *output;
1412 output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
1414 cancellable, error);
1418 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1419 g_object_unref (output);
1423 static GFileIOStream *
1424 g_local_file_create_readwrite (GFile *file,
1425 GFileCreateFlags flags,
1426 GCancellable *cancellable,
1429 GFileOutputStream *output;
1432 output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1434 cancellable, error);
1438 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1439 g_object_unref (output);
1443 static GFileIOStream *
1444 g_local_file_replace_readwrite (GFile *file,
1446 gboolean make_backup,
1447 GFileCreateFlags flags,
1448 GCancellable *cancellable,
1451 GFileOutputStream *output;
1454 output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1456 etag, make_backup, flags, NULL,
1457 cancellable, error);
1461 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1462 g_object_unref (output);
1467 g_local_file_delete (GFile *file,
1468 GCancellable *cancellable,
1471 GLocalFile *local = G_LOCAL_FILE (file);
1475 if (g_remove (local->filename) == -1)
1479 /* Posix allows EEXIST too, but the clearer error
1480 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1482 if (errsv == EEXIST)
1485 g_set_io_error (error,
1486 _("Error removing file %s: %s"),
1491 vfs = g_vfs_get_default ();
1492 class = G_VFS_GET_CLASS (vfs);
1493 if (class->local_file_removed)
1494 class->local_file_removed (vfs, local->filename);
1502 strip_trailing_slashes (const char *path)
1507 path_copy = g_strdup (path);
1508 len = strlen (path_copy);
1509 while (len > 1 && path_copy[len-1] == '/')
1510 path_copy[--len] = 0;
1516 expand_symlink (const char *link)
1518 char *resolved, *canonical, *parent, *link2;
1519 char symlink_value[4096];
1527 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1530 return g_strdup (link);
1531 symlink_value[res] = 0;
1534 if (g_path_is_absolute (symlink_value))
1535 return g_canonicalize_filename (symlink_value, NULL);
1538 link2 = strip_trailing_slashes (link);
1539 parent = g_path_get_dirname (link2);
1542 resolved = g_build_filename (parent, symlink_value, NULL);
1545 canonical = g_canonicalize_filename (resolved, NULL);
1554 expand_symlinks (const char *path,
1558 GStatBuf target_stat;
1561 target = g_strdup (path);
1566 if (g_lstat (target, &target_stat) != 0)
1572 if (S_ISLNK (target_stat.st_mode))
1575 target = expand_symlink (target);
1582 if (num_recursions > MAXSYMLINKS)
1584 /* 40 is used in kernel sources currently:
1585 * https://github.com/torvalds/linux/include/linux/namei.h
1587 if (num_recursions > 40)
1594 while (S_ISLNK (target_stat.st_mode));
1597 *dev = target_stat.st_dev;
1603 get_parent (const char *path,
1609 path_copy = strip_trailing_slashes (path);
1611 parent = g_path_get_dirname (path_copy);
1612 if (strcmp (parent, ".") == 0)
1620 res = expand_symlinks (parent, parent_dev);
1627 expand_all_symlinks (const char *path)
1629 char *parent, *parent_expanded;
1630 char *basename, *res;
1633 parent = get_parent (path, &parent_dev);
1637 if (g_strcmp0 (parent, "/") != 0)
1639 parent_expanded = expand_all_symlinks (parent);
1640 basename = g_path_get_basename (path);
1641 res = g_build_filename (parent_expanded, basename, NULL);
1643 g_free (parent_expanded);
1646 res = g_strdup (path);
1654 find_mountpoint_for (const char *file,
1656 gboolean resolve_basename_symlink)
1659 dev_t dir_dev, parent_dev;
1661 if (resolve_basename_symlink)
1663 dir = expand_symlinks (file, NULL);
1668 dir = g_strdup (file);
1672 while (g_strcmp0 (dir, "/") != 0)
1674 parent = get_parent (dir, &parent_dev);
1681 if (parent_dev != dir_dev)
1695 _g_local_file_find_topdir_for (const char *file)
1698 char *mountpoint = NULL;
1701 dir = get_parent (file, &dir_dev);
1705 mountpoint = find_mountpoint_for (dir, dir_dev, TRUE);
1712 get_unique_filename (const char *basename,
1718 return g_strdup (basename);
1720 dot = strchr (basename, '.');
1722 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1724 return g_strdup_printf ("%s.%d", basename, id);
1728 path_has_prefix (const char *path,
1736 prefix_len = strlen (prefix);
1738 if (strncmp (path, prefix, prefix_len) == 0 &&
1739 (prefix_len == 0 || /* empty prefix always matches */
1740 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1741 path[prefix_len] == 0 ||
1742 path[prefix_len] == '/'))
1749 try_make_relative (const char *path,
1752 char *path2, *base2;
1755 path2 = expand_all_symlinks (path);
1756 base2 = expand_all_symlinks (base);
1759 if (path2 != NULL && base2 != NULL && path_has_prefix (path2, base2))
1761 relative = path2 + strlen (base2);
1762 while (*relative == '/')
1764 relative = g_strdup (relative);
1772 /* Failed, use abs path */
1773 return g_strdup (path);
1777 ignore_trash_mount (GUnixMountEntry *mount)
1779 GUnixMountPoint *mount_point = NULL;
1780 const gchar *mount_options;
1781 gboolean retval = TRUE;
1783 if (g_unix_mount_is_system_internal (mount))
1786 mount_options = g_unix_mount_get_options (mount);
1787 if (mount_options == NULL)
1789 mount_point = g_unix_mount_point_at (g_unix_mount_get_mount_path (mount),
1791 if (mount_point != NULL)
1792 mount_options = g_unix_mount_point_get_options (mount_point);
1795 if (mount_options == NULL ||
1796 strstr (mount_options, "x-gvfs-notrash") == NULL)
1799 g_clear_pointer (&mount_point, g_unix_mount_point_free);
1805 ignore_trash_path (const gchar *topdir)
1807 GUnixMountEntry *mount;
1808 gboolean retval = TRUE;
1810 mount = g_unix_mount_at (topdir, NULL);
1814 retval = ignore_trash_mount (mount);
1817 g_clear_pointer (&mount, g_unix_mount_free);
1823 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1825 static gsize home_dev_set = 0;
1826 static dev_t home_dev;
1827 char *topdir, *globaldir, *trashdir, *tmpname;
1830 GStatBuf global_stat, trash_stat;
1833 if (g_once_init_enter (&home_dev_set))
1837 g_stat (g_get_home_dir (), &home_stat);
1838 home_dev = home_stat.st_dev;
1839 g_once_init_leave (&home_dev_set, 1);
1842 /* Assume we can trash to the home */
1843 if (dir_dev == home_dev)
1846 topdir = find_mountpoint_for (dirname, dir_dev, TRUE);
1850 if (ignore_trash_path (topdir))
1857 globaldir = g_build_filename (topdir, ".Trash", NULL);
1858 if (g_lstat (globaldir, &global_stat) == 0 &&
1859 S_ISDIR (global_stat.st_mode) &&
1860 (global_stat.st_mode & S_ISVTX) != 0)
1862 /* got a toplevel sysadmin created dir, assume we
1863 * can trash to it (we should be able to create a dir)
1864 * This fails for the FAT case where the ownership of
1865 * that dir would be wrong though..
1873 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1875 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1877 tmpname = g_strdup_printf (".Trash-%s", uid_str);
1878 trashdir = g_build_filename (topdir, tmpname, NULL);
1881 if (g_lstat (trashdir, &trash_stat) == 0)
1885 return S_ISDIR (trash_stat.st_mode) &&
1886 trash_stat.st_uid == uid;
1890 /* User specific trash didn't exist, can we create it? */
1891 res = g_access (topdir, W_OK) == 0;
1899 _g_local_file_is_lost_found_dir (const char *path, dev_t path_dev)
1901 gboolean ret = FALSE;
1902 gchar *mount_dir = NULL;
1903 size_t mount_dir_len;
1906 if (!g_str_has_suffix (path, "/lost+found"))
1909 mount_dir = find_mountpoint_for (path, path_dev, FALSE);
1910 if (mount_dir == NULL)
1913 mount_dir_len = strlen (mount_dir);
1914 /* We special-case rootfs ('/') since it's the only case where
1915 * mount_dir ends in '/'
1917 if (mount_dir_len == 1)
1919 if (mount_dir_len + strlen ("/lost+found") != strlen (path))
1922 if (g_lstat (path, &statbuf) != 0)
1925 if (!(S_ISDIR (statbuf.st_mode) &&
1926 statbuf.st_uid == 0 &&
1927 statbuf.st_gid == 0))
1939 g_local_file_trash (GFile *file,
1940 GCancellable *cancellable,
1943 GLocalFile *local = G_LOCAL_FILE (file);
1944 GStatBuf file_stat, home_stat;
1945 const char *homedir;
1946 char *trashdir, *topdir, *infodir, *filesdir;
1947 char *basename, *trashname, *trashfile, *infoname, *infofile;
1948 char *original_name, *original_name_escaped;
1952 gboolean is_homedir_trash;
1953 char *delete_time = NULL;
1955 GStatBuf trash_stat, global_stat;
1956 char *dirname, *globaldir;
1961 if (glib_should_use_portal ())
1962 return g_trash_portal_trash_file (file, error);
1964 if (g_lstat (local->filename, &file_stat) != 0)
1968 g_set_io_error (error,
1969 _("Error trashing file %s: %s"),
1974 homedir = g_get_home_dir ();
1975 g_stat (homedir, &home_stat);
1977 is_homedir_trash = FALSE;
1980 /* On overlayfs, a file's st_dev will be different to the home directory's.
1981 * We still want to create our trash directory under the home directory, so
1982 * instead we should stat the directory that the file we're deleting is in as
1983 * this will have the same st_dev.
1985 if (!S_ISDIR (file_stat.st_mode))
1987 path = g_path_get_dirname (local->filename);
1988 /* If the parent is a symlink to a different device then it might have
1989 * st_dev equal to the home directory's, in which case we will end up
1990 * trying to rename across a filesystem boundary, which doesn't work. So
1991 * we use g_stat here instead of g_lstat, to know where the symlink
1993 g_stat (path, &file_stat);
1997 if (file_stat.st_dev == home_stat.st_dev)
1999 is_homedir_trash = TRUE;
2001 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
2002 if (g_mkdir_with_parents (trashdir, 0700) < 0)
2007 display_name = g_filename_display_name (trashdir);
2008 g_set_error (error, G_IO_ERROR,
2009 g_io_error_from_errno (errsv),
2010 _("Unable to create trash dir %s: %s"),
2011 display_name, g_strerror (errsv));
2012 g_free (display_name);
2016 topdir = g_strdup (g_get_user_data_dir ());
2024 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
2026 topdir = _g_local_file_find_topdir_for (local->filename);
2029 g_set_io_error (error,
2030 _("Unable to find toplevel directory to trash %s"),
2035 if (ignore_trash_path (topdir))
2037 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2038 _("Trashing on system internal mounts is not supported"));
2044 /* Try looking for global trash dir $topdir/.Trash/$uid */
2045 globaldir = g_build_filename (topdir, ".Trash", NULL);
2046 if (g_lstat (globaldir, &global_stat) == 0 &&
2047 S_ISDIR (global_stat.st_mode) &&
2048 (global_stat.st_mode & S_ISVTX) != 0)
2050 trashdir = g_build_filename (globaldir, uid_str, NULL);
2052 if (g_lstat (trashdir, &trash_stat) == 0)
2054 if (!S_ISDIR (trash_stat.st_mode) ||
2055 trash_stat.st_uid != uid)
2057 /* Not a directory or not owned by user, ignore */
2062 else if (g_mkdir (trashdir, 0700) == -1)
2070 if (trashdir == NULL)
2072 gboolean tried_create;
2074 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
2075 dirname = g_strdup_printf (".Trash-%s", uid_str);
2076 trashdir = g_build_filename (topdir, dirname, NULL);
2079 tried_create = FALSE;
2082 if (g_lstat (trashdir, &trash_stat) == 0)
2084 if (!S_ISDIR (trash_stat.st_mode) ||
2085 trash_stat.st_uid != uid)
2087 /* Remove the failed directory */
2089 g_remove (trashdir);
2091 /* Not a directory or not owned by user, ignore */
2098 if (!tried_create &&
2099 g_mkdir (trashdir, 0700) != -1)
2101 /* Ensure that the created dir has the right uid etc.
2102 This might fail on e.g. a FAT dir */
2103 tried_create = TRUE;
2114 if (trashdir == NULL)
2117 g_set_io_error (error,
2118 _("Unable to find or create trash directory for %s"),
2119 file, G_IO_ERROR_NOT_SUPPORTED);
2124 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
2126 infodir = g_build_filename (trashdir, "info", NULL);
2127 filesdir = g_build_filename (trashdir, "files", NULL);
2130 /* Make sure we have the subdirectories */
2131 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
2132 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
2137 g_set_io_error (error,
2138 _("Unable to find or create trash directory for %s"),
2139 file, G_IO_ERROR_NOT_SUPPORTED);
2143 basename = g_path_get_basename (local->filename);
2151 trashname = get_unique_filename (basename, i++);
2152 infoname = g_strconcat (trashname, ".trashinfo", NULL);
2153 infofile = g_build_filename (infodir, infoname, NULL);
2156 fd = g_open (infofile, O_CREAT | O_EXCL, 0666);
2158 } while (fd == -1 && errsv == EEXIST);
2172 g_set_io_error (error,
2173 _("Unable to create trashing info file for %s: %s"),
2178 (void) g_close (fd, NULL);
2180 /* Write the full content of the info file before trashing to make
2181 * sure someone doesn't read an empty file. See #749314
2184 /* Use absolute names for homedir */
2185 if (is_homedir_trash)
2186 original_name = g_strdup (local->filename);
2188 original_name = try_make_relative (local->filename, topdir);
2189 original_name_escaped = g_uri_escape_string (original_name, "/", FALSE);
2191 g_free (original_name);
2195 GDateTime *now = g_date_time_new_now_local ();
2197 delete_time = g_date_time_format (now, "%Y-%m-%dT%H:%M:%S");
2199 delete_time = g_strdup ("9999-12-31T23:59:59");
2200 g_date_time_unref (now);
2203 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2204 original_name_escaped, delete_time);
2205 g_free (delete_time);
2207 g_file_set_contents_full (infofile, data, -1,
2208 G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING,
2211 /* TODO: Maybe we should verify that you can delete the file from the trash
2212 * before moving it? OTOH, that is hard, as it needs a recursive scan
2215 trashfile = g_build_filename (filesdir, trashname, NULL);
2219 if (g_rename (local->filename, trashfile) == -1)
2223 g_unlink (infofile);
2230 /* The trash dir was actually on another fs anyway!?
2231 * This can happen when the same device is mounted multiple
2232 * times, or with bind mounts of the same fs.
2234 g_set_io_error (error,
2235 _("Unable to trash file %s across filesystem boundaries"),
2238 g_set_io_error (error,
2239 _("Unable to trash file %s: %s"),
2244 vfs = g_vfs_get_default ();
2245 class = G_VFS_GET_CLASS (vfs);
2246 if (class->local_file_moved)
2247 class->local_file_moved (vfs, local->filename, trashfile);
2251 /* TODO: Do we need to update mtime/atime here after the move? */
2256 g_free (original_name_escaped);
2261 #else /* G_OS_WIN32 */
2263 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
2265 return FALSE; /* XXX ??? */
2269 g_local_file_trash (GFile *file,
2270 GCancellable *cancellable,
2273 GLocalFile *local = G_LOCAL_FILE (file);
2274 SHFILEOPSTRUCTW op = {0};
2279 wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
2280 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2281 wfilename = g_renew (wchar_t, wfilename, len + 2);
2282 wfilename[len + 1] = 0;
2284 op.wFunc = FO_DELETE;
2285 op.pFrom = wfilename;
2286 op.fFlags = FOF_ALLOWUNDO;
2288 success = SHFileOperationW (&op) == 0;
2290 if (success && op.fAnyOperationsAborted)
2292 if (cancellable && !g_cancellable_is_cancelled (cancellable))
2293 g_cancellable_cancel (cancellable);
2294 g_set_io_error (error,
2295 _("Unable to trash file %s: %s"),
2300 g_set_io_error (error,
2301 _("Unable to trash file %s"),
2307 #endif /* G_OS_WIN32 */
2310 g_local_file_make_directory (GFile *file,
2311 GCancellable *cancellable,
2314 GLocalFile *local = G_LOCAL_FILE (file);
2316 if (g_mkdir (local->filename, 0777) == -1)
2320 if (errsv == EINVAL)
2321 /* This must be an invalid filename, on e.g. FAT */
2322 g_set_error_literal (error, G_IO_ERROR,
2323 G_IO_ERROR_INVALID_FILENAME,
2324 _("Invalid filename"));
2326 g_set_io_error (error,
2327 _("Error creating directory %s: %s"),
2337 g_local_file_make_symbolic_link (GFile *file,
2338 const char *symlink_value,
2339 GCancellable *cancellable,
2342 GLocalFile *local = G_LOCAL_FILE (file);
2344 if (symlink (symlink_value, local->filename) == -1)
2348 if (errsv == EINVAL)
2349 /* This must be an invalid filename, on e.g. FAT */
2350 g_set_error_literal (error, G_IO_ERROR,
2351 G_IO_ERROR_INVALID_FILENAME,
2352 _("Invalid filename"));
2353 else if (errsv == EPERM)
2354 g_set_error (error, G_IO_ERROR,
2355 G_IO_ERROR_NOT_SUPPORTED,
2356 _("Filesystem does not support symbolic links"));
2358 g_set_io_error (error,
2359 _("Error making symbolic link %s: %s"),
2368 g_local_file_move (GFile *source,
2370 GFileCopyFlags flags,
2371 GCancellable *cancellable,
2372 GFileProgressCallback progress_callback,
2373 gpointer progress_callback_data,
2376 GLocalFile *local_source, *local_destination;
2378 gboolean destination_exist, source_is_dir;
2385 if (!G_IS_LOCAL_FILE (source) ||
2386 !G_IS_LOCAL_FILE (destination))
2388 /* Fall back to default move */
2389 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2393 local_source = G_LOCAL_FILE (source);
2394 local_destination = G_LOCAL_FILE (destination);
2396 res = g_lstat (local_source->filename, &statbuf);
2401 g_set_io_error (error,
2402 _("Error moving file %s: %s"),
2407 source_is_dir = S_ISDIR (statbuf.st_mode);
2408 source_size = statbuf.st_size;
2410 destination_exist = FALSE;
2411 res = g_lstat (local_destination->filename, &statbuf);
2414 destination_exist = TRUE; /* Target file exists */
2416 if (flags & G_FILE_COPY_OVERWRITE)
2418 /* Always fail on dirs, even with overwrite */
2419 if (S_ISDIR (statbuf.st_mode))
2422 g_set_error_literal (error,
2424 G_IO_ERROR_WOULD_MERGE,
2425 _("Can’t move directory over directory"));
2427 g_set_error_literal (error,
2429 G_IO_ERROR_IS_DIRECTORY,
2430 _("Can’t copy over directory"));
2436 g_set_io_error (error,
2437 _("Error moving file %s: %s"),
2443 if (flags & G_FILE_COPY_BACKUP && destination_exist)
2445 backup_name = g_strconcat (local_destination->filename, "~", NULL);
2446 if (g_rename (local_destination->filename, backup_name) == -1)
2448 g_set_error_literal (error,
2450 G_IO_ERROR_CANT_CREATE_BACKUP,
2451 _("Backup file creation failed"));
2452 g_free (backup_name);
2455 g_free (backup_name);
2456 destination_exist = FALSE; /* It did, but no more */
2459 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2461 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2462 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2463 res = g_unlink (local_destination->filename);
2468 g_set_error (error, G_IO_ERROR,
2469 g_io_error_from_errno (errsv),
2470 _("Error removing target file: %s"),
2471 g_strerror (errsv));
2476 if (g_rename (local_source->filename, local_destination->filename) == -1)
2481 /* This will cause the fallback code to run */
2482 g_set_error_literal (error, G_IO_ERROR,
2483 G_IO_ERROR_NOT_SUPPORTED,
2484 _("Move between mounts not supported"));
2485 else if (errsv == EINVAL)
2486 /* This must be an invalid filename, on e.g. FAT, or
2487 we're trying to move the file into itself...
2488 We return invalid filename for both... */
2489 g_set_error_literal (error, G_IO_ERROR,
2490 G_IO_ERROR_INVALID_FILENAME,
2491 _("Invalid filename"));
2493 g_set_io_error (error,
2494 _("Error moving file %s: %s"),
2499 vfs = g_vfs_get_default ();
2500 class = G_VFS_GET_CLASS (vfs);
2501 if (class->local_file_moved)
2502 class->local_file_moved (vfs, local_source->filename, local_destination->filename);
2504 /* Make sure we send full copied size */
2505 if (progress_callback)
2506 progress_callback (source_size, source_size, progress_callback_data);
2514 g_local_file_is_nfs_home (const gchar *filename)
2522 is_remote_fs_type (const gchar *fsname)
2526 if (strcmp (fsname, "nfs") == 0)
2528 if (strcmp (fsname, "nfs4") == 0)
2530 if (strcmp (fsname, "cifs") == 0)
2532 if (strcmp (fsname, "smb") == 0)
2534 if (strcmp (fsname, "smb2") == 0)
2542 g_local_file_is_nfs_home (const gchar *filename)
2544 static gboolean remote_home = FALSE;
2545 static gsize initialized;
2548 home = g_get_home_dir ();
2549 if (path_has_prefix (filename, home))
2551 if (g_once_init_enter (&initialized))
2555 const gchar *fs_type = NULL;
2557 file = _g_local_file_new (home);
2558 info = g_local_file_query_filesystem_info (file, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, NULL, NULL);
2560 fs_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE);
2561 if (g_strcmp0 (fs_type, "nfs") == 0 || g_strcmp0 (fs_type, "nfs4") == 0)
2563 g_clear_object (&info);
2564 g_object_unref (file);
2566 g_once_init_leave (&initialized, TRUE);
2573 #endif /* !G_OS_WIN32 */
2575 static GFileMonitor*
2576 g_local_file_monitor_dir (GFile *file,
2577 GFileMonitorFlags flags,
2578 GCancellable *cancellable,
2581 GLocalFile *local_file = G_LOCAL_FILE (file);
2583 return g_local_file_monitor_new_for_path (local_file->filename, TRUE, flags, error);
2586 static GFileMonitor*
2587 g_local_file_monitor_file (GFile *file,
2588 GFileMonitorFlags flags,
2589 GCancellable *cancellable,
2592 GLocalFile *local_file = G_LOCAL_FILE (file);
2594 return g_local_file_monitor_new_for_path (local_file->filename, FALSE, flags, error);
2597 /* Here is the GLocalFile implementation of g_file_measure_disk_usage().
2599 * If available, we use fopenat() in preference to filenames for
2600 * efficiency and safety reasons. We know that fopenat() is available
2601 * based on if AT_FDCWD is defined. POSIX guarantees that this will be
2602 * defined as a macro.
2604 * We use a linked list of stack-allocated GSList nodes in order to be
2605 * able to reconstruct the filename for error messages. We actually
2606 * pass the filename to operate on through the top node of the list.
2608 * In case we're using openat(), this top filename will be a basename
2609 * which should be opened in the directory which has also had its fd
2610 * passed along. If we're not using openat() then it will be a full
2611 * absolute filename.
2615 g_local_file_measure_size_error (GFileMeasureFlags flags,
2620 /* Only report an error if we were at the toplevel or if the caller
2621 * requested reporting of all errors.
2623 if ((name->next == NULL) || (flags & G_FILE_MEASURE_REPORT_ANY_ERROR))
2628 /* Skip some work if there is no error return */
2633 /* If using openat() we need to rebuild the filename for the message */
2634 filename = g_string_new (name->data);
2635 for (node = name->next; node; node = node->next)
2639 g_string_prepend_c (filename, G_DIR_SEPARATOR);
2640 utf8 = g_filename_display_name (node->data);
2641 g_string_prepend (filename, utf8);
2648 /* Otherwise, we already have it, so just use it. */
2650 filename = g_string_new (NULL);
2651 utf8 = g_filename_display_name (node->data);
2652 g_string_append (filename, utf8);
2657 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (saved_errno),
2658 _("Could not determine the disk usage of %s: %s"),
2659 filename->str, g_strerror (saved_errno));
2661 g_string_free (filename, TRUE);
2667 /* We're not reporting this error... */
2673 GFileMeasureFlags flags;
2675 GCancellable *cancellable;
2677 GFileMeasureProgressCallback progress_callback;
2678 gpointer progress_data;
2684 guint64 last_progress_report;
2688 g_local_file_measure_size_of_contents (gint fd,
2690 MeasureState *state,
2694 g_local_file_measure_size_of_file (gint parent_fd,
2696 MeasureState *state,
2701 if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
2704 #if defined (AT_FDCWD)
2705 if (g_local_file_fstatat (parent_fd, name->data, AT_SYMLINK_NOFOLLOW,
2706 G_LOCAL_FILE_STAT_FIELD_BASIC_STATS,
2707 G_LOCAL_FILE_STAT_FIELD_ALL & (~G_LOCAL_FILE_STAT_FIELD_ATIME),
2711 return g_local_file_measure_size_error (state->flags, errsv, name, error);
2713 #elif defined (HAVE_LSTAT) || !defined (G_OS_WIN32)
2714 if (g_lstat (name->data, &buf) != 0)
2717 return g_local_file_measure_size_error (state->flags, errsv, name, error);
2719 #else /* !AT_FDCWD && !HAVE_LSTAT && G_OS_WIN32 */
2720 if (GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (name->data, &buf) != 0)
2723 return g_local_file_measure_size_error (state->flags, errsv, name, error);
2729 /* If not at the toplevel, check for a device boundary. */
2731 if (state->flags & G_FILE_MEASURE_NO_XDEV)
2732 if (state->contained_on != _g_stat_dev (&buf))
2737 /* If, however, this is the toplevel, set the device number so
2738 * that recursive invocations can compare against it.
2740 state->contained_on = _g_stat_dev (&buf);
2743 #if defined (G_OS_WIN32)
2744 if (~state->flags & G_FILE_MEASURE_APPARENT_SIZE)
2745 state->disk_usage += buf.allocated_size;
2747 #elif defined (HAVE_STRUCT_STAT_ST_BLOCKS)
2748 if (~state->flags & G_FILE_MEASURE_APPARENT_SIZE)
2749 state->disk_usage += _g_stat_blocks (&buf) * G_GUINT64_CONSTANT (512);
2752 state->disk_usage += _g_stat_size (&buf);
2754 if (S_ISDIR (_g_stat_mode (&buf)))
2759 if (state->progress_callback)
2761 /* We could attempt to do some cleverness here in order to avoid
2762 * calling clock_gettime() so much, but we're doing stats and opens
2763 * all over the place already...
2765 if (state->last_progress_report)
2769 now = g_get_monotonic_time ();
2771 if (state->last_progress_report + 200 * G_TIME_SPAN_MILLISECOND < now)
2773 (* state->progress_callback) (TRUE,
2774 state->disk_usage, state->num_dirs, state->num_files,
2775 state->progress_data);
2776 state->last_progress_report = now;
2781 /* We must do an initial report to inform that more reports
2784 (* state->progress_callback) (TRUE, 0, 0, 0, state->progress_data);
2785 state->last_progress_report = g_get_monotonic_time ();
2789 if (S_ISDIR (_g_stat_mode (&buf)))
2796 if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
2800 #ifdef HAVE_OPEN_O_DIRECTORY
2801 dir_fd = openat (parent_fd, name->data, O_RDONLY|O_DIRECTORY);
2803 dir_fd = openat (parent_fd, name->data, O_RDONLY);
2807 return g_local_file_measure_size_error (state->flags, errsv, name, error);
2810 if (!g_local_file_measure_size_of_contents (dir_fd, name, state, error))
2818 g_local_file_measure_size_of_contents (gint fd,
2820 MeasureState *state,
2823 gboolean success = TRUE;
2829 /* If this fails, we want to preserve the errno from fopendir() */
2831 dirp = fdopendir (fd);
2832 dir = dirp ? GLIB_PRIVATE_CALL(g_dir_new_from_dirp) (dirp) : NULL;
2835 dir = GLIB_PRIVATE_CALL(g_dir_open_with_errno) (dir_name->data, 0);
2840 gint saved_errno = errno;
2846 return g_local_file_measure_size_error (state->flags, saved_errno, dir_name, error);
2849 while (success && (name = g_dir_read_name (dir)))
2853 node.next = dir_name;
2855 node.data = (gchar *) name;
2857 node.data = g_build_filename (dir_name->data, name, NULL);
2860 success = g_local_file_measure_size_of_file (fd, &node, state, error);
2873 g_local_file_measure_disk_usage (GFile *file,
2874 GFileMeasureFlags flags,
2875 GCancellable *cancellable,
2876 GFileMeasureProgressCallback progress_callback,
2877 gpointer progress_data,
2878 guint64 *disk_usage,
2883 GLocalFile *local_file = G_LOCAL_FILE (file);
2884 MeasureState state = { 0, };
2888 state.flags = flags;
2889 state.cancellable = cancellable;
2890 state.progress_callback = progress_callback;
2891 state.progress_data = progress_data;
2897 node.data = local_file->filename;
2900 if (!g_local_file_measure_size_of_file (root_fd, &node, &state, error))
2904 *disk_usage = state.disk_usage;
2907 *num_dirs = state.num_dirs;
2910 *num_files = state.num_files;
2916 g_local_file_file_iface_init (GFileIface *iface)
2918 iface->dup = g_local_file_dup;
2919 iface->hash = g_local_file_hash;
2920 iface->equal = g_local_file_equal;
2921 iface->is_native = g_local_file_is_native;
2922 iface->has_uri_scheme = g_local_file_has_uri_scheme;
2923 iface->get_uri_scheme = g_local_file_get_uri_scheme;
2924 iface->get_basename = g_local_file_get_basename;
2925 iface->get_path = g_local_file_get_path;
2926 iface->get_uri = g_local_file_get_uri;
2927 iface->get_parse_name = g_local_file_get_parse_name;
2928 iface->get_parent = g_local_file_get_parent;
2929 iface->prefix_matches = g_local_file_prefix_matches;
2930 iface->get_relative_path = g_local_file_get_relative_path;
2931 iface->resolve_relative_path = g_local_file_resolve_relative_path;
2932 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2933 iface->set_display_name = g_local_file_set_display_name;
2934 iface->enumerate_children = g_local_file_enumerate_children;
2935 iface->query_info = g_local_file_query_info;
2936 iface->query_filesystem_info = g_local_file_query_filesystem_info;
2937 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2938 iface->query_settable_attributes = g_local_file_query_settable_attributes;
2939 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2940 iface->set_attribute = g_local_file_set_attribute;
2941 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2942 iface->read_fn = g_local_file_read;
2943 iface->append_to = g_local_file_append_to;
2944 iface->create = g_local_file_create;
2945 iface->replace = g_local_file_replace;
2946 iface->open_readwrite = g_local_file_open_readwrite;
2947 iface->create_readwrite = g_local_file_create_readwrite;
2948 iface->replace_readwrite = g_local_file_replace_readwrite;
2949 iface->delete_file = g_local_file_delete;
2950 iface->trash = g_local_file_trash;
2951 iface->make_directory = g_local_file_make_directory;
2953 iface->make_symbolic_link = g_local_file_make_symbolic_link;
2955 iface->move = g_local_file_move;
2956 iface->monitor_dir = g_local_file_monitor_dir;
2957 iface->monitor_file = g_local_file_monitor_file;
2958 iface->measure_disk_usage = g_local_file_measure_disk_usage;
2960 iface->supports_thread_contexts = TRUE;