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,
460 prefix_len = strlen (prefix);
461 if (strncmp (path, prefix, prefix_len) != 0)
463 return path + prefix_len;
467 g_local_file_contains_file (GFile *parent,
470 GLocalFile *parent_local = G_LOCAL_FILE (parent);
471 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
472 const char *remainder;
474 remainder = match_prefix (descendant_local->filename, parent_local->filename);
475 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
481 g_local_file_get_relative_path (GFile *parent,
484 GLocalFile *parent_local = G_LOCAL_FILE (parent);
485 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
486 const char *remainder;
488 remainder = match_prefix (descendant_local->filename, parent_local->filename);
490 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
491 return g_strdup (remainder + 1);
496 g_local_file_resolve_relative_path (GFile *file,
497 const char *relative_path)
499 GLocalFile *local = G_LOCAL_FILE (file);
503 if (g_path_is_absolute (relative_path))
504 return _g_local_file_new (relative_path);
506 filename = g_build_filename (local->filename, relative_path, NULL);
507 child = _g_local_file_new (filename);
513 static GFileEnumerator *
514 g_local_file_enumerate_children (GFile *file,
515 const char *attributes,
516 GFileQueryInfoFlags flags,
517 GCancellable *cancellable,
520 GLocalFile *local = G_LOCAL_FILE (file);
521 return _g_local_file_enumerator_new (local->filename,
527 g_local_file_get_child_for_display_name (GFile *file,
528 const char *display_name,
531 GFile *parent, *new_file;
534 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
535 if (basename == NULL)
537 g_set_error (error, G_IO_ERROR,
538 G_IO_ERROR_INVALID_FILENAME,
539 _("Invalid filename %s"), display_name);
543 parent = g_file_get_parent (file);
544 new_file = g_file_get_child (file, basename);
545 g_object_unref (parent);
553 get_fs_type (long f_type)
556 /* filesystem ids taken from linux manpage */
651 G_LOCK_DEFINE_STATIC(mount_info_hash);
652 static GHashTable *mount_info_hash = NULL;
653 guint64 mount_info_hash_cache_time = 0;
656 MOUNT_INFO_READONLY = 1<<0
660 device_equal (gconstpointer v1,
663 return *(dev_t *)v1 == * (dev_t *)v2;
667 device_hash (gconstpointer v)
669 return (guint) *(dev_t *)v;
673 get_mount_info (GFileInfo *fs_info,
675 GFileAttributeMatcher *matcher)
679 gpointer info_as_ptr;
686 if (lstat (path, &buf) != 0)
689 G_LOCK (mount_info_hash);
691 if (mount_info_hash == NULL)
692 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
696 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
697 g_hash_table_remove_all (mount_info_hash);
699 got_info = g_hash_table_lookup_extended (mount_info_hash,
704 G_UNLOCK (mount_info_hash);
706 mount_info = GPOINTER_TO_UINT (info_as_ptr);
712 mountpoint = find_mountpoint_for (path, buf.st_dev);
713 if (mountpoint == NULL)
716 mount = g_get_unix_mount_at (mountpoint, &cache_time);
719 if (g_unix_mount_is_readonly (mount))
720 mount_info |= MOUNT_INFO_READONLY;
722 g_unix_mount_free (mount);
725 dev = g_new0 (dev_t, 1);
728 G_LOCK (mount_info_hash);
729 mount_info_hash_cache_time = cache_time;
730 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
731 G_UNLOCK (mount_info_hash);
734 if (mount_info & MOUNT_INFO_READONLY)
735 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FS_READONLY, TRUE);
739 g_local_file_query_filesystem_info (GFile *file,
740 const char *attributes,
741 GCancellable *cancellable,
744 GLocalFile *local = G_LOCAL_FILE (file);
750 struct statfs statfs_buffer;
752 #elif defined(USE_STATVFS)
753 struct statvfs statfs_buffer;
755 GFileAttributeMatcher *attribute_matcher;
762 statfs_result = statfs (local->filename, &statfs_buffer);
763 #elif STATFS_ARGS == 4
764 statfs_result = statfs (local->filename, &statfs_buffer,
765 sizeof (statfs_buffer), 0);
767 block_size = statfs_buffer.f_bsize;
769 #if defined(__linux__)
770 /* ncpfs does not know the amount of available and free space *
771 * assuming ncpfs is linux specific, if you are on a non-linux platform
772 * where ncpfs is available, please file a bug about it on bugzilla.gnome.org
774 if (statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0 &&
775 /* linux/ncp_fs.h: NCP_SUPER_MAGIC == 0x564c */
776 statfs_buffer.f_type == 0x564c)
780 #elif defined(USE_STATVFS)
781 statfs_result = statvfs (local->filename, &statfs_buffer);
782 block_size = statfs_buffer.f_frsize;
785 if (statfs_result == -1)
787 g_set_error (error, G_IO_ERROR,
788 g_io_error_from_errno (errno),
789 _("Error getting filesystem info: %s"),
794 info = g_file_info_new ();
796 attribute_matcher = g_file_attribute_matcher_new (attributes);
798 if (g_file_attribute_matcher_matches (attribute_matcher,
799 G_FILE_ATTRIBUTE_FS_FREE))
800 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FS_FREE, block_size * statfs_buffer.f_bavail);
801 if (g_file_attribute_matcher_matches (attribute_matcher,
802 G_FILE_ATTRIBUTE_FS_SIZE))
803 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FS_SIZE, block_size * statfs_buffer.f_blocks);
806 fstype = get_fs_type (statfs_buffer.f_type);
808 g_file_attribute_matcher_matches (attribute_matcher,
809 G_FILE_ATTRIBUTE_FS_TYPE))
810 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FS_TYPE, fstype);
813 if (g_file_attribute_matcher_matches (attribute_matcher,
814 G_FILE_ATTRIBUTE_FS_READONLY))
816 get_mount_info (info, local->filename, attribute_matcher);
819 g_file_attribute_matcher_unref (attribute_matcher);
825 g_local_file_find_enclosing_volume (GFile *file,
826 GCancellable *cancellable,
829 GLocalFile *local = G_LOCAL_FILE (file);
834 if (lstat (local->filename, &buf) != 0)
836 g_set_error (error, G_IO_ERROR,
837 G_IO_ERROR_NOT_FOUND,
838 _("Containing volume does not exist"));
842 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
843 if (mountpoint == NULL)
845 g_set_error (error, G_IO_ERROR,
846 G_IO_ERROR_NOT_FOUND,
847 _("Containing volume does not exist"));
851 volume = _g_volume_get_for_mount_path (mountpoint);
856 g_set_error (error, G_IO_ERROR,
857 G_IO_ERROR_NOT_FOUND,
858 _("Containing volume does not exist"));
863 g_local_file_set_display_name (GFile *file,
864 const char *display_name,
865 GCancellable *cancellable,
868 GLocalFile *local, *new_local;
869 GFile *new_file, *parent;
873 parent = g_file_get_parent (file);
876 g_set_error (error, G_IO_ERROR,
878 _("Can't rename root directory"));
882 new_file = g_file_get_child_for_display_name (parent, display_name, error);
883 g_object_unref (parent);
885 if (new_file == NULL)
888 local = G_LOCAL_FILE (file);
889 new_local = G_LOCAL_FILE (new_file);
891 if (!(g_lstat (new_local->filename, &statbuf) == -1 &&
894 g_set_error (error, G_IO_ERROR,
896 _("Can't rename file, filename already exist"));
900 if (rename (local->filename, new_local->filename) == -1)
905 /* We can't get a rename file into itself error herer,
906 so this must be an invalid filename, on e.g. FAT */
907 g_set_error (error, G_IO_ERROR,
908 G_IO_ERROR_INVALID_FILENAME,
909 _("Invalid filename"));
911 g_set_error (error, G_IO_ERROR,
912 g_io_error_from_errno (errsv),
913 _("Error renaming file: %s"),
915 g_object_unref (new_file);
923 g_local_file_query_info (GFile *file,
924 const char *attributes,
925 GFileQueryInfoFlags flags,
926 GCancellable *cancellable,
929 GLocalFile *local = G_LOCAL_FILE (file);
931 GFileAttributeMatcher *matcher;
932 char *basename, *dirname;
933 GLocalParentFileInfo parent_info;
935 matcher = g_file_attribute_matcher_new (attributes);
937 basename = g_path_get_basename (local->filename);
939 dirname = g_path_get_dirname (local->filename);
940 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
943 info = _g_local_file_info_get (basename, local->filename,
944 matcher, flags, &parent_info,
949 g_file_attribute_matcher_unref (matcher);
954 static GFileAttributeInfoList *
955 g_local_file_query_settable_attributes (GFile *file,
956 GCancellable *cancellable,
959 return g_file_attribute_info_list_ref (local_writable_attributes);
962 static GFileAttributeInfoList *
963 g_local_file_query_writable_namespaces (GFile *file,
964 GCancellable *cancellable,
967 return g_file_attribute_info_list_ref (local_writable_namespaces);
971 g_local_file_set_attribute (GFile *file,
972 const char *attribute,
973 const GFileAttributeValue *value,
974 GFileQueryInfoFlags flags,
975 GCancellable *cancellable,
978 GLocalFile *local = G_LOCAL_FILE (file);
980 return _g_local_file_info_set_attribute (local->filename,
989 g_local_file_set_attributes_from_info (GFile *file,
991 GFileQueryInfoFlags flags,
992 GCancellable *cancellable,
995 GLocalFile *local = G_LOCAL_FILE (file);
996 int res, chained_res;
997 GFileIface* default_iface;
999 res = _g_local_file_info_set_attributes (local->filename,
1005 error = NULL; /* Don't write over error if further errors */
1007 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1009 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1011 return res && chained_res;
1014 static GFileInputStream *
1015 g_local_file_read (GFile *file,
1016 GCancellable *cancellable,
1019 GLocalFile *local = G_LOCAL_FILE (file);
1023 fd = g_open (local->filename, O_RDONLY, 0);
1026 g_set_error (error, G_IO_ERROR,
1027 g_io_error_from_errno (errno),
1028 _("Error opening file: %s"),
1029 g_strerror (errno));
1033 if (fstat(fd, &buf) == 0 && S_ISDIR (buf.st_mode))
1036 g_set_error (error, G_IO_ERROR,
1037 G_IO_ERROR_IS_DIRECTORY,
1038 _("Can't open directory"));
1042 return _g_local_file_input_stream_new (fd);
1045 static GFileOutputStream *
1046 g_local_file_append_to (GFile *file,
1047 GFileCreateFlags flags,
1048 GCancellable *cancellable,
1051 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1052 flags, cancellable, error);
1055 static GFileOutputStream *
1056 g_local_file_create (GFile *file,
1057 GFileCreateFlags flags,
1058 GCancellable *cancellable,
1061 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1062 flags, cancellable, error);
1065 static GFileOutputStream *
1066 g_local_file_replace (GFile *file,
1068 gboolean make_backup,
1069 GFileCreateFlags flags,
1070 GCancellable *cancellable,
1073 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1074 etag, make_backup, flags,
1075 cancellable, error);
1080 g_local_file_delete (GFile *file,
1081 GCancellable *cancellable,
1084 GLocalFile *local = G_LOCAL_FILE (file);
1086 if (g_remove (local->filename) == -1)
1088 g_set_error (error, G_IO_ERROR,
1089 g_io_error_from_errno (errno),
1090 _("Error removing file: %s"),
1091 g_strerror (errno));
1099 strip_trailing_slashes (const char *path)
1104 path_copy = g_strdup (path);
1105 len = strlen (path_copy);
1106 while (len > 1 && path_copy[len-1] == '/')
1107 path_copy[--len] = 0;
1113 expand_symlink (const char *link)
1115 char *resolved, *canonical, *parent, *link2;
1116 char symlink_value[4096];
1119 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1121 return g_strdup (link);
1122 symlink_value[res] = 0;
1124 if (g_path_is_absolute (symlink_value))
1125 return canonicalize_filename (symlink_value);
1128 link2 = strip_trailing_slashes (link);
1129 parent = g_path_get_dirname (link2);
1132 resolved = g_build_filename (parent, symlink_value, NULL);
1135 canonical = canonicalize_filename (resolved);
1144 get_parent (const char *path,
1148 struct stat parent_stat;
1152 path_copy = strip_trailing_slashes (path);
1154 parent = g_path_get_dirname (path_copy);
1155 if (strcmp (parent, ".") == 0 ||
1156 strcmp (parent, path_copy) == 0)
1165 if (g_lstat (parent, &parent_stat) != 0)
1171 if (S_ISLNK (parent_stat.st_mode))
1174 parent = expand_symlink (parent);
1179 if (num_recursions > 12)
1184 } while (S_ISLNK (parent_stat.st_mode));
1186 *parent_dev = parent_stat.st_dev;
1192 expand_all_symlinks (const char *path)
1194 char *parent, *parent_expanded;
1195 char *basename, *res;
1198 parent = get_parent (path, &parent_dev);
1201 parent_expanded = expand_all_symlinks (parent);
1203 basename = g_path_get_basename (path);
1204 res = g_build_filename (parent_expanded, basename, NULL);
1206 g_free (parent_expanded);
1209 res = g_strdup (path);
1215 find_mountpoint_for (const char *file,
1219 dev_t dir_dev, parent_dev;
1221 dir = g_strdup (file);
1226 parent = get_parent (dir, &parent_dev);
1230 if (parent_dev != dir_dev)
1242 find_topdir_for (const char *file)
1247 dir = get_parent (file, &dir_dev);
1251 return find_mountpoint_for (dir, dir_dev);
1255 get_unique_filename (const char *basename,
1261 return g_strdup (basename);
1263 dot = strchr (basename, '.');
1265 return g_strdup_printf ("%.*s.%d%s", dot - basename, basename, id, dot);
1267 return g_strdup_printf ("%s.%d", basename, id);
1271 path_has_prefix (const char *path,
1279 prefix_len = strlen (prefix);
1281 if (strncmp (path, prefix, prefix_len) == 0 &&
1282 (prefix_len == 0 || /* empty prefix always matches */
1283 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1284 path[prefix_len] == 0 ||
1285 path[prefix_len] == '/'))
1292 try_make_relative (const char *path,
1295 char *path2, *base2;
1298 path2 = expand_all_symlinks (path);
1299 base2 = expand_all_symlinks (base);
1302 if (path_has_prefix (path2, base2))
1304 relative = path2 + strlen (base2);
1305 while (*relative == '/')
1307 relative = g_strdup (relative);
1315 /* Failed, use abs path */
1316 return g_strdup (path);
1320 escape_trash_name (char *name)
1323 const gchar hex[16] = "0123456789ABCDEF";
1325 str = g_string_new ("");
1333 if (g_ascii_isprint (c))
1334 g_string_append_c (str, c);
1337 g_string_append_c (str, '%');
1338 g_string_append_c (str, hex[((guchar)c) >> 4]);
1339 g_string_append_c (str, hex[((guchar)c) & 0xf]);
1343 return g_string_free (str, FALSE);
1347 g_local_file_trash (GFile *file,
1348 GCancellable *cancellable,
1351 GLocalFile *local = G_LOCAL_FILE (file);
1352 struct stat file_stat, home_stat, trash_stat, global_stat;
1353 const char *homedir;
1355 char *trashdir, *globaldir, *topdir, *infodir, *filesdir;
1356 char *basename, *trashname, *trashfile, *infoname, *infofile;
1357 char *original_name, *original_name_escaped;
1362 gboolean is_homedir_trash;
1365 char delete_time[32];
1368 if (g_lstat (local->filename, &file_stat) != 0)
1370 g_set_error (error, G_IO_ERROR,
1371 g_io_error_from_errno (errno),
1372 _("Error trashing file: %s"),
1373 g_strerror (errno));
1377 homedir = g_get_home_dir ();
1378 g_stat (homedir, &home_stat);
1380 is_homedir_trash = FALSE;
1382 if (file_stat.st_dev == home_stat.st_dev)
1384 is_homedir_trash = TRUE;
1386 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1387 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1393 display_name = g_filename_display_name (trashdir);
1394 g_set_error (error, G_IO_ERROR,
1395 g_io_error_from_errno (err),
1396 _("Unable to create trash dir %s: %s"),
1397 display_name, g_strerror (err));
1398 g_free (display_name);
1402 topdir = g_strdup (g_get_user_data_dir ());
1407 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1409 topdir = find_topdir_for (local->filename);
1412 g_set_error (error, G_IO_ERROR,
1413 G_IO_ERROR_NOT_SUPPORTED,
1414 _("Unable to find toplevel directory for trash"));
1418 /* Try looking for global trash dir $topdir/.Trash/$uid */
1419 globaldir = g_build_filename (topdir, ".Trash", NULL);
1420 if (g_lstat (globaldir, &global_stat) == 0 &&
1421 S_ISDIR (global_stat.st_mode) &&
1422 (global_stat.st_mode & S_ISVTX) != 0)
1424 trashdir = g_build_filename (globaldir, uid_str, NULL);
1426 if (g_lstat (trashdir, &trash_stat) == 0)
1428 if (!S_ISDIR (trash_stat.st_mode) ||
1429 trash_stat.st_uid != uid)
1431 /* Not a directory or not owned by user, ignore */
1436 else if (g_mkdir (trashdir, 0700) == -1)
1444 if (trashdir == NULL)
1446 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1447 dirname = g_strdup_printf (".Trash-%s", uid_str);
1448 trashdir = g_build_filename (topdir, dirname, NULL);
1451 if (g_lstat (trashdir, &trash_stat) == 0)
1453 if (!S_ISDIR (trash_stat.st_mode) ||
1454 trash_stat.st_uid != uid)
1456 /* Not a directory or not owned by user, ignore */
1461 else if (g_mkdir (trashdir, 0700) == -1)
1468 if (trashdir == NULL)
1471 g_set_error (error, G_IO_ERROR,
1472 G_IO_ERROR_NOT_SUPPORTED,
1473 _("Unable to find or create trash directory"));
1478 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1480 infodir = g_build_filename (trashdir, "info", NULL);
1481 filesdir = g_build_filename (trashdir, "files", NULL);
1484 /* Make sure we have the subdirectories */
1485 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1486 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
1491 g_set_error (error, G_IO_ERROR,
1492 G_IO_ERROR_NOT_SUPPORTED,
1493 _("Unable to find or create trash directory"));
1497 basename = g_path_get_basename (local->filename);
1505 trashname = get_unique_filename (basename, i++);
1506 infoname = g_strconcat (trashname, ".trashinfo", NULL);
1507 infofile = g_build_filename (infodir, infoname, NULL);
1510 fd = open (infofile, O_CREAT | O_EXCL, 0666);
1511 } while (fd == -1 && errno == EEXIST);
1523 g_set_error (error, G_IO_ERROR,
1524 g_io_error_from_errno (errno),
1525 _("Unable to create trashed file: %s"),
1526 g_strerror (errno));
1532 /* TODO: Maybe we should verify that you can delete the file from the trash
1533 before moving it? OTOH, that is hard, as it needs a recursive scan */
1535 trashfile = g_build_filename (filesdir, trashname, NULL);
1539 if (g_rename (local->filename, trashfile) == -1)
1546 g_set_error (error, G_IO_ERROR,
1547 g_io_error_from_errno (errno),
1548 _("Unable to trash file: %s"),
1549 g_strerror (errno));
1555 /* TODO: Do we need to update mtime/atime here after the move? */
1557 /* Use absolute names for homedir */
1558 if (is_homedir_trash)
1559 original_name = g_strdup (local->filename);
1561 original_name = try_make_relative (local->filename, topdir);
1562 original_name_escaped = escape_trash_name (original_name);
1564 g_free (original_name);
1568 localtime_r (&t, &now);
1570 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
1572 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
1573 original_name_escaped, delete_time);
1575 g_file_set_contents (infofile, data, -1, NULL);
1579 g_free (original_name_escaped);
1586 g_local_file_make_directory (GFile *file,
1587 GCancellable *cancellable,
1590 GLocalFile *local = G_LOCAL_FILE (file);
1592 if (g_mkdir (local->filename, 0755) == -1)
1596 if (errsv == EINVAL)
1597 /* This must be an invalid filename, on e.g. FAT */
1598 g_set_error (error, G_IO_ERROR,
1599 G_IO_ERROR_INVALID_FILENAME,
1600 _("Invalid filename"));
1602 g_set_error (error, G_IO_ERROR,
1603 g_io_error_from_errno (errsv),
1604 _("Error removing file: %s"),
1605 g_strerror (errsv));
1613 g_local_file_make_symbolic_link (GFile *file,
1614 const char *symlink_value,
1615 GCancellable *cancellable,
1619 GLocalFile *local = G_LOCAL_FILE (file);
1621 if (symlink (symlink_value, local->filename) == -1)
1625 if (errsv == EINVAL)
1626 /* This must be an invalid filename, on e.g. FAT */
1627 g_set_error (error, G_IO_ERROR,
1628 G_IO_ERROR_INVALID_FILENAME,
1629 _("Invalid filename"));
1631 g_set_error (error, G_IO_ERROR,
1632 g_io_error_from_errno (errsv),
1633 _("Error making symbolic link: %s"),
1634 g_strerror (errsv));
1639 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
1646 g_local_file_copy (GFile *source,
1648 GFileCopyFlags flags,
1649 GCancellable *cancellable,
1650 GFileProgressCallback progress_callback,
1651 gpointer progress_callback_data,
1654 /* Fall back to default copy */
1655 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
1660 g_local_file_move (GFile *source,
1662 GFileCopyFlags flags,
1663 GCancellable *cancellable,
1664 GFileProgressCallback progress_callback,
1665 gpointer progress_callback_data,
1668 GLocalFile *local_source = G_LOCAL_FILE (source);
1669 GLocalFile *local_destination = G_LOCAL_FILE (destination);
1670 struct stat statbuf;
1671 gboolean destination_exist, source_is_dir;
1675 res = g_lstat (local_source->filename, &statbuf);
1678 g_set_error (error, G_IO_ERROR,
1679 g_io_error_from_errno (errno),
1680 _("Error moving file: %s"),
1681 g_strerror (errno));
1685 source_is_dir = S_ISDIR (statbuf.st_mode);
1687 destination_exist = FALSE;
1688 res = g_lstat (local_destination->filename, &statbuf);
1691 destination_exist = TRUE; /* Target file exists */
1693 if (flags & G_FILE_COPY_OVERWRITE)
1695 /* Always fail on dirs, even with overwrite */
1696 if (S_ISDIR (statbuf.st_mode))
1700 G_IO_ERROR_WOULD_MERGE,
1701 _("Can't move directory over directory"));
1710 _("Target file already exists"));
1715 if (flags & G_FILE_COPY_BACKUP && destination_exist)
1717 backup_name = g_strconcat (local_destination->filename, "~", NULL);
1718 if (rename (local_destination->filename, backup_name) == -1)
1722 G_IO_ERROR_CANT_CREATE_BACKUP,
1723 _("Backup file creation failed"));
1724 g_free (backup_name);
1727 g_free (backup_name);
1728 destination_exist = FALSE; /* It did, but no more */
1731 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
1733 /* Source is a dir, destination exists (and is not a dir, because that would have failed
1734 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
1735 res = unlink (local_destination->filename);
1738 g_set_error (error, G_IO_ERROR,
1739 g_io_error_from_errno (errno),
1740 _("Error removing target file: %s"),
1741 g_strerror (errno));
1746 if (rename (local_source->filename, local_destination->filename) == -1)
1752 if (errsv == EINVAL)
1753 /* This must be an invalid filename, on e.g. FAT, or
1754 we're trying to move the file into itself...
1755 We return invalid filename for both... */
1756 g_set_error (error, G_IO_ERROR,
1757 G_IO_ERROR_INVALID_FILENAME,
1758 _("Invalid filename"));
1760 g_set_error (error, G_IO_ERROR,
1761 g_io_error_from_errno (errsv),
1762 _("Error moving file: %s"),
1763 g_strerror (errsv));
1771 if (!g_file_copy (source, destination, G_FILE_COPY_OVERWRITE | G_FILE_COPY_ALL_METADATA, cancellable,
1772 progress_callback, progress_callback_data,
1776 return g_file_delete (source, cancellable, error);
1780 static GDirectoryMonitor*
1781 g_local_file_monitor_dir (GFile *file,
1782 GFileMonitorFlags flags,
1783 GCancellable *cancellable)
1785 GLocalFile* local_file = G_LOCAL_FILE(file);
1786 return _g_local_directory_monitor_new (local_file->filename, flags);
1789 static GFileMonitor*
1790 g_local_file_monitor_file (GFile *file,
1791 GFileMonitorFlags flags,
1792 GCancellable *cancellable)
1794 GLocalFile* local_file = G_LOCAL_FILE(file);
1795 return _g_local_file_monitor_new (local_file->filename, flags);
1799 g_local_file_file_iface_init (GFileIface *iface)
1801 iface->dup = g_local_file_dup;
1802 iface->hash = g_local_file_hash;
1803 iface->equal = g_local_file_equal;
1804 iface->is_native = g_local_file_is_native;
1805 iface->has_uri_scheme = g_local_file_has_uri_scheme;
1806 iface->get_uri_scheme = g_local_file_get_uri_scheme;
1807 iface->get_basename = g_local_file_get_basename;
1808 iface->get_path = g_local_file_get_path;
1809 iface->get_uri = g_local_file_get_uri;
1810 iface->get_parse_name = g_local_file_get_parse_name;
1811 iface->get_parent = g_local_file_get_parent;
1812 iface->contains_file = g_local_file_contains_file;
1813 iface->get_relative_path = g_local_file_get_relative_path;
1814 iface->resolve_relative_path = g_local_file_resolve_relative_path;
1815 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
1816 iface->set_display_name = g_local_file_set_display_name;
1817 iface->enumerate_children = g_local_file_enumerate_children;
1818 iface->query_info = g_local_file_query_info;
1819 iface->query_filesystem_info = g_local_file_query_filesystem_info;
1820 iface->find_enclosing_volume = g_local_file_find_enclosing_volume;
1821 iface->query_settable_attributes = g_local_file_query_settable_attributes;
1822 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
1823 iface->set_attribute = g_local_file_set_attribute;
1824 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
1825 iface->read = g_local_file_read;
1826 iface->append_to = g_local_file_append_to;
1827 iface->create = g_local_file_create;
1828 iface->replace = g_local_file_replace;
1829 iface->delete_file = g_local_file_delete;
1830 iface->trash = g_local_file_trash;
1831 iface->make_directory = g_local_file_make_directory;
1832 iface->make_symbolic_link = g_local_file_make_symbolic_link;
1833 iface->copy = g_local_file_copy;
1834 iface->move = g_local_file_move;
1835 iface->monitor_dir = g_local_file_monitor_dir;
1836 iface->monitor_file = g_local_file_monitor_file;