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"
71 #include "glib-private.h"
73 #include "glib-private.h"
80 #ifndef FILE_READ_ONLY_VOLUME
81 #define FILE_READ_ONLY_VOLUME 0x00080000
85 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
88 #define S_ISLNK(m) (0)
93 static void g_local_file_file_iface_init (GFileIface *iface);
95 static GFileAttributeInfoList *local_writable_attributes = NULL;
96 static /* GFileAttributeInfoList * */ gsize local_writable_namespaces = 0;
100 GObject parent_instance;
105 #define g_local_file_get_type _g_local_file_get_type
106 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
107 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
108 g_local_file_file_iface_init))
110 static char *find_mountpoint_for (const char *file, dev_t dev);
113 g_local_file_finalize (GObject *object)
117 local = G_LOCAL_FILE (object);
119 g_free (local->filename);
121 G_OBJECT_CLASS (g_local_file_parent_class)->finalize (object);
125 g_local_file_class_init (GLocalFileClass *klass)
127 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
128 GFileAttributeInfoList *list;
130 gobject_class->finalize = g_local_file_finalize;
132 /* Set up attribute lists */
134 /* Writable attributes: */
136 list = g_file_attribute_info_list_new ();
138 g_file_attribute_info_list_add (list,
139 G_FILE_ATTRIBUTE_UNIX_MODE,
140 G_FILE_ATTRIBUTE_TYPE_UINT32,
141 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
142 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
145 g_file_attribute_info_list_add (list,
146 G_FILE_ATTRIBUTE_UNIX_UID,
147 G_FILE_ATTRIBUTE_TYPE_UINT32,
148 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
149 g_file_attribute_info_list_add (list,
150 G_FILE_ATTRIBUTE_UNIX_GID,
151 G_FILE_ATTRIBUTE_TYPE_UINT32,
152 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
156 g_file_attribute_info_list_add (list,
157 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
158 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
163 g_file_attribute_info_list_add (list,
164 G_FILE_ATTRIBUTE_TIME_MODIFIED,
165 G_FILE_ATTRIBUTE_TYPE_UINT64,
166 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
167 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
168 g_file_attribute_info_list_add (list,
169 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
170 G_FILE_ATTRIBUTE_TYPE_UINT32,
171 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
172 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
173 /* When copying, the target file is accessed. Replicating
174 * the source access time does not make sense in this case.
176 g_file_attribute_info_list_add (list,
177 G_FILE_ATTRIBUTE_TIME_ACCESS,
178 G_FILE_ATTRIBUTE_TYPE_UINT64,
179 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
180 g_file_attribute_info_list_add (list,
181 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
182 G_FILE_ATTRIBUTE_TYPE_UINT32,
183 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
186 local_writable_attributes = list;
190 g_local_file_init (GLocalFile *local)
195 _g_local_file_get_filename (GLocalFile *file)
197 return file->filename;
201 canonicalize_filename (const char *filename)
203 char *canon, *start, *p, *q;
207 if (!g_path_is_absolute (filename))
209 cwd = g_get_current_dir ();
210 canon = g_build_filename (cwd, filename, NULL);
214 canon = g_strdup (filename);
216 start = (char *)g_path_skip_root (canon);
220 /* This shouldn't really happen, as g_get_current_dir() should
221 return an absolute pathname, but bug 573843 shows this is
222 not always happening */
224 return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
227 /* POSIX allows double slashes at the start to
228 * mean something special (as does windows too).
229 * So, "//" != "/", but more than two slashes
235 G_IS_DIR_SEPARATOR (*p);
242 memmove (start, start+i, strlen (start+i)+1);
245 /* Make sure we're using the canonical dir separator */
247 while (p < start && G_IS_DIR_SEPARATOR (*p))
248 *p++ = G_DIR_SEPARATOR;
253 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
255 memmove (p, p+1, strlen (p+1)+1);
257 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
260 /* Skip previous separator */
264 while (p > start && !G_IS_DIR_SEPARATOR (*p))
266 if (G_IS_DIR_SEPARATOR (*p))
267 *p++ = G_DIR_SEPARATOR;
268 memmove (p, q, strlen (q)+1);
272 /* Skip until next separator */
273 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
278 /* Canonicalize one separator */
279 *p++ = G_DIR_SEPARATOR;
283 /* Remove additional separators */
285 while (*q && G_IS_DIR_SEPARATOR (*q))
289 memmove (p, q, strlen (q)+1);
292 /* Remove trailing slashes */
293 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
300 _g_local_file_new (const char *filename)
304 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
305 local->filename = canonicalize_filename (filename);
307 return G_FILE (local);
311 g_local_file_is_native (GFile *file)
317 g_local_file_has_uri_scheme (GFile *file,
318 const char *uri_scheme)
320 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
324 g_local_file_get_uri_scheme (GFile *file)
326 return g_strdup ("file");
330 g_local_file_get_basename (GFile *file)
332 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
336 g_local_file_get_path (GFile *file)
338 return g_strdup (G_LOCAL_FILE (file)->filename);
342 g_local_file_get_uri (GFile *file)
344 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
348 get_filename_charset (const gchar **filename_charset)
350 const gchar **charsets;
353 is_utf8 = g_get_filename_charsets (&charsets);
355 if (filename_charset)
356 *filename_charset = charsets[0];
362 name_is_valid_for_display (const char *string,
363 gboolean is_valid_utf8)
367 if (!is_valid_utf8 &&
368 !g_utf8_validate (string, -1, NULL))
371 while ((c = *string++) != 0)
373 if (g_ascii_iscntrl (c))
381 g_local_file_get_parse_name (GFile *file)
383 const char *filename;
385 const gchar *charset;
387 char *roundtripped_filename;
388 gboolean free_utf8_filename;
389 gboolean is_valid_utf8;
392 filename = G_LOCAL_FILE (file)->filename;
393 if (get_filename_charset (&charset))
395 utf8_filename = (char *)filename;
396 free_utf8_filename = FALSE;
397 is_valid_utf8 = FALSE; /* Can't guarantee this */
401 utf8_filename = g_convert (filename, -1,
402 "UTF-8", charset, NULL, NULL, NULL);
403 free_utf8_filename = TRUE;
404 is_valid_utf8 = TRUE;
406 if (utf8_filename != NULL)
408 /* Make sure we can roundtrip: */
409 roundtripped_filename = g_convert (utf8_filename, -1,
410 charset, "UTF-8", NULL, NULL, NULL);
412 if (roundtripped_filename == NULL ||
413 strcmp (filename, roundtripped_filename) != 0)
415 g_free (utf8_filename);
416 utf8_filename = NULL;
419 g_free (roundtripped_filename);
423 if (utf8_filename != NULL &&
424 name_is_valid_for_display (utf8_filename, is_valid_utf8))
426 if (free_utf8_filename)
427 parse_name = utf8_filename;
429 parse_name = g_strdup (utf8_filename);
434 char *dup_filename, *p, *backslash;
436 /* Turn backslashes into forward slashes like
437 * g_filename_to_uri() would do (but we can't use that because
438 * it doesn't output IRIs).
440 dup_filename = g_strdup (filename);
441 filename = p = dup_filename;
443 while ((backslash = strchr (p, '\\')) != NULL)
450 escaped_path = g_uri_escape_string (filename,
451 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
453 parse_name = g_strconcat ("file://",
454 (*escaped_path != '/') ? "/" : "",
458 g_free (escaped_path);
460 g_free (dup_filename);
462 if (free_utf8_filename)
463 g_free (utf8_filename);
470 g_local_file_get_parent (GFile *file)
472 GLocalFile *local = G_LOCAL_FILE (file);
473 const char *non_root;
478 non_root = g_path_skip_root (local->filename);
482 dirname = g_path_get_dirname (local->filename);
483 parent = _g_local_file_new (dirname);
489 g_local_file_dup (GFile *file)
491 GLocalFile *local = G_LOCAL_FILE (file);
493 return _g_local_file_new (local->filename);
497 g_local_file_hash (GFile *file)
499 GLocalFile *local = G_LOCAL_FILE (file);
501 return g_str_hash (local->filename);
505 g_local_file_equal (GFile *file1,
508 GLocalFile *local1 = G_LOCAL_FILE (file1);
509 GLocalFile *local2 = G_LOCAL_FILE (file2);
511 return g_str_equal (local1->filename, local2->filename);
515 match_prefix (const char *path,
520 prefix_len = strlen (prefix);
521 if (strncmp (path, prefix, prefix_len) != 0)
524 /* Handle the case where prefix is the root, so that
525 * the IS_DIR_SEPRARATOR check below works */
526 if (prefix_len > 0 &&
527 G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
530 return path + prefix_len;
534 g_local_file_prefix_matches (GFile *parent,
537 GLocalFile *parent_local = G_LOCAL_FILE (parent);
538 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
539 const char *remainder;
541 remainder = match_prefix (descendant_local->filename, parent_local->filename);
542 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
548 g_local_file_get_relative_path (GFile *parent,
551 GLocalFile *parent_local = G_LOCAL_FILE (parent);
552 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
553 const char *remainder;
555 remainder = match_prefix (descendant_local->filename, parent_local->filename);
557 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
558 return g_strdup (remainder + 1);
563 g_local_file_resolve_relative_path (GFile *file,
564 const char *relative_path)
566 GLocalFile *local = G_LOCAL_FILE (file);
570 if (g_path_is_absolute (relative_path))
571 return _g_local_file_new (relative_path);
573 filename = g_build_filename (local->filename, relative_path, NULL);
574 child = _g_local_file_new (filename);
580 static GFileEnumerator *
581 g_local_file_enumerate_children (GFile *file,
582 const char *attributes,
583 GFileQueryInfoFlags flags,
584 GCancellable *cancellable,
587 GLocalFile *local = G_LOCAL_FILE (file);
588 return _g_local_file_enumerator_new (local,
594 g_local_file_get_child_for_display_name (GFile *file,
595 const char *display_name,
601 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
602 if (basename == NULL)
604 g_set_error (error, G_IO_ERROR,
605 G_IO_ERROR_INVALID_FILENAME,
606 _("Invalid filename %s"), display_name);
610 new_file = g_file_get_child (file, basename);
616 #if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
618 get_fs_type (long f_type)
620 /* filesystem ids taken from linux manpage */
727 G_LOCK_DEFINE_STATIC(mount_info_hash);
728 static GHashTable *mount_info_hash = NULL;
729 static guint64 mount_info_hash_cache_time = 0;
732 MOUNT_INFO_READONLY = 1<<0
736 device_equal (gconstpointer v1,
739 return *(dev_t *)v1 == *(dev_t *)v2;
743 device_hash (gconstpointer v)
745 return (guint) *(dev_t *)v;
749 get_mount_info (GFileInfo *fs_info,
751 GFileAttributeMatcher *matcher)
755 gpointer info_as_ptr;
759 GUnixMountEntry *mount;
762 if (g_lstat (path, &buf) != 0)
765 G_LOCK (mount_info_hash);
767 if (mount_info_hash == NULL)
768 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
772 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
773 g_hash_table_remove_all (mount_info_hash);
775 got_info = g_hash_table_lookup_extended (mount_info_hash,
780 G_UNLOCK (mount_info_hash);
782 mount_info = GPOINTER_TO_UINT (info_as_ptr);
788 mountpoint = find_mountpoint_for (path, buf.st_dev);
789 if (mountpoint == NULL)
790 mountpoint = g_strdup ("/");
792 mount = g_unix_mount_at (mountpoint, &cache_time);
795 if (g_unix_mount_is_readonly (mount))
796 mount_info |= MOUNT_INFO_READONLY;
798 g_unix_mount_free (mount);
803 dev = g_new0 (dev_t, 1);
806 G_LOCK (mount_info_hash);
807 mount_info_hash_cache_time = cache_time;
808 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
809 G_UNLOCK (mount_info_hash);
812 if (mount_info & MOUNT_INFO_READONLY)
813 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
821 is_xp_or_later (void)
823 static int result = -1;
828 OSVERSIONINFOEX ver_info = {0};
829 DWORDLONG cond_mask = 0;
830 int op = VER_GREATER_EQUAL;
832 ver_info.dwOSVersionInfoSize = sizeof ver_info;
833 ver_info.dwMajorVersion = 5;
834 ver_info.dwMinorVersion = 1;
836 VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
837 VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
839 result = VerifyVersionInfo (&ver_info,
840 VER_MAJORVERSION | VER_MINORVERSION,
843 result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;
851 get_volume_for_path (const char *path)
857 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
858 result = g_new (wchar_t, MAX_PATH);
860 if (!GetVolumePathNameW (wpath, result, MAX_PATH))
862 char *msg = g_win32_error_message (GetLastError ());
863 g_critical ("GetVolumePathName failed: %s", msg);
870 len = wcslen (result);
871 if (len > 0 && result[len-1] != L'\\')
873 result = g_renew (wchar_t, result, len + 2);
883 find_mountpoint_for (const char *file, dev_t dev)
888 wpath = get_volume_for_path (file);
892 utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
899 get_filesystem_readonly (GFileInfo *info,
904 rootdir = get_volume_for_path (path);
908 if (is_xp_or_later ())
911 if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
912 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
913 (flags & FILE_READ_ONLY_VOLUME) != 0);
917 if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
918 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
925 #endif /* G_OS_WIN32 */
928 g_local_file_query_filesystem_info (GFile *file,
929 const char *attributes,
930 GCancellable *cancellable,
933 GLocalFile *local = G_LOCAL_FILE (file);
935 int statfs_result = 0;
941 struct statfs statfs_buffer;
942 #elif defined(USE_STATVFS)
944 struct statvfs statfs_buffer;
945 #endif /* USE_STATFS */
946 #endif /* G_OS_WIN32 */
947 GFileAttributeMatcher *attribute_matcher;
954 statfs_result = statfs (local->filename, &statfs_buffer);
955 #elif STATFS_ARGS == 4
956 statfs_result = statfs (local->filename, &statfs_buffer,
957 sizeof (statfs_buffer), 0);
958 #endif /* STATFS_ARGS == 2 */
959 block_size = statfs_buffer.f_bsize;
961 /* Many backends can't report free size (for instance the gvfs fuse
962 backend for backend not supporting this), and set f_bfree to 0,
963 but it can be 0 for real too. We treat the available == 0 and
964 free == 0 case as "both of these are invalid".
967 if (statfs_result == 0 &&
968 statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0)
970 #endif /* G_OS_WIN32 */
972 #elif defined(USE_STATVFS)
973 statfs_result = statvfs (local->filename, &statfs_buffer);
974 block_size = statfs_buffer.f_frsize;
975 #endif /* USE_STATFS */
977 if (statfs_result == -1)
981 g_set_error (error, G_IO_ERROR,
982 g_io_error_from_errno (errsv),
983 _("Error getting filesystem info: %s"),
988 info = g_file_info_new ();
990 attribute_matcher = g_file_attribute_matcher_new (attributes);
993 g_file_attribute_matcher_matches (attribute_matcher,
994 G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
997 gchar *localdir = g_path_get_dirname (local->filename);
998 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1002 if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
1003 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
1006 #if defined(USE_STATFS) || defined(USE_STATVFS)
1007 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
1012 g_file_attribute_matcher_matches (attribute_matcher,
1013 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
1016 gchar *localdir = g_path_get_dirname (local->filename);
1017 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1021 if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
1022 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, (guint64)li.QuadPart);
1025 #if defined(USE_STATFS) || defined(USE_STATVFS)
1026 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
1028 #endif /* G_OS_WIN32 */
1032 g_file_attribute_matcher_matches (attribute_matcher,
1033 G_FILE_ATTRIBUTE_FILESYSTEM_USED))
1036 gchar *localdir = g_path_get_dirname (local->filename);
1037 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1038 ULARGE_INTEGER li_free;
1039 ULARGE_INTEGER li_total;
1042 if (GetDiskFreeSpaceExW (wdirname, &li_free, &li_total, NULL))
1043 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, (guint64)li_total.QuadPart - (guint64)li_free.QuadPart);
1046 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, block_size * (statfs_buffer.f_blocks - statfs_buffer.f_bfree));
1047 #endif /* G_OS_WIN32 */
1052 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1053 fstype = g_strdup (statfs_buffer.f_fstypename);
1055 fstype = get_fs_type (statfs_buffer.f_type);
1058 #elif defined(USE_STATVFS)
1059 #if defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
1060 fstype = g_strdup (statfs_buffer.f_fstypename);
1061 #elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1062 fstype = g_strdup (statfs_buffer.f_basetype);
1066 #endif /* USE_STATFS */
1069 g_file_attribute_matcher_matches (attribute_matcher,
1070 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1071 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1072 #endif /* G_OS_WIN32 */
1074 if (g_file_attribute_matcher_matches (attribute_matcher,
1075 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
1078 get_filesystem_readonly (info, local->filename);
1080 get_mount_info (info, local->filename, attribute_matcher);
1081 #endif /* G_OS_WIN32 */
1084 g_file_attribute_matcher_unref (attribute_matcher);
1090 g_local_file_find_enclosing_mount (GFile *file,
1091 GCancellable *cancellable,
1094 GLocalFile *local = G_LOCAL_FILE (file);
1099 if (g_lstat (local->filename, &buf) != 0)
1101 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1102 /* Translators: This is an error message when trying to
1103 * find the enclosing (user visible) mount of a file, but
1105 _("Containing mount does not exist"));
1109 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
1110 if (mountpoint == NULL)
1112 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1113 /* Translators: This is an error message when trying to
1114 * find the enclosing (user visible) mount of a file, but
1116 _("Containing mount does not exist"));
1120 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1121 g_free (mountpoint);
1125 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1126 /* Translators: This is an error message when trying to find
1127 * the enclosing (user visible) mount of a file, but none
1129 _("Containing mount does not exist"));
1134 g_local_file_set_display_name (GFile *file,
1135 const char *display_name,
1136 GCancellable *cancellable,
1139 GLocalFile *local, *new_local;
1140 GFile *new_file, *parent;
1146 parent = g_file_get_parent (file);
1149 g_set_error_literal (error, G_IO_ERROR,
1151 _("Can't rename root directory"));
1155 new_file = g_file_get_child_for_display_name (parent, display_name, error);
1156 g_object_unref (parent);
1158 if (new_file == NULL)
1160 local = G_LOCAL_FILE (file);
1161 new_local = G_LOCAL_FILE (new_file);
1163 if (g_lstat (new_local->filename, &statbuf) == -1)
1167 if (errsv != ENOENT)
1169 g_set_error (error, G_IO_ERROR,
1170 g_io_error_from_errno (errsv),
1171 _("Error renaming file: %s"),
1172 g_strerror (errsv));
1178 g_set_error_literal (error, G_IO_ERROR,
1180 _("Can't rename file, filename already exists"));
1184 if (g_rename (local->filename, new_local->filename) == -1)
1188 if (errsv == EINVAL)
1189 /* We can't get a rename file into itself error herer,
1190 so this must be an invalid filename, on e.g. FAT */
1191 g_set_error_literal (error, G_IO_ERROR,
1192 G_IO_ERROR_INVALID_FILENAME,
1193 _("Invalid filename"));
1195 g_set_error (error, G_IO_ERROR,
1196 g_io_error_from_errno (errsv),
1197 _("Error renaming file: %s"),
1198 g_strerror (errsv));
1199 g_object_unref (new_file);
1203 vfs = g_vfs_get_default ();
1204 class = G_VFS_GET_CLASS (vfs);
1205 if (class->local_file_moved)
1206 class->local_file_moved (vfs, local->filename, new_local->filename);
1212 g_local_file_query_info (GFile *file,
1213 const char *attributes,
1214 GFileQueryInfoFlags flags,
1215 GCancellable *cancellable,
1218 GLocalFile *local = G_LOCAL_FILE (file);
1220 GFileAttributeMatcher *matcher;
1221 char *basename, *dirname;
1222 GLocalParentFileInfo parent_info;
1224 matcher = g_file_attribute_matcher_new (attributes);
1226 basename = g_path_get_basename (local->filename);
1228 dirname = g_path_get_dirname (local->filename);
1229 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1232 info = _g_local_file_info_get (basename, local->filename,
1233 matcher, flags, &parent_info,
1237 _g_local_file_info_free_parent_info (&parent_info);
1240 g_file_attribute_matcher_unref (matcher);
1245 static GFileAttributeInfoList *
1246 g_local_file_query_settable_attributes (GFile *file,
1247 GCancellable *cancellable,
1250 return g_file_attribute_info_list_ref (local_writable_attributes);
1253 static GFileAttributeInfoList *
1254 g_local_file_query_writable_namespaces (GFile *file,
1255 GCancellable *cancellable,
1258 GFileAttributeInfoList *list;
1262 if (g_once_init_enter (&local_writable_namespaces))
1264 /* Writable namespaces: */
1266 list = g_file_attribute_info_list_new ();
1269 g_file_attribute_info_list_add (list,
1271 G_FILE_ATTRIBUTE_TYPE_STRING,
1272 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
1273 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1274 g_file_attribute_info_list_add (list,
1276 G_FILE_ATTRIBUTE_TYPE_STRING,
1277 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1280 vfs = g_vfs_get_default ();
1281 class = G_VFS_GET_CLASS (vfs);
1282 if (class->add_writable_namespaces)
1283 class->add_writable_namespaces (vfs, list);
1285 g_once_init_leave (&local_writable_namespaces, (gsize)list);
1287 list = (GFileAttributeInfoList *)local_writable_namespaces;
1289 return g_file_attribute_info_list_ref (list);
1293 g_local_file_set_attribute (GFile *file,
1294 const char *attribute,
1295 GFileAttributeType type,
1297 GFileQueryInfoFlags flags,
1298 GCancellable *cancellable,
1301 GLocalFile *local = G_LOCAL_FILE (file);
1303 return _g_local_file_info_set_attribute (local->filename,
1313 g_local_file_set_attributes_from_info (GFile *file,
1315 GFileQueryInfoFlags flags,
1316 GCancellable *cancellable,
1319 GLocalFile *local = G_LOCAL_FILE (file);
1320 int res, chained_res;
1321 GFileIface *default_iface;
1323 res = _g_local_file_info_set_attributes (local->filename,
1329 error = NULL; /* Don't write over error if further errors */
1331 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1333 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1335 return res && chained_res;
1338 static GFileInputStream *
1339 g_local_file_read (GFile *file,
1340 GCancellable *cancellable,
1343 GLocalFile *local = G_LOCAL_FILE (file);
1347 fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1353 if (errsv == EACCES)
1355 ret = _stati64 (local->filename, &buf);
1356 if (ret == 0 && S_ISDIR (buf.st_mode))
1358 g_set_error_literal (error, G_IO_ERROR,
1359 G_IO_ERROR_IS_DIRECTORY,
1360 _("Can't open directory"));
1366 g_set_error (error, G_IO_ERROR,
1367 g_io_error_from_errno (errsv),
1368 _("Error opening file: %s"),
1369 g_strerror (errsv));
1374 ret = _fstati64 (fd, &buf);
1376 ret = fstat (fd, &buf);
1379 if (ret == 0 && S_ISDIR (buf.st_mode))
1381 (void) g_close (fd, NULL);
1382 g_set_error_literal (error, G_IO_ERROR,
1383 G_IO_ERROR_IS_DIRECTORY,
1384 _("Can't open directory"));
1388 return _g_local_file_input_stream_new (fd);
1391 static GFileOutputStream *
1392 g_local_file_append_to (GFile *file,
1393 GFileCreateFlags flags,
1394 GCancellable *cancellable,
1397 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1398 flags, cancellable, error);
1401 static GFileOutputStream *
1402 g_local_file_create (GFile *file,
1403 GFileCreateFlags flags,
1404 GCancellable *cancellable,
1407 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1409 cancellable, error);
1412 static GFileOutputStream *
1413 g_local_file_replace (GFile *file,
1415 gboolean make_backup,
1416 GFileCreateFlags flags,
1417 GCancellable *cancellable,
1420 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1422 etag, make_backup, flags, NULL,
1423 cancellable, error);
1426 static GFileIOStream *
1427 g_local_file_open_readwrite (GFile *file,
1428 GCancellable *cancellable,
1431 GFileOutputStream *output;
1434 output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
1436 cancellable, error);
1440 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1441 g_object_unref (output);
1445 static GFileIOStream *
1446 g_local_file_create_readwrite (GFile *file,
1447 GFileCreateFlags flags,
1448 GCancellable *cancellable,
1451 GFileOutputStream *output;
1454 output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1456 cancellable, error);
1460 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1461 g_object_unref (output);
1465 static GFileIOStream *
1466 g_local_file_replace_readwrite (GFile *file,
1468 gboolean make_backup,
1469 GFileCreateFlags flags,
1470 GCancellable *cancellable,
1473 GFileOutputStream *output;
1476 output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1478 etag, make_backup, flags, NULL,
1479 cancellable, error);
1483 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1484 g_object_unref (output);
1489 g_local_file_delete (GFile *file,
1490 GCancellable *cancellable,
1493 GLocalFile *local = G_LOCAL_FILE (file);
1497 if (g_remove (local->filename) == -1)
1501 /* Posix allows EEXIST too, but the more sane error
1502 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1504 if (errsv == EEXIST)
1507 g_set_error (error, G_IO_ERROR,
1508 g_io_error_from_errno (errsv),
1509 _("Error removing file: %s"),
1510 g_strerror (errsv));
1514 vfs = g_vfs_get_default ();
1515 class = G_VFS_GET_CLASS (vfs);
1516 if (class->local_file_removed)
1517 class->local_file_removed (vfs, local->filename);
1525 strip_trailing_slashes (const char *path)
1530 path_copy = g_strdup (path);
1531 len = strlen (path_copy);
1532 while (len > 1 && path_copy[len-1] == '/')
1533 path_copy[--len] = 0;
1539 expand_symlink (const char *link)
1541 char *resolved, *canonical, *parent, *link2;
1542 char symlink_value[4096];
1550 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1553 return g_strdup (link);
1554 symlink_value[res] = 0;
1557 if (g_path_is_absolute (symlink_value))
1558 return canonicalize_filename (symlink_value);
1561 link2 = strip_trailing_slashes (link);
1562 parent = g_path_get_dirname (link2);
1565 resolved = g_build_filename (parent, symlink_value, NULL);
1568 canonical = canonicalize_filename (resolved);
1577 get_parent (const char *path,
1581 GStatBuf parent_stat;
1585 path_copy = strip_trailing_slashes (path);
1587 parent = g_path_get_dirname (path_copy);
1588 if (strcmp (parent, ".") == 0 ||
1589 strcmp (parent, path_copy) == 0)
1599 if (g_lstat (parent, &parent_stat) != 0)
1605 if (S_ISLNK (parent_stat.st_mode))
1608 parent = expand_symlink (parent);
1613 if (num_recursions > 12)
1618 } while (S_ISLNK (parent_stat.st_mode));
1620 *parent_dev = parent_stat.st_dev;
1626 expand_all_symlinks (const char *path)
1628 char *parent, *parent_expanded;
1629 char *basename, *res;
1632 parent = get_parent (path, &parent_dev);
1635 parent_expanded = expand_all_symlinks (parent);
1637 basename = g_path_get_basename (path);
1638 res = g_build_filename (parent_expanded, basename, NULL);
1640 g_free (parent_expanded);
1643 res = g_strdup (path);
1649 find_mountpoint_for (const char *file,
1653 dev_t dir_dev, parent_dev;
1655 dir = g_strdup (file);
1660 parent = get_parent (dir, &parent_dev);
1664 if (parent_dev != dir_dev)
1676 find_topdir_for (const char *file)
1681 dir = get_parent (file, &dir_dev);
1685 return find_mountpoint_for (dir, dir_dev);
1689 get_unique_filename (const char *basename,
1695 return g_strdup (basename);
1697 dot = strchr (basename, '.');
1699 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1701 return g_strdup_printf ("%s.%d", basename, id);
1705 path_has_prefix (const char *path,
1713 prefix_len = strlen (prefix);
1715 if (strncmp (path, prefix, prefix_len) == 0 &&
1716 (prefix_len == 0 || /* empty prefix always matches */
1717 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1718 path[prefix_len] == 0 ||
1719 path[prefix_len] == '/'))
1726 try_make_relative (const char *path,
1729 char *path2, *base2;
1732 path2 = expand_all_symlinks (path);
1733 base2 = expand_all_symlinks (base);
1736 if (path_has_prefix (path2, base2))
1738 relative = path2 + strlen (base2);
1739 while (*relative == '/')
1741 relative = g_strdup (relative);
1749 /* Failed, use abs path */
1750 return g_strdup (path);
1754 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1756 static gsize home_dev_set = 0;
1757 static dev_t home_dev;
1758 char *topdir, *globaldir, *trashdir, *tmpname;
1761 GStatBuf global_stat, trash_stat;
1764 if (g_once_init_enter (&home_dev_set))
1768 g_stat (g_get_home_dir (), &home_stat);
1769 home_dev = home_stat.st_dev;
1770 g_once_init_leave (&home_dev_set, 1);
1773 /* Assume we can trash to the home */
1774 if (dir_dev == home_dev)
1777 topdir = find_mountpoint_for (dirname, dir_dev);
1781 globaldir = g_build_filename (topdir, ".Trash", NULL);
1782 if (g_lstat (globaldir, &global_stat) == 0 &&
1783 S_ISDIR (global_stat.st_mode) &&
1784 (global_stat.st_mode & S_ISVTX) != 0)
1786 /* got a toplevel sysadmin created dir, assume we
1787 * can trash to it (we should be able to create a dir)
1788 * This fails for the FAT case where the ownership of
1789 * that dir would be wrong though..
1797 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1799 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1801 tmpname = g_strdup_printf (".Trash-%s", uid_str);
1802 trashdir = g_build_filename (topdir, tmpname, NULL);
1805 if (g_lstat (trashdir, &trash_stat) == 0)
1809 return S_ISDIR (trash_stat.st_mode) &&
1810 trash_stat.st_uid == uid;
1814 /* User specific trash didn't exist, can we create it? */
1815 res = g_access (topdir, W_OK) == 0;
1823 _g_local_file_is_lost_found_dir (const char *path, dev_t path_dev)
1825 gboolean ret = FALSE;
1826 gchar *mount_dir = NULL;
1827 size_t mount_dir_len;
1830 if (!g_str_has_suffix (path, "/lost+found"))
1833 mount_dir = find_mountpoint_for (path, path_dev);
1834 if (mount_dir == NULL)
1837 mount_dir_len = strlen (mount_dir);
1838 /* We special-case rootfs ('/') since it's the only case where
1839 * mount_dir ends in '/'
1841 if (mount_dir_len == 1)
1843 if (mount_dir_len + strlen ("/lost+found") != strlen (path))
1846 if (g_lstat (path, &statbuf) != 0)
1849 if (!(S_ISDIR (statbuf.st_mode) &&
1850 statbuf.st_uid == 0 &&
1851 statbuf.st_gid == 0))
1863 g_local_file_trash (GFile *file,
1864 GCancellable *cancellable,
1867 GLocalFile *local = G_LOCAL_FILE (file);
1868 GStatBuf file_stat, home_stat;
1869 const char *homedir;
1870 char *trashdir, *topdir, *infodir, *filesdir;
1871 char *basename, *trashname, *trashfile, *infoname, *infofile;
1872 char *original_name, *original_name_escaped;
1875 gboolean is_homedir_trash;
1876 char delete_time[32];
1878 GStatBuf trash_stat, global_stat;
1879 char *dirname, *globaldir;
1883 if (g_lstat (local->filename, &file_stat) != 0)
1887 g_set_error (error, G_IO_ERROR,
1888 g_io_error_from_errno (errsv),
1889 _("Error trashing file: %s"),
1890 g_strerror (errsv));
1894 homedir = g_get_home_dir ();
1895 g_stat (homedir, &home_stat);
1897 is_homedir_trash = FALSE;
1899 if (file_stat.st_dev == home_stat.st_dev)
1901 is_homedir_trash = TRUE;
1903 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1904 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1909 display_name = g_filename_display_name (trashdir);
1910 g_set_error (error, G_IO_ERROR,
1911 g_io_error_from_errno (errsv),
1912 _("Unable to create trash dir %s: %s"),
1913 display_name, g_strerror (errsv));
1914 g_free (display_name);
1918 topdir = g_strdup (g_get_user_data_dir ());
1926 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1928 topdir = find_topdir_for (local->filename);
1931 g_set_error_literal (error, G_IO_ERROR,
1932 G_IO_ERROR_NOT_SUPPORTED,
1933 _("Unable to find toplevel directory for trash"));
1937 /* Try looking for global trash dir $topdir/.Trash/$uid */
1938 globaldir = g_build_filename (topdir, ".Trash", NULL);
1939 if (g_lstat (globaldir, &global_stat) == 0 &&
1940 S_ISDIR (global_stat.st_mode) &&
1941 (global_stat.st_mode & S_ISVTX) != 0)
1943 trashdir = g_build_filename (globaldir, uid_str, NULL);
1945 if (g_lstat (trashdir, &trash_stat) == 0)
1947 if (!S_ISDIR (trash_stat.st_mode) ||
1948 trash_stat.st_uid != uid)
1950 /* Not a directory or not owned by user, ignore */
1955 else if (g_mkdir (trashdir, 0700) == -1)
1963 if (trashdir == NULL)
1965 gboolean tried_create;
1967 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1968 dirname = g_strdup_printf (".Trash-%s", uid_str);
1969 trashdir = g_build_filename (topdir, dirname, NULL);
1972 tried_create = FALSE;
1975 if (g_lstat (trashdir, &trash_stat) == 0)
1977 if (!S_ISDIR (trash_stat.st_mode) ||
1978 trash_stat.st_uid != uid)
1980 /* Remove the failed directory */
1982 g_remove (trashdir);
1984 /* Not a directory or not owned by user, ignore */
1991 if (!tried_create &&
1992 g_mkdir (trashdir, 0700) != -1)
1994 /* Ensure that the created dir has the right uid etc.
1995 This might fail on e.g. a FAT dir */
1996 tried_create = TRUE;
2007 if (trashdir == NULL)
2010 g_set_error_literal (error, G_IO_ERROR,
2011 G_IO_ERROR_NOT_SUPPORTED,
2012 _("Unable to find or create trash directory"));
2017 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
2019 infodir = g_build_filename (trashdir, "info", NULL);
2020 filesdir = g_build_filename (trashdir, "files", NULL);
2023 /* Make sure we have the subdirectories */
2024 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
2025 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
2030 g_set_error_literal (error, G_IO_ERROR,
2031 G_IO_ERROR_NOT_SUPPORTED,
2032 _("Unable to find or create trash directory"));
2036 basename = g_path_get_basename (local->filename);
2044 trashname = get_unique_filename (basename, i++);
2045 infoname = g_strconcat (trashname, ".trashinfo", NULL);
2046 infofile = g_build_filename (infodir, infoname, NULL);
2049 fd = g_open (infofile, O_CREAT | O_EXCL, 0666);
2050 } while (fd == -1 && errno == EEXIST);
2064 g_set_error (error, G_IO_ERROR,
2065 g_io_error_from_errno (errsv),
2066 _("Unable to create trashing info file: %s"),
2067 g_strerror (errsv));
2071 (void) g_close (fd, NULL);
2073 /* TODO: Maybe we should verify that you can delete the file from the trash
2074 before moving it? OTOH, that is hard, as it needs a recursive scan */
2076 trashfile = g_build_filename (filesdir, trashname, NULL);
2080 if (g_rename (local->filename, trashfile) == -1)
2084 g_unlink (infofile);
2092 /* The trash dir was actually on another fs anyway!?
2093 This can happen when the same device is mounted multiple
2094 times, or with bind mounts of the same fs. */
2095 g_set_error (error, G_IO_ERROR,
2096 G_IO_ERROR_NOT_SUPPORTED,
2097 _("Unable to trash file: %s"),
2098 g_strerror (errsv));
2100 g_set_error (error, G_IO_ERROR,
2101 g_io_error_from_errno (errsv),
2102 _("Unable to trash file: %s"),
2103 g_strerror (errsv));
2107 vfs = g_vfs_get_default ();
2108 class = G_VFS_GET_CLASS (vfs);
2109 if (class->local_file_moved)
2110 class->local_file_moved (vfs, local->filename, trashfile);
2114 /* TODO: Do we need to update mtime/atime here after the move? */
2116 /* Use absolute names for homedir */
2117 if (is_homedir_trash)
2118 original_name = g_strdup (local->filename);
2120 original_name = try_make_relative (local->filename, topdir);
2121 original_name_escaped = g_uri_escape_string (original_name, "/", FALSE);
2123 g_free (original_name);
2130 localtime_r (&t, &now);
2132 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
2135 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2136 original_name_escaped, delete_time);
2138 g_file_set_contents (infofile, data, -1, NULL);
2142 g_free (original_name_escaped);
2147 #else /* G_OS_WIN32 */
2149 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
2151 return FALSE; /* XXX ??? */
2155 g_local_file_trash (GFile *file,
2156 GCancellable *cancellable,
2159 GLocalFile *local = G_LOCAL_FILE (file);
2160 SHFILEOPSTRUCTW op = {0};
2165 wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
2166 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2167 wfilename = g_renew (wchar_t, wfilename, len + 2);
2168 wfilename[len + 1] = 0;
2170 op.wFunc = FO_DELETE;
2171 op.pFrom = wfilename;
2172 op.fFlags = FOF_ALLOWUNDO;
2174 success = SHFileOperationW (&op) == 0;
2176 if (success && op.fAnyOperationsAborted)
2178 if (cancellable && !g_cancellable_is_cancelled (cancellable))
2179 g_cancellable_cancel (cancellable);
2180 g_set_error (error, G_IO_ERROR,
2181 G_IO_ERROR_CANCELLED,
2182 _("Unable to trash file: %s"),
2183 _("Operation was cancelled"));
2187 g_set_error (error, G_IO_ERROR,
2189 _("Unable to trash file: %s"),
2190 _("internal error"));
2195 #endif /* G_OS_WIN32 */
2198 g_local_file_make_directory (GFile *file,
2199 GCancellable *cancellable,
2202 GLocalFile *local = G_LOCAL_FILE (file);
2204 if (g_mkdir (local->filename, 0777) == -1)
2208 if (errsv == EINVAL)
2209 /* This must be an invalid filename, on e.g. FAT */
2210 g_set_error_literal (error, G_IO_ERROR,
2211 G_IO_ERROR_INVALID_FILENAME,
2212 _("Invalid filename"));
2214 g_set_error (error, G_IO_ERROR,
2215 g_io_error_from_errno (errsv),
2216 _("Error creating directory: %s"),
2217 g_strerror (errsv));
2225 g_local_file_make_symbolic_link (GFile *file,
2226 const char *symlink_value,
2227 GCancellable *cancellable,
2231 GLocalFile *local = G_LOCAL_FILE (file);
2233 if (symlink (symlink_value, local->filename) == -1)
2237 if (errsv == EINVAL)
2238 /* This must be an invalid filename, on e.g. FAT */
2239 g_set_error_literal (error, G_IO_ERROR,
2240 G_IO_ERROR_INVALID_FILENAME,
2241 _("Invalid filename"));
2242 else if (errsv == EPERM)
2243 g_set_error (error, G_IO_ERROR,
2244 G_IO_ERROR_NOT_SUPPORTED,
2245 _("Filesystem does not support symbolic links"));
2247 g_set_error (error, G_IO_ERROR,
2248 g_io_error_from_errno (errsv),
2249 _("Error making symbolic link: %s"),
2250 g_strerror (errsv));
2255 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
2262 g_local_file_copy (GFile *source,
2264 GFileCopyFlags flags,
2265 GCancellable *cancellable,
2266 GFileProgressCallback progress_callback,
2267 gpointer progress_callback_data,
2270 /* Fall back to default copy */
2271 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
2276 g_local_file_move (GFile *source,
2278 GFileCopyFlags flags,
2279 GCancellable *cancellable,
2280 GFileProgressCallback progress_callback,
2281 gpointer progress_callback_data,
2284 GLocalFile *local_source, *local_destination;
2286 gboolean destination_exist, source_is_dir;
2293 if (!G_IS_LOCAL_FILE (source) ||
2294 !G_IS_LOCAL_FILE (destination))
2296 /* Fall back to default move */
2297 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2301 local_source = G_LOCAL_FILE (source);
2302 local_destination = G_LOCAL_FILE (destination);
2304 res = g_lstat (local_source->filename, &statbuf);
2309 g_set_error (error, G_IO_ERROR,
2310 g_io_error_from_errno (errsv),
2311 _("Error moving file: %s"),
2312 g_strerror (errsv));
2316 source_is_dir = S_ISDIR (statbuf.st_mode);
2317 source_size = statbuf.st_size;
2319 destination_exist = FALSE;
2320 res = g_lstat (local_destination->filename, &statbuf);
2323 destination_exist = TRUE; /* Target file exists */
2325 if (flags & G_FILE_COPY_OVERWRITE)
2327 /* Always fail on dirs, even with overwrite */
2328 if (S_ISDIR (statbuf.st_mode))
2331 g_set_error_literal (error,
2333 G_IO_ERROR_WOULD_MERGE,
2334 _("Can't move directory over directory"));
2336 g_set_error_literal (error,
2338 G_IO_ERROR_IS_DIRECTORY,
2339 _("Can't copy over directory"));
2345 g_set_error_literal (error,
2348 _("Target file exists"));
2353 if (flags & G_FILE_COPY_BACKUP && destination_exist)
2355 backup_name = g_strconcat (local_destination->filename, "~", NULL);
2356 if (g_rename (local_destination->filename, backup_name) == -1)
2358 g_set_error_literal (error,
2360 G_IO_ERROR_CANT_CREATE_BACKUP,
2361 _("Backup file creation failed"));
2362 g_free (backup_name);
2365 g_free (backup_name);
2366 destination_exist = FALSE; /* It did, but no more */
2369 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2371 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2372 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2373 res = g_unlink (local_destination->filename);
2378 g_set_error (error, G_IO_ERROR,
2379 g_io_error_from_errno (errsv),
2380 _("Error removing target file: %s"),
2381 g_strerror (errsv));
2386 if (g_rename (local_source->filename, local_destination->filename) == -1)
2391 /* This will cause the fallback code to run */
2392 g_set_error_literal (error, G_IO_ERROR,
2393 G_IO_ERROR_NOT_SUPPORTED,
2394 _("Move between mounts not supported"));
2395 else if (errsv == EINVAL)
2396 /* This must be an invalid filename, on e.g. FAT, or
2397 we're trying to move the file into itself...
2398 We return invalid filename for both... */
2399 g_set_error_literal (error, G_IO_ERROR,
2400 G_IO_ERROR_INVALID_FILENAME,
2401 _("Invalid filename"));
2403 g_set_error (error, G_IO_ERROR,
2404 g_io_error_from_errno (errsv),
2405 _("Error moving file: %s"),
2406 g_strerror (errsv));
2410 vfs = g_vfs_get_default ();
2411 class = G_VFS_GET_CLASS (vfs);
2412 if (class->local_file_moved)
2413 class->local_file_moved (vfs, local_source->filename, local_destination->filename);
2415 /* Make sure we send full copied size */
2416 if (progress_callback)
2417 progress_callback (source_size, source_size, progress_callback_data);
2425 is_remote (const gchar *filename)
2433 is_remote_fs (const gchar *filename)
2435 const char *fsname = NULL;
2438 struct statfs statfs_buffer;
2439 int statfs_result = 0;
2441 #if STATFS_ARGS == 2
2442 statfs_result = statfs (filename, &statfs_buffer);
2443 #elif STATFS_ARGS == 4
2444 statfs_result = statfs (filename, &statfs_buffer, sizeof (statfs_buffer), 0);
2447 #elif defined(USE_STATVFS)
2448 struct statvfs statfs_buffer;
2449 int statfs_result = 0;
2451 statfs_result = statvfs (filename, &statfs_buffer);
2456 if (statfs_result == -1)
2460 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
2461 fsname = statfs_buffer.f_fstypename;
2463 fsname = get_fs_type (statfs_buffer.f_type);
2466 #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
2467 fsname = statfs_buffer.f_basetype;
2472 if (strcmp (fsname, "nfs") == 0)
2474 if (strcmp (fsname, "nfs4") == 0)
2482 is_remote (const gchar *filename)
2484 static gboolean remote_home;
2485 static gsize initialized;
2488 home = g_get_home_dir ();
2489 if (path_has_prefix (filename, home))
2491 if (g_once_init_enter (&initialized))
2493 remote_home = is_remote_fs (home);
2494 g_once_init_leave (&initialized, TRUE);
2501 #endif /* !G_OS_WIN32 */
2503 static GFileMonitor*
2504 g_local_file_monitor_dir (GFile *file,
2505 GFileMonitorFlags flags,
2506 GCancellable *cancellable,
2509 GLocalFile* local_file = G_LOCAL_FILE(file);
2510 return _g_local_directory_monitor_new (local_file->filename, flags, NULL, is_remote (local_file->filename), TRUE, error);
2513 static GFileMonitor*
2514 g_local_file_monitor_file (GFile *file,
2515 GFileMonitorFlags flags,
2516 GCancellable *cancellable,
2519 GLocalFile* local_file = G_LOCAL_FILE(file);
2520 return _g_local_file_monitor_new (local_file->filename, flags, NULL, is_remote (local_file->filename), TRUE, error);
2523 GLocalDirectoryMonitor *
2524 g_local_directory_monitor_new_in_worker (const char *pathname,
2525 GFileMonitorFlags flags,
2528 return (gpointer) _g_local_directory_monitor_new (pathname, flags,
2529 GLIB_PRIVATE_CALL (g_get_worker_context) (),
2530 is_remote (pathname), FALSE, error);
2534 g_local_file_monitor_new_in_worker (const char *pathname,
2535 GFileMonitorFlags flags,
2538 return (gpointer) _g_local_file_monitor_new (pathname, flags,
2539 GLIB_PRIVATE_CALL (g_get_worker_context) (),
2540 is_remote (pathname), FALSE, error);
2544 /* Here is the GLocalFile implementation of g_file_measure_disk_usage().
2546 * If available, we use fopenat() in preference to filenames for
2547 * efficiency and safety reasons. We know that fopenat() is available
2548 * based on if AT_FDCWD is defined. POSIX guarantees that this will be
2549 * defined as a macro.
2551 * We use a linked list of stack-allocated GSList nodes in order to be
2552 * able to reconstruct the filename for error messages. We actually
2553 * pass the filename to operate on through the top node of the list.
2555 * In case we're using openat(), this top filename will be a basename
2556 * which should be opened in the directory which has also had its fd
2557 * passed along. If we're not using openat() then it will be a full
2558 * absolute filename.
2562 g_local_file_measure_size_error (GFileMeasureFlags flags,
2567 /* Only report an error if we were at the toplevel or if the caller
2568 * requested reporting of all errors.
2570 if ((name->next == NULL) || (flags & G_FILE_MEASURE_REPORT_ANY_ERROR))
2575 /* Skip some work if there is no error return */
2580 /* If using openat() we need to rebuild the filename for the message */
2581 filename = g_string_new (name->data);
2582 for (node = name->next; node; node = node->next)
2586 g_string_prepend_c (filename, G_DIR_SEPARATOR);
2587 utf8 = g_filename_display_name (node->data);
2588 g_string_prepend (filename, utf8);
2595 /* Otherwise, we already have it, so just use it. */
2597 filename = g_string_new (NULL);
2598 utf8 = g_filename_display_name (node->data);
2599 g_string_append (filename, utf8);
2604 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (saved_errno),
2605 _("Could not determine the disk usage of %s: %s"),
2606 filename->str, g_strerror (saved_errno));
2608 g_string_free (filename, TRUE);
2614 /* We're not reporting this error... */
2620 GFileMeasureFlags flags;
2622 GCancellable *cancellable;
2624 GFileMeasureProgressCallback progress_callback;
2625 gpointer progress_data;
2631 guint64 last_progress_report;
2635 g_local_file_measure_size_of_contents (gint fd,
2637 MeasureState *state,
2641 g_local_file_measure_size_of_file (gint parent_fd,
2643 MeasureState *state,
2648 if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
2651 #if defined (AT_FDCWD)
2652 if (fstatat (parent_fd, name->data, &buf, AT_SYMLINK_NOFOLLOW) != 0)
2654 if (g_lstat (name->data, &buf) != 0)
2656 return g_local_file_measure_size_error (state->flags, errno, name, error);
2660 /* If not at the toplevel, check for a device boundary. */
2662 if (state->flags & G_FILE_MEASURE_NO_XDEV)
2663 if (state->contained_on != buf.st_dev)
2668 /* If, however, this is the toplevel, set the device number so
2669 * that recursive invocations can compare against it.
2671 state->contained_on = buf.st_dev;
2674 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
2675 if (~state->flags & G_FILE_MEASURE_APPARENT_SIZE)
2676 state->disk_usage += buf.st_blocks * G_GUINT64_CONSTANT (512);
2679 state->disk_usage += buf.st_size;
2681 if (S_ISDIR (buf.st_mode))
2686 if (state->progress_callback)
2688 /* We could attempt to do some cleverness here in order to avoid
2689 * calling clock_gettime() so much, but we're doing stats and opens
2690 * all over the place already...
2692 if (state->last_progress_report)
2696 now = g_get_monotonic_time ();
2698 if (state->last_progress_report + 200 * G_TIME_SPAN_MILLISECOND < now)
2700 (* state->progress_callback) (TRUE,
2701 state->disk_usage, state->num_dirs, state->num_files,
2702 state->progress_data);
2703 state->last_progress_report = now;
2708 /* We must do an initial report to inform that more reports
2711 (* state->progress_callback) (TRUE, 0, 0, 0, state->progress_data);
2712 state->last_progress_report = g_get_monotonic_time ();
2716 if (S_ISDIR (buf.st_mode))
2720 if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
2724 #ifdef HAVE_OPEN_O_DIRECTORY
2725 dir_fd = openat (parent_fd, name->data, O_RDONLY|O_DIRECTORY);
2727 dir_fd = openat (parent_fd, name->data, O_RDONLY);
2730 return g_local_file_measure_size_error (state->flags, errno, name, error);
2733 if (!g_local_file_measure_size_of_contents (dir_fd, name, state, error))
2741 g_local_file_measure_size_of_contents (gint fd,
2743 MeasureState *state,
2746 gboolean success = TRUE;
2752 /* If this fails, we want to preserve the errno from fopendir() */
2754 dirp = fdopendir (fd);
2755 dir = dirp ? GLIB_PRIVATE_CALL(g_dir_new_from_dirp) (dirp) : NULL;
2758 dir = GLIB_PRIVATE_CALL(g_dir_open_with_errno) (dir_name->data, 0);
2763 gint saved_errno = errno;
2769 return g_local_file_measure_size_error (state->flags, saved_errno, dir_name, error);
2772 while (success && (name = g_dir_read_name (dir)))
2776 node.next = dir_name;
2778 node.data = (gchar *) name;
2780 node.data = g_build_filename (dir_name->data, name, NULL);
2783 success = g_local_file_measure_size_of_file (fd, &node, state, error);
2796 g_local_file_measure_disk_usage (GFile *file,
2797 GFileMeasureFlags flags,
2798 GCancellable *cancellable,
2799 GFileMeasureProgressCallback progress_callback,
2800 gpointer progress_data,
2801 guint64 *disk_usage,
2806 GLocalFile *local_file = G_LOCAL_FILE (file);
2807 MeasureState state = { 0, };
2811 state.flags = flags;
2812 state.cancellable = cancellable;
2813 state.progress_callback = progress_callback;
2814 state.progress_data = progress_data;
2820 node.data = local_file->filename;
2823 if (!g_local_file_measure_size_of_file (root_fd, &node, &state, error))
2827 *disk_usage = state.disk_usage;
2830 *num_dirs = state.num_dirs;
2833 *num_files = state.num_files;
2839 g_local_file_file_iface_init (GFileIface *iface)
2841 iface->dup = g_local_file_dup;
2842 iface->hash = g_local_file_hash;
2843 iface->equal = g_local_file_equal;
2844 iface->is_native = g_local_file_is_native;
2845 iface->has_uri_scheme = g_local_file_has_uri_scheme;
2846 iface->get_uri_scheme = g_local_file_get_uri_scheme;
2847 iface->get_basename = g_local_file_get_basename;
2848 iface->get_path = g_local_file_get_path;
2849 iface->get_uri = g_local_file_get_uri;
2850 iface->get_parse_name = g_local_file_get_parse_name;
2851 iface->get_parent = g_local_file_get_parent;
2852 iface->prefix_matches = g_local_file_prefix_matches;
2853 iface->get_relative_path = g_local_file_get_relative_path;
2854 iface->resolve_relative_path = g_local_file_resolve_relative_path;
2855 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2856 iface->set_display_name = g_local_file_set_display_name;
2857 iface->enumerate_children = g_local_file_enumerate_children;
2858 iface->query_info = g_local_file_query_info;
2859 iface->query_filesystem_info = g_local_file_query_filesystem_info;
2860 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2861 iface->query_settable_attributes = g_local_file_query_settable_attributes;
2862 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2863 iface->set_attribute = g_local_file_set_attribute;
2864 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2865 iface->read_fn = g_local_file_read;
2866 iface->append_to = g_local_file_append_to;
2867 iface->create = g_local_file_create;
2868 iface->replace = g_local_file_replace;
2869 iface->open_readwrite = g_local_file_open_readwrite;
2870 iface->create_readwrite = g_local_file_create_readwrite;
2871 iface->replace_readwrite = g_local_file_replace_readwrite;
2872 iface->delete_file = g_local_file_delete;
2873 iface->trash = g_local_file_trash;
2874 iface->make_directory = g_local_file_make_directory;
2875 iface->make_symbolic_link = g_local_file_make_symbolic_link;
2876 iface->copy = g_local_file_copy;
2877 iface->move = g_local_file_move;
2878 iface->monitor_dir = g_local_file_monitor_dir;
2879 iface->monitor_file = g_local_file_monitor_file;
2880 iface->measure_disk_usage = g_local_file_measure_disk_usage;
2882 iface->supports_thread_contexts = TRUE;