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)
109 static void g_local_file_file_iface_init (GFileIface *iface);
111 static GFileAttributeInfoList *local_writable_attributes = NULL;
112 static /* GFileAttributeInfoList * */ gsize local_writable_namespaces = 0;
116 GObject parent_instance;
121 #define g_local_file_get_type _g_local_file_get_type
122 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
123 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
124 g_local_file_file_iface_init))
126 static char *find_mountpoint_for (const char *file, dev_t dev);
129 g_local_file_finalize (GObject *object)
133 local = G_LOCAL_FILE (object);
135 g_free (local->filename);
137 G_OBJECT_CLASS (g_local_file_parent_class)->finalize (object);
141 g_local_file_class_init (GLocalFileClass *klass)
143 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
144 GFileAttributeInfoList *list;
146 gobject_class->finalize = g_local_file_finalize;
148 /* Set up attribute lists */
150 /* Writable attributes: */
152 list = g_file_attribute_info_list_new ();
154 g_file_attribute_info_list_add (list,
155 G_FILE_ATTRIBUTE_UNIX_MODE,
156 G_FILE_ATTRIBUTE_TYPE_UINT32,
157 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
158 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
161 g_file_attribute_info_list_add (list,
162 G_FILE_ATTRIBUTE_UNIX_UID,
163 G_FILE_ATTRIBUTE_TYPE_UINT32,
164 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
165 g_file_attribute_info_list_add (list,
166 G_FILE_ATTRIBUTE_UNIX_GID,
167 G_FILE_ATTRIBUTE_TYPE_UINT32,
168 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
172 g_file_attribute_info_list_add (list,
173 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
174 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
179 g_file_attribute_info_list_add (list,
180 G_FILE_ATTRIBUTE_TIME_MODIFIED,
181 G_FILE_ATTRIBUTE_TYPE_UINT64,
182 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
183 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
184 g_file_attribute_info_list_add (list,
185 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
186 G_FILE_ATTRIBUTE_TYPE_UINT32,
187 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
188 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
189 /* When copying, the target file is accessed. Replicating
190 * the source access time does not make sense in this case.
192 g_file_attribute_info_list_add (list,
193 G_FILE_ATTRIBUTE_TIME_ACCESS,
194 G_FILE_ATTRIBUTE_TYPE_UINT64,
195 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
196 g_file_attribute_info_list_add (list,
197 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
198 G_FILE_ATTRIBUTE_TYPE_UINT32,
199 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
202 local_writable_attributes = list;
206 g_local_file_init (GLocalFile *local)
212 canonicalize_filename (const char *filename)
214 char *canon, *start, *p, *q;
218 if (!g_path_is_absolute (filename))
220 cwd = g_get_current_dir ();
221 canon = g_build_filename (cwd, filename, NULL);
225 canon = g_strdup (filename);
227 start = (char *)g_path_skip_root (canon);
231 /* This shouldn't really happen, as g_get_current_dir() should
232 return an absolute pathname, but bug 573843 shows this is
233 not always happening */
235 return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
238 /* POSIX allows double slashes at the start to
239 * mean something special (as does windows too).
240 * So, "//" != "/", but more than two slashes
246 G_IS_DIR_SEPARATOR (*p);
253 memmove (start, start+i, strlen (start+i)+1);
259 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
261 memmove (p, p+1, strlen (p+1)+1);
263 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
266 /* Skip previous separator */
270 while (p > start && !G_IS_DIR_SEPARATOR (*p))
272 if (G_IS_DIR_SEPARATOR (*p))
273 *p++ = G_DIR_SEPARATOR;
274 memmove (p, q, strlen (q)+1);
278 /* Skip until next separator */
279 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
284 /* Canonicalize one separator */
285 *p++ = G_DIR_SEPARATOR;
289 /* Remove additional separators */
291 while (*q && G_IS_DIR_SEPARATOR (*q))
295 memmove (p, q, strlen (q)+1);
298 /* Remove trailing slashes */
299 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
306 _g_local_file_new (const char *filename)
310 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
311 local->filename = canonicalize_filename (filename);
313 return G_FILE (local);
317 g_local_file_is_native (GFile *file)
323 g_local_file_has_uri_scheme (GFile *file,
324 const char *uri_scheme)
326 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
330 g_local_file_get_uri_scheme (GFile *file)
332 return g_strdup ("file");
336 g_local_file_get_basename (GFile *file)
338 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
342 g_local_file_get_path (GFile *file)
344 return g_strdup (G_LOCAL_FILE (file)->filename);
348 g_local_file_get_uri (GFile *file)
350 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
354 get_filename_charset (const gchar **filename_charset)
356 const gchar **charsets;
359 is_utf8 = g_get_filename_charsets (&charsets);
361 if (filename_charset)
362 *filename_charset = charsets[0];
368 name_is_valid_for_display (const char *string,
369 gboolean is_valid_utf8)
373 if (!is_valid_utf8 &&
374 !g_utf8_validate (string, -1, NULL))
377 while ((c = *string++) != 0)
379 if (g_ascii_iscntrl (c))
387 g_local_file_get_parse_name (GFile *file)
389 const char *filename;
391 const gchar *charset;
393 char *roundtripped_filename;
394 gboolean free_utf8_filename;
395 gboolean is_valid_utf8;
398 filename = G_LOCAL_FILE (file)->filename;
399 if (get_filename_charset (&charset))
401 utf8_filename = (char *)filename;
402 free_utf8_filename = FALSE;
403 is_valid_utf8 = FALSE; /* Can't guarantee this */
407 utf8_filename = g_convert (filename, -1,
408 "UTF-8", charset, NULL, NULL, NULL);
409 free_utf8_filename = TRUE;
410 is_valid_utf8 = TRUE;
412 if (utf8_filename != NULL)
414 /* Make sure we can roundtrip: */
415 roundtripped_filename = g_convert (utf8_filename, -1,
416 charset, "UTF-8", NULL, NULL, NULL);
418 if (roundtripped_filename == NULL ||
419 strcmp (filename, roundtripped_filename) != 0)
421 g_free (utf8_filename);
422 utf8_filename = NULL;
425 g_free (roundtripped_filename);
429 if (utf8_filename != NULL &&
430 name_is_valid_for_display (utf8_filename, is_valid_utf8))
432 if (free_utf8_filename)
433 parse_name = utf8_filename;
435 parse_name = g_strdup (utf8_filename);
439 escaped_path = g_uri_escape_string (filename,
440 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
442 parse_name = g_strconcat ("file://",
443 (*escaped_path != '/') ? "/" : "",
447 g_free (escaped_path);
449 if (free_utf8_filename)
450 g_free (utf8_filename);
457 g_local_file_get_parent (GFile *file)
459 GLocalFile *local = G_LOCAL_FILE (file);
460 const char *non_root;
465 non_root = g_path_skip_root (local->filename);
469 dirname = g_path_get_dirname (local->filename);
470 parent = _g_local_file_new (dirname);
476 g_local_file_dup (GFile *file)
478 GLocalFile *local = G_LOCAL_FILE (file);
480 return _g_local_file_new (local->filename);
484 g_local_file_hash (GFile *file)
486 GLocalFile *local = G_LOCAL_FILE (file);
488 return g_str_hash (local->filename);
492 g_local_file_equal (GFile *file1,
495 GLocalFile *local1 = G_LOCAL_FILE (file1);
496 GLocalFile *local2 = G_LOCAL_FILE (file2);
498 return g_str_equal (local1->filename, local2->filename);
502 match_prefix (const char *path,
507 prefix_len = strlen (prefix);
508 if (strncmp (path, prefix, prefix_len) != 0)
511 /* Handle the case where prefix is the root, so that
512 * the IS_DIR_SEPRARATOR check below works */
513 if (prefix_len > 0 &&
514 G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
517 return path + prefix_len;
521 g_local_file_prefix_matches (GFile *parent,
524 GLocalFile *parent_local = G_LOCAL_FILE (parent);
525 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
526 const char *remainder;
528 remainder = match_prefix (descendant_local->filename, parent_local->filename);
529 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
535 g_local_file_get_relative_path (GFile *parent,
538 GLocalFile *parent_local = G_LOCAL_FILE (parent);
539 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
540 const char *remainder;
542 remainder = match_prefix (descendant_local->filename, parent_local->filename);
544 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
545 return g_strdup (remainder + 1);
550 g_local_file_resolve_relative_path (GFile *file,
551 const char *relative_path)
553 GLocalFile *local = G_LOCAL_FILE (file);
557 if (g_path_is_absolute (relative_path))
558 return _g_local_file_new (relative_path);
560 filename = g_build_filename (local->filename, relative_path, NULL);
561 child = _g_local_file_new (filename);
567 static GFileEnumerator *
568 g_local_file_enumerate_children (GFile *file,
569 const char *attributes,
570 GFileQueryInfoFlags flags,
571 GCancellable *cancellable,
574 GLocalFile *local = G_LOCAL_FILE (file);
575 return _g_local_file_enumerator_new (local,
581 g_local_file_get_child_for_display_name (GFile *file,
582 const char *display_name,
588 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
589 if (basename == NULL)
591 g_set_error (error, G_IO_ERROR,
592 G_IO_ERROR_INVALID_FILENAME,
593 _("Invalid filename %s"), display_name);
597 new_file = g_file_get_child (file, basename);
605 get_fs_type (long f_type)
607 /* filesystem ids taken from linux manpage */
714 G_LOCK_DEFINE_STATIC(mount_info_hash);
715 static GHashTable *mount_info_hash = NULL;
716 static guint64 mount_info_hash_cache_time = 0;
719 MOUNT_INFO_READONLY = 1<<0
723 device_equal (gconstpointer v1,
726 return *(dev_t *)v1 == *(dev_t *)v2;
730 device_hash (gconstpointer v)
732 return (guint) *(dev_t *)v;
736 get_mount_info (GFileInfo *fs_info,
738 GFileAttributeMatcher *matcher)
742 gpointer info_as_ptr;
746 GUnixMountEntry *mount;
749 if (g_lstat (path, &buf) != 0)
752 G_LOCK (mount_info_hash);
754 if (mount_info_hash == NULL)
755 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
759 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
760 g_hash_table_remove_all (mount_info_hash);
762 got_info = g_hash_table_lookup_extended (mount_info_hash,
767 G_UNLOCK (mount_info_hash);
769 mount_info = GPOINTER_TO_UINT (info_as_ptr);
775 mountpoint = find_mountpoint_for (path, buf.st_dev);
776 if (mountpoint == NULL)
777 mountpoint = g_strdup ("/");
779 mount = g_unix_mount_at (mountpoint, &cache_time);
782 if (g_unix_mount_is_readonly (mount))
783 mount_info |= MOUNT_INFO_READONLY;
785 g_unix_mount_free (mount);
790 dev = g_new0 (dev_t, 1);
793 G_LOCK (mount_info_hash);
794 mount_info_hash_cache_time = cache_time;
795 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
796 G_UNLOCK (mount_info_hash);
799 if (mount_info & MOUNT_INFO_READONLY)
800 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
808 is_xp_or_later (void)
810 static int result = -1;
815 OSVERSIONINFOEX ver_info = {0};
816 DWORDLONG cond_mask = 0;
817 int op = VER_GREATER_EQUAL;
819 ver_info.dwOSVersionInfoSize = sizeof ver_info;
820 ver_info.dwMajorVersion = 5;
821 ver_info.dwMinorVersion = 1;
823 VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
824 VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
826 result = VerifyVersionInfo (&ver_info,
827 VER_MAJORVERSION | VER_MINORVERSION,
830 result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;
838 get_volume_for_path (const char *path)
844 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
845 result = g_new (wchar_t, MAX_PATH);
847 if (!GetVolumePathNameW (wpath, result, MAX_PATH))
849 char *msg = g_win32_error_message (GetLastError ());
850 g_critical ("GetVolumePathName failed: %s", msg);
857 len = wcslen (result);
858 if (len > 0 && result[len-1] != L'\\')
860 result = g_renew (wchar_t, result, len + 2);
870 find_mountpoint_for (const char *file, dev_t dev)
875 wpath = get_volume_for_path (file);
879 utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
886 get_filesystem_readonly (GFileInfo *info,
891 rootdir = get_volume_for_path (path);
895 if (is_xp_or_later ())
898 if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
899 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
900 (flags & FILE_READ_ONLY_VOLUME) != 0);
904 if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
905 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
912 #endif /* G_OS_WIN32 */
915 g_local_file_query_filesystem_info (GFile *file,
916 const char *attributes,
917 GCancellable *cancellable,
920 GLocalFile *local = G_LOCAL_FILE (file);
922 int statfs_result = 0;
928 struct statfs statfs_buffer;
929 #elif defined(USE_STATVFS)
930 struct statvfs statfs_buffer;
933 GFileAttributeMatcher *attribute_matcher;
940 statfs_result = statfs (local->filename, &statfs_buffer);
941 #elif STATFS_ARGS == 4
942 statfs_result = statfs (local->filename, &statfs_buffer,
943 sizeof (statfs_buffer), 0);
945 block_size = statfs_buffer.f_bsize;
947 /* Many backends can't report free size (for instance the gvfs fuse
948 backend for backend not supporting this), and set f_bfree to 0,
949 but it can be 0 for real too. We treat the availible == 0 and
950 free == 0 case as "both of these are invalid".
953 if (statfs_result == 0 &&
954 statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0)
958 #elif defined(USE_STATVFS)
959 statfs_result = statvfs (local->filename, &statfs_buffer);
960 block_size = statfs_buffer.f_frsize;
963 if (statfs_result == -1)
967 g_set_error (error, G_IO_ERROR,
968 g_io_error_from_errno (errsv),
969 _("Error getting filesystem info: %s"),
974 info = g_file_info_new ();
976 attribute_matcher = g_file_attribute_matcher_new (attributes);
979 g_file_attribute_matcher_matches (attribute_matcher,
980 G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
983 gchar *localdir = g_path_get_dirname (local->filename);
984 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
988 if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
989 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
992 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
996 g_file_attribute_matcher_matches (attribute_matcher,
997 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
1000 gchar *localdir = g_path_get_dirname (local->filename);
1001 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1005 if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
1006 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, (guint64)li.QuadPart);
1009 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
1013 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1014 fstype = g_strdup(statfs_buffer.f_fstypename);
1016 fstype = get_fs_type (statfs_buffer.f_type);
1019 #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1020 fstype = g_strdup(statfs_buffer.f_basetype);
1025 g_file_attribute_matcher_matches (attribute_matcher,
1026 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1027 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1030 if (g_file_attribute_matcher_matches (attribute_matcher,
1031 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
1034 get_filesystem_readonly (info, local->filename);
1036 get_mount_info (info, local->filename, attribute_matcher);
1040 g_file_attribute_matcher_unref (attribute_matcher);
1046 g_local_file_find_enclosing_mount (GFile *file,
1047 GCancellable *cancellable,
1050 GLocalFile *local = G_LOCAL_FILE (file);
1055 if (g_lstat (local->filename, &buf) != 0)
1057 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1058 /* Translators: This is an error message when trying to
1059 * find the enclosing (user visible) mount of a file, but
1061 _("Containing mount does not exist"));
1065 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
1066 if (mountpoint == NULL)
1068 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1069 /* Translators: This is an error message when trying to
1070 * find the enclosing (user visible) mount of a file, but
1072 _("Containing mount does not exist"));
1076 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1077 g_free (mountpoint);
1081 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1082 /* Translators: This is an error message when trying to find
1083 * the enclosing (user visible) mount of a file, but none
1085 _("Containing mount does not exist"));
1090 g_local_file_set_display_name (GFile *file,
1091 const char *display_name,
1092 GCancellable *cancellable,
1095 GLocalFile *local, *new_local;
1096 GFile *new_file, *parent;
1102 parent = g_file_get_parent (file);
1105 g_set_error_literal (error, G_IO_ERROR,
1107 _("Can't rename root directory"));
1111 new_file = g_file_get_child_for_display_name (parent, display_name, error);
1112 g_object_unref (parent);
1114 if (new_file == NULL)
1116 local = G_LOCAL_FILE (file);
1117 new_local = G_LOCAL_FILE (new_file);
1119 if (g_lstat (new_local->filename, &statbuf) == -1)
1123 if (errsv != ENOENT)
1125 g_set_error (error, G_IO_ERROR,
1126 g_io_error_from_errno (errsv),
1127 _("Error renaming file: %s"),
1128 g_strerror (errsv));
1134 g_set_error_literal (error, G_IO_ERROR,
1136 _("Can't rename file, filename already exist"));
1140 if (g_rename (local->filename, new_local->filename) == -1)
1144 if (errsv == EINVAL)
1145 /* We can't get a rename file into itself error herer,
1146 so this must be an invalid filename, on e.g. FAT */
1147 g_set_error_literal (error, G_IO_ERROR,
1148 G_IO_ERROR_INVALID_FILENAME,
1149 _("Invalid filename"));
1151 g_set_error (error, G_IO_ERROR,
1152 g_io_error_from_errno (errsv),
1153 _("Error renaming file: %s"),
1154 g_strerror (errsv));
1155 g_object_unref (new_file);
1159 vfs = g_vfs_get_default ();
1160 class = G_VFS_GET_CLASS (vfs);
1161 if (class->local_file_moved)
1162 class->local_file_moved (vfs, local->filename, new_local->filename);
1168 g_local_file_query_info (GFile *file,
1169 const char *attributes,
1170 GFileQueryInfoFlags flags,
1171 GCancellable *cancellable,
1174 GLocalFile *local = G_LOCAL_FILE (file);
1176 GFileAttributeMatcher *matcher;
1177 char *basename, *dirname;
1178 GLocalParentFileInfo parent_info;
1180 matcher = g_file_attribute_matcher_new (attributes);
1182 basename = g_path_get_basename (local->filename);
1184 dirname = g_path_get_dirname (local->filename);
1185 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1188 info = _g_local_file_info_get (basename, local->filename,
1189 matcher, flags, &parent_info,
1193 _g_local_file_info_free_parent_info (&parent_info);
1196 g_file_attribute_matcher_unref (matcher);
1201 static GFileAttributeInfoList *
1202 g_local_file_query_settable_attributes (GFile *file,
1203 GCancellable *cancellable,
1206 return g_file_attribute_info_list_ref (local_writable_attributes);
1209 static GFileAttributeInfoList *
1210 g_local_file_query_writable_namespaces (GFile *file,
1211 GCancellable *cancellable,
1214 GFileAttributeInfoList *list;
1218 if (g_once_init_enter (&local_writable_namespaces))
1220 /* Writable namespaces: */
1222 list = g_file_attribute_info_list_new ();
1225 g_file_attribute_info_list_add (list,
1227 G_FILE_ATTRIBUTE_TYPE_STRING,
1228 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
1229 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1230 g_file_attribute_info_list_add (list,
1232 G_FILE_ATTRIBUTE_TYPE_STRING,
1233 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1236 vfs = g_vfs_get_default ();
1237 class = G_VFS_GET_CLASS (vfs);
1238 if (class->add_writable_namespaces)
1239 class->add_writable_namespaces (vfs, list);
1241 g_once_init_leave (&local_writable_namespaces, (gsize)list);
1243 list = (GFileAttributeInfoList *)local_writable_namespaces;
1245 return g_file_attribute_info_list_ref (list);
1249 g_local_file_set_attribute (GFile *file,
1250 const char *attribute,
1251 GFileAttributeType type,
1253 GFileQueryInfoFlags flags,
1254 GCancellable *cancellable,
1257 GLocalFile *local = G_LOCAL_FILE (file);
1259 return _g_local_file_info_set_attribute (local->filename,
1269 g_local_file_set_attributes_from_info (GFile *file,
1271 GFileQueryInfoFlags flags,
1272 GCancellable *cancellable,
1275 GLocalFile *local = G_LOCAL_FILE (file);
1276 int res, chained_res;
1277 GFileIface *default_iface;
1279 res = _g_local_file_info_set_attributes (local->filename,
1285 error = NULL; /* Don't write over error if further errors */
1287 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1289 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1291 return res && chained_res;
1294 static GFileInputStream *
1295 g_local_file_read (GFile *file,
1296 GCancellable *cancellable,
1299 GLocalFile *local = G_LOCAL_FILE (file);
1303 fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1308 g_set_error (error, G_IO_ERROR,
1309 g_io_error_from_errno (errsv),
1310 _("Error opening file: %s"),
1311 g_strerror (errsv));
1315 if (fstat(fd, &buf) == 0 && S_ISDIR (buf.st_mode))
1318 g_set_error_literal (error, G_IO_ERROR,
1319 G_IO_ERROR_IS_DIRECTORY,
1320 _("Can't open directory"));
1324 return _g_local_file_input_stream_new (fd);
1327 static GFileOutputStream *
1328 g_local_file_append_to (GFile *file,
1329 GFileCreateFlags flags,
1330 GCancellable *cancellable,
1333 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1334 flags, cancellable, error);
1337 static GFileOutputStream *
1338 g_local_file_create (GFile *file,
1339 GFileCreateFlags flags,
1340 GCancellable *cancellable,
1343 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1345 flags, cancellable, error);
1348 static GFileOutputStream *
1349 g_local_file_replace (GFile *file,
1351 gboolean make_backup,
1352 GFileCreateFlags flags,
1353 GCancellable *cancellable,
1356 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1358 etag, make_backup, flags,
1359 cancellable, error);
1362 static GFileIOStream *
1363 g_local_file_open_readwrite (GFile *file,
1364 GCancellable *cancellable,
1367 GFileOutputStream *output;
1370 output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
1372 cancellable, error);
1376 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1377 g_object_unref (output);
1381 static GFileIOStream *
1382 g_local_file_create_readwrite (GFile *file,
1383 GFileCreateFlags flags,
1384 GCancellable *cancellable,
1387 GFileOutputStream *output;
1390 output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1392 cancellable, error);
1396 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1397 g_object_unref (output);
1401 static GFileIOStream *
1402 g_local_file_replace_readwrite (GFile *file,
1404 gboolean make_backup,
1405 GFileCreateFlags flags,
1406 GCancellable *cancellable,
1409 GFileOutputStream *output;
1412 output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1414 etag, make_backup, flags,
1415 cancellable, error);
1419 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1420 g_object_unref (output);
1425 g_local_file_delete (GFile *file,
1426 GCancellable *cancellable,
1429 GLocalFile *local = G_LOCAL_FILE (file);
1433 if (g_remove (local->filename) == -1)
1437 /* Posix allows EEXIST too, but the more sane error
1438 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1440 if (errsv == EEXIST)
1443 g_set_error (error, G_IO_ERROR,
1444 g_io_error_from_errno (errsv),
1445 _("Error removing file: %s"),
1446 g_strerror (errsv));
1450 vfs = g_vfs_get_default ();
1451 class = G_VFS_GET_CLASS (vfs);
1452 if (class->local_file_removed)
1453 class->local_file_removed (vfs, local->filename);
1461 strip_trailing_slashes (const char *path)
1466 path_copy = g_strdup (path);
1467 len = strlen (path_copy);
1468 while (len > 1 && path_copy[len-1] == '/')
1469 path_copy[--len] = 0;
1475 expand_symlink (const char *link)
1477 char *resolved, *canonical, *parent, *link2;
1478 char symlink_value[4096];
1486 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1489 return g_strdup (link);
1490 symlink_value[res] = 0;
1493 if (g_path_is_absolute (symlink_value))
1494 return canonicalize_filename (symlink_value);
1497 link2 = strip_trailing_slashes (link);
1498 parent = g_path_get_dirname (link2);
1501 resolved = g_build_filename (parent, symlink_value, NULL);
1504 canonical = canonicalize_filename (resolved);
1513 get_parent (const char *path,
1517 GStatBuf parent_stat;
1521 path_copy = strip_trailing_slashes (path);
1523 parent = g_path_get_dirname (path_copy);
1524 if (strcmp (parent, ".") == 0 ||
1525 strcmp (parent, path_copy) == 0)
1535 if (g_lstat (parent, &parent_stat) != 0)
1541 if (S_ISLNK (parent_stat.st_mode))
1544 parent = expand_symlink (parent);
1549 if (num_recursions > 12)
1554 } while (S_ISLNK (parent_stat.st_mode));
1556 *parent_dev = parent_stat.st_dev;
1562 expand_all_symlinks (const char *path)
1564 char *parent, *parent_expanded;
1565 char *basename, *res;
1568 parent = get_parent (path, &parent_dev);
1571 parent_expanded = expand_all_symlinks (parent);
1573 basename = g_path_get_basename (path);
1574 res = g_build_filename (parent_expanded, basename, NULL);
1576 g_free (parent_expanded);
1579 res = g_strdup (path);
1585 find_mountpoint_for (const char *file,
1589 dev_t dir_dev, parent_dev;
1591 dir = g_strdup (file);
1596 parent = get_parent (dir, &parent_dev);
1600 if (parent_dev != dir_dev)
1612 find_topdir_for (const char *file)
1617 dir = get_parent (file, &dir_dev);
1621 return find_mountpoint_for (dir, dir_dev);
1625 get_unique_filename (const char *basename,
1631 return g_strdup (basename);
1633 dot = strchr (basename, '.');
1635 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1637 return g_strdup_printf ("%s.%d", basename, id);
1641 path_has_prefix (const char *path,
1649 prefix_len = strlen (prefix);
1651 if (strncmp (path, prefix, prefix_len) == 0 &&
1652 (prefix_len == 0 || /* empty prefix always matches */
1653 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1654 path[prefix_len] == 0 ||
1655 path[prefix_len] == '/'))
1662 try_make_relative (const char *path,
1665 char *path2, *base2;
1668 path2 = expand_all_symlinks (path);
1669 base2 = expand_all_symlinks (base);
1672 if (path_has_prefix (path2, base2))
1674 relative = path2 + strlen (base2);
1675 while (*relative == '/')
1677 relative = g_strdup (relative);
1685 /* Failed, use abs path */
1686 return g_strdup (path);
1690 escape_trash_name (char *name)
1693 const gchar hex[16] = "0123456789ABCDEF";
1695 str = g_string_new ("");
1703 if (g_ascii_isprint (c))
1704 g_string_append_c (str, c);
1707 g_string_append_c (str, '%');
1708 g_string_append_c (str, hex[((guchar)c) >> 4]);
1709 g_string_append_c (str, hex[((guchar)c) & 0xf]);
1713 return g_string_free (str, FALSE);
1717 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1719 static gsize home_dev_set = 0;
1720 static dev_t home_dev;
1721 char *topdir, *globaldir, *trashdir, *tmpname;
1724 GStatBuf global_stat, trash_stat;
1727 if (g_once_init_enter (&home_dev_set))
1731 g_stat (g_get_home_dir (), &home_stat);
1732 home_dev = home_stat.st_dev;
1733 g_once_init_leave (&home_dev_set, 1);
1736 /* Assume we can trash to the home */
1737 if (dir_dev == home_dev)
1740 topdir = find_mountpoint_for (dirname, dir_dev);
1744 globaldir = g_build_filename (topdir, ".Trash", NULL);
1745 if (g_lstat (globaldir, &global_stat) == 0 &&
1746 S_ISDIR (global_stat.st_mode) &&
1747 (global_stat.st_mode & S_ISVTX) != 0)
1749 /* got a toplevel sysadmin created dir, assume we
1750 * can trash to it (we should be able to create a dir)
1751 * This fails for the FAT case where the ownership of
1752 * that dir would be wrong though..
1760 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1762 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1764 tmpname = g_strdup_printf (".Trash-%s", uid_str);
1765 trashdir = g_build_filename (topdir, tmpname, NULL);
1768 if (g_lstat (trashdir, &trash_stat) == 0)
1772 return S_ISDIR (trash_stat.st_mode) &&
1773 trash_stat.st_uid == uid;
1777 /* User specific trash didn't exist, can we create it? */
1778 res = g_access (topdir, W_OK) == 0;
1786 g_local_file_trash (GFile *file,
1787 GCancellable *cancellable,
1790 GLocalFile *local = G_LOCAL_FILE (file);
1791 GStatBuf file_stat, home_stat;
1792 const char *homedir;
1793 char *trashdir, *topdir, *infodir, *filesdir;
1794 char *basename, *trashname, *trashfile, *infoname, *infofile;
1795 char *original_name, *original_name_escaped;
1798 gboolean is_homedir_trash;
1799 char delete_time[32];
1801 GStatBuf trash_stat, global_stat;
1802 char *dirname, *globaldir;
1806 if (g_lstat (local->filename, &file_stat) != 0)
1810 g_set_error (error, G_IO_ERROR,
1811 g_io_error_from_errno (errsv),
1812 _("Error trashing file: %s"),
1813 g_strerror (errsv));
1817 homedir = g_get_home_dir ();
1818 g_stat (homedir, &home_stat);
1820 is_homedir_trash = FALSE;
1822 if (file_stat.st_dev == home_stat.st_dev)
1824 is_homedir_trash = TRUE;
1826 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1827 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1832 display_name = g_filename_display_name (trashdir);
1833 g_set_error (error, G_IO_ERROR,
1834 g_io_error_from_errno (errsv),
1835 _("Unable to create trash dir %s: %s"),
1836 display_name, g_strerror (errsv));
1837 g_free (display_name);
1841 topdir = g_strdup (g_get_user_data_dir ());
1849 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1851 topdir = find_topdir_for (local->filename);
1854 g_set_error_literal (error, G_IO_ERROR,
1855 G_IO_ERROR_NOT_SUPPORTED,
1856 _("Unable to find toplevel directory for trash"));
1860 /* Try looking for global trash dir $topdir/.Trash/$uid */
1861 globaldir = g_build_filename (topdir, ".Trash", NULL);
1862 if (g_lstat (globaldir, &global_stat) == 0 &&
1863 S_ISDIR (global_stat.st_mode) &&
1864 (global_stat.st_mode & S_ISVTX) != 0)
1866 trashdir = g_build_filename (globaldir, uid_str, NULL);
1868 if (g_lstat (trashdir, &trash_stat) == 0)
1870 if (!S_ISDIR (trash_stat.st_mode) ||
1871 trash_stat.st_uid != uid)
1873 /* Not a directory or not owned by user, ignore */
1878 else if (g_mkdir (trashdir, 0700) == -1)
1886 if (trashdir == NULL)
1888 gboolean tried_create;
1890 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1891 dirname = g_strdup_printf (".Trash-%s", uid_str);
1892 trashdir = g_build_filename (topdir, dirname, NULL);
1895 tried_create = FALSE;
1898 if (g_lstat (trashdir, &trash_stat) == 0)
1900 if (!S_ISDIR (trash_stat.st_mode) ||
1901 trash_stat.st_uid != uid)
1903 /* Remove the failed directory */
1905 g_remove (trashdir);
1907 /* Not a directory or not owned by user, ignore */
1914 if (!tried_create &&
1915 g_mkdir (trashdir, 0700) != -1)
1917 /* Ensure that the created dir has the right uid etc.
1918 This might fail on e.g. a FAT dir */
1919 tried_create = TRUE;
1930 if (trashdir == NULL)
1933 g_set_error_literal (error, G_IO_ERROR,
1934 G_IO_ERROR_NOT_SUPPORTED,
1935 _("Unable to find or create trash directory"));
1940 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1942 infodir = g_build_filename (trashdir, "info", NULL);
1943 filesdir = g_build_filename (trashdir, "files", NULL);
1946 /* Make sure we have the subdirectories */
1947 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1948 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
1953 g_set_error_literal (error, G_IO_ERROR,
1954 G_IO_ERROR_NOT_SUPPORTED,
1955 _("Unable to find or create trash directory"));
1959 basename = g_path_get_basename (local->filename);
1967 trashname = get_unique_filename (basename, i++);
1968 infoname = g_strconcat (trashname, ".trashinfo", NULL);
1969 infofile = g_build_filename (infodir, infoname, NULL);
1972 fd = open (infofile, O_CREAT | O_EXCL, 0666);
1973 } while (fd == -1 && errno == EEXIST);
1987 g_set_error (error, G_IO_ERROR,
1988 g_io_error_from_errno (errsv),
1989 _("Unable to create trashing info file: %s"),
1990 g_strerror (errsv));
1996 /* TODO: Maybe we should verify that you can delete the file from the trash
1997 before moving it? OTOH, that is hard, as it needs a recursive scan */
1999 trashfile = g_build_filename (filesdir, trashname, NULL);
2003 if (g_rename (local->filename, trashfile) == -1)
2013 /* The trash dir was actually on another fs anyway!?
2014 This can happen when the same device is mounted multiple
2015 times, or with bind mounts of the same fs. */
2016 g_set_error (error, G_IO_ERROR,
2017 G_IO_ERROR_NOT_SUPPORTED,
2018 _("Unable to trash file: %s"),
2019 g_strerror (errsv));
2021 g_set_error (error, G_IO_ERROR,
2022 g_io_error_from_errno (errsv),
2023 _("Unable to trash file: %s"),
2024 g_strerror (errsv));
2028 vfs = g_vfs_get_default ();
2029 class = G_VFS_GET_CLASS (vfs);
2030 if (class->local_file_moved)
2031 class->local_file_moved (vfs, local->filename, trashfile);
2035 /* TODO: Do we need to update mtime/atime here after the move? */
2037 /* Use absolute names for homedir */
2038 if (is_homedir_trash)
2039 original_name = g_strdup (local->filename);
2041 original_name = try_make_relative (local->filename, topdir);
2042 original_name_escaped = escape_trash_name (original_name);
2044 g_free (original_name);
2051 localtime_r (&t, &now);
2053 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
2056 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2057 original_name_escaped, delete_time);
2059 g_file_set_contents (infofile, data, -1, NULL);
2063 g_free (original_name_escaped);
2068 #else /* G_OS_WIN32 */
2070 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
2072 return FALSE; /* XXX ??? */
2076 g_local_file_trash (GFile *file,
2077 GCancellable *cancellable,
2080 GLocalFile *local = G_LOCAL_FILE (file);
2081 SHFILEOPSTRUCTW op = {0};
2086 wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
2087 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2088 wfilename = g_renew (wchar_t, wfilename, len + 2);
2089 wfilename[len + 1] = 0;
2091 op.wFunc = FO_DELETE;
2092 op.pFrom = wfilename;
2093 op.fFlags = FOF_ALLOWUNDO;
2095 success = SHFileOperationW (&op) == 0;
2097 if (success && op.fAnyOperationsAborted)
2099 if (cancellable && !g_cancellable_is_cancelled (cancellable))
2100 g_cancellable_cancel (cancellable);
2101 g_set_error (error, G_IO_ERROR,
2102 G_IO_ERROR_CANCELLED,
2103 _("Unable to trash file: %s"),
2104 _("Operation was cancelled"));
2108 g_set_error (error, G_IO_ERROR,
2110 _("Unable to trash file: %s"),
2111 _("internal error"));
2116 #endif /* G_OS_WIN32 */
2119 g_local_file_make_directory (GFile *file,
2120 GCancellable *cancellable,
2123 GLocalFile *local = G_LOCAL_FILE (file);
2125 if (g_mkdir (local->filename, 0777) == -1)
2129 if (errsv == EINVAL)
2130 /* This must be an invalid filename, on e.g. FAT */
2131 g_set_error_literal (error, G_IO_ERROR,
2132 G_IO_ERROR_INVALID_FILENAME,
2133 _("Invalid filename"));
2135 g_set_error (error, G_IO_ERROR,
2136 g_io_error_from_errno (errsv),
2137 _("Error creating directory: %s"),
2138 g_strerror (errsv));
2146 g_local_file_make_symbolic_link (GFile *file,
2147 const char *symlink_value,
2148 GCancellable *cancellable,
2152 GLocalFile *local = G_LOCAL_FILE (file);
2154 if (symlink (symlink_value, local->filename) == -1)
2158 if (errsv == EINVAL)
2159 /* This must be an invalid filename, on e.g. FAT */
2160 g_set_error_literal (error, G_IO_ERROR,
2161 G_IO_ERROR_INVALID_FILENAME,
2162 _("Invalid filename"));
2163 else if (errsv == EPERM)
2164 g_set_error (error, G_IO_ERROR,
2165 G_IO_ERROR_NOT_SUPPORTED,
2166 _("Filesystem does not support symbolic links"));
2168 g_set_error (error, G_IO_ERROR,
2169 g_io_error_from_errno (errsv),
2170 _("Error making symbolic link: %s"),
2171 g_strerror (errsv));
2176 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
2183 g_local_file_copy (GFile *source,
2185 GFileCopyFlags flags,
2186 GCancellable *cancellable,
2187 GFileProgressCallback progress_callback,
2188 gpointer progress_callback_data,
2191 /* Fall back to default copy */
2192 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
2197 g_local_file_move (GFile *source,
2199 GFileCopyFlags flags,
2200 GCancellable *cancellable,
2201 GFileProgressCallback progress_callback,
2202 gpointer progress_callback_data,
2205 GLocalFile *local_source, *local_destination;
2207 gboolean destination_exist, source_is_dir;
2214 if (!G_IS_LOCAL_FILE (source) ||
2215 !G_IS_LOCAL_FILE (destination))
2217 /* Fall back to default move */
2218 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2222 local_source = G_LOCAL_FILE (source);
2223 local_destination = G_LOCAL_FILE (destination);
2225 res = g_lstat (local_source->filename, &statbuf);
2230 g_set_error (error, G_IO_ERROR,
2231 g_io_error_from_errno (errsv),
2232 _("Error moving file: %s"),
2233 g_strerror (errsv));
2237 source_is_dir = S_ISDIR (statbuf.st_mode);
2238 source_size = statbuf.st_size;
2240 destination_exist = FALSE;
2241 res = g_lstat (local_destination->filename, &statbuf);
2244 destination_exist = TRUE; /* Target file exists */
2246 if (flags & G_FILE_COPY_OVERWRITE)
2248 /* Always fail on dirs, even with overwrite */
2249 if (S_ISDIR (statbuf.st_mode))
2252 g_set_error_literal (error,
2254 G_IO_ERROR_WOULD_MERGE,
2255 _("Can't move directory over directory"));
2257 g_set_error_literal (error,
2259 G_IO_ERROR_IS_DIRECTORY,
2260 _("Can't copy over directory"));
2266 g_set_error_literal (error,
2269 _("Target file exists"));
2274 if (flags & G_FILE_COPY_BACKUP && destination_exist)
2276 backup_name = g_strconcat (local_destination->filename, "~", NULL);
2277 if (g_rename (local_destination->filename, backup_name) == -1)
2279 g_set_error_literal (error,
2281 G_IO_ERROR_CANT_CREATE_BACKUP,
2282 _("Backup file creation failed"));
2283 g_free (backup_name);
2286 g_free (backup_name);
2287 destination_exist = FALSE; /* It did, but no more */
2290 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2292 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2293 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2294 res = g_unlink (local_destination->filename);
2299 g_set_error (error, G_IO_ERROR,
2300 g_io_error_from_errno (errsv),
2301 _("Error removing target file: %s"),
2302 g_strerror (errsv));
2307 if (g_rename (local_source->filename, local_destination->filename) == -1)
2312 /* This will cause the fallback code to run */
2313 g_set_error_literal (error, G_IO_ERROR,
2314 G_IO_ERROR_NOT_SUPPORTED,
2315 _("Move between mounts not supported"));
2316 else if (errsv == EINVAL)
2317 /* This must be an invalid filename, on e.g. FAT, or
2318 we're trying to move the file into itself...
2319 We return invalid filename for both... */
2320 g_set_error_literal (error, G_IO_ERROR,
2321 G_IO_ERROR_INVALID_FILENAME,
2322 _("Invalid filename"));
2324 g_set_error (error, G_IO_ERROR,
2325 g_io_error_from_errno (errsv),
2326 _("Error moving file: %s"),
2327 g_strerror (errsv));
2331 vfs = g_vfs_get_default ();
2332 class = G_VFS_GET_CLASS (vfs);
2333 if (class->local_file_moved)
2334 class->local_file_moved (vfs, local_source->filename, local_destination->filename);
2336 /* Make sure we send full copied size */
2337 if (progress_callback)
2338 progress_callback (source_size, source_size, progress_callback_data);
2343 static GFileMonitor*
2344 g_local_file_monitor_dir (GFile *file,
2345 GFileMonitorFlags flags,
2346 GCancellable *cancellable,
2349 GLocalFile* local_file = G_LOCAL_FILE(file);
2350 return _g_local_directory_monitor_new (local_file->filename, flags, error);
2353 static GFileMonitor*
2354 g_local_file_monitor_file (GFile *file,
2355 GFileMonitorFlags flags,
2356 GCancellable *cancellable,
2359 GLocalFile* local_file = G_LOCAL_FILE(file);
2360 return _g_local_file_monitor_new (local_file->filename, flags, error);
2364 g_local_file_file_iface_init (GFileIface *iface)
2366 iface->dup = g_local_file_dup;
2367 iface->hash = g_local_file_hash;
2368 iface->equal = g_local_file_equal;
2369 iface->is_native = g_local_file_is_native;
2370 iface->has_uri_scheme = g_local_file_has_uri_scheme;
2371 iface->get_uri_scheme = g_local_file_get_uri_scheme;
2372 iface->get_basename = g_local_file_get_basename;
2373 iface->get_path = g_local_file_get_path;
2374 iface->get_uri = g_local_file_get_uri;
2375 iface->get_parse_name = g_local_file_get_parse_name;
2376 iface->get_parent = g_local_file_get_parent;
2377 iface->prefix_matches = g_local_file_prefix_matches;
2378 iface->get_relative_path = g_local_file_get_relative_path;
2379 iface->resolve_relative_path = g_local_file_resolve_relative_path;
2380 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2381 iface->set_display_name = g_local_file_set_display_name;
2382 iface->enumerate_children = g_local_file_enumerate_children;
2383 iface->query_info = g_local_file_query_info;
2384 iface->query_filesystem_info = g_local_file_query_filesystem_info;
2385 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2386 iface->query_settable_attributes = g_local_file_query_settable_attributes;
2387 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2388 iface->set_attribute = g_local_file_set_attribute;
2389 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2390 iface->read_fn = g_local_file_read;
2391 iface->append_to = g_local_file_append_to;
2392 iface->create = g_local_file_create;
2393 iface->replace = g_local_file_replace;
2394 iface->open_readwrite = g_local_file_open_readwrite;
2395 iface->create_readwrite = g_local_file_create_readwrite;
2396 iface->replace_readwrite = g_local_file_replace_readwrite;
2397 iface->delete_file = g_local_file_delete;
2398 iface->trash = g_local_file_trash;
2399 iface->make_directory = g_local_file_make_directory;
2400 iface->make_symbolic_link = g_local_file_make_symbolic_link;
2401 iface->copy = g_local_file_copy;
2402 iface->move = g_local_file_move;
2403 iface->monitor_dir = g_local_file_monitor_dir;
2404 iface->monitor_file = g_local_file_monitor_file;
2406 iface->supports_thread_contexts = TRUE;