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 "gmountprivate.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_INFO_COPY_WITH_FILE |
147 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
150 g_file_attribute_info_list_add (list,
151 G_FILE_ATTRIBUTE_UNIX_UID,
152 G_FILE_ATTRIBUTE_TYPE_UINT32,
153 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
154 g_file_attribute_info_list_add (list,
155 G_FILE_ATTRIBUTE_UNIX_GID,
156 G_FILE_ATTRIBUTE_TYPE_UINT32,
157 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
161 g_file_attribute_info_list_add (list,
162 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
163 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
168 g_file_attribute_info_list_add (list,
169 G_FILE_ATTRIBUTE_TIME_MODIFIED,
170 G_FILE_ATTRIBUTE_TYPE_UINT64,
171 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
172 g_file_attribute_info_list_add (list,
173 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
174 G_FILE_ATTRIBUTE_TYPE_UINT32,
175 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
176 g_file_attribute_info_list_add (list,
177 G_FILE_ATTRIBUTE_TIME_ACCESS,
178 G_FILE_ATTRIBUTE_TYPE_UINT64,
179 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
180 g_file_attribute_info_list_add (list,
181 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
182 G_FILE_ATTRIBUTE_TYPE_UINT32,
183 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
186 local_writable_attributes = list;
188 /* Writable namespaces: */
190 list = g_file_attribute_info_list_new ();
193 g_file_attribute_info_list_add (list,
195 G_FILE_ATTRIBUTE_TYPE_STRING,
196 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
197 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
198 g_file_attribute_info_list_add (list,
200 G_FILE_ATTRIBUTE_TYPE_STRING,
201 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
204 local_writable_namespaces = list;
208 g_local_file_init (GLocalFile *local)
214 canonicalize_filename (const char *filename)
216 char *canon, *start, *p, *q;
220 if (!g_path_is_absolute (filename))
222 cwd = g_get_current_dir ();
223 canon = g_build_filename (cwd, filename, NULL);
227 canon = g_strdup (filename);
229 start = (char *)g_path_skip_root (canon);
231 /* POSIX allows double slashes at the start to
232 * mean something special (as does windows too).
233 * So, "//" != "/", but more than two slashes
239 G_IS_DIR_SEPARATOR (*p);
246 memmove (start, start+i, strlen (start+i)+1);
252 if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
254 memmove (p, p+1, strlen (p+1)+1);
256 else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
259 /* Skip previous separator */
263 while (p > start && !G_IS_DIR_SEPARATOR (*p))
265 if (G_IS_DIR_SEPARATOR (*p))
266 *p++ = G_DIR_SEPARATOR;
267 memmove (p, q, strlen (q)+1);
271 /* Skip until next separator */
272 while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
277 /* Canonicalize one separator */
278 *p++ = G_DIR_SEPARATOR;
282 /* Remove additional separators */
284 while (*q && G_IS_DIR_SEPARATOR (*q))
288 memmove (p, q, strlen (q)+1);
291 /* Remove trailing slashes */
292 if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
300 * @filename: filename of the file to create.
302 * Returns: new local #GFile.
305 _g_local_file_new (const char *filename)
309 local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
310 local->filename = canonicalize_filename (filename);
312 return G_FILE (local);
316 g_local_file_is_native (GFile *file)
322 g_local_file_has_uri_scheme (GFile *file,
323 const char *uri_scheme)
325 return g_ascii_strcasecmp (uri_scheme, "file") == 0;
329 g_local_file_get_uri_scheme (GFile *file)
331 return g_strdup ("file");
335 g_local_file_get_basename (GFile *file)
337 return g_path_get_basename (G_LOCAL_FILE (file)->filename);
341 g_local_file_get_path (GFile *file)
343 return g_strdup (G_LOCAL_FILE (file)->filename);
347 g_local_file_get_uri (GFile *file)
349 return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
353 get_filename_charset (const gchar **filename_charset)
355 const gchar **charsets;
358 is_utf8 = g_get_filename_charsets (&charsets);
360 if (filename_charset)
361 *filename_charset = charsets[0];
367 name_is_valid_for_display (const char *string,
368 gboolean is_valid_utf8)
372 if (!is_valid_utf8 &&
373 !g_utf8_validate (string, -1, NULL))
376 while ((c = *string++) != 0)
378 if (g_ascii_iscntrl (c))
386 g_local_file_get_parse_name (GFile *file)
388 const char *filename;
390 const gchar *charset;
392 char *roundtripped_filename;
393 gboolean free_utf8_filename;
394 gboolean is_valid_utf8;
397 filename = G_LOCAL_FILE (file)->filename;
398 if (get_filename_charset (&charset))
400 utf8_filename = (char *)filename;
401 free_utf8_filename = FALSE;
402 is_valid_utf8 = FALSE; /* Can't guarantee this */
406 utf8_filename = g_convert (filename, -1,
407 "UTF-8", charset, NULL, NULL, NULL);
408 free_utf8_filename = TRUE;
409 is_valid_utf8 = TRUE;
411 if (utf8_filename != NULL)
413 /* Make sure we can roundtrip: */
414 roundtripped_filename = g_convert (utf8_filename, -1,
415 charset, "UTF-8", NULL, NULL, NULL);
417 if (roundtripped_filename == NULL ||
418 strcmp (utf8_filename, roundtripped_filename) != 0)
420 g_free (utf8_filename);
421 utf8_filename = NULL;
426 if (utf8_filename != NULL &&
427 name_is_valid_for_display (utf8_filename, is_valid_utf8))
429 if (free_utf8_filename)
430 parse_name = utf8_filename;
432 parse_name = g_strdup (utf8_filename);
436 escaped_path = g_uri_escape_string (filename,
437 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
439 parse_name = g_strconcat ("file://",
440 (*escaped_path != '/') ? "/" : "",
444 g_free (escaped_path);
446 if (free_utf8_filename)
447 g_free (utf8_filename);
454 g_local_file_get_parent (GFile *file)
456 GLocalFile *local = G_LOCAL_FILE (file);
457 const char *non_root;
462 non_root = g_path_skip_root (local->filename);
466 dirname = g_path_get_dirname (local->filename);
467 parent = _g_local_file_new (dirname);
473 g_local_file_dup (GFile *file)
475 GLocalFile *local = G_LOCAL_FILE (file);
477 return _g_local_file_new (local->filename);
481 g_local_file_hash (GFile *file)
483 GLocalFile *local = G_LOCAL_FILE (file);
485 return g_str_hash (local->filename);
489 g_local_file_equal (GFile *file1,
492 GLocalFile *local1 = G_LOCAL_FILE (file1);
493 GLocalFile *local2 = G_LOCAL_FILE (file2);
495 return g_str_equal (local1->filename, local2->filename);
499 match_prefix (const char *path,
504 prefix_len = strlen (prefix);
505 if (strncmp (path, prefix, prefix_len) != 0)
508 /* Handle the case where prefix is the root, so that
509 * the IS_DIR_SEPRARATOR check below works */
510 if (prefix_len > 0 &&
511 G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
514 return path + prefix_len;
518 g_local_file_contains_file (GFile *parent,
521 GLocalFile *parent_local = G_LOCAL_FILE (parent);
522 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
523 const char *remainder;
525 remainder = match_prefix (descendant_local->filename, parent_local->filename);
526 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
532 g_local_file_get_relative_path (GFile *parent,
535 GLocalFile *parent_local = G_LOCAL_FILE (parent);
536 GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
537 const char *remainder;
539 remainder = match_prefix (descendant_local->filename, parent_local->filename);
541 if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
542 return g_strdup (remainder + 1);
547 g_local_file_resolve_relative_path (GFile *file,
548 const char *relative_path)
550 GLocalFile *local = G_LOCAL_FILE (file);
554 if (g_path_is_absolute (relative_path))
555 return _g_local_file_new (relative_path);
557 filename = g_build_filename (local->filename, relative_path, NULL);
558 child = _g_local_file_new (filename);
564 static GFileEnumerator *
565 g_local_file_enumerate_children (GFile *file,
566 const char *attributes,
567 GFileQueryInfoFlags flags,
568 GCancellable *cancellable,
571 GLocalFile *local = G_LOCAL_FILE (file);
572 return _g_local_file_enumerator_new (local->filename,
578 g_local_file_get_child_for_display_name (GFile *file,
579 const char *display_name,
582 GFile *parent, *new_file;
585 basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
586 if (basename == NULL)
588 g_set_error (error, G_IO_ERROR,
589 G_IO_ERROR_INVALID_FILENAME,
590 _("Invalid filename %s"), display_name);
594 parent = g_file_get_parent (file);
595 new_file = g_file_get_child (file, basename);
596 g_object_unref (parent);
604 get_fs_type (long f_type)
607 /* filesystem ids taken from linux manpage */
704 G_LOCK_DEFINE_STATIC(mount_info_hash);
705 static GHashTable *mount_info_hash = NULL;
706 static guint64 mount_info_hash_cache_time = 0;
709 MOUNT_INFO_READONLY = 1<<0
713 device_equal (gconstpointer v1,
716 return *(dev_t *)v1 == *(dev_t *)v2;
720 device_hash (gconstpointer v)
722 return (guint) *(dev_t *)v;
726 get_mount_info (GFileInfo *fs_info,
728 GFileAttributeMatcher *matcher)
732 gpointer info_as_ptr;
736 GUnixMountEntry *mount;
739 if (g_lstat (path, &buf) != 0)
742 G_LOCK (mount_info_hash);
744 if (mount_info_hash == NULL)
745 mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
749 if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
750 g_hash_table_remove_all (mount_info_hash);
752 got_info = g_hash_table_lookup_extended (mount_info_hash,
757 G_UNLOCK (mount_info_hash);
759 mount_info = GPOINTER_TO_UINT (info_as_ptr);
765 mountpoint = find_mountpoint_for (path, buf.st_dev);
766 if (mountpoint == NULL)
769 mount = g_unix_mount_at (mountpoint, &cache_time);
772 if (g_unix_mount_is_readonly (mount))
773 mount_info |= MOUNT_INFO_READONLY;
775 g_unix_mount_free (mount);
778 dev = g_new0 (dev_t, 1);
781 G_LOCK (mount_info_hash);
782 mount_info_hash_cache_time = cache_time;
783 g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
784 G_UNLOCK (mount_info_hash);
787 if (mount_info & MOUNT_INFO_READONLY)
788 g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
794 g_local_file_query_filesystem_info (GFile *file,
795 const char *attributes,
796 GCancellable *cancellable,
799 GLocalFile *local = G_LOCAL_FILE (file);
806 struct statfs statfs_buffer;
808 #elif defined(USE_STATVFS)
809 struct statvfs statfs_buffer;
812 GFileAttributeMatcher *attribute_matcher;
819 statfs_result = statfs (local->filename, &statfs_buffer);
820 #elif STATFS_ARGS == 4
821 statfs_result = statfs (local->filename, &statfs_buffer,
822 sizeof (statfs_buffer), 0);
824 block_size = statfs_buffer.f_bsize;
826 #if defined(__linux__)
827 /* ncpfs does not know the amount of available and free space *
828 * assuming ncpfs is linux specific, if you are on a non-linux platform
829 * where ncpfs is available, please file a bug about it on bugzilla.gnome.org
831 if (statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0 &&
832 /* linux/ncp_fs.h: NCP_SUPER_MAGIC == 0x564c */
833 statfs_buffer.f_type == 0x564c)
837 #elif defined(USE_STATVFS)
838 statfs_result = statvfs (local->filename, &statfs_buffer);
839 block_size = statfs_buffer.f_frsize;
842 if (statfs_result == -1)
846 g_set_error (error, G_IO_ERROR,
847 g_io_error_from_errno (errsv),
848 _("Error getting filesystem info: %s"),
853 info = g_file_info_new ();
855 attribute_matcher = g_file_attribute_matcher_new (attributes);
857 if (g_file_attribute_matcher_matches (attribute_matcher,
858 G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
861 gchar *localdir = g_path_get_dirname (local->filename);
862 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
866 if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
867 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
870 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
873 if (g_file_attribute_matcher_matches (attribute_matcher,
874 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
877 gchar *localdir = g_path_get_dirname (local->filename);
878 wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
882 if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
883 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, (guint64)li.QuadPart);
886 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
890 fstype = get_fs_type (statfs_buffer.f_type);
892 g_file_attribute_matcher_matches (attribute_matcher,
893 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
894 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
897 if (g_file_attribute_matcher_matches (attribute_matcher,
898 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
901 /* need to implement with *unix_mount* */
903 get_mount_info (info, local->filename, attribute_matcher);
907 g_file_attribute_matcher_unref (attribute_matcher);
913 g_local_file_find_enclosing_mount (GFile *file,
914 GCancellable *cancellable,
917 GLocalFile *local = G_LOCAL_FILE (file);
922 if (g_lstat (local->filename, &buf) != 0)
924 g_set_error (error, G_IO_ERROR,
925 G_IO_ERROR_NOT_FOUND,
926 _("Containing mount does not exist"));
930 mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
931 if (mountpoint == NULL)
933 g_set_error (error, G_IO_ERROR,
934 G_IO_ERROR_NOT_FOUND,
935 _("Containing mount does not exist"));
939 mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
944 g_set_error (error, G_IO_ERROR,
945 G_IO_ERROR_NOT_FOUND,
946 _("Containing mount does not exist"));
951 g_local_file_set_display_name (GFile *file,
952 const char *display_name,
953 GCancellable *cancellable,
956 GLocalFile *local, *new_local;
957 GFile *new_file, *parent;
961 parent = g_file_get_parent (file);
964 g_set_error (error, G_IO_ERROR,
966 _("Can't rename root directory"));
970 new_file = g_file_get_child_for_display_name (parent, display_name, error);
971 g_object_unref (parent);
973 if (new_file == NULL)
976 local = G_LOCAL_FILE (file);
977 new_local = G_LOCAL_FILE (new_file);
979 if (!(g_lstat (new_local->filename, &statbuf) == -1 &&
982 g_set_error (error, G_IO_ERROR,
984 _("Can't rename file, filename already exist"));
988 if (rename (local->filename, new_local->filename) == -1)
993 /* We can't get a rename file into itself error herer,
994 so this must be an invalid filename, on e.g. FAT */
995 g_set_error (error, G_IO_ERROR,
996 G_IO_ERROR_INVALID_FILENAME,
997 _("Invalid filename"));
999 g_set_error (error, G_IO_ERROR,
1000 g_io_error_from_errno (errsv),
1001 _("Error renaming file: %s"),
1002 g_strerror (errsv));
1003 g_object_unref (new_file);
1011 g_local_file_query_info (GFile *file,
1012 const char *attributes,
1013 GFileQueryInfoFlags flags,
1014 GCancellable *cancellable,
1017 GLocalFile *local = G_LOCAL_FILE (file);
1019 GFileAttributeMatcher *matcher;
1020 char *basename, *dirname;
1021 GLocalParentFileInfo parent_info;
1023 matcher = g_file_attribute_matcher_new (attributes);
1025 basename = g_path_get_basename (local->filename);
1027 dirname = g_path_get_dirname (local->filename);
1028 _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1031 info = _g_local_file_info_get (basename, local->filename,
1032 matcher, flags, &parent_info,
1037 g_file_attribute_matcher_unref (matcher);
1042 static GFileAttributeInfoList *
1043 g_local_file_query_settable_attributes (GFile *file,
1044 GCancellable *cancellable,
1047 return g_file_attribute_info_list_ref (local_writable_attributes);
1050 static GFileAttributeInfoList *
1051 g_local_file_query_writable_namespaces (GFile *file,
1052 GCancellable *cancellable,
1055 return g_file_attribute_info_list_ref (local_writable_namespaces);
1059 g_local_file_set_attribute (GFile *file,
1060 const char *attribute,
1061 GFileAttributeType type,
1063 GFileQueryInfoFlags flags,
1064 GCancellable *cancellable,
1067 GLocalFile *local = G_LOCAL_FILE (file);
1069 return _g_local_file_info_set_attribute (local->filename,
1079 g_local_file_set_attributes_from_info (GFile *file,
1081 GFileQueryInfoFlags flags,
1082 GCancellable *cancellable,
1085 GLocalFile *local = G_LOCAL_FILE (file);
1086 int res, chained_res;
1087 GFileIface *default_iface;
1089 res = _g_local_file_info_set_attributes (local->filename,
1095 error = NULL; /* Don't write over error if further errors */
1097 default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1099 chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1101 return res && chained_res;
1104 static GFileInputStream *
1105 g_local_file_read (GFile *file,
1106 GCancellable *cancellable,
1109 GLocalFile *local = G_LOCAL_FILE (file);
1113 fd = g_open (local->filename, O_RDONLY, 0);
1118 g_set_error (error, G_IO_ERROR,
1119 g_io_error_from_errno (errsv),
1120 _("Error opening file: %s"),
1121 g_strerror (errsv));
1125 if (fstat(fd, &buf) == 0 && S_ISDIR (buf.st_mode))
1128 g_set_error (error, G_IO_ERROR,
1129 G_IO_ERROR_IS_DIRECTORY,
1130 _("Can't open directory"));
1134 return _g_local_file_input_stream_new (fd);
1137 static GFileOutputStream *
1138 g_local_file_append_to (GFile *file,
1139 GFileCreateFlags flags,
1140 GCancellable *cancellable,
1143 return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1144 flags, cancellable, error);
1147 static GFileOutputStream *
1148 g_local_file_create (GFile *file,
1149 GFileCreateFlags flags,
1150 GCancellable *cancellable,
1153 return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1154 flags, cancellable, error);
1157 static GFileOutputStream *
1158 g_local_file_replace (GFile *file,
1160 gboolean make_backup,
1161 GFileCreateFlags flags,
1162 GCancellable *cancellable,
1165 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1166 etag, make_backup, flags,
1167 cancellable, error);
1172 g_local_file_delete (GFile *file,
1173 GCancellable *cancellable,
1176 GLocalFile *local = G_LOCAL_FILE (file);
1178 if (g_remove (local->filename) == -1)
1182 g_set_error (error, G_IO_ERROR,
1183 g_io_error_from_errno (errsv),
1184 _("Error removing file: %s"),
1185 g_strerror (errsv));
1193 strip_trailing_slashes (const char *path)
1198 path_copy = g_strdup (path);
1199 len = strlen (path_copy);
1200 while (len > 1 && path_copy[len-1] == '/')
1201 path_copy[--len] = 0;
1207 expand_symlink (const char *link)
1209 char *resolved, *canonical, *parent, *link2;
1210 char symlink_value[4096];
1218 res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1221 return g_strdup (link);
1222 symlink_value[res] = 0;
1225 if (g_path_is_absolute (symlink_value))
1226 return canonicalize_filename (symlink_value);
1229 link2 = strip_trailing_slashes (link);
1230 parent = g_path_get_dirname (link2);
1233 resolved = g_build_filename (parent, symlink_value, NULL);
1236 canonical = canonicalize_filename (resolved);
1245 get_parent (const char *path,
1249 struct stat parent_stat;
1253 path_copy = strip_trailing_slashes (path);
1255 parent = g_path_get_dirname (path_copy);
1256 if (strcmp (parent, ".") == 0 ||
1257 strcmp (parent, path_copy) == 0)
1266 if (g_lstat (parent, &parent_stat) != 0)
1272 if (S_ISLNK (parent_stat.st_mode))
1275 parent = expand_symlink (parent);
1280 if (num_recursions > 12)
1285 } while (S_ISLNK (parent_stat.st_mode));
1287 *parent_dev = parent_stat.st_dev;
1293 expand_all_symlinks (const char *path)
1295 char *parent, *parent_expanded;
1296 char *basename, *res;
1299 parent = get_parent (path, &parent_dev);
1302 parent_expanded = expand_all_symlinks (parent);
1304 basename = g_path_get_basename (path);
1305 res = g_build_filename (parent_expanded, basename, NULL);
1307 g_free (parent_expanded);
1310 res = g_strdup (path);
1316 find_mountpoint_for (const char *file,
1320 dev_t dir_dev, parent_dev;
1322 dir = g_strdup (file);
1327 parent = get_parent (dir, &parent_dev);
1331 if (parent_dev != dir_dev)
1345 find_topdir_for (const char *file)
1350 dir = get_parent (file, &dir_dev);
1354 return find_mountpoint_for (dir, dir_dev);
1360 get_unique_filename (const char *basename,
1366 return g_strdup (basename);
1368 dot = strchr (basename, '.');
1370 return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1372 return g_strdup_printf ("%s.%d", basename, id);
1376 path_has_prefix (const char *path,
1384 prefix_len = strlen (prefix);
1386 if (strncmp (path, prefix, prefix_len) == 0 &&
1387 (prefix_len == 0 || /* empty prefix always matches */
1388 prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1389 path[prefix_len] == 0 ||
1390 path[prefix_len] == '/'))
1397 try_make_relative (const char *path,
1400 char *path2, *base2;
1403 path2 = expand_all_symlinks (path);
1404 base2 = expand_all_symlinks (base);
1407 if (path_has_prefix (path2, base2))
1409 relative = path2 + strlen (base2);
1410 while (*relative == '/')
1412 relative = g_strdup (relative);
1420 /* Failed, use abs path */
1421 return g_strdup (path);
1425 escape_trash_name (char *name)
1428 const gchar hex[16] = "0123456789ABCDEF";
1430 str = g_string_new ("");
1438 if (g_ascii_isprint (c))
1439 g_string_append_c (str, c);
1442 g_string_append_c (str, '%');
1443 g_string_append_c (str, hex[((guchar)c) >> 4]);
1444 g_string_append_c (str, hex[((guchar)c) & 0xf]);
1448 return g_string_free (str, FALSE);
1452 g_local_file_trash (GFile *file,
1453 GCancellable *cancellable,
1456 GLocalFile *local = G_LOCAL_FILE (file);
1457 struct stat file_stat, home_stat;
1458 const char *homedir;
1459 char *trashdir, *topdir, *infodir, *filesdir;
1460 char *basename, *trashname, *trashfile, *infoname, *infofile;
1461 char *original_name, *original_name_escaped;
1464 gboolean is_homedir_trash;
1465 char delete_time[32];
1468 struct stat trash_stat, global_stat;
1469 char *dirname, *globaldir;
1472 if (g_lstat (local->filename, &file_stat) != 0)
1476 g_set_error (error, G_IO_ERROR,
1477 g_io_error_from_errno (errsv),
1478 _("Error trashing file: %s"),
1479 g_strerror (errsv));
1483 homedir = g_get_home_dir ();
1484 g_stat (homedir, &home_stat);
1486 is_homedir_trash = FALSE;
1488 if (file_stat.st_dev == home_stat.st_dev)
1490 is_homedir_trash = TRUE;
1492 trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1493 if (g_mkdir_with_parents (trashdir, 0700) < 0)
1498 display_name = g_filename_display_name (trashdir);
1499 g_set_error (error, G_IO_ERROR,
1500 g_io_error_from_errno (errsv),
1501 _("Unable to create trash dir %s: %s"),
1502 display_name, g_strerror (errsv));
1503 g_free (display_name);
1507 topdir = g_strdup (g_get_user_data_dir ());
1512 g_warning ("Recycle bin not implemented");
1518 g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1520 topdir = find_topdir_for (local->filename);
1523 g_set_error (error, G_IO_ERROR,
1524 G_IO_ERROR_NOT_SUPPORTED,
1525 _("Unable to find toplevel directory for trash"));
1529 /* Try looking for global trash dir $topdir/.Trash/$uid */
1530 globaldir = g_build_filename (topdir, ".Trash", NULL);
1531 if (g_lstat (globaldir, &global_stat) == 0 &&
1532 S_ISDIR (global_stat.st_mode) &&
1533 (global_stat.st_mode & S_ISVTX) != 0)
1535 trashdir = g_build_filename (globaldir, uid_str, NULL);
1537 if (g_lstat (trashdir, &trash_stat) == 0)
1539 if (!S_ISDIR (trash_stat.st_mode) ||
1540 trash_stat.st_uid != uid)
1542 /* Not a directory or not owned by user, ignore */
1547 else if (g_mkdir (trashdir, 0700) == -1)
1555 if (trashdir == NULL)
1557 gboolean tried_create;
1559 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1560 dirname = g_strdup_printf (".Trash-%s", uid_str);
1561 trashdir = g_build_filename (topdir, dirname, NULL);
1564 tried_create = FALSE;
1567 if (g_lstat (trashdir, &trash_stat) == 0)
1569 if (!S_ISDIR (trash_stat.st_mode) ||
1570 trash_stat.st_uid != uid)
1572 /* Remove the failed directory */
1574 g_remove (trashdir);
1576 /* Not a directory or not owned by user, ignore */
1583 if (!tried_create &&
1584 g_mkdir (trashdir, 0700) != -1)
1586 /* Ensure that the created dir has the right uid etc.
1587 This might fail on e.g. a FAT dir */
1588 tried_create = TRUE;
1600 if (trashdir == NULL)
1603 g_set_error (error, G_IO_ERROR,
1604 G_IO_ERROR_NOT_SUPPORTED,
1605 _("Unable to find or create trash directory"));
1610 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1612 infodir = g_build_filename (trashdir, "info", NULL);
1613 filesdir = g_build_filename (trashdir, "files", NULL);
1616 /* Make sure we have the subdirectories */
1617 if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1618 (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
1623 g_set_error (error, G_IO_ERROR,
1624 G_IO_ERROR_NOT_SUPPORTED,
1625 _("Unable to find or create trash directory"));
1629 basename = g_path_get_basename (local->filename);
1637 trashname = get_unique_filename (basename, i++);
1638 infoname = g_strconcat (trashname, ".trashinfo", NULL);
1639 infofile = g_build_filename (infodir, infoname, NULL);
1642 fd = open (infofile, O_CREAT | O_EXCL, 0666);
1643 } while (fd == -1 && errno == EEXIST);
1657 g_set_error (error, G_IO_ERROR,
1658 g_io_error_from_errno (errsv),
1659 _("Unable to create trashing info file: %s"),
1660 g_strerror (errsv));
1666 /* TODO: Maybe we should verify that you can delete the file from the trash
1667 before moving it? OTOH, that is hard, as it needs a recursive scan */
1669 trashfile = g_build_filename (filesdir, trashname, NULL);
1673 if (g_rename (local->filename, trashfile) == -1)
1682 g_set_error (error, G_IO_ERROR,
1683 g_io_error_from_errno (errsv),
1684 _("Unable to trash file: %s"),
1685 g_strerror (errsv));
1691 /* TODO: Do we need to update mtime/atime here after the move? */
1693 /* Use absolute names for homedir */
1694 if (is_homedir_trash)
1695 original_name = g_strdup (local->filename);
1697 original_name = try_make_relative (local->filename, topdir);
1698 original_name_escaped = escape_trash_name (original_name);
1700 g_free (original_name);
1706 g_get_current_time (&now);
1707 strncpy (delete_time, g_time_val_to_iso8601 (&now), sizeof (delete_time));
1714 localtime_r (&t, &now);
1716 strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
1720 data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
1721 original_name_escaped, delete_time);
1723 g_file_set_contents (infofile, data, -1, NULL);
1727 g_free (original_name_escaped);
1734 g_local_file_make_directory (GFile *file,
1735 GCancellable *cancellable,
1738 GLocalFile *local = G_LOCAL_FILE (file);
1740 if (g_mkdir (local->filename, 0755) == -1)
1744 if (errsv == EINVAL)
1745 /* This must be an invalid filename, on e.g. FAT */
1746 g_set_error (error, G_IO_ERROR,
1747 G_IO_ERROR_INVALID_FILENAME,
1748 _("Invalid filename"));
1750 g_set_error (error, G_IO_ERROR,
1751 g_io_error_from_errno (errsv),
1752 _("Error removing file: %s"),
1753 g_strerror (errsv));
1761 g_local_file_make_symbolic_link (GFile *file,
1762 const char *symlink_value,
1763 GCancellable *cancellable,
1767 GLocalFile *local = G_LOCAL_FILE (file);
1769 if (symlink (symlink_value, local->filename) == -1)
1773 if (errsv == EINVAL)
1774 /* This must be an invalid filename, on e.g. FAT */
1775 g_set_error (error, G_IO_ERROR,
1776 G_IO_ERROR_INVALID_FILENAME,
1777 _("Invalid filename"));
1779 g_set_error (error, G_IO_ERROR,
1780 g_io_error_from_errno (errsv),
1781 _("Error making symbolic link: %s"),
1782 g_strerror (errsv));
1787 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
1794 g_local_file_copy (GFile *source,
1796 GFileCopyFlags flags,
1797 GCancellable *cancellable,
1798 GFileProgressCallback progress_callback,
1799 gpointer progress_callback_data,
1802 /* Fall back to default copy */
1803 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
1808 g_local_file_move (GFile *source,
1810 GFileCopyFlags flags,
1811 GCancellable *cancellable,
1812 GFileProgressCallback progress_callback,
1813 gpointer progress_callback_data,
1816 GLocalFile *local_source, *local_destination;
1817 struct stat statbuf;
1818 gboolean destination_exist, source_is_dir;
1823 if (!G_IS_LOCAL_FILE (source) ||
1824 !G_IS_LOCAL_FILE (destination))
1826 /* Fall back to default move */
1827 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
1831 local_source = G_LOCAL_FILE (source);
1832 local_destination = G_LOCAL_FILE (destination);
1834 res = g_lstat (local_source->filename, &statbuf);
1839 g_set_error (error, G_IO_ERROR,
1840 g_io_error_from_errno (errsv),
1841 _("Error moving file: %s"),
1842 g_strerror (errsv));
1846 source_is_dir = S_ISDIR (statbuf.st_mode);
1848 source_size = statbuf.st_size;
1850 destination_exist = FALSE;
1851 res = g_lstat (local_destination->filename, &statbuf);
1854 destination_exist = TRUE; /* Target file exists */
1856 if (flags & G_FILE_COPY_OVERWRITE)
1858 /* Always fail on dirs, even with overwrite */
1859 if (S_ISDIR (statbuf.st_mode))
1863 G_IO_ERROR_WOULD_MERGE,
1864 _("Can't move directory over directory"));
1873 _("Target file exists"));
1878 if (flags & G_FILE_COPY_BACKUP && destination_exist)
1880 backup_name = g_strconcat (local_destination->filename, "~", NULL);
1881 if (rename (local_destination->filename, backup_name) == -1)
1885 G_IO_ERROR_CANT_CREATE_BACKUP,
1886 _("Backup file creation failed"));
1887 g_free (backup_name);
1890 g_free (backup_name);
1891 destination_exist = FALSE; /* It did, but no more */
1894 if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
1896 /* Source is a dir, destination exists (and is not a dir, because that would have failed
1897 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
1898 res = unlink (local_destination->filename);
1903 g_set_error (error, G_IO_ERROR,
1904 g_io_error_from_errno (errsv),
1905 _("Error removing target file: %s"),
1906 g_strerror (errsv));
1911 if (rename (local_source->filename, local_destination->filename) == -1)
1916 /* This will cause the fallback code to run */
1917 g_set_error (error, G_IO_ERROR,
1918 G_IO_ERROR_NOT_SUPPORTED,
1919 _("Move between mounts not supported"));
1920 else if (errsv == EINVAL)
1921 /* This must be an invalid filename, on e.g. FAT, or
1922 we're trying to move the file into itself...
1923 We return invalid filename for both... */
1924 g_set_error (error, G_IO_ERROR,
1925 G_IO_ERROR_INVALID_FILENAME,
1926 _("Invalid filename"));
1928 g_set_error (error, G_IO_ERROR,
1929 g_io_error_from_errno (errsv),
1930 _("Error moving file: %s"),
1931 g_strerror (errsv));
1935 /* Make sure we send full copied size */
1936 if (progress_callback)
1937 progress_callback (source_size, source_size, progress_callback_data);
1942 static GFileMonitor*
1943 g_local_file_monitor_dir (GFile *file,
1944 GFileMonitorFlags flags,
1945 GCancellable *cancellable,
1948 GLocalFile* local_file = G_LOCAL_FILE(file);
1949 return _g_local_directory_monitor_new (local_file->filename, flags, error);
1952 static GFileMonitor*
1953 g_local_file_monitor_file (GFile *file,
1954 GFileMonitorFlags flags,
1955 GCancellable *cancellable,
1958 GLocalFile* local_file = G_LOCAL_FILE(file);
1959 return _g_local_file_monitor_new (local_file->filename, flags, error);
1963 g_local_file_file_iface_init (GFileIface *iface)
1965 iface->dup = g_local_file_dup;
1966 iface->hash = g_local_file_hash;
1967 iface->equal = g_local_file_equal;
1968 iface->is_native = g_local_file_is_native;
1969 iface->has_uri_scheme = g_local_file_has_uri_scheme;
1970 iface->get_uri_scheme = g_local_file_get_uri_scheme;
1971 iface->get_basename = g_local_file_get_basename;
1972 iface->get_path = g_local_file_get_path;
1973 iface->get_uri = g_local_file_get_uri;
1974 iface->get_parse_name = g_local_file_get_parse_name;
1975 iface->get_parent = g_local_file_get_parent;
1976 iface->contains_file = g_local_file_contains_file;
1977 iface->get_relative_path = g_local_file_get_relative_path;
1978 iface->resolve_relative_path = g_local_file_resolve_relative_path;
1979 iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
1980 iface->set_display_name = g_local_file_set_display_name;
1981 iface->enumerate_children = g_local_file_enumerate_children;
1982 iface->query_info = g_local_file_query_info;
1983 iface->query_filesystem_info = g_local_file_query_filesystem_info;
1984 iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
1985 iface->query_settable_attributes = g_local_file_query_settable_attributes;
1986 iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
1987 iface->set_attribute = g_local_file_set_attribute;
1988 iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
1989 iface->read_fn = g_local_file_read;
1990 iface->append_to = g_local_file_append_to;
1991 iface->create = g_local_file_create;
1992 iface->replace = g_local_file_replace;
1993 iface->delete_file = g_local_file_delete;
1994 iface->trash = g_local_file_trash;
1995 iface->make_directory = g_local_file_make_directory;
1996 iface->make_symbolic_link = g_local_file_make_symbolic_link;
1997 iface->copy = g_local_file_copy;
1998 iface->move = g_local_file_move;
1999 iface->monitor_dir = g_local_file_monitor_dir;
2000 iface->monitor_file = g_local_file_monitor_file;