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(sun) && defined(__SVR4)
57 /* on solaris, 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 "glocalfile.h"
76 #include "glocalfileinfo.h"
77 #include "glocalfileenumerator.h"
78 #include "glocalfileinputstream.h"
79 #include "glocalfileoutputstream.h"
80 #include "glocaldirectorymonitor.h"
81 #include "glocalfilemonitor.h"
82 #include "gmountprivate.h"
83 #include <glib/gstdio.h>
87 #define _WIN32_WINNT 0x0500
93 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
96 #define S_ISLNK(m) (0)
100 #include "gioalias.h"
102 static void g_local_file_file_iface_init (GFileIface *iface);
104 static GFileAttributeInfoList *local_writable_attributes = NULL;
105 static GFileAttributeInfoList *local_writable_namespaces = NULL;
109 GObject parent_instance;
114 #define g_local_file_get_type _g_local_file_get_type
115 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
116 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
117 g_local_file_file_iface_init))
119 static char *find_mountpoint_for (const char *file, dev_t dev);
122 g_local_file_finalize (GObject *object)
126 local = G_LOCAL_FILE (object);
128 g_free (local->filename);
130 if (G_OBJECT_CLASS (g_local_file_parent_class)->finalize)
131 (*G_OBJECT_CLASS (g_local_file_parent_class)->finalize) (object);
135 g_local_file_class_init (GLocalFileClass *klass)
137 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
138 GFileAttributeInfoList *list;
140 gobject_class->finalize = g_local_file_finalize;
142 /* Set up attribute lists */
144 /* Writable attributes: */
146 list = g_file_attribute_info_list_new ();
148 g_file_attribute_info_list_add (list,
149 G_FILE_ATTRIBUTE_UNIX_MODE,
150 G_FILE_ATTRIBUTE_TYPE_UINT32,
151 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
152 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
155 g_file_attribute_info_list_add (list,
156 G_FILE_ATTRIBUTE_UNIX_UID,
157 G_FILE_ATTRIBUTE_TYPE_UINT32,
158 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
159 g_file_attribute_info_list_add (list,
160 G_FILE_ATTRIBUTE_UNIX_GID,
161 G_FILE_ATTRIBUTE_TYPE_UINT32,
162 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
166 g_file_attribute_info_list_add (list,
167 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
168 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
173 g_file_attribute_info_list_add (list,
174 G_FILE_ATTRIBUTE_TIME_MODIFIED,
175 G_FILE_ATTRIBUTE_TYPE_UINT64,
176 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
177 g_file_attribute_info_list_add (list,
178 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
179 G_FILE_ATTRIBUTE_TYPE_UINT32,
180 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
181 g_file_attribute_info_list_add (list,
182 G_FILE_ATTRIBUTE_TIME_ACCESS,
183 G_FILE_ATTRIBUTE_TYPE_UINT64,
184 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
185 g_file_attribute_info_list_add (list,
186 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
187 G_FILE_ATTRIBUTE_TYPE_UINT32,
188 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
191 local_writable_attributes = list;
193 /* Writable namespaces: */
195 list = g_file_attribute_info_list_new ();
198 g_file_attribute_info_list_add (list,
200 G_FILE_ATTRIBUTE_TYPE_STRING,
201 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
202 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
203 g_file_attribute_info_list_add (list,
205 G_FILE_ATTRIBUTE_TYPE_STRING,
206 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
209 local_writable_namespaces = list;
213 g_local_file_init (GLocalFile *local)
219 canonicalize_filename (const char *filename)
221 char *canon, *start, *p, *q;
225 if (!g_path_is_absolute (filename))
227 cwd = g_get_current_dir ();
228 canon = g_build_filename (cwd, filename, NULL);
232 canon = g_strdup (filename);
234 start = (char *)g_path_skip_root (canon);
236 /* POSIX allows double slashes at the start to
237 * mean something special (as does windows too).
238 * So, "//" != "/", but more than two slashes
244 G_IS_DIR_SEPARATOR (*p);
251 memmove (start, start+i, strlen (start+i)+1);
257 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
259 memmove (p, p+1, strlen (p+1)+1);
261 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
264 /* Skip previous separator */
268 while (p > start && !G_IS_DIR_SEPARATOR (*p))
270 if (G_IS_DIR_SEPARATOR (*p))
271 *p++ = G_DIR_SEPARATOR;
272 memmove (p, q, strlen (q)+1);
276 /* Skip until next separator */
277 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
282 /* Canonicalize one separator */
283 *p++ = G_DIR_SEPARATOR;
287 /* Remove additional separators */
289 while (*q && G_IS_DIR_SEPARATOR (*q))
293 memmove (p, q, strlen (q)+1);
296 /* Remove trailing slashes */
297 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
305 * @filename: filename of the file to create.
307 * Returns: new local #GFile.
310 _g_local_file_new (const char *filename)
314 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
315 local->filename = canonicalize_filename (filename);
317 return G_FILE (local);
321 g_local_file_is_native (GFile *file)
327 g_local_file_has_uri_scheme (GFile *file,
328 const char *uri_scheme)
330 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
334 g_local_file_get_uri_scheme (GFile *file)
336 return g_strdup ("file");
340 g_local_file_get_basename (GFile *file)
342 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
346 g_local_file_get_path (GFile *file)
348 return g_strdup (G_LOCAL_FILE (file)->filename);
352 g_local_file_get_uri (GFile *file)
354 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
358 get_filename_charset (const gchar **filename_charset)
360 const gchar **charsets;
363 is_utf8 = g_get_filename_charsets (&charsets);
365 if (filename_charset)
366 *filename_charset = charsets[0];
372 name_is_valid_for_display (const char *string,
373 gboolean is_valid_utf8)
377 if (!is_valid_utf8 &&
378 !g_utf8_validate (string, -1, NULL))
381 while ((c = *string++) != 0)
383 if (g_ascii_iscntrl (c))
391 g_local_file_get_parse_name (GFile *file)
393 const char *filename;
395 const gchar *charset;
397 char *roundtripped_filename;
398 gboolean free_utf8_filename;
399 gboolean is_valid_utf8;
402 filename = G_LOCAL_FILE (file)->filename;
403 if (get_filename_charset (&charset))
405 utf8_filename = (char *)filename;
406 free_utf8_filename = FALSE;
407 is_valid_utf8 = FALSE; /* Can't guarantee this */
411 utf8_filename = g_convert (filename, -1,
412 "UTF-8", charset, NULL, NULL, NULL);
413 free_utf8_filename = TRUE;
414 is_valid_utf8 = TRUE;
416 if (utf8_filename != NULL)
418 /* Make sure we can roundtrip: */
419 roundtripped_filename = g_convert (utf8_filename, -1,
420 charset, "UTF-8", NULL, NULL, NULL);
422 if (roundtripped_filename == NULL ||
423 strcmp (utf8_filename, roundtripped_filename) != 0)
425 g_free (utf8_filename);
426 utf8_filename = NULL;
431 if (utf8_filename != NULL &&
432 name_is_valid_for_display (utf8_filename, is_valid_utf8))
434 if (free_utf8_filename)
435 parse_name = utf8_filename;
437 parse_name = g_strdup (utf8_filename);
441 escaped_path = g_uri_escape_string (filename,
442 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
444 parse_name = g_strconcat ("file://",
445 (*escaped_path != '/') ? "/" : "",
449 g_free (escaped_path);
451 if (free_utf8_filename)
452 g_free (utf8_filename);
459 g_local_file_get_parent (GFile *file)
461 GLocalFile *local = G_LOCAL_FILE (file);
462 const char *non_root;
467 non_root = g_path_skip_root (local->filename);
471 dirname = g_path_get_dirname (local->filename);
472 parent = _g_local_file_new (dirname);
478 g_local_file_dup (GFile *file)
480 GLocalFile *local = G_LOCAL_FILE (file);
482 return _g_local_file_new (local->filename);
486 g_local_file_hash (GFile *file)
488 GLocalFile *local = G_LOCAL_FILE (file);
490 return g_str_hash (local->filename);
494 g_local_file_equal (GFile *file1,
497 GLocalFile *local1 = G_LOCAL_FILE (file1);
498 GLocalFile *local2 = G_LOCAL_FILE (file2);
500 return g_str_equal (local1->filename, local2->filename);
504 match_prefix (const char *path,
509 prefix_len = strlen (prefix);
510 if (strncmp (path, prefix, prefix_len) != 0)
513 /* Handle the case where prefix is the root, so that
514 * the IS_DIR_SEPRARATOR check below works */
515 if (prefix_len > 0 &&
516 G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
519 return path + prefix_len;
523 g_local_file_prefix_matches (GFile *parent,
526 GLocalFile *parent_local = G_LOCAL_FILE (parent);
527 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
528 const char *remainder;
530 remainder = match_prefix (descendant_local->filename, parent_local->filename);
531 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
537 g_local_file_get_relative_path (GFile *parent,
540 GLocalFile *parent_local = G_LOCAL_FILE (parent);
541 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
542 const char *remainder;
544 remainder = match_prefix (descendant_local->filename, parent_local->filename);
546 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
547 return g_strdup (remainder + 1);
552 g_local_file_resolve_relative_path (GFile *file,
553 const char *relative_path)
555 GLocalFile *local = G_LOCAL_FILE (file);
559 if (g_path_is_absolute (relative_path))
560 return _g_local_file_new (relative_path);
562 filename = g_build_filename (local->filename, relative_path, NULL);
563 child = _g_local_file_new (filename);
569 static GFileEnumerator *
570 g_local_file_enumerate_children (GFile *file,
571 const char *attributes,
572 GFileQueryInfoFlags flags,
573 GCancellable *cancellable,
576 GLocalFile *local = G_LOCAL_FILE (file);
577 return _g_local_file_enumerator_new (local->filename,
583 g_local_file_get_child_for_display_name (GFile *file,
584 const char *display_name,
587 GFile *parent, *new_file;
590 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
591 if (basename == NULL)
593 g_set_error (error, G_IO_ERROR,
594 G_IO_ERROR_INVALID_FILENAME,
595 _("Invalid filename %s"), display_name);
599 parent = g_file_get_parent (file);
600 new_file = g_file_get_child (file, basename);
601 g_object_unref (parent);
609 get_fs_type (long f_type)
612 /* filesystem ids taken from linux manpage */
709 G_LOCK_DEFINE_STATIC(mount_info_hash);
710 static GHashTable *mount_info_hash = NULL;
711 static guint64 mount_info_hash_cache_time = 0;
714 MOUNT_INFO_READONLY = 1<<0
718 device_equal (gconstpointer v1,
721 return *(dev_t *)v1 == *(dev_t *)v2;
725 device_hash (gconstpointer v)
727 return (guint) *(dev_t *)v;
731 get_mount_info (GFileInfo *fs_info,
733 GFileAttributeMatcher *matcher)
737 gpointer info_as_ptr;
741 GUnixMountEntry *mount;
744 if (g_lstat (path, &buf) != 0)
747 G_LOCK (mount_info_hash);
749 if (mount_info_hash == NULL)
750 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
754 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
755 g_hash_table_remove_all (mount_info_hash);
757 got_info = g_hash_table_lookup_extended (mount_info_hash,
762 G_UNLOCK (mount_info_hash);
764 mount_info = GPOINTER_TO_UINT (info_as_ptr);
770 mountpoint = find_mountpoint_for (path, buf.st_dev);
771 if (mountpoint == NULL)
774 mount = g_unix_mount_at (mountpoint, &cache_time);
777 if (g_unix_mount_is_readonly (mount))
778 mount_info |= MOUNT_INFO_READONLY;
780 g_unix_mount_free (mount);
783 dev = g_new0 (dev_t, 1);
786 G_LOCK (mount_info_hash);
787 mount_info_hash_cache_time = cache_time;
788 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
789 G_UNLOCK (mount_info_hash);
792 if (mount_info & MOUNT_INFO_READONLY)
793 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
801 is_xp_or_later (void)
803 static int result = -1;
807 OSVERSIONINFOEX ver_info = {0};
808 DWORDLONG cond_mask = 0;
809 int op = VER_GREATER_EQUAL;
811 ver_info.dwOSVersionInfoSize = sizeof ver_info;
812 ver_info.dwMajorVersion = 5;
813 ver_info.dwMinorVersion = 1;
815 VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
816 VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
818 result = VerifyVersionInfo (&ver_info,
819 VER_MAJORVERSION | VER_MINORVERSION,
827 get_volume_for_path (const char *path)
833 wpath = g_utf8_to_utf16 (path, -1, NULL, &len, NULL);
834 result = g_new (wchar_t, len + 2);
836 if (!GetVolumePathNameW (wpath, result, len + 2))
838 char *msg = g_win32_error_message (GetLastError ());
839 g_critical ("GetVolumePathName failed: %s", msg);
846 len = wcslen (result);
847 if (len > 0 && result[len-1] != L'\\')
849 result = g_renew (wchar_t, result, len + 2);
859 find_mountpoint_for (const char *file, dev_t dev)
864 wpath = get_volume_for_path (file);
868 utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
875 get_filesystem_readonly (GFileInfo *info,
880 rootdir = get_volume_for_path (path);
884 if (is_xp_or_later ())
887 if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
888 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
889 (flags & FILE_READ_ONLY_VOLUME) != 0);
893 if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
894 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
901 #endif /* G_OS_WIN32 */
904 g_local_file_query_filesystem_info (GFile *file,
905 const char *attributes,
906 GCancellable *cancellable,
909 GLocalFile *local = G_LOCAL_FILE (file);
916 struct statfs statfs_buffer;
918 #elif defined(USE_STATVFS)
919 struct statvfs statfs_buffer;
922 GFileAttributeMatcher *attribute_matcher;
929 statfs_result = statfs (local->filename, &statfs_buffer);
930 #elif STATFS_ARGS == 4
931 statfs_result = statfs (local->filename, &statfs_buffer,
932 sizeof (statfs_buffer), 0);
934 block_size = statfs_buffer.f_bsize;
936 #if defined(__linux__)
937 /* ncpfs does not know the amount of available and free space *
938 * assuming ncpfs is linux specific, if you are on a non-linux platform
939 * where ncpfs is available, please file a bug about it on bugzilla.gnome.org
941 if (statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0 &&
942 /* linux/ncp_fs.h: NCP_SUPER_MAGIC == 0x564c */
943 statfs_buffer.f_type == 0x564c)
947 #elif defined(USE_STATVFS)
948 statfs_result = statvfs (local->filename, &statfs_buffer);
949 block_size = statfs_buffer.f_frsize;
952 if (statfs_result == -1)
956 g_set_error (error, G_IO_ERROR,
957 g_io_error_from_errno (errsv),
958 _("Error getting filesystem info: %s"),
963 info = g_file_info_new ();
965 attribute_matcher = g_file_attribute_matcher_new (attributes);
967 if (g_file_attribute_matcher_matches (attribute_matcher,
968 G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
971 gchar *localdir = g_path_get_dirname (local->filename);
972 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
976 if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
977 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
980 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
983 if (g_file_attribute_matcher_matches (attribute_matcher,
984 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
987 gchar *localdir = g_path_get_dirname (local->filename);
988 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
992 if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
993 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, (guint64)li.QuadPart);
996 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
1000 fstype = get_fs_type (statfs_buffer.f_type);
1002 g_file_attribute_matcher_matches (attribute_matcher,
1003 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1004 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1007 if (g_file_attribute_matcher_matches (attribute_matcher,
1008 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
1011 get_filesystem_readonly (info, local->filename);
1013 get_mount_info (info, local->filename, attribute_matcher);
1017 g_file_attribute_matcher_unref (attribute_matcher);
1023 g_local_file_find_enclosing_mount (GFile *file,
1024 GCancellable *cancellable,
1027 GLocalFile *local = G_LOCAL_FILE (file);
1032 if (g_lstat (local->filename, &buf) != 0)
1034 /* Translators: This is an error message when trying to find the
1035 enclosing (user visible) mount of a file, but none exists. */
1036 g_set_error (error, G_IO_ERROR,
1037 G_IO_ERROR_NOT_FOUND,
1038 _("Containing mount does not exist"));
1042 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
1043 if (mountpoint == NULL)
1045 /* Translators: This is an error message when trying to find the
1046 enclosing (user visible) mount of a file, but none exists. */
1047 g_set_error (error, G_IO_ERROR,
1048 G_IO_ERROR_NOT_FOUND,
1049 _("Containing mount does not exist"));
1053 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1054 g_free (mountpoint);
1058 /* Translators: This is an error message when trying to find the
1059 enclosing (user visible) mount of a file, but none exists. */
1060 g_set_error (error, G_IO_ERROR,
1061 G_IO_ERROR_NOT_FOUND,
1062 _("Containing mount does not exist"));
1067 g_local_file_set_display_name (GFile *file,
1068 const char *display_name,
1069 GCancellable *cancellable,
1072 GLocalFile *local, *new_local;
1073 GFile *new_file, *parent;
1074 struct stat statbuf;
1077 parent = g_file_get_parent (file);
1080 g_set_error (error, G_IO_ERROR,
1082 _("Can't rename root directory"));
1086 new_file = g_file_get_child_for_display_name (parent, display_name, error);
1087 g_object_unref (parent);
1089 if (new_file == NULL)
1092 local = G_LOCAL_FILE (file);
1093 new_local = G_LOCAL_FILE (new_file);
1095 if (!(g_lstat (new_local->filename, &statbuf) == -1 &&
1098 g_set_error (error, G_IO_ERROR,
1100 _("Can't rename file, filename already exist"));
1104 if (g_rename (local->filename, new_local->filename) == -1)
1108 if (errsv == EINVAL)
1109 /* We can't get a rename file into itself error herer,
1110 so this must be an invalid filename, on e.g. FAT */
1111 g_set_error (error, G_IO_ERROR,
1112 G_IO_ERROR_INVALID_FILENAME,
1113 _("Invalid filename"));
1115 g_set_error (error, G_IO_ERROR,
1116 g_io_error_from_errno (errsv),
1117 _("Error renaming file: %s"),
1118 g_strerror (errsv));
1119 g_object_unref (new_file);
1127 g_local_file_query_info (GFile *file,
1128 const char *attributes,
1129 GFileQueryInfoFlags flags,
1130 GCancellable *cancellable,
1133 GLocalFile *local = G_LOCAL_FILE (file);
1135 GFileAttributeMatcher *matcher;
1136 char *basename, *dirname;
1137 GLocalParentFileInfo parent_info;
1139 matcher = g_file_attribute_matcher_new (attributes);
1141 basename = g_path_get_basename (local->filename);
1143 dirname = g_path_get_dirname (local->filename);
1144 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1147 info = _g_local_file_info_get (basename, local->filename,
1148 matcher, flags, &parent_info,
1153 g_file_attribute_matcher_unref (matcher);
1158 static GFileAttributeInfoList *
1159 g_local_file_query_settable_attributes (GFile *file,
1160 GCancellable *cancellable,
1163 return g_file_attribute_info_list_ref (local_writable_attributes);
1166 static GFileAttributeInfoList *
1167 g_local_file_query_writable_namespaces (GFile *file,
1168 GCancellable *cancellable,
1171 return g_file_attribute_info_list_ref (local_writable_namespaces);
1175 g_local_file_set_attribute (GFile *file,
1176 const char *attribute,
1177 GFileAttributeType type,
1179 GFileQueryInfoFlags flags,
1180 GCancellable *cancellable,
1183 GLocalFile *local = G_LOCAL_FILE (file);
1185 return _g_local_file_info_set_attribute (local->filename,
1195 g_local_file_set_attributes_from_info (GFile *file,
1197 GFileQueryInfoFlags flags,
1198 GCancellable *cancellable,
1201 GLocalFile *local = G_LOCAL_FILE (file);
1202 int res, chained_res;
1203 GFileIface *default_iface;
1205 res = _g_local_file_info_set_attributes (local->filename,
1211 error = NULL; /* Don't write over error if further errors */
1213 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1215 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1217 return res && chained_res;
1220 static GFileInputStream *
1221 g_local_file_read (GFile *file,
1222 GCancellable *cancellable,
1225 GLocalFile *local = G_LOCAL_FILE (file);
1229 fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1234 g_set_error (error, G_IO_ERROR,
1235 g_io_error_from_errno (errsv),
1236 _("Error opening file: %s"),
1237 g_strerror (errsv));
1241 if (fstat(fd, &buf) == 0 && S_ISDIR (buf.st_mode))
1244 g_set_error (error, G_IO_ERROR,
1245 G_IO_ERROR_IS_DIRECTORY,
1246 _("Can't open directory"));
1250 return _g_local_file_input_stream_new (fd);
1253 static GFileOutputStream *
1254 g_local_file_append_to (GFile *file,
1255 GFileCreateFlags flags,
1256 GCancellable *cancellable,
1259 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1260 flags, cancellable, error);
1263 static GFileOutputStream *
1264 g_local_file_create (GFile *file,
1265 GFileCreateFlags flags,
1266 GCancellable *cancellable,
1269 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1270 flags, cancellable, error);
1273 static GFileOutputStream *
1274 g_local_file_replace (GFile *file,
1276 gboolean make_backup,
1277 GFileCreateFlags flags,
1278 GCancellable *cancellable,
1281 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1282 etag, make_backup, flags,
1283 cancellable, error);
1288 g_local_file_delete (GFile *file,
1289 GCancellable *cancellable,
1292 GLocalFile *local = G_LOCAL_FILE (file);
1294 if (g_remove (local->filename) == -1)
1298 g_set_error (error, G_IO_ERROR,
1299 g_io_error_from_errno (errsv),
1300 _("Error removing file: %s"),
1301 g_strerror (errsv));
1309 strip_trailing_slashes (const char *path)
1314 path_copy = g_strdup (path);
1315 len = strlen (path_copy);
1316 while (len > 1 && path_copy[len-1] == '/')
1317 path_copy[--len] = 0;
1323 expand_symlink (const char *link)
1325 char *resolved, *canonical, *parent, *link2;
1326 char symlink_value[4096];
1334 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1337 return g_strdup (link);
1338 symlink_value[res] = 0;
1341 if (g_path_is_absolute (symlink_value))
1342 return canonicalize_filename (symlink_value);
1345 link2 = strip_trailing_slashes (link);
1346 parent = g_path_get_dirname (link2);
1349 resolved = g_build_filename (parent, symlink_value, NULL);
1352 canonical = canonicalize_filename (resolved);
1361 get_parent (const char *path,
1365 struct stat parent_stat;
1369 path_copy = strip_trailing_slashes (path);
1371 parent = g_path_get_dirname (path_copy);
1372 if (strcmp (parent, ".") == 0 ||
1373 strcmp (parent, path_copy) == 0)
1382 if (g_lstat (parent, &parent_stat) != 0)
1388 if (S_ISLNK (parent_stat.st_mode))
1391 parent = expand_symlink (parent);
1396 if (num_recursions > 12)
1401 } while (S_ISLNK (parent_stat.st_mode));
1403 *parent_dev = parent_stat.st_dev;
1409 expand_all_symlinks (const char *path)
1411 char *parent, *parent_expanded;
1412 char *basename, *res;
1415 parent = get_parent (path, &parent_dev);
1418 parent_expanded = expand_all_symlinks (parent);
1420 basename = g_path_get_basename (path);
1421 res = g_build_filename (parent_expanded, basename, NULL);
1423 g_free (parent_expanded);
1426 res = g_strdup (path);
1434 find_mountpoint_for (const char *file,
1438 dev_t dir_dev, parent_dev;
1440 dir = g_strdup (file);
1445 parent = get_parent (dir, &parent_dev);
1449 if (parent_dev != dir_dev)
1461 find_topdir_for (const char *file)
1466 dir = get_parent (file, &dir_dev);
1470 return find_mountpoint_for (dir, dir_dev);
1474 get_unique_filename (const char *basename,
1480 return g_strdup (basename);
1482 dot = strchr (basename, '.');
1484 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1486 return g_strdup_printf ("%s.%d", basename, id);
1490 path_has_prefix (const char *path,
1498 prefix_len = strlen (prefix);
1500 if (strncmp (path, prefix, prefix_len) == 0 &&
1501 (prefix_len == 0 || /* empty prefix always matches */
1502 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1503 path[prefix_len] == 0 ||
1504 path[prefix_len] == '/'))
1511 try_make_relative (const char *path,
1514 char *path2, *base2;
1517 path2 = expand_all_symlinks (path);
1518 base2 = expand_all_symlinks (base);
1521 if (path_has_prefix (path2, base2))
1523 relative = path2 + strlen (base2);
1524 while (*relative == '/')
1526 relative = g_strdup (relative);
1534 /* Failed, use abs path */
1535 return g_strdup (path);
1539 escape_trash_name (char *name)
1542 const gchar hex[16] = "0123456789ABCDEF";
1544 str = g_string_new ("");
1552 if (g_ascii_isprint (c))
1553 g_string_append_c (str, c);
1556 g_string_append_c (str, '%');
1557 g_string_append_c (str, hex[((guchar)c) >> 4]);
1558 g_string_append_c (str, hex[((guchar)c) & 0xf]);
1562 return g_string_free (str, FALSE);
1566 g_local_file_trash (GFile *file,
1567 GCancellable *cancellable,
1570 GLocalFile *local = G_LOCAL_FILE (file);
1571 struct stat file_stat, home_stat;
1572 const char *homedir;
1573 char *trashdir, *topdir, *infodir, *filesdir;
1574 char *basename, *trashname, *trashfile, *infoname, *infofile;
1575 char *original_name, *original_name_escaped;
1578 gboolean is_homedir_trash;
1579 char delete_time[32];
1581 struct stat trash_stat, global_stat;
1582 char *dirname, *globaldir;
1584 if (g_lstat (local->filename, &file_stat) != 0)
1588 g_set_error (error, G_IO_ERROR,
1589 g_io_error_from_errno (errsv),
1590 _("Error trashing file: %s"),
1591 g_strerror (errsv));
1595 homedir = g_get_home_dir ();
1596 g_stat (homedir, &home_stat);
1598 is_homedir_trash = FALSE;
1600 if (file_stat.st_dev == home_stat.st_dev)
1602 is_homedir_trash = TRUE;
1604 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1605 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1610 display_name = g_filename_display_name (trashdir);
1611 g_set_error (error, G_IO_ERROR,
1612 g_io_error_from_errno (errsv),
1613 _("Unable to create trash dir %s: %s"),
1614 display_name, g_strerror (errsv));
1615 g_free (display_name);
1619 topdir = g_strdup (g_get_user_data_dir ());
1627 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1629 topdir = find_topdir_for (local->filename);
1632 g_set_error (error, G_IO_ERROR,
1633 G_IO_ERROR_NOT_SUPPORTED,
1634 _("Unable to find toplevel directory for trash"));
1638 /* Try looking for global trash dir $topdir/.Trash/$uid */
1639 globaldir = g_build_filename (topdir, ".Trash", NULL);
1640 if (g_lstat (globaldir, &global_stat) == 0 &&
1641 S_ISDIR (global_stat.st_mode) &&
1642 (global_stat.st_mode & S_ISVTX) != 0)
1644 trashdir = g_build_filename (globaldir, uid_str, NULL);
1646 if (g_lstat (trashdir, &trash_stat) == 0)
1648 if (!S_ISDIR (trash_stat.st_mode) ||
1649 trash_stat.st_uid != uid)
1651 /* Not a directory or not owned by user, ignore */
1656 else if (g_mkdir (trashdir, 0700) == -1)
1664 if (trashdir == NULL)
1666 gboolean tried_create;
1668 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1669 dirname = g_strdup_printf (".Trash-%s", uid_str);
1670 trashdir = g_build_filename (topdir, dirname, NULL);
1673 tried_create = FALSE;
1676 if (g_lstat (trashdir, &trash_stat) == 0)
1678 if (!S_ISDIR (trash_stat.st_mode) ||
1679 trash_stat.st_uid != uid)
1681 /* Remove the failed directory */
1683 g_remove (trashdir);
1685 /* Not a directory or not owned by user, ignore */
1692 if (!tried_create &&
1693 g_mkdir (trashdir, 0700) != -1)
1695 /* Ensure that the created dir has the right uid etc.
1696 This might fail on e.g. a FAT dir */
1697 tried_create = TRUE;
1708 if (trashdir == NULL)
1711 g_set_error (error, G_IO_ERROR,
1712 G_IO_ERROR_NOT_SUPPORTED,
1713 _("Unable to find or create trash directory"));
1718 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1720 infodir = g_build_filename (trashdir, "info", NULL);
1721 filesdir = g_build_filename (trashdir, "files", NULL);
1724 /* Make sure we have the subdirectories */
1725 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1726 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
1731 g_set_error (error, G_IO_ERROR,
1732 G_IO_ERROR_NOT_SUPPORTED,
1733 _("Unable to find or create trash directory"));
1737 basename = g_path_get_basename (local->filename);
1745 trashname = get_unique_filename (basename, i++);
1746 infoname = g_strconcat (trashname, ".trashinfo", NULL);
1747 infofile = g_build_filename (infodir, infoname, NULL);
1750 fd = open (infofile, O_CREAT | O_EXCL, 0666);
1751 } while (fd == -1 && errno == EEXIST);
1765 g_set_error (error, G_IO_ERROR,
1766 g_io_error_from_errno (errsv),
1767 _("Unable to create trashing info file: %s"),
1768 g_strerror (errsv));
1774 /* TODO: Maybe we should verify that you can delete the file from the trash
1775 before moving it? OTOH, that is hard, as it needs a recursive scan */
1777 trashfile = g_build_filename (filesdir, trashname, NULL);
1781 if (g_rename (local->filename, trashfile) == -1)
1790 g_set_error (error, G_IO_ERROR,
1791 g_io_error_from_errno (errsv),
1792 _("Unable to trash file: %s"),
1793 g_strerror (errsv));
1799 /* TODO: Do we need to update mtime/atime here after the move? */
1801 /* Use absolute names for homedir */
1802 if (is_homedir_trash)
1803 original_name = g_strdup (local->filename);
1805 original_name = try_make_relative (local->filename, topdir);
1806 original_name_escaped = escape_trash_name (original_name);
1808 g_free (original_name);
1815 localtime_r (&t, &now);
1817 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
1820 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
1821 original_name_escaped, delete_time);
1823 g_file_set_contents (infofile, data, -1, NULL);
1827 g_free (original_name_escaped);
1832 #else /* G_OS_WIN32 */
1834 g_local_file_trash (GFile *file,
1835 GCancellable *cancellable,
1838 GLocalFile *local = G_LOCAL_FILE (file);
1839 SHFILEOPSTRUCTW op = {0};
1844 wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
1845 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
1846 wfilename = g_renew (wchar_t, wfilename, len + 2);
1847 wfilename[len + 1] = 0;
1849 op.wFunc = FO_DELETE;
1850 op.pFrom = wfilename;
1851 op.fFlags = FOF_ALLOWUNDO;
1853 success = SHFileOperationW (&op) == 0;
1855 if (success && op.fAnyOperationsAborted)
1857 if (cancellable && !g_cancellable_is_cancelled (cancellable))
1858 g_cancellable_cancel (cancellable);
1859 g_set_error (error, G_IO_ERROR,
1860 G_IO_ERROR_CANCELLED,
1861 _("Unable to trash file: %s"),
1862 _("Operation was cancelled"));
1866 g_set_error (error, G_IO_ERROR,
1868 _("Unable to trash file: %s"),
1869 _("internal error"));
1874 #endif /* G_OS_WIN32 */
1877 g_local_file_make_directory (GFile *file,
1878 GCancellable *cancellable,
1881 GLocalFile *local = G_LOCAL_FILE (file);
1883 if (g_mkdir (local->filename, 0755) == -1)
1887 if (errsv == EINVAL)
1888 /* This must be an invalid filename, on e.g. FAT */
1889 g_set_error (error, G_IO_ERROR,
1890 G_IO_ERROR_INVALID_FILENAME,
1891 _("Invalid filename"));
1893 g_set_error (error, G_IO_ERROR,
1894 g_io_error_from_errno (errsv),
1895 _("Error removing file: %s"),
1896 g_strerror (errsv));
1904 g_local_file_make_symbolic_link (GFile *file,
1905 const char *symlink_value,
1906 GCancellable *cancellable,
1910 GLocalFile *local = G_LOCAL_FILE (file);
1912 if (symlink (symlink_value, local->filename) == -1)
1916 if (errsv == EINVAL)
1917 /* This must be an invalid filename, on e.g. FAT */
1918 g_set_error (error, G_IO_ERROR,
1919 G_IO_ERROR_INVALID_FILENAME,
1920 _("Invalid filename"));
1922 g_set_error (error, G_IO_ERROR,
1923 g_io_error_from_errno (errsv),
1924 _("Error making symbolic link: %s"),
1925 g_strerror (errsv));
1930 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
1937 g_local_file_copy (GFile *source,
1939 GFileCopyFlags flags,
1940 GCancellable *cancellable,
1941 GFileProgressCallback progress_callback,
1942 gpointer progress_callback_data,
1945 /* Fall back to default copy */
1946 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
1951 g_local_file_move (GFile *source,
1953 GFileCopyFlags flags,
1954 GCancellable *cancellable,
1955 GFileProgressCallback progress_callback,
1956 gpointer progress_callback_data,
1959 GLocalFile *local_source, *local_destination;
1960 struct stat statbuf;
1961 gboolean destination_exist, source_is_dir;
1966 if (!G_IS_LOCAL_FILE (source) ||
1967 !G_IS_LOCAL_FILE (destination))
1969 /* Fall back to default move */
1970 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
1974 local_source = G_LOCAL_FILE (source);
1975 local_destination = G_LOCAL_FILE (destination);
1977 res = g_lstat (local_source->filename, &statbuf);
1982 g_set_error (error, G_IO_ERROR,
1983 g_io_error_from_errno (errsv),
1984 _("Error moving file: %s"),
1985 g_strerror (errsv));
1989 source_is_dir = S_ISDIR (statbuf.st_mode);
1991 source_size = statbuf.st_size;
1993 destination_exist = FALSE;
1994 res = g_lstat (local_destination->filename, &statbuf);
1997 destination_exist = TRUE; /* Target file exists */
1999 if (flags & G_FILE_COPY_OVERWRITE)
2001 /* Always fail on dirs, even with overwrite */
2002 if (S_ISDIR (statbuf.st_mode))
2006 G_IO_ERROR_WOULD_MERGE,
2007 _("Can't move directory over directory"));
2016 _("Target file exists"));
2021 if (flags & G_FILE_COPY_BACKUP && destination_exist)
2023 backup_name = g_strconcat (local_destination->filename, "~", NULL);
2024 if (g_rename (local_destination->filename, backup_name) == -1)
2028 G_IO_ERROR_CANT_CREATE_BACKUP,
2029 _("Backup file creation failed"));
2030 g_free (backup_name);
2033 g_free (backup_name);
2034 destination_exist = FALSE; /* It did, but no more */
2037 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2039 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2040 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2041 res = g_unlink (local_destination->filename);
2046 g_set_error (error, G_IO_ERROR,
2047 g_io_error_from_errno (errsv),
2048 _("Error removing target file: %s"),
2049 g_strerror (errsv));
2054 if (g_rename (local_source->filename, local_destination->filename) == -1)
2059 /* This will cause the fallback code to run */
2060 g_set_error (error, G_IO_ERROR,
2061 G_IO_ERROR_NOT_SUPPORTED,
2062 _("Move between mounts not supported"));
2063 else if (errsv == EINVAL)
2064 /* This must be an invalid filename, on e.g. FAT, or
2065 we're trying to move the file into itself...
2066 We return invalid filename for both... */
2067 g_set_error (error, G_IO_ERROR,
2068 G_IO_ERROR_INVALID_FILENAME,
2069 _("Invalid filename"));
2071 g_set_error (error, G_IO_ERROR,
2072 g_io_error_from_errno (errsv),
2073 _("Error moving file: %s"),
2074 g_strerror (errsv));
2078 /* Make sure we send full copied size */
2079 if (progress_callback)
2080 progress_callback (source_size, source_size, progress_callback_data);
2085 static GFileMonitor*
2086 g_local_file_monitor_dir (GFile *file,
2087 GFileMonitorFlags flags,
2088 GCancellable *cancellable,
2091 GLocalFile* local_file = G_LOCAL_FILE(file);
2092 return _g_local_directory_monitor_new (local_file->filename, flags, error);
2095 static GFileMonitor*
2096 g_local_file_monitor_file (GFile *file,
2097 GFileMonitorFlags flags,
2098 GCancellable *cancellable,
2101 GLocalFile* local_file = G_LOCAL_FILE(file);
2102 return _g_local_file_monitor_new (local_file->filename, flags, error);
2106 g_local_file_file_iface_init (GFileIface *iface)
2108 iface->dup = g_local_file_dup;
2109 iface->hash = g_local_file_hash;
2110 iface->equal = g_local_file_equal;
2111 iface->is_native = g_local_file_is_native;
2112 iface->has_uri_scheme = g_local_file_has_uri_scheme;
2113 iface->get_uri_scheme = g_local_file_get_uri_scheme;
2114 iface->get_basename = g_local_file_get_basename;
2115 iface->get_path = g_local_file_get_path;
2116 iface->get_uri = g_local_file_get_uri;
2117 iface->get_parse_name = g_local_file_get_parse_name;
2118 iface->get_parent = g_local_file_get_parent;
2119 iface->prefix_matches = g_local_file_prefix_matches;
2120 iface->get_relative_path = g_local_file_get_relative_path;
2121 iface->resolve_relative_path = g_local_file_resolve_relative_path;
2122 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2123 iface->set_display_name = g_local_file_set_display_name;
2124 iface->enumerate_children = g_local_file_enumerate_children;
2125 iface->query_info = g_local_file_query_info;
2126 iface->query_filesystem_info = g_local_file_query_filesystem_info;
2127 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2128 iface->query_settable_attributes = g_local_file_query_settable_attributes;
2129 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2130 iface->set_attribute = g_local_file_set_attribute;
2131 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2132 iface->read_fn = g_local_file_read;
2133 iface->append_to = g_local_file_append_to;
2134 iface->create = g_local_file_create;
2135 iface->replace = g_local_file_replace;
2136 iface->delete_file = g_local_file_delete;
2137 iface->trash = g_local_file_trash;
2138 iface->make_directory = g_local_file_make_directory;
2139 iface->make_symbolic_link = g_local_file_make_symbolic_link;
2140 iface->copy = g_local_file_copy;
2141 iface->move = g_local_file_move;
2142 iface->monitor_dir = g_local_file_monitor_dir;
2143 iface->monitor_file = g_local_file_monitor_file;