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>
35 #include <sys/statfs.h>
37 #if HAVE_SYS_STATVFS_H
38 #include <sys/statvfs.h>
42 #elif HAVE_SYS_MOUNT_H
44 #include <sys/param.h>
46 #include <sys/mount.h>
53 #include "gfileattribute.h"
54 #include "glocalfile.h"
55 #include "glocalfileinfo.h"
56 #include "glocalfileenumerator.h"
57 #include "glocalfileinputstream.h"
58 #include "glocalfileoutputstream.h"
59 #include "glocalfileiostream.h"
60 #include "glocaldirectorymonitor.h"
61 #include "glocalfilemonitor.h"
62 #include "gmountprivate.h"
63 #include "gunixmounts.h"
65 #include <glib/gstdio.h>
68 #include "glib-unix.h"
76 #ifndef FILE_READ_ONLY_VOLUME
77 #define FILE_READ_ONLY_VOLUME 0x00080000
81 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
84 #define S_ISLNK(m) (0)
89 static void g_local_file_file_iface_init (GFileIface *iface);
91 static GFileAttributeInfoList *local_writable_attributes = NULL;
92 static /* GFileAttributeInfoList * */ gsize local_writable_namespaces = 0;
96 GObject parent_instance;
101 #define g_local_file_get_type _g_local_file_get_type
102 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
103 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
104 g_local_file_file_iface_init))
106 static char *find_mountpoint_for (const char *file, dev_t dev);
109 g_local_file_finalize (GObject *object)
113 local = G_LOCAL_FILE (object);
115 g_free (local->filename);
117 G_OBJECT_CLASS (g_local_file_parent_class)->finalize (object);
121 g_local_file_class_init (GLocalFileClass *klass)
123 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
124 GFileAttributeInfoList *list;
126 gobject_class->finalize = g_local_file_finalize;
128 /* Set up attribute lists */
130 /* Writable attributes: */
132 list = g_file_attribute_info_list_new ();
134 g_file_attribute_info_list_add (list,
135 G_FILE_ATTRIBUTE_UNIX_MODE,
136 G_FILE_ATTRIBUTE_TYPE_UINT32,
137 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
138 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
141 g_file_attribute_info_list_add (list,
142 G_FILE_ATTRIBUTE_UNIX_UID,
143 G_FILE_ATTRIBUTE_TYPE_UINT32,
144 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
145 g_file_attribute_info_list_add (list,
146 G_FILE_ATTRIBUTE_UNIX_GID,
147 G_FILE_ATTRIBUTE_TYPE_UINT32,
148 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
152 g_file_attribute_info_list_add (list,
153 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
154 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
159 g_file_attribute_info_list_add (list,
160 G_FILE_ATTRIBUTE_TIME_MODIFIED,
161 G_FILE_ATTRIBUTE_TYPE_UINT64,
162 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
163 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
164 g_file_attribute_info_list_add (list,
165 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
166 G_FILE_ATTRIBUTE_TYPE_UINT32,
167 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
168 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
169 /* When copying, the target file is accessed. Replicating
170 * the source access time does not make sense in this case.
172 g_file_attribute_info_list_add (list,
173 G_FILE_ATTRIBUTE_TIME_ACCESS,
174 G_FILE_ATTRIBUTE_TYPE_UINT64,
175 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
176 g_file_attribute_info_list_add (list,
177 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
178 G_FILE_ATTRIBUTE_TYPE_UINT32,
179 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
182 local_writable_attributes = list;
186 g_local_file_init (GLocalFile *local)
192 canonicalize_filename (const char *filename)
194 char *canon, *start, *p, *q;
198 if (!g_path_is_absolute (filename))
200 cwd = g_get_current_dir ();
201 canon = g_build_filename (cwd, filename, NULL);
205 canon = g_strdup (filename);
207 start = (char *)g_path_skip_root (canon);
211 /* This shouldn't really happen, as g_get_current_dir() should
212 return an absolute pathname, but bug 573843 shows this is
213 not always happening */
215 return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
218 /* POSIX allows double slashes at the start to
219 * mean something special (as does windows too).
220 * So, "//" != "/", but more than two slashes
226 G_IS_DIR_SEPARATOR (*p);
233 memmove (start, start+i, strlen (start+i)+1);
236 /* Make sure we're using the canonical dir separator */
238 while (p < start && G_IS_DIR_SEPARATOR (*p))
239 *p++ = G_DIR_SEPARATOR;
244 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
246 memmove (p, p+1, strlen (p+1)+1);
248 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
251 /* Skip previous separator */
255 while (p > start && !G_IS_DIR_SEPARATOR (*p))
257 if (G_IS_DIR_SEPARATOR (*p))
258 *p++ = G_DIR_SEPARATOR;
259 memmove (p, q, strlen (q)+1);
263 /* Skip until next separator */
264 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
269 /* Canonicalize one separator */
270 *p++ = G_DIR_SEPARATOR;
274 /* Remove additional separators */
276 while (*q && G_IS_DIR_SEPARATOR (*q))
280 memmove (p, q, strlen (q)+1);
283 /* Remove trailing slashes */
284 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
291 _g_local_file_new (const char *filename)
295 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
296 local->filename = canonicalize_filename (filename);
298 return G_FILE (local);
302 g_local_file_is_native (GFile *file)
308 g_local_file_has_uri_scheme (GFile *file,
309 const char *uri_scheme)
311 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
315 g_local_file_get_uri_scheme (GFile *file)
317 return g_strdup ("file");
321 g_local_file_get_basename (GFile *file)
323 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
327 g_local_file_get_path (GFile *file)
329 return g_strdup (G_LOCAL_FILE (file)->filename);
333 g_local_file_get_uri (GFile *file)
335 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
339 get_filename_charset (const gchar **filename_charset)
341 const gchar **charsets;
344 is_utf8 = g_get_filename_charsets (&charsets);
346 if (filename_charset)
347 *filename_charset = charsets[0];
353 name_is_valid_for_display (const char *string,
354 gboolean is_valid_utf8)
358 if (!is_valid_utf8 &&
359 !g_utf8_validate (string, -1, NULL))
362 while ((c = *string++) != 0)
364 if (g_ascii_iscntrl (c))
372 g_local_file_get_parse_name (GFile *file)
374 const char *filename;
376 const gchar *charset;
378 char *roundtripped_filename;
379 gboolean free_utf8_filename;
380 gboolean is_valid_utf8;
383 filename = G_LOCAL_FILE (file)->filename;
384 if (get_filename_charset (&charset))
386 utf8_filename = (char *)filename;
387 free_utf8_filename = FALSE;
388 is_valid_utf8 = FALSE; /* Can't guarantee this */
392 utf8_filename = g_convert (filename, -1,
393 "UTF-8", charset, NULL, NULL, NULL);
394 free_utf8_filename = TRUE;
395 is_valid_utf8 = TRUE;
397 if (utf8_filename != NULL)
399 /* Make sure we can roundtrip: */
400 roundtripped_filename = g_convert (utf8_filename, -1,
401 charset, "UTF-8", NULL, NULL, NULL);
403 if (roundtripped_filename == NULL ||
404 strcmp (filename, roundtripped_filename) != 0)
406 g_free (utf8_filename);
407 utf8_filename = NULL;
410 g_free (roundtripped_filename);
414 if (utf8_filename != NULL &&
415 name_is_valid_for_display (utf8_filename, is_valid_utf8))
417 if (free_utf8_filename)
418 parse_name = utf8_filename;
420 parse_name = g_strdup (utf8_filename);
425 char *dup_filename, *p, *backslash;
427 /* Turn backslashes into forward slashes like
428 * g_filename_to_uri() would do (but we can't use that because
429 * it doesn't output IRIs).
431 dup_filename = g_strdup (filename);
432 filename = p = dup_filename;
434 while ((backslash = strchr (p, '\\')) != NULL)
441 escaped_path = g_uri_escape_string (filename,
442 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
444 parse_name = g_strconcat ("file://",
445 (*escaped_path != '/') ? "/" : "",
449 g_free (escaped_path);
451 g_free (dup_filename);
453 if (free_utf8_filename)
454 g_free (utf8_filename);
461 g_local_file_get_parent (GFile *file)
463 GLocalFile *local = G_LOCAL_FILE (file);
464 const char *non_root;
469 non_root = g_path_skip_root (local->filename);
473 dirname = g_path_get_dirname (local->filename);
474 parent = _g_local_file_new (dirname);
480 g_local_file_dup (GFile *file)
482 GLocalFile *local = G_LOCAL_FILE (file);
484 return _g_local_file_new (local->filename);
488 g_local_file_hash (GFile *file)
490 GLocalFile *local = G_LOCAL_FILE (file);
492 return g_str_hash (local->filename);
496 g_local_file_equal (GFile *file1,
499 GLocalFile *local1 = G_LOCAL_FILE (file1);
500 GLocalFile *local2 = G_LOCAL_FILE (file2);
502 return g_str_equal (local1->filename, local2->filename);
506 match_prefix (const char *path,
511 prefix_len = strlen (prefix);
512 if (strncmp (path, prefix, prefix_len) != 0)
515 /* Handle the case where prefix is the root, so that
516 * the IS_DIR_SEPRARATOR check below works */
517 if (prefix_len > 0 &&
518 G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
521 return path + prefix_len;
525 g_local_file_prefix_matches (GFile *parent,
528 GLocalFile *parent_local = G_LOCAL_FILE (parent);
529 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
530 const char *remainder;
532 remainder = match_prefix (descendant_local->filename, parent_local->filename);
533 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
539 g_local_file_get_relative_path (GFile *parent,
542 GLocalFile *parent_local = G_LOCAL_FILE (parent);
543 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
544 const char *remainder;
546 remainder = match_prefix (descendant_local->filename, parent_local->filename);
548 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
549 return g_strdup (remainder + 1);
554 g_local_file_resolve_relative_path (GFile *file,
555 const char *relative_path)
557 GLocalFile *local = G_LOCAL_FILE (file);
561 if (g_path_is_absolute (relative_path))
562 return _g_local_file_new (relative_path);
564 filename = g_build_filename (local->filename, relative_path, NULL);
565 child = _g_local_file_new (filename);
571 static GFileEnumerator *
572 g_local_file_enumerate_children (GFile *file,
573 const char *attributes,
574 GFileQueryInfoFlags flags,
575 GCancellable *cancellable,
578 GLocalFile *local = G_LOCAL_FILE (file);
579 return _g_local_file_enumerator_new (local,
585 g_local_file_get_child_for_display_name (GFile *file,
586 const char *display_name,
592 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
593 if (basename == NULL)
595 g_set_error (error, G_IO_ERROR,
596 G_IO_ERROR_INVALID_FILENAME,
597 _("Invalid filename %s"), display_name);
601 new_file = g_file_get_child (file, basename);
607 #if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
609 get_fs_type (long f_type)
611 /* filesystem ids taken from linux manpage */
718 G_LOCK_DEFINE_STATIC(mount_info_hash);
719 static GHashTable *mount_info_hash = NULL;
720 static guint64 mount_info_hash_cache_time = 0;
723 MOUNT_INFO_READONLY = 1<<0
727 device_equal (gconstpointer v1,
730 return *(dev_t *)v1 == *(dev_t *)v2;
734 device_hash (gconstpointer v)
736 return (guint) *(dev_t *)v;
740 get_mount_info (GFileInfo *fs_info,
742 GFileAttributeMatcher *matcher)
746 gpointer info_as_ptr;
750 GUnixMountEntry *mount;
753 if (g_lstat (path, &buf) != 0)
756 G_LOCK (mount_info_hash);
758 if (mount_info_hash == NULL)
759 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
763 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
764 g_hash_table_remove_all (mount_info_hash);
766 got_info = g_hash_table_lookup_extended (mount_info_hash,
771 G_UNLOCK (mount_info_hash);
773 mount_info = GPOINTER_TO_UINT (info_as_ptr);
779 mountpoint = find_mountpoint_for (path, buf.st_dev);
780 if (mountpoint == NULL)
781 mountpoint = g_strdup ("/");
783 mount = g_unix_mount_at (mountpoint, &cache_time);
786 if (g_unix_mount_is_readonly (mount))
787 mount_info |= MOUNT_INFO_READONLY;
789 g_unix_mount_free (mount);
794 dev = g_new0 (dev_t, 1);
797 G_LOCK (mount_info_hash);
798 mount_info_hash_cache_time = cache_time;
799 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
800 G_UNLOCK (mount_info_hash);
803 if (mount_info & MOUNT_INFO_READONLY)
804 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
812 is_xp_or_later (void)
814 static int result = -1;
819 OSVERSIONINFOEX ver_info = {0};
820 DWORDLONG cond_mask = 0;
821 int op = VER_GREATER_EQUAL;
823 ver_info.dwOSVersionInfoSize = sizeof ver_info;
824 ver_info.dwMajorVersion = 5;
825 ver_info.dwMinorVersion = 1;
827 VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
828 VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
830 result = VerifyVersionInfo (&ver_info,
831 VER_MAJORVERSION | VER_MINORVERSION,
834 result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;
842 get_volume_for_path (const char *path)
848 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
849 result = g_new (wchar_t, MAX_PATH);
851 if (!GetVolumePathNameW (wpath, result, MAX_PATH))
853 char *msg = g_win32_error_message (GetLastError ());
854 g_critical ("GetVolumePathName failed: %s", msg);
861 len = wcslen (result);
862 if (len > 0 && result[len-1] != L'\\')
864 result = g_renew (wchar_t, result, len + 2);
874 find_mountpoint_for (const char *file, dev_t dev)
879 wpath = get_volume_for_path (file);
883 utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
890 get_filesystem_readonly (GFileInfo *info,
895 rootdir = get_volume_for_path (path);
899 if (is_xp_or_later ())
902 if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
903 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
904 (flags & FILE_READ_ONLY_VOLUME) != 0);
908 if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
909 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
916 #endif /* G_OS_WIN32 */
919 g_local_file_query_filesystem_info (GFile *file,
920 const char *attributes,
921 GCancellable *cancellable,
924 GLocalFile *local = G_LOCAL_FILE (file);
926 int statfs_result = 0;
932 struct statfs statfs_buffer;
933 #elif defined(USE_STATVFS)
935 struct statvfs statfs_buffer;
936 #endif /* USE_STATFS */
937 #endif /* G_OS_WIN32 */
938 GFileAttributeMatcher *attribute_matcher;
945 statfs_result = statfs (local->filename, &statfs_buffer);
946 #elif STATFS_ARGS == 4
947 statfs_result = statfs (local->filename, &statfs_buffer,
948 sizeof (statfs_buffer), 0);
949 #endif /* STATFS_ARGS == 2 */
950 block_size = statfs_buffer.f_bsize;
952 /* Many backends can't report free size (for instance the gvfs fuse
953 backend for backend not supporting this), and set f_bfree to 0,
954 but it can be 0 for real too. We treat the available == 0 and
955 free == 0 case as "both of these are invalid".
958 if (statfs_result == 0 &&
959 statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0)
961 #endif /* G_OS_WIN32 */
963 #elif defined(USE_STATVFS)
964 statfs_result = statvfs (local->filename, &statfs_buffer);
965 block_size = statfs_buffer.f_frsize;
966 #endif /* USE_STATFS */
968 if (statfs_result == -1)
972 g_set_error (error, G_IO_ERROR,
973 g_io_error_from_errno (errsv),
974 _("Error getting filesystem info: %s"),
979 info = g_file_info_new ();
981 attribute_matcher = g_file_attribute_matcher_new (attributes);
984 g_file_attribute_matcher_matches (attribute_matcher,
985 G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
988 gchar *localdir = g_path_get_dirname (local->filename);
989 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
993 if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
994 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
997 #if defined(USE_STATFS) || defined(USE_STATVFS)
998 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
1003 g_file_attribute_matcher_matches (attribute_matcher,
1004 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
1007 gchar *localdir = g_path_get_dirname (local->filename);
1008 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1012 if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
1013 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, (guint64)li.QuadPart);
1016 #if defined(USE_STATFS) || defined(USE_STATVFS)
1017 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
1019 #endif /* G_OS_WIN32 */
1023 g_file_attribute_matcher_matches (attribute_matcher,
1024 G_FILE_ATTRIBUTE_FILESYSTEM_USED))
1027 gchar *localdir = g_path_get_dirname (local->filename);
1028 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1029 ULARGE_INTEGER li_free;
1030 ULARGE_INTEGER li_total;
1033 if (GetDiskFreeSpaceExW (wdirname, &li_free, &li_total, NULL))
1034 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, (guint64)li_total.QuadPart - (guint64)li_free.QuadPart);
1037 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, block_size * (statfs_buffer.f_blocks - statfs_buffer.f_bfree));
1038 #endif /* G_OS_WIN32 */
1043 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1044 fstype = g_strdup (statfs_buffer.f_fstypename);
1046 fstype = get_fs_type (statfs_buffer.f_type);
1049 #elif defined(USE_STATVFS)
1050 #if defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
1051 fstype = g_strdup (statfs_buffer.f_fstypename);
1052 #elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1053 fstype = g_strdup (statfs_buffer.f_basetype);
1057 #endif /* USE_STATFS */
1060 g_file_attribute_matcher_matches (attribute_matcher,
1061 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1062 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1063 #endif /* G_OS_WIN32 */
1065 if (g_file_attribute_matcher_matches (attribute_matcher,
1066 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
1069 get_filesystem_readonly (info, local->filename);
1071 get_mount_info (info, local->filename, attribute_matcher);
1072 #endif /* G_OS_WIN32 */
1075 g_file_attribute_matcher_unref (attribute_matcher);
1081 g_local_file_find_enclosing_mount (GFile *file,
1082 GCancellable *cancellable,
1085 GLocalFile *local = G_LOCAL_FILE (file);
1090 if (g_lstat (local->filename, &buf) != 0)
1092 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1093 /* Translators: This is an error message when trying to
1094 * find the enclosing (user visible) mount of a file, but
1096 _("Containing mount does not exist"));
1100 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
1101 if (mountpoint == NULL)
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 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1112 g_free (mountpoint);
1116 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1117 /* Translators: This is an error message when trying to find
1118 * the enclosing (user visible) mount of a file, but none
1120 _("Containing mount does not exist"));
1125 g_local_file_set_display_name (GFile *file,
1126 const char *display_name,
1127 GCancellable *cancellable,
1130 GLocalFile *local, *new_local;
1131 GFile *new_file, *parent;
1137 parent = g_file_get_parent (file);
1140 g_set_error_literal (error, G_IO_ERROR,
1142 _("Can't rename root directory"));
1146 new_file = g_file_get_child_for_display_name (parent, display_name, error);
1147 g_object_unref (parent);
1149 if (new_file == NULL)
1151 local = G_LOCAL_FILE (file);
1152 new_local = G_LOCAL_FILE (new_file);
1154 if (g_lstat (new_local->filename, &statbuf) == -1)
1158 if (errsv != ENOENT)
1160 g_set_error (error, G_IO_ERROR,
1161 g_io_error_from_errno (errsv),
1162 _("Error renaming file: %s"),
1163 g_strerror (errsv));
1169 g_set_error_literal (error, G_IO_ERROR,
1171 _("Can't rename file, filename already exists"));
1175 if (g_rename (local->filename, new_local->filename) == -1)
1179 if (errsv == EINVAL)
1180 /* We can't get a rename file into itself error herer,
1181 so this must be an invalid filename, on e.g. FAT */
1182 g_set_error_literal (error, G_IO_ERROR,
1183 G_IO_ERROR_INVALID_FILENAME,
1184 _("Invalid filename"));
1186 g_set_error (error, G_IO_ERROR,
1187 g_io_error_from_errno (errsv),
1188 _("Error renaming file: %s"),
1189 g_strerror (errsv));
1190 g_object_unref (new_file);
1194 vfs = g_vfs_get_default ();
1195 class = G_VFS_GET_CLASS (vfs);
1196 if (class->local_file_moved)
1197 class->local_file_moved (vfs, local->filename, new_local->filename);
1203 g_local_file_query_info (GFile *file,
1204 const char *attributes,
1205 GFileQueryInfoFlags flags,
1206 GCancellable *cancellable,
1209 GLocalFile *local = G_LOCAL_FILE (file);
1211 GFileAttributeMatcher *matcher;
1212 char *basename, *dirname;
1213 GLocalParentFileInfo parent_info;
1215 matcher = g_file_attribute_matcher_new (attributes);
1217 basename = g_path_get_basename (local->filename);
1219 dirname = g_path_get_dirname (local->filename);
1220 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1223 info = _g_local_file_info_get (basename, local->filename,
1224 matcher, flags, &parent_info,
1228 _g_local_file_info_free_parent_info (&parent_info);
1231 g_file_attribute_matcher_unref (matcher);
1236 static GFileAttributeInfoList *
1237 g_local_file_query_settable_attributes (GFile *file,
1238 GCancellable *cancellable,
1241 return g_file_attribute_info_list_ref (local_writable_attributes);
1244 static GFileAttributeInfoList *
1245 g_local_file_query_writable_namespaces (GFile *file,
1246 GCancellable *cancellable,
1249 GFileAttributeInfoList *list;
1253 if (g_once_init_enter (&local_writable_namespaces))
1255 /* Writable namespaces: */
1257 list = g_file_attribute_info_list_new ();
1260 g_file_attribute_info_list_add (list,
1262 G_FILE_ATTRIBUTE_TYPE_STRING,
1263 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
1264 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1265 g_file_attribute_info_list_add (list,
1267 G_FILE_ATTRIBUTE_TYPE_STRING,
1268 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1271 vfs = g_vfs_get_default ();
1272 class = G_VFS_GET_CLASS (vfs);
1273 if (class->add_writable_namespaces)
1274 class->add_writable_namespaces (vfs, list);
1276 g_once_init_leave (&local_writable_namespaces, (gsize)list);
1278 list = (GFileAttributeInfoList *)local_writable_namespaces;
1280 return g_file_attribute_info_list_ref (list);
1284 g_local_file_set_attribute (GFile *file,
1285 const char *attribute,
1286 GFileAttributeType type,
1288 GFileQueryInfoFlags flags,
1289 GCancellable *cancellable,
1292 GLocalFile *local = G_LOCAL_FILE (file);
1294 return _g_local_file_info_set_attribute (local->filename,
1304 g_local_file_set_attributes_from_info (GFile *file,
1306 GFileQueryInfoFlags flags,
1307 GCancellable *cancellable,
1310 GLocalFile *local = G_LOCAL_FILE (file);
1311 int res, chained_res;
1312 GFileIface *default_iface;
1314 res = _g_local_file_info_set_attributes (local->filename,
1320 error = NULL; /* Don't write over error if further errors */
1322 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1324 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1326 return res && chained_res;
1329 static GFileInputStream *
1330 g_local_file_read (GFile *file,
1331 GCancellable *cancellable,
1334 GLocalFile *local = G_LOCAL_FILE (file);
1338 fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1344 if (errsv == EACCES)
1346 ret = _stati64 (local->filename, &buf);
1347 if (ret == 0 && S_ISDIR (buf.st_mode))
1349 g_set_error_literal (error, G_IO_ERROR,
1350 G_IO_ERROR_IS_DIRECTORY,
1351 _("Can't open directory"));
1357 g_set_error (error, G_IO_ERROR,
1358 g_io_error_from_errno (errsv),
1359 _("Error opening file: %s"),
1360 g_strerror (errsv));
1365 ret = _fstati64 (fd, &buf);
1367 ret = fstat (fd, &buf);
1370 if (ret == 0 && S_ISDIR (buf.st_mode))
1372 (void) g_close (fd, NULL);
1373 g_set_error_literal (error, G_IO_ERROR,
1374 G_IO_ERROR_IS_DIRECTORY,
1375 _("Can't open directory"));
1379 return _g_local_file_input_stream_new (fd);
1382 static GFileOutputStream *
1383 g_local_file_append_to (GFile *file,
1384 GFileCreateFlags flags,
1385 GCancellable *cancellable,
1388 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1389 flags, cancellable, error);
1392 static GFileOutputStream *
1393 g_local_file_create (GFile *file,
1394 GFileCreateFlags flags,
1395 GCancellable *cancellable,
1398 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1400 flags, cancellable, error);
1403 static GFileOutputStream *
1404 g_local_file_replace (GFile *file,
1406 gboolean make_backup,
1407 GFileCreateFlags flags,
1408 GCancellable *cancellable,
1411 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1413 etag, make_backup, flags,
1414 cancellable, error);
1417 static GFileIOStream *
1418 g_local_file_open_readwrite (GFile *file,
1419 GCancellable *cancellable,
1422 GFileOutputStream *output;
1425 output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
1427 cancellable, error);
1431 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1432 g_object_unref (output);
1436 static GFileIOStream *
1437 g_local_file_create_readwrite (GFile *file,
1438 GFileCreateFlags flags,
1439 GCancellable *cancellable,
1442 GFileOutputStream *output;
1445 output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1447 cancellable, error);
1451 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1452 g_object_unref (output);
1456 static GFileIOStream *
1457 g_local_file_replace_readwrite (GFile *file,
1459 gboolean make_backup,
1460 GFileCreateFlags flags,
1461 GCancellable *cancellable,
1464 GFileOutputStream *output;
1467 output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1469 etag, make_backup, flags,
1470 cancellable, error);
1474 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1475 g_object_unref (output);
1480 g_local_file_delete (GFile *file,
1481 GCancellable *cancellable,
1484 GLocalFile *local = G_LOCAL_FILE (file);
1488 if (g_remove (local->filename) == -1)
1492 /* Posix allows EEXIST too, but the more sane error
1493 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1495 if (errsv == EEXIST)
1498 g_set_error (error, G_IO_ERROR,
1499 g_io_error_from_errno (errsv),
1500 _("Error removing file: %s"),
1501 g_strerror (errsv));
1505 vfs = g_vfs_get_default ();
1506 class = G_VFS_GET_CLASS (vfs);
1507 if (class->local_file_removed)
1508 class->local_file_removed (vfs, local->filename);
1516 strip_trailing_slashes (const char *path)
1521 path_copy = g_strdup (path);
1522 len = strlen (path_copy);
1523 while (len > 1 && path_copy[len-1] == '/')
1524 path_copy[--len] = 0;
1530 expand_symlink (const char *link)
1532 char *resolved, *canonical, *parent, *link2;
1533 char symlink_value[4096];
1541 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1544 return g_strdup (link);
1545 symlink_value[res] = 0;
1548 if (g_path_is_absolute (symlink_value))
1549 return canonicalize_filename (symlink_value);
1552 link2 = strip_trailing_slashes (link);
1553 parent = g_path_get_dirname (link2);
1556 resolved = g_build_filename (parent, symlink_value, NULL);
1559 canonical = canonicalize_filename (resolved);
1568 get_parent (const char *path,
1572 GStatBuf parent_stat;
1576 path_copy = strip_trailing_slashes (path);
1578 parent = g_path_get_dirname (path_copy);
1579 if (strcmp (parent, ".") == 0 ||
1580 strcmp (parent, path_copy) == 0)
1590 if (g_lstat (parent, &parent_stat) != 0)
1596 if (S_ISLNK (parent_stat.st_mode))
1599 parent = expand_symlink (parent);
1604 if (num_recursions > 12)
1609 } while (S_ISLNK (parent_stat.st_mode));
1611 *parent_dev = parent_stat.st_dev;
1617 expand_all_symlinks (const char *path)
1619 char *parent, *parent_expanded;
1620 char *basename, *res;
1623 parent = get_parent (path, &parent_dev);
1626 parent_expanded = expand_all_symlinks (parent);
1628 basename = g_path_get_basename (path);
1629 res = g_build_filename (parent_expanded, basename, NULL);
1631 g_free (parent_expanded);
1634 res = g_strdup (path);
1640 find_mountpoint_for (const char *file,
1644 dev_t dir_dev, parent_dev;
1646 dir = g_strdup (file);
1651 parent = get_parent (dir, &parent_dev);
1655 if (parent_dev != dir_dev)
1667 find_topdir_for (const char *file)
1672 dir = get_parent (file, &dir_dev);
1676 return find_mountpoint_for (dir, dir_dev);
1680 get_unique_filename (const char *basename,
1686 return g_strdup (basename);
1688 dot = strchr (basename, '.');
1690 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1692 return g_strdup_printf ("%s.%d", basename, id);
1696 path_has_prefix (const char *path,
1704 prefix_len = strlen (prefix);
1706 if (strncmp (path, prefix, prefix_len) == 0 &&
1707 (prefix_len == 0 || /* empty prefix always matches */
1708 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1709 path[prefix_len] == 0 ||
1710 path[prefix_len] == '/'))
1717 try_make_relative (const char *path,
1720 char *path2, *base2;
1723 path2 = expand_all_symlinks (path);
1724 base2 = expand_all_symlinks (base);
1727 if (path_has_prefix (path2, base2))
1729 relative = path2 + strlen (base2);
1730 while (*relative == '/')
1732 relative = g_strdup (relative);
1740 /* Failed, use abs path */
1741 return g_strdup (path);
1745 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1747 static gsize home_dev_set = 0;
1748 static dev_t home_dev;
1749 char *topdir, *globaldir, *trashdir, *tmpname;
1752 GStatBuf global_stat, trash_stat;
1755 if (g_once_init_enter (&home_dev_set))
1759 g_stat (g_get_home_dir (), &home_stat);
1760 home_dev = home_stat.st_dev;
1761 g_once_init_leave (&home_dev_set, 1);
1764 /* Assume we can trash to the home */
1765 if (dir_dev == home_dev)
1768 topdir = find_mountpoint_for (dirname, dir_dev);
1772 globaldir = g_build_filename (topdir, ".Trash", NULL);
1773 if (g_lstat (globaldir, &global_stat) == 0 &&
1774 S_ISDIR (global_stat.st_mode) &&
1775 (global_stat.st_mode & S_ISVTX) != 0)
1777 /* got a toplevel sysadmin created dir, assume we
1778 * can trash to it (we should be able to create a dir)
1779 * This fails for the FAT case where the ownership of
1780 * that dir would be wrong though..
1788 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1790 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1792 tmpname = g_strdup_printf (".Trash-%s", uid_str);
1793 trashdir = g_build_filename (topdir, tmpname, NULL);
1796 if (g_lstat (trashdir, &trash_stat) == 0)
1800 return S_ISDIR (trash_stat.st_mode) &&
1801 trash_stat.st_uid == uid;
1805 /* User specific trash didn't exist, can we create it? */
1806 res = g_access (topdir, W_OK) == 0;
1814 _g_local_file_is_lost_found_dir (const char *path, dev_t path_dev)
1816 gboolean ret = FALSE;
1817 gchar *mount_dir = NULL;
1818 size_t mount_dir_len;
1821 if (!g_str_has_suffix (path, "/lost+found"))
1824 mount_dir = find_mountpoint_for (path, path_dev);
1825 if (mount_dir == NULL)
1828 mount_dir_len = strlen (mount_dir);
1829 /* We special-case rootfs ('/') since it's the only case where
1830 * mount_dir ends in '/'
1832 if (mount_dir_len == 1)
1834 if (mount_dir_len + strlen ("/lost+found") != strlen (path))
1837 if (g_lstat (path, &statbuf) != 0)
1840 if (!(S_ISDIR (statbuf.st_mode) &&
1841 statbuf.st_uid == 0 &&
1842 statbuf.st_gid == 0))
1854 g_local_file_trash (GFile *file,
1855 GCancellable *cancellable,
1858 GLocalFile *local = G_LOCAL_FILE (file);
1859 GStatBuf file_stat, home_stat;
1860 const char *homedir;
1861 char *trashdir, *topdir, *infodir, *filesdir;
1862 char *basename, *trashname, *trashfile, *infoname, *infofile;
1863 char *original_name, *original_name_escaped;
1866 gboolean is_homedir_trash;
1867 char delete_time[32];
1869 GStatBuf trash_stat, global_stat;
1870 char *dirname, *globaldir;
1874 if (g_lstat (local->filename, &file_stat) != 0)
1878 g_set_error (error, G_IO_ERROR,
1879 g_io_error_from_errno (errsv),
1880 _("Error trashing file: %s"),
1881 g_strerror (errsv));
1885 homedir = g_get_home_dir ();
1886 g_stat (homedir, &home_stat);
1888 is_homedir_trash = FALSE;
1890 if (file_stat.st_dev == home_stat.st_dev)
1892 is_homedir_trash = TRUE;
1894 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1895 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1900 display_name = g_filename_display_name (trashdir);
1901 g_set_error (error, G_IO_ERROR,
1902 g_io_error_from_errno (errsv),
1903 _("Unable to create trash dir %s: %s"),
1904 display_name, g_strerror (errsv));
1905 g_free (display_name);
1909 topdir = g_strdup (g_get_user_data_dir ());
1917 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1919 topdir = find_topdir_for (local->filename);
1922 g_set_error_literal (error, G_IO_ERROR,
1923 G_IO_ERROR_NOT_SUPPORTED,
1924 _("Unable to find toplevel directory for trash"));
1928 /* Try looking for global trash dir $topdir/.Trash/$uid */
1929 globaldir = g_build_filename (topdir, ".Trash", NULL);
1930 if (g_lstat (globaldir, &global_stat) == 0 &&
1931 S_ISDIR (global_stat.st_mode) &&
1932 (global_stat.st_mode & S_ISVTX) != 0)
1934 trashdir = g_build_filename (globaldir, uid_str, NULL);
1936 if (g_lstat (trashdir, &trash_stat) == 0)
1938 if (!S_ISDIR (trash_stat.st_mode) ||
1939 trash_stat.st_uid != uid)
1941 /* Not a directory or not owned by user, ignore */
1946 else if (g_mkdir (trashdir, 0700) == -1)
1954 if (trashdir == NULL)
1956 gboolean tried_create;
1958 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1959 dirname = g_strdup_printf (".Trash-%s", uid_str);
1960 trashdir = g_build_filename (topdir, dirname, NULL);
1963 tried_create = FALSE;
1966 if (g_lstat (trashdir, &trash_stat) == 0)
1968 if (!S_ISDIR (trash_stat.st_mode) ||
1969 trash_stat.st_uid != uid)
1971 /* Remove the failed directory */
1973 g_remove (trashdir);
1975 /* Not a directory or not owned by user, ignore */
1982 if (!tried_create &&
1983 g_mkdir (trashdir, 0700) != -1)
1985 /* Ensure that the created dir has the right uid etc.
1986 This might fail on e.g. a FAT dir */
1987 tried_create = TRUE;
1998 if (trashdir == NULL)
2001 g_set_error_literal (error, G_IO_ERROR,
2002 G_IO_ERROR_NOT_SUPPORTED,
2003 _("Unable to find or create trash directory"));
2008 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
2010 infodir = g_build_filename (trashdir, "info", NULL);
2011 filesdir = g_build_filename (trashdir, "files", NULL);
2014 /* Make sure we have the subdirectories */
2015 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
2016 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
2021 g_set_error_literal (error, G_IO_ERROR,
2022 G_IO_ERROR_NOT_SUPPORTED,
2023 _("Unable to find or create trash directory"));
2027 basename = g_path_get_basename (local->filename);
2035 trashname = get_unique_filename (basename, i++);
2036 infoname = g_strconcat (trashname, ".trashinfo", NULL);
2037 infofile = g_build_filename (infodir, infoname, NULL);
2040 fd = g_open (infofile, O_CREAT | O_EXCL, 0666);
2041 } while (fd == -1 && errno == EEXIST);
2055 g_set_error (error, G_IO_ERROR,
2056 g_io_error_from_errno (errsv),
2057 _("Unable to create trashing info file: %s"),
2058 g_strerror (errsv));
2062 (void) g_close (fd, NULL);
2064 /* TODO: Maybe we should verify that you can delete the file from the trash
2065 before moving it? OTOH, that is hard, as it needs a recursive scan */
2067 trashfile = g_build_filename (filesdir, trashname, NULL);
2071 if (g_rename (local->filename, trashfile) == -1)
2081 /* The trash dir was actually on another fs anyway!?
2082 This can happen when the same device is mounted multiple
2083 times, or with bind mounts of the same fs. */
2084 g_set_error (error, G_IO_ERROR,
2085 G_IO_ERROR_NOT_SUPPORTED,
2086 _("Unable to trash file: %s"),
2087 g_strerror (errsv));
2089 g_set_error (error, G_IO_ERROR,
2090 g_io_error_from_errno (errsv),
2091 _("Unable to trash file: %s"),
2092 g_strerror (errsv));
2096 vfs = g_vfs_get_default ();
2097 class = G_VFS_GET_CLASS (vfs);
2098 if (class->local_file_moved)
2099 class->local_file_moved (vfs, local->filename, trashfile);
2103 /* TODO: Do we need to update mtime/atime here after the move? */
2105 /* Use absolute names for homedir */
2106 if (is_homedir_trash)
2107 original_name = g_strdup (local->filename);
2109 original_name = try_make_relative (local->filename, topdir);
2110 original_name_escaped = g_uri_escape_string (original_name, "/", FALSE);
2112 g_free (original_name);
2119 localtime_r (&t, &now);
2121 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
2124 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2125 original_name_escaped, delete_time);
2127 g_file_set_contents (infofile, data, -1, NULL);
2131 g_free (original_name_escaped);
2136 #else /* G_OS_WIN32 */
2138 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
2140 return FALSE; /* XXX ??? */
2144 g_local_file_trash (GFile *file,
2145 GCancellable *cancellable,
2148 GLocalFile *local = G_LOCAL_FILE (file);
2149 SHFILEOPSTRUCTW op = {0};
2154 wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
2155 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2156 wfilename = g_renew (wchar_t, wfilename, len + 2);
2157 wfilename[len + 1] = 0;
2159 op.wFunc = FO_DELETE;
2160 op.pFrom = wfilename;
2161 op.fFlags = FOF_ALLOWUNDO;
2163 success = SHFileOperationW (&op) == 0;
2165 if (success && op.fAnyOperationsAborted)
2167 if (cancellable && !g_cancellable_is_cancelled (cancellable))
2168 g_cancellable_cancel (cancellable);
2169 g_set_error (error, G_IO_ERROR,
2170 G_IO_ERROR_CANCELLED,
2171 _("Unable to trash file: %s"),
2172 _("Operation was cancelled"));
2176 g_set_error (error, G_IO_ERROR,
2178 _("Unable to trash file: %s"),
2179 _("internal error"));
2184 #endif /* G_OS_WIN32 */
2187 g_local_file_make_directory (GFile *file,
2188 GCancellable *cancellable,
2191 GLocalFile *local = G_LOCAL_FILE (file);
2193 if (g_mkdir (local->filename, 0777) == -1)
2197 if (errsv == EINVAL)
2198 /* This must be an invalid filename, on e.g. FAT */
2199 g_set_error_literal (error, G_IO_ERROR,
2200 G_IO_ERROR_INVALID_FILENAME,
2201 _("Invalid filename"));
2203 g_set_error (error, G_IO_ERROR,
2204 g_io_error_from_errno (errsv),
2205 _("Error creating directory: %s"),
2206 g_strerror (errsv));
2214 g_local_file_make_symbolic_link (GFile *file,
2215 const char *symlink_value,
2216 GCancellable *cancellable,
2220 GLocalFile *local = G_LOCAL_FILE (file);
2222 if (symlink (symlink_value, local->filename) == -1)
2226 if (errsv == EINVAL)
2227 /* This must be an invalid filename, on e.g. FAT */
2228 g_set_error_literal (error, G_IO_ERROR,
2229 G_IO_ERROR_INVALID_FILENAME,
2230 _("Invalid filename"));
2231 else if (errsv == EPERM)
2232 g_set_error (error, G_IO_ERROR,
2233 G_IO_ERROR_NOT_SUPPORTED,
2234 _("Filesystem does not support symbolic links"));
2236 g_set_error (error, G_IO_ERROR,
2237 g_io_error_from_errno (errsv),
2238 _("Error making symbolic link: %s"),
2239 g_strerror (errsv));
2244 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
2251 g_local_file_copy (GFile *source,
2253 GFileCopyFlags flags,
2254 GCancellable *cancellable,
2255 GFileProgressCallback progress_callback,
2256 gpointer progress_callback_data,
2259 /* Fall back to default copy */
2260 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
2265 g_local_file_move (GFile *source,
2267 GFileCopyFlags flags,
2268 GCancellable *cancellable,
2269 GFileProgressCallback progress_callback,
2270 gpointer progress_callback_data,
2273 GLocalFile *local_source, *local_destination;
2275 gboolean destination_exist, source_is_dir;
2282 if (!G_IS_LOCAL_FILE (source) ||
2283 !G_IS_LOCAL_FILE (destination))
2285 /* Fall back to default move */
2286 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2290 local_source = G_LOCAL_FILE (source);
2291 local_destination = G_LOCAL_FILE (destination);
2293 res = g_lstat (local_source->filename, &statbuf);
2298 g_set_error (error, G_IO_ERROR,
2299 g_io_error_from_errno (errsv),
2300 _("Error moving file: %s"),
2301 g_strerror (errsv));
2305 source_is_dir = S_ISDIR (statbuf.st_mode);
2306 source_size = statbuf.st_size;
2308 destination_exist = FALSE;
2309 res = g_lstat (local_destination->filename, &statbuf);
2312 destination_exist = TRUE; /* Target file exists */
2314 if (flags & G_FILE_COPY_OVERWRITE)
2316 /* Always fail on dirs, even with overwrite */
2317 if (S_ISDIR (statbuf.st_mode))
2320 g_set_error_literal (error,
2322 G_IO_ERROR_WOULD_MERGE,
2323 _("Can't move directory over directory"));
2325 g_set_error_literal (error,
2327 G_IO_ERROR_IS_DIRECTORY,
2328 _("Can't copy over directory"));
2334 g_set_error_literal (error,
2337 _("Target file exists"));
2342 if (flags & G_FILE_COPY_BACKUP && destination_exist)
2344 backup_name = g_strconcat (local_destination->filename, "~", NULL);
2345 if (g_rename (local_destination->filename, backup_name) == -1)
2347 g_set_error_literal (error,
2349 G_IO_ERROR_CANT_CREATE_BACKUP,
2350 _("Backup file creation failed"));
2351 g_free (backup_name);
2354 g_free (backup_name);
2355 destination_exist = FALSE; /* It did, but no more */
2358 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2360 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2361 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2362 res = g_unlink (local_destination->filename);
2367 g_set_error (error, G_IO_ERROR,
2368 g_io_error_from_errno (errsv),
2369 _("Error removing target file: %s"),
2370 g_strerror (errsv));
2375 if (g_rename (local_source->filename, local_destination->filename) == -1)
2380 /* This will cause the fallback code to run */
2381 g_set_error_literal (error, G_IO_ERROR,
2382 G_IO_ERROR_NOT_SUPPORTED,
2383 _("Move between mounts not supported"));
2384 else if (errsv == EINVAL)
2385 /* This must be an invalid filename, on e.g. FAT, or
2386 we're trying to move the file into itself...
2387 We return invalid filename for both... */
2388 g_set_error_literal (error, G_IO_ERROR,
2389 G_IO_ERROR_INVALID_FILENAME,
2390 _("Invalid filename"));
2392 g_set_error (error, G_IO_ERROR,
2393 g_io_error_from_errno (errsv),
2394 _("Error moving file: %s"),
2395 g_strerror (errsv));
2399 vfs = g_vfs_get_default ();
2400 class = G_VFS_GET_CLASS (vfs);
2401 if (class->local_file_moved)
2402 class->local_file_moved (vfs, local_source->filename, local_destination->filename);
2404 /* Make sure we send full copied size */
2405 if (progress_callback)
2406 progress_callback (source_size, source_size, progress_callback_data);
2414 is_remote (const gchar *filename)
2422 is_remote_fs (const gchar *filename)
2424 const char *fsname = NULL;
2427 struct statfs statfs_buffer;
2428 int statfs_result = 0;
2430 #if STATFS_ARGS == 2
2431 statfs_result = statfs (filename, &statfs_buffer);
2432 #elif STATFS_ARGS == 4
2433 statfs_result = statfs (filename, &statfs_buffer, sizeof (statfs_buffer), 0);
2436 #elif defined(USE_STATVFS)
2437 struct statvfs statfs_buffer;
2439 statfs_result = statvfs (filename, &statfs_buffer);
2444 if (statfs_result == -1)
2448 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
2449 fsname = statfs_buffer.f_fstypename;
2451 fsname = get_fs_type (statfs_buffer.f_type);
2454 #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
2455 fsname = statfs_buffer.f_basetype;
2460 if (strcmp (fsname, "nfs") == 0)
2462 if (strcmp (fsname, "nfs4") == 0)
2470 is_remote (const gchar *filename)
2472 static gboolean remote_home;
2473 static gsize initialized;
2476 home = g_get_home_dir ();
2477 if (path_has_prefix (filename, home))
2479 if (g_once_init_enter (&initialized))
2481 remote_home = is_remote_fs (home);
2482 g_once_init_leave (&initialized, TRUE);
2489 #endif /* !G_OS_WIN32 */
2491 static GFileMonitor*
2492 g_local_file_monitor_dir (GFile *file,
2493 GFileMonitorFlags flags,
2494 GCancellable *cancellable,
2497 GLocalFile* local_file = G_LOCAL_FILE(file);
2498 return _g_local_directory_monitor_new (local_file->filename, flags, is_remote (local_file->filename), error);
2501 static GFileMonitor*
2502 g_local_file_monitor_file (GFile *file,
2503 GFileMonitorFlags flags,
2504 GCancellable *cancellable,
2507 GLocalFile* local_file = G_LOCAL_FILE(file);
2508 return _g_local_file_monitor_new (local_file->filename, flags, is_remote (local_file->filename), error);
2512 g_local_file_file_iface_init (GFileIface *iface)
2514 iface->dup = g_local_file_dup;
2515 iface->hash = g_local_file_hash;
2516 iface->equal = g_local_file_equal;
2517 iface->is_native = g_local_file_is_native;
2518 iface->has_uri_scheme = g_local_file_has_uri_scheme;
2519 iface->get_uri_scheme = g_local_file_get_uri_scheme;
2520 iface->get_basename = g_local_file_get_basename;
2521 iface->get_path = g_local_file_get_path;
2522 iface->get_uri = g_local_file_get_uri;
2523 iface->get_parse_name = g_local_file_get_parse_name;
2524 iface->get_parent = g_local_file_get_parent;
2525 iface->prefix_matches = g_local_file_prefix_matches;
2526 iface->get_relative_path = g_local_file_get_relative_path;
2527 iface->resolve_relative_path = g_local_file_resolve_relative_path;
2528 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2529 iface->set_display_name = g_local_file_set_display_name;
2530 iface->enumerate_children = g_local_file_enumerate_children;
2531 iface->query_info = g_local_file_query_info;
2532 iface->query_filesystem_info = g_local_file_query_filesystem_info;
2533 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2534 iface->query_settable_attributes = g_local_file_query_settable_attributes;
2535 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2536 iface->set_attribute = g_local_file_set_attribute;
2537 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2538 iface->read_fn = g_local_file_read;
2539 iface->append_to = g_local_file_append_to;
2540 iface->create = g_local_file_create;
2541 iface->replace = g_local_file_replace;
2542 iface->open_readwrite = g_local_file_open_readwrite;
2543 iface->create_readwrite = g_local_file_create_readwrite;
2544 iface->replace_readwrite = g_local_file_replace_readwrite;
2545 iface->delete_file = g_local_file_delete;
2546 iface->trash = g_local_file_trash;
2547 iface->make_directory = g_local_file_make_directory;
2548 iface->make_symbolic_link = g_local_file_make_symbolic_link;
2549 iface->copy = g_local_file_copy;
2550 iface->move = g_local_file_move;
2551 iface->monitor_dir = g_local_file_monitor_dir;
2552 iface->monitor_file = g_local_file_monitor_file;
2554 iface->supports_thread_contexts = TRUE;