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 #if defined(HAVE_STATFS) && defined(HAVE_STATVFS)
54 /* Some systems have both statfs and statvfs, pick the
55 most "native" for these */
56 # if !defined(HAVE_STRUCT_STATFS_F_BAVAIL)
57 /* on solaris and irix, statfs doesn't even have the
61 /* at least on linux, statfs is the actual syscall */
65 #elif defined(HAVE_STATFS)
69 #elif defined(HAVE_STATVFS)
75 #include "gfileattribute.h"
76 #include "glocalfile.h"
77 #include "glocalfileinfo.h"
78 #include "glocalfileenumerator.h"
79 #include "glocalfileinputstream.h"
80 #include "glocalfileoutputstream.h"
81 #include "glocalfileiostream.h"
82 #include "glocaldirectorymonitor.h"
83 #include "glocalfilemonitor.h"
84 #include "gmountprivate.h"
85 #include "gunixmounts.h"
87 #include <glib/gstdio.h>
91 #define _WIN32_WINNT 0x0500
96 #ifndef FILE_READ_ONLY_VOLUME
97 #define FILE_READ_ONLY_VOLUME 0x00080000
101 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
104 #define S_ISLNK(m) (0)
108 #include "gioalias.h"
110 static void g_local_file_file_iface_init (GFileIface *iface);
112 static GFileAttributeInfoList *local_writable_attributes = NULL;
113 static /* GFileAttributeInfoList * */ gsize local_writable_namespaces = 0;
117 GObject parent_instance;
122 #define g_local_file_get_type _g_local_file_get_type
123 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
124 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
125 g_local_file_file_iface_init))
127 static char *find_mountpoint_for (const char *file, dev_t dev);
130 g_local_file_finalize (GObject *object)
134 local = G_LOCAL_FILE (object);
136 g_free (local->filename);
138 G_OBJECT_CLASS (g_local_file_parent_class)->finalize (object);
142 g_local_file_class_init (GLocalFileClass *klass)
144 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
145 GFileAttributeInfoList *list;
147 gobject_class->finalize = g_local_file_finalize;
149 /* Set up attribute lists */
151 /* Writable attributes: */
153 list = g_file_attribute_info_list_new ();
155 g_file_attribute_info_list_add (list,
156 G_FILE_ATTRIBUTE_UNIX_MODE,
157 G_FILE_ATTRIBUTE_TYPE_UINT32,
158 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
159 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
162 g_file_attribute_info_list_add (list,
163 G_FILE_ATTRIBUTE_UNIX_UID,
164 G_FILE_ATTRIBUTE_TYPE_UINT32,
165 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
166 g_file_attribute_info_list_add (list,
167 G_FILE_ATTRIBUTE_UNIX_GID,
168 G_FILE_ATTRIBUTE_TYPE_UINT32,
169 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
173 g_file_attribute_info_list_add (list,
174 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
175 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
180 g_file_attribute_info_list_add (list,
181 G_FILE_ATTRIBUTE_TIME_MODIFIED,
182 G_FILE_ATTRIBUTE_TYPE_UINT64,
183 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
184 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
185 g_file_attribute_info_list_add (list,
186 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
187 G_FILE_ATTRIBUTE_TYPE_UINT32,
188 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
189 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
190 /* When copying, the target file is accessed. Replicating
191 * the source access time does not make sense in this case.
193 g_file_attribute_info_list_add (list,
194 G_FILE_ATTRIBUTE_TIME_ACCESS,
195 G_FILE_ATTRIBUTE_TYPE_UINT64,
196 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
197 g_file_attribute_info_list_add (list,
198 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
199 G_FILE_ATTRIBUTE_TYPE_UINT32,
200 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
203 local_writable_attributes = list;
207 g_local_file_init (GLocalFile *local)
213 canonicalize_filename (const char *filename)
215 char *canon, *start, *p, *q;
219 if (!g_path_is_absolute (filename))
221 cwd = g_get_current_dir ();
222 canon = g_build_filename (cwd, filename, NULL);
226 canon = g_strdup (filename);
228 start = (char *)g_path_skip_root (canon);
232 /* This shouldn't really happen, as g_get_current_dir() should
233 return an absolute pathname, but bug 573843 shows this is
234 not always happening */
236 return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
239 /* POSIX allows double slashes at the start to
240 * mean something special (as does windows too).
241 * So, "//" != "/", but more than two slashes
247 G_IS_DIR_SEPARATOR (*p);
254 memmove (start, start+i, strlen (start+i)+1);
260 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
262 memmove (p, p+1, strlen (p+1)+1);
264 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
267 /* Skip previous separator */
271 while (p > start && !G_IS_DIR_SEPARATOR (*p))
273 if (G_IS_DIR_SEPARATOR (*p))
274 *p++ = G_DIR_SEPARATOR;
275 memmove (p, q, strlen (q)+1);
279 /* Skip until next separator */
280 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
285 /* Canonicalize one separator */
286 *p++ = G_DIR_SEPARATOR;
290 /* Remove additional separators */
292 while (*q && G_IS_DIR_SEPARATOR (*q))
296 memmove (p, q, strlen (q)+1);
299 /* Remove trailing slashes */
300 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
307 _g_local_file_new (const char *filename)
311 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
312 local->filename = canonicalize_filename (filename);
314 return G_FILE (local);
318 g_local_file_is_native (GFile *file)
324 g_local_file_has_uri_scheme (GFile *file,
325 const char *uri_scheme)
327 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
331 g_local_file_get_uri_scheme (GFile *file)
333 return g_strdup ("file");
337 g_local_file_get_basename (GFile *file)
339 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
343 g_local_file_get_path (GFile *file)
345 return g_strdup (G_LOCAL_FILE (file)->filename);
349 g_local_file_get_uri (GFile *file)
351 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
355 get_filename_charset (const gchar **filename_charset)
357 const gchar **charsets;
360 is_utf8 = g_get_filename_charsets (&charsets);
362 if (filename_charset)
363 *filename_charset = charsets[0];
369 name_is_valid_for_display (const char *string,
370 gboolean is_valid_utf8)
374 if (!is_valid_utf8 &&
375 !g_utf8_validate (string, -1, NULL))
378 while ((c = *string++) != 0)
380 if (g_ascii_iscntrl (c))
388 g_local_file_get_parse_name (GFile *file)
390 const char *filename;
392 const gchar *charset;
394 char *roundtripped_filename;
395 gboolean free_utf8_filename;
396 gboolean is_valid_utf8;
399 filename = G_LOCAL_FILE (file)->filename;
400 if (get_filename_charset (&charset))
402 utf8_filename = (char *)filename;
403 free_utf8_filename = FALSE;
404 is_valid_utf8 = FALSE; /* Can't guarantee this */
408 utf8_filename = g_convert (filename, -1,
409 "UTF-8", charset, NULL, NULL, NULL);
410 free_utf8_filename = TRUE;
411 is_valid_utf8 = TRUE;
413 if (utf8_filename != NULL)
415 /* Make sure we can roundtrip: */
416 roundtripped_filename = g_convert (utf8_filename, -1,
417 charset, "UTF-8", NULL, NULL, NULL);
419 if (roundtripped_filename == NULL ||
420 strcmp (utf8_filename, roundtripped_filename) != 0)
422 g_free (utf8_filename);
423 utf8_filename = NULL;
426 g_free (roundtripped_filename);
430 if (utf8_filename != NULL &&
431 name_is_valid_for_display (utf8_filename, is_valid_utf8))
433 if (free_utf8_filename)
434 parse_name = utf8_filename;
436 parse_name = g_strdup (utf8_filename);
440 escaped_path = g_uri_escape_string (filename,
441 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
443 parse_name = g_strconcat ("file://",
444 (*escaped_path != '/') ? "/" : "",
448 g_free (escaped_path);
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);
606 get_fs_type (long f_type)
608 /* filesystem ids taken from linux manpage */
713 G_LOCK_DEFINE_STATIC(mount_info_hash);
714 static GHashTable *mount_info_hash = NULL;
715 static guint64 mount_info_hash_cache_time = 0;
718 MOUNT_INFO_READONLY = 1<<0
722 device_equal (gconstpointer v1,
725 return *(dev_t *)v1 == *(dev_t *)v2;
729 device_hash (gconstpointer v)
731 return (guint) *(dev_t *)v;
735 get_mount_info (GFileInfo *fs_info,
737 GFileAttributeMatcher *matcher)
741 gpointer info_as_ptr;
745 GUnixMountEntry *mount;
748 if (g_lstat (path, &buf) != 0)
751 G_LOCK (mount_info_hash);
753 if (mount_info_hash == NULL)
754 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
758 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
759 g_hash_table_remove_all (mount_info_hash);
761 got_info = g_hash_table_lookup_extended (mount_info_hash,
766 G_UNLOCK (mount_info_hash);
768 mount_info = GPOINTER_TO_UINT (info_as_ptr);
774 mountpoint = find_mountpoint_for (path, buf.st_dev);
775 if (mountpoint == NULL)
776 mountpoint = g_strdup ("/");
778 mount = g_unix_mount_at (mountpoint, &cache_time);
781 if (g_unix_mount_is_readonly (mount))
782 mount_info |= MOUNT_INFO_READONLY;
784 g_unix_mount_free (mount);
789 dev = g_new0 (dev_t, 1);
792 G_LOCK (mount_info_hash);
793 mount_info_hash_cache_time = cache_time;
794 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
795 G_UNLOCK (mount_info_hash);
798 if (mount_info & MOUNT_INFO_READONLY)
799 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
807 is_xp_or_later (void)
809 static int result = -1;
814 OSVERSIONINFOEX ver_info = {0};
815 DWORDLONG cond_mask = 0;
816 int op = VER_GREATER_EQUAL;
818 ver_info.dwOSVersionInfoSize = sizeof ver_info;
819 ver_info.dwMajorVersion = 5;
820 ver_info.dwMinorVersion = 1;
822 VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
823 VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
825 result = VerifyVersionInfo (&ver_info,
826 VER_MAJORVERSION | VER_MINORVERSION,
829 result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;
837 get_volume_for_path (const char *path)
843 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
844 result = g_new (wchar_t, MAX_PATH);
846 if (!GetVolumePathNameW (wpath, result, MAX_PATH))
848 char *msg = g_win32_error_message (GetLastError ());
849 g_critical ("GetVolumePathName failed: %s", msg);
856 len = wcslen (result);
857 if (len > 0 && result[len-1] != L'\\')
859 result = g_renew (wchar_t, result, len + 2);
869 find_mountpoint_for (const char *file, dev_t dev)
874 wpath = get_volume_for_path (file);
878 utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
885 get_filesystem_readonly (GFileInfo *info,
890 rootdir = get_volume_for_path (path);
894 if (is_xp_or_later ())
897 if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
898 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
899 (flags & FILE_READ_ONLY_VOLUME) != 0);
903 if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
904 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
911 #endif /* G_OS_WIN32 */
914 g_local_file_query_filesystem_info (GFile *file,
915 const char *attributes,
916 GCancellable *cancellable,
919 GLocalFile *local = G_LOCAL_FILE (file);
921 int statfs_result = 0;
927 struct statfs statfs_buffer;
928 #elif defined(USE_STATVFS)
929 struct statvfs statfs_buffer;
932 GFileAttributeMatcher *attribute_matcher;
939 statfs_result = statfs (local->filename, &statfs_buffer);
940 #elif STATFS_ARGS == 4
941 statfs_result = statfs (local->filename, &statfs_buffer,
942 sizeof (statfs_buffer), 0);
944 block_size = statfs_buffer.f_bsize;
946 /* Many backends can't report free size (for instance the gvfs fuse
947 backend for backend not supporting this), and set f_bfree to 0,
948 but it can be 0 for real too. We treat the availible == 0 and
949 free == 0 case as "both of these are invalid".
952 if (statfs_result == 0 &&
953 statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0)
957 #elif defined(USE_STATVFS)
958 statfs_result = statvfs (local->filename, &statfs_buffer);
959 block_size = statfs_buffer.f_frsize;
962 if (statfs_result == -1)
966 g_set_error (error, G_IO_ERROR,
967 g_io_error_from_errno (errsv),
968 _("Error getting filesystem info: %s"),
973 info = g_file_info_new ();
975 attribute_matcher = g_file_attribute_matcher_new (attributes);
978 g_file_attribute_matcher_matches (attribute_matcher,
979 G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
982 gchar *localdir = g_path_get_dirname (local->filename);
983 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
987 if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
988 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
991 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
995 g_file_attribute_matcher_matches (attribute_matcher,
996 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
999 gchar *localdir = g_path_get_dirname (local->filename);
1000 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1004 if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
1005 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, (guint64)li.QuadPart);
1008 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
1012 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1013 fstype = g_strdup(statfs_buffer.f_fstypename);
1015 fstype = get_fs_type (statfs_buffer.f_type);
1018 #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1019 fstype = g_strdup(statfs_buffer.f_basetype);
1024 g_file_attribute_matcher_matches (attribute_matcher,
1025 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1026 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1029 if (g_file_attribute_matcher_matches (attribute_matcher,
1030 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
1033 get_filesystem_readonly (info, local->filename);
1035 get_mount_info (info, local->filename, attribute_matcher);
1039 g_file_attribute_matcher_unref (attribute_matcher);
1045 g_local_file_find_enclosing_mount (GFile *file,
1046 GCancellable *cancellable,
1049 GLocalFile *local = G_LOCAL_FILE (file);
1054 if (g_lstat (local->filename, &buf) != 0)
1056 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1057 /* Translators: This is an error message when trying to
1058 * find the enclosing (user visible) mount of a file, but
1060 _("Containing mount does not exist"));
1064 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
1065 if (mountpoint == NULL)
1067 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1068 /* Translators: This is an error message when trying to
1069 * find the enclosing (user visible) mount of a file, but
1071 _("Containing mount does not exist"));
1075 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1076 g_free (mountpoint);
1080 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1081 /* Translators: This is an error message when trying to find
1082 * the enclosing (user visible) mount of a file, but none
1084 _("Containing mount does not exist"));
1089 g_local_file_set_display_name (GFile *file,
1090 const char *display_name,
1091 GCancellable *cancellable,
1094 GLocalFile *local, *new_local;
1095 GFile *new_file, *parent;
1096 struct stat statbuf;
1099 parent = g_file_get_parent (file);
1102 g_set_error_literal (error, G_IO_ERROR,
1104 _("Can't rename root directory"));
1108 new_file = g_file_get_child_for_display_name (parent, display_name, error);
1109 g_object_unref (parent);
1111 if (new_file == NULL)
1113 local = G_LOCAL_FILE (file);
1114 new_local = G_LOCAL_FILE (new_file);
1116 if (g_lstat (new_local->filename, &statbuf) == -1)
1120 if (errsv != ENOENT)
1122 g_set_error (error, G_IO_ERROR,
1123 g_io_error_from_errno (errsv),
1124 _("Error renaming file: %s"),
1125 g_strerror (errsv));
1131 g_set_error_literal (error, G_IO_ERROR,
1133 _("Can't rename file, filename already exist"));
1137 if (g_rename (local->filename, new_local->filename) == -1)
1141 if (errsv == EINVAL)
1142 /* We can't get a rename file into itself error herer,
1143 so this must be an invalid filename, on e.g. FAT */
1144 g_set_error_literal (error, G_IO_ERROR,
1145 G_IO_ERROR_INVALID_FILENAME,
1146 _("Invalid filename"));
1148 g_set_error (error, G_IO_ERROR,
1149 g_io_error_from_errno (errsv),
1150 _("Error renaming file: %s"),
1151 g_strerror (errsv));
1152 g_object_unref (new_file);
1160 g_local_file_query_info (GFile *file,
1161 const char *attributes,
1162 GFileQueryInfoFlags flags,
1163 GCancellable *cancellable,
1166 GLocalFile *local = G_LOCAL_FILE (file);
1168 GFileAttributeMatcher *matcher;
1169 char *basename, *dirname;
1170 GLocalParentFileInfo parent_info;
1172 matcher = g_file_attribute_matcher_new (attributes);
1174 basename = g_path_get_basename (local->filename);
1176 dirname = g_path_get_dirname (local->filename);
1177 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1180 info = _g_local_file_info_get (basename, local->filename,
1181 matcher, flags, &parent_info,
1185 _g_local_file_info_free_parent_info (&parent_info);
1188 g_file_attribute_matcher_unref (matcher);
1193 static GFileAttributeInfoList *
1194 g_local_file_query_settable_attributes (GFile *file,
1195 GCancellable *cancellable,
1198 return g_file_attribute_info_list_ref (local_writable_attributes);
1201 static GFileAttributeInfoList *
1202 g_local_file_query_writable_namespaces (GFile *file,
1203 GCancellable *cancellable,
1206 GFileAttributeInfoList *list;
1210 if (g_once_init_enter (&local_writable_namespaces))
1212 /* Writable namespaces: */
1214 list = g_file_attribute_info_list_new ();
1217 g_file_attribute_info_list_add (list,
1219 G_FILE_ATTRIBUTE_TYPE_STRING,
1220 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
1221 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1222 g_file_attribute_info_list_add (list,
1224 G_FILE_ATTRIBUTE_TYPE_STRING,
1225 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1228 vfs = g_vfs_get_default ();
1229 class = G_VFS_GET_CLASS (vfs);
1230 if (class->add_writable_namespaces)
1231 class->add_writable_namespaces (vfs, list);
1233 g_once_init_leave (&local_writable_namespaces, (gsize)list);
1235 list = (GFileAttributeInfoList *)local_writable_namespaces;
1237 return g_file_attribute_info_list_ref (list);
1241 g_local_file_set_attribute (GFile *file,
1242 const char *attribute,
1243 GFileAttributeType type,
1245 GFileQueryInfoFlags flags,
1246 GCancellable *cancellable,
1249 GLocalFile *local = G_LOCAL_FILE (file);
1251 return _g_local_file_info_set_attribute (local->filename,
1261 g_local_file_set_attributes_from_info (GFile *file,
1263 GFileQueryInfoFlags flags,
1264 GCancellable *cancellable,
1267 GLocalFile *local = G_LOCAL_FILE (file);
1268 int res, chained_res;
1269 GFileIface *default_iface;
1271 res = _g_local_file_info_set_attributes (local->filename,
1277 error = NULL; /* Don't write over error if further errors */
1279 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1281 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1283 return res && chained_res;
1286 static GFileInputStream *
1287 g_local_file_read (GFile *file,
1288 GCancellable *cancellable,
1291 GLocalFile *local = G_LOCAL_FILE (file);
1295 fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1300 g_set_error (error, G_IO_ERROR,
1301 g_io_error_from_errno (errsv),
1302 _("Error opening file: %s"),
1303 g_strerror (errsv));
1307 if (fstat(fd, &buf) == 0 && S_ISDIR (buf.st_mode))
1310 g_set_error_literal (error, G_IO_ERROR,
1311 G_IO_ERROR_IS_DIRECTORY,
1312 _("Can't open directory"));
1316 return _g_local_file_input_stream_new (fd);
1319 static GFileOutputStream *
1320 g_local_file_append_to (GFile *file,
1321 GFileCreateFlags flags,
1322 GCancellable *cancellable,
1325 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1326 flags, cancellable, error);
1329 static GFileOutputStream *
1330 g_local_file_create (GFile *file,
1331 GFileCreateFlags flags,
1332 GCancellable *cancellable,
1335 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1337 flags, cancellable, error);
1340 static GFileOutputStream *
1341 g_local_file_replace (GFile *file,
1343 gboolean make_backup,
1344 GFileCreateFlags flags,
1345 GCancellable *cancellable,
1348 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1350 etag, make_backup, flags,
1351 cancellable, error);
1354 static GFileIOStream *
1355 g_local_file_open_readwrite (GFile *file,
1356 GCancellable *cancellable,
1359 GFileOutputStream *output;
1362 output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
1364 cancellable, error);
1368 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1369 g_object_unref (output);
1373 static GFileIOStream *
1374 g_local_file_create_readwrite (GFile *file,
1375 GFileCreateFlags flags,
1376 GCancellable *cancellable,
1379 GFileOutputStream *output;
1382 output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1384 cancellable, error);
1388 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1389 g_object_unref (output);
1393 static GFileIOStream *
1394 g_local_file_replace_readwrite (GFile *file,
1396 gboolean make_backup,
1397 GFileCreateFlags flags,
1398 GCancellable *cancellable,
1401 GFileOutputStream *output;
1404 output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1406 etag, make_backup, flags,
1407 cancellable, error);
1411 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1412 g_object_unref (output);
1417 g_local_file_delete (GFile *file,
1418 GCancellable *cancellable,
1421 GLocalFile *local = G_LOCAL_FILE (file);
1425 if (g_remove (local->filename) == -1)
1429 /* Posix allows EEXIST too, but the more sane error
1430 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1432 if (errsv == EEXIST)
1435 g_set_error (error, G_IO_ERROR,
1436 g_io_error_from_errno (errsv),
1437 _("Error removing file: %s"),
1438 g_strerror (errsv));
1442 vfs = g_vfs_get_default ();
1443 class = G_VFS_GET_CLASS (vfs);
1444 if (class->local_file_removed)
1445 class->local_file_removed (vfs, local->filename);
1451 strip_trailing_slashes (const char *path)
1456 path_copy = g_strdup (path);
1457 len = strlen (path_copy);
1458 while (len > 1 && path_copy[len-1] == '/')
1459 path_copy[--len] = 0;
1465 expand_symlink (const char *link)
1467 char *resolved, *canonical, *parent, *link2;
1468 char symlink_value[4096];
1476 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1479 return g_strdup (link);
1480 symlink_value[res] = 0;
1483 if (g_path_is_absolute (symlink_value))
1484 return canonicalize_filename (symlink_value);
1487 link2 = strip_trailing_slashes (link);
1488 parent = g_path_get_dirname (link2);
1491 resolved = g_build_filename (parent, symlink_value, NULL);
1494 canonical = canonicalize_filename (resolved);
1503 get_parent (const char *path,
1507 struct stat parent_stat;
1511 path_copy = strip_trailing_slashes (path);
1513 parent = g_path_get_dirname (path_copy);
1514 if (strcmp (parent, ".") == 0 ||
1515 strcmp (parent, path_copy) == 0)
1525 if (g_lstat (parent, &parent_stat) != 0)
1531 if (S_ISLNK (parent_stat.st_mode))
1534 parent = expand_symlink (parent);
1539 if (num_recursions > 12)
1544 } while (S_ISLNK (parent_stat.st_mode));
1546 *parent_dev = parent_stat.st_dev;
1552 expand_all_symlinks (const char *path)
1554 char *parent, *parent_expanded;
1555 char *basename, *res;
1558 parent = get_parent (path, &parent_dev);
1561 parent_expanded = expand_all_symlinks (parent);
1563 basename = g_path_get_basename (path);
1564 res = g_build_filename (parent_expanded, basename, NULL);
1566 g_free (parent_expanded);
1569 res = g_strdup (path);
1577 find_mountpoint_for (const char *file,
1581 dev_t dir_dev, parent_dev;
1583 dir = g_strdup (file);
1588 parent = get_parent (dir, &parent_dev);
1592 if (parent_dev != dir_dev)
1604 find_topdir_for (const char *file)
1609 dir = get_parent (file, &dir_dev);
1613 return find_mountpoint_for (dir, dir_dev);
1617 get_unique_filename (const char *basename,
1623 return g_strdup (basename);
1625 dot = strchr (basename, '.');
1627 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1629 return g_strdup_printf ("%s.%d", basename, id);
1633 path_has_prefix (const char *path,
1641 prefix_len = strlen (prefix);
1643 if (strncmp (path, prefix, prefix_len) == 0 &&
1644 (prefix_len == 0 || /* empty prefix always matches */
1645 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1646 path[prefix_len] == 0 ||
1647 path[prefix_len] == '/'))
1654 try_make_relative (const char *path,
1657 char *path2, *base2;
1660 path2 = expand_all_symlinks (path);
1661 base2 = expand_all_symlinks (base);
1664 if (path_has_prefix (path2, base2))
1666 relative = path2 + strlen (base2);
1667 while (*relative == '/')
1669 relative = g_strdup (relative);
1677 /* Failed, use abs path */
1678 return g_strdup (path);
1682 escape_trash_name (char *name)
1685 const gchar hex[16] = "0123456789ABCDEF";
1687 str = g_string_new ("");
1695 if (g_ascii_isprint (c))
1696 g_string_append_c (str, c);
1699 g_string_append_c (str, '%');
1700 g_string_append_c (str, hex[((guchar)c) >> 4]);
1701 g_string_append_c (str, hex[((guchar)c) & 0xf]);
1705 return g_string_free (str, FALSE);
1709 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1711 static gsize home_dev_set = 0;
1712 static dev_t home_dev;
1713 char *topdir, *globaldir, *trashdir, *tmpname;
1716 struct stat global_stat, trash_stat;
1719 if (g_once_init_enter (&home_dev_set))
1721 struct stat home_stat;
1723 g_stat (g_get_home_dir (), &home_stat);
1724 home_dev = home_stat.st_dev;
1725 g_once_init_leave (&home_dev_set, 1);
1728 /* Assume we can trash to the home */
1729 if (dir_dev == home_dev)
1732 topdir = find_mountpoint_for (dirname, dir_dev);
1736 globaldir = g_build_filename (topdir, ".Trash", NULL);
1737 if (g_lstat (globaldir, &global_stat) == 0 &&
1738 S_ISDIR (global_stat.st_mode) &&
1739 (global_stat.st_mode & S_ISVTX) != 0)
1741 /* got a toplevel sysadmin created dir, assume we
1742 * can trash to it (we should be able to create a dir)
1743 * This fails for the FAT case where the ownership of
1744 * that dir would be wrong though..
1752 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1754 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1756 tmpname = g_strdup_printf (".Trash-%s", uid_str);
1757 trashdir = g_build_filename (topdir, tmpname, NULL);
1760 if (g_lstat (trashdir, &trash_stat) == 0)
1764 return S_ISDIR (trash_stat.st_mode) &&
1765 trash_stat.st_uid == uid;
1769 /* User specific trash didn't exist, can we create it? */
1770 res = g_access (topdir, W_OK) == 0;
1778 g_local_file_trash (GFile *file,
1779 GCancellable *cancellable,
1782 GLocalFile *local = G_LOCAL_FILE (file);
1783 struct stat file_stat, home_stat;
1784 const char *homedir;
1785 char *trashdir, *topdir, *infodir, *filesdir;
1786 char *basename, *trashname, *trashfile, *infoname, *infofile;
1787 char *original_name, *original_name_escaped;
1790 gboolean is_homedir_trash;
1791 char delete_time[32];
1793 struct stat trash_stat, global_stat;
1794 char *dirname, *globaldir;
1796 if (g_lstat (local->filename, &file_stat) != 0)
1800 g_set_error (error, G_IO_ERROR,
1801 g_io_error_from_errno (errsv),
1802 _("Error trashing file: %s"),
1803 g_strerror (errsv));
1807 homedir = g_get_home_dir ();
1808 g_stat (homedir, &home_stat);
1810 is_homedir_trash = FALSE;
1812 if (file_stat.st_dev == home_stat.st_dev)
1814 is_homedir_trash = TRUE;
1816 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1817 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1822 display_name = g_filename_display_name (trashdir);
1823 g_set_error (error, G_IO_ERROR,
1824 g_io_error_from_errno (errsv),
1825 _("Unable to create trash dir %s: %s"),
1826 display_name, g_strerror (errsv));
1827 g_free (display_name);
1831 topdir = g_strdup (g_get_user_data_dir ());
1839 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1841 topdir = find_topdir_for (local->filename);
1844 g_set_error_literal (error, G_IO_ERROR,
1845 G_IO_ERROR_NOT_SUPPORTED,
1846 _("Unable to find toplevel directory for trash"));
1850 /* Try looking for global trash dir $topdir/.Trash/$uid */
1851 globaldir = g_build_filename (topdir, ".Trash", NULL);
1852 if (g_lstat (globaldir, &global_stat) == 0 &&
1853 S_ISDIR (global_stat.st_mode) &&
1854 (global_stat.st_mode & S_ISVTX) != 0)
1856 trashdir = g_build_filename (globaldir, uid_str, NULL);
1858 if (g_lstat (trashdir, &trash_stat) == 0)
1860 if (!S_ISDIR (trash_stat.st_mode) ||
1861 trash_stat.st_uid != uid)
1863 /* Not a directory or not owned by user, ignore */
1868 else if (g_mkdir (trashdir, 0700) == -1)
1876 if (trashdir == NULL)
1878 gboolean tried_create;
1880 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1881 dirname = g_strdup_printf (".Trash-%s", uid_str);
1882 trashdir = g_build_filename (topdir, dirname, NULL);
1885 tried_create = FALSE;
1888 if (g_lstat (trashdir, &trash_stat) == 0)
1890 if (!S_ISDIR (trash_stat.st_mode) ||
1891 trash_stat.st_uid != uid)
1893 /* Remove the failed directory */
1895 g_remove (trashdir);
1897 /* Not a directory or not owned by user, ignore */
1904 if (!tried_create &&
1905 g_mkdir (trashdir, 0700) != -1)
1907 /* Ensure that the created dir has the right uid etc.
1908 This might fail on e.g. a FAT dir */
1909 tried_create = TRUE;
1920 if (trashdir == NULL)
1923 g_set_error_literal (error, G_IO_ERROR,
1924 G_IO_ERROR_NOT_SUPPORTED,
1925 _("Unable to find or create trash directory"));
1930 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1932 infodir = g_build_filename (trashdir, "info", NULL);
1933 filesdir = g_build_filename (trashdir, "files", NULL);
1936 /* Make sure we have the subdirectories */
1937 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1938 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
1943 g_set_error_literal (error, G_IO_ERROR,
1944 G_IO_ERROR_NOT_SUPPORTED,
1945 _("Unable to find or create trash directory"));
1949 basename = g_path_get_basename (local->filename);
1957 trashname = get_unique_filename (basename, i++);
1958 infoname = g_strconcat (trashname, ".trashinfo", NULL);
1959 infofile = g_build_filename (infodir, infoname, NULL);
1962 fd = open (infofile, O_CREAT | O_EXCL, 0666);
1963 } while (fd == -1 && errno == EEXIST);
1977 g_set_error (error, G_IO_ERROR,
1978 g_io_error_from_errno (errsv),
1979 _("Unable to create trashing info file: %s"),
1980 g_strerror (errsv));
1986 /* TODO: Maybe we should verify that you can delete the file from the trash
1987 before moving it? OTOH, that is hard, as it needs a recursive scan */
1989 trashfile = g_build_filename (filesdir, trashname, NULL);
1993 if (g_rename (local->filename, trashfile) == -1)
2003 /* The trash dir was actually on another fs anyway!?
2004 This can happen when the same device is mounted multiple
2005 times, or with bind mounts of the same fs. */
2006 g_set_error (error, G_IO_ERROR,
2007 G_IO_ERROR_NOT_SUPPORTED,
2008 _("Unable to trash file: %s"),
2009 g_strerror (errsv));
2011 g_set_error (error, G_IO_ERROR,
2012 g_io_error_from_errno (errsv),
2013 _("Unable to trash file: %s"),
2014 g_strerror (errsv));
2020 /* TODO: Do we need to update mtime/atime here after the move? */
2022 /* Use absolute names for homedir */
2023 if (is_homedir_trash)
2024 original_name = g_strdup (local->filename);
2026 original_name = try_make_relative (local->filename, topdir);
2027 original_name_escaped = escape_trash_name (original_name);
2029 g_free (original_name);
2036 localtime_r (&t, &now);
2038 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
2041 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2042 original_name_escaped, delete_time);
2044 g_file_set_contents (infofile, data, -1, NULL);
2048 g_free (original_name_escaped);
2053 #else /* G_OS_WIN32 */
2055 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
2057 return FALSE; /* XXX ??? */
2061 g_local_file_trash (GFile *file,
2062 GCancellable *cancellable,
2065 GLocalFile *local = G_LOCAL_FILE (file);
2066 SHFILEOPSTRUCTW op = {0};
2071 wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
2072 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2073 wfilename = g_renew (wchar_t, wfilename, len + 2);
2074 wfilename[len + 1] = 0;
2076 op.wFunc = FO_DELETE;
2077 op.pFrom = wfilename;
2078 op.fFlags = FOF_ALLOWUNDO;
2080 success = SHFileOperationW (&op) == 0;
2082 if (success && op.fAnyOperationsAborted)
2084 if (cancellable && !g_cancellable_is_cancelled (cancellable))
2085 g_cancellable_cancel (cancellable);
2086 g_set_error (error, G_IO_ERROR,
2087 G_IO_ERROR_CANCELLED,
2088 _("Unable to trash file: %s"),
2089 _("Operation was cancelled"));
2093 g_set_error (error, G_IO_ERROR,
2095 _("Unable to trash file: %s"),
2096 _("internal error"));
2101 #endif /* G_OS_WIN32 */
2104 g_local_file_make_directory (GFile *file,
2105 GCancellable *cancellable,
2108 GLocalFile *local = G_LOCAL_FILE (file);
2110 if (g_mkdir (local->filename, 0777) == -1)
2114 if (errsv == EINVAL)
2115 /* This must be an invalid filename, on e.g. FAT */
2116 g_set_error_literal (error, G_IO_ERROR,
2117 G_IO_ERROR_INVALID_FILENAME,
2118 _("Invalid filename"));
2120 g_set_error (error, G_IO_ERROR,
2121 g_io_error_from_errno (errsv),
2122 _("Error creating directory: %s"),
2123 g_strerror (errsv));
2131 g_local_file_make_symbolic_link (GFile *file,
2132 const char *symlink_value,
2133 GCancellable *cancellable,
2137 GLocalFile *local = G_LOCAL_FILE (file);
2139 if (symlink (symlink_value, local->filename) == -1)
2143 if (errsv == EINVAL)
2144 /* This must be an invalid filename, on e.g. FAT */
2145 g_set_error_literal (error, G_IO_ERROR,
2146 G_IO_ERROR_INVALID_FILENAME,
2147 _("Invalid filename"));
2148 else if (errsv == EPERM)
2149 g_set_error (error, G_IO_ERROR,
2150 G_IO_ERROR_NOT_SUPPORTED,
2151 _("Filesystem does not support symbolic links"));
2153 g_set_error (error, G_IO_ERROR,
2154 g_io_error_from_errno (errsv),
2155 _("Error making symbolic link: %s"),
2156 g_strerror (errsv));
2161 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
2168 g_local_file_copy (GFile *source,
2170 GFileCopyFlags flags,
2171 GCancellable *cancellable,
2172 GFileProgressCallback progress_callback,
2173 gpointer progress_callback_data,
2176 /* Fall back to default copy */
2177 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
2182 g_local_file_move (GFile *source,
2184 GFileCopyFlags flags,
2185 GCancellable *cancellable,
2186 GFileProgressCallback progress_callback,
2187 gpointer progress_callback_data,
2190 GLocalFile *local_source, *local_destination;
2191 struct stat statbuf;
2192 gboolean destination_exist, source_is_dir;
2199 if (!G_IS_LOCAL_FILE (source) ||
2200 !G_IS_LOCAL_FILE (destination))
2202 /* Fall back to default move */
2203 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2207 local_source = G_LOCAL_FILE (source);
2208 local_destination = G_LOCAL_FILE (destination);
2210 res = g_lstat (local_source->filename, &statbuf);
2215 g_set_error (error, G_IO_ERROR,
2216 g_io_error_from_errno (errsv),
2217 _("Error moving file: %s"),
2218 g_strerror (errsv));
2222 source_is_dir = S_ISDIR (statbuf.st_mode);
2223 source_size = statbuf.st_size;
2225 destination_exist = FALSE;
2226 res = g_lstat (local_destination->filename, &statbuf);
2229 destination_exist = TRUE; /* Target file exists */
2231 if (flags & G_FILE_COPY_OVERWRITE)
2233 /* Always fail on dirs, even with overwrite */
2234 if (S_ISDIR (statbuf.st_mode))
2237 g_set_error_literal (error,
2239 G_IO_ERROR_WOULD_MERGE,
2240 _("Can't move directory over directory"));
2242 g_set_error_literal (error,
2244 G_IO_ERROR_IS_DIRECTORY,
2245 _("Can't copy over directory"));
2251 g_set_error_literal (error,
2254 _("Target file exists"));
2259 if (flags & G_FILE_COPY_BACKUP && destination_exist)
2261 backup_name = g_strconcat (local_destination->filename, "~", NULL);
2262 if (g_rename (local_destination->filename, backup_name) == -1)
2264 g_set_error_literal (error,
2266 G_IO_ERROR_CANT_CREATE_BACKUP,
2267 _("Backup file creation failed"));
2268 g_free (backup_name);
2271 g_free (backup_name);
2272 destination_exist = FALSE; /* It did, but no more */
2275 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2277 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2278 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2279 res = g_unlink (local_destination->filename);
2284 g_set_error (error, G_IO_ERROR,
2285 g_io_error_from_errno (errsv),
2286 _("Error removing target file: %s"),
2287 g_strerror (errsv));
2292 if (g_rename (local_source->filename, local_destination->filename) == -1)
2297 /* This will cause the fallback code to run */
2298 g_set_error_literal (error, G_IO_ERROR,
2299 G_IO_ERROR_NOT_SUPPORTED,
2300 _("Move between mounts not supported"));
2301 else if (errsv == EINVAL)
2302 /* This must be an invalid filename, on e.g. FAT, or
2303 we're trying to move the file into itself...
2304 We return invalid filename for both... */
2305 g_set_error_literal (error, G_IO_ERROR,
2306 G_IO_ERROR_INVALID_FILENAME,
2307 _("Invalid filename"));
2309 g_set_error (error, G_IO_ERROR,
2310 g_io_error_from_errno (errsv),
2311 _("Error moving file: %s"),
2312 g_strerror (errsv));
2316 vfs = g_vfs_get_default ();
2317 class = G_VFS_GET_CLASS (vfs);
2318 if (class->local_file_moved)
2319 class->local_file_moved (vfs, local_source->filename, local_destination->filename);
2321 /* Make sure we send full copied size */
2322 if (progress_callback)
2323 progress_callback (source_size, source_size, progress_callback_data);
2328 static GFileMonitor*
2329 g_local_file_monitor_dir (GFile *file,
2330 GFileMonitorFlags flags,
2331 GCancellable *cancellable,
2334 GLocalFile* local_file = G_LOCAL_FILE(file);
2335 return _g_local_directory_monitor_new (local_file->filename, flags, error);
2338 static GFileMonitor*
2339 g_local_file_monitor_file (GFile *file,
2340 GFileMonitorFlags flags,
2341 GCancellable *cancellable,
2344 GLocalFile* local_file = G_LOCAL_FILE(file);
2345 return _g_local_file_monitor_new (local_file->filename, flags, error);
2349 g_local_file_file_iface_init (GFileIface *iface)
2351 iface->dup = g_local_file_dup;
2352 iface->hash = g_local_file_hash;
2353 iface->equal = g_local_file_equal;
2354 iface->is_native = g_local_file_is_native;
2355 iface->has_uri_scheme = g_local_file_has_uri_scheme;
2356 iface->get_uri_scheme = g_local_file_get_uri_scheme;
2357 iface->get_basename = g_local_file_get_basename;
2358 iface->get_path = g_local_file_get_path;
2359 iface->get_uri = g_local_file_get_uri;
2360 iface->get_parse_name = g_local_file_get_parse_name;
2361 iface->get_parent = g_local_file_get_parent;
2362 iface->prefix_matches = g_local_file_prefix_matches;
2363 iface->get_relative_path = g_local_file_get_relative_path;
2364 iface->resolve_relative_path = g_local_file_resolve_relative_path;
2365 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2366 iface->set_display_name = g_local_file_set_display_name;
2367 iface->enumerate_children = g_local_file_enumerate_children;
2368 iface->query_info = g_local_file_query_info;
2369 iface->query_filesystem_info = g_local_file_query_filesystem_info;
2370 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2371 iface->query_settable_attributes = g_local_file_query_settable_attributes;
2372 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2373 iface->set_attribute = g_local_file_set_attribute;
2374 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2375 iface->read_fn = g_local_file_read;
2376 iface->append_to = g_local_file_append_to;
2377 iface->create = g_local_file_create;
2378 iface->replace = g_local_file_replace;
2379 iface->open_readwrite = g_local_file_open_readwrite;
2380 iface->create_readwrite = g_local_file_create_readwrite;
2381 iface->replace_readwrite = g_local_file_replace_readwrite;
2382 iface->delete_file = g_local_file_delete;
2383 iface->trash = g_local_file_trash;
2384 iface->make_directory = g_local_file_make_directory;
2385 iface->make_symbolic_link = g_local_file_make_symbolic_link;
2386 iface->copy = g_local_file_copy;
2387 iface->move = g_local_file_move;
2388 iface->monitor_dir = g_local_file_monitor_dir;
2389 iface->monitor_file = g_local_file_monitor_file;
2391 iface->supports_thread_contexts = TRUE;