1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
35 #include <sys/statfs.h>
37 #if HAVE_SYS_STATVFS_H
38 #include <sys/statvfs.h>
42 #elif HAVE_SYS_MOUNT_H
44 #include <sys/param.h>
46 #include <sys/mount.h>
53 #if defined(HAVE_STATFS) && defined(HAVE_STATVFS)
54 /* Some systems have both statfs and statvfs, pick the
55 most "native" for these */
56 # if defined(sun) && defined(__SVR4)
57 /* on solaris, statfs doesn't even have the
61 /* at least on linux, statfs is the actual syscall */
65 #elif defined(HAVE_STATFS)
69 #elif defined(HAVE_STATVFS)
75 #include "glocalfile.h"
76 #include "glocalfileinfo.h"
77 #include "glocalfileenumerator.h"
78 #include "glocalfileinputstream.h"
79 #include "glocalfileoutputstream.h"
80 #include "glocaldirectorymonitor.h"
81 #include "glocalfilemonitor.h"
82 #include "gmountprivate.h"
83 #include <glib/gstdio.h>
92 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
95 #define S_ISLNK(m) (0)
101 static void g_local_file_file_iface_init (GFileIface *iface);
103 static GFileAttributeInfoList *local_writable_attributes = NULL;
104 static GFileAttributeInfoList *local_writable_namespaces = NULL;
108 GObject parent_instance;
113 #define g_local_file_get_type _g_local_file_get_type
114 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
115 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
116 g_local_file_file_iface_init))
118 static char *find_mountpoint_for (const char *file, dev_t dev);
121 g_local_file_finalize (GObject *object)
125 local = G_LOCAL_FILE (object);
127 g_free (local->filename);
129 if (G_OBJECT_CLASS (g_local_file_parent_class)->finalize)
130 (*G_OBJECT_CLASS (g_local_file_parent_class)->finalize) (object);
134 g_local_file_class_init (GLocalFileClass *klass)
136 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
137 GFileAttributeInfoList *list;
139 gobject_class->finalize = g_local_file_finalize;
141 /* Set up attribute lists */
143 /* Writable attributes: */
145 list = g_file_attribute_info_list_new ();
147 g_file_attribute_info_list_add (list,
148 G_FILE_ATTRIBUTE_UNIX_MODE,
149 G_FILE_ATTRIBUTE_TYPE_UINT32,
150 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
151 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
154 g_file_attribute_info_list_add (list,
155 G_FILE_ATTRIBUTE_UNIX_UID,
156 G_FILE_ATTRIBUTE_TYPE_UINT32,
157 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
158 g_file_attribute_info_list_add (list,
159 G_FILE_ATTRIBUTE_UNIX_GID,
160 G_FILE_ATTRIBUTE_TYPE_UINT32,
161 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
165 g_file_attribute_info_list_add (list,
166 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
167 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
172 g_file_attribute_info_list_add (list,
173 G_FILE_ATTRIBUTE_TIME_MODIFIED,
174 G_FILE_ATTRIBUTE_TYPE_UINT64,
175 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
176 g_file_attribute_info_list_add (list,
177 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
178 G_FILE_ATTRIBUTE_TYPE_UINT32,
179 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
180 g_file_attribute_info_list_add (list,
181 G_FILE_ATTRIBUTE_TIME_ACCESS,
182 G_FILE_ATTRIBUTE_TYPE_UINT64,
183 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
184 g_file_attribute_info_list_add (list,
185 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
186 G_FILE_ATTRIBUTE_TYPE_UINT32,
187 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
190 local_writable_attributes = list;
192 /* Writable namespaces: */
194 list = g_file_attribute_info_list_new ();
197 g_file_attribute_info_list_add (list,
199 G_FILE_ATTRIBUTE_TYPE_STRING,
200 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
201 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
202 g_file_attribute_info_list_add (list,
204 G_FILE_ATTRIBUTE_TYPE_STRING,
205 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
208 local_writable_namespaces = list;
212 g_local_file_init (GLocalFile *local)
218 canonicalize_filename (const char *filename)
220 char *canon, *start, *p, *q;
224 if (!g_path_is_absolute (filename))
226 cwd = g_get_current_dir ();
227 canon = g_build_filename (cwd, filename, NULL);
231 canon = g_strdup (filename);
233 start = (char *)g_path_skip_root (canon);
235 /* POSIX allows double slashes at the start to
236 * mean something special (as does windows too).
237 * So, "//" != "/", but more than two slashes
243 G_IS_DIR_SEPARATOR (*p);
250 memmove (start, start+i, strlen (start+i)+1);
256 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
258 memmove (p, p+1, strlen (p+1)+1);
260 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
263 /* Skip previous separator */
267 while (p > start && !G_IS_DIR_SEPARATOR (*p))
269 if (G_IS_DIR_SEPARATOR (*p))
270 *p++ = G_DIR_SEPARATOR;
271 memmove (p, q, strlen (q)+1);
275 /* Skip until next separator */
276 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
281 /* Canonicalize one separator */
282 *p++ = G_DIR_SEPARATOR;
286 /* Remove additional separators */
288 while (*q && G_IS_DIR_SEPARATOR (*q))
292 memmove (p, q, strlen (q)+1);
295 /* Remove trailing slashes */
296 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
304 * @filename: filename of the file to create.
306 * Returns: new local #GFile.
309 _g_local_file_new (const char *filename)
313 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
314 local->filename = canonicalize_filename (filename);
316 return G_FILE (local);
320 g_local_file_is_native (GFile *file)
326 g_local_file_has_uri_scheme (GFile *file,
327 const char *uri_scheme)
329 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
333 g_local_file_get_uri_scheme (GFile *file)
335 return g_strdup ("file");
339 g_local_file_get_basename (GFile *file)
341 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
345 g_local_file_get_path (GFile *file)
347 return g_strdup (G_LOCAL_FILE (file)->filename);
351 g_local_file_get_uri (GFile *file)
353 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
357 get_filename_charset (const gchar **filename_charset)
359 const gchar **charsets;
362 is_utf8 = g_get_filename_charsets (&charsets);
364 if (filename_charset)
365 *filename_charset = charsets[0];
371 name_is_valid_for_display (const char *string,
372 gboolean is_valid_utf8)
376 if (!is_valid_utf8 &&
377 !g_utf8_validate (string, -1, NULL))
380 while ((c = *string++) != 0)
382 if (g_ascii_iscntrl (c))
390 g_local_file_get_parse_name (GFile *file)
392 const char *filename;
394 const gchar *charset;
396 char *roundtripped_filename;
397 gboolean free_utf8_filename;
398 gboolean is_valid_utf8;
401 filename = G_LOCAL_FILE (file)->filename;
402 if (get_filename_charset (&charset))
404 utf8_filename = (char *)filename;
405 free_utf8_filename = FALSE;
406 is_valid_utf8 = FALSE; /* Can't guarantee this */
410 utf8_filename = g_convert (filename, -1,
411 "UTF-8", charset, NULL, NULL, NULL);
412 free_utf8_filename = TRUE;
413 is_valid_utf8 = TRUE;
415 if (utf8_filename != NULL)
417 /* Make sure we can roundtrip: */
418 roundtripped_filename = g_convert (utf8_filename, -1,
419 charset, "UTF-8", NULL, NULL, NULL);
421 if (roundtripped_filename == NULL ||
422 strcmp (utf8_filename, roundtripped_filename) != 0)
424 g_free (utf8_filename);
425 utf8_filename = NULL;
430 if (utf8_filename != NULL &&
431 name_is_valid_for_display (utf8_filename, is_valid_utf8))
433 if (free_utf8_filename)
434 parse_name = utf8_filename;
436 parse_name = g_strdup (utf8_filename);
440 escaped_path = g_uri_escape_string (filename,
441 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
443 parse_name = g_strconcat ("file://",
444 (*escaped_path != '/') ? "/" : "",
448 g_free (escaped_path);
450 if (free_utf8_filename)
451 g_free (utf8_filename);
458 g_local_file_get_parent (GFile *file)
460 GLocalFile *local = G_LOCAL_FILE (file);
461 const char *non_root;
466 non_root = g_path_skip_root (local->filename);
470 dirname = g_path_get_dirname (local->filename);
471 parent = _g_local_file_new (dirname);
477 g_local_file_dup (GFile *file)
479 GLocalFile *local = G_LOCAL_FILE (file);
481 return _g_local_file_new (local->filename);
485 g_local_file_hash (GFile *file)
487 GLocalFile *local = G_LOCAL_FILE (file);
489 return g_str_hash (local->filename);
493 g_local_file_equal (GFile *file1,
496 GLocalFile *local1 = G_LOCAL_FILE (file1);
497 GLocalFile *local2 = G_LOCAL_FILE (file2);
499 return g_str_equal (local1->filename, local2->filename);
503 match_prefix (const char *path,
508 prefix_len = strlen (prefix);
509 if (strncmp (path, prefix, prefix_len) != 0)
512 /* Handle the case where prefix is the root, so that
513 * the IS_DIR_SEPRARATOR check below works */
514 if (prefix_len > 0 &&
515 G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
518 return path + prefix_len;
522 g_local_file_contains_file (GFile *parent,
525 GLocalFile *parent_local = G_LOCAL_FILE (parent);
526 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
527 const char *remainder;
529 remainder = match_prefix (descendant_local->filename, parent_local->filename);
530 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
536 g_local_file_get_relative_path (GFile *parent,
539 GLocalFile *parent_local = G_LOCAL_FILE (parent);
540 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
541 const char *remainder;
543 remainder = match_prefix (descendant_local->filename, parent_local->filename);
545 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
546 return g_strdup (remainder + 1);
551 g_local_file_resolve_relative_path (GFile *file,
552 const char *relative_path)
554 GLocalFile *local = G_LOCAL_FILE (file);
558 if (g_path_is_absolute (relative_path))
559 return _g_local_file_new (relative_path);
561 filename = g_build_filename (local->filename, relative_path, NULL);
562 child = _g_local_file_new (filename);
568 static GFileEnumerator *
569 g_local_file_enumerate_children (GFile *file,
570 const char *attributes,
571 GFileQueryInfoFlags flags,
572 GCancellable *cancellable,
575 GLocalFile *local = G_LOCAL_FILE (file);
576 return _g_local_file_enumerator_new (local->filename,
582 g_local_file_get_child_for_display_name (GFile *file,
583 const char *display_name,
586 GFile *parent, *new_file;
589 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
590 if (basename == NULL)
592 g_set_error (error, G_IO_ERROR,
593 G_IO_ERROR_INVALID_FILENAME,
594 _("Invalid filename %s"), display_name);
598 parent = g_file_get_parent (file);
599 new_file = g_file_get_child (file, basename);
600 g_object_unref (parent);
608 get_fs_type (long f_type)
611 /* filesystem ids taken from linux manpage */
708 G_LOCK_DEFINE_STATIC(mount_info_hash);
709 static GHashTable *mount_info_hash = NULL;
710 static guint64 mount_info_hash_cache_time = 0;
713 MOUNT_INFO_READONLY = 1<<0
717 device_equal (gconstpointer v1,
720 return *(dev_t *)v1 == *(dev_t *)v2;
724 device_hash (gconstpointer v)
726 return (guint) *(dev_t *)v;
730 get_mount_info (GFileInfo *fs_info,
732 GFileAttributeMatcher *matcher)
736 gpointer info_as_ptr;
740 GUnixMountEntry *mount;
743 if (g_lstat (path, &buf) != 0)
746 G_LOCK (mount_info_hash);
748 if (mount_info_hash == NULL)
749 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
753 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
754 g_hash_table_remove_all (mount_info_hash);
756 got_info = g_hash_table_lookup_extended (mount_info_hash,
761 G_UNLOCK (mount_info_hash);
763 mount_info = GPOINTER_TO_UINT (info_as_ptr);
769 mountpoint = find_mountpoint_for (path, buf.st_dev);
770 if (mountpoint == NULL)
773 mount = g_unix_mount_at (mountpoint, &cache_time);
776 if (g_unix_mount_is_readonly (mount))
777 mount_info |= MOUNT_INFO_READONLY;
779 g_unix_mount_free (mount);
782 dev = g_new0 (dev_t, 1);
785 G_LOCK (mount_info_hash);
786 mount_info_hash_cache_time = cache_time;
787 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
788 G_UNLOCK (mount_info_hash);
791 if (mount_info & MOUNT_INFO_READONLY)
792 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
798 g_local_file_query_filesystem_info (GFile *file,
799 const char *attributes,
800 GCancellable *cancellable,
803 GLocalFile *local = G_LOCAL_FILE (file);
810 struct statfs statfs_buffer;
812 #elif defined(USE_STATVFS)
813 struct statvfs statfs_buffer;
816 GFileAttributeMatcher *attribute_matcher;
823 statfs_result = statfs (local->filename, &statfs_buffer);
824 #elif STATFS_ARGS == 4
825 statfs_result = statfs (local->filename, &statfs_buffer,
826 sizeof (statfs_buffer), 0);
828 block_size = statfs_buffer.f_bsize;
830 #if defined(__linux__)
831 /* ncpfs does not know the amount of available and free space *
832 * assuming ncpfs is linux specific, if you are on a non-linux platform
833 * where ncpfs is available, please file a bug about it on bugzilla.gnome.org
835 if (statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0 &&
836 /* linux/ncp_fs.h: NCP_SUPER_MAGIC == 0x564c */
837 statfs_buffer.f_type == 0x564c)
841 #elif defined(USE_STATVFS)
842 statfs_result = statvfs (local->filename, &statfs_buffer);
843 block_size = statfs_buffer.f_frsize;
846 if (statfs_result == -1)
850 g_set_error (error, G_IO_ERROR,
851 g_io_error_from_errno (errsv),
852 _("Error getting filesystem info: %s"),
857 info = g_file_info_new ();
859 attribute_matcher = g_file_attribute_matcher_new (attributes);
861 if (g_file_attribute_matcher_matches (attribute_matcher,
862 G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
865 gchar *localdir = g_path_get_dirname (local->filename);
866 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
870 if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
871 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
874 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
877 if (g_file_attribute_matcher_matches (attribute_matcher,
878 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
881 gchar *localdir = g_path_get_dirname (local->filename);
882 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
886 if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
887 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, (guint64)li.QuadPart);
890 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
894 fstype = get_fs_type (statfs_buffer.f_type);
896 g_file_attribute_matcher_matches (attribute_matcher,
897 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
898 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
901 if (g_file_attribute_matcher_matches (attribute_matcher,
902 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
905 /* need to implement with *unix_mount* */
907 get_mount_info (info, local->filename, attribute_matcher);
911 g_file_attribute_matcher_unref (attribute_matcher);
917 g_local_file_find_enclosing_mount (GFile *file,
918 GCancellable *cancellable,
921 GLocalFile *local = G_LOCAL_FILE (file);
926 if (g_lstat (local->filename, &buf) != 0)
928 g_set_error (error, G_IO_ERROR,
929 G_IO_ERROR_NOT_FOUND,
930 _("Containing mount does not exist"));
934 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
935 if (mountpoint == NULL)
937 g_set_error (error, G_IO_ERROR,
938 G_IO_ERROR_NOT_FOUND,
939 _("Containing mount does not exist"));
943 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
948 g_set_error (error, G_IO_ERROR,
949 G_IO_ERROR_NOT_FOUND,
950 _("Containing mount does not exist"));
955 g_local_file_set_display_name (GFile *file,
956 const char *display_name,
957 GCancellable *cancellable,
960 GLocalFile *local, *new_local;
961 GFile *new_file, *parent;
965 parent = g_file_get_parent (file);
968 g_set_error (error, G_IO_ERROR,
970 _("Can't rename root directory"));
974 new_file = g_file_get_child_for_display_name (parent, display_name, error);
975 g_object_unref (parent);
977 if (new_file == NULL)
980 local = G_LOCAL_FILE (file);
981 new_local = G_LOCAL_FILE (new_file);
983 if (!(g_lstat (new_local->filename, &statbuf) == -1 &&
986 g_set_error (error, G_IO_ERROR,
988 _("Can't rename file, filename already exist"));
992 if (rename (local->filename, new_local->filename) == -1)
997 /* We can't get a rename file into itself error herer,
998 so this must be an invalid filename, on e.g. FAT */
999 g_set_error (error, G_IO_ERROR,
1000 G_IO_ERROR_INVALID_FILENAME,
1001 _("Invalid filename"));
1003 g_set_error (error, G_IO_ERROR,
1004 g_io_error_from_errno (errsv),
1005 _("Error renaming file: %s"),
1006 g_strerror (errsv));
1007 g_object_unref (new_file);
1015 g_local_file_query_info (GFile *file,
1016 const char *attributes,
1017 GFileQueryInfoFlags flags,
1018 GCancellable *cancellable,
1021 GLocalFile *local = G_LOCAL_FILE (file);
1023 GFileAttributeMatcher *matcher;
1024 char *basename, *dirname;
1025 GLocalParentFileInfo parent_info;
1027 matcher = g_file_attribute_matcher_new (attributes);
1029 basename = g_path_get_basename (local->filename);
1031 dirname = g_path_get_dirname (local->filename);
1032 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1035 info = _g_local_file_info_get (basename, local->filename,
1036 matcher, flags, &parent_info,
1041 g_file_attribute_matcher_unref (matcher);
1046 static GFileAttributeInfoList *
1047 g_local_file_query_settable_attributes (GFile *file,
1048 GCancellable *cancellable,
1051 return g_file_attribute_info_list_ref (local_writable_attributes);
1054 static GFileAttributeInfoList *
1055 g_local_file_query_writable_namespaces (GFile *file,
1056 GCancellable *cancellable,
1059 return g_file_attribute_info_list_ref (local_writable_namespaces);
1063 g_local_file_set_attribute (GFile *file,
1064 const char *attribute,
1065 GFileAttributeType type,
1067 GFileQueryInfoFlags flags,
1068 GCancellable *cancellable,
1071 GLocalFile *local = G_LOCAL_FILE (file);
1073 return _g_local_file_info_set_attribute (local->filename,
1083 g_local_file_set_attributes_from_info (GFile *file,
1085 GFileQueryInfoFlags flags,
1086 GCancellable *cancellable,
1089 GLocalFile *local = G_LOCAL_FILE (file);
1090 int res, chained_res;
1091 GFileIface *default_iface;
1093 res = _g_local_file_info_set_attributes (local->filename,
1099 error = NULL; /* Don't write over error if further errors */
1101 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1103 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1105 return res && chained_res;
1108 static GFileInputStream *
1109 g_local_file_read (GFile *file,
1110 GCancellable *cancellable,
1113 GLocalFile *local = G_LOCAL_FILE (file);
1117 fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1122 g_set_error (error, G_IO_ERROR,
1123 g_io_error_from_errno (errsv),
1124 _("Error opening file: %s"),
1125 g_strerror (errsv));
1129 if (fstat(fd, &buf) == 0 && S_ISDIR (buf.st_mode))
1132 g_set_error (error, G_IO_ERROR,
1133 G_IO_ERROR_IS_DIRECTORY,
1134 _("Can't open directory"));
1138 return _g_local_file_input_stream_new (fd);
1141 static GFileOutputStream *
1142 g_local_file_append_to (GFile *file,
1143 GFileCreateFlags flags,
1144 GCancellable *cancellable,
1147 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1148 flags, cancellable, error);
1151 static GFileOutputStream *
1152 g_local_file_create (GFile *file,
1153 GFileCreateFlags flags,
1154 GCancellable *cancellable,
1157 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1158 flags, cancellable, error);
1161 static GFileOutputStream *
1162 g_local_file_replace (GFile *file,
1164 gboolean make_backup,
1165 GFileCreateFlags flags,
1166 GCancellable *cancellable,
1169 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1170 etag, make_backup, flags,
1171 cancellable, error);
1176 g_local_file_delete (GFile *file,
1177 GCancellable *cancellable,
1180 GLocalFile *local = G_LOCAL_FILE (file);
1182 if (g_remove (local->filename) == -1)
1186 g_set_error (error, G_IO_ERROR,
1187 g_io_error_from_errno (errsv),
1188 _("Error removing file: %s"),
1189 g_strerror (errsv));
1197 strip_trailing_slashes (const char *path)
1202 path_copy = g_strdup (path);
1203 len = strlen (path_copy);
1204 while (len > 1 && path_copy[len-1] == '/')
1205 path_copy[--len] = 0;
1211 expand_symlink (const char *link)
1213 char *resolved, *canonical, *parent, *link2;
1214 char symlink_value[4096];
1222 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1225 return g_strdup (link);
1226 symlink_value[res] = 0;
1229 if (g_path_is_absolute (symlink_value))
1230 return canonicalize_filename (symlink_value);
1233 link2 = strip_trailing_slashes (link);
1234 parent = g_path_get_dirname (link2);
1237 resolved = g_build_filename (parent, symlink_value, NULL);
1240 canonical = canonicalize_filename (resolved);
1249 get_parent (const char *path,
1253 struct stat parent_stat;
1257 path_copy = strip_trailing_slashes (path);
1259 parent = g_path_get_dirname (path_copy);
1260 if (strcmp (parent, ".") == 0 ||
1261 strcmp (parent, path_copy) == 0)
1270 if (g_lstat (parent, &parent_stat) != 0)
1276 if (S_ISLNK (parent_stat.st_mode))
1279 parent = expand_symlink (parent);
1284 if (num_recursions > 12)
1289 } while (S_ISLNK (parent_stat.st_mode));
1291 *parent_dev = parent_stat.st_dev;
1297 expand_all_symlinks (const char *path)
1299 char *parent, *parent_expanded;
1300 char *basename, *res;
1303 parent = get_parent (path, &parent_dev);
1306 parent_expanded = expand_all_symlinks (parent);
1308 basename = g_path_get_basename (path);
1309 res = g_build_filename (parent_expanded, basename, NULL);
1311 g_free (parent_expanded);
1314 res = g_strdup (path);
1320 find_mountpoint_for (const char *file,
1324 dev_t dir_dev, parent_dev;
1326 dir = g_strdup (file);
1331 parent = get_parent (dir, &parent_dev);
1335 if (parent_dev != dir_dev)
1349 find_topdir_for (const char *file)
1354 dir = get_parent (file, &dir_dev);
1358 return find_mountpoint_for (dir, dir_dev);
1364 get_unique_filename (const char *basename,
1370 return g_strdup (basename);
1372 dot = strchr (basename, '.');
1374 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1376 return g_strdup_printf ("%s.%d", basename, id);
1380 path_has_prefix (const char *path,
1388 prefix_len = strlen (prefix);
1390 if (strncmp (path, prefix, prefix_len) == 0 &&
1391 (prefix_len == 0 || /* empty prefix always matches */
1392 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1393 path[prefix_len] == 0 ||
1394 path[prefix_len] == '/'))
1401 try_make_relative (const char *path,
1404 char *path2, *base2;
1407 path2 = expand_all_symlinks (path);
1408 base2 = expand_all_symlinks (base);
1411 if (path_has_prefix (path2, base2))
1413 relative = path2 + strlen (base2);
1414 while (*relative == '/')
1416 relative = g_strdup (relative);
1424 /* Failed, use abs path */
1425 return g_strdup (path);
1429 escape_trash_name (char *name)
1432 const gchar hex[16] = "0123456789ABCDEF";
1434 str = g_string_new ("");
1442 if (g_ascii_isprint (c))
1443 g_string_append_c (str, c);
1446 g_string_append_c (str, '%');
1447 g_string_append_c (str, hex[((guchar)c) >> 4]);
1448 g_string_append_c (str, hex[((guchar)c) & 0xf]);
1452 return g_string_free (str, FALSE);
1456 g_local_file_trash (GFile *file,
1457 GCancellable *cancellable,
1460 GLocalFile *local = G_LOCAL_FILE (file);
1461 struct stat file_stat, home_stat;
1462 const char *homedir;
1463 char *trashdir, *topdir, *infodir, *filesdir;
1464 char *basename, *trashname, *trashfile, *infoname, *infofile;
1465 char *original_name, *original_name_escaped;
1468 gboolean is_homedir_trash;
1469 char delete_time[32];
1472 struct stat trash_stat, global_stat;
1473 char *dirname, *globaldir;
1476 if (g_lstat (local->filename, &file_stat) != 0)
1480 g_set_error (error, G_IO_ERROR,
1481 g_io_error_from_errno (errsv),
1482 _("Error trashing file: %s"),
1483 g_strerror (errsv));
1487 homedir = g_get_home_dir ();
1488 g_stat (homedir, &home_stat);
1490 is_homedir_trash = FALSE;
1492 if (file_stat.st_dev == home_stat.st_dev)
1494 is_homedir_trash = TRUE;
1496 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1497 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1502 display_name = g_filename_display_name (trashdir);
1503 g_set_error (error, G_IO_ERROR,
1504 g_io_error_from_errno (errsv),
1505 _("Unable to create trash dir %s: %s"),
1506 display_name, g_strerror (errsv));
1507 g_free (display_name);
1511 topdir = g_strdup (g_get_user_data_dir ());
1516 g_warning ("Recycle bin not implemented");
1522 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1524 topdir = find_topdir_for (local->filename);
1527 g_set_error (error, G_IO_ERROR,
1528 G_IO_ERROR_NOT_SUPPORTED,
1529 _("Unable to find toplevel directory for trash"));
1533 /* Try looking for global trash dir $topdir/.Trash/$uid */
1534 globaldir = g_build_filename (topdir, ".Trash", NULL);
1535 if (g_lstat (globaldir, &global_stat) == 0 &&
1536 S_ISDIR (global_stat.st_mode) &&
1537 (global_stat.st_mode & S_ISVTX) != 0)
1539 trashdir = g_build_filename (globaldir, uid_str, NULL);
1541 if (g_lstat (trashdir, &trash_stat) == 0)
1543 if (!S_ISDIR (trash_stat.st_mode) ||
1544 trash_stat.st_uid != uid)
1546 /* Not a directory or not owned by user, ignore */
1551 else if (g_mkdir (trashdir, 0700) == -1)
1559 if (trashdir == NULL)
1561 gboolean tried_create;
1563 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1564 dirname = g_strdup_printf (".Trash-%s", uid_str);
1565 trashdir = g_build_filename (topdir, dirname, NULL);
1568 tried_create = FALSE;
1571 if (g_lstat (trashdir, &trash_stat) == 0)
1573 if (!S_ISDIR (trash_stat.st_mode) ||
1574 trash_stat.st_uid != uid)
1576 /* Remove the failed directory */
1578 g_remove (trashdir);
1580 /* Not a directory or not owned by user, ignore */
1587 if (!tried_create &&
1588 g_mkdir (trashdir, 0700) != -1)
1590 /* Ensure that the created dir has the right uid etc.
1591 This might fail on e.g. a FAT dir */
1592 tried_create = TRUE;
1604 if (trashdir == NULL)
1607 g_set_error (error, G_IO_ERROR,
1608 G_IO_ERROR_NOT_SUPPORTED,
1609 _("Unable to find or create trash directory"));
1614 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1616 infodir = g_build_filename (trashdir, "info", NULL);
1617 filesdir = g_build_filename (trashdir, "files", NULL);
1620 /* Make sure we have the subdirectories */
1621 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1622 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
1627 g_set_error (error, G_IO_ERROR,
1628 G_IO_ERROR_NOT_SUPPORTED,
1629 _("Unable to find or create trash directory"));
1633 basename = g_path_get_basename (local->filename);
1641 trashname = get_unique_filename (basename, i++);
1642 infoname = g_strconcat (trashname, ".trashinfo", NULL);
1643 infofile = g_build_filename (infodir, infoname, NULL);
1646 fd = open (infofile, O_CREAT | O_EXCL, 0666);
1647 } while (fd == -1 && errno == EEXIST);
1661 g_set_error (error, G_IO_ERROR,
1662 g_io_error_from_errno (errsv),
1663 _("Unable to create trashing info file: %s"),
1664 g_strerror (errsv));
1670 /* TODO: Maybe we should verify that you can delete the file from the trash
1671 before moving it? OTOH, that is hard, as it needs a recursive scan */
1673 trashfile = g_build_filename (filesdir, trashname, NULL);
1677 if (g_rename (local->filename, trashfile) == -1)
1686 g_set_error (error, G_IO_ERROR,
1687 g_io_error_from_errno (errsv),
1688 _("Unable to trash file: %s"),
1689 g_strerror (errsv));
1695 /* TODO: Do we need to update mtime/atime here after the move? */
1697 /* Use absolute names for homedir */
1698 if (is_homedir_trash)
1699 original_name = g_strdup (local->filename);
1701 original_name = try_make_relative (local->filename, topdir);
1702 original_name_escaped = escape_trash_name (original_name);
1704 g_free (original_name);
1710 g_get_current_time (&now);
1711 strncpy (delete_time, g_time_val_to_iso8601 (&now), sizeof (delete_time));
1718 localtime_r (&t, &now);
1720 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
1724 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
1725 original_name_escaped, delete_time);
1727 g_file_set_contents (infofile, data, -1, NULL);
1731 g_free (original_name_escaped);
1738 g_local_file_make_directory (GFile *file,
1739 GCancellable *cancellable,
1742 GLocalFile *local = G_LOCAL_FILE (file);
1744 if (g_mkdir (local->filename, 0755) == -1)
1748 if (errsv == EINVAL)
1749 /* This must be an invalid filename, on e.g. FAT */
1750 g_set_error (error, G_IO_ERROR,
1751 G_IO_ERROR_INVALID_FILENAME,
1752 _("Invalid filename"));
1754 g_set_error (error, G_IO_ERROR,
1755 g_io_error_from_errno (errsv),
1756 _("Error removing file: %s"),
1757 g_strerror (errsv));
1765 g_local_file_make_symbolic_link (GFile *file,
1766 const char *symlink_value,
1767 GCancellable *cancellable,
1771 GLocalFile *local = G_LOCAL_FILE (file);
1773 if (symlink (symlink_value, local->filename) == -1)
1777 if (errsv == EINVAL)
1778 /* This must be an invalid filename, on e.g. FAT */
1779 g_set_error (error, G_IO_ERROR,
1780 G_IO_ERROR_INVALID_FILENAME,
1781 _("Invalid filename"));
1783 g_set_error (error, G_IO_ERROR,
1784 g_io_error_from_errno (errsv),
1785 _("Error making symbolic link: %s"),
1786 g_strerror (errsv));
1791 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
1798 g_local_file_copy (GFile *source,
1800 GFileCopyFlags flags,
1801 GCancellable *cancellable,
1802 GFileProgressCallback progress_callback,
1803 gpointer progress_callback_data,
1806 /* Fall back to default copy */
1807 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
1812 g_local_file_move (GFile *source,
1814 GFileCopyFlags flags,
1815 GCancellable *cancellable,
1816 GFileProgressCallback progress_callback,
1817 gpointer progress_callback_data,
1820 GLocalFile *local_source, *local_destination;
1821 struct stat statbuf;
1822 gboolean destination_exist, source_is_dir;
1827 if (!G_IS_LOCAL_FILE (source) ||
1828 !G_IS_LOCAL_FILE (destination))
1830 /* Fall back to default move */
1831 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
1835 local_source = G_LOCAL_FILE (source);
1836 local_destination = G_LOCAL_FILE (destination);
1838 res = g_lstat (local_source->filename, &statbuf);
1843 g_set_error (error, G_IO_ERROR,
1844 g_io_error_from_errno (errsv),
1845 _("Error moving file: %s"),
1846 g_strerror (errsv));
1850 source_is_dir = S_ISDIR (statbuf.st_mode);
1852 source_size = statbuf.st_size;
1854 destination_exist = FALSE;
1855 res = g_lstat (local_destination->filename, &statbuf);
1858 destination_exist = TRUE; /* Target file exists */
1860 if (flags & G_FILE_COPY_OVERWRITE)
1862 /* Always fail on dirs, even with overwrite */
1863 if (S_ISDIR (statbuf.st_mode))
1867 G_IO_ERROR_WOULD_MERGE,
1868 _("Can't move directory over directory"));
1877 _("Target file exists"));
1882 if (flags & G_FILE_COPY_BACKUP && destination_exist)
1884 backup_name = g_strconcat (local_destination->filename, "~", NULL);
1885 if (rename (local_destination->filename, backup_name) == -1)
1889 G_IO_ERROR_CANT_CREATE_BACKUP,
1890 _("Backup file creation failed"));
1891 g_free (backup_name);
1894 g_free (backup_name);
1895 destination_exist = FALSE; /* It did, but no more */
1898 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
1900 /* Source is a dir, destination exists (and is not a dir, because that would have failed
1901 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
1902 res = unlink (local_destination->filename);
1907 g_set_error (error, G_IO_ERROR,
1908 g_io_error_from_errno (errsv),
1909 _("Error removing target file: %s"),
1910 g_strerror (errsv));
1915 if (rename (local_source->filename, local_destination->filename) == -1)
1920 /* This will cause the fallback code to run */
1921 g_set_error (error, G_IO_ERROR,
1922 G_IO_ERROR_NOT_SUPPORTED,
1923 _("Move between mounts not supported"));
1924 else if (errsv == EINVAL)
1925 /* This must be an invalid filename, on e.g. FAT, or
1926 we're trying to move the file into itself...
1927 We return invalid filename for both... */
1928 g_set_error (error, G_IO_ERROR,
1929 G_IO_ERROR_INVALID_FILENAME,
1930 _("Invalid filename"));
1932 g_set_error (error, G_IO_ERROR,
1933 g_io_error_from_errno (errsv),
1934 _("Error moving file: %s"),
1935 g_strerror (errsv));
1939 /* Make sure we send full copied size */
1940 if (progress_callback)
1941 progress_callback (source_size, source_size, progress_callback_data);
1946 static GFileMonitor*
1947 g_local_file_monitor_dir (GFile *file,
1948 GFileMonitorFlags flags,
1949 GCancellable *cancellable,
1952 GLocalFile* local_file = G_LOCAL_FILE(file);
1953 return _g_local_directory_monitor_new (local_file->filename, flags, error);
1956 static GFileMonitor*
1957 g_local_file_monitor_file (GFile *file,
1958 GFileMonitorFlags flags,
1959 GCancellable *cancellable,
1962 GLocalFile* local_file = G_LOCAL_FILE(file);
1963 return _g_local_file_monitor_new (local_file->filename, flags, error);
1967 g_local_file_file_iface_init (GFileIface *iface)
1969 iface->dup = g_local_file_dup;
1970 iface->hash = g_local_file_hash;
1971 iface->equal = g_local_file_equal;
1972 iface->is_native = g_local_file_is_native;
1973 iface->has_uri_scheme = g_local_file_has_uri_scheme;
1974 iface->get_uri_scheme = g_local_file_get_uri_scheme;
1975 iface->get_basename = g_local_file_get_basename;
1976 iface->get_path = g_local_file_get_path;
1977 iface->get_uri = g_local_file_get_uri;
1978 iface->get_parse_name = g_local_file_get_parse_name;
1979 iface->get_parent = g_local_file_get_parent;
1980 iface->contains_file = g_local_file_contains_file;
1981 iface->get_relative_path = g_local_file_get_relative_path;
1982 iface->resolve_relative_path = g_local_file_resolve_relative_path;
1983 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
1984 iface->set_display_name = g_local_file_set_display_name;
1985 iface->enumerate_children = g_local_file_enumerate_children;
1986 iface->query_info = g_local_file_query_info;
1987 iface->query_filesystem_info = g_local_file_query_filesystem_info;
1988 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
1989 iface->query_settable_attributes = g_local_file_query_settable_attributes;
1990 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
1991 iface->set_attribute = g_local_file_set_attribute;
1992 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
1993 iface->read_fn = g_local_file_read;
1994 iface->append_to = g_local_file_append_to;
1995 iface->create = g_local_file_create;
1996 iface->replace = g_local_file_replace;
1997 iface->delete_file = g_local_file_delete;
1998 iface->trash = g_local_file_trash;
1999 iface->make_directory = g_local_file_make_directory;
2000 iface->make_symbolic_link = g_local_file_make_symbolic_link;
2001 iface->copy = g_local_file_copy;
2002 iface->move = g_local_file_move;
2003 iface->monitor_dir = g_local_file_monitor_dir;
2004 iface->monitor_file = g_local_file_monitor_file;