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>
80 static void g_local_file_file_iface_init (GFileIface *iface);
82 static GFileAttributeInfoList *local_writable_attributes = NULL;
83 static GFileAttributeInfoList *local_writable_namespaces = NULL;
87 GObject parent_instance;
92 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
93 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
94 g_local_file_file_iface_init))
96 static char * find_mountpoint_for (const char *file, dev_t dev);
99 g_local_file_finalize (GObject *object)
103 local = G_LOCAL_FILE (object);
105 g_free (local->filename);
107 if (G_OBJECT_CLASS (g_local_file_parent_class)->finalize)
108 (*G_OBJECT_CLASS (g_local_file_parent_class)->finalize) (object);
112 g_local_file_class_init (GLocalFileClass *klass)
114 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
115 GFileAttributeInfoList *list;
117 gobject_class->finalize = g_local_file_finalize;
119 /* Set up attribute lists */
121 /* Writable attributes: */
123 list = g_file_attribute_info_list_new ();
125 g_file_attribute_info_list_add (list,
126 G_FILE_ATTRIBUTE_UNIX_MODE,
127 G_FILE_ATTRIBUTE_TYPE_UINT32,
128 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
131 g_file_attribute_info_list_add (list,
132 G_FILE_ATTRIBUTE_UNIX_UID,
133 G_FILE_ATTRIBUTE_TYPE_UINT32,
134 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
135 g_file_attribute_info_list_add (list,
136 G_FILE_ATTRIBUTE_UNIX_GID,
137 G_FILE_ATTRIBUTE_TYPE_UINT32,
138 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
142 g_file_attribute_info_list_add (list,
143 G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET,
144 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
149 g_file_attribute_info_list_add (list,
150 G_FILE_ATTRIBUTE_TIME_MODIFIED,
151 G_FILE_ATTRIBUTE_TYPE_UINT64,
152 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
153 g_file_attribute_info_list_add (list,
154 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
155 G_FILE_ATTRIBUTE_TYPE_UINT32,
156 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
157 g_file_attribute_info_list_add (list,
158 G_FILE_ATTRIBUTE_TIME_ACCESS,
159 G_FILE_ATTRIBUTE_TYPE_UINT64,
160 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
161 g_file_attribute_info_list_add (list,
162 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
163 G_FILE_ATTRIBUTE_TYPE_UINT32,
164 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
167 local_writable_attributes = list;
169 /* Writable namespaces: */
171 list = g_file_attribute_info_list_new ();
174 g_file_attribute_info_list_add (list,
176 G_FILE_ATTRIBUTE_TYPE_STRING,
177 G_FILE_ATTRIBUTE_FLAGS_COPY_WITH_FILE |
178 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
179 g_file_attribute_info_list_add (list,
181 G_FILE_ATTRIBUTE_TYPE_STRING,
182 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
185 local_writable_namespaces = list;
189 g_local_file_init (GLocalFile *local)
195 canonicalize_filename (const char *filename)
197 char *canon, *start, *p, *q;
200 if (!g_path_is_absolute (filename))
202 cwd = g_get_current_dir ();
203 canon = g_build_filename (cwd, filename, NULL);
207 canon = g_strdup (filename);
209 start = (char *)g_path_skip_root (canon);
214 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
216 memmove (p, p+1, strlen (p+1)+1);
218 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
221 /* Skip previous separator */
225 while (p > start && !G_IS_DIR_SEPARATOR (*p))
227 if (G_IS_DIR_SEPARATOR (*p))
228 *p++ = G_DIR_SEPARATOR;
229 memmove (p, q, strlen (q)+1);
233 /* Skip until next separator */
234 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
239 /* Canonicalize one separator */
240 *p++ = G_DIR_SEPARATOR;
244 /* Remove additional separators */
246 while (*q && G_IS_DIR_SEPARATOR (*q))
250 memmove (p, q, strlen (q)+1);
253 /* Remove trailing slashes */
254 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
262 * @filename: filename of the file to create.
264 * Returns: new local #GFile.
267 g_local_file_new (const char *filename)
271 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
272 local->filename = canonicalize_filename (filename);
274 return G_FILE (local);
278 g_local_file_is_native (GFile *file)
284 g_local_file_has_uri_scheme (GFile *file,
285 const char *uri_scheme)
287 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
291 g_local_file_get_uri_scheme (GFile *file)
293 return g_strdup ("file");
297 g_local_file_get_basename (GFile *file)
299 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
303 g_local_file_get_path (GFile *file)
305 return g_strdup (G_LOCAL_FILE (file)->filename);
309 g_local_file_get_uri (GFile *file)
311 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
315 get_filename_charset (const gchar **filename_charset)
317 const gchar **charsets;
320 is_utf8 = g_get_filename_charsets (&charsets);
322 if (filename_charset)
323 *filename_charset = charsets[0];
329 name_is_valid_for_display (const char *string,
330 gboolean is_valid_utf8)
334 if (!is_valid_utf8 &&
335 !g_utf8_validate (string, -1, NULL))
338 while ((c = *string++) != 0)
340 if (g_ascii_iscntrl(c))
348 g_local_file_get_parse_name (GFile *file)
350 const char *filename;
352 const gchar *charset;
354 char *roundtripped_filename;
355 gboolean free_utf8_filename;
356 gboolean is_valid_utf8;
358 filename = G_LOCAL_FILE (file)->filename;
359 if (get_filename_charset (&charset))
361 utf8_filename = (char *)filename;
362 free_utf8_filename = FALSE;
363 is_valid_utf8 = FALSE; /* Can't guarantee this */
367 utf8_filename = g_convert (filename, -1,
368 "UTF-8", charset, NULL, NULL, NULL);
369 free_utf8_filename = TRUE;
370 is_valid_utf8 = TRUE;
372 if (utf8_filename != NULL)
374 /* Make sure we can roundtrip: */
375 roundtripped_filename = g_convert (utf8_filename, -1,
376 charset, "UTF-8", NULL, NULL, NULL);
378 if (roundtripped_filename == NULL ||
379 strcmp (utf8_filename, roundtripped_filename) != 0)
381 g_free (utf8_filename);
382 utf8_filename = NULL;
388 if (utf8_filename != NULL &&
389 name_is_valid_for_display (utf8_filename, is_valid_utf8))
391 if (free_utf8_filename)
392 parse_name = utf8_filename;
394 parse_name = g_strdup (utf8_filename);
398 parse_name = g_filename_to_uri (filename, NULL, NULL);
399 if (free_utf8_filename)
400 g_free (utf8_filename);
407 g_local_file_get_parent (GFile *file)
409 GLocalFile *local = G_LOCAL_FILE (file);
410 const char *non_root;
415 non_root = g_path_skip_root (local->filename);
419 dirname = g_path_get_dirname (local->filename);
420 parent = g_local_file_new (dirname);
426 g_local_file_dup (GFile *file)
428 GLocalFile *local = G_LOCAL_FILE (file);
430 return g_local_file_new (local->filename);
434 g_local_file_hash (GFile *file)
436 GLocalFile *local = G_LOCAL_FILE (file);
438 return g_str_hash (local->filename);
442 g_local_file_equal (GFile *file1,
445 GLocalFile *local1 = G_LOCAL_FILE (file1);
446 GLocalFile *local2 = G_LOCAL_FILE (file2);
448 return g_str_equal (local1->filename, local2->filename);
452 match_prefix (const char *path, const char *prefix)
456 prefix_len = strlen (prefix);
457 if (strncmp (path, prefix, prefix_len) != 0)
459 return path + prefix_len;
463 g_local_file_contains_file (GFile *parent,
466 GLocalFile *parent_local = G_LOCAL_FILE (parent);
467 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
468 const char *remainder;
470 remainder = match_prefix (descendant_local->filename, parent_local->filename);
471 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
477 g_local_file_get_relative_path (GFile *parent,
480 GLocalFile *parent_local = G_LOCAL_FILE (parent);
481 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
482 const char *remainder;
484 remainder = match_prefix (descendant_local->filename, parent_local->filename);
486 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
487 return g_strdup (remainder + 1);
492 g_local_file_resolve_relative_path (GFile *file,
493 const char *relative_path)
495 GLocalFile *local = G_LOCAL_FILE (file);
499 if (g_path_is_absolute (relative_path))
500 return g_local_file_new (relative_path);
502 filename = g_build_filename (local->filename, relative_path, NULL);
503 child = g_local_file_new (filename);
509 static GFileEnumerator *
510 g_local_file_enumerate_children (GFile *file,
511 const char *attributes,
512 GFileQueryInfoFlags flags,
513 GCancellable *cancellable,
516 GLocalFile *local = G_LOCAL_FILE (file);
517 return g_local_file_enumerator_new (local->filename,
523 g_local_file_get_child_for_display_name (GFile *file,
524 const char *display_name,
527 GFile *parent, *new_file;
530 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
531 if (basename == NULL)
533 g_set_error (error, G_IO_ERROR,
534 G_IO_ERROR_INVALID_FILENAME,
535 _("Invalid filename %s"), display_name);
539 parent = g_file_get_parent (file);
540 new_file = g_file_get_child (file, basename);
541 g_object_unref (parent);
549 get_fs_type (long f_type)
552 /* filesystem ids taken from linux manpage */
646 G_LOCK_DEFINE_STATIC(mount_info_hash);
647 static GHashTable *mount_info_hash = NULL;
648 guint64 mount_info_hash_cache_time = 0;
651 MOUNT_INFO_READONLY = 1<<0
655 device_equal (gconstpointer v1,
658 return *(dev_t *)v1 == * (dev_t *)v2;
662 device_hash (gconstpointer v)
664 return (guint) *(dev_t *)v;
668 get_mount_info (GFileInfo *fs_info,
670 GFileAttributeMatcher *matcher)
674 gpointer info_as_ptr;
681 if (lstat (path, &buf) != 0)
684 G_LOCK (mount_info_hash);
686 if (mount_info_hash == NULL)
687 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
691 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
692 g_hash_table_remove_all (mount_info_hash);
694 got_info = g_hash_table_lookup_extended (mount_info_hash,
699 G_UNLOCK (mount_info_hash);
701 mount_info = GPOINTER_TO_UINT (info_as_ptr);
707 mountpoint = find_mountpoint_for (path, buf.st_dev);
708 if (mountpoint == NULL)
711 mount = g_get_unix_mount_at (mountpoint, &cache_time);
714 if (g_unix_mount_is_readonly (mount))
715 mount_info |= MOUNT_INFO_READONLY;
717 g_unix_mount_free (mount);
720 dev = g_new0 (dev_t, 1);
723 G_LOCK (mount_info_hash);
724 mount_info_hash_cache_time = cache_time;
725 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
726 G_UNLOCK (mount_info_hash);
729 if (mount_info & MOUNT_INFO_READONLY)
730 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FS_READONLY, TRUE);
734 g_local_file_query_filesystem_info (GFile *file,
735 const char *attributes,
736 GCancellable *cancellable,
739 GLocalFile *local = G_LOCAL_FILE (file);
745 struct statfs statfs_buffer;
747 #elif defined(USE_STATVFS)
748 struct statvfs statfs_buffer;
750 GFileAttributeMatcher *attribute_matcher;
757 statfs_result = statfs (local->filename, &statfs_buffer);
758 #elif STATFS_ARGS == 4
759 statfs_result = statfs (local->filename, &statfs_buffer,
760 sizeof (statfs_buffer), 0);
762 block_size = statfs_buffer.f_bsize;
764 #if defined(__linux__)
765 /* ncpfs does not know the amount of available and free space *
766 * assuming ncpfs is linux specific, if you are on a non-linux platform
767 * where ncpfs is available, please file a bug about it on bugzilla.gnome.org
769 if (statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0 &&
770 /* linux/ncp_fs.h: NCP_SUPER_MAGIC == 0x564c */
771 statfs_buffer.f_type == 0x564c)
775 #elif defined(USE_STATVFS)
776 statfs_result = statvfs (local->filename, &statfs_buffer);
777 block_size = statfs_buffer.f_frsize;
780 if (statfs_result == -1)
782 g_set_error (error, G_IO_ERROR,
783 g_io_error_from_errno (errno),
784 _("Error getting filesystem info: %s"),
789 info = g_file_info_new ();
791 attribute_matcher = g_file_attribute_matcher_new (attributes);
793 if (g_file_attribute_matcher_matches (attribute_matcher,
794 G_FILE_ATTRIBUTE_FS_FREE))
795 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FS_FREE, block_size * statfs_buffer.f_bavail);
796 if (g_file_attribute_matcher_matches (attribute_matcher,
797 G_FILE_ATTRIBUTE_FS_SIZE))
798 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FS_SIZE, block_size * statfs_buffer.f_blocks);
801 fstype = get_fs_type (statfs_buffer.f_type);
803 g_file_attribute_matcher_matches (attribute_matcher,
804 G_FILE_ATTRIBUTE_FS_TYPE))
805 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FS_TYPE, fstype);
808 if (g_file_attribute_matcher_matches (attribute_matcher,
809 G_FILE_ATTRIBUTE_FS_READONLY))
811 get_mount_info (info, local->filename, attribute_matcher);
814 g_file_attribute_matcher_unref (attribute_matcher);
820 g_local_file_find_enclosing_volume (GFile *file,
821 GCancellable *cancellable,
824 GLocalFile *local = G_LOCAL_FILE (file);
829 if (lstat (local->filename, &buf) != 0)
831 g_set_error (error, G_IO_ERROR,
832 G_IO_ERROR_NOT_FOUND,
833 _("Containing volume does not exist"));
837 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
838 if (mountpoint == NULL)
840 g_set_error (error, G_IO_ERROR,
841 G_IO_ERROR_NOT_FOUND,
842 _("Containing volume does not exist"));
846 volume = g_volume_get_for_mount_path (mountpoint);
851 g_set_error (error, G_IO_ERROR,
852 G_IO_ERROR_NOT_FOUND,
853 _("Containing volume does not exist"));
858 g_local_file_set_display_name (GFile *file,
859 const char *display_name,
860 GCancellable *cancellable,
863 GLocalFile *local, *new_local;
864 GFile *new_file, *parent;
868 parent = g_file_get_parent (file);
871 g_set_error (error, G_IO_ERROR,
873 _("Can't rename root directory"));
877 new_file = g_file_get_child_for_display_name (parent, display_name, error);
878 g_object_unref (parent);
880 if (new_file == NULL)
883 local = G_LOCAL_FILE (file);
884 new_local = G_LOCAL_FILE (new_file);
886 if (!(g_lstat (new_local->filename, &statbuf) == -1 &&
889 g_set_error (error, G_IO_ERROR,
891 _("Can't rename file, filename already exist"));
895 if (rename (local->filename, new_local->filename) == -1)
900 /* We can't get a rename file into itself error herer,
901 so this must be an invalid filename, on e.g. FAT */
902 g_set_error (error, G_IO_ERROR,
903 G_IO_ERROR_INVALID_FILENAME,
904 _("Invalid filename"));
906 g_set_error (error, G_IO_ERROR,
907 g_io_error_from_errno (errsv),
908 _("Error renaming file: %s"),
910 g_object_unref (new_file);
918 g_local_file_query_info (GFile *file,
919 const char *attributes,
920 GFileQueryInfoFlags flags,
921 GCancellable *cancellable,
924 GLocalFile *local = G_LOCAL_FILE (file);
926 GFileAttributeMatcher *matcher;
927 char *basename, *dirname;
928 GLocalParentFileInfo parent_info;
930 matcher = g_file_attribute_matcher_new (attributes);
932 basename = g_path_get_basename (local->filename);
934 dirname = g_path_get_dirname (local->filename);
935 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
938 info = _g_local_file_info_get (basename, local->filename,
939 matcher, flags, &parent_info,
944 g_file_attribute_matcher_unref (matcher);
949 static GFileAttributeInfoList *
950 g_local_file_query_settable_attributes (GFile *file,
951 GCancellable *cancellable,
954 return g_file_attribute_info_list_ref (local_writable_attributes);
957 static GFileAttributeInfoList *
958 g_local_file_query_writable_namespaces (GFile *file,
959 GCancellable *cancellable,
962 return g_file_attribute_info_list_ref (local_writable_namespaces);
966 g_local_file_set_attribute (GFile *file,
967 const char *attribute,
968 const GFileAttributeValue *value,
969 GFileQueryInfoFlags flags,
970 GCancellable *cancellable,
973 GLocalFile *local = G_LOCAL_FILE (file);
975 return _g_local_file_info_set_attribute (local->filename,
984 g_local_file_set_attributes_from_info (GFile *file,
986 GFileQueryInfoFlags flags,
987 GCancellable *cancellable,
990 GLocalFile *local = G_LOCAL_FILE (file);
991 int res, chained_res;
992 GFileIface* default_iface;
994 res = _g_local_file_info_set_attributes (local->filename,
1000 error = NULL; /* Don't write over error if further errors */
1002 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1004 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1006 return res && chained_res;
1009 static GFileInputStream *
1010 g_local_file_read (GFile *file,
1011 GCancellable *cancellable,
1014 GLocalFile *local = G_LOCAL_FILE (file);
1018 fd = g_open (local->filename, O_RDONLY, 0);
1021 g_set_error (error, G_IO_ERROR,
1022 g_io_error_from_errno (errno),
1023 _("Error opening file: %s"),
1024 g_strerror (errno));
1028 if (fstat(fd, &buf) == 0 && S_ISDIR (buf.st_mode))
1031 g_set_error (error, G_IO_ERROR,
1032 G_IO_ERROR_IS_DIRECTORY,
1033 _("Can't open directory"));
1037 return g_local_file_input_stream_new (fd);
1040 static GFileOutputStream *
1041 g_local_file_append_to (GFile *file,
1042 GFileCreateFlags flags,
1043 GCancellable *cancellable,
1046 return g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1047 flags, cancellable, error);
1050 static GFileOutputStream *
1051 g_local_file_create (GFile *file,
1052 GFileCreateFlags flags,
1053 GCancellable *cancellable,
1056 return g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1057 flags, cancellable, error);
1060 static GFileOutputStream *
1061 g_local_file_replace (GFile *file,
1063 gboolean make_backup,
1064 GFileCreateFlags flags,
1065 GCancellable *cancellable,
1068 return g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1069 etag, make_backup, flags,
1070 cancellable, error);
1075 g_local_file_delete (GFile *file,
1076 GCancellable *cancellable,
1079 GLocalFile *local = G_LOCAL_FILE (file);
1081 if (g_remove (local->filename) == -1)
1083 g_set_error (error, G_IO_ERROR,
1084 g_io_error_from_errno (errno),
1085 _("Error removing file: %s"),
1086 g_strerror (errno));
1094 strip_trailing_slashes (const char *path)
1099 path_copy = g_strdup (path);
1100 len = strlen (path_copy);
1101 while (len > 1 && path_copy[len-1] == '/')
1102 path_copy[--len] = 0;
1108 expand_symlink (const char *link)
1110 char *resolved, *canonical, *parent, *link2;
1111 char symlink_value[4096];
1114 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1116 return g_strdup (link);
1117 symlink_value[res] = 0;
1119 if (g_path_is_absolute (symlink_value))
1120 return canonicalize_filename (symlink_value);
1123 link2 = strip_trailing_slashes (link);
1124 parent = g_path_get_dirname (link2);
1127 resolved = g_build_filename (parent, symlink_value, NULL);
1130 canonical = canonicalize_filename (resolved);
1139 get_parent (const char *path, dev_t *parent_dev)
1142 struct stat parent_stat;
1146 path_copy = strip_trailing_slashes (path);
1148 parent = g_path_get_dirname (path_copy);
1149 if (strcmp (parent, ".") == 0 ||
1150 strcmp (parent, path_copy) == 0)
1159 if (g_lstat (parent, &parent_stat) != 0)
1165 if (S_ISLNK (parent_stat.st_mode))
1168 parent = expand_symlink (parent);
1173 if (num_recursions > 12)
1178 } while (S_ISLNK (parent_stat.st_mode));
1180 *parent_dev = parent_stat.st_dev;
1186 expand_all_symlinks (const char *path)
1188 char *parent, *parent_expanded;
1189 char *basename, *res;
1192 parent = get_parent (path, &parent_dev);
1195 parent_expanded = expand_all_symlinks (parent);
1197 basename = g_path_get_basename (path);
1198 res = g_build_filename (parent_expanded, basename, NULL);
1200 g_free (parent_expanded);
1203 res = g_strdup (path);
1209 find_mountpoint_for (const char *file, dev_t dev)
1212 dev_t dir_dev, parent_dev;
1214 dir = g_strdup (file);
1218 parent = get_parent (dir, &parent_dev);
1222 if (parent_dev != dir_dev)
1234 find_topdir_for (const char *file)
1239 dir = get_parent (file, &dir_dev);
1243 return find_mountpoint_for (dir, dir_dev);
1247 get_unique_filename (const char *basename, int id)
1252 return g_strdup (basename);
1254 dot = strchr (basename, '.');
1256 return g_strdup_printf ("%.*s.%d%s", dot - basename, basename, id, dot);
1258 return g_strdup_printf ("%s.%d", basename, id);
1262 path_has_prefix (const char *path, const char *prefix)
1269 prefix_len = strlen (prefix);
1271 if (strncmp (path, prefix, prefix_len) == 0 &&
1272 (prefix_len == 0 || /* empty prefix always matches */
1273 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1274 path[prefix_len] == 0 ||
1275 path[prefix_len] == '/'))
1282 try_make_relative (const char *path, const char *base)
1284 char *path2, *base2;
1287 path2 = expand_all_symlinks (path);
1288 base2 = expand_all_symlinks (base);
1291 if (path_has_prefix (path2, base2))
1293 relative = path2 + strlen (base2);
1294 while (*relative == '/')
1296 relative = g_strdup (relative);
1304 /* Failed, use abs path */
1305 return g_strdup (path);
1309 escape_trash_name (char *name)
1312 const gchar hex[16] = "0123456789ABCDEF";
1314 str = g_string_new ("");
1322 if (g_ascii_isprint (c))
1323 g_string_append_c (str, c);
1326 g_string_append_c (str, '%');
1327 g_string_append_c (str, hex[((guchar)c) >> 4]);
1328 g_string_append_c (str, hex[((guchar)c) & 0xf]);
1332 return g_string_free (str, FALSE);
1336 g_local_file_trash (GFile *file,
1337 GCancellable *cancellable,
1340 GLocalFile *local = G_LOCAL_FILE (file);
1341 struct stat file_stat, home_stat, trash_stat, global_stat;
1342 const char *homedir;
1344 char *trashdir, *globaldir, *topdir, *infodir, *filesdir;
1345 char *basename, *trashname, *trashfile, *infoname, *infofile;
1346 char *original_name, *original_name_escaped;
1351 gboolean is_homedir_trash;
1354 char delete_time[32];
1357 if (g_lstat (local->filename, &file_stat) != 0)
1359 g_set_error (error, G_IO_ERROR,
1360 g_io_error_from_errno (errno),
1361 _("Error trashing file: %s"),
1362 g_strerror (errno));
1366 homedir = g_get_home_dir ();
1367 g_stat (homedir, &home_stat);
1369 is_homedir_trash = FALSE;
1371 if (file_stat.st_dev == home_stat.st_dev)
1373 is_homedir_trash = TRUE;
1375 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1376 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1382 display_name = g_filename_display_name (trashdir);
1383 g_set_error (error, G_IO_ERROR,
1384 g_io_error_from_errno (err),
1385 _("Unable to create trash dir %s: %s"),
1386 display_name, g_strerror (err));
1387 g_free (display_name);
1391 topdir = g_strdup (g_get_user_data_dir ());
1396 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1398 topdir = find_topdir_for (local->filename);
1401 g_set_error (error, G_IO_ERROR,
1402 G_IO_ERROR_NOT_SUPPORTED,
1403 _("Unable to find toplevel directory for trash"));
1407 /* Try looking for global trash dir $topdir/.Trash/$uid */
1408 globaldir = g_build_filename (topdir, ".Trash", NULL);
1409 if (g_lstat (globaldir, &global_stat) == 0 &&
1410 S_ISDIR (global_stat.st_mode) &&
1411 (global_stat.st_mode & S_ISVTX) != 0)
1413 trashdir = g_build_filename (globaldir, uid_str, NULL);
1415 if (g_lstat (trashdir, &trash_stat) == 0)
1417 if (!S_ISDIR (trash_stat.st_mode) ||
1418 trash_stat.st_uid != uid)
1420 /* Not a directory or not owned by user, ignore */
1425 else if (g_mkdir (trashdir, 0700) == -1)
1433 if (trashdir == NULL)
1435 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1436 dirname = g_strdup_printf (".Trash-%s", uid_str);
1437 trashdir = g_build_filename (topdir, dirname, NULL);
1440 if (g_lstat (trashdir, &trash_stat) == 0)
1442 if (!S_ISDIR (trash_stat.st_mode) ||
1443 trash_stat.st_uid != uid)
1445 /* Not a directory or not owned by user, ignore */
1450 else if (g_mkdir (trashdir, 0700) == -1)
1457 if (trashdir == NULL)
1460 g_set_error (error, G_IO_ERROR,
1461 G_IO_ERROR_NOT_SUPPORTED,
1462 _("Unable to find or create trash directory"));
1467 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1469 infodir = g_build_filename (trashdir, "info", NULL);
1470 filesdir = g_build_filename (trashdir, "files", NULL);
1473 /* Make sure we have the subdirectories */
1474 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1475 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
1480 g_set_error (error, G_IO_ERROR,
1481 G_IO_ERROR_NOT_SUPPORTED,
1482 _("Unable to find or create trash directory"));
1486 basename = g_path_get_basename (local->filename);
1494 trashname = get_unique_filename (basename, i++);
1495 infoname = g_strconcat (trashname, ".trashinfo", NULL);
1496 infofile = g_build_filename (infodir, infoname, NULL);
1499 fd = open (infofile, O_CREAT | O_EXCL, 0666);
1500 } while (fd == -1 && errno == EEXIST);
1512 g_set_error (error, G_IO_ERROR,
1513 g_io_error_from_errno (errno),
1514 _("Unable to create trashed file: %s"),
1515 g_strerror (errno));
1521 /* TODO: Maybe we should verify that you can delete the file from the trash
1522 before moving it? OTOH, that is hard, as it needs a recursive scan */
1524 trashfile = g_build_filename (filesdir, trashname, NULL);
1528 if (g_rename (local->filename, trashfile) == -1)
1535 g_set_error (error, G_IO_ERROR,
1536 g_io_error_from_errno (errno),
1537 _("Unable to trash file: %s"),
1538 g_strerror (errno));
1544 /* TODO: Do we need to update mtime/atime here after the move? */
1546 /* Use absolute names for homedir */
1547 if (is_homedir_trash)
1548 original_name = g_strdup (local->filename);
1550 original_name = try_make_relative (local->filename, topdir);
1551 original_name_escaped = escape_trash_name (original_name);
1553 g_free (original_name);
1557 localtime_r (&t, &now);
1559 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
1561 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
1562 original_name_escaped, delete_time);
1564 g_file_set_contents (infofile, data, -1, NULL);
1568 g_free (original_name_escaped);
1575 g_local_file_make_directory (GFile *file,
1576 GCancellable *cancellable,
1579 GLocalFile *local = G_LOCAL_FILE (file);
1581 if (g_mkdir (local->filename, 0755) == -1)
1585 if (errsv == EINVAL)
1586 /* This must be an invalid filename, on e.g. FAT */
1587 g_set_error (error, G_IO_ERROR,
1588 G_IO_ERROR_INVALID_FILENAME,
1589 _("Invalid filename"));
1591 g_set_error (error, G_IO_ERROR,
1592 g_io_error_from_errno (errsv),
1593 _("Error removing file: %s"),
1594 g_strerror (errsv));
1602 g_local_file_make_symbolic_link (GFile *file,
1603 const char *symlink_value,
1604 GCancellable *cancellable,
1608 GLocalFile *local = G_LOCAL_FILE (file);
1610 if (symlink (symlink_value, local->filename) == -1)
1614 if (errsv == EINVAL)
1615 /* This must be an invalid filename, on e.g. FAT */
1616 g_set_error (error, G_IO_ERROR,
1617 G_IO_ERROR_INVALID_FILENAME,
1618 _("Invalid filename"));
1620 g_set_error (error, G_IO_ERROR,
1621 g_io_error_from_errno (errsv),
1622 _("Error making symbolic link: %s"),
1623 g_strerror (errsv));
1628 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
1635 g_local_file_copy (GFile *source,
1637 GFileCopyFlags flags,
1638 GCancellable *cancellable,
1639 GFileProgressCallback progress_callback,
1640 gpointer progress_callback_data,
1643 /* Fall back to default copy */
1644 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
1649 g_local_file_move (GFile *source,
1651 GFileCopyFlags flags,
1652 GCancellable *cancellable,
1653 GFileProgressCallback progress_callback,
1654 gpointer progress_callback_data,
1657 GLocalFile *local_source = G_LOCAL_FILE (source);
1658 GLocalFile *local_destination = G_LOCAL_FILE (destination);
1659 struct stat statbuf;
1660 gboolean destination_exist, source_is_dir;
1664 res = g_lstat (local_source->filename, &statbuf);
1667 g_set_error (error, G_IO_ERROR,
1668 g_io_error_from_errno (errno),
1669 _("Error moving file: %s"),
1670 g_strerror (errno));
1674 source_is_dir = S_ISDIR (statbuf.st_mode);
1676 destination_exist = FALSE;
1677 res = g_lstat (local_destination->filename, &statbuf);
1680 destination_exist = TRUE; /* Target file exists */
1682 if (flags & G_FILE_COPY_OVERWRITE)
1684 /* Always fail on dirs, even with overwrite */
1685 if (S_ISDIR (statbuf.st_mode))
1689 G_IO_ERROR_WOULD_MERGE,
1690 _("Can't move directory over directory"));
1699 _("Target file already exists"));
1704 if (flags & G_FILE_COPY_BACKUP && destination_exist)
1706 backup_name = g_strconcat (local_destination->filename, "~", NULL);
1707 if (rename (local_destination->filename, backup_name) == -1)
1711 G_IO_ERROR_CANT_CREATE_BACKUP,
1712 _("Backup file creation failed"));
1713 g_free (backup_name);
1716 g_free (backup_name);
1717 destination_exist = FALSE; /* It did, but no more */
1720 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
1722 /* Source is a dir, destination exists (and is not a dir, because that would have failed
1723 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
1724 res = unlink (local_destination->filename);
1727 g_set_error (error, G_IO_ERROR,
1728 g_io_error_from_errno (errno),
1729 _("Error removing target file: %s"),
1730 g_strerror (errno));
1735 if (rename (local_source->filename, local_destination->filename) == -1)
1741 if (errsv == EINVAL)
1742 /* This must be an invalid filename, on e.g. FAT, or
1743 we're trying to move the file into itself...
1744 We return invalid filename for both... */
1745 g_set_error (error, G_IO_ERROR,
1746 G_IO_ERROR_INVALID_FILENAME,
1747 _("Invalid filename"));
1749 g_set_error (error, G_IO_ERROR,
1750 g_io_error_from_errno (errsv),
1751 _("Error moving file: %s"),
1752 g_strerror (errsv));
1760 if (!g_file_copy (source, destination, G_FILE_COPY_OVERWRITE | G_FILE_COPY_ALL_METADATA, cancellable,
1761 progress_callback, progress_callback_data,
1765 return g_file_delete (source, cancellable, error);
1769 static GDirectoryMonitor*
1770 g_local_file_monitor_dir (GFile* file,
1771 GFileMonitorFlags flags,
1772 GCancellable *cancellable)
1774 GLocalFile* local_file = G_LOCAL_FILE(file);
1775 return g_local_directory_monitor_new (local_file->filename, flags);
1778 static GFileMonitor*
1779 g_local_file_monitor_file (GFile* file,
1780 GFileMonitorFlags flags,
1781 GCancellable *cancellable)
1783 GLocalFile* local_file = G_LOCAL_FILE(file);
1784 return g_local_file_monitor_new (local_file->filename, flags);
1788 g_local_file_file_iface_init (GFileIface *iface)
1790 iface->dup = g_local_file_dup;
1791 iface->hash = g_local_file_hash;
1792 iface->equal = g_local_file_equal;
1793 iface->is_native = g_local_file_is_native;
1794 iface->has_uri_scheme = g_local_file_has_uri_scheme;
1795 iface->get_uri_scheme = g_local_file_get_uri_scheme;
1796 iface->get_basename = g_local_file_get_basename;
1797 iface->get_path = g_local_file_get_path;
1798 iface->get_uri = g_local_file_get_uri;
1799 iface->get_parse_name = g_local_file_get_parse_name;
1800 iface->get_parent = g_local_file_get_parent;
1801 iface->contains_file = g_local_file_contains_file;
1802 iface->get_relative_path = g_local_file_get_relative_path;
1803 iface->resolve_relative_path = g_local_file_resolve_relative_path;
1804 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
1805 iface->set_display_name = g_local_file_set_display_name;
1806 iface->enumerate_children = g_local_file_enumerate_children;
1807 iface->query_info = g_local_file_query_info;
1808 iface->query_filesystem_info = g_local_file_query_filesystem_info;
1809 iface->find_enclosing_volume = g_local_file_find_enclosing_volume;
1810 iface->query_settable_attributes = g_local_file_query_settable_attributes;
1811 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
1812 iface->set_attribute = g_local_file_set_attribute;
1813 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
1814 iface->read = g_local_file_read;
1815 iface->append_to = g_local_file_append_to;
1816 iface->create = g_local_file_create;
1817 iface->replace = g_local_file_replace;
1818 iface->delete_file = g_local_file_delete;
1819 iface->trash = g_local_file_trash;
1820 iface->make_directory = g_local_file_make_directory;
1821 iface->make_symbolic_link = g_local_file_make_symbolic_link;
1822 iface->copy = g_local_file_copy;
1823 iface->move = g_local_file_move;
1824 iface->monitor_dir = g_local_file_monitor_dir;
1825 iface->monitor_file = g_local_file_monitor_file;