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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
36 #include <sys/statfs.h>
38 #if HAVE_SYS_STATVFS_H
39 #include <sys/statvfs.h>
43 #elif HAVE_SYS_MOUNT_H
45 #include <sys/param.h>
47 #include <sys/mount.h>
54 #include "gfileattribute.h"
55 #include "glocalfile.h"
56 #include "glocalfileinfo.h"
57 #include "glocalfileenumerator.h"
58 #include "glocalfileinputstream.h"
59 #include "glocalfileoutputstream.h"
60 #include "glocalfileiostream.h"
61 #include "glocaldirectorymonitor.h"
62 #include "glocalfilemonitor.h"
63 #include "gmountprivate.h"
64 #include "gunixmounts.h"
66 #include <glib/gstdio.h>
69 #include "glib-unix.h"
77 #ifndef FILE_READ_ONLY_VOLUME
78 #define FILE_READ_ONLY_VOLUME 0x00080000
82 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
85 #define S_ISLNK(m) (0)
90 static void g_local_file_file_iface_init (GFileIface *iface);
92 static GFileAttributeInfoList *local_writable_attributes = NULL;
93 static /* GFileAttributeInfoList * */ gsize local_writable_namespaces = 0;
97 GObject parent_instance;
102 #define g_local_file_get_type _g_local_file_get_type
103 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
104 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
105 g_local_file_file_iface_init))
107 static char *find_mountpoint_for (const char *file, dev_t dev);
110 g_local_file_finalize (GObject *object)
114 local = G_LOCAL_FILE (object);
116 g_free (local->filename);
118 G_OBJECT_CLASS (g_local_file_parent_class)->finalize (object);
122 g_local_file_class_init (GLocalFileClass *klass)
124 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
125 GFileAttributeInfoList *list;
127 gobject_class->finalize = g_local_file_finalize;
129 /* Set up attribute lists */
131 /* Writable attributes: */
133 list = g_file_attribute_info_list_new ();
135 g_file_attribute_info_list_add (list,
136 G_FILE_ATTRIBUTE_UNIX_MODE,
137 G_FILE_ATTRIBUTE_TYPE_UINT32,
138 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
139 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
142 g_file_attribute_info_list_add (list,
143 G_FILE_ATTRIBUTE_UNIX_UID,
144 G_FILE_ATTRIBUTE_TYPE_UINT32,
145 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
146 g_file_attribute_info_list_add (list,
147 G_FILE_ATTRIBUTE_UNIX_GID,
148 G_FILE_ATTRIBUTE_TYPE_UINT32,
149 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
153 g_file_attribute_info_list_add (list,
154 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
155 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
160 g_file_attribute_info_list_add (list,
161 G_FILE_ATTRIBUTE_TIME_MODIFIED,
162 G_FILE_ATTRIBUTE_TYPE_UINT64,
163 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
164 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
165 g_file_attribute_info_list_add (list,
166 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
167 G_FILE_ATTRIBUTE_TYPE_UINT32,
168 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
169 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
170 /* When copying, the target file is accessed. Replicating
171 * the source access time does not make sense in this case.
173 g_file_attribute_info_list_add (list,
174 G_FILE_ATTRIBUTE_TIME_ACCESS,
175 G_FILE_ATTRIBUTE_TYPE_UINT64,
176 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
177 g_file_attribute_info_list_add (list,
178 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
179 G_FILE_ATTRIBUTE_TYPE_UINT32,
180 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
183 local_writable_attributes = list;
187 g_local_file_init (GLocalFile *local)
192 _g_local_file_get_filename (GLocalFile *file)
194 return file->filename;
198 canonicalize_filename (const char *filename)
200 char *canon, *start, *p, *q;
204 if (!g_path_is_absolute (filename))
206 cwd = g_get_current_dir ();
207 canon = g_build_filename (cwd, filename, NULL);
211 canon = g_strdup (filename);
213 start = (char *)g_path_skip_root (canon);
217 /* This shouldn't really happen, as g_get_current_dir() should
218 return an absolute pathname, but bug 573843 shows this is
219 not always happening */
221 return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
224 /* POSIX allows double slashes at the start to
225 * mean something special (as does windows too).
226 * So, "//" != "/", but more than two slashes
232 G_IS_DIR_SEPARATOR (*p);
239 memmove (start, start+i, strlen (start+i)+1);
242 /* Make sure we're using the canonical dir separator */
244 while (p < start && G_IS_DIR_SEPARATOR (*p))
245 *p++ = G_DIR_SEPARATOR;
250 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
252 memmove (p, p+1, strlen (p+1)+1);
254 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
257 /* Skip previous separator */
261 while (p > start && !G_IS_DIR_SEPARATOR (*p))
263 if (G_IS_DIR_SEPARATOR (*p))
264 *p++ = G_DIR_SEPARATOR;
265 memmove (p, q, strlen (q)+1);
269 /* Skip until next separator */
270 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
275 /* Canonicalize one separator */
276 *p++ = G_DIR_SEPARATOR;
280 /* Remove additional separators */
282 while (*q && G_IS_DIR_SEPARATOR (*q))
286 memmove (p, q, strlen (q)+1);
289 /* Remove trailing slashes */
290 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
297 _g_local_file_new (const char *filename)
301 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
302 local->filename = canonicalize_filename (filename);
304 return G_FILE (local);
308 g_local_file_is_native (GFile *file)
314 g_local_file_has_uri_scheme (GFile *file,
315 const char *uri_scheme)
317 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
321 g_local_file_get_uri_scheme (GFile *file)
323 return g_strdup ("file");
327 g_local_file_get_basename (GFile *file)
329 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
333 g_local_file_get_path (GFile *file)
335 return g_strdup (G_LOCAL_FILE (file)->filename);
339 g_local_file_get_uri (GFile *file)
341 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
345 get_filename_charset (const gchar **filename_charset)
347 const gchar **charsets;
350 is_utf8 = g_get_filename_charsets (&charsets);
352 if (filename_charset)
353 *filename_charset = charsets[0];
359 name_is_valid_for_display (const char *string,
360 gboolean is_valid_utf8)
364 if (!is_valid_utf8 &&
365 !g_utf8_validate (string, -1, NULL))
368 while ((c = *string++) != 0)
370 if (g_ascii_iscntrl (c))
378 g_local_file_get_parse_name (GFile *file)
380 const char *filename;
382 const gchar *charset;
384 char *roundtripped_filename;
385 gboolean free_utf8_filename;
386 gboolean is_valid_utf8;
389 filename = G_LOCAL_FILE (file)->filename;
390 if (get_filename_charset (&charset))
392 utf8_filename = (char *)filename;
393 free_utf8_filename = FALSE;
394 is_valid_utf8 = FALSE; /* Can't guarantee this */
398 utf8_filename = g_convert (filename, -1,
399 "UTF-8", charset, NULL, NULL, NULL);
400 free_utf8_filename = TRUE;
401 is_valid_utf8 = TRUE;
403 if (utf8_filename != NULL)
405 /* Make sure we can roundtrip: */
406 roundtripped_filename = g_convert (utf8_filename, -1,
407 charset, "UTF-8", NULL, NULL, NULL);
409 if (roundtripped_filename == NULL ||
410 strcmp (filename, roundtripped_filename) != 0)
412 g_free (utf8_filename);
413 utf8_filename = NULL;
416 g_free (roundtripped_filename);
420 if (utf8_filename != NULL &&
421 name_is_valid_for_display (utf8_filename, is_valid_utf8))
423 if (free_utf8_filename)
424 parse_name = utf8_filename;
426 parse_name = g_strdup (utf8_filename);
431 char *dup_filename, *p, *backslash;
433 /* Turn backslashes into forward slashes like
434 * g_filename_to_uri() would do (but we can't use that because
435 * it doesn't output IRIs).
437 dup_filename = g_strdup (filename);
438 filename = p = dup_filename;
440 while ((backslash = strchr (p, '\\')) != NULL)
447 escaped_path = g_uri_escape_string (filename,
448 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
450 parse_name = g_strconcat ("file://",
451 (*escaped_path != '/') ? "/" : "",
455 g_free (escaped_path);
457 g_free (dup_filename);
459 if (free_utf8_filename)
460 g_free (utf8_filename);
467 g_local_file_get_parent (GFile *file)
469 GLocalFile *local = G_LOCAL_FILE (file);
470 const char *non_root;
475 non_root = g_path_skip_root (local->filename);
479 dirname = g_path_get_dirname (local->filename);
480 parent = _g_local_file_new (dirname);
486 g_local_file_dup (GFile *file)
488 GLocalFile *local = G_LOCAL_FILE (file);
490 return _g_local_file_new (local->filename);
494 g_local_file_hash (GFile *file)
496 GLocalFile *local = G_LOCAL_FILE (file);
498 return g_str_hash (local->filename);
502 g_local_file_equal (GFile *file1,
505 GLocalFile *local1 = G_LOCAL_FILE (file1);
506 GLocalFile *local2 = G_LOCAL_FILE (file2);
508 return g_str_equal (local1->filename, local2->filename);
512 match_prefix (const char *path,
517 prefix_len = strlen (prefix);
518 if (strncmp (path, prefix, prefix_len) != 0)
521 /* Handle the case where prefix is the root, so that
522 * the IS_DIR_SEPRARATOR check below works */
523 if (prefix_len > 0 &&
524 G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
527 return path + prefix_len;
531 g_local_file_prefix_matches (GFile *parent,
534 GLocalFile *parent_local = G_LOCAL_FILE (parent);
535 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
536 const char *remainder;
538 remainder = match_prefix (descendant_local->filename, parent_local->filename);
539 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
545 g_local_file_get_relative_path (GFile *parent,
548 GLocalFile *parent_local = G_LOCAL_FILE (parent);
549 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
550 const char *remainder;
552 remainder = match_prefix (descendant_local->filename, parent_local->filename);
554 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
555 return g_strdup (remainder + 1);
560 g_local_file_resolve_relative_path (GFile *file,
561 const char *relative_path)
563 GLocalFile *local = G_LOCAL_FILE (file);
567 if (g_path_is_absolute (relative_path))
568 return _g_local_file_new (relative_path);
570 filename = g_build_filename (local->filename, relative_path, NULL);
571 child = _g_local_file_new (filename);
577 static GFileEnumerator *
578 g_local_file_enumerate_children (GFile *file,
579 const char *attributes,
580 GFileQueryInfoFlags flags,
581 GCancellable *cancellable,
584 GLocalFile *local = G_LOCAL_FILE (file);
585 return _g_local_file_enumerator_new (local,
591 g_local_file_get_child_for_display_name (GFile *file,
592 const char *display_name,
598 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
599 if (basename == NULL)
601 g_set_error (error, G_IO_ERROR,
602 G_IO_ERROR_INVALID_FILENAME,
603 _("Invalid filename %s"), display_name);
607 new_file = g_file_get_child (file, basename);
613 #if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
615 get_fs_type (long f_type)
617 /* filesystem ids taken from linux manpage */
724 G_LOCK_DEFINE_STATIC(mount_info_hash);
725 static GHashTable *mount_info_hash = NULL;
726 static guint64 mount_info_hash_cache_time = 0;
729 MOUNT_INFO_READONLY = 1<<0
733 device_equal (gconstpointer v1,
736 return *(dev_t *)v1 == *(dev_t *)v2;
740 device_hash (gconstpointer v)
742 return (guint) *(dev_t *)v;
746 get_mount_info (GFileInfo *fs_info,
748 GFileAttributeMatcher *matcher)
752 gpointer info_as_ptr;
756 GUnixMountEntry *mount;
759 if (g_lstat (path, &buf) != 0)
762 G_LOCK (mount_info_hash);
764 if (mount_info_hash == NULL)
765 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
769 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
770 g_hash_table_remove_all (mount_info_hash);
772 got_info = g_hash_table_lookup_extended (mount_info_hash,
777 G_UNLOCK (mount_info_hash);
779 mount_info = GPOINTER_TO_UINT (info_as_ptr);
785 mountpoint = find_mountpoint_for (path, buf.st_dev);
786 if (mountpoint == NULL)
787 mountpoint = g_strdup ("/");
789 mount = g_unix_mount_at (mountpoint, &cache_time);
792 if (g_unix_mount_is_readonly (mount))
793 mount_info |= MOUNT_INFO_READONLY;
795 g_unix_mount_free (mount);
800 dev = g_new0 (dev_t, 1);
803 G_LOCK (mount_info_hash);
804 mount_info_hash_cache_time = cache_time;
805 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
806 G_UNLOCK (mount_info_hash);
809 if (mount_info & MOUNT_INFO_READONLY)
810 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
818 is_xp_or_later (void)
820 static int result = -1;
825 OSVERSIONINFOEX ver_info = {0};
826 DWORDLONG cond_mask = 0;
827 int op = VER_GREATER_EQUAL;
829 ver_info.dwOSVersionInfoSize = sizeof ver_info;
830 ver_info.dwMajorVersion = 5;
831 ver_info.dwMinorVersion = 1;
833 VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
834 VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
836 result = VerifyVersionInfo (&ver_info,
837 VER_MAJORVERSION | VER_MINORVERSION,
840 result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;
848 get_volume_for_path (const char *path)
854 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
855 result = g_new (wchar_t, MAX_PATH);
857 if (!GetVolumePathNameW (wpath, result, MAX_PATH))
859 char *msg = g_win32_error_message (GetLastError ());
860 g_critical ("GetVolumePathName failed: %s", msg);
867 len = wcslen (result);
868 if (len > 0 && result[len-1] != L'\\')
870 result = g_renew (wchar_t, result, len + 2);
880 find_mountpoint_for (const char *file, dev_t dev)
885 wpath = get_volume_for_path (file);
889 utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
896 get_filesystem_readonly (GFileInfo *info,
901 rootdir = get_volume_for_path (path);
905 if (is_xp_or_later ())
908 if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
909 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
910 (flags & FILE_READ_ONLY_VOLUME) != 0);
914 if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
915 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
922 #endif /* G_OS_WIN32 */
925 g_local_file_query_filesystem_info (GFile *file,
926 const char *attributes,
927 GCancellable *cancellable,
930 GLocalFile *local = G_LOCAL_FILE (file);
932 int statfs_result = 0;
938 struct statfs statfs_buffer;
939 #elif defined(USE_STATVFS)
941 struct statvfs statfs_buffer;
942 #endif /* USE_STATFS */
943 #endif /* G_OS_WIN32 */
944 GFileAttributeMatcher *attribute_matcher;
951 statfs_result = statfs (local->filename, &statfs_buffer);
952 #elif STATFS_ARGS == 4
953 statfs_result = statfs (local->filename, &statfs_buffer,
954 sizeof (statfs_buffer), 0);
955 #endif /* STATFS_ARGS == 2 */
956 block_size = statfs_buffer.f_bsize;
958 /* Many backends can't report free size (for instance the gvfs fuse
959 backend for backend not supporting this), and set f_bfree to 0,
960 but it can be 0 for real too. We treat the available == 0 and
961 free == 0 case as "both of these are invalid".
964 if (statfs_result == 0 &&
965 statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0)
967 #endif /* G_OS_WIN32 */
969 #elif defined(USE_STATVFS)
970 statfs_result = statvfs (local->filename, &statfs_buffer);
971 block_size = statfs_buffer.f_frsize;
972 #endif /* USE_STATFS */
974 if (statfs_result == -1)
978 g_set_error (error, G_IO_ERROR,
979 g_io_error_from_errno (errsv),
980 _("Error getting filesystem info: %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 = g_strdup (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 = g_strdup (statfs_buffer.f_fstypename);
1058 #elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1059 fstype = g_strdup (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 */
1081 g_file_attribute_matcher_unref (attribute_matcher);
1087 g_local_file_find_enclosing_mount (GFile *file,
1088 GCancellable *cancellable,
1091 GLocalFile *local = G_LOCAL_FILE (file);
1096 if (g_lstat (local->filename, &buf) != 0)
1098 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1099 /* Translators: This is an error message when trying to
1100 * find the enclosing (user visible) mount of a file, but
1102 _("Containing mount does not exist"));
1106 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
1107 if (mountpoint == NULL)
1109 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1110 /* Translators: This is an error message when trying to
1111 * find the enclosing (user visible) mount of a file, but
1113 _("Containing mount does not exist"));
1117 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1118 g_free (mountpoint);
1122 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1123 /* Translators: This is an error message when trying to find
1124 * the enclosing (user visible) mount of a file, but none
1126 _("Containing mount does not exist"));
1131 g_local_file_set_display_name (GFile *file,
1132 const char *display_name,
1133 GCancellable *cancellable,
1136 GLocalFile *local, *new_local;
1137 GFile *new_file, *parent;
1143 parent = g_file_get_parent (file);
1146 g_set_error_literal (error, G_IO_ERROR,
1148 _("Can't rename root directory"));
1152 new_file = g_file_get_child_for_display_name (parent, display_name, error);
1153 g_object_unref (parent);
1155 if (new_file == NULL)
1157 local = G_LOCAL_FILE (file);
1158 new_local = G_LOCAL_FILE (new_file);
1160 if (g_lstat (new_local->filename, &statbuf) == -1)
1164 if (errsv != ENOENT)
1166 g_set_error (error, G_IO_ERROR,
1167 g_io_error_from_errno (errsv),
1168 _("Error renaming file: %s"),
1169 g_strerror (errsv));
1175 g_set_error_literal (error, G_IO_ERROR,
1177 _("Can't rename file, filename already exists"));
1181 if (g_rename (local->filename, new_local->filename) == -1)
1185 if (errsv == EINVAL)
1186 /* We can't get a rename file into itself error herer,
1187 so this must be an invalid filename, on e.g. FAT */
1188 g_set_error_literal (error, G_IO_ERROR,
1189 G_IO_ERROR_INVALID_FILENAME,
1190 _("Invalid filename"));
1192 g_set_error (error, G_IO_ERROR,
1193 g_io_error_from_errno (errsv),
1194 _("Error renaming file: %s"),
1195 g_strerror (errsv));
1196 g_object_unref (new_file);
1200 vfs = g_vfs_get_default ();
1201 class = G_VFS_GET_CLASS (vfs);
1202 if (class->local_file_moved)
1203 class->local_file_moved (vfs, local->filename, new_local->filename);
1209 g_local_file_query_info (GFile *file,
1210 const char *attributes,
1211 GFileQueryInfoFlags flags,
1212 GCancellable *cancellable,
1215 GLocalFile *local = G_LOCAL_FILE (file);
1217 GFileAttributeMatcher *matcher;
1218 char *basename, *dirname;
1219 GLocalParentFileInfo parent_info;
1221 matcher = g_file_attribute_matcher_new (attributes);
1223 basename = g_path_get_basename (local->filename);
1225 dirname = g_path_get_dirname (local->filename);
1226 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1229 info = _g_local_file_info_get (basename, local->filename,
1230 matcher, flags, &parent_info,
1234 _g_local_file_info_free_parent_info (&parent_info);
1237 g_file_attribute_matcher_unref (matcher);
1242 static GFileAttributeInfoList *
1243 g_local_file_query_settable_attributes (GFile *file,
1244 GCancellable *cancellable,
1247 return g_file_attribute_info_list_ref (local_writable_attributes);
1250 static GFileAttributeInfoList *
1251 g_local_file_query_writable_namespaces (GFile *file,
1252 GCancellable *cancellable,
1255 GFileAttributeInfoList *list;
1259 if (g_once_init_enter (&local_writable_namespaces))
1261 /* Writable namespaces: */
1263 list = g_file_attribute_info_list_new ();
1266 g_file_attribute_info_list_add (list,
1268 G_FILE_ATTRIBUTE_TYPE_STRING,
1269 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
1270 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1271 g_file_attribute_info_list_add (list,
1273 G_FILE_ATTRIBUTE_TYPE_STRING,
1274 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1277 vfs = g_vfs_get_default ();
1278 class = G_VFS_GET_CLASS (vfs);
1279 if (class->add_writable_namespaces)
1280 class->add_writable_namespaces (vfs, list);
1282 g_once_init_leave (&local_writable_namespaces, (gsize)list);
1284 list = (GFileAttributeInfoList *)local_writable_namespaces;
1286 return g_file_attribute_info_list_ref (list);
1290 g_local_file_set_attribute (GFile *file,
1291 const char *attribute,
1292 GFileAttributeType type,
1294 GFileQueryInfoFlags flags,
1295 GCancellable *cancellable,
1298 GLocalFile *local = G_LOCAL_FILE (file);
1300 return _g_local_file_info_set_attribute (local->filename,
1310 g_local_file_set_attributes_from_info (GFile *file,
1312 GFileQueryInfoFlags flags,
1313 GCancellable *cancellable,
1316 GLocalFile *local = G_LOCAL_FILE (file);
1317 int res, chained_res;
1318 GFileIface *default_iface;
1320 res = _g_local_file_info_set_attributes (local->filename,
1326 error = NULL; /* Don't write over error if further errors */
1328 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1330 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1332 return res && chained_res;
1335 static GFileInputStream *
1336 g_local_file_read (GFile *file,
1337 GCancellable *cancellable,
1340 GLocalFile *local = G_LOCAL_FILE (file);
1344 fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1350 if (errsv == EACCES)
1352 ret = _stati64 (local->filename, &buf);
1353 if (ret == 0 && S_ISDIR (buf.st_mode))
1355 g_set_error_literal (error, G_IO_ERROR,
1356 G_IO_ERROR_IS_DIRECTORY,
1357 _("Can't open directory"));
1363 g_set_error (error, G_IO_ERROR,
1364 g_io_error_from_errno (errsv),
1365 _("Error opening file: %s"),
1366 g_strerror (errsv));
1371 ret = _fstati64 (fd, &buf);
1373 ret = fstat (fd, &buf);
1376 if (ret == 0 && S_ISDIR (buf.st_mode))
1378 (void) g_close (fd, NULL);
1379 g_set_error_literal (error, G_IO_ERROR,
1380 G_IO_ERROR_IS_DIRECTORY,
1381 _("Can't open directory"));
1385 return _g_local_file_input_stream_new (fd);
1388 static GFileOutputStream *
1389 g_local_file_append_to (GFile *file,
1390 GFileCreateFlags flags,
1391 GCancellable *cancellable,
1394 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1395 flags, cancellable, error);
1398 static GFileOutputStream *
1399 g_local_file_create (GFile *file,
1400 GFileCreateFlags flags,
1401 GCancellable *cancellable,
1404 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1406 cancellable, error);
1409 static GFileOutputStream *
1410 g_local_file_replace (GFile *file,
1412 gboolean make_backup,
1413 GFileCreateFlags flags,
1414 GCancellable *cancellable,
1417 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1419 etag, make_backup, flags, NULL,
1420 cancellable, error);
1423 static GFileIOStream *
1424 g_local_file_open_readwrite (GFile *file,
1425 GCancellable *cancellable,
1428 GFileOutputStream *output;
1431 output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
1433 cancellable, error);
1437 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1438 g_object_unref (output);
1442 static GFileIOStream *
1443 g_local_file_create_readwrite (GFile *file,
1444 GFileCreateFlags flags,
1445 GCancellable *cancellable,
1448 GFileOutputStream *output;
1451 output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1453 cancellable, error);
1457 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1458 g_object_unref (output);
1462 static GFileIOStream *
1463 g_local_file_replace_readwrite (GFile *file,
1465 gboolean make_backup,
1466 GFileCreateFlags flags,
1467 GCancellable *cancellable,
1470 GFileOutputStream *output;
1473 output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1475 etag, make_backup, flags, NULL,
1476 cancellable, error);
1480 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1481 g_object_unref (output);
1486 g_local_file_delete (GFile *file,
1487 GCancellable *cancellable,
1490 GLocalFile *local = G_LOCAL_FILE (file);
1494 if (g_remove (local->filename) == -1)
1498 /* Posix allows EEXIST too, but the more sane error
1499 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1501 if (errsv == EEXIST)
1504 g_set_error (error, G_IO_ERROR,
1505 g_io_error_from_errno (errsv),
1506 _("Error removing file: %s"),
1507 g_strerror (errsv));
1511 vfs = g_vfs_get_default ();
1512 class = G_VFS_GET_CLASS (vfs);
1513 if (class->local_file_removed)
1514 class->local_file_removed (vfs, local->filename);
1522 strip_trailing_slashes (const char *path)
1527 path_copy = g_strdup (path);
1528 len = strlen (path_copy);
1529 while (len > 1 && path_copy[len-1] == '/')
1530 path_copy[--len] = 0;
1536 expand_symlink (const char *link)
1538 char *resolved, *canonical, *parent, *link2;
1539 char symlink_value[4096];
1547 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1550 return g_strdup (link);
1551 symlink_value[res] = 0;
1554 if (g_path_is_absolute (symlink_value))
1555 return canonicalize_filename (symlink_value);
1558 link2 = strip_trailing_slashes (link);
1559 parent = g_path_get_dirname (link2);
1562 resolved = g_build_filename (parent, symlink_value, NULL);
1565 canonical = canonicalize_filename (resolved);
1574 get_parent (const char *path,
1578 GStatBuf parent_stat;
1582 path_copy = strip_trailing_slashes (path);
1584 parent = g_path_get_dirname (path_copy);
1585 if (strcmp (parent, ".") == 0 ||
1586 strcmp (parent, path_copy) == 0)
1596 if (g_lstat (parent, &parent_stat) != 0)
1602 if (S_ISLNK (parent_stat.st_mode))
1605 parent = expand_symlink (parent);
1610 if (num_recursions > 12)
1615 } while (S_ISLNK (parent_stat.st_mode));
1617 *parent_dev = parent_stat.st_dev;
1623 expand_all_symlinks (const char *path)
1625 char *parent, *parent_expanded;
1626 char *basename, *res;
1629 parent = get_parent (path, &parent_dev);
1632 parent_expanded = expand_all_symlinks (parent);
1634 basename = g_path_get_basename (path);
1635 res = g_build_filename (parent_expanded, basename, NULL);
1637 g_free (parent_expanded);
1640 res = g_strdup (path);
1646 find_mountpoint_for (const char *file,
1650 dev_t dir_dev, parent_dev;
1652 dir = g_strdup (file);
1657 parent = get_parent (dir, &parent_dev);
1661 if (parent_dev != dir_dev)
1673 find_topdir_for (const char *file)
1678 dir = get_parent (file, &dir_dev);
1682 return find_mountpoint_for (dir, dir_dev);
1686 get_unique_filename (const char *basename,
1692 return g_strdup (basename);
1694 dot = strchr (basename, '.');
1696 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1698 return g_strdup_printf ("%s.%d", basename, id);
1702 path_has_prefix (const char *path,
1710 prefix_len = strlen (prefix);
1712 if (strncmp (path, prefix, prefix_len) == 0 &&
1713 (prefix_len == 0 || /* empty prefix always matches */
1714 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1715 path[prefix_len] == 0 ||
1716 path[prefix_len] == '/'))
1723 try_make_relative (const char *path,
1726 char *path2, *base2;
1729 path2 = expand_all_symlinks (path);
1730 base2 = expand_all_symlinks (base);
1733 if (path_has_prefix (path2, base2))
1735 relative = path2 + strlen (base2);
1736 while (*relative == '/')
1738 relative = g_strdup (relative);
1746 /* Failed, use abs path */
1747 return g_strdup (path);
1751 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1753 static gsize home_dev_set = 0;
1754 static dev_t home_dev;
1755 char *topdir, *globaldir, *trashdir, *tmpname;
1758 GStatBuf global_stat, trash_stat;
1761 if (g_once_init_enter (&home_dev_set))
1765 g_stat (g_get_home_dir (), &home_stat);
1766 home_dev = home_stat.st_dev;
1767 g_once_init_leave (&home_dev_set, 1);
1770 /* Assume we can trash to the home */
1771 if (dir_dev == home_dev)
1774 topdir = find_mountpoint_for (dirname, dir_dev);
1778 globaldir = g_build_filename (topdir, ".Trash", NULL);
1779 if (g_lstat (globaldir, &global_stat) == 0 &&
1780 S_ISDIR (global_stat.st_mode) &&
1781 (global_stat.st_mode & S_ISVTX) != 0)
1783 /* got a toplevel sysadmin created dir, assume we
1784 * can trash to it (we should be able to create a dir)
1785 * This fails for the FAT case where the ownership of
1786 * that dir would be wrong though..
1794 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1796 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1798 tmpname = g_strdup_printf (".Trash-%s", uid_str);
1799 trashdir = g_build_filename (topdir, tmpname, NULL);
1802 if (g_lstat (trashdir, &trash_stat) == 0)
1806 return S_ISDIR (trash_stat.st_mode) &&
1807 trash_stat.st_uid == uid;
1811 /* User specific trash didn't exist, can we create it? */
1812 res = g_access (topdir, W_OK) == 0;
1820 _g_local_file_is_lost_found_dir (const char *path, dev_t path_dev)
1822 gboolean ret = FALSE;
1823 gchar *mount_dir = NULL;
1824 size_t mount_dir_len;
1827 if (!g_str_has_suffix (path, "/lost+found"))
1830 mount_dir = find_mountpoint_for (path, path_dev);
1831 if (mount_dir == NULL)
1834 mount_dir_len = strlen (mount_dir);
1835 /* We special-case rootfs ('/') since it's the only case where
1836 * mount_dir ends in '/'
1838 if (mount_dir_len == 1)
1840 if (mount_dir_len + strlen ("/lost+found") != strlen (path))
1843 if (g_lstat (path, &statbuf) != 0)
1846 if (!(S_ISDIR (statbuf.st_mode) &&
1847 statbuf.st_uid == 0 &&
1848 statbuf.st_gid == 0))
1860 g_local_file_trash (GFile *file,
1861 GCancellable *cancellable,
1864 GLocalFile *local = G_LOCAL_FILE (file);
1865 GStatBuf file_stat, home_stat;
1866 const char *homedir;
1867 char *trashdir, *topdir, *infodir, *filesdir;
1868 char *basename, *trashname, *trashfile, *infoname, *infofile;
1869 char *original_name, *original_name_escaped;
1872 gboolean is_homedir_trash;
1873 char delete_time[32];
1875 GStatBuf trash_stat, global_stat;
1876 char *dirname, *globaldir;
1880 if (g_lstat (local->filename, &file_stat) != 0)
1884 g_set_error (error, G_IO_ERROR,
1885 g_io_error_from_errno (errsv),
1886 _("Error trashing file: %s"),
1887 g_strerror (errsv));
1891 homedir = g_get_home_dir ();
1892 g_stat (homedir, &home_stat);
1894 is_homedir_trash = FALSE;
1896 if (file_stat.st_dev == home_stat.st_dev)
1898 is_homedir_trash = TRUE;
1900 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1901 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1906 display_name = g_filename_display_name (trashdir);
1907 g_set_error (error, G_IO_ERROR,
1908 g_io_error_from_errno (errsv),
1909 _("Unable to create trash dir %s: %s"),
1910 display_name, g_strerror (errsv));
1911 g_free (display_name);
1915 topdir = g_strdup (g_get_user_data_dir ());
1923 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1925 topdir = find_topdir_for (local->filename);
1928 g_set_error_literal (error, G_IO_ERROR,
1929 G_IO_ERROR_NOT_SUPPORTED,
1930 _("Unable to find toplevel directory for trash"));
1934 /* Try looking for global trash dir $topdir/.Trash/$uid */
1935 globaldir = g_build_filename (topdir, ".Trash", NULL);
1936 if (g_lstat (globaldir, &global_stat) == 0 &&
1937 S_ISDIR (global_stat.st_mode) &&
1938 (global_stat.st_mode & S_ISVTX) != 0)
1940 trashdir = g_build_filename (globaldir, uid_str, NULL);
1942 if (g_lstat (trashdir, &trash_stat) == 0)
1944 if (!S_ISDIR (trash_stat.st_mode) ||
1945 trash_stat.st_uid != uid)
1947 /* Not a directory or not owned by user, ignore */
1952 else if (g_mkdir (trashdir, 0700) == -1)
1960 if (trashdir == NULL)
1962 gboolean tried_create;
1964 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1965 dirname = g_strdup_printf (".Trash-%s", uid_str);
1966 trashdir = g_build_filename (topdir, dirname, NULL);
1969 tried_create = FALSE;
1972 if (g_lstat (trashdir, &trash_stat) == 0)
1974 if (!S_ISDIR (trash_stat.st_mode) ||
1975 trash_stat.st_uid != uid)
1977 /* Remove the failed directory */
1979 g_remove (trashdir);
1981 /* Not a directory or not owned by user, ignore */
1988 if (!tried_create &&
1989 g_mkdir (trashdir, 0700) != -1)
1991 /* Ensure that the created dir has the right uid etc.
1992 This might fail on e.g. a FAT dir */
1993 tried_create = TRUE;
2004 if (trashdir == NULL)
2007 g_set_error_literal (error, G_IO_ERROR,
2008 G_IO_ERROR_NOT_SUPPORTED,
2009 _("Unable to find or create trash directory"));
2014 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
2016 infodir = g_build_filename (trashdir, "info", NULL);
2017 filesdir = g_build_filename (trashdir, "files", NULL);
2020 /* Make sure we have the subdirectories */
2021 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
2022 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
2027 g_set_error_literal (error, G_IO_ERROR,
2028 G_IO_ERROR_NOT_SUPPORTED,
2029 _("Unable to find or create trash directory"));
2033 basename = g_path_get_basename (local->filename);
2041 trashname = get_unique_filename (basename, i++);
2042 infoname = g_strconcat (trashname, ".trashinfo", NULL);
2043 infofile = g_build_filename (infodir, infoname, NULL);
2046 fd = g_open (infofile, O_CREAT | O_EXCL, 0666);
2047 } while (fd == -1 && errno == EEXIST);
2061 g_set_error (error, G_IO_ERROR,
2062 g_io_error_from_errno (errsv),
2063 _("Unable to create trashing info file: %s"),
2064 g_strerror (errsv));
2068 (void) g_close (fd, NULL);
2070 /* TODO: Maybe we should verify that you can delete the file from the trash
2071 before moving it? OTOH, that is hard, as it needs a recursive scan */
2073 trashfile = g_build_filename (filesdir, trashname, NULL);
2077 if (g_rename (local->filename, trashfile) == -1)
2087 /* The trash dir was actually on another fs anyway!?
2088 This can happen when the same device is mounted multiple
2089 times, or with bind mounts of the same fs. */
2090 g_set_error (error, G_IO_ERROR,
2091 G_IO_ERROR_NOT_SUPPORTED,
2092 _("Unable to trash file: %s"),
2093 g_strerror (errsv));
2095 g_set_error (error, G_IO_ERROR,
2096 g_io_error_from_errno (errsv),
2097 _("Unable to trash file: %s"),
2098 g_strerror (errsv));
2102 vfs = g_vfs_get_default ();
2103 class = G_VFS_GET_CLASS (vfs);
2104 if (class->local_file_moved)
2105 class->local_file_moved (vfs, local->filename, trashfile);
2109 /* TODO: Do we need to update mtime/atime here after the move? */
2111 /* Use absolute names for homedir */
2112 if (is_homedir_trash)
2113 original_name = g_strdup (local->filename);
2115 original_name = try_make_relative (local->filename, topdir);
2116 original_name_escaped = g_uri_escape_string (original_name, "/", FALSE);
2118 g_free (original_name);
2125 localtime_r (&t, &now);
2127 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
2130 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2131 original_name_escaped, delete_time);
2133 g_file_set_contents (infofile, data, -1, NULL);
2137 g_free (original_name_escaped);
2142 #else /* G_OS_WIN32 */
2144 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
2146 return FALSE; /* XXX ??? */
2150 g_local_file_trash (GFile *file,
2151 GCancellable *cancellable,
2154 GLocalFile *local = G_LOCAL_FILE (file);
2155 SHFILEOPSTRUCTW op = {0};
2160 wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
2161 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2162 wfilename = g_renew (wchar_t, wfilename, len + 2);
2163 wfilename[len + 1] = 0;
2165 op.wFunc = FO_DELETE;
2166 op.pFrom = wfilename;
2167 op.fFlags = FOF_ALLOWUNDO;
2169 success = SHFileOperationW (&op) == 0;
2171 if (success && op.fAnyOperationsAborted)
2173 if (cancellable && !g_cancellable_is_cancelled (cancellable))
2174 g_cancellable_cancel (cancellable);
2175 g_set_error (error, G_IO_ERROR,
2176 G_IO_ERROR_CANCELLED,
2177 _("Unable to trash file: %s"),
2178 _("Operation was cancelled"));
2182 g_set_error (error, G_IO_ERROR,
2184 _("Unable to trash file: %s"),
2185 _("internal error"));
2190 #endif /* G_OS_WIN32 */
2193 g_local_file_make_directory (GFile *file,
2194 GCancellable *cancellable,
2197 GLocalFile *local = G_LOCAL_FILE (file);
2199 if (g_mkdir (local->filename, 0777) == -1)
2203 if (errsv == EINVAL)
2204 /* This must be an invalid filename, on e.g. FAT */
2205 g_set_error_literal (error, G_IO_ERROR,
2206 G_IO_ERROR_INVALID_FILENAME,
2207 _("Invalid filename"));
2209 g_set_error (error, G_IO_ERROR,
2210 g_io_error_from_errno (errsv),
2211 _("Error creating directory: %s"),
2212 g_strerror (errsv));
2220 g_local_file_make_symbolic_link (GFile *file,
2221 const char *symlink_value,
2222 GCancellable *cancellable,
2226 GLocalFile *local = G_LOCAL_FILE (file);
2228 if (symlink (symlink_value, local->filename) == -1)
2232 if (errsv == EINVAL)
2233 /* This must be an invalid filename, on e.g. FAT */
2234 g_set_error_literal (error, G_IO_ERROR,
2235 G_IO_ERROR_INVALID_FILENAME,
2236 _("Invalid filename"));
2237 else if (errsv == EPERM)
2238 g_set_error (error, G_IO_ERROR,
2239 G_IO_ERROR_NOT_SUPPORTED,
2240 _("Filesystem does not support symbolic links"));
2242 g_set_error (error, G_IO_ERROR,
2243 g_io_error_from_errno (errsv),
2244 _("Error making symbolic link: %s"),
2245 g_strerror (errsv));
2250 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
2257 g_local_file_copy (GFile *source,
2259 GFileCopyFlags flags,
2260 GCancellable *cancellable,
2261 GFileProgressCallback progress_callback,
2262 gpointer progress_callback_data,
2265 /* Fall back to default copy */
2266 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
2271 g_local_file_move (GFile *source,
2273 GFileCopyFlags flags,
2274 GCancellable *cancellable,
2275 GFileProgressCallback progress_callback,
2276 gpointer progress_callback_data,
2279 GLocalFile *local_source, *local_destination;
2281 gboolean destination_exist, source_is_dir;
2288 if (!G_IS_LOCAL_FILE (source) ||
2289 !G_IS_LOCAL_FILE (destination))
2291 /* Fall back to default move */
2292 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2296 local_source = G_LOCAL_FILE (source);
2297 local_destination = G_LOCAL_FILE (destination);
2299 res = g_lstat (local_source->filename, &statbuf);
2304 g_set_error (error, G_IO_ERROR,
2305 g_io_error_from_errno (errsv),
2306 _("Error moving file: %s"),
2307 g_strerror (errsv));
2311 source_is_dir = S_ISDIR (statbuf.st_mode);
2312 source_size = statbuf.st_size;
2314 destination_exist = FALSE;
2315 res = g_lstat (local_destination->filename, &statbuf);
2318 destination_exist = TRUE; /* Target file exists */
2320 if (flags & G_FILE_COPY_OVERWRITE)
2322 /* Always fail on dirs, even with overwrite */
2323 if (S_ISDIR (statbuf.st_mode))
2326 g_set_error_literal (error,
2328 G_IO_ERROR_WOULD_MERGE,
2329 _("Can't move directory over directory"));
2331 g_set_error_literal (error,
2333 G_IO_ERROR_IS_DIRECTORY,
2334 _("Can't copy over directory"));
2340 g_set_error_literal (error,
2343 _("Target file exists"));
2348 if (flags & G_FILE_COPY_BACKUP && destination_exist)
2350 backup_name = g_strconcat (local_destination->filename, "~", NULL);
2351 if (g_rename (local_destination->filename, backup_name) == -1)
2353 g_set_error_literal (error,
2355 G_IO_ERROR_CANT_CREATE_BACKUP,
2356 _("Backup file creation failed"));
2357 g_free (backup_name);
2360 g_free (backup_name);
2361 destination_exist = FALSE; /* It did, but no more */
2364 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2366 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2367 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2368 res = g_unlink (local_destination->filename);
2373 g_set_error (error, G_IO_ERROR,
2374 g_io_error_from_errno (errsv),
2375 _("Error removing target file: %s"),
2376 g_strerror (errsv));
2381 if (g_rename (local_source->filename, local_destination->filename) == -1)
2386 /* This will cause the fallback code to run */
2387 g_set_error_literal (error, G_IO_ERROR,
2388 G_IO_ERROR_NOT_SUPPORTED,
2389 _("Move between mounts not supported"));
2390 else if (errsv == EINVAL)
2391 /* This must be an invalid filename, on e.g. FAT, or
2392 we're trying to move the file into itself...
2393 We return invalid filename for both... */
2394 g_set_error_literal (error, G_IO_ERROR,
2395 G_IO_ERROR_INVALID_FILENAME,
2396 _("Invalid filename"));
2398 g_set_error (error, G_IO_ERROR,
2399 g_io_error_from_errno (errsv),
2400 _("Error moving file: %s"),
2401 g_strerror (errsv));
2405 vfs = g_vfs_get_default ();
2406 class = G_VFS_GET_CLASS (vfs);
2407 if (class->local_file_moved)
2408 class->local_file_moved (vfs, local_source->filename, local_destination->filename);
2410 /* Make sure we send full copied size */
2411 if (progress_callback)
2412 progress_callback (source_size, source_size, progress_callback_data);
2420 is_remote (const gchar *filename)
2428 is_remote_fs (const gchar *filename)
2430 const char *fsname = NULL;
2433 struct statfs statfs_buffer;
2434 int statfs_result = 0;
2436 #if STATFS_ARGS == 2
2437 statfs_result = statfs (filename, &statfs_buffer);
2438 #elif STATFS_ARGS == 4
2439 statfs_result = statfs (filename, &statfs_buffer, sizeof (statfs_buffer), 0);
2442 #elif defined(USE_STATVFS)
2443 struct statvfs statfs_buffer;
2444 int statfs_result = 0;
2446 statfs_result = statvfs (filename, &statfs_buffer);
2451 if (statfs_result == -1)
2455 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
2456 fsname = statfs_buffer.f_fstypename;
2458 fsname = get_fs_type (statfs_buffer.f_type);
2461 #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
2462 fsname = statfs_buffer.f_basetype;
2467 if (strcmp (fsname, "nfs") == 0)
2469 if (strcmp (fsname, "nfs4") == 0)
2477 is_remote (const gchar *filename)
2479 static gboolean remote_home;
2480 static gsize initialized;
2483 home = g_get_home_dir ();
2484 if (path_has_prefix (filename, home))
2486 if (g_once_init_enter (&initialized))
2488 remote_home = is_remote_fs (home);
2489 g_once_init_leave (&initialized, TRUE);
2496 #endif /* !G_OS_WIN32 */
2498 static GFileMonitor*
2499 g_local_file_monitor_dir (GFile *file,
2500 GFileMonitorFlags flags,
2501 GCancellable *cancellable,
2504 GLocalFile* local_file = G_LOCAL_FILE(file);
2505 return _g_local_directory_monitor_new (local_file->filename, flags, is_remote (local_file->filename), error);
2508 static GFileMonitor*
2509 g_local_file_monitor_file (GFile *file,
2510 GFileMonitorFlags flags,
2511 GCancellable *cancellable,
2514 GLocalFile* local_file = G_LOCAL_FILE(file);
2515 return _g_local_file_monitor_new (local_file->filename, flags, is_remote (local_file->filename), error);
2519 /* Here is the GLocalFile implementation of g_file_measure_disk_usage().
2521 * If available, we use fopenat() in preference to filenames for
2522 * efficiency and safety reasons. We know that fopenat() is available
2523 * based on if AT_FDCWD is defined. POSIX guarantees that this will be
2524 * defined as a macro.
2526 * We use a linked list of stack-allocated GSList nodes in order to be
2527 * able to reconstruct the filename for error messages. We actually
2528 * pass the filename to operate on through the top node of the list.
2530 * In case we're using openat(), this top filename will be a basename
2531 * which should be opened in the directory which has also had its fd
2532 * passed along. If we're not using openat() then it will be a full
2533 * absolute filename.
2537 g_local_file_measure_size_error (GFileMeasureFlags flags,
2542 /* Only report an error if we were at the toplevel or if the caller
2543 * requested reporting of all errors.
2545 if ((name->next == NULL) || (flags & G_FILE_MEASURE_REPORT_ANY_ERROR))
2550 /* Skip some work if there is no error return */
2555 /* If using openat() we need to rebuild the filename for the message */
2556 filename = g_string_new (name->data);
2557 for (node = name->next; node; node = node->next)
2559 g_string_prepend_c (filename, G_DIR_SEPARATOR);
2560 g_string_prepend (filename, node->data);
2563 g_string_prepend (filename, "file://");
2565 /* Otherwise, we already have it, so just use it. */
2567 filename = g_string_new ("file://");
2568 g_string_append (filename, node->data);
2571 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (saved_errno),
2572 _("Could not determine the disk usage of %s: %s"),
2573 filename->str, g_strerror (saved_errno));
2575 g_string_free (filename, TRUE);
2581 /* We're not reporting this error... */
2587 GFileMeasureFlags flags;
2589 GCancellable *cancellable;
2591 GFileMeasureProgressCallback progress_callback;
2592 gpointer progress_data;
2598 guint64 last_progress_report;
2602 g_local_file_measure_size_of_contents (gint fd,
2604 MeasureState *state,
2608 g_local_file_measure_size_of_file (gint parent_fd,
2610 MeasureState *state,
2615 if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
2618 #if defined (AT_FDCWD)
2619 if (fstatat (parent_fd, name->data, &buf, AT_SYMLINK_NOFOLLOW) != 0)
2620 #elif defined (HAVE_LSTAT)
2621 if (lstat (name->data, &buf) != 0)
2623 if (stat (name->data, &buf) != 0)
2625 return g_local_file_measure_size_error (state->flags, errno, name, error);
2629 /* If not at the toplevel, check for a device boundary. */
2631 if (state->flags & G_FILE_MEASURE_NO_XDEV)
2632 if (state->contained_on != buf.st_dev)
2637 /* If, however, this is the toplevel, set the device number so
2638 * that recursive invocations can compare against it.
2640 state->contained_on = buf.st_dev;
2643 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
2644 if (~state->flags & G_FILE_MEASURE_APPARENT_SIZE)
2645 state->disk_usage += buf.st_blocks * G_GUINT64_CONSTANT (512);
2648 state->disk_usage += buf.st_size;
2650 if (S_ISDIR (buf.st_mode))
2655 if (state->progress_callback)
2657 /* We could attempt to do some cleverness here in order to avoid
2658 * calling clock_gettime() so much, but we're doing stats and opens
2659 * all over the place already...
2661 if (state->last_progress_report)
2665 now = g_get_monotonic_time ();
2667 if (state->last_progress_report + 200 * G_TIME_SPAN_MILLISECOND < now)
2669 (* state->progress_callback) (TRUE,
2670 state->disk_usage, state->num_dirs, state->num_files,
2671 state->progress_data);
2672 state->last_progress_report = now;
2677 /* We must do an initial report to inform that more reports
2680 (* state->progress_callback) (TRUE, 0, 0, 0, state->progress_data);
2681 state->last_progress_report = g_get_monotonic_time ();
2685 if (S_ISDIR (buf.st_mode))
2689 if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
2693 dir_fd = openat (parent_fd, name->data, O_RDONLY | O_DIRECTORY);
2695 return g_local_file_measure_size_error (state->flags, errno, name, error);
2698 if (!g_local_file_measure_size_of_contents (dir_fd, name, state, error))
2706 g_local_file_measure_size_of_contents (gint fd,
2708 MeasureState *state,
2711 gboolean success = TRUE;
2712 struct dirent *entry;
2716 dirp = fdopendir (fd);
2718 dirp = opendir (dir_name->data);
2723 gint saved_errno = errno;
2729 return g_local_file_measure_size_error (state->flags, saved_errno, dir_name, error);
2732 while (success && (entry = readdir (dirp)))
2734 gchar *name = entry->d_name;
2737 node.next = dir_name;
2741 node.data = g_build_filename (dir_name->data, name, NULL);
2744 /* skip '.' and '..' */
2745 if (name[0] == '.' &&
2747 (name[1] == '.' && name[2] == '\0')))
2750 success = g_local_file_measure_size_of_file (fd, &node, state, error);
2763 g_local_file_measure_disk_usage (GFile *file,
2764 GFileMeasureFlags flags,
2765 GCancellable *cancellable,
2766 GFileMeasureProgressCallback progress_callback,
2767 gpointer progress_data,
2768 guint64 *disk_usage,
2773 GLocalFile *local_file = G_LOCAL_FILE (file);
2774 MeasureState state = { 0, };
2778 state.flags = flags;
2779 state.cancellable = cancellable;
2780 state.progress_callback = progress_callback;
2781 state.progress_data = progress_data;
2787 node.data = local_file->filename;
2790 if (!g_local_file_measure_size_of_file (root_fd, &node, &state, error))
2794 *disk_usage = state.disk_usage;
2797 *num_dirs = state.num_dirs;
2800 *num_files = state.num_files;
2806 g_local_file_file_iface_init (GFileIface *iface)
2808 iface->dup = g_local_file_dup;
2809 iface->hash = g_local_file_hash;
2810 iface->equal = g_local_file_equal;
2811 iface->is_native = g_local_file_is_native;
2812 iface->has_uri_scheme = g_local_file_has_uri_scheme;
2813 iface->get_uri_scheme = g_local_file_get_uri_scheme;
2814 iface->get_basename = g_local_file_get_basename;
2815 iface->get_path = g_local_file_get_path;
2816 iface->get_uri = g_local_file_get_uri;
2817 iface->get_parse_name = g_local_file_get_parse_name;
2818 iface->get_parent = g_local_file_get_parent;
2819 iface->prefix_matches = g_local_file_prefix_matches;
2820 iface->get_relative_path = g_local_file_get_relative_path;
2821 iface->resolve_relative_path = g_local_file_resolve_relative_path;
2822 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2823 iface->set_display_name = g_local_file_set_display_name;
2824 iface->enumerate_children = g_local_file_enumerate_children;
2825 iface->query_info = g_local_file_query_info;
2826 iface->query_filesystem_info = g_local_file_query_filesystem_info;
2827 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2828 iface->query_settable_attributes = g_local_file_query_settable_attributes;
2829 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2830 iface->set_attribute = g_local_file_set_attribute;
2831 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2832 iface->read_fn = g_local_file_read;
2833 iface->append_to = g_local_file_append_to;
2834 iface->create = g_local_file_create;
2835 iface->replace = g_local_file_replace;
2836 iface->open_readwrite = g_local_file_open_readwrite;
2837 iface->create_readwrite = g_local_file_create_readwrite;
2838 iface->replace_readwrite = g_local_file_replace_readwrite;
2839 iface->delete_file = g_local_file_delete;
2840 iface->trash = g_local_file_trash;
2841 iface->make_directory = g_local_file_make_directory;
2842 iface->make_symbolic_link = g_local_file_make_symbolic_link;
2843 iface->copy = g_local_file_copy;
2844 iface->move = g_local_file_move;
2845 iface->monitor_dir = g_local_file_monitor_dir;
2846 iface->monitor_file = g_local_file_monitor_file;
2847 iface->measure_disk_usage = g_local_file_measure_disk_usage;
2849 iface->supports_thread_contexts = TRUE;