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>
69 #define _WIN32_WINNT 0x0500
74 #ifndef FILE_READ_ONLY_VOLUME
75 #define FILE_READ_ONLY_VOLUME 0x00080000
79 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
82 #define S_ISLNK(m) (0)
87 static void g_local_file_file_iface_init (GFileIface *iface);
89 static GFileAttributeInfoList *local_writable_attributes = NULL;
90 static /* GFileAttributeInfoList * */ gsize local_writable_namespaces = 0;
94 GObject parent_instance;
99 #define g_local_file_get_type _g_local_file_get_type
100 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
101 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
102 g_local_file_file_iface_init))
104 static char *find_mountpoint_for (const char *file, dev_t dev);
107 g_local_file_finalize (GObject *object)
111 local = G_LOCAL_FILE (object);
113 g_free (local->filename);
115 G_OBJECT_CLASS (g_local_file_parent_class)->finalize (object);
119 g_local_file_class_init (GLocalFileClass *klass)
121 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
122 GFileAttributeInfoList *list;
124 gobject_class->finalize = g_local_file_finalize;
126 /* Set up attribute lists */
128 /* Writable attributes: */
130 list = g_file_attribute_info_list_new ();
132 g_file_attribute_info_list_add (list,
133 G_FILE_ATTRIBUTE_UNIX_MODE,
134 G_FILE_ATTRIBUTE_TYPE_UINT32,
135 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
136 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
139 g_file_attribute_info_list_add (list,
140 G_FILE_ATTRIBUTE_UNIX_UID,
141 G_FILE_ATTRIBUTE_TYPE_UINT32,
142 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
143 g_file_attribute_info_list_add (list,
144 G_FILE_ATTRIBUTE_UNIX_GID,
145 G_FILE_ATTRIBUTE_TYPE_UINT32,
146 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
150 g_file_attribute_info_list_add (list,
151 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
152 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
157 g_file_attribute_info_list_add (list,
158 G_FILE_ATTRIBUTE_TIME_MODIFIED,
159 G_FILE_ATTRIBUTE_TYPE_UINT64,
160 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
161 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
162 g_file_attribute_info_list_add (list,
163 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
164 G_FILE_ATTRIBUTE_TYPE_UINT32,
165 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
166 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
167 /* When copying, the target file is accessed. Replicating
168 * the source access time does not make sense in this case.
170 g_file_attribute_info_list_add (list,
171 G_FILE_ATTRIBUTE_TIME_ACCESS,
172 G_FILE_ATTRIBUTE_TYPE_UINT64,
173 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
174 g_file_attribute_info_list_add (list,
175 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
176 G_FILE_ATTRIBUTE_TYPE_UINT32,
177 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
180 local_writable_attributes = list;
184 g_local_file_init (GLocalFile *local)
190 canonicalize_filename (const char *filename)
192 char *canon, *start, *p, *q;
196 if (!g_path_is_absolute (filename))
198 cwd = g_get_current_dir ();
199 canon = g_build_filename (cwd, filename, NULL);
203 canon = g_strdup (filename);
205 start = (char *)g_path_skip_root (canon);
209 /* This shouldn't really happen, as g_get_current_dir() should
210 return an absolute pathname, but bug 573843 shows this is
211 not always happening */
213 return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
216 /* POSIX allows double slashes at the start to
217 * mean something special (as does windows too).
218 * So, "//" != "/", but more than two slashes
224 G_IS_DIR_SEPARATOR (*p);
231 memmove (start, start+i, strlen (start+i)+1);
237 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
239 memmove (p, p+1, strlen (p+1)+1);
241 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
244 /* Skip previous separator */
248 while (p > start && !G_IS_DIR_SEPARATOR (*p))
250 if (G_IS_DIR_SEPARATOR (*p))
251 *p++ = G_DIR_SEPARATOR;
252 memmove (p, q, strlen (q)+1);
256 /* Skip until next separator */
257 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
262 /* Canonicalize one separator */
263 *p++ = G_DIR_SEPARATOR;
267 /* Remove additional separators */
269 while (*q && G_IS_DIR_SEPARATOR (*q))
273 memmove (p, q, strlen (q)+1);
276 /* Remove trailing slashes */
277 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
284 _g_local_file_new (const char *filename)
288 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
289 local->filename = canonicalize_filename (filename);
291 return G_FILE (local);
295 g_local_file_is_native (GFile *file)
301 g_local_file_has_uri_scheme (GFile *file,
302 const char *uri_scheme)
304 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
308 g_local_file_get_uri_scheme (GFile *file)
310 return g_strdup ("file");
314 g_local_file_get_basename (GFile *file)
316 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
320 g_local_file_get_path (GFile *file)
322 return g_strdup (G_LOCAL_FILE (file)->filename);
326 g_local_file_get_uri (GFile *file)
328 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
332 get_filename_charset (const gchar **filename_charset)
334 const gchar **charsets;
337 is_utf8 = g_get_filename_charsets (&charsets);
339 if (filename_charset)
340 *filename_charset = charsets[0];
346 name_is_valid_for_display (const char *string,
347 gboolean is_valid_utf8)
351 if (!is_valid_utf8 &&
352 !g_utf8_validate (string, -1, NULL))
355 while ((c = *string++) != 0)
357 if (g_ascii_iscntrl (c))
365 g_local_file_get_parse_name (GFile *file)
367 const char *filename;
369 const gchar *charset;
371 char *roundtripped_filename;
372 gboolean free_utf8_filename;
373 gboolean is_valid_utf8;
376 filename = G_LOCAL_FILE (file)->filename;
377 if (get_filename_charset (&charset))
379 utf8_filename = (char *)filename;
380 free_utf8_filename = FALSE;
381 is_valid_utf8 = FALSE; /* Can't guarantee this */
385 utf8_filename = g_convert (filename, -1,
386 "UTF-8", charset, NULL, NULL, NULL);
387 free_utf8_filename = TRUE;
388 is_valid_utf8 = TRUE;
390 if (utf8_filename != NULL)
392 /* Make sure we can roundtrip: */
393 roundtripped_filename = g_convert (utf8_filename, -1,
394 charset, "UTF-8", NULL, NULL, NULL);
396 if (roundtripped_filename == NULL ||
397 strcmp (filename, roundtripped_filename) != 0)
399 g_free (utf8_filename);
400 utf8_filename = NULL;
403 g_free (roundtripped_filename);
407 if (utf8_filename != NULL &&
408 name_is_valid_for_display (utf8_filename, is_valid_utf8))
410 if (free_utf8_filename)
411 parse_name = utf8_filename;
413 parse_name = g_strdup (utf8_filename);
417 escaped_path = g_uri_escape_string (filename,
418 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
420 parse_name = g_strconcat ("file://",
421 (*escaped_path != '/') ? "/" : "",
425 g_free (escaped_path);
427 if (free_utf8_filename)
428 g_free (utf8_filename);
435 g_local_file_get_parent (GFile *file)
437 GLocalFile *local = G_LOCAL_FILE (file);
438 const char *non_root;
443 non_root = g_path_skip_root (local->filename);
447 dirname = g_path_get_dirname (local->filename);
448 parent = _g_local_file_new (dirname);
454 g_local_file_dup (GFile *file)
456 GLocalFile *local = G_LOCAL_FILE (file);
458 return _g_local_file_new (local->filename);
462 g_local_file_hash (GFile *file)
464 GLocalFile *local = G_LOCAL_FILE (file);
466 return g_str_hash (local->filename);
470 g_local_file_equal (GFile *file1,
473 GLocalFile *local1 = G_LOCAL_FILE (file1);
474 GLocalFile *local2 = G_LOCAL_FILE (file2);
476 return g_str_equal (local1->filename, local2->filename);
480 match_prefix (const char *path,
485 prefix_len = strlen (prefix);
486 if (strncmp (path, prefix, prefix_len) != 0)
489 /* Handle the case where prefix is the root, so that
490 * the IS_DIR_SEPRARATOR check below works */
491 if (prefix_len > 0 &&
492 G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
495 return path + prefix_len;
499 g_local_file_prefix_matches (GFile *parent,
502 GLocalFile *parent_local = G_LOCAL_FILE (parent);
503 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
504 const char *remainder;
506 remainder = match_prefix (descendant_local->filename, parent_local->filename);
507 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
513 g_local_file_get_relative_path (GFile *parent,
516 GLocalFile *parent_local = G_LOCAL_FILE (parent);
517 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
518 const char *remainder;
520 remainder = match_prefix (descendant_local->filename, parent_local->filename);
522 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
523 return g_strdup (remainder + 1);
528 g_local_file_resolve_relative_path (GFile *file,
529 const char *relative_path)
531 GLocalFile *local = G_LOCAL_FILE (file);
535 if (g_path_is_absolute (relative_path))
536 return _g_local_file_new (relative_path);
538 filename = g_build_filename (local->filename, relative_path, NULL);
539 child = _g_local_file_new (filename);
545 static GFileEnumerator *
546 g_local_file_enumerate_children (GFile *file,
547 const char *attributes,
548 GFileQueryInfoFlags flags,
549 GCancellable *cancellable,
552 GLocalFile *local = G_LOCAL_FILE (file);
553 return _g_local_file_enumerator_new (local,
559 g_local_file_get_child_for_display_name (GFile *file,
560 const char *display_name,
566 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
567 if (basename == NULL)
569 g_set_error (error, G_IO_ERROR,
570 G_IO_ERROR_INVALID_FILENAME,
571 _("Invalid filename %s"), display_name);
575 new_file = g_file_get_child (file, basename);
581 #if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
583 get_fs_type (long f_type)
585 /* filesystem ids taken from linux manpage */
692 G_LOCK_DEFINE_STATIC(mount_info_hash);
693 static GHashTable *mount_info_hash = NULL;
694 static guint64 mount_info_hash_cache_time = 0;
697 MOUNT_INFO_READONLY = 1<<0
701 device_equal (gconstpointer v1,
704 return *(dev_t *)v1 == *(dev_t *)v2;
708 device_hash (gconstpointer v)
710 return (guint) *(dev_t *)v;
714 get_mount_info (GFileInfo *fs_info,
716 GFileAttributeMatcher *matcher)
720 gpointer info_as_ptr;
724 GUnixMountEntry *mount;
727 if (g_lstat (path, &buf) != 0)
730 G_LOCK (mount_info_hash);
732 if (mount_info_hash == NULL)
733 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
737 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
738 g_hash_table_remove_all (mount_info_hash);
740 got_info = g_hash_table_lookup_extended (mount_info_hash,
745 G_UNLOCK (mount_info_hash);
747 mount_info = GPOINTER_TO_UINT (info_as_ptr);
753 mountpoint = find_mountpoint_for (path, buf.st_dev);
754 if (mountpoint == NULL)
755 mountpoint = g_strdup ("/");
757 mount = g_unix_mount_at (mountpoint, &cache_time);
760 if (g_unix_mount_is_readonly (mount))
761 mount_info |= MOUNT_INFO_READONLY;
763 g_unix_mount_free (mount);
768 dev = g_new0 (dev_t, 1);
771 G_LOCK (mount_info_hash);
772 mount_info_hash_cache_time = cache_time;
773 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
774 G_UNLOCK (mount_info_hash);
777 if (mount_info & MOUNT_INFO_READONLY)
778 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
786 is_xp_or_later (void)
788 static int result = -1;
793 OSVERSIONINFOEX ver_info = {0};
794 DWORDLONG cond_mask = 0;
795 int op = VER_GREATER_EQUAL;
797 ver_info.dwOSVersionInfoSize = sizeof ver_info;
798 ver_info.dwMajorVersion = 5;
799 ver_info.dwMinorVersion = 1;
801 VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
802 VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
804 result = VerifyVersionInfo (&ver_info,
805 VER_MAJORVERSION | VER_MINORVERSION,
808 result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;
816 get_volume_for_path (const char *path)
822 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
823 result = g_new (wchar_t, MAX_PATH);
825 if (!GetVolumePathNameW (wpath, result, MAX_PATH))
827 char *msg = g_win32_error_message (GetLastError ());
828 g_critical ("GetVolumePathName failed: %s", msg);
835 len = wcslen (result);
836 if (len > 0 && result[len-1] != L'\\')
838 result = g_renew (wchar_t, result, len + 2);
848 find_mountpoint_for (const char *file, dev_t dev)
853 wpath = get_volume_for_path (file);
857 utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
864 get_filesystem_readonly (GFileInfo *info,
869 rootdir = get_volume_for_path (path);
873 if (is_xp_or_later ())
876 if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
877 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
878 (flags & FILE_READ_ONLY_VOLUME) != 0);
882 if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
883 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
890 #endif /* G_OS_WIN32 */
893 g_local_file_query_filesystem_info (GFile *file,
894 const char *attributes,
895 GCancellable *cancellable,
898 GLocalFile *local = G_LOCAL_FILE (file);
900 int statfs_result = 0;
906 struct statfs statfs_buffer;
907 #elif defined(USE_STATVFS)
909 struct statvfs statfs_buffer;
912 GFileAttributeMatcher *attribute_matcher;
919 statfs_result = statfs (local->filename, &statfs_buffer);
920 #elif STATFS_ARGS == 4
921 statfs_result = statfs (local->filename, &statfs_buffer,
922 sizeof (statfs_buffer), 0);
924 block_size = statfs_buffer.f_bsize;
926 /* Many backends can't report free size (for instance the gvfs fuse
927 backend for backend not supporting this), and set f_bfree to 0,
928 but it can be 0 for real too. We treat the available == 0 and
929 free == 0 case as "both of these are invalid".
932 if (statfs_result == 0 &&
933 statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0)
937 #elif defined(USE_STATVFS)
938 statfs_result = statvfs (local->filename, &statfs_buffer);
939 block_size = statfs_buffer.f_frsize;
942 if (statfs_result == -1)
946 g_set_error (error, G_IO_ERROR,
947 g_io_error_from_errno (errsv),
948 _("Error getting filesystem info: %s"),
953 info = g_file_info_new ();
955 attribute_matcher = g_file_attribute_matcher_new (attributes);
958 g_file_attribute_matcher_matches (attribute_matcher,
959 G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
962 gchar *localdir = g_path_get_dirname (local->filename);
963 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
967 if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
968 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
971 #if defined(USE_STATFS) || defined(USE_STATVFS)
972 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
977 g_file_attribute_matcher_matches (attribute_matcher,
978 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
981 gchar *localdir = g_path_get_dirname (local->filename);
982 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
986 if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
987 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, (guint64)li.QuadPart);
990 #if defined(USE_STATFS) || defined(USE_STATVFS)
991 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
998 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
999 fstype = g_strdup (statfs_buffer.f_fstypename);
1001 fstype = get_fs_type (statfs_buffer.f_type);
1004 #elif defined(USE_STATVFS)
1005 #if defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
1006 fstype = g_strdup (statfs_buffer.f_fstypename);
1007 #elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1008 fstype = g_strdup (statfs_buffer.f_basetype);
1015 g_file_attribute_matcher_matches (attribute_matcher,
1016 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1017 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1020 if (g_file_attribute_matcher_matches (attribute_matcher,
1021 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
1024 get_filesystem_readonly (info, local->filename);
1026 get_mount_info (info, local->filename, attribute_matcher);
1030 g_file_attribute_matcher_unref (attribute_matcher);
1036 g_local_file_find_enclosing_mount (GFile *file,
1037 GCancellable *cancellable,
1040 GLocalFile *local = G_LOCAL_FILE (file);
1045 if (g_lstat (local->filename, &buf) != 0)
1047 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1048 /* Translators: This is an error message when trying to
1049 * find the enclosing (user visible) mount of a file, but
1051 _("Containing mount does not exist"));
1055 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
1056 if (mountpoint == NULL)
1058 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1059 /* Translators: This is an error message when trying to
1060 * find the enclosing (user visible) mount of a file, but
1062 _("Containing mount does not exist"));
1066 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1067 g_free (mountpoint);
1071 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1072 /* Translators: This is an error message when trying to find
1073 * the enclosing (user visible) mount of a file, but none
1075 _("Containing mount does not exist"));
1080 g_local_file_set_display_name (GFile *file,
1081 const char *display_name,
1082 GCancellable *cancellable,
1085 GLocalFile *local, *new_local;
1086 GFile *new_file, *parent;
1092 parent = g_file_get_parent (file);
1095 g_set_error_literal (error, G_IO_ERROR,
1097 _("Can't rename root directory"));
1101 new_file = g_file_get_child_for_display_name (parent, display_name, error);
1102 g_object_unref (parent);
1104 if (new_file == NULL)
1106 local = G_LOCAL_FILE (file);
1107 new_local = G_LOCAL_FILE (new_file);
1109 if (g_lstat (new_local->filename, &statbuf) == -1)
1113 if (errsv != ENOENT)
1115 g_set_error (error, G_IO_ERROR,
1116 g_io_error_from_errno (errsv),
1117 _("Error renaming file: %s"),
1118 g_strerror (errsv));
1124 g_set_error_literal (error, G_IO_ERROR,
1126 _("Can't rename file, filename already exists"));
1130 if (g_rename (local->filename, new_local->filename) == -1)
1134 if (errsv == EINVAL)
1135 /* We can't get a rename file into itself error herer,
1136 so this must be an invalid filename, on e.g. FAT */
1137 g_set_error_literal (error, G_IO_ERROR,
1138 G_IO_ERROR_INVALID_FILENAME,
1139 _("Invalid filename"));
1141 g_set_error (error, G_IO_ERROR,
1142 g_io_error_from_errno (errsv),
1143 _("Error renaming file: %s"),
1144 g_strerror (errsv));
1145 g_object_unref (new_file);
1149 vfs = g_vfs_get_default ();
1150 class = G_VFS_GET_CLASS (vfs);
1151 if (class->local_file_moved)
1152 class->local_file_moved (vfs, local->filename, new_local->filename);
1158 g_local_file_query_info (GFile *file,
1159 const char *attributes,
1160 GFileQueryInfoFlags flags,
1161 GCancellable *cancellable,
1164 GLocalFile *local = G_LOCAL_FILE (file);
1166 GFileAttributeMatcher *matcher;
1167 char *basename, *dirname;
1168 GLocalParentFileInfo parent_info;
1170 matcher = g_file_attribute_matcher_new (attributes);
1172 basename = g_path_get_basename (local->filename);
1174 dirname = g_path_get_dirname (local->filename);
1175 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1178 info = _g_local_file_info_get (basename, local->filename,
1179 matcher, flags, &parent_info,
1183 _g_local_file_info_free_parent_info (&parent_info);
1186 g_file_attribute_matcher_unref (matcher);
1191 static GFileAttributeInfoList *
1192 g_local_file_query_settable_attributes (GFile *file,
1193 GCancellable *cancellable,
1196 return g_file_attribute_info_list_ref (local_writable_attributes);
1199 static GFileAttributeInfoList *
1200 g_local_file_query_writable_namespaces (GFile *file,
1201 GCancellable *cancellable,
1204 GFileAttributeInfoList *list;
1208 if (g_once_init_enter (&local_writable_namespaces))
1210 /* Writable namespaces: */
1212 list = g_file_attribute_info_list_new ();
1215 g_file_attribute_info_list_add (list,
1217 G_FILE_ATTRIBUTE_TYPE_STRING,
1218 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
1219 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1220 g_file_attribute_info_list_add (list,
1222 G_FILE_ATTRIBUTE_TYPE_STRING,
1223 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1226 vfs = g_vfs_get_default ();
1227 class = G_VFS_GET_CLASS (vfs);
1228 if (class->add_writable_namespaces)
1229 class->add_writable_namespaces (vfs, list);
1231 g_once_init_leave (&local_writable_namespaces, (gsize)list);
1233 list = (GFileAttributeInfoList *)local_writable_namespaces;
1235 return g_file_attribute_info_list_ref (list);
1239 g_local_file_set_attribute (GFile *file,
1240 const char *attribute,
1241 GFileAttributeType type,
1243 GFileQueryInfoFlags flags,
1244 GCancellable *cancellable,
1247 GLocalFile *local = G_LOCAL_FILE (file);
1249 return _g_local_file_info_set_attribute (local->filename,
1259 g_local_file_set_attributes_from_info (GFile *file,
1261 GFileQueryInfoFlags flags,
1262 GCancellable *cancellable,
1265 GLocalFile *local = G_LOCAL_FILE (file);
1266 int res, chained_res;
1267 GFileIface *default_iface;
1269 res = _g_local_file_info_set_attributes (local->filename,
1275 error = NULL; /* Don't write over error if further errors */
1277 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1279 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1281 return res && chained_res;
1284 static GFileInputStream *
1285 g_local_file_read (GFile *file,
1286 GCancellable *cancellable,
1289 GLocalFile *local = G_LOCAL_FILE (file);
1293 fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1298 g_set_error (error, G_IO_ERROR,
1299 g_io_error_from_errno (errsv),
1300 _("Error opening file: %s"),
1301 g_strerror (errsv));
1306 ret = _fstati64 (fd, &buf);
1308 ret = fstat (fd, &buf);
1311 if (ret == 0 && S_ISDIR (buf.st_mode))
1314 g_set_error_literal (error, G_IO_ERROR,
1315 G_IO_ERROR_IS_DIRECTORY,
1316 _("Can't open directory"));
1320 return _g_local_file_input_stream_new (fd);
1323 static GFileOutputStream *
1324 g_local_file_append_to (GFile *file,
1325 GFileCreateFlags flags,
1326 GCancellable *cancellable,
1329 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1330 flags, cancellable, error);
1333 static GFileOutputStream *
1334 g_local_file_create (GFile *file,
1335 GFileCreateFlags flags,
1336 GCancellable *cancellable,
1339 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1341 flags, cancellable, error);
1344 static GFileOutputStream *
1345 g_local_file_replace (GFile *file,
1347 gboolean make_backup,
1348 GFileCreateFlags flags,
1349 GCancellable *cancellable,
1352 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1354 etag, make_backup, flags,
1355 cancellable, error);
1358 static GFileIOStream *
1359 g_local_file_open_readwrite (GFile *file,
1360 GCancellable *cancellable,
1363 GFileOutputStream *output;
1366 output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
1368 cancellable, error);
1372 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1373 g_object_unref (output);
1377 static GFileIOStream *
1378 g_local_file_create_readwrite (GFile *file,
1379 GFileCreateFlags flags,
1380 GCancellable *cancellable,
1383 GFileOutputStream *output;
1386 output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1388 cancellable, error);
1392 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1393 g_object_unref (output);
1397 static GFileIOStream *
1398 g_local_file_replace_readwrite (GFile *file,
1400 gboolean make_backup,
1401 GFileCreateFlags flags,
1402 GCancellable *cancellable,
1405 GFileOutputStream *output;
1408 output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1410 etag, make_backup, flags,
1411 cancellable, error);
1415 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1416 g_object_unref (output);
1421 g_local_file_delete (GFile *file,
1422 GCancellable *cancellable,
1425 GLocalFile *local = G_LOCAL_FILE (file);
1429 if (g_remove (local->filename) == -1)
1433 /* Posix allows EEXIST too, but the more sane error
1434 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1436 if (errsv == EEXIST)
1439 g_set_error (error, G_IO_ERROR,
1440 g_io_error_from_errno (errsv),
1441 _("Error removing file: %s"),
1442 g_strerror (errsv));
1446 vfs = g_vfs_get_default ();
1447 class = G_VFS_GET_CLASS (vfs);
1448 if (class->local_file_removed)
1449 class->local_file_removed (vfs, local->filename);
1457 strip_trailing_slashes (const char *path)
1462 path_copy = g_strdup (path);
1463 len = strlen (path_copy);
1464 while (len > 1 && path_copy[len-1] == '/')
1465 path_copy[--len] = 0;
1471 expand_symlink (const char *link)
1473 char *resolved, *canonical, *parent, *link2;
1474 char symlink_value[4096];
1482 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1485 return g_strdup (link);
1486 symlink_value[res] = 0;
1489 if (g_path_is_absolute (symlink_value))
1490 return canonicalize_filename (symlink_value);
1493 link2 = strip_trailing_slashes (link);
1494 parent = g_path_get_dirname (link2);
1497 resolved = g_build_filename (parent, symlink_value, NULL);
1500 canonical = canonicalize_filename (resolved);
1509 get_parent (const char *path,
1513 GStatBuf parent_stat;
1517 path_copy = strip_trailing_slashes (path);
1519 parent = g_path_get_dirname (path_copy);
1520 if (strcmp (parent, ".") == 0 ||
1521 strcmp (parent, path_copy) == 0)
1531 if (g_lstat (parent, &parent_stat) != 0)
1537 if (S_ISLNK (parent_stat.st_mode))
1540 parent = expand_symlink (parent);
1545 if (num_recursions > 12)
1550 } while (S_ISLNK (parent_stat.st_mode));
1552 *parent_dev = parent_stat.st_dev;
1558 expand_all_symlinks (const char *path)
1560 char *parent, *parent_expanded;
1561 char *basename, *res;
1564 parent = get_parent (path, &parent_dev);
1567 parent_expanded = expand_all_symlinks (parent);
1569 basename = g_path_get_basename (path);
1570 res = g_build_filename (parent_expanded, basename, NULL);
1572 g_free (parent_expanded);
1575 res = g_strdup (path);
1581 find_mountpoint_for (const char *file,
1585 dev_t dir_dev, parent_dev;
1587 dir = g_strdup (file);
1592 parent = get_parent (dir, &parent_dev);
1596 if (parent_dev != dir_dev)
1608 find_topdir_for (const char *file)
1613 dir = get_parent (file, &dir_dev);
1617 return find_mountpoint_for (dir, dir_dev);
1621 get_unique_filename (const char *basename,
1627 return g_strdup (basename);
1629 dot = strchr (basename, '.');
1631 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1633 return g_strdup_printf ("%s.%d", basename, id);
1637 path_has_prefix (const char *path,
1645 prefix_len = strlen (prefix);
1647 if (strncmp (path, prefix, prefix_len) == 0 &&
1648 (prefix_len == 0 || /* empty prefix always matches */
1649 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1650 path[prefix_len] == 0 ||
1651 path[prefix_len] == '/'))
1658 try_make_relative (const char *path,
1661 char *path2, *base2;
1664 path2 = expand_all_symlinks (path);
1665 base2 = expand_all_symlinks (base);
1668 if (path_has_prefix (path2, base2))
1670 relative = path2 + strlen (base2);
1671 while (*relative == '/')
1673 relative = g_strdup (relative);
1681 /* Failed, use abs path */
1682 return g_strdup (path);
1686 escape_trash_name (char *name)
1689 const gchar hex[16] = "0123456789ABCDEF";
1691 str = g_string_new ("");
1699 if (g_ascii_isprint (c))
1700 g_string_append_c (str, c);
1703 g_string_append_c (str, '%');
1704 g_string_append_c (str, hex[((guchar)c) >> 4]);
1705 g_string_append_c (str, hex[((guchar)c) & 0xf]);
1709 return g_string_free (str, FALSE);
1713 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1715 static gsize home_dev_set = 0;
1716 static dev_t home_dev;
1717 char *topdir, *globaldir, *trashdir, *tmpname;
1720 GStatBuf global_stat, trash_stat;
1723 if (g_once_init_enter (&home_dev_set))
1727 g_stat (g_get_home_dir (), &home_stat);
1728 home_dev = home_stat.st_dev;
1729 g_once_init_leave (&home_dev_set, 1);
1732 /* Assume we can trash to the home */
1733 if (dir_dev == home_dev)
1736 topdir = find_mountpoint_for (dirname, dir_dev);
1740 globaldir = g_build_filename (topdir, ".Trash", NULL);
1741 if (g_lstat (globaldir, &global_stat) == 0 &&
1742 S_ISDIR (global_stat.st_mode) &&
1743 (global_stat.st_mode & S_ISVTX) != 0)
1745 /* got a toplevel sysadmin created dir, assume we
1746 * can trash to it (we should be able to create a dir)
1747 * This fails for the FAT case where the ownership of
1748 * that dir would be wrong though..
1756 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1758 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1760 tmpname = g_strdup_printf (".Trash-%s", uid_str);
1761 trashdir = g_build_filename (topdir, tmpname, NULL);
1764 if (g_lstat (trashdir, &trash_stat) == 0)
1768 return S_ISDIR (trash_stat.st_mode) &&
1769 trash_stat.st_uid == uid;
1773 /* User specific trash didn't exist, can we create it? */
1774 res = g_access (topdir, W_OK) == 0;
1782 g_local_file_trash (GFile *file,
1783 GCancellable *cancellable,
1786 GLocalFile *local = G_LOCAL_FILE (file);
1787 GStatBuf file_stat, home_stat;
1788 const char *homedir;
1789 char *trashdir, *topdir, *infodir, *filesdir;
1790 char *basename, *trashname, *trashfile, *infoname, *infofile;
1791 char *original_name, *original_name_escaped;
1794 gboolean is_homedir_trash;
1795 char delete_time[32];
1797 GStatBuf trash_stat, global_stat;
1798 char *dirname, *globaldir;
1802 if (g_lstat (local->filename, &file_stat) != 0)
1806 g_set_error (error, G_IO_ERROR,
1807 g_io_error_from_errno (errsv),
1808 _("Error trashing file: %s"),
1809 g_strerror (errsv));
1813 homedir = g_get_home_dir ();
1814 g_stat (homedir, &home_stat);
1816 is_homedir_trash = FALSE;
1818 if (file_stat.st_dev == home_stat.st_dev)
1820 is_homedir_trash = TRUE;
1822 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1823 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1828 display_name = g_filename_display_name (trashdir);
1829 g_set_error (error, G_IO_ERROR,
1830 g_io_error_from_errno (errsv),
1831 _("Unable to create trash dir %s: %s"),
1832 display_name, g_strerror (errsv));
1833 g_free (display_name);
1837 topdir = g_strdup (g_get_user_data_dir ());
1845 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1847 topdir = find_topdir_for (local->filename);
1850 g_set_error_literal (error, G_IO_ERROR,
1851 G_IO_ERROR_NOT_SUPPORTED,
1852 _("Unable to find toplevel directory for trash"));
1856 /* Try looking for global trash dir $topdir/.Trash/$uid */
1857 globaldir = g_build_filename (topdir, ".Trash", NULL);
1858 if (g_lstat (globaldir, &global_stat) == 0 &&
1859 S_ISDIR (global_stat.st_mode) &&
1860 (global_stat.st_mode & S_ISVTX) != 0)
1862 trashdir = g_build_filename (globaldir, uid_str, NULL);
1864 if (g_lstat (trashdir, &trash_stat) == 0)
1866 if (!S_ISDIR (trash_stat.st_mode) ||
1867 trash_stat.st_uid != uid)
1869 /* Not a directory or not owned by user, ignore */
1874 else if (g_mkdir (trashdir, 0700) == -1)
1882 if (trashdir == NULL)
1884 gboolean tried_create;
1886 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1887 dirname = g_strdup_printf (".Trash-%s", uid_str);
1888 trashdir = g_build_filename (topdir, dirname, NULL);
1891 tried_create = FALSE;
1894 if (g_lstat (trashdir, &trash_stat) == 0)
1896 if (!S_ISDIR (trash_stat.st_mode) ||
1897 trash_stat.st_uid != uid)
1899 /* Remove the failed directory */
1901 g_remove (trashdir);
1903 /* Not a directory or not owned by user, ignore */
1910 if (!tried_create &&
1911 g_mkdir (trashdir, 0700) != -1)
1913 /* Ensure that the created dir has the right uid etc.
1914 This might fail on e.g. a FAT dir */
1915 tried_create = TRUE;
1926 if (trashdir == NULL)
1929 g_set_error_literal (error, G_IO_ERROR,
1930 G_IO_ERROR_NOT_SUPPORTED,
1931 _("Unable to find or create trash directory"));
1936 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1938 infodir = g_build_filename (trashdir, "info", NULL);
1939 filesdir = g_build_filename (trashdir, "files", NULL);
1942 /* Make sure we have the subdirectories */
1943 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1944 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
1949 g_set_error_literal (error, G_IO_ERROR,
1950 G_IO_ERROR_NOT_SUPPORTED,
1951 _("Unable to find or create trash directory"));
1955 basename = g_path_get_basename (local->filename);
1963 trashname = get_unique_filename (basename, i++);
1964 infoname = g_strconcat (trashname, ".trashinfo", NULL);
1965 infofile = g_build_filename (infodir, infoname, NULL);
1968 fd = open (infofile, O_CREAT | O_EXCL, 0666);
1969 } while (fd == -1 && errno == EEXIST);
1983 g_set_error (error, G_IO_ERROR,
1984 g_io_error_from_errno (errsv),
1985 _("Unable to create trashing info file: %s"),
1986 g_strerror (errsv));
1992 /* TODO: Maybe we should verify that you can delete the file from the trash
1993 before moving it? OTOH, that is hard, as it needs a recursive scan */
1995 trashfile = g_build_filename (filesdir, trashname, NULL);
1999 if (g_rename (local->filename, trashfile) == -1)
2009 /* The trash dir was actually on another fs anyway!?
2010 This can happen when the same device is mounted multiple
2011 times, or with bind mounts of the same fs. */
2012 g_set_error (error, G_IO_ERROR,
2013 G_IO_ERROR_NOT_SUPPORTED,
2014 _("Unable to trash file: %s"),
2015 g_strerror (errsv));
2017 g_set_error (error, G_IO_ERROR,
2018 g_io_error_from_errno (errsv),
2019 _("Unable to trash file: %s"),
2020 g_strerror (errsv));
2024 vfs = g_vfs_get_default ();
2025 class = G_VFS_GET_CLASS (vfs);
2026 if (class->local_file_moved)
2027 class->local_file_moved (vfs, local->filename, trashfile);
2031 /* TODO: Do we need to update mtime/atime here after the move? */
2033 /* Use absolute names for homedir */
2034 if (is_homedir_trash)
2035 original_name = g_strdup (local->filename);
2037 original_name = try_make_relative (local->filename, topdir);
2038 original_name_escaped = escape_trash_name (original_name);
2040 g_free (original_name);
2047 localtime_r (&t, &now);
2049 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
2052 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2053 original_name_escaped, delete_time);
2055 g_file_set_contents (infofile, data, -1, NULL);
2059 g_free (original_name_escaped);
2064 #else /* G_OS_WIN32 */
2066 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
2068 return FALSE; /* XXX ??? */
2072 g_local_file_trash (GFile *file,
2073 GCancellable *cancellable,
2076 GLocalFile *local = G_LOCAL_FILE (file);
2077 SHFILEOPSTRUCTW op = {0};
2082 wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
2083 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2084 wfilename = g_renew (wchar_t, wfilename, len + 2);
2085 wfilename[len + 1] = 0;
2087 op.wFunc = FO_DELETE;
2088 op.pFrom = wfilename;
2089 op.fFlags = FOF_ALLOWUNDO;
2091 success = SHFileOperationW (&op) == 0;
2093 if (success && op.fAnyOperationsAborted)
2095 if (cancellable && !g_cancellable_is_cancelled (cancellable))
2096 g_cancellable_cancel (cancellable);
2097 g_set_error (error, G_IO_ERROR,
2098 G_IO_ERROR_CANCELLED,
2099 _("Unable to trash file: %s"),
2100 _("Operation was cancelled"));
2104 g_set_error (error, G_IO_ERROR,
2106 _("Unable to trash file: %s"),
2107 _("internal error"));
2112 #endif /* G_OS_WIN32 */
2115 g_local_file_make_directory (GFile *file,
2116 GCancellable *cancellable,
2119 GLocalFile *local = G_LOCAL_FILE (file);
2121 if (g_mkdir (local->filename, 0777) == -1)
2125 if (errsv == EINVAL)
2126 /* This must be an invalid filename, on e.g. FAT */
2127 g_set_error_literal (error, G_IO_ERROR,
2128 G_IO_ERROR_INVALID_FILENAME,
2129 _("Invalid filename"));
2131 g_set_error (error, G_IO_ERROR,
2132 g_io_error_from_errno (errsv),
2133 _("Error creating directory: %s"),
2134 g_strerror (errsv));
2142 g_local_file_make_symbolic_link (GFile *file,
2143 const char *symlink_value,
2144 GCancellable *cancellable,
2148 GLocalFile *local = G_LOCAL_FILE (file);
2150 if (symlink (symlink_value, local->filename) == -1)
2154 if (errsv == EINVAL)
2155 /* This must be an invalid filename, on e.g. FAT */
2156 g_set_error_literal (error, G_IO_ERROR,
2157 G_IO_ERROR_INVALID_FILENAME,
2158 _("Invalid filename"));
2159 else if (errsv == EPERM)
2160 g_set_error (error, G_IO_ERROR,
2161 G_IO_ERROR_NOT_SUPPORTED,
2162 _("Filesystem does not support symbolic links"));
2164 g_set_error (error, G_IO_ERROR,
2165 g_io_error_from_errno (errsv),
2166 _("Error making symbolic link: %s"),
2167 g_strerror (errsv));
2172 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
2179 g_local_file_copy (GFile *source,
2181 GFileCopyFlags flags,
2182 GCancellable *cancellable,
2183 GFileProgressCallback progress_callback,
2184 gpointer progress_callback_data,
2187 /* Fall back to default copy */
2188 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
2193 g_local_file_move (GFile *source,
2195 GFileCopyFlags flags,
2196 GCancellable *cancellable,
2197 GFileProgressCallback progress_callback,
2198 gpointer progress_callback_data,
2201 GLocalFile *local_source, *local_destination;
2203 gboolean destination_exist, source_is_dir;
2210 if (!G_IS_LOCAL_FILE (source) ||
2211 !G_IS_LOCAL_FILE (destination))
2213 /* Fall back to default move */
2214 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2218 local_source = G_LOCAL_FILE (source);
2219 local_destination = G_LOCAL_FILE (destination);
2221 res = g_lstat (local_source->filename, &statbuf);
2226 g_set_error (error, G_IO_ERROR,
2227 g_io_error_from_errno (errsv),
2228 _("Error moving file: %s"),
2229 g_strerror (errsv));
2233 source_is_dir = S_ISDIR (statbuf.st_mode);
2234 source_size = statbuf.st_size;
2236 destination_exist = FALSE;
2237 res = g_lstat (local_destination->filename, &statbuf);
2240 destination_exist = TRUE; /* Target file exists */
2242 if (flags & G_FILE_COPY_OVERWRITE)
2244 /* Always fail on dirs, even with overwrite */
2245 if (S_ISDIR (statbuf.st_mode))
2248 g_set_error_literal (error,
2250 G_IO_ERROR_WOULD_MERGE,
2251 _("Can't move directory over directory"));
2253 g_set_error_literal (error,
2255 G_IO_ERROR_IS_DIRECTORY,
2256 _("Can't copy over directory"));
2262 g_set_error_literal (error,
2265 _("Target file exists"));
2270 if (flags & G_FILE_COPY_BACKUP && destination_exist)
2272 backup_name = g_strconcat (local_destination->filename, "~", NULL);
2273 if (g_rename (local_destination->filename, backup_name) == -1)
2275 g_set_error_literal (error,
2277 G_IO_ERROR_CANT_CREATE_BACKUP,
2278 _("Backup file creation failed"));
2279 g_free (backup_name);
2282 g_free (backup_name);
2283 destination_exist = FALSE; /* It did, but no more */
2286 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2288 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2289 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2290 res = g_unlink (local_destination->filename);
2295 g_set_error (error, G_IO_ERROR,
2296 g_io_error_from_errno (errsv),
2297 _("Error removing target file: %s"),
2298 g_strerror (errsv));
2303 if (g_rename (local_source->filename, local_destination->filename) == -1)
2308 /* This will cause the fallback code to run */
2309 g_set_error_literal (error, G_IO_ERROR,
2310 G_IO_ERROR_NOT_SUPPORTED,
2311 _("Move between mounts not supported"));
2312 else if (errsv == EINVAL)
2313 /* This must be an invalid filename, on e.g. FAT, or
2314 we're trying to move the file into itself...
2315 We return invalid filename for both... */
2316 g_set_error_literal (error, G_IO_ERROR,
2317 G_IO_ERROR_INVALID_FILENAME,
2318 _("Invalid filename"));
2320 g_set_error (error, G_IO_ERROR,
2321 g_io_error_from_errno (errsv),
2322 _("Error moving file: %s"),
2323 g_strerror (errsv));
2327 vfs = g_vfs_get_default ();
2328 class = G_VFS_GET_CLASS (vfs);
2329 if (class->local_file_moved)
2330 class->local_file_moved (vfs, local_source->filename, local_destination->filename);
2332 /* Make sure we send full copied size */
2333 if (progress_callback)
2334 progress_callback (source_size, source_size, progress_callback_data);
2339 static GFileMonitor*
2340 g_local_file_monitor_dir (GFile *file,
2341 GFileMonitorFlags flags,
2342 GCancellable *cancellable,
2345 GLocalFile* local_file = G_LOCAL_FILE(file);
2346 return _g_local_directory_monitor_new (local_file->filename, flags, error);
2349 static GFileMonitor*
2350 g_local_file_monitor_file (GFile *file,
2351 GFileMonitorFlags flags,
2352 GCancellable *cancellable,
2355 GLocalFile* local_file = G_LOCAL_FILE(file);
2356 return _g_local_file_monitor_new (local_file->filename, flags, error);
2360 g_local_file_file_iface_init (GFileIface *iface)
2362 iface->dup = g_local_file_dup;
2363 iface->hash = g_local_file_hash;
2364 iface->equal = g_local_file_equal;
2365 iface->is_native = g_local_file_is_native;
2366 iface->has_uri_scheme = g_local_file_has_uri_scheme;
2367 iface->get_uri_scheme = g_local_file_get_uri_scheme;
2368 iface->get_basename = g_local_file_get_basename;
2369 iface->get_path = g_local_file_get_path;
2370 iface->get_uri = g_local_file_get_uri;
2371 iface->get_parse_name = g_local_file_get_parse_name;
2372 iface->get_parent = g_local_file_get_parent;
2373 iface->prefix_matches = g_local_file_prefix_matches;
2374 iface->get_relative_path = g_local_file_get_relative_path;
2375 iface->resolve_relative_path = g_local_file_resolve_relative_path;
2376 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2377 iface->set_display_name = g_local_file_set_display_name;
2378 iface->enumerate_children = g_local_file_enumerate_children;
2379 iface->query_info = g_local_file_query_info;
2380 iface->query_filesystem_info = g_local_file_query_filesystem_info;
2381 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2382 iface->query_settable_attributes = g_local_file_query_settable_attributes;
2383 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2384 iface->set_attribute = g_local_file_set_attribute;
2385 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2386 iface->read_fn = g_local_file_read;
2387 iface->append_to = g_local_file_append_to;
2388 iface->create = g_local_file_create;
2389 iface->replace = g_local_file_replace;
2390 iface->open_readwrite = g_local_file_open_readwrite;
2391 iface->create_readwrite = g_local_file_create_readwrite;
2392 iface->replace_readwrite = g_local_file_replace_readwrite;
2393 iface->delete_file = g_local_file_delete;
2394 iface->trash = g_local_file_trash;
2395 iface->make_directory = g_local_file_make_directory;
2396 iface->make_symbolic_link = g_local_file_make_symbolic_link;
2397 iface->copy = g_local_file_copy;
2398 iface->move = g_local_file_move;
2399 iface->monitor_dir = g_local_file_monitor_dir;
2400 iface->monitor_file = g_local_file_monitor_file;
2402 iface->supports_thread_contexts = TRUE;