1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
35 #include <sys/statfs.h>
37 #if HAVE_SYS_STATVFS_H
38 #include <sys/statvfs.h>
42 #elif HAVE_SYS_MOUNT_H
44 #include <sys/param.h>
46 #include <sys/mount.h>
49 #if defined(HAVE_STATFS) && defined(HAVE_STATVFS)
50 /* Some systems have both statfs and statvfs, pick the
51 most "native" for these */
52 # if defined(sun) && defined(__SVR4)
53 /* on solaris, statfs doesn't even have the
57 /* at least on linux, statfs is the actual syscall */
61 #elif defined(HAVE_STATFS)
65 #elif defined(HAVE_STATVFS)
71 #include "glocalfile.h"
72 #include "glocalfileinfo.h"
73 #include "glocalfileenumerator.h"
74 #include "glocalfileinputstream.h"
75 #include "glocalfileoutputstream.h"
76 #include "glocaldirectorymonitor.h"
77 #include "glocalfilemonitor.h"
78 #include "gvolumeprivate.h"
79 #include <glib/gstdio.h>
88 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
91 #define S_ISLNK(m) (0)
97 static void g_local_file_file_iface_init (GFileIface *iface);
99 static GFileAttributeInfoList *local_writable_attributes = NULL;
100 static GFileAttributeInfoList *local_writable_namespaces = NULL;
104 GObject parent_instance;
109 #define g_local_file_get_type _g_local_file_get_type
110 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
111 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
112 g_local_file_file_iface_init))
114 static char * find_mountpoint_for (const char *file, dev_t dev);
117 g_local_file_finalize (GObject *object)
121 local = G_LOCAL_FILE (object);
123 g_free (local->filename);
125 if (G_OBJECT_CLASS (g_local_file_parent_class)->finalize)
126 (*G_OBJECT_CLASS (g_local_file_parent_class)->finalize) (object);
130 g_local_file_class_init (GLocalFileClass *klass)
132 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133 GFileAttributeInfoList *list;
135 gobject_class->finalize = g_local_file_finalize;
137 /* Set up attribute lists */
139 /* Writable attributes: */
141 list = g_file_attribute_info_list_new ();
143 g_file_attribute_info_list_add (list,
144 G_FILE_ATTRIBUTE_UNIX_MODE,
145 G_FILE_ATTRIBUTE_TYPE_UINT32,
146 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
149 g_file_attribute_info_list_add (list,
150 G_FILE_ATTRIBUTE_UNIX_UID,
151 G_FILE_ATTRIBUTE_TYPE_UINT32,
152 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
153 g_file_attribute_info_list_add (list,
154 G_FILE_ATTRIBUTE_UNIX_GID,
155 G_FILE_ATTRIBUTE_TYPE_UINT32,
156 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
160 g_file_attribute_info_list_add (list,
161 G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET,
162 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
167 g_file_attribute_info_list_add (list,
168 G_FILE_ATTRIBUTE_TIME_MODIFIED,
169 G_FILE_ATTRIBUTE_TYPE_UINT64,
170 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
171 g_file_attribute_info_list_add (list,
172 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
173 G_FILE_ATTRIBUTE_TYPE_UINT32,
174 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
175 g_file_attribute_info_list_add (list,
176 G_FILE_ATTRIBUTE_TIME_ACCESS,
177 G_FILE_ATTRIBUTE_TYPE_UINT64,
178 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
179 g_file_attribute_info_list_add (list,
180 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
181 G_FILE_ATTRIBUTE_TYPE_UINT32,
182 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
185 local_writable_attributes = list;
187 /* Writable namespaces: */
189 list = g_file_attribute_info_list_new ();
192 g_file_attribute_info_list_add (list,
194 G_FILE_ATTRIBUTE_TYPE_STRING,
195 G_FILE_ATTRIBUTE_FLAGS_COPY_WITH_FILE |
196 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
197 g_file_attribute_info_list_add (list,
199 G_FILE_ATTRIBUTE_TYPE_STRING,
200 G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
203 local_writable_namespaces = list;
207 g_local_file_init (GLocalFile *local)
213 canonicalize_filename (const char *filename)
215 char *canon, *start, *p, *q;
218 if (!g_path_is_absolute (filename))
220 cwd = g_get_current_dir ();
221 canon = g_build_filename (cwd, filename, NULL);
225 canon = g_strdup (filename);
227 start = (char *)g_path_skip_root (canon);
232 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
234 memmove (p, p+1, strlen (p+1)+1);
236 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
239 /* Skip previous separator */
243 while (p > start && !G_IS_DIR_SEPARATOR (*p))
245 if (G_IS_DIR_SEPARATOR (*p))
246 *p++ = G_DIR_SEPARATOR;
247 memmove (p, q, strlen (q)+1);
251 /* Skip until next separator */
252 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
257 /* Canonicalize one separator */
258 *p++ = G_DIR_SEPARATOR;
262 /* Remove additional separators */
264 while (*q && G_IS_DIR_SEPARATOR (*q))
268 memmove (p, q, strlen (q)+1);
271 /* Remove trailing slashes */
272 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
280 * @filename: filename of the file to create.
282 * Returns: new local #GFile.
285 _g_local_file_new (const char *filename)
289 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
290 local->filename = canonicalize_filename (filename);
292 return G_FILE (local);
296 g_local_file_is_native (GFile *file)
302 g_local_file_has_uri_scheme (GFile *file,
303 const char *uri_scheme)
305 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
309 g_local_file_get_uri_scheme (GFile *file)
311 return g_strdup ("file");
315 g_local_file_get_basename (GFile *file)
317 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
321 g_local_file_get_path (GFile *file)
323 return g_strdup (G_LOCAL_FILE (file)->filename);
327 g_local_file_get_uri (GFile *file)
329 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
333 get_filename_charset (const gchar **filename_charset)
335 const gchar **charsets;
338 is_utf8 = g_get_filename_charsets (&charsets);
340 if (filename_charset)
341 *filename_charset = charsets[0];
347 name_is_valid_for_display (const char *string,
348 gboolean is_valid_utf8)
352 if (!is_valid_utf8 &&
353 !g_utf8_validate (string, -1, NULL))
356 while ((c = *string++) != 0)
358 if (g_ascii_iscntrl(c))
366 g_local_file_get_parse_name (GFile *file)
368 const char *filename;
370 const gchar *charset;
372 char *roundtripped_filename;
373 gboolean free_utf8_filename;
374 gboolean is_valid_utf8;
376 filename = G_LOCAL_FILE (file)->filename;
377 if (get_filename_charset (&charset))
379 utf8_filename = (char *)filename;
380 free_utf8_filename = FALSE;
381 is_valid_utf8 = FALSE; /* Can't guarantee this */
385 utf8_filename = g_convert (filename, -1,
386 "UTF-8", charset, NULL, NULL, NULL);
387 free_utf8_filename = TRUE;
388 is_valid_utf8 = TRUE;
390 if (utf8_filename != NULL)
392 /* Make sure we can roundtrip: */
393 roundtripped_filename = g_convert (utf8_filename, -1,
394 charset, "UTF-8", NULL, NULL, NULL);
396 if (roundtripped_filename == NULL ||
397 strcmp (utf8_filename, roundtripped_filename) != 0)
399 g_free (utf8_filename);
400 utf8_filename = NULL;
406 if (utf8_filename != NULL &&
407 name_is_valid_for_display (utf8_filename, is_valid_utf8))
409 if (free_utf8_filename)
410 parse_name = utf8_filename;
412 parse_name = g_strdup (utf8_filename);
416 parse_name = g_filename_to_uri (filename, NULL, NULL);
417 if (free_utf8_filename)
418 g_free (utf8_filename);
425 g_local_file_get_parent (GFile *file)
427 GLocalFile *local = G_LOCAL_FILE (file);
428 const char *non_root;
433 non_root = g_path_skip_root (local->filename);
437 dirname = g_path_get_dirname (local->filename);
438 parent = _g_local_file_new (dirname);
444 g_local_file_dup (GFile *file)
446 GLocalFile *local = G_LOCAL_FILE (file);
448 return _g_local_file_new (local->filename);
452 g_local_file_hash (GFile *file)
454 GLocalFile *local = G_LOCAL_FILE (file);
456 return g_str_hash (local->filename);
460 g_local_file_equal (GFile *file1,
463 GLocalFile *local1 = G_LOCAL_FILE (file1);
464 GLocalFile *local2 = G_LOCAL_FILE (file2);
466 return g_str_equal (local1->filename, local2->filename);
470 match_prefix (const char *path,
475 prefix_len = strlen (prefix);
476 if (strncmp (path, prefix, prefix_len) != 0)
478 return path + prefix_len;
482 g_local_file_contains_file (GFile *parent,
485 GLocalFile *parent_local = G_LOCAL_FILE (parent);
486 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
487 const char *remainder;
489 remainder = match_prefix (descendant_local->filename, parent_local->filename);
490 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
496 g_local_file_get_relative_path (GFile *parent,
499 GLocalFile *parent_local = G_LOCAL_FILE (parent);
500 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
501 const char *remainder;
503 remainder = match_prefix (descendant_local->filename, parent_local->filename);
505 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
506 return g_strdup (remainder + 1);
511 g_local_file_resolve_relative_path (GFile *file,
512 const char *relative_path)
514 GLocalFile *local = G_LOCAL_FILE (file);
518 if (g_path_is_absolute (relative_path))
519 return _g_local_file_new (relative_path);
521 filename = g_build_filename (local->filename, relative_path, NULL);
522 child = _g_local_file_new (filename);
528 static GFileEnumerator *
529 g_local_file_enumerate_children (GFile *file,
530 const char *attributes,
531 GFileQueryInfoFlags flags,
532 GCancellable *cancellable,
535 GLocalFile *local = G_LOCAL_FILE (file);
536 return _g_local_file_enumerator_new (local->filename,
542 g_local_file_get_child_for_display_name (GFile *file,
543 const char *display_name,
546 GFile *parent, *new_file;
549 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
550 if (basename == NULL)
552 g_set_error (error, G_IO_ERROR,
553 G_IO_ERROR_INVALID_FILENAME,
554 _("Invalid filename %s"), display_name);
558 parent = g_file_get_parent (file);
559 new_file = g_file_get_child (file, basename);
560 g_object_unref (parent);
568 get_fs_type (long f_type)
571 /* filesystem ids taken from linux manpage */
666 G_LOCK_DEFINE_STATIC(mount_info_hash);
667 static GHashTable *mount_info_hash = NULL;
668 guint64 mount_info_hash_cache_time = 0;
671 MOUNT_INFO_READONLY = 1<<0
675 device_equal (gconstpointer v1,
678 return *(dev_t *)v1 == * (dev_t *)v2;
682 device_hash (gconstpointer v)
684 return (guint) *(dev_t *)v;
688 get_mount_info (GFileInfo *fs_info,
690 GFileAttributeMatcher *matcher)
694 gpointer info_as_ptr;
701 if (g_lstat (path, &buf) != 0)
704 G_LOCK (mount_info_hash);
706 if (mount_info_hash == NULL)
707 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
711 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
712 g_hash_table_remove_all (mount_info_hash);
714 got_info = g_hash_table_lookup_extended (mount_info_hash,
719 G_UNLOCK (mount_info_hash);
721 mount_info = GPOINTER_TO_UINT (info_as_ptr);
727 mountpoint = find_mountpoint_for (path, buf.st_dev);
728 if (mountpoint == NULL)
731 mount = g_get_unix_mount_at (mountpoint, &cache_time);
734 if (g_unix_mount_is_readonly (mount))
735 mount_info |= MOUNT_INFO_READONLY;
737 g_unix_mount_free (mount);
740 dev = g_new0 (dev_t, 1);
743 G_LOCK (mount_info_hash);
744 mount_info_hash_cache_time = cache_time;
745 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
746 G_UNLOCK (mount_info_hash);
749 if (mount_info & MOUNT_INFO_READONLY)
750 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FS_READONLY, TRUE);
754 g_local_file_query_filesystem_info (GFile *file,
755 const char *attributes,
756 GCancellable *cancellable,
759 GLocalFile *local = G_LOCAL_FILE (file);
765 struct statfs statfs_buffer;
767 #elif defined(USE_STATVFS)
768 struct statvfs statfs_buffer;
770 GFileAttributeMatcher *attribute_matcher;
777 statfs_result = statfs (local->filename, &statfs_buffer);
778 #elif STATFS_ARGS == 4
779 statfs_result = statfs (local->filename, &statfs_buffer,
780 sizeof (statfs_buffer), 0);
782 block_size = statfs_buffer.f_bsize;
784 #if defined(__linux__)
785 /* ncpfs does not know the amount of available and free space *
786 * assuming ncpfs is linux specific, if you are on a non-linux platform
787 * where ncpfs is available, please file a bug about it on bugzilla.gnome.org
789 if (statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0 &&
790 /* linux/ncp_fs.h: NCP_SUPER_MAGIC == 0x564c */
791 statfs_buffer.f_type == 0x564c)
795 #elif defined(USE_STATVFS)
796 statfs_result = statvfs (local->filename, &statfs_buffer);
797 block_size = statfs_buffer.f_frsize;
800 if (statfs_result == -1)
802 g_set_error (error, G_IO_ERROR,
803 g_io_error_from_errno (errno),
804 _("Error getting filesystem info: %s"),
809 info = g_file_info_new ();
811 attribute_matcher = g_file_attribute_matcher_new (attributes);
813 if (g_file_attribute_matcher_matches (attribute_matcher,
814 G_FILE_ATTRIBUTE_FS_FREE))
817 gchar *localdir = g_path_get_dirname (local->filename);
818 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
822 if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
823 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FS_FREE, (guint64)li.QuadPart);
826 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FS_FREE, block_size * statfs_buffer.f_bavail);
829 if (g_file_attribute_matcher_matches (attribute_matcher,
830 G_FILE_ATTRIBUTE_FS_SIZE))
833 gchar *localdir = g_path_get_dirname (local->filename);
834 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
838 if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
839 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FS_SIZE, (guint64)li.QuadPart);
842 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FS_SIZE, block_size * statfs_buffer.f_blocks);
846 fstype = get_fs_type (statfs_buffer.f_type);
848 g_file_attribute_matcher_matches (attribute_matcher,
849 G_FILE_ATTRIBUTE_FS_TYPE))
850 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FS_TYPE, fstype);
853 if (g_file_attribute_matcher_matches (attribute_matcher,
854 G_FILE_ATTRIBUTE_FS_READONLY))
857 /* need to implement with *unix_mount* */
859 get_mount_info (info, local->filename, attribute_matcher);
863 g_file_attribute_matcher_unref (attribute_matcher);
869 g_local_file_find_enclosing_volume (GFile *file,
870 GCancellable *cancellable,
873 GLocalFile *local = G_LOCAL_FILE (file);
878 if (g_lstat (local->filename, &buf) != 0)
880 g_set_error (error, G_IO_ERROR,
881 G_IO_ERROR_NOT_FOUND,
882 _("Containing volume does not exist"));
886 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
887 if (mountpoint == NULL)
889 g_set_error (error, G_IO_ERROR,
890 G_IO_ERROR_NOT_FOUND,
891 _("Containing volume does not exist"));
895 volume = _g_volume_get_for_mount_path (mountpoint);
900 g_set_error (error, G_IO_ERROR,
901 G_IO_ERROR_NOT_FOUND,
902 _("Containing volume does not exist"));
907 g_local_file_set_display_name (GFile *file,
908 const char *display_name,
909 GCancellable *cancellable,
912 GLocalFile *local, *new_local;
913 GFile *new_file, *parent;
917 parent = g_file_get_parent (file);
920 g_set_error (error, G_IO_ERROR,
922 _("Can't rename root directory"));
926 new_file = g_file_get_child_for_display_name (parent, display_name, error);
927 g_object_unref (parent);
929 if (new_file == NULL)
932 local = G_LOCAL_FILE (file);
933 new_local = G_LOCAL_FILE (new_file);
935 if (!(g_lstat (new_local->filename, &statbuf) == -1 &&
938 g_set_error (error, G_IO_ERROR,
940 _("Can't rename file, filename already exist"));
944 if (rename (local->filename, new_local->filename) == -1)
949 /* We can't get a rename file into itself error herer,
950 so this must be an invalid filename, on e.g. FAT */
951 g_set_error (error, G_IO_ERROR,
952 G_IO_ERROR_INVALID_FILENAME,
953 _("Invalid filename"));
955 g_set_error (error, G_IO_ERROR,
956 g_io_error_from_errno (errsv),
957 _("Error renaming file: %s"),
959 g_object_unref (new_file);
967 g_local_file_query_info (GFile *file,
968 const char *attributes,
969 GFileQueryInfoFlags flags,
970 GCancellable *cancellable,
973 GLocalFile *local = G_LOCAL_FILE (file);
975 GFileAttributeMatcher *matcher;
976 char *basename, *dirname;
977 GLocalParentFileInfo parent_info;
979 matcher = g_file_attribute_matcher_new (attributes);
981 basename = g_path_get_basename (local->filename);
983 dirname = g_path_get_dirname (local->filename);
984 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
987 info = _g_local_file_info_get (basename, local->filename,
988 matcher, flags, &parent_info,
993 g_file_attribute_matcher_unref (matcher);
998 static GFileAttributeInfoList *
999 g_local_file_query_settable_attributes (GFile *file,
1000 GCancellable *cancellable,
1003 return g_file_attribute_info_list_ref (local_writable_attributes);
1006 static GFileAttributeInfoList *
1007 g_local_file_query_writable_namespaces (GFile *file,
1008 GCancellable *cancellable,
1011 return g_file_attribute_info_list_ref (local_writable_namespaces);
1015 g_local_file_set_attribute (GFile *file,
1016 const char *attribute,
1017 const GFileAttributeValue *value,
1018 GFileQueryInfoFlags flags,
1019 GCancellable *cancellable,
1022 GLocalFile *local = G_LOCAL_FILE (file);
1024 return _g_local_file_info_set_attribute (local->filename,
1033 g_local_file_set_attributes_from_info (GFile *file,
1035 GFileQueryInfoFlags flags,
1036 GCancellable *cancellable,
1039 GLocalFile *local = G_LOCAL_FILE (file);
1040 int res, chained_res;
1041 GFileIface* default_iface;
1043 res = _g_local_file_info_set_attributes (local->filename,
1049 error = NULL; /* Don't write over error if further errors */
1051 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1053 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1055 return res && chained_res;
1058 static GFileInputStream *
1059 g_local_file_read (GFile *file,
1060 GCancellable *cancellable,
1063 GLocalFile *local = G_LOCAL_FILE (file);
1067 fd = g_open (local->filename, O_RDONLY, 0);
1070 g_set_error (error, G_IO_ERROR,
1071 g_io_error_from_errno (errno),
1072 _("Error opening file: %s"),
1073 g_strerror (errno));
1077 if (fstat(fd, &buf) == 0 && S_ISDIR (buf.st_mode))
1080 g_set_error (error, G_IO_ERROR,
1081 G_IO_ERROR_IS_DIRECTORY,
1082 _("Can't open directory"));
1086 return _g_local_file_input_stream_new (fd);
1089 static GFileOutputStream *
1090 g_local_file_append_to (GFile *file,
1091 GFileCreateFlags flags,
1092 GCancellable *cancellable,
1095 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1096 flags, cancellable, error);
1099 static GFileOutputStream *
1100 g_local_file_create (GFile *file,
1101 GFileCreateFlags flags,
1102 GCancellable *cancellable,
1105 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1106 flags, cancellable, error);
1109 static GFileOutputStream *
1110 g_local_file_replace (GFile *file,
1112 gboolean make_backup,
1113 GFileCreateFlags flags,
1114 GCancellable *cancellable,
1117 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1118 etag, make_backup, flags,
1119 cancellable, error);
1124 g_local_file_delete (GFile *file,
1125 GCancellable *cancellable,
1128 GLocalFile *local = G_LOCAL_FILE (file);
1130 if (g_remove (local->filename) == -1)
1132 g_set_error (error, G_IO_ERROR,
1133 g_io_error_from_errno (errno),
1134 _("Error removing file: %s"),
1135 g_strerror (errno));
1143 strip_trailing_slashes (const char *path)
1148 path_copy = g_strdup (path);
1149 len = strlen (path_copy);
1150 while (len > 1 && path_copy[len-1] == '/')
1151 path_copy[--len] = 0;
1157 expand_symlink (const char *link)
1159 char *resolved, *canonical, *parent, *link2;
1160 char symlink_value[4096];
1168 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1171 return g_strdup (link);
1172 symlink_value[res] = 0;
1175 if (g_path_is_absolute (symlink_value))
1176 return canonicalize_filename (symlink_value);
1179 link2 = strip_trailing_slashes (link);
1180 parent = g_path_get_dirname (link2);
1183 resolved = g_build_filename (parent, symlink_value, NULL);
1186 canonical = canonicalize_filename (resolved);
1195 get_parent (const char *path,
1199 struct stat parent_stat;
1203 path_copy = strip_trailing_slashes (path);
1205 parent = g_path_get_dirname (path_copy);
1206 if (strcmp (parent, ".") == 0 ||
1207 strcmp (parent, path_copy) == 0)
1216 if (g_lstat (parent, &parent_stat) != 0)
1222 if (S_ISLNK (parent_stat.st_mode))
1225 parent = expand_symlink (parent);
1230 if (num_recursions > 12)
1235 } while (S_ISLNK (parent_stat.st_mode));
1237 *parent_dev = parent_stat.st_dev;
1243 expand_all_symlinks (const char *path)
1245 char *parent, *parent_expanded;
1246 char *basename, *res;
1249 parent = get_parent (path, &parent_dev);
1252 parent_expanded = expand_all_symlinks (parent);
1254 basename = g_path_get_basename (path);
1255 res = g_build_filename (parent_expanded, basename, NULL);
1257 g_free (parent_expanded);
1260 res = g_strdup (path);
1266 find_mountpoint_for (const char *file,
1270 dev_t dir_dev, parent_dev;
1272 dir = g_strdup (file);
1277 parent = get_parent (dir, &parent_dev);
1281 if (parent_dev != dir_dev)
1293 find_topdir_for (const char *file)
1298 dir = get_parent (file, &dir_dev);
1302 return find_mountpoint_for (dir, dir_dev);
1306 get_unique_filename (const char *basename,
1312 return g_strdup (basename);
1314 dot = strchr (basename, '.');
1316 return g_strdup_printf ("%.*s.%d%s", dot - basename, basename, id, dot);
1318 return g_strdup_printf ("%s.%d", basename, id);
1322 path_has_prefix (const char *path,
1330 prefix_len = strlen (prefix);
1332 if (strncmp (path, prefix, prefix_len) == 0 &&
1333 (prefix_len == 0 || /* empty prefix always matches */
1334 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1335 path[prefix_len] == 0 ||
1336 path[prefix_len] == '/'))
1343 try_make_relative (const char *path,
1346 char *path2, *base2;
1349 path2 = expand_all_symlinks (path);
1350 base2 = expand_all_symlinks (base);
1353 if (path_has_prefix (path2, base2))
1355 relative = path2 + strlen (base2);
1356 while (*relative == '/')
1358 relative = g_strdup (relative);
1366 /* Failed, use abs path */
1367 return g_strdup (path);
1371 escape_trash_name (char *name)
1374 const gchar hex[16] = "0123456789ABCDEF";
1376 str = g_string_new ("");
1384 if (g_ascii_isprint (c))
1385 g_string_append_c (str, c);
1388 g_string_append_c (str, '%');
1389 g_string_append_c (str, hex[((guchar)c) >> 4]);
1390 g_string_append_c (str, hex[((guchar)c) & 0xf]);
1394 return g_string_free (str, FALSE);
1398 g_local_file_trash (GFile *file,
1399 GCancellable *cancellable,
1402 GLocalFile *local = G_LOCAL_FILE (file);
1403 struct stat file_stat, home_stat, trash_stat, global_stat;
1404 const char *homedir;
1406 char *trashdir, *globaldir, *topdir, *infodir, *filesdir;
1407 char *basename, *trashname, *trashfile, *infoname, *infofile;
1408 char *original_name, *original_name_escaped;
1411 gboolean is_homedir_trash;
1412 char delete_time[32];
1415 if (g_lstat (local->filename, &file_stat) != 0)
1417 g_set_error (error, G_IO_ERROR,
1418 g_io_error_from_errno (errno),
1419 _("Error trashing file: %s"),
1420 g_strerror (errno));
1424 homedir = g_get_home_dir ();
1425 g_stat (homedir, &home_stat);
1427 is_homedir_trash = FALSE;
1429 if (file_stat.st_dev == home_stat.st_dev)
1431 is_homedir_trash = TRUE;
1433 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1434 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1440 display_name = g_filename_display_name (trashdir);
1441 g_set_error (error, G_IO_ERROR,
1442 g_io_error_from_errno (err),
1443 _("Unable to create trash dir %s: %s"),
1444 display_name, g_strerror (err));
1445 g_free (display_name);
1449 topdir = g_strdup (g_get_user_data_dir ());
1454 g_warning ("Recycle bin not implemented");
1460 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1462 topdir = find_topdir_for (local->filename);
1465 g_set_error (error, G_IO_ERROR,
1466 G_IO_ERROR_NOT_SUPPORTED,
1467 _("Unable to find toplevel directory for trash"));
1471 /* Try looking for global trash dir $topdir/.Trash/$uid */
1472 globaldir = g_build_filename (topdir, ".Trash", NULL);
1473 if (g_lstat (globaldir, &global_stat) == 0 &&
1474 S_ISDIR (global_stat.st_mode) &&
1475 (global_stat.st_mode & S_ISVTX) != 0)
1477 trashdir = g_build_filename (globaldir, uid_str, NULL);
1479 if (g_lstat (trashdir, &trash_stat) == 0)
1481 if (!S_ISDIR (trash_stat.st_mode) ||
1482 trash_stat.st_uid != uid)
1484 /* Not a directory or not owned by user, ignore */
1489 else if (g_mkdir (trashdir, 0700) == -1)
1497 if (trashdir == NULL)
1499 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1500 dirname = g_strdup_printf (".Trash-%s", uid_str);
1501 trashdir = g_build_filename (topdir, dirname, NULL);
1504 if (g_lstat (trashdir, &trash_stat) == 0)
1506 if (!S_ISDIR (trash_stat.st_mode) ||
1507 trash_stat.st_uid != uid)
1509 /* Not a directory or not owned by user, ignore */
1514 else if (g_mkdir (trashdir, 0700) == -1)
1522 if (trashdir == NULL)
1525 g_set_error (error, G_IO_ERROR,
1526 G_IO_ERROR_NOT_SUPPORTED,
1527 _("Unable to find or create trash directory"));
1532 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1534 infodir = g_build_filename (trashdir, "info", NULL);
1535 filesdir = g_build_filename (trashdir, "files", NULL);
1538 /* Make sure we have the subdirectories */
1539 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1540 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
1545 g_set_error (error, G_IO_ERROR,
1546 G_IO_ERROR_NOT_SUPPORTED,
1547 _("Unable to find or create trash directory"));
1551 basename = g_path_get_basename (local->filename);
1559 trashname = get_unique_filename (basename, i++);
1560 infoname = g_strconcat (trashname, ".trashinfo", NULL);
1561 infofile = g_build_filename (infodir, infoname, NULL);
1564 fd = open (infofile, O_CREAT | O_EXCL, 0666);
1565 } while (fd == -1 && errno == EEXIST);
1577 g_set_error (error, G_IO_ERROR,
1578 g_io_error_from_errno (errno),
1579 _("Unable to create trashed file: %s"),
1580 g_strerror (errno));
1586 /* TODO: Maybe we should verify that you can delete the file from the trash
1587 before moving it? OTOH, that is hard, as it needs a recursive scan */
1589 trashfile = g_build_filename (filesdir, trashname, NULL);
1593 if (g_rename (local->filename, trashfile) == -1)
1600 g_set_error (error, G_IO_ERROR,
1601 g_io_error_from_errno (errno),
1602 _("Unable to trash file: %s"),
1603 g_strerror (errno));
1609 /* TODO: Do we need to update mtime/atime here after the move? */
1611 /* Use absolute names for homedir */
1612 if (is_homedir_trash)
1613 original_name = g_strdup (local->filename);
1615 original_name = try_make_relative (local->filename, topdir);
1616 original_name_escaped = escape_trash_name (original_name);
1618 g_free (original_name);
1624 g_get_current_time (&now);
1625 strncpy (delete_time, g_time_val_to_iso8601 (&now), sizeof (delete_time));
1632 localtime_r (&t, &now);
1634 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
1638 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
1639 original_name_escaped, delete_time);
1641 g_file_set_contents (infofile, data, -1, NULL);
1645 g_free (original_name_escaped);
1652 g_local_file_make_directory (GFile *file,
1653 GCancellable *cancellable,
1656 GLocalFile *local = G_LOCAL_FILE (file);
1658 if (g_mkdir (local->filename, 0755) == -1)
1662 if (errsv == EINVAL)
1663 /* This must be an invalid filename, on e.g. FAT */
1664 g_set_error (error, G_IO_ERROR,
1665 G_IO_ERROR_INVALID_FILENAME,
1666 _("Invalid filename"));
1668 g_set_error (error, G_IO_ERROR,
1669 g_io_error_from_errno (errsv),
1670 _("Error removing file: %s"),
1671 g_strerror (errsv));
1679 g_local_file_make_symbolic_link (GFile *file,
1680 const char *symlink_value,
1681 GCancellable *cancellable,
1685 GLocalFile *local = G_LOCAL_FILE (file);
1687 if (symlink (symlink_value, local->filename) == -1)
1691 if (errsv == EINVAL)
1692 /* This must be an invalid filename, on e.g. FAT */
1693 g_set_error (error, G_IO_ERROR,
1694 G_IO_ERROR_INVALID_FILENAME,
1695 _("Invalid filename"));
1697 g_set_error (error, G_IO_ERROR,
1698 g_io_error_from_errno (errsv),
1699 _("Error making symbolic link: %s"),
1700 g_strerror (errsv));
1705 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
1712 g_local_file_copy (GFile *source,
1714 GFileCopyFlags flags,
1715 GCancellable *cancellable,
1716 GFileProgressCallback progress_callback,
1717 gpointer progress_callback_data,
1720 /* Fall back to default copy */
1721 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
1726 g_local_file_move (GFile *source,
1728 GFileCopyFlags flags,
1729 GCancellable *cancellable,
1730 GFileProgressCallback progress_callback,
1731 gpointer progress_callback_data,
1734 GLocalFile *local_source = G_LOCAL_FILE (source);
1735 GLocalFile *local_destination = G_LOCAL_FILE (destination);
1736 struct stat statbuf;
1737 gboolean destination_exist, source_is_dir;
1741 res = g_lstat (local_source->filename, &statbuf);
1744 g_set_error (error, G_IO_ERROR,
1745 g_io_error_from_errno (errno),
1746 _("Error moving file: %s"),
1747 g_strerror (errno));
1751 source_is_dir = S_ISDIR (statbuf.st_mode);
1753 destination_exist = FALSE;
1754 res = g_lstat (local_destination->filename, &statbuf);
1757 destination_exist = TRUE; /* Target file exists */
1759 if (flags & G_FILE_COPY_OVERWRITE)
1761 /* Always fail on dirs, even with overwrite */
1762 if (S_ISDIR (statbuf.st_mode))
1766 G_IO_ERROR_WOULD_MERGE,
1767 _("Can't move directory over directory"));
1776 _("Target file already exists"));
1781 if (flags & G_FILE_COPY_BACKUP && destination_exist)
1783 backup_name = g_strconcat (local_destination->filename, "~", NULL);
1784 if (rename (local_destination->filename, backup_name) == -1)
1788 G_IO_ERROR_CANT_CREATE_BACKUP,
1789 _("Backup file creation failed"));
1790 g_free (backup_name);
1793 g_free (backup_name);
1794 destination_exist = FALSE; /* It did, but no more */
1797 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
1799 /* Source is a dir, destination exists (and is not a dir, because that would have failed
1800 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
1801 res = unlink (local_destination->filename);
1804 g_set_error (error, G_IO_ERROR,
1805 g_io_error_from_errno (errno),
1806 _("Error removing target file: %s"),
1807 g_strerror (errno));
1812 if (rename (local_source->filename, local_destination->filename) == -1)
1817 /* This will cause the fallback code to run */
1818 g_set_error (error, G_IO_ERROR,
1819 G_IO_ERROR_NOT_SUPPORTED,
1820 _("Move between mounts not supported"));
1821 else if (errsv == EINVAL)
1822 /* This must be an invalid filename, on e.g. FAT, or
1823 we're trying to move the file into itself...
1824 We return invalid filename for both... */
1825 g_set_error (error, G_IO_ERROR,
1826 G_IO_ERROR_INVALID_FILENAME,
1827 _("Invalid filename"));
1829 g_set_error (error, G_IO_ERROR,
1830 g_io_error_from_errno (errsv),
1831 _("Error moving file: %s"),
1832 g_strerror (errsv));
1839 static GDirectoryMonitor*
1840 g_local_file_monitor_dir (GFile *file,
1841 GFileMonitorFlags flags,
1842 GCancellable *cancellable)
1844 GLocalFile* local_file = G_LOCAL_FILE(file);
1845 return _g_local_directory_monitor_new (local_file->filename, flags);
1848 static GFileMonitor*
1849 g_local_file_monitor_file (GFile *file,
1850 GFileMonitorFlags flags,
1851 GCancellable *cancellable)
1853 GLocalFile* local_file = G_LOCAL_FILE(file);
1854 return _g_local_file_monitor_new (local_file->filename, flags);
1858 g_local_file_file_iface_init (GFileIface *iface)
1860 iface->dup = g_local_file_dup;
1861 iface->hash = g_local_file_hash;
1862 iface->equal = g_local_file_equal;
1863 iface->is_native = g_local_file_is_native;
1864 iface->has_uri_scheme = g_local_file_has_uri_scheme;
1865 iface->get_uri_scheme = g_local_file_get_uri_scheme;
1866 iface->get_basename = g_local_file_get_basename;
1867 iface->get_path = g_local_file_get_path;
1868 iface->get_uri = g_local_file_get_uri;
1869 iface->get_parse_name = g_local_file_get_parse_name;
1870 iface->get_parent = g_local_file_get_parent;
1871 iface->contains_file = g_local_file_contains_file;
1872 iface->get_relative_path = g_local_file_get_relative_path;
1873 iface->resolve_relative_path = g_local_file_resolve_relative_path;
1874 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
1875 iface->set_display_name = g_local_file_set_display_name;
1876 iface->enumerate_children = g_local_file_enumerate_children;
1877 iface->query_info = g_local_file_query_info;
1878 iface->query_filesystem_info = g_local_file_query_filesystem_info;
1879 iface->find_enclosing_volume = g_local_file_find_enclosing_volume;
1880 iface->query_settable_attributes = g_local_file_query_settable_attributes;
1881 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
1882 iface->set_attribute = g_local_file_set_attribute;
1883 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
1884 iface->read_fn = g_local_file_read;
1885 iface->append_to = g_local_file_append_to;
1886 iface->create = g_local_file_create;
1887 iface->replace = g_local_file_replace;
1888 iface->delete_file = g_local_file_delete;
1889 iface->trash = g_local_file_trash;
1890 iface->make_directory = g_local_file_make_directory;
1891 iface->make_symbolic_link = g_local_file_make_symbolic_link;
1892 iface->copy = g_local_file_copy;
1893 iface->move = g_local_file_move;
1894 iface->monitor_dir = g_local_file_monitor_dir;
1895 iface->monitor_file = g_local_file_monitor_file;