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)
191 _g_local_file_get_filename (GLocalFile *file)
193 return file->filename;
197 canonicalize_filename (const char *filename)
199 char *canon, *start, *p, *q;
203 if (!g_path_is_absolute (filename))
205 cwd = g_get_current_dir ();
206 canon = g_build_filename (cwd, filename, NULL);
210 canon = g_strdup (filename);
212 start = (char *)g_path_skip_root (canon);
216 /* This shouldn't really happen, as g_get_current_dir() should
217 return an absolute pathname, but bug 573843 shows this is
218 not always happening */
220 return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
223 /* POSIX allows double slashes at the start to
224 * mean something special (as does windows too).
225 * So, "//" != "/", but more than two slashes
231 G_IS_DIR_SEPARATOR (*p);
238 memmove (start, start+i, strlen (start+i)+1);
241 /* Make sure we're using the canonical dir separator */
243 while (p < start && G_IS_DIR_SEPARATOR (*p))
244 *p++ = G_DIR_SEPARATOR;
249 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
251 memmove (p, p+1, strlen (p+1)+1);
253 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
256 /* Skip previous separator */
260 while (p > start && !G_IS_DIR_SEPARATOR (*p))
262 if (G_IS_DIR_SEPARATOR (*p))
263 *p++ = G_DIR_SEPARATOR;
264 memmove (p, q, strlen (q)+1);
268 /* Skip until next separator */
269 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
274 /* Canonicalize one separator */
275 *p++ = G_DIR_SEPARATOR;
279 /* Remove additional separators */
281 while (*q && G_IS_DIR_SEPARATOR (*q))
285 memmove (p, q, strlen (q)+1);
288 /* Remove trailing slashes */
289 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
296 _g_local_file_new (const char *filename)
300 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
301 local->filename = canonicalize_filename (filename);
303 return G_FILE (local);
307 g_local_file_is_native (GFile *file)
313 g_local_file_has_uri_scheme (GFile *file,
314 const char *uri_scheme)
316 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
320 g_local_file_get_uri_scheme (GFile *file)
322 return g_strdup ("file");
326 g_local_file_get_basename (GFile *file)
328 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
332 g_local_file_get_path (GFile *file)
334 return g_strdup (G_LOCAL_FILE (file)->filename);
338 g_local_file_get_uri (GFile *file)
340 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
344 get_filename_charset (const gchar **filename_charset)
346 const gchar **charsets;
349 is_utf8 = g_get_filename_charsets (&charsets);
351 if (filename_charset)
352 *filename_charset = charsets[0];
358 name_is_valid_for_display (const char *string,
359 gboolean is_valid_utf8)
363 if (!is_valid_utf8 &&
364 !g_utf8_validate (string, -1, NULL))
367 while ((c = *string++) != 0)
369 if (g_ascii_iscntrl (c))
377 g_local_file_get_parse_name (GFile *file)
379 const char *filename;
381 const gchar *charset;
383 char *roundtripped_filename;
384 gboolean free_utf8_filename;
385 gboolean is_valid_utf8;
388 filename = G_LOCAL_FILE (file)->filename;
389 if (get_filename_charset (&charset))
391 utf8_filename = (char *)filename;
392 free_utf8_filename = FALSE;
393 is_valid_utf8 = FALSE; /* Can't guarantee this */
397 utf8_filename = g_convert (filename, -1,
398 "UTF-8", charset, NULL, NULL, NULL);
399 free_utf8_filename = TRUE;
400 is_valid_utf8 = TRUE;
402 if (utf8_filename != NULL)
404 /* Make sure we can roundtrip: */
405 roundtripped_filename = g_convert (utf8_filename, -1,
406 charset, "UTF-8", NULL, NULL, NULL);
408 if (roundtripped_filename == NULL ||
409 strcmp (filename, roundtripped_filename) != 0)
411 g_free (utf8_filename);
412 utf8_filename = NULL;
415 g_free (roundtripped_filename);
419 if (utf8_filename != NULL &&
420 name_is_valid_for_display (utf8_filename, is_valid_utf8))
422 if (free_utf8_filename)
423 parse_name = utf8_filename;
425 parse_name = g_strdup (utf8_filename);
430 char *dup_filename, *p, *backslash;
432 /* Turn backslashes into forward slashes like
433 * g_filename_to_uri() would do (but we can't use that because
434 * it doesn't output IRIs).
436 dup_filename = g_strdup (filename);
437 filename = p = dup_filename;
439 while ((backslash = strchr (p, '\\')) != NULL)
446 escaped_path = g_uri_escape_string (filename,
447 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
449 parse_name = g_strconcat ("file://",
450 (*escaped_path != '/') ? "/" : "",
454 g_free (escaped_path);
456 g_free (dup_filename);
458 if (free_utf8_filename)
459 g_free (utf8_filename);
466 g_local_file_get_parent (GFile *file)
468 GLocalFile *local = G_LOCAL_FILE (file);
469 const char *non_root;
474 non_root = g_path_skip_root (local->filename);
478 dirname = g_path_get_dirname (local->filename);
479 parent = _g_local_file_new (dirname);
485 g_local_file_dup (GFile *file)
487 GLocalFile *local = G_LOCAL_FILE (file);
489 return _g_local_file_new (local->filename);
493 g_local_file_hash (GFile *file)
495 GLocalFile *local = G_LOCAL_FILE (file);
497 return g_str_hash (local->filename);
501 g_local_file_equal (GFile *file1,
504 GLocalFile *local1 = G_LOCAL_FILE (file1);
505 GLocalFile *local2 = G_LOCAL_FILE (file2);
507 return g_str_equal (local1->filename, local2->filename);
511 match_prefix (const char *path,
516 prefix_len = strlen (prefix);
517 if (strncmp (path, prefix, prefix_len) != 0)
520 /* Handle the case where prefix is the root, so that
521 * the IS_DIR_SEPRARATOR check below works */
522 if (prefix_len > 0 &&
523 G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
526 return path + prefix_len;
530 g_local_file_prefix_matches (GFile *parent,
533 GLocalFile *parent_local = G_LOCAL_FILE (parent);
534 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
535 const char *remainder;
537 remainder = match_prefix (descendant_local->filename, parent_local->filename);
538 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
544 g_local_file_get_relative_path (GFile *parent,
547 GLocalFile *parent_local = G_LOCAL_FILE (parent);
548 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
549 const char *remainder;
551 remainder = match_prefix (descendant_local->filename, parent_local->filename);
553 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
554 return g_strdup (remainder + 1);
559 g_local_file_resolve_relative_path (GFile *file,
560 const char *relative_path)
562 GLocalFile *local = G_LOCAL_FILE (file);
566 if (g_path_is_absolute (relative_path))
567 return _g_local_file_new (relative_path);
569 filename = g_build_filename (local->filename, relative_path, NULL);
570 child = _g_local_file_new (filename);
576 static GFileEnumerator *
577 g_local_file_enumerate_children (GFile *file,
578 const char *attributes,
579 GFileQueryInfoFlags flags,
580 GCancellable *cancellable,
583 GLocalFile *local = G_LOCAL_FILE (file);
584 return _g_local_file_enumerator_new (local,
590 g_local_file_get_child_for_display_name (GFile *file,
591 const char *display_name,
597 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
598 if (basename == NULL)
600 g_set_error (error, G_IO_ERROR,
601 G_IO_ERROR_INVALID_FILENAME,
602 _("Invalid filename %s"), display_name);
606 new_file = g_file_get_child (file, basename);
612 #if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
614 get_fs_type (long f_type)
616 /* filesystem ids taken from linux manpage */
723 G_LOCK_DEFINE_STATIC(mount_info_hash);
724 static GHashTable *mount_info_hash = NULL;
725 static guint64 mount_info_hash_cache_time = 0;
728 MOUNT_INFO_READONLY = 1<<0
732 device_equal (gconstpointer v1,
735 return *(dev_t *)v1 == *(dev_t *)v2;
739 device_hash (gconstpointer v)
741 return (guint) *(dev_t *)v;
745 get_mount_info (GFileInfo *fs_info,
747 GFileAttributeMatcher *matcher)
751 gpointer info_as_ptr;
755 GUnixMountEntry *mount;
758 if (g_lstat (path, &buf) != 0)
761 G_LOCK (mount_info_hash);
763 if (mount_info_hash == NULL)
764 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
768 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
769 g_hash_table_remove_all (mount_info_hash);
771 got_info = g_hash_table_lookup_extended (mount_info_hash,
776 G_UNLOCK (mount_info_hash);
778 mount_info = GPOINTER_TO_UINT (info_as_ptr);
784 mountpoint = find_mountpoint_for (path, buf.st_dev);
785 if (mountpoint == NULL)
786 mountpoint = g_strdup ("/");
788 mount = g_unix_mount_at (mountpoint, &cache_time);
791 if (g_unix_mount_is_readonly (mount))
792 mount_info |= MOUNT_INFO_READONLY;
794 g_unix_mount_free (mount);
799 dev = g_new0 (dev_t, 1);
802 G_LOCK (mount_info_hash);
803 mount_info_hash_cache_time = cache_time;
804 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
805 G_UNLOCK (mount_info_hash);
808 if (mount_info & MOUNT_INFO_READONLY)
809 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
817 is_xp_or_later (void)
819 static int result = -1;
824 OSVERSIONINFOEX ver_info = {0};
825 DWORDLONG cond_mask = 0;
826 int op = VER_GREATER_EQUAL;
828 ver_info.dwOSVersionInfoSize = sizeof ver_info;
829 ver_info.dwMajorVersion = 5;
830 ver_info.dwMinorVersion = 1;
832 VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
833 VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
835 result = VerifyVersionInfo (&ver_info,
836 VER_MAJORVERSION | VER_MINORVERSION,
839 result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;
847 get_volume_for_path (const char *path)
853 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
854 result = g_new (wchar_t, MAX_PATH);
856 if (!GetVolumePathNameW (wpath, result, MAX_PATH))
858 char *msg = g_win32_error_message (GetLastError ());
859 g_critical ("GetVolumePathName failed: %s", msg);
866 len = wcslen (result);
867 if (len > 0 && result[len-1] != L'\\')
869 result = g_renew (wchar_t, result, len + 2);
879 find_mountpoint_for (const char *file, dev_t dev)
884 wpath = get_volume_for_path (file);
888 utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
895 get_filesystem_readonly (GFileInfo *info,
900 rootdir = get_volume_for_path (path);
904 if (is_xp_or_later ())
907 if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
908 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
909 (flags & FILE_READ_ONLY_VOLUME) != 0);
913 if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
914 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
921 #endif /* G_OS_WIN32 */
924 g_local_file_query_filesystem_info (GFile *file,
925 const char *attributes,
926 GCancellable *cancellable,
929 GLocalFile *local = G_LOCAL_FILE (file);
931 int statfs_result = 0;
937 struct statfs statfs_buffer;
938 #elif defined(USE_STATVFS)
940 struct statvfs statfs_buffer;
941 #endif /* USE_STATFS */
942 #endif /* G_OS_WIN32 */
943 GFileAttributeMatcher *attribute_matcher;
950 statfs_result = statfs (local->filename, &statfs_buffer);
951 #elif STATFS_ARGS == 4
952 statfs_result = statfs (local->filename, &statfs_buffer,
953 sizeof (statfs_buffer), 0);
954 #endif /* STATFS_ARGS == 2 */
955 block_size = statfs_buffer.f_bsize;
957 /* Many backends can't report free size (for instance the gvfs fuse
958 backend for backend not supporting this), and set f_bfree to 0,
959 but it can be 0 for real too. We treat the available == 0 and
960 free == 0 case as "both of these are invalid".
963 if (statfs_result == 0 &&
964 statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0)
966 #endif /* G_OS_WIN32 */
968 #elif defined(USE_STATVFS)
969 statfs_result = statvfs (local->filename, &statfs_buffer);
970 block_size = statfs_buffer.f_frsize;
971 #endif /* USE_STATFS */
973 if (statfs_result == -1)
977 g_set_error (error, G_IO_ERROR,
978 g_io_error_from_errno (errsv),
979 _("Error getting filesystem info: %s"),
984 info = g_file_info_new ();
986 attribute_matcher = g_file_attribute_matcher_new (attributes);
989 g_file_attribute_matcher_matches (attribute_matcher,
990 G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
993 gchar *localdir = g_path_get_dirname (local->filename);
994 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
998 if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
999 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
1002 #if defined(USE_STATFS) || defined(USE_STATVFS)
1003 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
1008 g_file_attribute_matcher_matches (attribute_matcher,
1009 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
1012 gchar *localdir = g_path_get_dirname (local->filename);
1013 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1017 if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
1018 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, (guint64)li.QuadPart);
1021 #if defined(USE_STATFS) || defined(USE_STATVFS)
1022 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
1024 #endif /* G_OS_WIN32 */
1028 g_file_attribute_matcher_matches (attribute_matcher,
1029 G_FILE_ATTRIBUTE_FILESYSTEM_USED))
1032 gchar *localdir = g_path_get_dirname (local->filename);
1033 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1034 ULARGE_INTEGER li_free;
1035 ULARGE_INTEGER li_total;
1038 if (GetDiskFreeSpaceExW (wdirname, &li_free, &li_total, NULL))
1039 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, (guint64)li_total.QuadPart - (guint64)li_free.QuadPart);
1042 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, block_size * (statfs_buffer.f_blocks - statfs_buffer.f_bfree));
1043 #endif /* G_OS_WIN32 */
1048 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1049 fstype = g_strdup (statfs_buffer.f_fstypename);
1051 fstype = get_fs_type (statfs_buffer.f_type);
1054 #elif defined(USE_STATVFS)
1055 #if defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
1056 fstype = g_strdup (statfs_buffer.f_fstypename);
1057 #elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1058 fstype = g_strdup (statfs_buffer.f_basetype);
1062 #endif /* USE_STATFS */
1065 g_file_attribute_matcher_matches (attribute_matcher,
1066 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1067 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1068 #endif /* G_OS_WIN32 */
1070 if (g_file_attribute_matcher_matches (attribute_matcher,
1071 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
1074 get_filesystem_readonly (info, local->filename);
1076 get_mount_info (info, local->filename, attribute_matcher);
1077 #endif /* G_OS_WIN32 */
1080 g_file_attribute_matcher_unref (attribute_matcher);
1086 g_local_file_find_enclosing_mount (GFile *file,
1087 GCancellable *cancellable,
1090 GLocalFile *local = G_LOCAL_FILE (file);
1095 if (g_lstat (local->filename, &buf) != 0)
1097 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1098 /* Translators: This is an error message when trying to
1099 * find the enclosing (user visible) mount of a file, but
1101 _("Containing mount does not exist"));
1105 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
1106 if (mountpoint == NULL)
1108 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1109 /* Translators: This is an error message when trying to
1110 * find the enclosing (user visible) mount of a file, but
1112 _("Containing mount does not exist"));
1116 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1117 g_free (mountpoint);
1121 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1122 /* Translators: This is an error message when trying to find
1123 * the enclosing (user visible) mount of a file, but none
1125 _("Containing mount does not exist"));
1130 g_local_file_set_display_name (GFile *file,
1131 const char *display_name,
1132 GCancellable *cancellable,
1135 GLocalFile *local, *new_local;
1136 GFile *new_file, *parent;
1142 parent = g_file_get_parent (file);
1145 g_set_error_literal (error, G_IO_ERROR,
1147 _("Can't rename root directory"));
1151 new_file = g_file_get_child_for_display_name (parent, display_name, error);
1152 g_object_unref (parent);
1154 if (new_file == NULL)
1156 local = G_LOCAL_FILE (file);
1157 new_local = G_LOCAL_FILE (new_file);
1159 if (g_lstat (new_local->filename, &statbuf) == -1)
1163 if (errsv != ENOENT)
1165 g_set_error (error, G_IO_ERROR,
1166 g_io_error_from_errno (errsv),
1167 _("Error renaming file: %s"),
1168 g_strerror (errsv));
1174 g_set_error_literal (error, G_IO_ERROR,
1176 _("Can't rename file, filename already exists"));
1180 if (g_rename (local->filename, new_local->filename) == -1)
1184 if (errsv == EINVAL)
1185 /* We can't get a rename file into itself error herer,
1186 so this must be an invalid filename, on e.g. FAT */
1187 g_set_error_literal (error, G_IO_ERROR,
1188 G_IO_ERROR_INVALID_FILENAME,
1189 _("Invalid filename"));
1191 g_set_error (error, G_IO_ERROR,
1192 g_io_error_from_errno (errsv),
1193 _("Error renaming file: %s"),
1194 g_strerror (errsv));
1195 g_object_unref (new_file);
1199 vfs = g_vfs_get_default ();
1200 class = G_VFS_GET_CLASS (vfs);
1201 if (class->local_file_moved)
1202 class->local_file_moved (vfs, local->filename, new_local->filename);
1208 g_local_file_query_info (GFile *file,
1209 const char *attributes,
1210 GFileQueryInfoFlags flags,
1211 GCancellable *cancellable,
1214 GLocalFile *local = G_LOCAL_FILE (file);
1216 GFileAttributeMatcher *matcher;
1217 char *basename, *dirname;
1218 GLocalParentFileInfo parent_info;
1220 matcher = g_file_attribute_matcher_new (attributes);
1222 basename = g_path_get_basename (local->filename);
1224 dirname = g_path_get_dirname (local->filename);
1225 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1228 info = _g_local_file_info_get (basename, local->filename,
1229 matcher, flags, &parent_info,
1233 _g_local_file_info_free_parent_info (&parent_info);
1236 g_file_attribute_matcher_unref (matcher);
1241 static GFileAttributeInfoList *
1242 g_local_file_query_settable_attributes (GFile *file,
1243 GCancellable *cancellable,
1246 return g_file_attribute_info_list_ref (local_writable_attributes);
1249 static GFileAttributeInfoList *
1250 g_local_file_query_writable_namespaces (GFile *file,
1251 GCancellable *cancellable,
1254 GFileAttributeInfoList *list;
1258 if (g_once_init_enter (&local_writable_namespaces))
1260 /* Writable namespaces: */
1262 list = g_file_attribute_info_list_new ();
1265 g_file_attribute_info_list_add (list,
1267 G_FILE_ATTRIBUTE_TYPE_STRING,
1268 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
1269 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1270 g_file_attribute_info_list_add (list,
1272 G_FILE_ATTRIBUTE_TYPE_STRING,
1273 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1276 vfs = g_vfs_get_default ();
1277 class = G_VFS_GET_CLASS (vfs);
1278 if (class->add_writable_namespaces)
1279 class->add_writable_namespaces (vfs, list);
1281 g_once_init_leave (&local_writable_namespaces, (gsize)list);
1283 list = (GFileAttributeInfoList *)local_writable_namespaces;
1285 return g_file_attribute_info_list_ref (list);
1289 g_local_file_set_attribute (GFile *file,
1290 const char *attribute,
1291 GFileAttributeType type,
1293 GFileQueryInfoFlags flags,
1294 GCancellable *cancellable,
1297 GLocalFile *local = G_LOCAL_FILE (file);
1299 return _g_local_file_info_set_attribute (local->filename,
1309 g_local_file_set_attributes_from_info (GFile *file,
1311 GFileQueryInfoFlags flags,
1312 GCancellable *cancellable,
1315 GLocalFile *local = G_LOCAL_FILE (file);
1316 int res, chained_res;
1317 GFileIface *default_iface;
1319 res = _g_local_file_info_set_attributes (local->filename,
1325 error = NULL; /* Don't write over error if further errors */
1327 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1329 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1331 return res && chained_res;
1334 static GFileInputStream *
1335 g_local_file_read (GFile *file,
1336 GCancellable *cancellable,
1339 GLocalFile *local = G_LOCAL_FILE (file);
1343 fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1349 if (errsv == EACCES)
1351 ret = _stati64 (local->filename, &buf);
1352 if (ret == 0 && S_ISDIR (buf.st_mode))
1354 g_set_error_literal (error, G_IO_ERROR,
1355 G_IO_ERROR_IS_DIRECTORY,
1356 _("Can't open directory"));
1362 g_set_error (error, G_IO_ERROR,
1363 g_io_error_from_errno (errsv),
1364 _("Error opening file: %s"),
1365 g_strerror (errsv));
1370 ret = _fstati64 (fd, &buf);
1372 ret = fstat (fd, &buf);
1375 if (ret == 0 && S_ISDIR (buf.st_mode))
1377 (void) g_close (fd, NULL);
1378 g_set_error_literal (error, G_IO_ERROR,
1379 G_IO_ERROR_IS_DIRECTORY,
1380 _("Can't open directory"));
1384 return _g_local_file_input_stream_new (fd);
1387 static GFileOutputStream *
1388 g_local_file_append_to (GFile *file,
1389 GFileCreateFlags flags,
1390 GCancellable *cancellable,
1393 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1394 flags, cancellable, error);
1397 static GFileOutputStream *
1398 g_local_file_create (GFile *file,
1399 GFileCreateFlags flags,
1400 GCancellable *cancellable,
1403 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1405 cancellable, error);
1408 static GFileOutputStream *
1409 g_local_file_replace (GFile *file,
1411 gboolean make_backup,
1412 GFileCreateFlags flags,
1413 GCancellable *cancellable,
1416 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1418 etag, make_backup, flags, NULL,
1419 cancellable, error);
1422 static GFileIOStream *
1423 g_local_file_open_readwrite (GFile *file,
1424 GCancellable *cancellable,
1427 GFileOutputStream *output;
1430 output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
1432 cancellable, error);
1436 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1437 g_object_unref (output);
1441 static GFileIOStream *
1442 g_local_file_create_readwrite (GFile *file,
1443 GFileCreateFlags flags,
1444 GCancellable *cancellable,
1447 GFileOutputStream *output;
1450 output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1452 cancellable, error);
1456 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1457 g_object_unref (output);
1461 static GFileIOStream *
1462 g_local_file_replace_readwrite (GFile *file,
1464 gboolean make_backup,
1465 GFileCreateFlags flags,
1466 GCancellable *cancellable,
1469 GFileOutputStream *output;
1472 output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1474 etag, make_backup, flags, NULL,
1475 cancellable, error);
1479 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1480 g_object_unref (output);
1485 g_local_file_delete (GFile *file,
1486 GCancellable *cancellable,
1489 GLocalFile *local = G_LOCAL_FILE (file);
1493 if (g_remove (local->filename) == -1)
1497 /* Posix allows EEXIST too, but the more sane error
1498 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1500 if (errsv == EEXIST)
1503 g_set_error (error, G_IO_ERROR,
1504 g_io_error_from_errno (errsv),
1505 _("Error removing file: %s"),
1506 g_strerror (errsv));
1510 vfs = g_vfs_get_default ();
1511 class = G_VFS_GET_CLASS (vfs);
1512 if (class->local_file_removed)
1513 class->local_file_removed (vfs, local->filename);
1521 strip_trailing_slashes (const char *path)
1526 path_copy = g_strdup (path);
1527 len = strlen (path_copy);
1528 while (len > 1 && path_copy[len-1] == '/')
1529 path_copy[--len] = 0;
1535 expand_symlink (const char *link)
1537 char *resolved, *canonical, *parent, *link2;
1538 char symlink_value[4096];
1546 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1549 return g_strdup (link);
1550 symlink_value[res] = 0;
1553 if (g_path_is_absolute (symlink_value))
1554 return canonicalize_filename (symlink_value);
1557 link2 = strip_trailing_slashes (link);
1558 parent = g_path_get_dirname (link2);
1561 resolved = g_build_filename (parent, symlink_value, NULL);
1564 canonical = canonicalize_filename (resolved);
1573 get_parent (const char *path,
1577 GStatBuf parent_stat;
1581 path_copy = strip_trailing_slashes (path);
1583 parent = g_path_get_dirname (path_copy);
1584 if (strcmp (parent, ".") == 0 ||
1585 strcmp (parent, path_copy) == 0)
1595 if (g_lstat (parent, &parent_stat) != 0)
1601 if (S_ISLNK (parent_stat.st_mode))
1604 parent = expand_symlink (parent);
1609 if (num_recursions > 12)
1614 } while (S_ISLNK (parent_stat.st_mode));
1616 *parent_dev = parent_stat.st_dev;
1622 expand_all_symlinks (const char *path)
1624 char *parent, *parent_expanded;
1625 char *basename, *res;
1628 parent = get_parent (path, &parent_dev);
1631 parent_expanded = expand_all_symlinks (parent);
1633 basename = g_path_get_basename (path);
1634 res = g_build_filename (parent_expanded, basename, NULL);
1636 g_free (parent_expanded);
1639 res = g_strdup (path);
1645 find_mountpoint_for (const char *file,
1649 dev_t dir_dev, parent_dev;
1651 dir = g_strdup (file);
1656 parent = get_parent (dir, &parent_dev);
1660 if (parent_dev != dir_dev)
1672 find_topdir_for (const char *file)
1677 dir = get_parent (file, &dir_dev);
1681 return find_mountpoint_for (dir, dir_dev);
1685 get_unique_filename (const char *basename,
1691 return g_strdup (basename);
1693 dot = strchr (basename, '.');
1695 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1697 return g_strdup_printf ("%s.%d", basename, id);
1701 path_has_prefix (const char *path,
1709 prefix_len = strlen (prefix);
1711 if (strncmp (path, prefix, prefix_len) == 0 &&
1712 (prefix_len == 0 || /* empty prefix always matches */
1713 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1714 path[prefix_len] == 0 ||
1715 path[prefix_len] == '/'))
1722 try_make_relative (const char *path,
1725 char *path2, *base2;
1728 path2 = expand_all_symlinks (path);
1729 base2 = expand_all_symlinks (base);
1732 if (path_has_prefix (path2, base2))
1734 relative = path2 + strlen (base2);
1735 while (*relative == '/')
1737 relative = g_strdup (relative);
1745 /* Failed, use abs path */
1746 return g_strdup (path);
1750 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1752 static gsize home_dev_set = 0;
1753 static dev_t home_dev;
1754 char *topdir, *globaldir, *trashdir, *tmpname;
1757 GStatBuf global_stat, trash_stat;
1760 if (g_once_init_enter (&home_dev_set))
1764 g_stat (g_get_home_dir (), &home_stat);
1765 home_dev = home_stat.st_dev;
1766 g_once_init_leave (&home_dev_set, 1);
1769 /* Assume we can trash to the home */
1770 if (dir_dev == home_dev)
1773 topdir = find_mountpoint_for (dirname, dir_dev);
1777 globaldir = g_build_filename (topdir, ".Trash", NULL);
1778 if (g_lstat (globaldir, &global_stat) == 0 &&
1779 S_ISDIR (global_stat.st_mode) &&
1780 (global_stat.st_mode & S_ISVTX) != 0)
1782 /* got a toplevel sysadmin created dir, assume we
1783 * can trash to it (we should be able to create a dir)
1784 * This fails for the FAT case where the ownership of
1785 * that dir would be wrong though..
1793 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1795 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1797 tmpname = g_strdup_printf (".Trash-%s", uid_str);
1798 trashdir = g_build_filename (topdir, tmpname, NULL);
1801 if (g_lstat (trashdir, &trash_stat) == 0)
1805 return S_ISDIR (trash_stat.st_mode) &&
1806 trash_stat.st_uid == uid;
1810 /* User specific trash didn't exist, can we create it? */
1811 res = g_access (topdir, W_OK) == 0;
1819 _g_local_file_is_lost_found_dir (const char *path, dev_t path_dev)
1821 gboolean ret = FALSE;
1822 gchar *mount_dir = NULL;
1823 size_t mount_dir_len;
1826 if (!g_str_has_suffix (path, "/lost+found"))
1829 mount_dir = find_mountpoint_for (path, path_dev);
1830 if (mount_dir == NULL)
1833 mount_dir_len = strlen (mount_dir);
1834 /* We special-case rootfs ('/') since it's the only case where
1835 * mount_dir ends in '/'
1837 if (mount_dir_len == 1)
1839 if (mount_dir_len + strlen ("/lost+found") != strlen (path))
1842 if (g_lstat (path, &statbuf) != 0)
1845 if (!(S_ISDIR (statbuf.st_mode) &&
1846 statbuf.st_uid == 0 &&
1847 statbuf.st_gid == 0))
1859 g_local_file_trash (GFile *file,
1860 GCancellable *cancellable,
1863 GLocalFile *local = G_LOCAL_FILE (file);
1864 GStatBuf file_stat, home_stat;
1865 const char *homedir;
1866 char *trashdir, *topdir, *infodir, *filesdir;
1867 char *basename, *trashname, *trashfile, *infoname, *infofile;
1868 char *original_name, *original_name_escaped;
1871 gboolean is_homedir_trash;
1872 char delete_time[32];
1874 GStatBuf trash_stat, global_stat;
1875 char *dirname, *globaldir;
1879 if (g_lstat (local->filename, &file_stat) != 0)
1883 g_set_error (error, G_IO_ERROR,
1884 g_io_error_from_errno (errsv),
1885 _("Error trashing file: %s"),
1886 g_strerror (errsv));
1890 homedir = g_get_home_dir ();
1891 g_stat (homedir, &home_stat);
1893 is_homedir_trash = FALSE;
1895 if (file_stat.st_dev == home_stat.st_dev)
1897 is_homedir_trash = TRUE;
1899 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1900 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1905 display_name = g_filename_display_name (trashdir);
1906 g_set_error (error, G_IO_ERROR,
1907 g_io_error_from_errno (errsv),
1908 _("Unable to create trash dir %s: %s"),
1909 display_name, g_strerror (errsv));
1910 g_free (display_name);
1914 topdir = g_strdup (g_get_user_data_dir ());
1922 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1924 topdir = find_topdir_for (local->filename);
1927 g_set_error_literal (error, G_IO_ERROR,
1928 G_IO_ERROR_NOT_SUPPORTED,
1929 _("Unable to find toplevel directory for trash"));
1933 /* Try looking for global trash dir $topdir/.Trash/$uid */
1934 globaldir = g_build_filename (topdir, ".Trash", NULL);
1935 if (g_lstat (globaldir, &global_stat) == 0 &&
1936 S_ISDIR (global_stat.st_mode) &&
1937 (global_stat.st_mode & S_ISVTX) != 0)
1939 trashdir = g_build_filename (globaldir, uid_str, NULL);
1941 if (g_lstat (trashdir, &trash_stat) == 0)
1943 if (!S_ISDIR (trash_stat.st_mode) ||
1944 trash_stat.st_uid != uid)
1946 /* Not a directory or not owned by user, ignore */
1951 else if (g_mkdir (trashdir, 0700) == -1)
1959 if (trashdir == NULL)
1961 gboolean tried_create;
1963 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1964 dirname = g_strdup_printf (".Trash-%s", uid_str);
1965 trashdir = g_build_filename (topdir, dirname, NULL);
1968 tried_create = FALSE;
1971 if (g_lstat (trashdir, &trash_stat) == 0)
1973 if (!S_ISDIR (trash_stat.st_mode) ||
1974 trash_stat.st_uid != uid)
1976 /* Remove the failed directory */
1978 g_remove (trashdir);
1980 /* Not a directory or not owned by user, ignore */
1987 if (!tried_create &&
1988 g_mkdir (trashdir, 0700) != -1)
1990 /* Ensure that the created dir has the right uid etc.
1991 This might fail on e.g. a FAT dir */
1992 tried_create = TRUE;
2003 if (trashdir == NULL)
2006 g_set_error_literal (error, G_IO_ERROR,
2007 G_IO_ERROR_NOT_SUPPORTED,
2008 _("Unable to find or create trash directory"));
2013 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
2015 infodir = g_build_filename (trashdir, "info", NULL);
2016 filesdir = g_build_filename (trashdir, "files", NULL);
2019 /* Make sure we have the subdirectories */
2020 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
2021 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
2026 g_set_error_literal (error, G_IO_ERROR,
2027 G_IO_ERROR_NOT_SUPPORTED,
2028 _("Unable to find or create trash directory"));
2032 basename = g_path_get_basename (local->filename);
2040 trashname = get_unique_filename (basename, i++);
2041 infoname = g_strconcat (trashname, ".trashinfo", NULL);
2042 infofile = g_build_filename (infodir, infoname, NULL);
2045 fd = g_open (infofile, O_CREAT | O_EXCL, 0666);
2046 } while (fd == -1 && errno == EEXIST);
2060 g_set_error (error, G_IO_ERROR,
2061 g_io_error_from_errno (errsv),
2062 _("Unable to create trashing info file: %s"),
2063 g_strerror (errsv));
2067 (void) g_close (fd, NULL);
2069 /* TODO: Maybe we should verify that you can delete the file from the trash
2070 before moving it? OTOH, that is hard, as it needs a recursive scan */
2072 trashfile = g_build_filename (filesdir, trashname, NULL);
2076 if (g_rename (local->filename, trashfile) == -1)
2086 /* The trash dir was actually on another fs anyway!?
2087 This can happen when the same device is mounted multiple
2088 times, or with bind mounts of the same fs. */
2089 g_set_error (error, G_IO_ERROR,
2090 G_IO_ERROR_NOT_SUPPORTED,
2091 _("Unable to trash file: %s"),
2092 g_strerror (errsv));
2094 g_set_error (error, G_IO_ERROR,
2095 g_io_error_from_errno (errsv),
2096 _("Unable to trash file: %s"),
2097 g_strerror (errsv));
2101 vfs = g_vfs_get_default ();
2102 class = G_VFS_GET_CLASS (vfs);
2103 if (class->local_file_moved)
2104 class->local_file_moved (vfs, local->filename, trashfile);
2108 /* TODO: Do we need to update mtime/atime here after the move? */
2110 /* Use absolute names for homedir */
2111 if (is_homedir_trash)
2112 original_name = g_strdup (local->filename);
2114 original_name = try_make_relative (local->filename, topdir);
2115 original_name_escaped = g_uri_escape_string (original_name, "/", FALSE);
2117 g_free (original_name);
2124 localtime_r (&t, &now);
2126 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
2129 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2130 original_name_escaped, delete_time);
2132 g_file_set_contents (infofile, data, -1, NULL);
2136 g_free (original_name_escaped);
2141 #else /* G_OS_WIN32 */
2143 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
2145 return FALSE; /* XXX ??? */
2149 g_local_file_trash (GFile *file,
2150 GCancellable *cancellable,
2153 GLocalFile *local = G_LOCAL_FILE (file);
2154 SHFILEOPSTRUCTW op = {0};
2159 wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
2160 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2161 wfilename = g_renew (wchar_t, wfilename, len + 2);
2162 wfilename[len + 1] = 0;
2164 op.wFunc = FO_DELETE;
2165 op.pFrom = wfilename;
2166 op.fFlags = FOF_ALLOWUNDO;
2168 success = SHFileOperationW (&op) == 0;
2170 if (success && op.fAnyOperationsAborted)
2172 if (cancellable && !g_cancellable_is_cancelled (cancellable))
2173 g_cancellable_cancel (cancellable);
2174 g_set_error (error, G_IO_ERROR,
2175 G_IO_ERROR_CANCELLED,
2176 _("Unable to trash file: %s"),
2177 _("Operation was cancelled"));
2181 g_set_error (error, G_IO_ERROR,
2183 _("Unable to trash file: %s"),
2184 _("internal error"));
2189 #endif /* G_OS_WIN32 */
2192 g_local_file_make_directory (GFile *file,
2193 GCancellable *cancellable,
2196 GLocalFile *local = G_LOCAL_FILE (file);
2198 if (g_mkdir (local->filename, 0777) == -1)
2202 if (errsv == EINVAL)
2203 /* This must be an invalid filename, on e.g. FAT */
2204 g_set_error_literal (error, G_IO_ERROR,
2205 G_IO_ERROR_INVALID_FILENAME,
2206 _("Invalid filename"));
2208 g_set_error (error, G_IO_ERROR,
2209 g_io_error_from_errno (errsv),
2210 _("Error creating directory: %s"),
2211 g_strerror (errsv));
2219 g_local_file_make_symbolic_link (GFile *file,
2220 const char *symlink_value,
2221 GCancellable *cancellable,
2225 GLocalFile *local = G_LOCAL_FILE (file);
2227 if (symlink (symlink_value, local->filename) == -1)
2231 if (errsv == EINVAL)
2232 /* This must be an invalid filename, on e.g. FAT */
2233 g_set_error_literal (error, G_IO_ERROR,
2234 G_IO_ERROR_INVALID_FILENAME,
2235 _("Invalid filename"));
2236 else if (errsv == EPERM)
2237 g_set_error (error, G_IO_ERROR,
2238 G_IO_ERROR_NOT_SUPPORTED,
2239 _("Filesystem does not support symbolic links"));
2241 g_set_error (error, G_IO_ERROR,
2242 g_io_error_from_errno (errsv),
2243 _("Error making symbolic link: %s"),
2244 g_strerror (errsv));
2249 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
2256 g_local_file_copy (GFile *source,
2258 GFileCopyFlags flags,
2259 GCancellable *cancellable,
2260 GFileProgressCallback progress_callback,
2261 gpointer progress_callback_data,
2264 /* Fall back to default copy */
2265 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
2270 g_local_file_move (GFile *source,
2272 GFileCopyFlags flags,
2273 GCancellable *cancellable,
2274 GFileProgressCallback progress_callback,
2275 gpointer progress_callback_data,
2278 GLocalFile *local_source, *local_destination;
2280 gboolean destination_exist, source_is_dir;
2287 if (!G_IS_LOCAL_FILE (source) ||
2288 !G_IS_LOCAL_FILE (destination))
2290 /* Fall back to default move */
2291 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2295 local_source = G_LOCAL_FILE (source);
2296 local_destination = G_LOCAL_FILE (destination);
2298 res = g_lstat (local_source->filename, &statbuf);
2303 g_set_error (error, G_IO_ERROR,
2304 g_io_error_from_errno (errsv),
2305 _("Error moving file: %s"),
2306 g_strerror (errsv));
2310 source_is_dir = S_ISDIR (statbuf.st_mode);
2311 source_size = statbuf.st_size;
2313 destination_exist = FALSE;
2314 res = g_lstat (local_destination->filename, &statbuf);
2317 destination_exist = TRUE; /* Target file exists */
2319 if (flags & G_FILE_COPY_OVERWRITE)
2321 /* Always fail on dirs, even with overwrite */
2322 if (S_ISDIR (statbuf.st_mode))
2325 g_set_error_literal (error,
2327 G_IO_ERROR_WOULD_MERGE,
2328 _("Can't move directory over directory"));
2330 g_set_error_literal (error,
2332 G_IO_ERROR_IS_DIRECTORY,
2333 _("Can't copy over directory"));
2339 g_set_error_literal (error,
2342 _("Target file exists"));
2347 if (flags & G_FILE_COPY_BACKUP && destination_exist)
2349 backup_name = g_strconcat (local_destination->filename, "~", NULL);
2350 if (g_rename (local_destination->filename, backup_name) == -1)
2352 g_set_error_literal (error,
2354 G_IO_ERROR_CANT_CREATE_BACKUP,
2355 _("Backup file creation failed"));
2356 g_free (backup_name);
2359 g_free (backup_name);
2360 destination_exist = FALSE; /* It did, but no more */
2363 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2365 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2366 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2367 res = g_unlink (local_destination->filename);
2372 g_set_error (error, G_IO_ERROR,
2373 g_io_error_from_errno (errsv),
2374 _("Error removing target file: %s"),
2375 g_strerror (errsv));
2380 if (g_rename (local_source->filename, local_destination->filename) == -1)
2385 /* This will cause the fallback code to run */
2386 g_set_error_literal (error, G_IO_ERROR,
2387 G_IO_ERROR_NOT_SUPPORTED,
2388 _("Move between mounts not supported"));
2389 else if (errsv == EINVAL)
2390 /* This must be an invalid filename, on e.g. FAT, or
2391 we're trying to move the file into itself...
2392 We return invalid filename for both... */
2393 g_set_error_literal (error, G_IO_ERROR,
2394 G_IO_ERROR_INVALID_FILENAME,
2395 _("Invalid filename"));
2397 g_set_error (error, G_IO_ERROR,
2398 g_io_error_from_errno (errsv),
2399 _("Error moving file: %s"),
2400 g_strerror (errsv));
2404 vfs = g_vfs_get_default ();
2405 class = G_VFS_GET_CLASS (vfs);
2406 if (class->local_file_moved)
2407 class->local_file_moved (vfs, local_source->filename, local_destination->filename);
2409 /* Make sure we send full copied size */
2410 if (progress_callback)
2411 progress_callback (source_size, source_size, progress_callback_data);
2419 is_remote (const gchar *filename)
2427 is_remote_fs (const gchar *filename)
2429 const char *fsname = NULL;
2432 struct statfs statfs_buffer;
2433 int statfs_result = 0;
2435 #if STATFS_ARGS == 2
2436 statfs_result = statfs (filename, &statfs_buffer);
2437 #elif STATFS_ARGS == 4
2438 statfs_result = statfs (filename, &statfs_buffer, sizeof (statfs_buffer), 0);
2441 #elif defined(USE_STATVFS)
2442 struct statvfs statfs_buffer;
2443 int statfs_result = 0;
2445 statfs_result = statvfs (filename, &statfs_buffer);
2450 if (statfs_result == -1)
2454 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
2455 fsname = statfs_buffer.f_fstypename;
2457 fsname = get_fs_type (statfs_buffer.f_type);
2460 #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
2461 fsname = statfs_buffer.f_basetype;
2466 if (strcmp (fsname, "nfs") == 0)
2468 if (strcmp (fsname, "nfs4") == 0)
2476 is_remote (const gchar *filename)
2478 static gboolean remote_home;
2479 static gsize initialized;
2482 home = g_get_home_dir ();
2483 if (path_has_prefix (filename, home))
2485 if (g_once_init_enter (&initialized))
2487 remote_home = is_remote_fs (home);
2488 g_once_init_leave (&initialized, TRUE);
2495 #endif /* !G_OS_WIN32 */
2497 static GFileMonitor*
2498 g_local_file_monitor_dir (GFile *file,
2499 GFileMonitorFlags flags,
2500 GCancellable *cancellable,
2503 GLocalFile* local_file = G_LOCAL_FILE(file);
2504 return _g_local_directory_monitor_new (local_file->filename, flags, is_remote (local_file->filename), error);
2507 static GFileMonitor*
2508 g_local_file_monitor_file (GFile *file,
2509 GFileMonitorFlags flags,
2510 GCancellable *cancellable,
2513 GLocalFile* local_file = G_LOCAL_FILE(file);
2514 return _g_local_file_monitor_new (local_file->filename, flags, is_remote (local_file->filename), error);
2518 g_local_file_file_iface_init (GFileIface *iface)
2520 iface->dup = g_local_file_dup;
2521 iface->hash = g_local_file_hash;
2522 iface->equal = g_local_file_equal;
2523 iface->is_native = g_local_file_is_native;
2524 iface->has_uri_scheme = g_local_file_has_uri_scheme;
2525 iface->get_uri_scheme = g_local_file_get_uri_scheme;
2526 iface->get_basename = g_local_file_get_basename;
2527 iface->get_path = g_local_file_get_path;
2528 iface->get_uri = g_local_file_get_uri;
2529 iface->get_parse_name = g_local_file_get_parse_name;
2530 iface->get_parent = g_local_file_get_parent;
2531 iface->prefix_matches = g_local_file_prefix_matches;
2532 iface->get_relative_path = g_local_file_get_relative_path;
2533 iface->resolve_relative_path = g_local_file_resolve_relative_path;
2534 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2535 iface->set_display_name = g_local_file_set_display_name;
2536 iface->enumerate_children = g_local_file_enumerate_children;
2537 iface->query_info = g_local_file_query_info;
2538 iface->query_filesystem_info = g_local_file_query_filesystem_info;
2539 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2540 iface->query_settable_attributes = g_local_file_query_settable_attributes;
2541 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2542 iface->set_attribute = g_local_file_set_attribute;
2543 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2544 iface->read_fn = g_local_file_read;
2545 iface->append_to = g_local_file_append_to;
2546 iface->create = g_local_file_create;
2547 iface->replace = g_local_file_replace;
2548 iface->open_readwrite = g_local_file_open_readwrite;
2549 iface->create_readwrite = g_local_file_create_readwrite;
2550 iface->replace_readwrite = g_local_file_replace_readwrite;
2551 iface->delete_file = g_local_file_delete;
2552 iface->trash = g_local_file_trash;
2553 iface->make_directory = g_local_file_make_directory;
2554 iface->make_symbolic_link = g_local_file_make_symbolic_link;
2555 iface->copy = g_local_file_copy;
2556 iface->move = g_local_file_move;
2557 iface->monitor_dir = g_local_file_monitor_dir;
2558 iface->monitor_file = g_local_file_monitor_file;
2560 iface->supports_thread_contexts = TRUE;