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>
33 #include <sys/statfs.h>
35 #if HAVE_SYS_STATVFS_H
36 #include <sys/statvfs.h>
40 #elif HAVE_SYS_MOUNT_H
42 #include <sys/param.h>
44 #include <sys/mount.h>
47 #if defined(HAVE_STATFS) && defined(HAVE_STATVFS)
48 /* Some systems have both statfs and statvfs, pick the
49 most "native" for these */
50 # if defined(sun) && defined(__SVR4)
51 /* on solaris, statfs doesn't even have the
55 /* at least on linux, statfs is the actual syscall */
59 #elif defined(HAVE_STATFS)
63 #elif defined(HAVE_STATVFS)
69 #include "glocalfile.h"
70 #include "glocalfileinfo.h"
71 #include "glocalfileenumerator.h"
72 #include "glocalfileinputstream.h"
73 #include "glocalfileoutputstream.h"
74 #include "glocaldirectorymonitor.h"
75 #include "glocalfilemonitor.h"
76 #include "gvolumeprivate.h"
77 #include <glib/gstdio.h>
82 static void g_local_file_file_iface_init (GFileIface *iface);
84 static GFileAttributeInfoList *local_writable_attributes = NULL;
85 static GFileAttributeInfoList *local_writable_namespaces = NULL;
89 GObject parent_instance;
94 #define g_local_file_get_type _g_local_file_get_type
95 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
96 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
97 g_local_file_file_iface_init))
99 static char * find_mountpoint_for (const char *file, dev_t dev);
102 g_local_file_finalize (GObject *object)
106 local = G_LOCAL_FILE (object);
108 g_free (local->filename);
110 if (G_OBJECT_CLASS (g_local_file_parent_class)->finalize)
111 (*G_OBJECT_CLASS (g_local_file_parent_class)->finalize) (object);
115 g_local_file_class_init (GLocalFileClass *klass)
117 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
118 GFileAttributeInfoList *list;
120 gobject_class->finalize = g_local_file_finalize;
122 /* Set up attribute lists */
124 /* Writable attributes: */
126 list = g_file_attribute_info_list_new ();
128 g_file_attribute_info_list_add (list,
129 G_FILE_ATTRIBUTE_UNIX_MODE,
130 G_FILE_ATTRIBUTE_TYPE_UINT32,
131 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
134 g_file_attribute_info_list_add (list,
135 G_FILE_ATTRIBUTE_UNIX_UID,
136 G_FILE_ATTRIBUTE_TYPE_UINT32,
137 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
138 g_file_attribute_info_list_add (list,
139 G_FILE_ATTRIBUTE_UNIX_GID,
140 G_FILE_ATTRIBUTE_TYPE_UINT32,
141 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
145 g_file_attribute_info_list_add (list,
146 G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET,
147 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
152 g_file_attribute_info_list_add (list,
153 G_FILE_ATTRIBUTE_TIME_MODIFIED,
154 G_FILE_ATTRIBUTE_TYPE_UINT64,
155 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
156 g_file_attribute_info_list_add (list,
157 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
158 G_FILE_ATTRIBUTE_TYPE_UINT32,
159 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
160 g_file_attribute_info_list_add (list,
161 G_FILE_ATTRIBUTE_TIME_ACCESS,
162 G_FILE_ATTRIBUTE_TYPE_UINT64,
163 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
164 g_file_attribute_info_list_add (list,
165 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
166 G_FILE_ATTRIBUTE_TYPE_UINT32,
167 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
170 local_writable_attributes = list;
172 /* Writable namespaces: */
174 list = g_file_attribute_info_list_new ();
177 g_file_attribute_info_list_add (list,
179 G_FILE_ATTRIBUTE_TYPE_STRING,
180 G_FILE_ATTRIBUTE_FLAGS_COPY_WITH_FILE |
181 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
182 g_file_attribute_info_list_add (list,
184 G_FILE_ATTRIBUTE_TYPE_STRING,
185 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
188 local_writable_namespaces = list;
192 g_local_file_init (GLocalFile *local)
198 canonicalize_filename (const char *filename)
200 char *canon, *start, *p, *q;
203 if (!g_path_is_absolute (filename))
205 cwd = g_get_current_dir ();
206 canon = g_build_filename (cwd, filename, NULL);
210 canon = g_strdup (filename);
212 start = (char *)g_path_skip_root (canon);
217 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
219 memmove (p, p+1, strlen (p+1)+1);
221 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
224 /* Skip previous separator */
228 while (p > start && !G_IS_DIR_SEPARATOR (*p))
230 if (G_IS_DIR_SEPARATOR (*p))
231 *p++ = G_DIR_SEPARATOR;
232 memmove (p, q, strlen (q)+1);
236 /* Skip until next separator */
237 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
242 /* Canonicalize one separator */
243 *p++ = G_DIR_SEPARATOR;
247 /* Remove additional separators */
249 while (*q && G_IS_DIR_SEPARATOR (*q))
253 memmove (p, q, strlen (q)+1);
256 /* Remove trailing slashes */
257 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
265 * @filename: filename of the file to create.
267 * Returns: new local #GFile.
270 _g_local_file_new (const char *filename)
274 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
275 local->filename = canonicalize_filename (filename);
277 return G_FILE (local);
281 g_local_file_is_native (GFile *file)
287 g_local_file_has_uri_scheme (GFile *file,
288 const char *uri_scheme)
290 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
294 g_local_file_get_uri_scheme (GFile *file)
296 return g_strdup ("file");
300 g_local_file_get_basename (GFile *file)
302 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
306 g_local_file_get_path (GFile *file)
308 return g_strdup (G_LOCAL_FILE (file)->filename);
312 g_local_file_get_uri (GFile *file)
314 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
318 get_filename_charset (const gchar **filename_charset)
320 const gchar **charsets;
323 is_utf8 = g_get_filename_charsets (&charsets);
325 if (filename_charset)
326 *filename_charset = charsets[0];
332 name_is_valid_for_display (const char *string,
333 gboolean is_valid_utf8)
337 if (!is_valid_utf8 &&
338 !g_utf8_validate (string, -1, NULL))
341 while ((c = *string++) != 0)
343 if (g_ascii_iscntrl(c))
351 g_local_file_get_parse_name (GFile *file)
353 const char *filename;
355 const gchar *charset;
357 char *roundtripped_filename;
358 gboolean free_utf8_filename;
359 gboolean is_valid_utf8;
361 filename = G_LOCAL_FILE (file)->filename;
362 if (get_filename_charset (&charset))
364 utf8_filename = (char *)filename;
365 free_utf8_filename = FALSE;
366 is_valid_utf8 = FALSE; /* Can't guarantee this */
370 utf8_filename = g_convert (filename, -1,
371 "UTF-8", charset, NULL, NULL, NULL);
372 free_utf8_filename = TRUE;
373 is_valid_utf8 = TRUE;
375 if (utf8_filename != NULL)
377 /* Make sure we can roundtrip: */
378 roundtripped_filename = g_convert (utf8_filename, -1,
379 charset, "UTF-8", NULL, NULL, NULL);
381 if (roundtripped_filename == NULL ||
382 strcmp (utf8_filename, roundtripped_filename) != 0)
384 g_free (utf8_filename);
385 utf8_filename = NULL;
391 if (utf8_filename != NULL &&
392 name_is_valid_for_display (utf8_filename, is_valid_utf8))
394 if (free_utf8_filename)
395 parse_name = utf8_filename;
397 parse_name = g_strdup (utf8_filename);
401 parse_name = g_filename_to_uri (filename, NULL, NULL);
402 if (free_utf8_filename)
403 g_free (utf8_filename);
410 g_local_file_get_parent (GFile *file)
412 GLocalFile *local = G_LOCAL_FILE (file);
413 const char *non_root;
418 non_root = g_path_skip_root (local->filename);
422 dirname = g_path_get_dirname (local->filename);
423 parent = _g_local_file_new (dirname);
429 g_local_file_dup (GFile *file)
431 GLocalFile *local = G_LOCAL_FILE (file);
433 return _g_local_file_new (local->filename);
437 g_local_file_hash (GFile *file)
439 GLocalFile *local = G_LOCAL_FILE (file);
441 return g_str_hash (local->filename);
445 g_local_file_equal (GFile *file1,
448 GLocalFile *local1 = G_LOCAL_FILE (file1);
449 GLocalFile *local2 = G_LOCAL_FILE (file2);
451 return g_str_equal (local1->filename, local2->filename);
455 match_prefix (const char *path, const char *prefix)
459 prefix_len = strlen (prefix);
460 if (strncmp (path, prefix, prefix_len) != 0)
462 return path + prefix_len;
466 g_local_file_contains_file (GFile *parent,
469 GLocalFile *parent_local = G_LOCAL_FILE (parent);
470 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
471 const char *remainder;
473 remainder = match_prefix (descendant_local->filename, parent_local->filename);
474 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
480 g_local_file_get_relative_path (GFile *parent,
483 GLocalFile *parent_local = G_LOCAL_FILE (parent);
484 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
485 const char *remainder;
487 remainder = match_prefix (descendant_local->filename, parent_local->filename);
489 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
490 return g_strdup (remainder + 1);
495 g_local_file_resolve_relative_path (GFile *file,
496 const char *relative_path)
498 GLocalFile *local = G_LOCAL_FILE (file);
502 if (g_path_is_absolute (relative_path))
503 return _g_local_file_new (relative_path);
505 filename = g_build_filename (local->filename, relative_path, NULL);
506 child = _g_local_file_new (filename);
512 static GFileEnumerator *
513 g_local_file_enumerate_children (GFile *file,
514 const char *attributes,
515 GFileQueryInfoFlags flags,
516 GCancellable *cancellable,
519 GLocalFile *local = G_LOCAL_FILE (file);
520 return _g_local_file_enumerator_new (local->filename,
526 g_local_file_get_child_for_display_name (GFile *file,
527 const char *display_name,
530 GFile *parent, *new_file;
533 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
534 if (basename == NULL)
536 g_set_error (error, G_IO_ERROR,
537 G_IO_ERROR_INVALID_FILENAME,
538 _("Invalid filename %s"), display_name);
542 parent = g_file_get_parent (file);
543 new_file = g_file_get_child (file, basename);
544 g_object_unref (parent);
552 get_fs_type (long f_type)
555 /* filesystem ids taken from linux manpage */
649 G_LOCK_DEFINE_STATIC(mount_info_hash);
650 static GHashTable *mount_info_hash = NULL;
651 guint64 mount_info_hash_cache_time = 0;
654 MOUNT_INFO_READONLY = 1<<0
658 device_equal (gconstpointer v1,
661 return *(dev_t *)v1 == * (dev_t *)v2;
665 device_hash (gconstpointer v)
667 return (guint) *(dev_t *)v;
671 get_mount_info (GFileInfo *fs_info,
673 GFileAttributeMatcher *matcher)
677 gpointer info_as_ptr;
684 if (lstat (path, &buf) != 0)
687 G_LOCK (mount_info_hash);
689 if (mount_info_hash == NULL)
690 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
694 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
695 g_hash_table_remove_all (mount_info_hash);
697 got_info = g_hash_table_lookup_extended (mount_info_hash,
702 G_UNLOCK (mount_info_hash);
704 mount_info = GPOINTER_TO_UINT (info_as_ptr);
710 mountpoint = find_mountpoint_for (path, buf.st_dev);
711 if (mountpoint == NULL)
714 mount = g_get_unix_mount_at (mountpoint, &cache_time);
717 if (g_unix_mount_is_readonly (mount))
718 mount_info |= MOUNT_INFO_READONLY;
720 g_unix_mount_free (mount);
723 dev = g_new0 (dev_t, 1);
726 G_LOCK (mount_info_hash);
727 mount_info_hash_cache_time = cache_time;
728 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
729 G_UNLOCK (mount_info_hash);
732 if (mount_info & MOUNT_INFO_READONLY)
733 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FS_READONLY, TRUE);
737 g_local_file_query_filesystem_info (GFile *file,
738 const char *attributes,
739 GCancellable *cancellable,
742 GLocalFile *local = G_LOCAL_FILE (file);
748 struct statfs statfs_buffer;
750 #elif defined(USE_STATVFS)
751 struct statvfs statfs_buffer;
753 GFileAttributeMatcher *attribute_matcher;
760 statfs_result = statfs (local->filename, &statfs_buffer);
761 #elif STATFS_ARGS == 4
762 statfs_result = statfs (local->filename, &statfs_buffer,
763 sizeof (statfs_buffer), 0);
765 block_size = statfs_buffer.f_bsize;
767 #if defined(__linux__)
768 /* ncpfs does not know the amount of available and free space *
769 * assuming ncpfs is linux specific, if you are on a non-linux platform
770 * where ncpfs is available, please file a bug about it on bugzilla.gnome.org
772 if (statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0 &&
773 /* linux/ncp_fs.h: NCP_SUPER_MAGIC == 0x564c */
774 statfs_buffer.f_type == 0x564c)
778 #elif defined(USE_STATVFS)
779 statfs_result = statvfs (local->filename, &statfs_buffer);
780 block_size = statfs_buffer.f_frsize;
783 if (statfs_result == -1)
785 g_set_error (error, G_IO_ERROR,
786 g_io_error_from_errno (errno),
787 _("Error getting filesystem info: %s"),
792 info = g_file_info_new ();
794 attribute_matcher = g_file_attribute_matcher_new (attributes);
796 if (g_file_attribute_matcher_matches (attribute_matcher,
797 G_FILE_ATTRIBUTE_FS_FREE))
798 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FS_FREE, block_size * statfs_buffer.f_bavail);
799 if (g_file_attribute_matcher_matches (attribute_matcher,
800 G_FILE_ATTRIBUTE_FS_SIZE))
801 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FS_SIZE, block_size * statfs_buffer.f_blocks);
804 fstype = get_fs_type (statfs_buffer.f_type);
806 g_file_attribute_matcher_matches (attribute_matcher,
807 G_FILE_ATTRIBUTE_FS_TYPE))
808 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FS_TYPE, fstype);
811 if (g_file_attribute_matcher_matches (attribute_matcher,
812 G_FILE_ATTRIBUTE_FS_READONLY))
814 get_mount_info (info, local->filename, attribute_matcher);
817 g_file_attribute_matcher_unref (attribute_matcher);
823 g_local_file_find_enclosing_volume (GFile *file,
824 GCancellable *cancellable,
827 GLocalFile *local = G_LOCAL_FILE (file);
832 if (lstat (local->filename, &buf) != 0)
834 g_set_error (error, G_IO_ERROR,
835 G_IO_ERROR_NOT_FOUND,
836 _("Containing volume does not exist"));
840 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
841 if (mountpoint == NULL)
843 g_set_error (error, G_IO_ERROR,
844 G_IO_ERROR_NOT_FOUND,
845 _("Containing volume does not exist"));
849 volume = _g_volume_get_for_mount_path (mountpoint);
854 g_set_error (error, G_IO_ERROR,
855 G_IO_ERROR_NOT_FOUND,
856 _("Containing volume does not exist"));
861 g_local_file_set_display_name (GFile *file,
862 const char *display_name,
863 GCancellable *cancellable,
866 GLocalFile *local, *new_local;
867 GFile *new_file, *parent;
871 parent = g_file_get_parent (file);
874 g_set_error (error, G_IO_ERROR,
876 _("Can't rename root directory"));
880 new_file = g_file_get_child_for_display_name (parent, display_name, error);
881 g_object_unref (parent);
883 if (new_file == NULL)
886 local = G_LOCAL_FILE (file);
887 new_local = G_LOCAL_FILE (new_file);
889 if (!(g_lstat (new_local->filename, &statbuf) == -1 &&
892 g_set_error (error, G_IO_ERROR,
894 _("Can't rename file, filename already exist"));
898 if (rename (local->filename, new_local->filename) == -1)
903 /* We can't get a rename file into itself error herer,
904 so this must be an invalid filename, on e.g. FAT */
905 g_set_error (error, G_IO_ERROR,
906 G_IO_ERROR_INVALID_FILENAME,
907 _("Invalid filename"));
909 g_set_error (error, G_IO_ERROR,
910 g_io_error_from_errno (errsv),
911 _("Error renaming file: %s"),
913 g_object_unref (new_file);
921 g_local_file_query_info (GFile *file,
922 const char *attributes,
923 GFileQueryInfoFlags flags,
924 GCancellable *cancellable,
927 GLocalFile *local = G_LOCAL_FILE (file);
929 GFileAttributeMatcher *matcher;
930 char *basename, *dirname;
931 GLocalParentFileInfo parent_info;
933 matcher = g_file_attribute_matcher_new (attributes);
935 basename = g_path_get_basename (local->filename);
937 dirname = g_path_get_dirname (local->filename);
938 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
941 info = _g_local_file_info_get (basename, local->filename,
942 matcher, flags, &parent_info,
947 g_file_attribute_matcher_unref (matcher);
952 static GFileAttributeInfoList *
953 g_local_file_query_settable_attributes (GFile *file,
954 GCancellable *cancellable,
957 return g_file_attribute_info_list_ref (local_writable_attributes);
960 static GFileAttributeInfoList *
961 g_local_file_query_writable_namespaces (GFile *file,
962 GCancellable *cancellable,
965 return g_file_attribute_info_list_ref (local_writable_namespaces);
969 g_local_file_set_attribute (GFile *file,
970 const char *attribute,
971 const GFileAttributeValue *value,
972 GFileQueryInfoFlags flags,
973 GCancellable *cancellable,
976 GLocalFile *local = G_LOCAL_FILE (file);
978 return _g_local_file_info_set_attribute (local->filename,
987 g_local_file_set_attributes_from_info (GFile *file,
989 GFileQueryInfoFlags flags,
990 GCancellable *cancellable,
993 GLocalFile *local = G_LOCAL_FILE (file);
994 int res, chained_res;
995 GFileIface* default_iface;
997 res = _g_local_file_info_set_attributes (local->filename,
1003 error = NULL; /* Don't write over error if further errors */
1005 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1007 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1009 return res && chained_res;
1012 static GFileInputStream *
1013 g_local_file_read (GFile *file,
1014 GCancellable *cancellable,
1017 GLocalFile *local = G_LOCAL_FILE (file);
1021 fd = g_open (local->filename, O_RDONLY, 0);
1024 g_set_error (error, G_IO_ERROR,
1025 g_io_error_from_errno (errno),
1026 _("Error opening file: %s"),
1027 g_strerror (errno));
1031 if (fstat(fd, &buf) == 0 && S_ISDIR (buf.st_mode))
1034 g_set_error (error, G_IO_ERROR,
1035 G_IO_ERROR_IS_DIRECTORY,
1036 _("Can't open directory"));
1040 return _g_local_file_input_stream_new (fd);
1043 static GFileOutputStream *
1044 g_local_file_append_to (GFile *file,
1045 GFileCreateFlags flags,
1046 GCancellable *cancellable,
1049 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1050 flags, cancellable, error);
1053 static GFileOutputStream *
1054 g_local_file_create (GFile *file,
1055 GFileCreateFlags flags,
1056 GCancellable *cancellable,
1059 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1060 flags, cancellable, error);
1063 static GFileOutputStream *
1064 g_local_file_replace (GFile *file,
1066 gboolean make_backup,
1067 GFileCreateFlags flags,
1068 GCancellable *cancellable,
1071 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1072 etag, make_backup, flags,
1073 cancellable, error);
1078 g_local_file_delete (GFile *file,
1079 GCancellable *cancellable,
1082 GLocalFile *local = G_LOCAL_FILE (file);
1084 if (g_remove (local->filename) == -1)
1086 g_set_error (error, G_IO_ERROR,
1087 g_io_error_from_errno (errno),
1088 _("Error removing file: %s"),
1089 g_strerror (errno));
1097 strip_trailing_slashes (const char *path)
1102 path_copy = g_strdup (path);
1103 len = strlen (path_copy);
1104 while (len > 1 && path_copy[len-1] == '/')
1105 path_copy[--len] = 0;
1111 expand_symlink (const char *link)
1113 char *resolved, *canonical, *parent, *link2;
1114 char symlink_value[4096];
1117 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1119 return g_strdup (link);
1120 symlink_value[res] = 0;
1122 if (g_path_is_absolute (symlink_value))
1123 return canonicalize_filename (symlink_value);
1126 link2 = strip_trailing_slashes (link);
1127 parent = g_path_get_dirname (link2);
1130 resolved = g_build_filename (parent, symlink_value, NULL);
1133 canonical = canonicalize_filename (resolved);
1142 get_parent (const char *path, dev_t *parent_dev)
1145 struct stat parent_stat;
1149 path_copy = strip_trailing_slashes (path);
1151 parent = g_path_get_dirname (path_copy);
1152 if (strcmp (parent, ".") == 0 ||
1153 strcmp (parent, path_copy) == 0)
1162 if (g_lstat (parent, &parent_stat) != 0)
1168 if (S_ISLNK (parent_stat.st_mode))
1171 parent = expand_symlink (parent);
1176 if (num_recursions > 12)
1181 } while (S_ISLNK (parent_stat.st_mode));
1183 *parent_dev = parent_stat.st_dev;
1189 expand_all_symlinks (const char *path)
1191 char *parent, *parent_expanded;
1192 char *basename, *res;
1195 parent = get_parent (path, &parent_dev);
1198 parent_expanded = expand_all_symlinks (parent);
1200 basename = g_path_get_basename (path);
1201 res = g_build_filename (parent_expanded, basename, NULL);
1203 g_free (parent_expanded);
1206 res = g_strdup (path);
1212 find_mountpoint_for (const char *file, dev_t dev)
1215 dev_t dir_dev, parent_dev;
1217 dir = g_strdup (file);
1221 parent = get_parent (dir, &parent_dev);
1225 if (parent_dev != dir_dev)
1237 find_topdir_for (const char *file)
1242 dir = get_parent (file, &dir_dev);
1246 return find_mountpoint_for (dir, dir_dev);
1250 get_unique_filename (const char *basename, int id)
1255 return g_strdup (basename);
1257 dot = strchr (basename, '.');
1259 return g_strdup_printf ("%.*s.%d%s", dot - basename, basename, id, dot);
1261 return g_strdup_printf ("%s.%d", basename, id);
1265 path_has_prefix (const char *path, const char *prefix)
1272 prefix_len = strlen (prefix);
1274 if (strncmp (path, prefix, prefix_len) == 0 &&
1275 (prefix_len == 0 || /* empty prefix always matches */
1276 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1277 path[prefix_len] == 0 ||
1278 path[prefix_len] == '/'))
1285 try_make_relative (const char *path, const char *base)
1287 char *path2, *base2;
1290 path2 = expand_all_symlinks (path);
1291 base2 = expand_all_symlinks (base);
1294 if (path_has_prefix (path2, base2))
1296 relative = path2 + strlen (base2);
1297 while (*relative == '/')
1299 relative = g_strdup (relative);
1307 /* Failed, use abs path */
1308 return g_strdup (path);
1312 escape_trash_name (char *name)
1315 const gchar hex[16] = "0123456789ABCDEF";
1317 str = g_string_new ("");
1325 if (g_ascii_isprint (c))
1326 g_string_append_c (str, c);
1329 g_string_append_c (str, '%');
1330 g_string_append_c (str, hex[((guchar)c) >> 4]);
1331 g_string_append_c (str, hex[((guchar)c) & 0xf]);
1335 return g_string_free (str, FALSE);
1339 g_local_file_trash (GFile *file,
1340 GCancellable *cancellable,
1343 GLocalFile *local = G_LOCAL_FILE (file);
1344 struct stat file_stat, home_stat, trash_stat, global_stat;
1345 const char *homedir;
1347 char *trashdir, *globaldir, *topdir, *infodir, *filesdir;
1348 char *basename, *trashname, *trashfile, *infoname, *infofile;
1349 char *original_name, *original_name_escaped;
1354 gboolean is_homedir_trash;
1357 char delete_time[32];
1360 if (g_lstat (local->filename, &file_stat) != 0)
1362 g_set_error (error, G_IO_ERROR,
1363 g_io_error_from_errno (errno),
1364 _("Error trashing file: %s"),
1365 g_strerror (errno));
1369 homedir = g_get_home_dir ();
1370 g_stat (homedir, &home_stat);
1372 is_homedir_trash = FALSE;
1374 if (file_stat.st_dev == home_stat.st_dev)
1376 is_homedir_trash = TRUE;
1378 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1379 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1385 display_name = g_filename_display_name (trashdir);
1386 g_set_error (error, G_IO_ERROR,
1387 g_io_error_from_errno (err),
1388 _("Unable to create trash dir %s: %s"),
1389 display_name, g_strerror (err));
1390 g_free (display_name);
1394 topdir = g_strdup (g_get_user_data_dir ());
1399 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1401 topdir = find_topdir_for (local->filename);
1404 g_set_error (error, G_IO_ERROR,
1405 G_IO_ERROR_NOT_SUPPORTED,
1406 _("Unable to find toplevel directory for trash"));
1410 /* Try looking for global trash dir $topdir/.Trash/$uid */
1411 globaldir = g_build_filename (topdir, ".Trash", NULL);
1412 if (g_lstat (globaldir, &global_stat) == 0 &&
1413 S_ISDIR (global_stat.st_mode) &&
1414 (global_stat.st_mode & S_ISVTX) != 0)
1416 trashdir = g_build_filename (globaldir, uid_str, NULL);
1418 if (g_lstat (trashdir, &trash_stat) == 0)
1420 if (!S_ISDIR (trash_stat.st_mode) ||
1421 trash_stat.st_uid != uid)
1423 /* Not a directory or not owned by user, ignore */
1428 else if (g_mkdir (trashdir, 0700) == -1)
1436 if (trashdir == NULL)
1438 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1439 dirname = g_strdup_printf (".Trash-%s", uid_str);
1440 trashdir = g_build_filename (topdir, dirname, NULL);
1443 if (g_lstat (trashdir, &trash_stat) == 0)
1445 if (!S_ISDIR (trash_stat.st_mode) ||
1446 trash_stat.st_uid != uid)
1448 /* Not a directory or not owned by user, ignore */
1453 else if (g_mkdir (trashdir, 0700) == -1)
1460 if (trashdir == NULL)
1463 g_set_error (error, G_IO_ERROR,
1464 G_IO_ERROR_NOT_SUPPORTED,
1465 _("Unable to find or create trash directory"));
1470 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1472 infodir = g_build_filename (trashdir, "info", NULL);
1473 filesdir = g_build_filename (trashdir, "files", NULL);
1476 /* Make sure we have the subdirectories */
1477 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1478 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
1483 g_set_error (error, G_IO_ERROR,
1484 G_IO_ERROR_NOT_SUPPORTED,
1485 _("Unable to find or create trash directory"));
1489 basename = g_path_get_basename (local->filename);
1497 trashname = get_unique_filename (basename, i++);
1498 infoname = g_strconcat (trashname, ".trashinfo", NULL);
1499 infofile = g_build_filename (infodir, infoname, NULL);
1502 fd = open (infofile, O_CREAT | O_EXCL, 0666);
1503 } while (fd == -1 && errno == EEXIST);
1515 g_set_error (error, G_IO_ERROR,
1516 g_io_error_from_errno (errno),
1517 _("Unable to create trashed file: %s"),
1518 g_strerror (errno));
1524 /* TODO: Maybe we should verify that you can delete the file from the trash
1525 before moving it? OTOH, that is hard, as it needs a recursive scan */
1527 trashfile = g_build_filename (filesdir, trashname, NULL);
1531 if (g_rename (local->filename, trashfile) == -1)
1538 g_set_error (error, G_IO_ERROR,
1539 g_io_error_from_errno (errno),
1540 _("Unable to trash file: %s"),
1541 g_strerror (errno));
1547 /* TODO: Do we need to update mtime/atime here after the move? */
1549 /* Use absolute names for homedir */
1550 if (is_homedir_trash)
1551 original_name = g_strdup (local->filename);
1553 original_name = try_make_relative (local->filename, topdir);
1554 original_name_escaped = escape_trash_name (original_name);
1556 g_free (original_name);
1560 localtime_r (&t, &now);
1562 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
1564 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
1565 original_name_escaped, delete_time);
1567 g_file_set_contents (infofile, data, -1, NULL);
1571 g_free (original_name_escaped);
1578 g_local_file_make_directory (GFile *file,
1579 GCancellable *cancellable,
1582 GLocalFile *local = G_LOCAL_FILE (file);
1584 if (g_mkdir (local->filename, 0755) == -1)
1588 if (errsv == EINVAL)
1589 /* This must be an invalid filename, on e.g. FAT */
1590 g_set_error (error, G_IO_ERROR,
1591 G_IO_ERROR_INVALID_FILENAME,
1592 _("Invalid filename"));
1594 g_set_error (error, G_IO_ERROR,
1595 g_io_error_from_errno (errsv),
1596 _("Error removing file: %s"),
1597 g_strerror (errsv));
1605 g_local_file_make_symbolic_link (GFile *file,
1606 const char *symlink_value,
1607 GCancellable *cancellable,
1611 GLocalFile *local = G_LOCAL_FILE (file);
1613 if (symlink (symlink_value, local->filename) == -1)
1617 if (errsv == EINVAL)
1618 /* This must be an invalid filename, on e.g. FAT */
1619 g_set_error (error, G_IO_ERROR,
1620 G_IO_ERROR_INVALID_FILENAME,
1621 _("Invalid filename"));
1623 g_set_error (error, G_IO_ERROR,
1624 g_io_error_from_errno (errsv),
1625 _("Error making symbolic link: %s"),
1626 g_strerror (errsv));
1631 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
1638 g_local_file_copy (GFile *source,
1640 GFileCopyFlags flags,
1641 GCancellable *cancellable,
1642 GFileProgressCallback progress_callback,
1643 gpointer progress_callback_data,
1646 /* Fall back to default copy */
1647 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
1652 g_local_file_move (GFile *source,
1654 GFileCopyFlags flags,
1655 GCancellable *cancellable,
1656 GFileProgressCallback progress_callback,
1657 gpointer progress_callback_data,
1660 GLocalFile *local_source = G_LOCAL_FILE (source);
1661 GLocalFile *local_destination = G_LOCAL_FILE (destination);
1662 struct stat statbuf;
1663 gboolean destination_exist, source_is_dir;
1667 res = g_lstat (local_source->filename, &statbuf);
1670 g_set_error (error, G_IO_ERROR,
1671 g_io_error_from_errno (errno),
1672 _("Error moving file: %s"),
1673 g_strerror (errno));
1677 source_is_dir = S_ISDIR (statbuf.st_mode);
1679 destination_exist = FALSE;
1680 res = g_lstat (local_destination->filename, &statbuf);
1683 destination_exist = TRUE; /* Target file exists */
1685 if (flags & G_FILE_COPY_OVERWRITE)
1687 /* Always fail on dirs, even with overwrite */
1688 if (S_ISDIR (statbuf.st_mode))
1692 G_IO_ERROR_WOULD_MERGE,
1693 _("Can't move directory over directory"));
1702 _("Target file already exists"));
1707 if (flags & G_FILE_COPY_BACKUP && destination_exist)
1709 backup_name = g_strconcat (local_destination->filename, "~", NULL);
1710 if (rename (local_destination->filename, backup_name) == -1)
1714 G_IO_ERROR_CANT_CREATE_BACKUP,
1715 _("Backup file creation failed"));
1716 g_free (backup_name);
1719 g_free (backup_name);
1720 destination_exist = FALSE; /* It did, but no more */
1723 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
1725 /* Source is a dir, destination exists (and is not a dir, because that would have failed
1726 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
1727 res = unlink (local_destination->filename);
1730 g_set_error (error, G_IO_ERROR,
1731 g_io_error_from_errno (errno),
1732 _("Error removing target file: %s"),
1733 g_strerror (errno));
1738 if (rename (local_source->filename, local_destination->filename) == -1)
1744 if (errsv == EINVAL)
1745 /* This must be an invalid filename, on e.g. FAT, or
1746 we're trying to move the file into itself...
1747 We return invalid filename for both... */
1748 g_set_error (error, G_IO_ERROR,
1749 G_IO_ERROR_INVALID_FILENAME,
1750 _("Invalid filename"));
1752 g_set_error (error, G_IO_ERROR,
1753 g_io_error_from_errno (errsv),
1754 _("Error moving file: %s"),
1755 g_strerror (errsv));
1763 if (!g_file_copy (source, destination, G_FILE_COPY_OVERWRITE | G_FILE_COPY_ALL_METADATA, cancellable,
1764 progress_callback, progress_callback_data,
1768 return g_file_delete (source, cancellable, error);
1772 static GDirectoryMonitor*
1773 g_local_file_monitor_dir (GFile* file,
1774 GFileMonitorFlags flags,
1775 GCancellable *cancellable)
1777 GLocalFile* local_file = G_LOCAL_FILE(file);
1778 return _g_local_directory_monitor_new (local_file->filename, flags);
1781 static GFileMonitor*
1782 g_local_file_monitor_file (GFile* file,
1783 GFileMonitorFlags flags,
1784 GCancellable *cancellable)
1786 GLocalFile* local_file = G_LOCAL_FILE(file);
1787 return _g_local_file_monitor_new (local_file->filename, flags);
1791 g_local_file_file_iface_init (GFileIface *iface)
1793 iface->dup = g_local_file_dup;
1794 iface->hash = g_local_file_hash;
1795 iface->equal = g_local_file_equal;
1796 iface->is_native = g_local_file_is_native;
1797 iface->has_uri_scheme = g_local_file_has_uri_scheme;
1798 iface->get_uri_scheme = g_local_file_get_uri_scheme;
1799 iface->get_basename = g_local_file_get_basename;
1800 iface->get_path = g_local_file_get_path;
1801 iface->get_uri = g_local_file_get_uri;
1802 iface->get_parse_name = g_local_file_get_parse_name;
1803 iface->get_parent = g_local_file_get_parent;
1804 iface->contains_file = g_local_file_contains_file;
1805 iface->get_relative_path = g_local_file_get_relative_path;
1806 iface->resolve_relative_path = g_local_file_resolve_relative_path;
1807 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
1808 iface->set_display_name = g_local_file_set_display_name;
1809 iface->enumerate_children = g_local_file_enumerate_children;
1810 iface->query_info = g_local_file_query_info;
1811 iface->query_filesystem_info = g_local_file_query_filesystem_info;
1812 iface->find_enclosing_volume = g_local_file_find_enclosing_volume;
1813 iface->query_settable_attributes = g_local_file_query_settable_attributes;
1814 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
1815 iface->set_attribute = g_local_file_set_attribute;
1816 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
1817 iface->read = g_local_file_read;
1818 iface->append_to = g_local_file_append_to;
1819 iface->create = g_local_file_create;
1820 iface->replace = g_local_file_replace;
1821 iface->delete_file = g_local_file_delete;
1822 iface->trash = g_local_file_trash;
1823 iface->make_directory = g_local_file_make_directory;
1824 iface->make_symbolic_link = g_local_file_make_symbolic_link;
1825 iface->copy = g_local_file_copy;
1826 iface->move = g_local_file_move;
1827 iface->monitor_dir = g_local_file_monitor_dir;
1828 iface->monitor_file = g_local_file_monitor_file;