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);
418 char *dup_filename, *p, *backslash;
420 /* Turn backslashes into forward slashes like
421 * g_filename_to_uri() would do (but we can't use that because
422 * it doesn't output IRIs).
424 dup_filename = g_strdup (filename);
425 filename = p = dup_filename;
427 while ((backslash = strchr (p, '\\')) != NULL)
434 escaped_path = g_uri_escape_string (filename,
435 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
437 parse_name = g_strconcat ("file://",
438 (*escaped_path != '/') ? "/" : "",
442 g_free (escaped_path);
444 g_free (dup_filename);
446 if (free_utf8_filename)
447 g_free (utf8_filename);
454 g_local_file_get_parent (GFile *file)
456 GLocalFile *local = G_LOCAL_FILE (file);
457 const char *non_root;
462 non_root = g_path_skip_root (local->filename);
466 dirname = g_path_get_dirname (local->filename);
467 parent = _g_local_file_new (dirname);
473 g_local_file_dup (GFile *file)
475 GLocalFile *local = G_LOCAL_FILE (file);
477 return _g_local_file_new (local->filename);
481 g_local_file_hash (GFile *file)
483 GLocalFile *local = G_LOCAL_FILE (file);
485 return g_str_hash (local->filename);
489 g_local_file_equal (GFile *file1,
492 GLocalFile *local1 = G_LOCAL_FILE (file1);
493 GLocalFile *local2 = G_LOCAL_FILE (file2);
495 return g_str_equal (local1->filename, local2->filename);
499 match_prefix (const char *path,
504 prefix_len = strlen (prefix);
505 if (strncmp (path, prefix, prefix_len) != 0)
508 /* Handle the case where prefix is the root, so that
509 * the IS_DIR_SEPRARATOR check below works */
510 if (prefix_len > 0 &&
511 G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
514 return path + prefix_len;
518 g_local_file_prefix_matches (GFile *parent,
521 GLocalFile *parent_local = G_LOCAL_FILE (parent);
522 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
523 const char *remainder;
525 remainder = match_prefix (descendant_local->filename, parent_local->filename);
526 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
532 g_local_file_get_relative_path (GFile *parent,
535 GLocalFile *parent_local = G_LOCAL_FILE (parent);
536 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
537 const char *remainder;
539 remainder = match_prefix (descendant_local->filename, parent_local->filename);
541 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
542 return g_strdup (remainder + 1);
547 g_local_file_resolve_relative_path (GFile *file,
548 const char *relative_path)
550 GLocalFile *local = G_LOCAL_FILE (file);
554 if (g_path_is_absolute (relative_path))
555 return _g_local_file_new (relative_path);
557 filename = g_build_filename (local->filename, relative_path, NULL);
558 child = _g_local_file_new (filename);
564 static GFileEnumerator *
565 g_local_file_enumerate_children (GFile *file,
566 const char *attributes,
567 GFileQueryInfoFlags flags,
568 GCancellable *cancellable,
571 GLocalFile *local = G_LOCAL_FILE (file);
572 return _g_local_file_enumerator_new (local,
578 g_local_file_get_child_for_display_name (GFile *file,
579 const char *display_name,
585 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
586 if (basename == NULL)
588 g_set_error (error, G_IO_ERROR,
589 G_IO_ERROR_INVALID_FILENAME,
590 _("Invalid filename %s"), display_name);
594 new_file = g_file_get_child (file, basename);
600 #if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
602 get_fs_type (long f_type)
604 /* filesystem ids taken from linux manpage */
711 G_LOCK_DEFINE_STATIC(mount_info_hash);
712 static GHashTable *mount_info_hash = NULL;
713 static guint64 mount_info_hash_cache_time = 0;
716 MOUNT_INFO_READONLY = 1<<0
720 device_equal (gconstpointer v1,
723 return *(dev_t *)v1 == *(dev_t *)v2;
727 device_hash (gconstpointer v)
729 return (guint) *(dev_t *)v;
733 get_mount_info (GFileInfo *fs_info,
735 GFileAttributeMatcher *matcher)
739 gpointer info_as_ptr;
743 GUnixMountEntry *mount;
746 if (g_lstat (path, &buf) != 0)
749 G_LOCK (mount_info_hash);
751 if (mount_info_hash == NULL)
752 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
756 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
757 g_hash_table_remove_all (mount_info_hash);
759 got_info = g_hash_table_lookup_extended (mount_info_hash,
764 G_UNLOCK (mount_info_hash);
766 mount_info = GPOINTER_TO_UINT (info_as_ptr);
772 mountpoint = find_mountpoint_for (path, buf.st_dev);
773 if (mountpoint == NULL)
774 mountpoint = g_strdup ("/");
776 mount = g_unix_mount_at (mountpoint, &cache_time);
779 if (g_unix_mount_is_readonly (mount))
780 mount_info |= MOUNT_INFO_READONLY;
782 g_unix_mount_free (mount);
787 dev = g_new0 (dev_t, 1);
790 G_LOCK (mount_info_hash);
791 mount_info_hash_cache_time = cache_time;
792 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
793 G_UNLOCK (mount_info_hash);
796 if (mount_info & MOUNT_INFO_READONLY)
797 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
805 is_xp_or_later (void)
807 static int result = -1;
812 OSVERSIONINFOEX ver_info = {0};
813 DWORDLONG cond_mask = 0;
814 int op = VER_GREATER_EQUAL;
816 ver_info.dwOSVersionInfoSize = sizeof ver_info;
817 ver_info.dwMajorVersion = 5;
818 ver_info.dwMinorVersion = 1;
820 VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
821 VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
823 result = VerifyVersionInfo (&ver_info,
824 VER_MAJORVERSION | VER_MINORVERSION,
827 result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;
835 get_volume_for_path (const char *path)
841 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
842 result = g_new (wchar_t, MAX_PATH);
844 if (!GetVolumePathNameW (wpath, result, MAX_PATH))
846 char *msg = g_win32_error_message (GetLastError ());
847 g_critical ("GetVolumePathName failed: %s", msg);
854 len = wcslen (result);
855 if (len > 0 && result[len-1] != L'\\')
857 result = g_renew (wchar_t, result, len + 2);
867 find_mountpoint_for (const char *file, dev_t dev)
872 wpath = get_volume_for_path (file);
876 utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
883 get_filesystem_readonly (GFileInfo *info,
888 rootdir = get_volume_for_path (path);
892 if (is_xp_or_later ())
895 if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
896 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
897 (flags & FILE_READ_ONLY_VOLUME) != 0);
901 if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
902 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
909 #endif /* G_OS_WIN32 */
912 g_local_file_query_filesystem_info (GFile *file,
913 const char *attributes,
914 GCancellable *cancellable,
917 GLocalFile *local = G_LOCAL_FILE (file);
919 int statfs_result = 0;
925 struct statfs statfs_buffer;
926 #elif defined(USE_STATVFS)
928 struct statvfs statfs_buffer;
929 #endif /* USE_STATFS */
930 #endif /* G_OS_WIN32 */
931 GFileAttributeMatcher *attribute_matcher;
938 statfs_result = statfs (local->filename, &statfs_buffer);
939 #elif STATFS_ARGS == 4
940 statfs_result = statfs (local->filename, &statfs_buffer,
941 sizeof (statfs_buffer), 0);
942 #endif /* STATFS_ARGS == 2 */
943 block_size = statfs_buffer.f_bsize;
945 /* Many backends can't report free size (for instance the gvfs fuse
946 backend for backend not supporting this), and set f_bfree to 0,
947 but it can be 0 for real too. We treat the available == 0 and
948 free == 0 case as "both of these are invalid".
951 if (statfs_result == 0 &&
952 statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0)
954 #endif /* G_OS_WIN32 */
956 #elif defined(USE_STATVFS)
957 statfs_result = statvfs (local->filename, &statfs_buffer);
958 block_size = statfs_buffer.f_frsize;
959 #endif /* USE_STATFS */
961 if (statfs_result == -1)
965 g_set_error (error, G_IO_ERROR,
966 g_io_error_from_errno (errsv),
967 _("Error getting filesystem info: %s"),
972 info = g_file_info_new ();
974 attribute_matcher = g_file_attribute_matcher_new (attributes);
977 g_file_attribute_matcher_matches (attribute_matcher,
978 G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
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, &li, NULL, NULL))
987 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
990 #if defined(USE_STATFS) || defined(USE_STATVFS)
991 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 #if defined(USE_STATFS) || defined(USE_STATVFS)
1010 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
1012 #endif /* G_OS_WIN32 */
1016 g_file_attribute_matcher_matches (attribute_matcher,
1017 G_FILE_ATTRIBUTE_FILESYSTEM_USED))
1020 gchar *localdir = g_path_get_dirname (local->filename);
1021 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1022 ULARGE_INTEGER li_free;
1023 ULARGE_INTEGER li_total;
1026 if (GetDiskFreeSpaceExW (wdirname, &li_free, &li_total, NULL))
1027 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, (guint64)li_total.QuadPart - (guint64)li_free.QuadPart);
1030 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, block_size * (statfs_buffer.f_blocks - statfs_buffer.f_bfree));
1031 #endif /* G_OS_WIN32 */
1036 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1037 fstype = g_strdup (statfs_buffer.f_fstypename);
1039 fstype = get_fs_type (statfs_buffer.f_type);
1042 #elif defined(USE_STATVFS)
1043 #if defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
1044 fstype = g_strdup (statfs_buffer.f_fstypename);
1045 #elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1046 fstype = g_strdup (statfs_buffer.f_basetype);
1050 #endif /* USE_STATFS */
1053 g_file_attribute_matcher_matches (attribute_matcher,
1054 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1055 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1056 #endif /* G_OS_WIN32 */
1058 if (g_file_attribute_matcher_matches (attribute_matcher,
1059 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
1062 get_filesystem_readonly (info, local->filename);
1064 get_mount_info (info, local->filename, attribute_matcher);
1065 #endif /* G_OS_WIN32 */
1068 g_file_attribute_matcher_unref (attribute_matcher);
1074 g_local_file_find_enclosing_mount (GFile *file,
1075 GCancellable *cancellable,
1078 GLocalFile *local = G_LOCAL_FILE (file);
1083 if (g_lstat (local->filename, &buf) != 0)
1085 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1086 /* Translators: This is an error message when trying to
1087 * find the enclosing (user visible) mount of a file, but
1089 _("Containing mount does not exist"));
1093 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
1094 if (mountpoint == NULL)
1096 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1097 /* Translators: This is an error message when trying to
1098 * find the enclosing (user visible) mount of a file, but
1100 _("Containing mount does not exist"));
1104 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1105 g_free (mountpoint);
1109 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1110 /* Translators: This is an error message when trying to find
1111 * the enclosing (user visible) mount of a file, but none
1113 _("Containing mount does not exist"));
1118 g_local_file_set_display_name (GFile *file,
1119 const char *display_name,
1120 GCancellable *cancellable,
1123 GLocalFile *local, *new_local;
1124 GFile *new_file, *parent;
1130 parent = g_file_get_parent (file);
1133 g_set_error_literal (error, G_IO_ERROR,
1135 _("Can't rename root directory"));
1139 new_file = g_file_get_child_for_display_name (parent, display_name, error);
1140 g_object_unref (parent);
1142 if (new_file == NULL)
1144 local = G_LOCAL_FILE (file);
1145 new_local = G_LOCAL_FILE (new_file);
1147 if (g_lstat (new_local->filename, &statbuf) == -1)
1151 if (errsv != ENOENT)
1153 g_set_error (error, G_IO_ERROR,
1154 g_io_error_from_errno (errsv),
1155 _("Error renaming file: %s"),
1156 g_strerror (errsv));
1162 g_set_error_literal (error, G_IO_ERROR,
1164 _("Can't rename file, filename already exists"));
1168 if (g_rename (local->filename, new_local->filename) == -1)
1172 if (errsv == EINVAL)
1173 /* We can't get a rename file into itself error herer,
1174 so this must be an invalid filename, on e.g. FAT */
1175 g_set_error_literal (error, G_IO_ERROR,
1176 G_IO_ERROR_INVALID_FILENAME,
1177 _("Invalid filename"));
1179 g_set_error (error, G_IO_ERROR,
1180 g_io_error_from_errno (errsv),
1181 _("Error renaming file: %s"),
1182 g_strerror (errsv));
1183 g_object_unref (new_file);
1187 vfs = g_vfs_get_default ();
1188 class = G_VFS_GET_CLASS (vfs);
1189 if (class->local_file_moved)
1190 class->local_file_moved (vfs, local->filename, new_local->filename);
1196 g_local_file_query_info (GFile *file,
1197 const char *attributes,
1198 GFileQueryInfoFlags flags,
1199 GCancellable *cancellable,
1202 GLocalFile *local = G_LOCAL_FILE (file);
1204 GFileAttributeMatcher *matcher;
1205 char *basename, *dirname;
1206 GLocalParentFileInfo parent_info;
1208 matcher = g_file_attribute_matcher_new (attributes);
1210 basename = g_path_get_basename (local->filename);
1212 dirname = g_path_get_dirname (local->filename);
1213 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1216 info = _g_local_file_info_get (basename, local->filename,
1217 matcher, flags, &parent_info,
1221 _g_local_file_info_free_parent_info (&parent_info);
1224 g_file_attribute_matcher_unref (matcher);
1229 static GFileAttributeInfoList *
1230 g_local_file_query_settable_attributes (GFile *file,
1231 GCancellable *cancellable,
1234 return g_file_attribute_info_list_ref (local_writable_attributes);
1237 static GFileAttributeInfoList *
1238 g_local_file_query_writable_namespaces (GFile *file,
1239 GCancellable *cancellable,
1242 GFileAttributeInfoList *list;
1246 if (g_once_init_enter (&local_writable_namespaces))
1248 /* Writable namespaces: */
1250 list = g_file_attribute_info_list_new ();
1253 g_file_attribute_info_list_add (list,
1255 G_FILE_ATTRIBUTE_TYPE_STRING,
1256 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
1257 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1258 g_file_attribute_info_list_add (list,
1260 G_FILE_ATTRIBUTE_TYPE_STRING,
1261 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
1264 vfs = g_vfs_get_default ();
1265 class = G_VFS_GET_CLASS (vfs);
1266 if (class->add_writable_namespaces)
1267 class->add_writable_namespaces (vfs, list);
1269 g_once_init_leave (&local_writable_namespaces, (gsize)list);
1271 list = (GFileAttributeInfoList *)local_writable_namespaces;
1273 return g_file_attribute_info_list_ref (list);
1277 g_local_file_set_attribute (GFile *file,
1278 const char *attribute,
1279 GFileAttributeType type,
1281 GFileQueryInfoFlags flags,
1282 GCancellable *cancellable,
1285 GLocalFile *local = G_LOCAL_FILE (file);
1287 return _g_local_file_info_set_attribute (local->filename,
1297 g_local_file_set_attributes_from_info (GFile *file,
1299 GFileQueryInfoFlags flags,
1300 GCancellable *cancellable,
1303 GLocalFile *local = G_LOCAL_FILE (file);
1304 int res, chained_res;
1305 GFileIface *default_iface;
1307 res = _g_local_file_info_set_attributes (local->filename,
1313 error = NULL; /* Don't write over error if further errors */
1315 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1317 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1319 return res && chained_res;
1322 static GFileInputStream *
1323 g_local_file_read (GFile *file,
1324 GCancellable *cancellable,
1327 GLocalFile *local = G_LOCAL_FILE (file);
1331 fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1337 if (errsv == EACCES)
1339 ret = _stati64 (local->filename, &buf);
1340 if (ret == 0 && S_ISDIR (buf.st_mode))
1342 g_set_error_literal (error, G_IO_ERROR,
1343 G_IO_ERROR_IS_DIRECTORY,
1344 _("Can't open directory"));
1350 g_set_error (error, G_IO_ERROR,
1351 g_io_error_from_errno (errsv),
1352 _("Error opening file: %s"),
1353 g_strerror (errsv));
1358 ret = _fstati64 (fd, &buf);
1360 ret = fstat (fd, &buf);
1363 if (ret == 0 && S_ISDIR (buf.st_mode))
1366 g_set_error_literal (error, G_IO_ERROR,
1367 G_IO_ERROR_IS_DIRECTORY,
1368 _("Can't open directory"));
1372 return _g_local_file_input_stream_new (fd);
1375 static GFileOutputStream *
1376 g_local_file_append_to (GFile *file,
1377 GFileCreateFlags flags,
1378 GCancellable *cancellable,
1381 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1382 flags, cancellable, error);
1385 static GFileOutputStream *
1386 g_local_file_create (GFile *file,
1387 GFileCreateFlags flags,
1388 GCancellable *cancellable,
1391 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1393 flags, cancellable, error);
1396 static GFileOutputStream *
1397 g_local_file_replace (GFile *file,
1399 gboolean make_backup,
1400 GFileCreateFlags flags,
1401 GCancellable *cancellable,
1404 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1406 etag, make_backup, flags,
1407 cancellable, error);
1410 static GFileIOStream *
1411 g_local_file_open_readwrite (GFile *file,
1412 GCancellable *cancellable,
1415 GFileOutputStream *output;
1418 output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
1420 cancellable, error);
1424 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1425 g_object_unref (output);
1429 static GFileIOStream *
1430 g_local_file_create_readwrite (GFile *file,
1431 GFileCreateFlags flags,
1432 GCancellable *cancellable,
1435 GFileOutputStream *output;
1438 output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1440 cancellable, error);
1444 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1445 g_object_unref (output);
1449 static GFileIOStream *
1450 g_local_file_replace_readwrite (GFile *file,
1452 gboolean make_backup,
1453 GFileCreateFlags flags,
1454 GCancellable *cancellable,
1457 GFileOutputStream *output;
1460 output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1462 etag, make_backup, flags,
1463 cancellable, error);
1467 res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
1468 g_object_unref (output);
1473 g_local_file_delete (GFile *file,
1474 GCancellable *cancellable,
1477 GLocalFile *local = G_LOCAL_FILE (file);
1481 if (g_remove (local->filename) == -1)
1485 /* Posix allows EEXIST too, but the more sane error
1486 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1488 if (errsv == EEXIST)
1491 g_set_error (error, G_IO_ERROR,
1492 g_io_error_from_errno (errsv),
1493 _("Error removing file: %s"),
1494 g_strerror (errsv));
1498 vfs = g_vfs_get_default ();
1499 class = G_VFS_GET_CLASS (vfs);
1500 if (class->local_file_removed)
1501 class->local_file_removed (vfs, local->filename);
1509 strip_trailing_slashes (const char *path)
1514 path_copy = g_strdup (path);
1515 len = strlen (path_copy);
1516 while (len > 1 && path_copy[len-1] == '/')
1517 path_copy[--len] = 0;
1523 expand_symlink (const char *link)
1525 char *resolved, *canonical, *parent, *link2;
1526 char symlink_value[4096];
1534 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1537 return g_strdup (link);
1538 symlink_value[res] = 0;
1541 if (g_path_is_absolute (symlink_value))
1542 return canonicalize_filename (symlink_value);
1545 link2 = strip_trailing_slashes (link);
1546 parent = g_path_get_dirname (link2);
1549 resolved = g_build_filename (parent, symlink_value, NULL);
1552 canonical = canonicalize_filename (resolved);
1561 get_parent (const char *path,
1565 GStatBuf parent_stat;
1569 path_copy = strip_trailing_slashes (path);
1571 parent = g_path_get_dirname (path_copy);
1572 if (strcmp (parent, ".") == 0 ||
1573 strcmp (parent, path_copy) == 0)
1583 if (g_lstat (parent, &parent_stat) != 0)
1589 if (S_ISLNK (parent_stat.st_mode))
1592 parent = expand_symlink (parent);
1597 if (num_recursions > 12)
1602 } while (S_ISLNK (parent_stat.st_mode));
1604 *parent_dev = parent_stat.st_dev;
1610 expand_all_symlinks (const char *path)
1612 char *parent, *parent_expanded;
1613 char *basename, *res;
1616 parent = get_parent (path, &parent_dev);
1619 parent_expanded = expand_all_symlinks (parent);
1621 basename = g_path_get_basename (path);
1622 res = g_build_filename (parent_expanded, basename, NULL);
1624 g_free (parent_expanded);
1627 res = g_strdup (path);
1633 find_mountpoint_for (const char *file,
1637 dev_t dir_dev, parent_dev;
1639 dir = g_strdup (file);
1644 parent = get_parent (dir, &parent_dev);
1648 if (parent_dev != dir_dev)
1660 find_topdir_for (const char *file)
1665 dir = get_parent (file, &dir_dev);
1669 return find_mountpoint_for (dir, dir_dev);
1673 get_unique_filename (const char *basename,
1679 return g_strdup (basename);
1681 dot = strchr (basename, '.');
1683 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1685 return g_strdup_printf ("%s.%d", basename, id);
1689 path_has_prefix (const char *path,
1697 prefix_len = strlen (prefix);
1699 if (strncmp (path, prefix, prefix_len) == 0 &&
1700 (prefix_len == 0 || /* empty prefix always matches */
1701 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1702 path[prefix_len] == 0 ||
1703 path[prefix_len] == '/'))
1710 try_make_relative (const char *path,
1713 char *path2, *base2;
1716 path2 = expand_all_symlinks (path);
1717 base2 = expand_all_symlinks (base);
1720 if (path_has_prefix (path2, base2))
1722 relative = path2 + strlen (base2);
1723 while (*relative == '/')
1725 relative = g_strdup (relative);
1733 /* Failed, use abs path */
1734 return g_strdup (path);
1738 escape_trash_name (char *name)
1741 const gchar hex[16] = "0123456789ABCDEF";
1743 str = g_string_new ("");
1751 if (g_ascii_isprint (c))
1752 g_string_append_c (str, c);
1755 g_string_append_c (str, '%');
1756 g_string_append_c (str, hex[((guchar)c) >> 4]);
1757 g_string_append_c (str, hex[((guchar)c) & 0xf]);
1761 return g_string_free (str, FALSE);
1765 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1767 static gsize home_dev_set = 0;
1768 static dev_t home_dev;
1769 char *topdir, *globaldir, *trashdir, *tmpname;
1772 GStatBuf global_stat, trash_stat;
1775 if (g_once_init_enter (&home_dev_set))
1779 g_stat (g_get_home_dir (), &home_stat);
1780 home_dev = home_stat.st_dev;
1781 g_once_init_leave (&home_dev_set, 1);
1784 /* Assume we can trash to the home */
1785 if (dir_dev == home_dev)
1788 topdir = find_mountpoint_for (dirname, dir_dev);
1792 globaldir = g_build_filename (topdir, ".Trash", NULL);
1793 if (g_lstat (globaldir, &global_stat) == 0 &&
1794 S_ISDIR (global_stat.st_mode) &&
1795 (global_stat.st_mode & S_ISVTX) != 0)
1797 /* got a toplevel sysadmin created dir, assume we
1798 * can trash to it (we should be able to create a dir)
1799 * This fails for the FAT case where the ownership of
1800 * that dir would be wrong though..
1808 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1810 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1812 tmpname = g_strdup_printf (".Trash-%s", uid_str);
1813 trashdir = g_build_filename (topdir, tmpname, NULL);
1816 if (g_lstat (trashdir, &trash_stat) == 0)
1820 return S_ISDIR (trash_stat.st_mode) &&
1821 trash_stat.st_uid == uid;
1825 /* User specific trash didn't exist, can we create it? */
1826 res = g_access (topdir, W_OK) == 0;
1834 g_local_file_trash (GFile *file,
1835 GCancellable *cancellable,
1838 GLocalFile *local = G_LOCAL_FILE (file);
1839 GStatBuf file_stat, home_stat;
1840 const char *homedir;
1841 char *trashdir, *topdir, *infodir, *filesdir;
1842 char *basename, *trashname, *trashfile, *infoname, *infofile;
1843 char *original_name, *original_name_escaped;
1846 gboolean is_homedir_trash;
1847 char delete_time[32];
1849 GStatBuf trash_stat, global_stat;
1850 char *dirname, *globaldir;
1854 if (g_lstat (local->filename, &file_stat) != 0)
1858 g_set_error (error, G_IO_ERROR,
1859 g_io_error_from_errno (errsv),
1860 _("Error trashing file: %s"),
1861 g_strerror (errsv));
1865 homedir = g_get_home_dir ();
1866 g_stat (homedir, &home_stat);
1868 is_homedir_trash = FALSE;
1870 if (file_stat.st_dev == home_stat.st_dev)
1872 is_homedir_trash = TRUE;
1874 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1875 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1880 display_name = g_filename_display_name (trashdir);
1881 g_set_error (error, G_IO_ERROR,
1882 g_io_error_from_errno (errsv),
1883 _("Unable to create trash dir %s: %s"),
1884 display_name, g_strerror (errsv));
1885 g_free (display_name);
1889 topdir = g_strdup (g_get_user_data_dir ());
1897 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1899 topdir = find_topdir_for (local->filename);
1902 g_set_error_literal (error, G_IO_ERROR,
1903 G_IO_ERROR_NOT_SUPPORTED,
1904 _("Unable to find toplevel directory for trash"));
1908 /* Try looking for global trash dir $topdir/.Trash/$uid */
1909 globaldir = g_build_filename (topdir, ".Trash", NULL);
1910 if (g_lstat (globaldir, &global_stat) == 0 &&
1911 S_ISDIR (global_stat.st_mode) &&
1912 (global_stat.st_mode & S_ISVTX) != 0)
1914 trashdir = g_build_filename (globaldir, uid_str, NULL);
1916 if (g_lstat (trashdir, &trash_stat) == 0)
1918 if (!S_ISDIR (trash_stat.st_mode) ||
1919 trash_stat.st_uid != uid)
1921 /* Not a directory or not owned by user, ignore */
1926 else if (g_mkdir (trashdir, 0700) == -1)
1934 if (trashdir == NULL)
1936 gboolean tried_create;
1938 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1939 dirname = g_strdup_printf (".Trash-%s", uid_str);
1940 trashdir = g_build_filename (topdir, dirname, NULL);
1943 tried_create = FALSE;
1946 if (g_lstat (trashdir, &trash_stat) == 0)
1948 if (!S_ISDIR (trash_stat.st_mode) ||
1949 trash_stat.st_uid != uid)
1951 /* Remove the failed directory */
1953 g_remove (trashdir);
1955 /* Not a directory or not owned by user, ignore */
1962 if (!tried_create &&
1963 g_mkdir (trashdir, 0700) != -1)
1965 /* Ensure that the created dir has the right uid etc.
1966 This might fail on e.g. a FAT dir */
1967 tried_create = TRUE;
1978 if (trashdir == NULL)
1981 g_set_error_literal (error, G_IO_ERROR,
1982 G_IO_ERROR_NOT_SUPPORTED,
1983 _("Unable to find or create trash directory"));
1988 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1990 infodir = g_build_filename (trashdir, "info", NULL);
1991 filesdir = g_build_filename (trashdir, "files", NULL);
1994 /* Make sure we have the subdirectories */
1995 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1996 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
2001 g_set_error_literal (error, G_IO_ERROR,
2002 G_IO_ERROR_NOT_SUPPORTED,
2003 _("Unable to find or create trash directory"));
2007 basename = g_path_get_basename (local->filename);
2015 trashname = get_unique_filename (basename, i++);
2016 infoname = g_strconcat (trashname, ".trashinfo", NULL);
2017 infofile = g_build_filename (infodir, infoname, NULL);
2020 fd = g_open (infofile, O_CREAT | O_EXCL, 0666);
2021 } while (fd == -1 && errno == EEXIST);
2035 g_set_error (error, G_IO_ERROR,
2036 g_io_error_from_errno (errsv),
2037 _("Unable to create trashing info file: %s"),
2038 g_strerror (errsv));
2044 /* TODO: Maybe we should verify that you can delete the file from the trash
2045 before moving it? OTOH, that is hard, as it needs a recursive scan */
2047 trashfile = g_build_filename (filesdir, trashname, NULL);
2051 if (g_rename (local->filename, trashfile) == -1)
2061 /* The trash dir was actually on another fs anyway!?
2062 This can happen when the same device is mounted multiple
2063 times, or with bind mounts of the same fs. */
2064 g_set_error (error, G_IO_ERROR,
2065 G_IO_ERROR_NOT_SUPPORTED,
2066 _("Unable to trash file: %s"),
2067 g_strerror (errsv));
2069 g_set_error (error, G_IO_ERROR,
2070 g_io_error_from_errno (errsv),
2071 _("Unable to trash file: %s"),
2072 g_strerror (errsv));
2076 vfs = g_vfs_get_default ();
2077 class = G_VFS_GET_CLASS (vfs);
2078 if (class->local_file_moved)
2079 class->local_file_moved (vfs, local->filename, trashfile);
2083 /* TODO: Do we need to update mtime/atime here after the move? */
2085 /* Use absolute names for homedir */
2086 if (is_homedir_trash)
2087 original_name = g_strdup (local->filename);
2089 original_name = try_make_relative (local->filename, topdir);
2090 original_name_escaped = escape_trash_name (original_name);
2092 g_free (original_name);
2099 localtime_r (&t, &now);
2101 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
2104 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2105 original_name_escaped, delete_time);
2107 g_file_set_contents (infofile, data, -1, NULL);
2111 g_free (original_name_escaped);
2116 #else /* G_OS_WIN32 */
2118 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
2120 return FALSE; /* XXX ??? */
2124 g_local_file_trash (GFile *file,
2125 GCancellable *cancellable,
2128 GLocalFile *local = G_LOCAL_FILE (file);
2129 SHFILEOPSTRUCTW op = {0};
2134 wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
2135 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2136 wfilename = g_renew (wchar_t, wfilename, len + 2);
2137 wfilename[len + 1] = 0;
2139 op.wFunc = FO_DELETE;
2140 op.pFrom = wfilename;
2141 op.fFlags = FOF_ALLOWUNDO;
2143 success = SHFileOperationW (&op) == 0;
2145 if (success && op.fAnyOperationsAborted)
2147 if (cancellable && !g_cancellable_is_cancelled (cancellable))
2148 g_cancellable_cancel (cancellable);
2149 g_set_error (error, G_IO_ERROR,
2150 G_IO_ERROR_CANCELLED,
2151 _("Unable to trash file: %s"),
2152 _("Operation was cancelled"));
2156 g_set_error (error, G_IO_ERROR,
2158 _("Unable to trash file: %s"),
2159 _("internal error"));
2164 #endif /* G_OS_WIN32 */
2167 g_local_file_make_directory (GFile *file,
2168 GCancellable *cancellable,
2171 GLocalFile *local = G_LOCAL_FILE (file);
2173 if (g_mkdir (local->filename, 0777) == -1)
2177 if (errsv == EINVAL)
2178 /* This must be an invalid filename, on e.g. FAT */
2179 g_set_error_literal (error, G_IO_ERROR,
2180 G_IO_ERROR_INVALID_FILENAME,
2181 _("Invalid filename"));
2183 g_set_error (error, G_IO_ERROR,
2184 g_io_error_from_errno (errsv),
2185 _("Error creating directory: %s"),
2186 g_strerror (errsv));
2194 g_local_file_make_symbolic_link (GFile *file,
2195 const char *symlink_value,
2196 GCancellable *cancellable,
2200 GLocalFile *local = G_LOCAL_FILE (file);
2202 if (symlink (symlink_value, local->filename) == -1)
2206 if (errsv == EINVAL)
2207 /* This must be an invalid filename, on e.g. FAT */
2208 g_set_error_literal (error, G_IO_ERROR,
2209 G_IO_ERROR_INVALID_FILENAME,
2210 _("Invalid filename"));
2211 else if (errsv == EPERM)
2212 g_set_error (error, G_IO_ERROR,
2213 G_IO_ERROR_NOT_SUPPORTED,
2214 _("Filesystem does not support symbolic links"));
2216 g_set_error (error, G_IO_ERROR,
2217 g_io_error_from_errno (errsv),
2218 _("Error making symbolic link: %s"),
2219 g_strerror (errsv));
2224 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
2231 g_local_file_copy (GFile *source,
2233 GFileCopyFlags flags,
2234 GCancellable *cancellable,
2235 GFileProgressCallback progress_callback,
2236 gpointer progress_callback_data,
2239 /* Fall back to default copy */
2240 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
2245 g_local_file_move (GFile *source,
2247 GFileCopyFlags flags,
2248 GCancellable *cancellable,
2249 GFileProgressCallback progress_callback,
2250 gpointer progress_callback_data,
2253 GLocalFile *local_source, *local_destination;
2255 gboolean destination_exist, source_is_dir;
2262 if (!G_IS_LOCAL_FILE (source) ||
2263 !G_IS_LOCAL_FILE (destination))
2265 /* Fall back to default move */
2266 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2270 local_source = G_LOCAL_FILE (source);
2271 local_destination = G_LOCAL_FILE (destination);
2273 res = g_lstat (local_source->filename, &statbuf);
2278 g_set_error (error, G_IO_ERROR,
2279 g_io_error_from_errno (errsv),
2280 _("Error moving file: %s"),
2281 g_strerror (errsv));
2285 source_is_dir = S_ISDIR (statbuf.st_mode);
2286 source_size = statbuf.st_size;
2288 destination_exist = FALSE;
2289 res = g_lstat (local_destination->filename, &statbuf);
2292 destination_exist = TRUE; /* Target file exists */
2294 if (flags & G_FILE_COPY_OVERWRITE)
2296 /* Always fail on dirs, even with overwrite */
2297 if (S_ISDIR (statbuf.st_mode))
2300 g_set_error_literal (error,
2302 G_IO_ERROR_WOULD_MERGE,
2303 _("Can't move directory over directory"));
2305 g_set_error_literal (error,
2307 G_IO_ERROR_IS_DIRECTORY,
2308 _("Can't copy over directory"));
2314 g_set_error_literal (error,
2317 _("Target file exists"));
2322 if (flags & G_FILE_COPY_BACKUP && destination_exist)
2324 backup_name = g_strconcat (local_destination->filename, "~", NULL);
2325 if (g_rename (local_destination->filename, backup_name) == -1)
2327 g_set_error_literal (error,
2329 G_IO_ERROR_CANT_CREATE_BACKUP,
2330 _("Backup file creation failed"));
2331 g_free (backup_name);
2334 g_free (backup_name);
2335 destination_exist = FALSE; /* It did, but no more */
2338 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2340 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2341 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2342 res = g_unlink (local_destination->filename);
2347 g_set_error (error, G_IO_ERROR,
2348 g_io_error_from_errno (errsv),
2349 _("Error removing target file: %s"),
2350 g_strerror (errsv));
2355 if (g_rename (local_source->filename, local_destination->filename) == -1)
2360 /* This will cause the fallback code to run */
2361 g_set_error_literal (error, G_IO_ERROR,
2362 G_IO_ERROR_NOT_SUPPORTED,
2363 _("Move between mounts not supported"));
2364 else if (errsv == EINVAL)
2365 /* This must be an invalid filename, on e.g. FAT, or
2366 we're trying to move the file into itself...
2367 We return invalid filename for both... */
2368 g_set_error_literal (error, G_IO_ERROR,
2369 G_IO_ERROR_INVALID_FILENAME,
2370 _("Invalid filename"));
2372 g_set_error (error, G_IO_ERROR,
2373 g_io_error_from_errno (errsv),
2374 _("Error moving file: %s"),
2375 g_strerror (errsv));
2379 vfs = g_vfs_get_default ();
2380 class = G_VFS_GET_CLASS (vfs);
2381 if (class->local_file_moved)
2382 class->local_file_moved (vfs, local_source->filename, local_destination->filename);
2384 /* Make sure we send full copied size */
2385 if (progress_callback)
2386 progress_callback (source_size, source_size, progress_callback_data);
2391 static GFileMonitor*
2392 g_local_file_monitor_dir (GFile *file,
2393 GFileMonitorFlags flags,
2394 GCancellable *cancellable,
2397 GLocalFile* local_file = G_LOCAL_FILE(file);
2398 return _g_local_directory_monitor_new (local_file->filename, flags, error);
2401 static GFileMonitor*
2402 g_local_file_monitor_file (GFile *file,
2403 GFileMonitorFlags flags,
2404 GCancellable *cancellable,
2407 GLocalFile* local_file = G_LOCAL_FILE(file);
2408 return _g_local_file_monitor_new (local_file->filename, flags, error);
2412 g_local_file_file_iface_init (GFileIface *iface)
2414 iface->dup = g_local_file_dup;
2415 iface->hash = g_local_file_hash;
2416 iface->equal = g_local_file_equal;
2417 iface->is_native = g_local_file_is_native;
2418 iface->has_uri_scheme = g_local_file_has_uri_scheme;
2419 iface->get_uri_scheme = g_local_file_get_uri_scheme;
2420 iface->get_basename = g_local_file_get_basename;
2421 iface->get_path = g_local_file_get_path;
2422 iface->get_uri = g_local_file_get_uri;
2423 iface->get_parse_name = g_local_file_get_parse_name;
2424 iface->get_parent = g_local_file_get_parent;
2425 iface->prefix_matches = g_local_file_prefix_matches;
2426 iface->get_relative_path = g_local_file_get_relative_path;
2427 iface->resolve_relative_path = g_local_file_resolve_relative_path;
2428 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2429 iface->set_display_name = g_local_file_set_display_name;
2430 iface->enumerate_children = g_local_file_enumerate_children;
2431 iface->query_info = g_local_file_query_info;
2432 iface->query_filesystem_info = g_local_file_query_filesystem_info;
2433 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2434 iface->query_settable_attributes = g_local_file_query_settable_attributes;
2435 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2436 iface->set_attribute = g_local_file_set_attribute;
2437 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2438 iface->read_fn = g_local_file_read;
2439 iface->append_to = g_local_file_append_to;
2440 iface->create = g_local_file_create;
2441 iface->replace = g_local_file_replace;
2442 iface->open_readwrite = g_local_file_open_readwrite;
2443 iface->create_readwrite = g_local_file_create_readwrite;
2444 iface->replace_readwrite = g_local_file_replace_readwrite;
2445 iface->delete_file = g_local_file_delete;
2446 iface->trash = g_local_file_trash;
2447 iface->make_directory = g_local_file_make_directory;
2448 iface->make_symbolic_link = g_local_file_make_symbolic_link;
2449 iface->copy = g_local_file_copy;
2450 iface->move = g_local_file_move;
2451 iface->monitor_dir = g_local_file_monitor_dir;
2452 iface->monitor_file = g_local_file_monitor_file;
2454 iface->supports_thread_contexts = TRUE;