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>
73 #ifndef FILE_READ_ONLY_VOLUME
74 #define FILE_READ_ONLY_VOLUME 0x00080000
78 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
81 #define S_ISLNK(m) (0)
86 static void g_local_file_file_iface_init (GFileIface *iface);
88 static GFileAttributeInfoList *local_writable_attributes = NULL;
89 static /* GFileAttributeInfoList * */ gsize local_writable_namespaces = 0;
93 GObject parent_instance;
98 #define g_local_file_get_type _g_local_file_get_type
99 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
100 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
101 g_local_file_file_iface_init))
103 static char *find_mountpoint_for (const char *file, dev_t dev);
106 g_local_file_finalize (GObject *object)
110 local = G_LOCAL_FILE (object);
112 g_free (local->filename);
114 G_OBJECT_CLASS (g_local_file_parent_class)->finalize (object);
118 g_local_file_class_init (GLocalFileClass *klass)
120 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
121 GFileAttributeInfoList *list;
123 gobject_class->finalize = g_local_file_finalize;
125 /* Set up attribute lists */
127 /* Writable attributes: */
129 list = g_file_attribute_info_list_new ();
131 g_file_attribute_info_list_add (list,
132 G_FILE_ATTRIBUTE_UNIX_MODE,
133 G_FILE_ATTRIBUTE_TYPE_UINT32,
134 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
135 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
138 g_file_attribute_info_list_add (list,
139 G_FILE_ATTRIBUTE_UNIX_UID,
140 G_FILE_ATTRIBUTE_TYPE_UINT32,
141 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
142 g_file_attribute_info_list_add (list,
143 G_FILE_ATTRIBUTE_UNIX_GID,
144 G_FILE_ATTRIBUTE_TYPE_UINT32,
145 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
149 g_file_attribute_info_list_add (list,
150 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
151 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
156 g_file_attribute_info_list_add (list,
157 G_FILE_ATTRIBUTE_TIME_MODIFIED,
158 G_FILE_ATTRIBUTE_TYPE_UINT64,
159 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
160 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
161 g_file_attribute_info_list_add (list,
162 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
163 G_FILE_ATTRIBUTE_TYPE_UINT32,
164 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
165 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
166 /* When copying, the target file is accessed. Replicating
167 * the source access time does not make sense in this case.
169 g_file_attribute_info_list_add (list,
170 G_FILE_ATTRIBUTE_TIME_ACCESS,
171 G_FILE_ATTRIBUTE_TYPE_UINT64,
172 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
173 g_file_attribute_info_list_add (list,
174 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
175 G_FILE_ATTRIBUTE_TYPE_UINT32,
176 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
179 local_writable_attributes = list;
183 g_local_file_init (GLocalFile *local)
189 canonicalize_filename (const char *filename)
191 char *canon, *start, *p, *q;
195 if (!g_path_is_absolute (filename))
197 cwd = g_get_current_dir ();
198 canon = g_build_filename (cwd, filename, NULL);
202 canon = g_strdup (filename);
204 start = (char *)g_path_skip_root (canon);
208 /* This shouldn't really happen, as g_get_current_dir() should
209 return an absolute pathname, but bug 573843 shows this is
210 not always happening */
212 return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
215 /* POSIX allows double slashes at the start to
216 * mean something special (as does windows too).
217 * So, "//" != "/", but more than two slashes
223 G_IS_DIR_SEPARATOR (*p);
230 memmove (start, start+i, strlen (start+i)+1);
233 /* Make sure we're using the canonical dir separator */
235 while (p < start && G_IS_DIR_SEPARATOR (*p))
236 *p++ = G_DIR_SEPARATOR;
241 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
243 memmove (p, p+1, strlen (p+1)+1);
245 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
248 /* Skip previous separator */
252 while (p > start && !G_IS_DIR_SEPARATOR (*p))
254 if (G_IS_DIR_SEPARATOR (*p))
255 *p++ = G_DIR_SEPARATOR;
256 memmove (p, q, strlen (q)+1);
260 /* Skip until next separator */
261 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
266 /* Canonicalize one separator */
267 *p++ = G_DIR_SEPARATOR;
271 /* Remove additional separators */
273 while (*q && G_IS_DIR_SEPARATOR (*q))
277 memmove (p, q, strlen (q)+1);
280 /* Remove trailing slashes */
281 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
288 _g_local_file_new (const char *filename)
292 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
293 local->filename = canonicalize_filename (filename);
295 return G_FILE (local);
299 g_local_file_is_native (GFile *file)
305 g_local_file_has_uri_scheme (GFile *file,
306 const char *uri_scheme)
308 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
312 g_local_file_get_uri_scheme (GFile *file)
314 return g_strdup ("file");
318 g_local_file_get_basename (GFile *file)
320 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
324 g_local_file_get_path (GFile *file)
326 return g_strdup (G_LOCAL_FILE (file)->filename);
330 g_local_file_get_uri (GFile *file)
332 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
336 get_filename_charset (const gchar **filename_charset)
338 const gchar **charsets;
341 is_utf8 = g_get_filename_charsets (&charsets);
343 if (filename_charset)
344 *filename_charset = charsets[0];
350 name_is_valid_for_display (const char *string,
351 gboolean is_valid_utf8)
355 if (!is_valid_utf8 &&
356 !g_utf8_validate (string, -1, NULL))
359 while ((c = *string++) != 0)
361 if (g_ascii_iscntrl (c))
369 g_local_file_get_parse_name (GFile *file)
371 const char *filename;
373 const gchar *charset;
375 char *roundtripped_filename;
376 gboolean free_utf8_filename;
377 gboolean is_valid_utf8;
380 filename = G_LOCAL_FILE (file)->filename;
381 if (get_filename_charset (&charset))
383 utf8_filename = (char *)filename;
384 free_utf8_filename = FALSE;
385 is_valid_utf8 = FALSE; /* Can't guarantee this */
389 utf8_filename = g_convert (filename, -1,
390 "UTF-8", charset, NULL, NULL, NULL);
391 free_utf8_filename = TRUE;
392 is_valid_utf8 = TRUE;
394 if (utf8_filename != NULL)
396 /* Make sure we can roundtrip: */
397 roundtripped_filename = g_convert (utf8_filename, -1,
398 charset, "UTF-8", NULL, NULL, NULL);
400 if (roundtripped_filename == NULL ||
401 strcmp (filename, roundtripped_filename) != 0)
403 g_free (utf8_filename);
404 utf8_filename = NULL;
407 g_free (roundtripped_filename);
411 if (utf8_filename != NULL &&
412 name_is_valid_for_display (utf8_filename, is_valid_utf8))
414 if (free_utf8_filename)
415 parse_name = utf8_filename;
417 parse_name = g_strdup (utf8_filename);
422 char *dup_filename, *p, *backslash;
424 /* Turn backslashes into forward slashes like
425 * g_filename_to_uri() would do (but we can't use that because
426 * it doesn't output IRIs).
428 dup_filename = g_strdup (filename);
429 filename = p = dup_filename;
431 while ((backslash = strchr (p, '\\')) != NULL)
438 escaped_path = g_uri_escape_string (filename,
439 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
441 parse_name = g_strconcat ("file://",
442 (*escaped_path != '/') ? "/" : "",
446 g_free (escaped_path);
448 g_free (dup_filename);
450 if (free_utf8_filename)
451 g_free (utf8_filename);
458 g_local_file_get_parent (GFile *file)
460 GLocalFile *local = G_LOCAL_FILE (file);
461 const char *non_root;
466 non_root = g_path_skip_root (local->filename);
470 dirname = g_path_get_dirname (local->filename);
471 parent = _g_local_file_new (dirname);
477 g_local_file_dup (GFile *file)
479 GLocalFile *local = G_LOCAL_FILE (file);
481 return _g_local_file_new (local->filename);
485 g_local_file_hash (GFile *file)
487 GLocalFile *local = G_LOCAL_FILE (file);
489 return g_str_hash (local->filename);
493 g_local_file_equal (GFile *file1,
496 GLocalFile *local1 = G_LOCAL_FILE (file1);
497 GLocalFile *local2 = G_LOCAL_FILE (file2);
499 return g_str_equal (local1->filename, local2->filename);
503 match_prefix (const char *path,
508 prefix_len = strlen (prefix);
509 if (strncmp (path, prefix, prefix_len) != 0)
512 /* Handle the case where prefix is the root, so that
513 * the IS_DIR_SEPRARATOR check below works */
514 if (prefix_len > 0 &&
515 G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
518 return path + prefix_len;
522 g_local_file_prefix_matches (GFile *parent,
525 GLocalFile *parent_local = G_LOCAL_FILE (parent);
526 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
527 const char *remainder;
529 remainder = match_prefix (descendant_local->filename, parent_local->filename);
530 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
536 g_local_file_get_relative_path (GFile *parent,
539 GLocalFile *parent_local = G_LOCAL_FILE (parent);
540 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
541 const char *remainder;
543 remainder = match_prefix (descendant_local->filename, parent_local->filename);
545 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
546 return g_strdup (remainder + 1);
551 g_local_file_resolve_relative_path (GFile *file,
552 const char *relative_path)
554 GLocalFile *local = G_LOCAL_FILE (file);
558 if (g_path_is_absolute (relative_path))
559 return _g_local_file_new (relative_path);
561 filename = g_build_filename (local->filename, relative_path, NULL);
562 child = _g_local_file_new (filename);
568 static GFileEnumerator *
569 g_local_file_enumerate_children (GFile *file,
570 const char *attributes,
571 GFileQueryInfoFlags flags,
572 GCancellable *cancellable,
575 GLocalFile *local = G_LOCAL_FILE (file);
576 return _g_local_file_enumerator_new (local,
582 g_local_file_get_child_for_display_name (GFile *file,
583 const char *display_name,
589 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
590 if (basename == NULL)
592 g_set_error (error, G_IO_ERROR,
593 G_IO_ERROR_INVALID_FILENAME,
594 _("Invalid filename %s"), display_name);
598 new_file = g_file_get_child (file, basename);
604 #if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
606 get_fs_type (long f_type)
608 /* filesystem ids taken from linux manpage */
715 G_LOCK_DEFINE_STATIC(mount_info_hash);
716 static GHashTable *mount_info_hash = NULL;
717 static guint64 mount_info_hash_cache_time = 0;
720 MOUNT_INFO_READONLY = 1<<0
724 device_equal (gconstpointer v1,
727 return *(dev_t *)v1 == *(dev_t *)v2;
731 device_hash (gconstpointer v)
733 return (guint) *(dev_t *)v;
737 get_mount_info (GFileInfo *fs_info,
739 GFileAttributeMatcher *matcher)
743 gpointer info_as_ptr;
747 GUnixMountEntry *mount;
750 if (g_lstat (path, &buf) != 0)
753 G_LOCK (mount_info_hash);
755 if (mount_info_hash == NULL)
756 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
760 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
761 g_hash_table_remove_all (mount_info_hash);
763 got_info = g_hash_table_lookup_extended (mount_info_hash,
768 G_UNLOCK (mount_info_hash);
770 mount_info = GPOINTER_TO_UINT (info_as_ptr);
776 mountpoint = find_mountpoint_for (path, buf.st_dev);
777 if (mountpoint == NULL)
778 mountpoint = g_strdup ("/");
780 mount = g_unix_mount_at (mountpoint, &cache_time);
783 if (g_unix_mount_is_readonly (mount))
784 mount_info |= MOUNT_INFO_READONLY;
786 g_unix_mount_free (mount);
791 dev = g_new0 (dev_t, 1);
794 G_LOCK (mount_info_hash);
795 mount_info_hash_cache_time = cache_time;
796 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
797 G_UNLOCK (mount_info_hash);
800 if (mount_info & MOUNT_INFO_READONLY)
801 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
809 is_xp_or_later (void)
811 static int result = -1;
816 OSVERSIONINFOEX ver_info = {0};
817 DWORDLONG cond_mask = 0;
818 int op = VER_GREATER_EQUAL;
820 ver_info.dwOSVersionInfoSize = sizeof ver_info;
821 ver_info.dwMajorVersion = 5;
822 ver_info.dwMinorVersion = 1;
824 VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
825 VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
827 result = VerifyVersionInfo (&ver_info,
828 VER_MAJORVERSION | VER_MINORVERSION,
831 result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;
839 get_volume_for_path (const char *path)
845 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
846 result = g_new (wchar_t, MAX_PATH);
848 if (!GetVolumePathNameW (wpath, result, MAX_PATH))
850 char *msg = g_win32_error_message (GetLastError ());
851 g_critical ("GetVolumePathName failed: %s", msg);
858 len = wcslen (result);
859 if (len > 0 && result[len-1] != L'\\')
861 result = g_renew (wchar_t, result, len + 2);
871 find_mountpoint_for (const char *file, dev_t dev)
876 wpath = get_volume_for_path (file);
880 utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
887 get_filesystem_readonly (GFileInfo *info,
892 rootdir = get_volume_for_path (path);
896 if (is_xp_or_later ())
899 if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
900 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
901 (flags & FILE_READ_ONLY_VOLUME) != 0);
905 if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
906 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
913 #endif /* G_OS_WIN32 */
916 g_local_file_query_filesystem_info (GFile *file,
917 const char *attributes,
918 GCancellable *cancellable,
921 GLocalFile *local = G_LOCAL_FILE (file);
923 int statfs_result = 0;
929 struct statfs statfs_buffer;
930 #elif defined(USE_STATVFS)
932 struct statvfs statfs_buffer;
933 #endif /* USE_STATFS */
934 #endif /* G_OS_WIN32 */
935 GFileAttributeMatcher *attribute_matcher;
942 statfs_result = statfs (local->filename, &statfs_buffer);
943 #elif STATFS_ARGS == 4
944 statfs_result = statfs (local->filename, &statfs_buffer,
945 sizeof (statfs_buffer), 0);
946 #endif /* STATFS_ARGS == 2 */
947 block_size = statfs_buffer.f_bsize;
949 /* Many backends can't report free size (for instance the gvfs fuse
950 backend for backend not supporting this), and set f_bfree to 0,
951 but it can be 0 for real too. We treat the available == 0 and
952 free == 0 case as "both of these are invalid".
955 if (statfs_result == 0 &&
956 statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0)
958 #endif /* G_OS_WIN32 */
960 #elif defined(USE_STATVFS)
961 statfs_result = statvfs (local->filename, &statfs_buffer);
962 block_size = statfs_buffer.f_frsize;
963 #endif /* USE_STATFS */
965 if (statfs_result == -1)
969 g_set_error (error, G_IO_ERROR,
970 g_io_error_from_errno (errsv),
971 _("Error getting filesystem info: %s"),
976 info = g_file_info_new ();
978 attribute_matcher = g_file_attribute_matcher_new (attributes);
981 g_file_attribute_matcher_matches (attribute_matcher,
982 G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
985 gchar *localdir = g_path_get_dirname (local->filename);
986 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
990 if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
991 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
994 #if defined(USE_STATFS) || defined(USE_STATVFS)
995 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
1000 g_file_attribute_matcher_matches (attribute_matcher,
1001 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
1004 gchar *localdir = g_path_get_dirname (local->filename);
1005 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1009 if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
1010 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, (guint64)li.QuadPart);
1013 #if defined(USE_STATFS) || defined(USE_STATVFS)
1014 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
1016 #endif /* G_OS_WIN32 */
1020 g_file_attribute_matcher_matches (attribute_matcher,
1021 G_FILE_ATTRIBUTE_FILESYSTEM_USED))
1024 gchar *localdir = g_path_get_dirname (local->filename);
1025 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1026 ULARGE_INTEGER li_free;
1027 ULARGE_INTEGER li_total;
1030 if (GetDiskFreeSpaceExW (wdirname, &li_free, &li_total, NULL))
1031 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, (guint64)li_total.QuadPart - (guint64)li_free.QuadPart);
1034 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, block_size * (statfs_buffer.f_blocks - statfs_buffer.f_bfree));
1035 #endif /* G_OS_WIN32 */
1040 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1041 fstype = g_strdup (statfs_buffer.f_fstypename);
1043 fstype = get_fs_type (statfs_buffer.f_type);
1046 #elif defined(USE_STATVFS)
1047 #if defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
1048 fstype = g_strdup (statfs_buffer.f_fstypename);
1049 #elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1050 fstype = g_strdup (statfs_buffer.f_basetype);
1054 #endif /* USE_STATFS */
1057 g_file_attribute_matcher_matches (attribute_matcher,
1058 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1059 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1060 #endif /* G_OS_WIN32 */
1062 if (g_file_attribute_matcher_matches (attribute_matcher,
1063 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
1066 get_filesystem_readonly (info, local->filename);
1068 get_mount_info (info, local->filename, attribute_matcher);
1069 #endif /* G_OS_WIN32 */
1072 g_file_attribute_matcher_unref (attribute_matcher);
1078 g_local_file_find_enclosing_mount (GFile *file,
1079 GCancellable *cancellable,
1082 GLocalFile *local = G_LOCAL_FILE (file);
1087 if (g_lstat (local->filename, &buf) != 0)
1089 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1090 /* Translators: This is an error message when trying to
1091 * find the enclosing (user visible) mount of a file, but
1093 _("Containing mount does not exist"));
1097 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
1098 if (mountpoint == NULL)
1100 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1101 /* Translators: This is an error message when trying to
1102 * find the enclosing (user visible) mount of a file, but
1104 _("Containing mount does not exist"));
1108 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1109 g_free (mountpoint);
1113 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1114 /* Translators: This is an error message when trying to find
1115 * the enclosing (user visible) mount of a file, but none
1117 _("Containing mount does not exist"));
1122 g_local_file_set_display_name (GFile *file,
1123 const char *display_name,
1124 GCancellable *cancellable,
1127 GLocalFile *local, *new_local;
1128 GFile *new_file, *parent;
1134 parent = g_file_get_parent (file);
1137 g_set_error_literal (error, G_IO_ERROR,
1139 _("Can't rename root directory"));
1143 new_file = g_file_get_child_for_display_name (parent, display_name, error);
1144 g_object_unref (parent);
1146 if (new_file == NULL)
1148 local = G_LOCAL_FILE (file);
1149 new_local = G_LOCAL_FILE (new_file);
1151 if (g_lstat (new_local->filename, &statbuf) == -1)
1155 if (errsv != ENOENT)
1157 g_set_error (error, G_IO_ERROR,
1158 g_io_error_from_errno (errsv),
1159 _("Error renaming file: %s"),
1160 g_strerror (errsv));
1166 g_set_error_literal (error, G_IO_ERROR,
1168 _("Can't rename file, filename already exists"));
1172 if (g_rename (local->filename, new_local->filename) == -1)
1176 if (errsv == EINVAL)
1177 /* We can't get a rename file into itself error herer,
1178 so this must be an invalid filename, on e.g. FAT */
1179 g_set_error_literal (error, G_IO_ERROR,
1180 G_IO_ERROR_INVALID_FILENAME,
1181 _("Invalid filename"));
1183 g_set_error (error, G_IO_ERROR,
1184 g_io_error_from_errno (errsv),
1185 _("Error renaming file: %s"),
1186 g_strerror (errsv));
1187 g_object_unref (new_file);
1191 vfs = g_vfs_get_default ();
1192 class = G_VFS_GET_CLASS (vfs);
1193 if (class->local_file_moved)
1194 class->local_file_moved (vfs, local->filename, new_local->filename);
1200 g_local_file_query_info (GFile *file,
1201 const char *attributes,
1202 GFileQueryInfoFlags flags,
1203 GCancellable *cancellable,
1206 GLocalFile *local = G_LOCAL_FILE (file);
1208 GFileAttributeMatcher *matcher;
1209 char *basename, *dirname;
1210 GLocalParentFileInfo parent_info;
1212 matcher = g_file_attribute_matcher_new (attributes);
1214 basename = g_path_get_basename (local->filename);
1216 dirname = g_path_get_dirname (local->filename);
1217 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1220 info = _g_local_file_info_get (basename, local->filename,
1221 matcher, flags, &parent_info,
1225 _g_local_file_info_free_parent_info (&parent_info);
1228 g_file_attribute_matcher_unref (matcher);
1233 static GFileAttributeInfoList *
1234 g_local_file_query_settable_attributes (GFile *file,
1235 GCancellable *cancellable,
1238 return g_file_attribute_info_list_ref (local_writable_attributes);
1241 static GFileAttributeInfoList *
1242 g_local_file_query_writable_namespaces (GFile *file,
1243 GCancellable *cancellable,
1246 GFileAttributeInfoList *list;
1250 if (g_once_init_enter (&local_writable_namespaces))
1252 /* Writable namespaces: */
1254 list = g_file_attribute_info_list_new ();
1257 g_file_attribute_info_list_add (list,
1259 G_FILE_ATTRIBUTE_TYPE_STRING,
1260 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
1261 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1262 g_file_attribute_info_list_add (list,
1264 G_FILE_ATTRIBUTE_TYPE_STRING,
1265 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1268 vfs = g_vfs_get_default ();
1269 class = G_VFS_GET_CLASS (vfs);
1270 if (class->add_writable_namespaces)
1271 class->add_writable_namespaces (vfs, list);
1273 g_once_init_leave (&local_writable_namespaces, (gsize)list);
1275 list = (GFileAttributeInfoList *)local_writable_namespaces;
1277 return g_file_attribute_info_list_ref (list);
1281 g_local_file_set_attribute (GFile *file,
1282 const char *attribute,
1283 GFileAttributeType type,
1285 GFileQueryInfoFlags flags,
1286 GCancellable *cancellable,
1289 GLocalFile *local = G_LOCAL_FILE (file);
1291 return _g_local_file_info_set_attribute (local->filename,
1301 g_local_file_set_attributes_from_info (GFile *file,
1303 GFileQueryInfoFlags flags,
1304 GCancellable *cancellable,
1307 GLocalFile *local = G_LOCAL_FILE (file);
1308 int res, chained_res;
1309 GFileIface *default_iface;
1311 res = _g_local_file_info_set_attributes (local->filename,
1317 error = NULL; /* Don't write over error if further errors */
1319 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1321 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1323 return res && chained_res;
1326 static GFileInputStream *
1327 g_local_file_read (GFile *file,
1328 GCancellable *cancellable,
1331 GLocalFile *local = G_LOCAL_FILE (file);
1335 fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1341 if (errsv == EACCES)
1343 ret = _stati64 (local->filename, &buf);
1344 if (ret == 0 && S_ISDIR (buf.st_mode))
1346 g_set_error_literal (error, G_IO_ERROR,
1347 G_IO_ERROR_IS_DIRECTORY,
1348 _("Can't open directory"));
1354 g_set_error (error, G_IO_ERROR,
1355 g_io_error_from_errno (errsv),
1356 _("Error opening file: %s"),
1357 g_strerror (errsv));
1362 ret = _fstati64 (fd, &buf);
1364 ret = fstat (fd, &buf);
1367 if (ret == 0 && S_ISDIR (buf.st_mode))
1370 g_set_error_literal (error, G_IO_ERROR,
1371 G_IO_ERROR_IS_DIRECTORY,
1372 _("Can't open directory"));
1376 return _g_local_file_input_stream_new (fd);
1379 static GFileOutputStream *
1380 g_local_file_append_to (GFile *file,
1381 GFileCreateFlags flags,
1382 GCancellable *cancellable,
1385 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1386 flags, cancellable, error);
1389 static GFileOutputStream *
1390 g_local_file_create (GFile *file,
1391 GFileCreateFlags flags,
1392 GCancellable *cancellable,
1395 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1397 flags, cancellable, error);
1400 static GFileOutputStream *
1401 g_local_file_replace (GFile *file,
1403 gboolean make_backup,
1404 GFileCreateFlags flags,
1405 GCancellable *cancellable,
1408 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1410 etag, make_backup, flags,
1411 cancellable, error);
1414 static GFileIOStream *
1415 g_local_file_open_readwrite (GFile *file,
1416 GCancellable *cancellable,
1419 GFileOutputStream *output;
1422 output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
1424 cancellable, error);
1428 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1429 g_object_unref (output);
1433 static GFileIOStream *
1434 g_local_file_create_readwrite (GFile *file,
1435 GFileCreateFlags flags,
1436 GCancellable *cancellable,
1439 GFileOutputStream *output;
1442 output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1444 cancellable, error);
1448 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1449 g_object_unref (output);
1453 static GFileIOStream *
1454 g_local_file_replace_readwrite (GFile *file,
1456 gboolean make_backup,
1457 GFileCreateFlags flags,
1458 GCancellable *cancellable,
1461 GFileOutputStream *output;
1464 output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1466 etag, make_backup, flags,
1467 cancellable, error);
1471 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1472 g_object_unref (output);
1477 g_local_file_delete (GFile *file,
1478 GCancellable *cancellable,
1481 GLocalFile *local = G_LOCAL_FILE (file);
1485 if (g_remove (local->filename) == -1)
1489 /* Posix allows EEXIST too, but the more sane error
1490 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1492 if (errsv == EEXIST)
1495 g_set_error (error, G_IO_ERROR,
1496 g_io_error_from_errno (errsv),
1497 _("Error removing file: %s"),
1498 g_strerror (errsv));
1502 vfs = g_vfs_get_default ();
1503 class = G_VFS_GET_CLASS (vfs);
1504 if (class->local_file_removed)
1505 class->local_file_removed (vfs, local->filename);
1513 strip_trailing_slashes (const char *path)
1518 path_copy = g_strdup (path);
1519 len = strlen (path_copy);
1520 while (len > 1 && path_copy[len-1] == '/')
1521 path_copy[--len] = 0;
1527 expand_symlink (const char *link)
1529 char *resolved, *canonical, *parent, *link2;
1530 char symlink_value[4096];
1538 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1541 return g_strdup (link);
1542 symlink_value[res] = 0;
1545 if (g_path_is_absolute (symlink_value))
1546 return canonicalize_filename (symlink_value);
1549 link2 = strip_trailing_slashes (link);
1550 parent = g_path_get_dirname (link2);
1553 resolved = g_build_filename (parent, symlink_value, NULL);
1556 canonical = canonicalize_filename (resolved);
1565 get_parent (const char *path,
1569 GStatBuf parent_stat;
1573 path_copy = strip_trailing_slashes (path);
1575 parent = g_path_get_dirname (path_copy);
1576 if (strcmp (parent, ".") == 0 ||
1577 strcmp (parent, path_copy) == 0)
1587 if (g_lstat (parent, &parent_stat) != 0)
1593 if (S_ISLNK (parent_stat.st_mode))
1596 parent = expand_symlink (parent);
1601 if (num_recursions > 12)
1606 } while (S_ISLNK (parent_stat.st_mode));
1608 *parent_dev = parent_stat.st_dev;
1614 expand_all_symlinks (const char *path)
1616 char *parent, *parent_expanded;
1617 char *basename, *res;
1620 parent = get_parent (path, &parent_dev);
1623 parent_expanded = expand_all_symlinks (parent);
1625 basename = g_path_get_basename (path);
1626 res = g_build_filename (parent_expanded, basename, NULL);
1628 g_free (parent_expanded);
1631 res = g_strdup (path);
1637 find_mountpoint_for (const char *file,
1641 dev_t dir_dev, parent_dev;
1643 dir = g_strdup (file);
1648 parent = get_parent (dir, &parent_dev);
1652 if (parent_dev != dir_dev)
1664 find_topdir_for (const char *file)
1669 dir = get_parent (file, &dir_dev);
1673 return find_mountpoint_for (dir, dir_dev);
1677 get_unique_filename (const char *basename,
1683 return g_strdup (basename);
1685 dot = strchr (basename, '.');
1687 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1689 return g_strdup_printf ("%s.%d", basename, id);
1693 path_has_prefix (const char *path,
1701 prefix_len = strlen (prefix);
1703 if (strncmp (path, prefix, prefix_len) == 0 &&
1704 (prefix_len == 0 || /* empty prefix always matches */
1705 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1706 path[prefix_len] == 0 ||
1707 path[prefix_len] == '/'))
1714 try_make_relative (const char *path,
1717 char *path2, *base2;
1720 path2 = expand_all_symlinks (path);
1721 base2 = expand_all_symlinks (base);
1724 if (path_has_prefix (path2, base2))
1726 relative = path2 + strlen (base2);
1727 while (*relative == '/')
1729 relative = g_strdup (relative);
1737 /* Failed, use abs path */
1738 return g_strdup (path);
1742 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1744 static gsize home_dev_set = 0;
1745 static dev_t home_dev;
1746 char *topdir, *globaldir, *trashdir, *tmpname;
1749 GStatBuf global_stat, trash_stat;
1752 if (g_once_init_enter (&home_dev_set))
1756 g_stat (g_get_home_dir (), &home_stat);
1757 home_dev = home_stat.st_dev;
1758 g_once_init_leave (&home_dev_set, 1);
1761 /* Assume we can trash to the home */
1762 if (dir_dev == home_dev)
1765 topdir = find_mountpoint_for (dirname, dir_dev);
1769 globaldir = g_build_filename (topdir, ".Trash", NULL);
1770 if (g_lstat (globaldir, &global_stat) == 0 &&
1771 S_ISDIR (global_stat.st_mode) &&
1772 (global_stat.st_mode & S_ISVTX) != 0)
1774 /* got a toplevel sysadmin created dir, assume we
1775 * can trash to it (we should be able to create a dir)
1776 * This fails for the FAT case where the ownership of
1777 * that dir would be wrong though..
1785 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1787 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1789 tmpname = g_strdup_printf (".Trash-%s", uid_str);
1790 trashdir = g_build_filename (topdir, tmpname, NULL);
1793 if (g_lstat (trashdir, &trash_stat) == 0)
1797 return S_ISDIR (trash_stat.st_mode) &&
1798 trash_stat.st_uid == uid;
1802 /* User specific trash didn't exist, can we create it? */
1803 res = g_access (topdir, W_OK) == 0;
1811 _g_local_file_is_lost_found_dir (const char *path, dev_t path_dev)
1813 gboolean ret = FALSE;
1814 gchar *mount_dir = NULL;
1815 size_t mount_dir_len;
1818 if (!g_str_has_suffix (path, "/lost+found"))
1821 mount_dir = find_mountpoint_for (path, path_dev);
1822 if (mount_dir == NULL)
1825 mount_dir_len = strlen (mount_dir);
1826 /* We special-case rootfs ('/') since it's the only case where
1827 * mount_dir ends in '/'
1829 if (mount_dir_len == 1)
1831 if (mount_dir_len + strlen ("/lost+found") != strlen (path))
1834 if (g_lstat (path, &statbuf) != 0)
1837 if (!(S_ISDIR (statbuf.st_mode) &&
1838 statbuf.st_uid == 0 &&
1839 statbuf.st_gid == 0))
1851 g_local_file_trash (GFile *file,
1852 GCancellable *cancellable,
1855 GLocalFile *local = G_LOCAL_FILE (file);
1856 GStatBuf file_stat, home_stat;
1857 const char *homedir;
1858 char *trashdir, *topdir, *infodir, *filesdir;
1859 char *basename, *trashname, *trashfile, *infoname, *infofile;
1860 char *original_name, *original_name_escaped;
1863 gboolean is_homedir_trash;
1864 char delete_time[32];
1866 GStatBuf trash_stat, global_stat;
1867 char *dirname, *globaldir;
1871 if (g_lstat (local->filename, &file_stat) != 0)
1875 g_set_error (error, G_IO_ERROR,
1876 g_io_error_from_errno (errsv),
1877 _("Error trashing file: %s"),
1878 g_strerror (errsv));
1882 homedir = g_get_home_dir ();
1883 g_stat (homedir, &home_stat);
1885 is_homedir_trash = FALSE;
1887 if (file_stat.st_dev == home_stat.st_dev)
1889 is_homedir_trash = TRUE;
1891 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1892 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1897 display_name = g_filename_display_name (trashdir);
1898 g_set_error (error, G_IO_ERROR,
1899 g_io_error_from_errno (errsv),
1900 _("Unable to create trash dir %s: %s"),
1901 display_name, g_strerror (errsv));
1902 g_free (display_name);
1906 topdir = g_strdup (g_get_user_data_dir ());
1914 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1916 topdir = find_topdir_for (local->filename);
1919 g_set_error_literal (error, G_IO_ERROR,
1920 G_IO_ERROR_NOT_SUPPORTED,
1921 _("Unable to find toplevel directory for trash"));
1925 /* Try looking for global trash dir $topdir/.Trash/$uid */
1926 globaldir = g_build_filename (topdir, ".Trash", NULL);
1927 if (g_lstat (globaldir, &global_stat) == 0 &&
1928 S_ISDIR (global_stat.st_mode) &&
1929 (global_stat.st_mode & S_ISVTX) != 0)
1931 trashdir = g_build_filename (globaldir, uid_str, NULL);
1933 if (g_lstat (trashdir, &trash_stat) == 0)
1935 if (!S_ISDIR (trash_stat.st_mode) ||
1936 trash_stat.st_uid != uid)
1938 /* Not a directory or not owned by user, ignore */
1943 else if (g_mkdir (trashdir, 0700) == -1)
1951 if (trashdir == NULL)
1953 gboolean tried_create;
1955 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1956 dirname = g_strdup_printf (".Trash-%s", uid_str);
1957 trashdir = g_build_filename (topdir, dirname, NULL);
1960 tried_create = FALSE;
1963 if (g_lstat (trashdir, &trash_stat) == 0)
1965 if (!S_ISDIR (trash_stat.st_mode) ||
1966 trash_stat.st_uid != uid)
1968 /* Remove the failed directory */
1970 g_remove (trashdir);
1972 /* Not a directory or not owned by user, ignore */
1979 if (!tried_create &&
1980 g_mkdir (trashdir, 0700) != -1)
1982 /* Ensure that the created dir has the right uid etc.
1983 This might fail on e.g. a FAT dir */
1984 tried_create = TRUE;
1995 if (trashdir == NULL)
1998 g_set_error_literal (error, G_IO_ERROR,
1999 G_IO_ERROR_NOT_SUPPORTED,
2000 _("Unable to find or create trash directory"));
2005 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
2007 infodir = g_build_filename (trashdir, "info", NULL);
2008 filesdir = g_build_filename (trashdir, "files", NULL);
2011 /* Make sure we have the subdirectories */
2012 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
2013 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
2018 g_set_error_literal (error, G_IO_ERROR,
2019 G_IO_ERROR_NOT_SUPPORTED,
2020 _("Unable to find or create trash directory"));
2024 basename = g_path_get_basename (local->filename);
2032 trashname = get_unique_filename (basename, i++);
2033 infoname = g_strconcat (trashname, ".trashinfo", NULL);
2034 infofile = g_build_filename (infodir, infoname, NULL);
2037 fd = g_open (infofile, O_CREAT | O_EXCL, 0666);
2038 } while (fd == -1 && errno == EEXIST);
2052 g_set_error (error, G_IO_ERROR,
2053 g_io_error_from_errno (errsv),
2054 _("Unable to create trashing info file: %s"),
2055 g_strerror (errsv));
2061 /* TODO: Maybe we should verify that you can delete the file from the trash
2062 before moving it? OTOH, that is hard, as it needs a recursive scan */
2064 trashfile = g_build_filename (filesdir, trashname, NULL);
2068 if (g_rename (local->filename, trashfile) == -1)
2078 /* The trash dir was actually on another fs anyway!?
2079 This can happen when the same device is mounted multiple
2080 times, or with bind mounts of the same fs. */
2081 g_set_error (error, G_IO_ERROR,
2082 G_IO_ERROR_NOT_SUPPORTED,
2083 _("Unable to trash file: %s"),
2084 g_strerror (errsv));
2086 g_set_error (error, G_IO_ERROR,
2087 g_io_error_from_errno (errsv),
2088 _("Unable to trash file: %s"),
2089 g_strerror (errsv));
2093 vfs = g_vfs_get_default ();
2094 class = G_VFS_GET_CLASS (vfs);
2095 if (class->local_file_moved)
2096 class->local_file_moved (vfs, local->filename, trashfile);
2100 /* TODO: Do we need to update mtime/atime here after the move? */
2102 /* Use absolute names for homedir */
2103 if (is_homedir_trash)
2104 original_name = g_strdup (local->filename);
2106 original_name = try_make_relative (local->filename, topdir);
2107 original_name_escaped = g_uri_escape_string (original_name, "/", FALSE);
2109 g_free (original_name);
2116 localtime_r (&t, &now);
2118 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
2121 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2122 original_name_escaped, delete_time);
2124 g_file_set_contents (infofile, data, -1, NULL);
2128 g_free (original_name_escaped);
2133 #else /* G_OS_WIN32 */
2135 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
2137 return FALSE; /* XXX ??? */
2141 g_local_file_trash (GFile *file,
2142 GCancellable *cancellable,
2145 GLocalFile *local = G_LOCAL_FILE (file);
2146 SHFILEOPSTRUCTW op = {0};
2151 wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
2152 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2153 wfilename = g_renew (wchar_t, wfilename, len + 2);
2154 wfilename[len + 1] = 0;
2156 op.wFunc = FO_DELETE;
2157 op.pFrom = wfilename;
2158 op.fFlags = FOF_ALLOWUNDO;
2160 success = SHFileOperationW (&op) == 0;
2162 if (success && op.fAnyOperationsAborted)
2164 if (cancellable && !g_cancellable_is_cancelled (cancellable))
2165 g_cancellable_cancel (cancellable);
2166 g_set_error (error, G_IO_ERROR,
2167 G_IO_ERROR_CANCELLED,
2168 _("Unable to trash file: %s"),
2169 _("Operation was cancelled"));
2173 g_set_error (error, G_IO_ERROR,
2175 _("Unable to trash file: %s"),
2176 _("internal error"));
2181 #endif /* G_OS_WIN32 */
2184 g_local_file_make_directory (GFile *file,
2185 GCancellable *cancellable,
2188 GLocalFile *local = G_LOCAL_FILE (file);
2190 if (g_mkdir (local->filename, 0777) == -1)
2194 if (errsv == EINVAL)
2195 /* This must be an invalid filename, on e.g. FAT */
2196 g_set_error_literal (error, G_IO_ERROR,
2197 G_IO_ERROR_INVALID_FILENAME,
2198 _("Invalid filename"));
2200 g_set_error (error, G_IO_ERROR,
2201 g_io_error_from_errno (errsv),
2202 _("Error creating directory: %s"),
2203 g_strerror (errsv));
2211 g_local_file_make_symbolic_link (GFile *file,
2212 const char *symlink_value,
2213 GCancellable *cancellable,
2217 GLocalFile *local = G_LOCAL_FILE (file);
2219 if (symlink (symlink_value, local->filename) == -1)
2223 if (errsv == EINVAL)
2224 /* This must be an invalid filename, on e.g. FAT */
2225 g_set_error_literal (error, G_IO_ERROR,
2226 G_IO_ERROR_INVALID_FILENAME,
2227 _("Invalid filename"));
2228 else if (errsv == EPERM)
2229 g_set_error (error, G_IO_ERROR,
2230 G_IO_ERROR_NOT_SUPPORTED,
2231 _("Filesystem does not support symbolic links"));
2233 g_set_error (error, G_IO_ERROR,
2234 g_io_error_from_errno (errsv),
2235 _("Error making symbolic link: %s"),
2236 g_strerror (errsv));
2241 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
2248 g_local_file_copy (GFile *source,
2250 GFileCopyFlags flags,
2251 GCancellable *cancellable,
2252 GFileProgressCallback progress_callback,
2253 gpointer progress_callback_data,
2256 /* Fall back to default copy */
2257 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
2262 g_local_file_move (GFile *source,
2264 GFileCopyFlags flags,
2265 GCancellable *cancellable,
2266 GFileProgressCallback progress_callback,
2267 gpointer progress_callback_data,
2270 GLocalFile *local_source, *local_destination;
2272 gboolean destination_exist, source_is_dir;
2279 if (!G_IS_LOCAL_FILE (source) ||
2280 !G_IS_LOCAL_FILE (destination))
2282 /* Fall back to default move */
2283 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2287 local_source = G_LOCAL_FILE (source);
2288 local_destination = G_LOCAL_FILE (destination);
2290 res = g_lstat (local_source->filename, &statbuf);
2295 g_set_error (error, G_IO_ERROR,
2296 g_io_error_from_errno (errsv),
2297 _("Error moving file: %s"),
2298 g_strerror (errsv));
2302 source_is_dir = S_ISDIR (statbuf.st_mode);
2303 source_size = statbuf.st_size;
2305 destination_exist = FALSE;
2306 res = g_lstat (local_destination->filename, &statbuf);
2309 destination_exist = TRUE; /* Target file exists */
2311 if (flags & G_FILE_COPY_OVERWRITE)
2313 /* Always fail on dirs, even with overwrite */
2314 if (S_ISDIR (statbuf.st_mode))
2317 g_set_error_literal (error,
2319 G_IO_ERROR_WOULD_MERGE,
2320 _("Can't move directory over directory"));
2322 g_set_error_literal (error,
2324 G_IO_ERROR_IS_DIRECTORY,
2325 _("Can't copy over directory"));
2331 g_set_error_literal (error,
2334 _("Target file exists"));
2339 if (flags & G_FILE_COPY_BACKUP && destination_exist)
2341 backup_name = g_strconcat (local_destination->filename, "~", NULL);
2342 if (g_rename (local_destination->filename, backup_name) == -1)
2344 g_set_error_literal (error,
2346 G_IO_ERROR_CANT_CREATE_BACKUP,
2347 _("Backup file creation failed"));
2348 g_free (backup_name);
2351 g_free (backup_name);
2352 destination_exist = FALSE; /* It did, but no more */
2355 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2357 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2358 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2359 res = g_unlink (local_destination->filename);
2364 g_set_error (error, G_IO_ERROR,
2365 g_io_error_from_errno (errsv),
2366 _("Error removing target file: %s"),
2367 g_strerror (errsv));
2372 if (g_rename (local_source->filename, local_destination->filename) == -1)
2377 /* This will cause the fallback code to run */
2378 g_set_error_literal (error, G_IO_ERROR,
2379 G_IO_ERROR_NOT_SUPPORTED,
2380 _("Move between mounts not supported"));
2381 else if (errsv == EINVAL)
2382 /* This must be an invalid filename, on e.g. FAT, or
2383 we're trying to move the file into itself...
2384 We return invalid filename for both... */
2385 g_set_error_literal (error, G_IO_ERROR,
2386 G_IO_ERROR_INVALID_FILENAME,
2387 _("Invalid filename"));
2389 g_set_error (error, G_IO_ERROR,
2390 g_io_error_from_errno (errsv),
2391 _("Error moving file: %s"),
2392 g_strerror (errsv));
2396 vfs = g_vfs_get_default ();
2397 class = G_VFS_GET_CLASS (vfs);
2398 if (class->local_file_moved)
2399 class->local_file_moved (vfs, local_source->filename, local_destination->filename);
2401 /* Make sure we send full copied size */
2402 if (progress_callback)
2403 progress_callback (source_size, source_size, progress_callback_data);
2409 is_remote_fs (const gchar *filename)
2411 const char *fsname = NULL;
2414 struct statfs statfs_buffer;
2415 int statfs_result = 0;
2417 #if STATFS_ARGS == 2
2418 statfs_result = statfs (filename, &statfs_buffer);
2419 #elif STATFS_ARGS == 4
2420 statfs_result = statfs (filename, &statfs_buffer, sizeof (statfs_buffer), 0);
2423 #elif defined(USE_STATVFS)
2424 struct statvfs statfs_buffer;
2426 statfs_result = statvfs (filename, &statfs_buffer);
2431 if (statfs_result == -1)
2435 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
2436 fsname = statfs_buffer.f_fstypename;
2438 fsname = get_fs_type (statfs_buffer.f_type);
2441 #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
2442 fsname = statfs_buffer.f_basetype;
2447 if (strcmp (fsname, "nfs") == 0)
2449 if (strcmp (fsname, "nfs4") == 0)
2457 is_remote (const gchar *filename)
2459 static gboolean remote_home;
2460 static gsize initialized;
2463 home = g_get_home_dir ();
2464 if (path_has_prefix (filename, home))
2466 if (g_once_init_enter (&initialized))
2468 remote_home = is_remote_fs (home);
2469 g_once_init_leave (&initialized, TRUE);
2477 static GFileMonitor*
2478 g_local_file_monitor_dir (GFile *file,
2479 GFileMonitorFlags flags,
2480 GCancellable *cancellable,
2483 GLocalFile* local_file = G_LOCAL_FILE(file);
2484 return _g_local_directory_monitor_new (local_file->filename, flags, is_remote (local_file->filename), error);
2487 static GFileMonitor*
2488 g_local_file_monitor_file (GFile *file,
2489 GFileMonitorFlags flags,
2490 GCancellable *cancellable,
2493 GLocalFile* local_file = G_LOCAL_FILE(file);
2494 return _g_local_file_monitor_new (local_file->filename, flags, is_remote (local_file->filename), error);
2498 g_local_file_file_iface_init (GFileIface *iface)
2500 iface->dup = g_local_file_dup;
2501 iface->hash = g_local_file_hash;
2502 iface->equal = g_local_file_equal;
2503 iface->is_native = g_local_file_is_native;
2504 iface->has_uri_scheme = g_local_file_has_uri_scheme;
2505 iface->get_uri_scheme = g_local_file_get_uri_scheme;
2506 iface->get_basename = g_local_file_get_basename;
2507 iface->get_path = g_local_file_get_path;
2508 iface->get_uri = g_local_file_get_uri;
2509 iface->get_parse_name = g_local_file_get_parse_name;
2510 iface->get_parent = g_local_file_get_parent;
2511 iface->prefix_matches = g_local_file_prefix_matches;
2512 iface->get_relative_path = g_local_file_get_relative_path;
2513 iface->resolve_relative_path = g_local_file_resolve_relative_path;
2514 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2515 iface->set_display_name = g_local_file_set_display_name;
2516 iface->enumerate_children = g_local_file_enumerate_children;
2517 iface->query_info = g_local_file_query_info;
2518 iface->query_filesystem_info = g_local_file_query_filesystem_info;
2519 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2520 iface->query_settable_attributes = g_local_file_query_settable_attributes;
2521 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2522 iface->set_attribute = g_local_file_set_attribute;
2523 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2524 iface->read_fn = g_local_file_read;
2525 iface->append_to = g_local_file_append_to;
2526 iface->create = g_local_file_create;
2527 iface->replace = g_local_file_replace;
2528 iface->open_readwrite = g_local_file_open_readwrite;
2529 iface->create_readwrite = g_local_file_create_readwrite;
2530 iface->replace_readwrite = g_local_file_replace_readwrite;
2531 iface->delete_file = g_local_file_delete;
2532 iface->trash = g_local_file_trash;
2533 iface->make_directory = g_local_file_make_directory;
2534 iface->make_symbolic_link = g_local_file_make_symbolic_link;
2535 iface->copy = g_local_file_copy;
2536 iface->move = g_local_file_move;
2537 iface->monitor_dir = g_local_file_monitor_dir;
2538 iface->monitor_file = g_local_file_monitor_file;
2540 iface->supports_thread_contexts = TRUE;