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>
38 #include <sys/statfs.h>
40 #if HAVE_SYS_STATVFS_H
41 #include <sys/statvfs.h>
45 #elif HAVE_SYS_MOUNT_H
47 #include <sys/param.h>
49 #include <sys/mount.h>
56 #include "gfileattribute.h"
57 #include "glocalfile.h"
58 #include "glocalfileinfo.h"
59 #include "glocalfileenumerator.h"
60 #include "glocalfileinputstream.h"
61 #include "glocalfileoutputstream.h"
62 #include "glocalfileiostream.h"
63 #include "glocaldirectorymonitor.h"
64 #include "glocalfilemonitor.h"
65 #include "gmountprivate.h"
66 #include "gunixmounts.h"
68 #include <glib/gstdio.h>
71 #include "glib-unix.h"
73 #include "glib-private.h"
75 #include "glib-private.h"
82 #ifndef FILE_READ_ONLY_VOLUME
83 #define FILE_READ_ONLY_VOLUME 0x00080000
87 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
90 #define S_ISLNK(m) (0)
95 static void g_local_file_file_iface_init (GFileIface *iface);
97 static GFileAttributeInfoList *local_writable_attributes = NULL;
98 static /* GFileAttributeInfoList * */ gsize local_writable_namespaces = 0;
102 GObject parent_instance;
107 #define g_local_file_get_type _g_local_file_get_type
108 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
109 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
110 g_local_file_file_iface_init))
112 static char *find_mountpoint_for (const char *file, dev_t dev);
115 g_local_file_finalize (GObject *object)
119 local = G_LOCAL_FILE (object);
121 g_free (local->filename);
123 G_OBJECT_CLASS (g_local_file_parent_class)->finalize (object);
127 g_local_file_class_init (GLocalFileClass *klass)
129 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
130 GFileAttributeInfoList *list;
132 gobject_class->finalize = g_local_file_finalize;
134 /* Set up attribute lists */
136 /* Writable attributes: */
138 list = g_file_attribute_info_list_new ();
140 g_file_attribute_info_list_add (list,
141 G_FILE_ATTRIBUTE_UNIX_MODE,
142 G_FILE_ATTRIBUTE_TYPE_UINT32,
143 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
144 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
147 g_file_attribute_info_list_add (list,
148 G_FILE_ATTRIBUTE_UNIX_UID,
149 G_FILE_ATTRIBUTE_TYPE_UINT32,
150 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
151 g_file_attribute_info_list_add (list,
152 G_FILE_ATTRIBUTE_UNIX_GID,
153 G_FILE_ATTRIBUTE_TYPE_UINT32,
154 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
158 g_file_attribute_info_list_add (list,
159 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
160 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
165 g_file_attribute_info_list_add (list,
166 G_FILE_ATTRIBUTE_TIME_MODIFIED,
167 G_FILE_ATTRIBUTE_TYPE_UINT64,
168 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
169 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
170 g_file_attribute_info_list_add (list,
171 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
172 G_FILE_ATTRIBUTE_TYPE_UINT32,
173 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
174 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
175 /* When copying, the target file is accessed. Replicating
176 * the source access time does not make sense in this case.
178 g_file_attribute_info_list_add (list,
179 G_FILE_ATTRIBUTE_TIME_ACCESS,
180 G_FILE_ATTRIBUTE_TYPE_UINT64,
181 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
182 g_file_attribute_info_list_add (list,
183 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
184 G_FILE_ATTRIBUTE_TYPE_UINT32,
185 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
188 local_writable_attributes = list;
192 g_local_file_init (GLocalFile *local)
197 _g_local_file_get_filename (GLocalFile *file)
199 return file->filename;
203 canonicalize_filename (const char *filename)
205 char *canon, *start, *p, *q;
209 if (!g_path_is_absolute (filename))
211 cwd = g_get_current_dir ();
212 canon = g_build_filename (cwd, filename, NULL);
216 canon = g_strdup (filename);
218 start = (char *)g_path_skip_root (canon);
222 /* This shouldn't really happen, as g_get_current_dir() should
223 return an absolute pathname, but bug 573843 shows this is
224 not always happening */
226 return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
229 /* POSIX allows double slashes at the start to
230 * mean something special (as does windows too).
231 * So, "//" != "/", but more than two slashes
237 G_IS_DIR_SEPARATOR (*p);
244 memmove (start, start+i, strlen (start+i)+1);
247 /* Make sure we're using the canonical dir separator */
249 while (p < start && G_IS_DIR_SEPARATOR (*p))
250 *p++ = G_DIR_SEPARATOR;
255 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
257 memmove (p, p+1, strlen (p+1)+1);
259 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
262 /* Skip previous separator */
266 while (p > start && !G_IS_DIR_SEPARATOR (*p))
268 if (G_IS_DIR_SEPARATOR (*p))
269 *p++ = G_DIR_SEPARATOR;
270 memmove (p, q, strlen (q)+1);
274 /* Skip until next separator */
275 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
280 /* Canonicalize one separator */
281 *p++ = G_DIR_SEPARATOR;
285 /* Remove additional separators */
287 while (*q && G_IS_DIR_SEPARATOR (*q))
291 memmove (p, q, strlen (q)+1);
294 /* Remove trailing slashes */
295 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
302 _g_local_file_new (const char *filename)
306 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
307 local->filename = canonicalize_filename (filename);
309 return G_FILE (local);
313 g_local_file_is_native (GFile *file)
319 g_local_file_has_uri_scheme (GFile *file,
320 const char *uri_scheme)
322 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
326 g_local_file_get_uri_scheme (GFile *file)
328 return g_strdup ("file");
332 g_local_file_get_basename (GFile *file)
334 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
338 g_local_file_get_path (GFile *file)
340 return g_strdup (G_LOCAL_FILE (file)->filename);
344 g_local_file_get_uri (GFile *file)
346 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
350 get_filename_charset (const gchar **filename_charset)
352 const gchar **charsets;
355 is_utf8 = g_get_filename_charsets (&charsets);
357 if (filename_charset)
358 *filename_charset = charsets[0];
364 name_is_valid_for_display (const char *string,
365 gboolean is_valid_utf8)
369 if (!is_valid_utf8 &&
370 !g_utf8_validate (string, -1, NULL))
373 while ((c = *string++) != 0)
375 if (g_ascii_iscntrl (c))
383 g_local_file_get_parse_name (GFile *file)
385 const char *filename;
387 const gchar *charset;
389 char *roundtripped_filename;
390 gboolean free_utf8_filename;
391 gboolean is_valid_utf8;
394 filename = G_LOCAL_FILE (file)->filename;
395 if (get_filename_charset (&charset))
397 utf8_filename = (char *)filename;
398 free_utf8_filename = FALSE;
399 is_valid_utf8 = FALSE; /* Can't guarantee this */
403 utf8_filename = g_convert (filename, -1,
404 "UTF-8", charset, NULL, NULL, NULL);
405 free_utf8_filename = TRUE;
406 is_valid_utf8 = TRUE;
408 if (utf8_filename != NULL)
410 /* Make sure we can roundtrip: */
411 roundtripped_filename = g_convert (utf8_filename, -1,
412 charset, "UTF-8", NULL, NULL, NULL);
414 if (roundtripped_filename == NULL ||
415 strcmp (filename, roundtripped_filename) != 0)
417 g_free (utf8_filename);
418 utf8_filename = NULL;
421 g_free (roundtripped_filename);
425 if (utf8_filename != NULL &&
426 name_is_valid_for_display (utf8_filename, is_valid_utf8))
428 if (free_utf8_filename)
429 parse_name = utf8_filename;
431 parse_name = g_strdup (utf8_filename);
436 char *dup_filename, *p, *backslash;
438 /* Turn backslashes into forward slashes like
439 * g_filename_to_uri() would do (but we can't use that because
440 * it doesn't output IRIs).
442 dup_filename = g_strdup (filename);
443 filename = p = dup_filename;
445 while ((backslash = strchr (p, '\\')) != NULL)
452 escaped_path = g_uri_escape_string (filename,
453 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
455 parse_name = g_strconcat ("file://",
456 (*escaped_path != '/') ? "/" : "",
460 g_free (escaped_path);
462 g_free (dup_filename);
464 if (free_utf8_filename)
465 g_free (utf8_filename);
472 g_local_file_get_parent (GFile *file)
474 GLocalFile *local = G_LOCAL_FILE (file);
475 const char *non_root;
480 non_root = g_path_skip_root (local->filename);
484 dirname = g_path_get_dirname (local->filename);
485 parent = _g_local_file_new (dirname);
491 g_local_file_dup (GFile *file)
493 GLocalFile *local = G_LOCAL_FILE (file);
495 return _g_local_file_new (local->filename);
499 g_local_file_hash (GFile *file)
501 GLocalFile *local = G_LOCAL_FILE (file);
503 return g_str_hash (local->filename);
507 g_local_file_equal (GFile *file1,
510 GLocalFile *local1 = G_LOCAL_FILE (file1);
511 GLocalFile *local2 = G_LOCAL_FILE (file2);
513 return g_str_equal (local1->filename, local2->filename);
517 match_prefix (const char *path,
522 prefix_len = strlen (prefix);
523 if (strncmp (path, prefix, prefix_len) != 0)
526 /* Handle the case where prefix is the root, so that
527 * the IS_DIR_SEPRARATOR check below works */
528 if (prefix_len > 0 &&
529 G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
532 return path + prefix_len;
536 g_local_file_prefix_matches (GFile *parent,
539 GLocalFile *parent_local = G_LOCAL_FILE (parent);
540 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
541 const char *remainder;
543 remainder = match_prefix (descendant_local->filename, parent_local->filename);
544 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
550 g_local_file_get_relative_path (GFile *parent,
553 GLocalFile *parent_local = G_LOCAL_FILE (parent);
554 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
555 const char *remainder;
557 remainder = match_prefix (descendant_local->filename, parent_local->filename);
559 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
560 return g_strdup (remainder + 1);
565 g_local_file_resolve_relative_path (GFile *file,
566 const char *relative_path)
568 GLocalFile *local = G_LOCAL_FILE (file);
572 if (g_path_is_absolute (relative_path))
573 return _g_local_file_new (relative_path);
575 filename = g_build_filename (local->filename, relative_path, NULL);
576 child = _g_local_file_new (filename);
582 static GFileEnumerator *
583 g_local_file_enumerate_children (GFile *file,
584 const char *attributes,
585 GFileQueryInfoFlags flags,
586 GCancellable *cancellable,
589 GLocalFile *local = G_LOCAL_FILE (file);
590 return _g_local_file_enumerator_new (local,
596 g_local_file_get_child_for_display_name (GFile *file,
597 const char *display_name,
603 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
604 if (basename == NULL)
606 g_set_error (error, G_IO_ERROR,
607 G_IO_ERROR_INVALID_FILENAME,
608 _("Invalid filename %s"), display_name);
612 new_file = g_file_get_child (file, basename);
618 #if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
620 get_fs_type (long f_type)
622 /* filesystem ids taken from linux manpage */
729 G_LOCK_DEFINE_STATIC(mount_info_hash);
730 static GHashTable *mount_info_hash = NULL;
731 static guint64 mount_info_hash_cache_time = 0;
734 MOUNT_INFO_READONLY = 1<<0
738 device_equal (gconstpointer v1,
741 return *(dev_t *)v1 == *(dev_t *)v2;
745 device_hash (gconstpointer v)
747 return (guint) *(dev_t *)v;
751 get_mount_info (GFileInfo *fs_info,
753 GFileAttributeMatcher *matcher)
757 gpointer info_as_ptr;
761 GUnixMountEntry *mount;
764 if (g_lstat (path, &buf) != 0)
767 G_LOCK (mount_info_hash);
769 if (mount_info_hash == NULL)
770 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
774 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
775 g_hash_table_remove_all (mount_info_hash);
777 got_info = g_hash_table_lookup_extended (mount_info_hash,
782 G_UNLOCK (mount_info_hash);
784 mount_info = GPOINTER_TO_UINT (info_as_ptr);
790 mountpoint = find_mountpoint_for (path, buf.st_dev);
791 if (mountpoint == NULL)
792 mountpoint = g_strdup ("/");
794 mount = g_unix_mount_at (mountpoint, &cache_time);
797 if (g_unix_mount_is_readonly (mount))
798 mount_info |= MOUNT_INFO_READONLY;
800 g_unix_mount_free (mount);
805 dev = g_new0 (dev_t, 1);
808 G_LOCK (mount_info_hash);
809 mount_info_hash_cache_time = cache_time;
810 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
811 G_UNLOCK (mount_info_hash);
814 if (mount_info & MOUNT_INFO_READONLY)
815 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
823 is_xp_or_later (void)
825 static int result = -1;
830 OSVERSIONINFOEX ver_info = {0};
831 DWORDLONG cond_mask = 0;
832 int op = VER_GREATER_EQUAL;
834 ver_info.dwOSVersionInfoSize = sizeof ver_info;
835 ver_info.dwMajorVersion = 5;
836 ver_info.dwMinorVersion = 1;
838 VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
839 VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
841 result = VerifyVersionInfo (&ver_info,
842 VER_MAJORVERSION | VER_MINORVERSION,
845 result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;
853 get_volume_for_path (const char *path)
859 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
860 result = g_new (wchar_t, MAX_PATH);
862 if (!GetVolumePathNameW (wpath, result, MAX_PATH))
864 char *msg = g_win32_error_message (GetLastError ());
865 g_critical ("GetVolumePathName failed: %s", msg);
872 len = wcslen (result);
873 if (len > 0 && result[len-1] != L'\\')
875 result = g_renew (wchar_t, result, len + 2);
885 find_mountpoint_for (const char *file, dev_t dev)
890 wpath = get_volume_for_path (file);
894 utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
901 get_filesystem_readonly (GFileInfo *info,
906 rootdir = get_volume_for_path (path);
910 if (is_xp_or_later ())
913 if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
914 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
915 (flags & FILE_READ_ONLY_VOLUME) != 0);
919 if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
920 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
927 #endif /* G_OS_WIN32 */
930 g_local_file_query_filesystem_info (GFile *file,
931 const char *attributes,
932 GCancellable *cancellable,
935 GLocalFile *local = G_LOCAL_FILE (file);
937 int statfs_result = 0;
943 struct statfs statfs_buffer;
944 #elif defined(USE_STATVFS)
946 struct statvfs statfs_buffer;
947 #endif /* USE_STATFS */
948 #endif /* G_OS_WIN32 */
949 GFileAttributeMatcher *attribute_matcher;
956 statfs_result = statfs (local->filename, &statfs_buffer);
957 #elif STATFS_ARGS == 4
958 statfs_result = statfs (local->filename, &statfs_buffer,
959 sizeof (statfs_buffer), 0);
960 #endif /* STATFS_ARGS == 2 */
961 block_size = statfs_buffer.f_bsize;
963 /* Many backends can't report free size (for instance the gvfs fuse
964 backend for backend not supporting this), and set f_bfree to 0,
965 but it can be 0 for real too. We treat the available == 0 and
966 free == 0 case as "both of these are invalid".
969 if (statfs_result == 0 &&
970 statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0)
972 #endif /* G_OS_WIN32 */
974 #elif defined(USE_STATVFS)
975 statfs_result = statvfs (local->filename, &statfs_buffer);
976 block_size = statfs_buffer.f_frsize;
977 #endif /* USE_STATFS */
979 if (statfs_result == -1)
983 g_set_error (error, G_IO_ERROR,
984 g_io_error_from_errno (errsv),
985 _("Error getting filesystem info: %s"),
990 info = g_file_info_new ();
992 attribute_matcher = g_file_attribute_matcher_new (attributes);
995 g_file_attribute_matcher_matches (attribute_matcher,
996 G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
999 gchar *localdir = g_path_get_dirname (local->filename);
1000 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1004 if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
1005 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
1008 #if defined(USE_STATFS) || defined(USE_STATVFS)
1009 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
1014 g_file_attribute_matcher_matches (attribute_matcher,
1015 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
1018 gchar *localdir = g_path_get_dirname (local->filename);
1019 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1023 if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
1024 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, (guint64)li.QuadPart);
1027 #if defined(USE_STATFS) || defined(USE_STATVFS)
1028 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
1030 #endif /* G_OS_WIN32 */
1034 g_file_attribute_matcher_matches (attribute_matcher,
1035 G_FILE_ATTRIBUTE_FILESYSTEM_USED))
1038 gchar *localdir = g_path_get_dirname (local->filename);
1039 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1040 ULARGE_INTEGER li_free;
1041 ULARGE_INTEGER li_total;
1044 if (GetDiskFreeSpaceExW (wdirname, &li_free, &li_total, NULL))
1045 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, (guint64)li_total.QuadPart - (guint64)li_free.QuadPart);
1048 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, block_size * (statfs_buffer.f_blocks - statfs_buffer.f_bfree));
1049 #endif /* G_OS_WIN32 */
1054 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1055 fstype = g_strdup (statfs_buffer.f_fstypename);
1057 fstype = get_fs_type (statfs_buffer.f_type);
1060 #elif defined(USE_STATVFS)
1061 #if defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
1062 fstype = g_strdup (statfs_buffer.f_fstypename);
1063 #elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1064 fstype = g_strdup (statfs_buffer.f_basetype);
1068 #endif /* USE_STATFS */
1071 g_file_attribute_matcher_matches (attribute_matcher,
1072 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1073 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1074 #endif /* G_OS_WIN32 */
1076 if (g_file_attribute_matcher_matches (attribute_matcher,
1077 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
1080 get_filesystem_readonly (info, local->filename);
1082 get_mount_info (info, local->filename, attribute_matcher);
1083 #endif /* G_OS_WIN32 */
1086 g_file_attribute_matcher_unref (attribute_matcher);
1092 g_local_file_find_enclosing_mount (GFile *file,
1093 GCancellable *cancellable,
1096 GLocalFile *local = G_LOCAL_FILE (file);
1101 if (g_lstat (local->filename, &buf) != 0)
1103 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1104 /* Translators: This is an error message when trying to
1105 * find the enclosing (user visible) mount of a file, but
1107 _("Containing mount does not exist"));
1111 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
1112 if (mountpoint == NULL)
1114 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1115 /* Translators: This is an error message when trying to
1116 * find the enclosing (user visible) mount of a file, but
1118 _("Containing mount does not exist"));
1122 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1123 g_free (mountpoint);
1127 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1128 /* Translators: This is an error message when trying to find
1129 * the enclosing (user visible) mount of a file, but none
1131 _("Containing mount does not exist"));
1136 g_local_file_set_display_name (GFile *file,
1137 const char *display_name,
1138 GCancellable *cancellable,
1141 GLocalFile *local, *new_local;
1142 GFile *new_file, *parent;
1148 parent = g_file_get_parent (file);
1151 g_set_error_literal (error, G_IO_ERROR,
1153 _("Can't rename root directory"));
1157 new_file = g_file_get_child_for_display_name (parent, display_name, error);
1158 g_object_unref (parent);
1160 if (new_file == NULL)
1162 local = G_LOCAL_FILE (file);
1163 new_local = G_LOCAL_FILE (new_file);
1165 if (g_lstat (new_local->filename, &statbuf) == -1)
1169 if (errsv != ENOENT)
1171 g_set_error (error, G_IO_ERROR,
1172 g_io_error_from_errno (errsv),
1173 _("Error renaming file: %s"),
1174 g_strerror (errsv));
1180 g_set_error_literal (error, G_IO_ERROR,
1182 _("Can't rename file, filename already exists"));
1186 if (g_rename (local->filename, new_local->filename) == -1)
1190 if (errsv == EINVAL)
1191 /* We can't get a rename file into itself error herer,
1192 so this must be an invalid filename, on e.g. FAT */
1193 g_set_error_literal (error, G_IO_ERROR,
1194 G_IO_ERROR_INVALID_FILENAME,
1195 _("Invalid filename"));
1197 g_set_error (error, G_IO_ERROR,
1198 g_io_error_from_errno (errsv),
1199 _("Error renaming file: %s"),
1200 g_strerror (errsv));
1201 g_object_unref (new_file);
1205 vfs = g_vfs_get_default ();
1206 class = G_VFS_GET_CLASS (vfs);
1207 if (class->local_file_moved)
1208 class->local_file_moved (vfs, local->filename, new_local->filename);
1214 g_local_file_query_info (GFile *file,
1215 const char *attributes,
1216 GFileQueryInfoFlags flags,
1217 GCancellable *cancellable,
1220 GLocalFile *local = G_LOCAL_FILE (file);
1222 GFileAttributeMatcher *matcher;
1223 char *basename, *dirname;
1224 GLocalParentFileInfo parent_info;
1226 matcher = g_file_attribute_matcher_new (attributes);
1228 basename = g_path_get_basename (local->filename);
1230 dirname = g_path_get_dirname (local->filename);
1231 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1234 info = _g_local_file_info_get (basename, local->filename,
1235 matcher, flags, &parent_info,
1239 _g_local_file_info_free_parent_info (&parent_info);
1242 g_file_attribute_matcher_unref (matcher);
1247 static GFileAttributeInfoList *
1248 g_local_file_query_settable_attributes (GFile *file,
1249 GCancellable *cancellable,
1252 return g_file_attribute_info_list_ref (local_writable_attributes);
1255 static GFileAttributeInfoList *
1256 g_local_file_query_writable_namespaces (GFile *file,
1257 GCancellable *cancellable,
1260 GFileAttributeInfoList *list;
1264 if (g_once_init_enter (&local_writable_namespaces))
1266 /* Writable namespaces: */
1268 list = g_file_attribute_info_list_new ();
1271 g_file_attribute_info_list_add (list,
1273 G_FILE_ATTRIBUTE_TYPE_STRING,
1274 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
1275 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1276 g_file_attribute_info_list_add (list,
1278 G_FILE_ATTRIBUTE_TYPE_STRING,
1279 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1282 vfs = g_vfs_get_default ();
1283 class = G_VFS_GET_CLASS (vfs);
1284 if (class->add_writable_namespaces)
1285 class->add_writable_namespaces (vfs, list);
1287 g_once_init_leave (&local_writable_namespaces, (gsize)list);
1289 list = (GFileAttributeInfoList *)local_writable_namespaces;
1291 return g_file_attribute_info_list_ref (list);
1295 g_local_file_set_attribute (GFile *file,
1296 const char *attribute,
1297 GFileAttributeType type,
1299 GFileQueryInfoFlags flags,
1300 GCancellable *cancellable,
1303 GLocalFile *local = G_LOCAL_FILE (file);
1305 return _g_local_file_info_set_attribute (local->filename,
1315 g_local_file_set_attributes_from_info (GFile *file,
1317 GFileQueryInfoFlags flags,
1318 GCancellable *cancellable,
1321 GLocalFile *local = G_LOCAL_FILE (file);
1322 int res, chained_res;
1323 GFileIface *default_iface;
1325 res = _g_local_file_info_set_attributes (local->filename,
1331 error = NULL; /* Don't write over error if further errors */
1333 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1335 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1337 return res && chained_res;
1340 static GFileInputStream *
1341 g_local_file_read (GFile *file,
1342 GCancellable *cancellable,
1345 GLocalFile *local = G_LOCAL_FILE (file);
1349 fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1355 if (errsv == EACCES)
1357 ret = _stati64 (local->filename, &buf);
1358 if (ret == 0 && S_ISDIR (buf.st_mode))
1360 g_set_error_literal (error, G_IO_ERROR,
1361 G_IO_ERROR_IS_DIRECTORY,
1362 _("Can't open directory"));
1368 g_set_error (error, G_IO_ERROR,
1369 g_io_error_from_errno (errsv),
1370 _("Error opening file: %s"),
1371 g_strerror (errsv));
1376 ret = _fstati64 (fd, &buf);
1378 ret = fstat (fd, &buf);
1381 if (ret == 0 && S_ISDIR (buf.st_mode))
1383 (void) g_close (fd, NULL);
1384 g_set_error_literal (error, G_IO_ERROR,
1385 G_IO_ERROR_IS_DIRECTORY,
1386 _("Can't open directory"));
1390 return _g_local_file_input_stream_new (fd);
1393 static GFileOutputStream *
1394 g_local_file_append_to (GFile *file,
1395 GFileCreateFlags flags,
1396 GCancellable *cancellable,
1399 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1400 flags, cancellable, error);
1403 static GFileOutputStream *
1404 g_local_file_create (GFile *file,
1405 GFileCreateFlags flags,
1406 GCancellable *cancellable,
1409 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1411 cancellable, error);
1414 static GFileOutputStream *
1415 g_local_file_replace (GFile *file,
1417 gboolean make_backup,
1418 GFileCreateFlags flags,
1419 GCancellable *cancellable,
1422 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1424 etag, make_backup, flags, NULL,
1425 cancellable, error);
1428 static GFileIOStream *
1429 g_local_file_open_readwrite (GFile *file,
1430 GCancellable *cancellable,
1433 GFileOutputStream *output;
1436 output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
1438 cancellable, error);
1442 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1443 g_object_unref (output);
1447 static GFileIOStream *
1448 g_local_file_create_readwrite (GFile *file,
1449 GFileCreateFlags flags,
1450 GCancellable *cancellable,
1453 GFileOutputStream *output;
1456 output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1458 cancellable, error);
1462 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1463 g_object_unref (output);
1467 static GFileIOStream *
1468 g_local_file_replace_readwrite (GFile *file,
1470 gboolean make_backup,
1471 GFileCreateFlags flags,
1472 GCancellable *cancellable,
1475 GFileOutputStream *output;
1478 output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1480 etag, make_backup, flags, NULL,
1481 cancellable, error);
1485 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1486 g_object_unref (output);
1491 g_local_file_delete (GFile *file,
1492 GCancellable *cancellable,
1495 GLocalFile *local = G_LOCAL_FILE (file);
1499 if (g_remove (local->filename) == -1)
1503 /* Posix allows EEXIST too, but the more sane error
1504 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1506 if (errsv == EEXIST)
1509 g_set_error (error, G_IO_ERROR,
1510 g_io_error_from_errno (errsv),
1511 _("Error removing file: %s"),
1512 g_strerror (errsv));
1516 vfs = g_vfs_get_default ();
1517 class = G_VFS_GET_CLASS (vfs);
1518 if (class->local_file_removed)
1519 class->local_file_removed (vfs, local->filename);
1527 strip_trailing_slashes (const char *path)
1532 path_copy = g_strdup (path);
1533 len = strlen (path_copy);
1534 while (len > 1 && path_copy[len-1] == '/')
1535 path_copy[--len] = 0;
1541 expand_symlink (const char *link)
1543 char *resolved, *canonical, *parent, *link2;
1544 char symlink_value[4096];
1552 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1555 return g_strdup (link);
1556 symlink_value[res] = 0;
1559 if (g_path_is_absolute (symlink_value))
1560 return canonicalize_filename (symlink_value);
1563 link2 = strip_trailing_slashes (link);
1564 parent = g_path_get_dirname (link2);
1567 resolved = g_build_filename (parent, symlink_value, NULL);
1570 canonical = canonicalize_filename (resolved);
1579 get_parent (const char *path,
1583 GStatBuf parent_stat;
1587 path_copy = strip_trailing_slashes (path);
1589 parent = g_path_get_dirname (path_copy);
1590 if (strcmp (parent, ".") == 0 ||
1591 strcmp (parent, path_copy) == 0)
1601 if (g_lstat (parent, &parent_stat) != 0)
1607 if (S_ISLNK (parent_stat.st_mode))
1610 parent = expand_symlink (parent);
1615 if (num_recursions > 12)
1620 } while (S_ISLNK (parent_stat.st_mode));
1622 *parent_dev = parent_stat.st_dev;
1628 expand_all_symlinks (const char *path)
1630 char *parent, *parent_expanded;
1631 char *basename, *res;
1634 parent = get_parent (path, &parent_dev);
1637 parent_expanded = expand_all_symlinks (parent);
1639 basename = g_path_get_basename (path);
1640 res = g_build_filename (parent_expanded, basename, NULL);
1642 g_free (parent_expanded);
1645 res = g_strdup (path);
1651 find_mountpoint_for (const char *file,
1655 dev_t dir_dev, parent_dev;
1657 dir = g_strdup (file);
1662 parent = get_parent (dir, &parent_dev);
1666 if (parent_dev != dir_dev)
1678 find_topdir_for (const char *file)
1683 dir = get_parent (file, &dir_dev);
1687 return find_mountpoint_for (dir, dir_dev);
1691 get_unique_filename (const char *basename,
1697 return g_strdup (basename);
1699 dot = strchr (basename, '.');
1701 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1703 return g_strdup_printf ("%s.%d", basename, id);
1707 path_has_prefix (const char *path,
1715 prefix_len = strlen (prefix);
1717 if (strncmp (path, prefix, prefix_len) == 0 &&
1718 (prefix_len == 0 || /* empty prefix always matches */
1719 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1720 path[prefix_len] == 0 ||
1721 path[prefix_len] == '/'))
1728 try_make_relative (const char *path,
1731 char *path2, *base2;
1734 path2 = expand_all_symlinks (path);
1735 base2 = expand_all_symlinks (base);
1738 if (path_has_prefix (path2, base2))
1740 relative = path2 + strlen (base2);
1741 while (*relative == '/')
1743 relative = g_strdup (relative);
1751 /* Failed, use abs path */
1752 return g_strdup (path);
1756 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1758 static gsize home_dev_set = 0;
1759 static dev_t home_dev;
1760 char *topdir, *globaldir, *trashdir, *tmpname;
1763 GStatBuf global_stat, trash_stat;
1766 if (g_once_init_enter (&home_dev_set))
1770 g_stat (g_get_home_dir (), &home_stat);
1771 home_dev = home_stat.st_dev;
1772 g_once_init_leave (&home_dev_set, 1);
1775 /* Assume we can trash to the home */
1776 if (dir_dev == home_dev)
1779 topdir = find_mountpoint_for (dirname, dir_dev);
1783 globaldir = g_build_filename (topdir, ".Trash", NULL);
1784 if (g_lstat (globaldir, &global_stat) == 0 &&
1785 S_ISDIR (global_stat.st_mode) &&
1786 (global_stat.st_mode & S_ISVTX) != 0)
1788 /* got a toplevel sysadmin created dir, assume we
1789 * can trash to it (we should be able to create a dir)
1790 * This fails for the FAT case where the ownership of
1791 * that dir would be wrong though..
1799 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1801 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1803 tmpname = g_strdup_printf (".Trash-%s", uid_str);
1804 trashdir = g_build_filename (topdir, tmpname, NULL);
1807 if (g_lstat (trashdir, &trash_stat) == 0)
1811 return S_ISDIR (trash_stat.st_mode) &&
1812 trash_stat.st_uid == uid;
1816 /* User specific trash didn't exist, can we create it? */
1817 res = g_access (topdir, W_OK) == 0;
1825 _g_local_file_is_lost_found_dir (const char *path, dev_t path_dev)
1827 gboolean ret = FALSE;
1828 gchar *mount_dir = NULL;
1829 size_t mount_dir_len;
1832 if (!g_str_has_suffix (path, "/lost+found"))
1835 mount_dir = find_mountpoint_for (path, path_dev);
1836 if (mount_dir == NULL)
1839 mount_dir_len = strlen (mount_dir);
1840 /* We special-case rootfs ('/') since it's the only case where
1841 * mount_dir ends in '/'
1843 if (mount_dir_len == 1)
1845 if (mount_dir_len + strlen ("/lost+found") != strlen (path))
1848 if (g_lstat (path, &statbuf) != 0)
1851 if (!(S_ISDIR (statbuf.st_mode) &&
1852 statbuf.st_uid == 0 &&
1853 statbuf.st_gid == 0))
1865 g_local_file_trash (GFile *file,
1866 GCancellable *cancellable,
1869 GLocalFile *local = G_LOCAL_FILE (file);
1870 GStatBuf file_stat, home_stat;
1871 const char *homedir;
1872 char *trashdir, *topdir, *infodir, *filesdir;
1873 char *basename, *trashname, *trashfile, *infoname, *infofile;
1874 char *original_name, *original_name_escaped;
1877 gboolean is_homedir_trash;
1878 char delete_time[32];
1880 GStatBuf trash_stat, global_stat;
1881 char *dirname, *globaldir;
1885 if (g_lstat (local->filename, &file_stat) != 0)
1889 g_set_error (error, G_IO_ERROR,
1890 g_io_error_from_errno (errsv),
1891 _("Error trashing file: %s"),
1892 g_strerror (errsv));
1896 homedir = g_get_home_dir ();
1897 g_stat (homedir, &home_stat);
1899 is_homedir_trash = FALSE;
1901 if (file_stat.st_dev == home_stat.st_dev)
1903 is_homedir_trash = TRUE;
1905 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1906 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1911 display_name = g_filename_display_name (trashdir);
1912 g_set_error (error, G_IO_ERROR,
1913 g_io_error_from_errno (errsv),
1914 _("Unable to create trash dir %s: %s"),
1915 display_name, g_strerror (errsv));
1916 g_free (display_name);
1920 topdir = g_strdup (g_get_user_data_dir ());
1928 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1930 topdir = find_topdir_for (local->filename);
1933 g_set_error_literal (error, G_IO_ERROR,
1934 G_IO_ERROR_NOT_SUPPORTED,
1935 _("Unable to find toplevel directory for trash"));
1939 /* Try looking for global trash dir $topdir/.Trash/$uid */
1940 globaldir = g_build_filename (topdir, ".Trash", NULL);
1941 if (g_lstat (globaldir, &global_stat) == 0 &&
1942 S_ISDIR (global_stat.st_mode) &&
1943 (global_stat.st_mode & S_ISVTX) != 0)
1945 trashdir = g_build_filename (globaldir, uid_str, NULL);
1947 if (g_lstat (trashdir, &trash_stat) == 0)
1949 if (!S_ISDIR (trash_stat.st_mode) ||
1950 trash_stat.st_uid != uid)
1952 /* Not a directory or not owned by user, ignore */
1957 else if (g_mkdir (trashdir, 0700) == -1)
1965 if (trashdir == NULL)
1967 gboolean tried_create;
1969 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1970 dirname = g_strdup_printf (".Trash-%s", uid_str);
1971 trashdir = g_build_filename (topdir, dirname, NULL);
1974 tried_create = FALSE;
1977 if (g_lstat (trashdir, &trash_stat) == 0)
1979 if (!S_ISDIR (trash_stat.st_mode) ||
1980 trash_stat.st_uid != uid)
1982 /* Remove the failed directory */
1984 g_remove (trashdir);
1986 /* Not a directory or not owned by user, ignore */
1993 if (!tried_create &&
1994 g_mkdir (trashdir, 0700) != -1)
1996 /* Ensure that the created dir has the right uid etc.
1997 This might fail on e.g. a FAT dir */
1998 tried_create = TRUE;
2009 if (trashdir == NULL)
2012 g_set_error_literal (error, G_IO_ERROR,
2013 G_IO_ERROR_NOT_SUPPORTED,
2014 _("Unable to find or create trash directory"));
2019 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
2021 infodir = g_build_filename (trashdir, "info", NULL);
2022 filesdir = g_build_filename (trashdir, "files", NULL);
2025 /* Make sure we have the subdirectories */
2026 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
2027 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
2032 g_set_error_literal (error, G_IO_ERROR,
2033 G_IO_ERROR_NOT_SUPPORTED,
2034 _("Unable to find or create trash directory"));
2038 basename = g_path_get_basename (local->filename);
2046 trashname = get_unique_filename (basename, i++);
2047 infoname = g_strconcat (trashname, ".trashinfo", NULL);
2048 infofile = g_build_filename (infodir, infoname, NULL);
2051 fd = g_open (infofile, O_CREAT | O_EXCL, 0666);
2052 } while (fd == -1 && errno == EEXIST);
2066 g_set_error (error, G_IO_ERROR,
2067 g_io_error_from_errno (errsv),
2068 _("Unable to create trashing info file: %s"),
2069 g_strerror (errsv));
2073 (void) g_close (fd, NULL);
2075 /* TODO: Maybe we should verify that you can delete the file from the trash
2076 before moving it? OTOH, that is hard, as it needs a recursive scan */
2078 trashfile = g_build_filename (filesdir, trashname, NULL);
2082 if (g_rename (local->filename, trashfile) == -1)
2086 g_unlink (infofile);
2094 /* The trash dir was actually on another fs anyway!?
2095 This can happen when the same device is mounted multiple
2096 times, or with bind mounts of the same fs. */
2097 g_set_error (error, G_IO_ERROR,
2098 G_IO_ERROR_NOT_SUPPORTED,
2099 _("Unable to trash file: %s"),
2100 g_strerror (errsv));
2102 g_set_error (error, G_IO_ERROR,
2103 g_io_error_from_errno (errsv),
2104 _("Unable to trash file: %s"),
2105 g_strerror (errsv));
2109 vfs = g_vfs_get_default ();
2110 class = G_VFS_GET_CLASS (vfs);
2111 if (class->local_file_moved)
2112 class->local_file_moved (vfs, local->filename, trashfile);
2116 /* TODO: Do we need to update mtime/atime here after the move? */
2118 /* Use absolute names for homedir */
2119 if (is_homedir_trash)
2120 original_name = g_strdup (local->filename);
2122 original_name = try_make_relative (local->filename, topdir);
2123 original_name_escaped = g_uri_escape_string (original_name, "/", FALSE);
2125 g_free (original_name);
2132 localtime_r (&t, &now);
2134 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
2137 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2138 original_name_escaped, delete_time);
2140 g_file_set_contents (infofile, data, -1, NULL);
2144 g_free (original_name_escaped);
2149 #else /* G_OS_WIN32 */
2151 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
2153 return FALSE; /* XXX ??? */
2157 g_local_file_trash (GFile *file,
2158 GCancellable *cancellable,
2161 GLocalFile *local = G_LOCAL_FILE (file);
2162 SHFILEOPSTRUCTW op = {0};
2167 wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
2168 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2169 wfilename = g_renew (wchar_t, wfilename, len + 2);
2170 wfilename[len + 1] = 0;
2172 op.wFunc = FO_DELETE;
2173 op.pFrom = wfilename;
2174 op.fFlags = FOF_ALLOWUNDO;
2176 success = SHFileOperationW (&op) == 0;
2178 if (success && op.fAnyOperationsAborted)
2180 if (cancellable && !g_cancellable_is_cancelled (cancellable))
2181 g_cancellable_cancel (cancellable);
2182 g_set_error (error, G_IO_ERROR,
2183 G_IO_ERROR_CANCELLED,
2184 _("Unable to trash file: %s"),
2185 _("Operation was cancelled"));
2189 g_set_error (error, G_IO_ERROR,
2191 _("Unable to trash file: %s"),
2192 _("internal error"));
2197 #endif /* G_OS_WIN32 */
2200 g_local_file_make_directory (GFile *file,
2201 GCancellable *cancellable,
2204 GLocalFile *local = G_LOCAL_FILE (file);
2206 if (g_mkdir (local->filename, 0777) == -1)
2210 if (errsv == EINVAL)
2211 /* This must be an invalid filename, on e.g. FAT */
2212 g_set_error_literal (error, G_IO_ERROR,
2213 G_IO_ERROR_INVALID_FILENAME,
2214 _("Invalid filename"));
2216 g_set_error (error, G_IO_ERROR,
2217 g_io_error_from_errno (errsv),
2218 _("Error creating directory: %s"),
2219 g_strerror (errsv));
2227 g_local_file_make_symbolic_link (GFile *file,
2228 const char *symlink_value,
2229 GCancellable *cancellable,
2233 GLocalFile *local = G_LOCAL_FILE (file);
2235 if (symlink (symlink_value, local->filename) == -1)
2239 if (errsv == EINVAL)
2240 /* This must be an invalid filename, on e.g. FAT */
2241 g_set_error_literal (error, G_IO_ERROR,
2242 G_IO_ERROR_INVALID_FILENAME,
2243 _("Invalid filename"));
2244 else if (errsv == EPERM)
2245 g_set_error (error, G_IO_ERROR,
2246 G_IO_ERROR_NOT_SUPPORTED,
2247 _("Filesystem does not support symbolic links"));
2249 g_set_error (error, G_IO_ERROR,
2250 g_io_error_from_errno (errsv),
2251 _("Error making symbolic link: %s"),
2252 g_strerror (errsv));
2257 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
2264 g_local_file_copy (GFile *source,
2266 GFileCopyFlags flags,
2267 GCancellable *cancellable,
2268 GFileProgressCallback progress_callback,
2269 gpointer progress_callback_data,
2272 /* Fall back to default copy */
2273 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
2278 g_local_file_move (GFile *source,
2280 GFileCopyFlags flags,
2281 GCancellable *cancellable,
2282 GFileProgressCallback progress_callback,
2283 gpointer progress_callback_data,
2286 GLocalFile *local_source, *local_destination;
2288 gboolean destination_exist, source_is_dir;
2295 if (!G_IS_LOCAL_FILE (source) ||
2296 !G_IS_LOCAL_FILE (destination))
2298 /* Fall back to default move */
2299 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2303 local_source = G_LOCAL_FILE (source);
2304 local_destination = G_LOCAL_FILE (destination);
2306 res = g_lstat (local_source->filename, &statbuf);
2311 g_set_error (error, G_IO_ERROR,
2312 g_io_error_from_errno (errsv),
2313 _("Error moving file: %s"),
2314 g_strerror (errsv));
2318 source_is_dir = S_ISDIR (statbuf.st_mode);
2319 source_size = statbuf.st_size;
2321 destination_exist = FALSE;
2322 res = g_lstat (local_destination->filename, &statbuf);
2325 destination_exist = TRUE; /* Target file exists */
2327 if (flags & G_FILE_COPY_OVERWRITE)
2329 /* Always fail on dirs, even with overwrite */
2330 if (S_ISDIR (statbuf.st_mode))
2333 g_set_error_literal (error,
2335 G_IO_ERROR_WOULD_MERGE,
2336 _("Can't move directory over directory"));
2338 g_set_error_literal (error,
2340 G_IO_ERROR_IS_DIRECTORY,
2341 _("Can't copy over directory"));
2347 g_set_error_literal (error,
2350 _("Target file exists"));
2355 if (flags & G_FILE_COPY_BACKUP && destination_exist)
2357 backup_name = g_strconcat (local_destination->filename, "~", NULL);
2358 if (g_rename (local_destination->filename, backup_name) == -1)
2360 g_set_error_literal (error,
2362 G_IO_ERROR_CANT_CREATE_BACKUP,
2363 _("Backup file creation failed"));
2364 g_free (backup_name);
2367 g_free (backup_name);
2368 destination_exist = FALSE; /* It did, but no more */
2371 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2373 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2374 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2375 res = g_unlink (local_destination->filename);
2380 g_set_error (error, G_IO_ERROR,
2381 g_io_error_from_errno (errsv),
2382 _("Error removing target file: %s"),
2383 g_strerror (errsv));
2388 if (g_rename (local_source->filename, local_destination->filename) == -1)
2393 /* This will cause the fallback code to run */
2394 g_set_error_literal (error, G_IO_ERROR,
2395 G_IO_ERROR_NOT_SUPPORTED,
2396 _("Move between mounts not supported"));
2397 else if (errsv == EINVAL)
2398 /* This must be an invalid filename, on e.g. FAT, or
2399 we're trying to move the file into itself...
2400 We return invalid filename for both... */
2401 g_set_error_literal (error, G_IO_ERROR,
2402 G_IO_ERROR_INVALID_FILENAME,
2403 _("Invalid filename"));
2405 g_set_error (error, G_IO_ERROR,
2406 g_io_error_from_errno (errsv),
2407 _("Error moving file: %s"),
2408 g_strerror (errsv));
2412 vfs = g_vfs_get_default ();
2413 class = G_VFS_GET_CLASS (vfs);
2414 if (class->local_file_moved)
2415 class->local_file_moved (vfs, local_source->filename, local_destination->filename);
2417 /* Make sure we send full copied size */
2418 if (progress_callback)
2419 progress_callback (source_size, source_size, progress_callback_data);
2427 is_remote (const gchar *filename)
2435 is_remote_fs (const gchar *filename)
2437 const char *fsname = NULL;
2440 struct statfs statfs_buffer;
2441 int statfs_result = 0;
2443 #if STATFS_ARGS == 2
2444 statfs_result = statfs (filename, &statfs_buffer);
2445 #elif STATFS_ARGS == 4
2446 statfs_result = statfs (filename, &statfs_buffer, sizeof (statfs_buffer), 0);
2449 #elif defined(USE_STATVFS)
2450 struct statvfs statfs_buffer;
2451 int statfs_result = 0;
2453 statfs_result = statvfs (filename, &statfs_buffer);
2458 if (statfs_result == -1)
2462 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
2463 fsname = statfs_buffer.f_fstypename;
2465 fsname = get_fs_type (statfs_buffer.f_type);
2468 #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
2469 fsname = statfs_buffer.f_basetype;
2474 if (strcmp (fsname, "nfs") == 0)
2476 if (strcmp (fsname, "nfs4") == 0)
2484 is_remote (const gchar *filename)
2486 static gboolean remote_home;
2487 static gsize initialized;
2490 home = g_get_home_dir ();
2491 if (path_has_prefix (filename, home))
2493 if (g_once_init_enter (&initialized))
2495 remote_home = is_remote_fs (home);
2496 g_once_init_leave (&initialized, TRUE);
2503 #endif /* !G_OS_WIN32 */
2505 static GFileMonitor*
2506 g_local_file_monitor_dir (GFile *file,
2507 GFileMonitorFlags flags,
2508 GCancellable *cancellable,
2511 GLocalFile* local_file = G_LOCAL_FILE(file);
2512 return _g_local_directory_monitor_new (local_file->filename, flags, NULL, is_remote (local_file->filename), TRUE, error);
2515 static GFileMonitor*
2516 g_local_file_monitor_file (GFile *file,
2517 GFileMonitorFlags flags,
2518 GCancellable *cancellable,
2521 GLocalFile* local_file = G_LOCAL_FILE(file);
2522 return _g_local_file_monitor_new (local_file->filename, flags, NULL, is_remote (local_file->filename), TRUE, error);
2525 GLocalDirectoryMonitor *
2526 g_local_directory_monitor_new_in_worker (const char *pathname,
2527 GFileMonitorFlags flags,
2530 return (gpointer) _g_local_directory_monitor_new (pathname, flags,
2531 GLIB_PRIVATE_CALL (g_get_worker_context) (),
2532 is_remote (pathname), FALSE, error);
2536 g_local_file_monitor_new_in_worker (const char *pathname,
2537 GFileMonitorFlags flags,
2540 return (gpointer) _g_local_file_monitor_new (pathname, flags,
2541 GLIB_PRIVATE_CALL (g_get_worker_context) (),
2542 is_remote (pathname), FALSE, error);
2546 /* Here is the GLocalFile implementation of g_file_measure_disk_usage().
2548 * If available, we use fopenat() in preference to filenames for
2549 * efficiency and safety reasons. We know that fopenat() is available
2550 * based on if AT_FDCWD is defined. POSIX guarantees that this will be
2551 * defined as a macro.
2553 * We use a linked list of stack-allocated GSList nodes in order to be
2554 * able to reconstruct the filename for error messages. We actually
2555 * pass the filename to operate on through the top node of the list.
2557 * In case we're using openat(), this top filename will be a basename
2558 * which should be opened in the directory which has also had its fd
2559 * passed along. If we're not using openat() then it will be a full
2560 * absolute filename.
2564 g_local_file_measure_size_error (GFileMeasureFlags flags,
2569 /* Only report an error if we were at the toplevel or if the caller
2570 * requested reporting of all errors.
2572 if ((name->next == NULL) || (flags & G_FILE_MEASURE_REPORT_ANY_ERROR))
2577 /* Skip some work if there is no error return */
2582 /* If using openat() we need to rebuild the filename for the message */
2583 filename = g_string_new (name->data);
2584 for (node = name->next; node; node = node->next)
2588 g_string_prepend_c (filename, G_DIR_SEPARATOR);
2589 utf8 = g_filename_display_name (node->data);
2590 g_string_prepend (filename, utf8);
2597 /* Otherwise, we already have it, so just use it. */
2599 filename = g_string_new (NULL);
2600 utf8 = g_filename_display_name (node->data);
2601 g_string_append (filename, utf8);
2606 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (saved_errno),
2607 _("Could not determine the disk usage of %s: %s"),
2608 filename->str, g_strerror (saved_errno));
2610 g_string_free (filename, TRUE);
2616 /* We're not reporting this error... */
2622 GFileMeasureFlags flags;
2624 GCancellable *cancellable;
2626 GFileMeasureProgressCallback progress_callback;
2627 gpointer progress_data;
2633 guint64 last_progress_report;
2637 g_local_file_measure_size_of_contents (gint fd,
2639 MeasureState *state,
2643 g_local_file_measure_size_of_file (gint parent_fd,
2645 MeasureState *state,
2650 if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
2653 #if defined (AT_FDCWD)
2654 if (fstatat (parent_fd, name->data, &buf, AT_SYMLINK_NOFOLLOW) != 0)
2656 if (g_lstat (name->data, &buf) != 0)
2658 return g_local_file_measure_size_error (state->flags, errno, name, error);
2662 /* If not at the toplevel, check for a device boundary. */
2664 if (state->flags & G_FILE_MEASURE_NO_XDEV)
2665 if (state->contained_on != buf.st_dev)
2670 /* If, however, this is the toplevel, set the device number so
2671 * that recursive invocations can compare against it.
2673 state->contained_on = buf.st_dev;
2676 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
2677 if (~state->flags & G_FILE_MEASURE_APPARENT_SIZE)
2678 state->disk_usage += buf.st_blocks * G_GUINT64_CONSTANT (512);
2681 state->disk_usage += buf.st_size;
2683 if (S_ISDIR (buf.st_mode))
2688 if (state->progress_callback)
2690 /* We could attempt to do some cleverness here in order to avoid
2691 * calling clock_gettime() so much, but we're doing stats and opens
2692 * all over the place already...
2694 if (state->last_progress_report)
2698 now = g_get_monotonic_time ();
2700 if (state->last_progress_report + 200 * G_TIME_SPAN_MILLISECOND < now)
2702 (* state->progress_callback) (TRUE,
2703 state->disk_usage, state->num_dirs, state->num_files,
2704 state->progress_data);
2705 state->last_progress_report = now;
2710 /* We must do an initial report to inform that more reports
2713 (* state->progress_callback) (TRUE, 0, 0, 0, state->progress_data);
2714 state->last_progress_report = g_get_monotonic_time ();
2718 if (S_ISDIR (buf.st_mode))
2722 if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
2726 #ifdef HAVE_OPEN_O_DIRECTORY
2727 dir_fd = openat (parent_fd, name->data, O_RDONLY|O_DIRECTORY);
2729 dir_fd = openat (parent_fd, name->data, O_RDONLY);
2732 return g_local_file_measure_size_error (state->flags, errno, name, error);
2735 if (!g_local_file_measure_size_of_contents (dir_fd, name, state, error))
2743 g_local_file_measure_size_of_contents (gint fd,
2745 MeasureState *state,
2748 gboolean success = TRUE;
2754 /* If this fails, we want to preserve the errno from fopendir() */
2756 dirp = fdopendir (fd);
2757 dir = dirp ? GLIB_PRIVATE_CALL(g_dir_new_from_dirp) (dirp) : NULL;
2760 dir = GLIB_PRIVATE_CALL(g_dir_open_with_errno) (dir_name->data, 0);
2765 gint saved_errno = errno;
2771 return g_local_file_measure_size_error (state->flags, saved_errno, dir_name, error);
2774 while (success && (name = g_dir_read_name (dir)))
2778 node.next = dir_name;
2780 node.data = (gchar *) name;
2782 node.data = g_build_filename (dir_name->data, name, NULL);
2785 success = g_local_file_measure_size_of_file (fd, &node, state, error);
2798 g_local_file_measure_disk_usage (GFile *file,
2799 GFileMeasureFlags flags,
2800 GCancellable *cancellable,
2801 GFileMeasureProgressCallback progress_callback,
2802 gpointer progress_data,
2803 guint64 *disk_usage,
2808 GLocalFile *local_file = G_LOCAL_FILE (file);
2809 MeasureState state = { 0, };
2813 state.flags = flags;
2814 state.cancellable = cancellable;
2815 state.progress_callback = progress_callback;
2816 state.progress_data = progress_data;
2822 node.data = local_file->filename;
2825 if (!g_local_file_measure_size_of_file (root_fd, &node, &state, error))
2829 *disk_usage = state.disk_usage;
2832 *num_dirs = state.num_dirs;
2835 *num_files = state.num_files;
2841 g_local_file_file_iface_init (GFileIface *iface)
2843 iface->dup = g_local_file_dup;
2844 iface->hash = g_local_file_hash;
2845 iface->equal = g_local_file_equal;
2846 iface->is_native = g_local_file_is_native;
2847 iface->has_uri_scheme = g_local_file_has_uri_scheme;
2848 iface->get_uri_scheme = g_local_file_get_uri_scheme;
2849 iface->get_basename = g_local_file_get_basename;
2850 iface->get_path = g_local_file_get_path;
2851 iface->get_uri = g_local_file_get_uri;
2852 iface->get_parse_name = g_local_file_get_parse_name;
2853 iface->get_parent = g_local_file_get_parent;
2854 iface->prefix_matches = g_local_file_prefix_matches;
2855 iface->get_relative_path = g_local_file_get_relative_path;
2856 iface->resolve_relative_path = g_local_file_resolve_relative_path;
2857 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2858 iface->set_display_name = g_local_file_set_display_name;
2859 iface->enumerate_children = g_local_file_enumerate_children;
2860 iface->query_info = g_local_file_query_info;
2861 iface->query_filesystem_info = g_local_file_query_filesystem_info;
2862 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2863 iface->query_settable_attributes = g_local_file_query_settable_attributes;
2864 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2865 iface->set_attribute = g_local_file_set_attribute;
2866 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2867 iface->read_fn = g_local_file_read;
2868 iface->append_to = g_local_file_append_to;
2869 iface->create = g_local_file_create;
2870 iface->replace = g_local_file_replace;
2871 iface->open_readwrite = g_local_file_open_readwrite;
2872 iface->create_readwrite = g_local_file_create_readwrite;
2873 iface->replace_readwrite = g_local_file_replace_readwrite;
2874 iface->delete_file = g_local_file_delete;
2875 iface->trash = g_local_file_trash;
2876 iface->make_directory = g_local_file_make_directory;
2877 iface->make_symbolic_link = g_local_file_make_symbolic_link;
2878 iface->copy = g_local_file_copy;
2879 iface->move = g_local_file_move;
2880 iface->monitor_dir = g_local_file_monitor_dir;
2881 iface->monitor_file = g_local_file_monitor_file;
2882 iface->measure_disk_usage = g_local_file_measure_disk_usage;
2884 iface->supports_thread_contexts = TRUE;