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.1 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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
23 #include <sys/types.h>
34 #include <sys/statfs.h>
36 #if HAVE_SYS_STATVFS_H
37 #include <sys/statvfs.h>
41 #elif HAVE_SYS_MOUNT_H
43 #include <sys/param.h>
45 #include <sys/mount.h>
52 #include "gfileattribute.h"
53 #include "glocalfile.h"
54 #include "glocalfileprivate.h"
55 #include "glocalfileinfo.h"
56 #include "glocalfileenumerator.h"
57 #include "glocalfileinputstream.h"
58 #include "glocalfileoutputstream.h"
59 #include "glocalfileiostream.h"
60 #include "glocalfilemonitor.h"
61 #include "gmountprivate.h"
62 #include "gunixmounts.h"
64 #include <glib/gstdio.h>
65 #include <glib/gstdioprivate.h>
68 #include "glib-unix.h"
71 #include "glib-private.h"
78 #ifndef FILE_READ_ONLY_VOLUME
79 #define FILE_READ_ONLY_VOLUME 0x00080000
83 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
86 #define S_ISLNK(m) (0)
95 static void g_local_file_file_iface_init (GFileIface
*iface
);
97 static GFileAttributeInfoList
*local_writable_attributes
= NULL
;
98 static /* GFileAttributeInfoList * */ gsize local_writable_namespaces
= 0;
102 GObject parent_instance
;
107 #define g_local_file_get_type _g_local_file_get_type
108 G_DEFINE_TYPE_WITH_CODE (GLocalFile
, g_local_file
, G_TYPE_OBJECT
,
109 G_IMPLEMENT_INTERFACE (G_TYPE_FILE
,
110 g_local_file_file_iface_init
))
112 static char *find_mountpoint_for (const char *file
, dev_t dev
);
115 g_local_file_finalize (GObject
*object
)
119 local
= G_LOCAL_FILE (object
);
121 g_free (local
->filename
);
123 G_OBJECT_CLASS (g_local_file_parent_class
)->finalize (object
);
127 g_local_file_class_init (GLocalFileClass
*klass
)
129 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
130 GFileAttributeInfoList
*list
;
132 gobject_class
->finalize
= g_local_file_finalize
;
134 /* Set up attribute lists */
136 /* Writable attributes: */
138 list
= g_file_attribute_info_list_new ();
140 g_file_attribute_info_list_add (list
,
141 G_FILE_ATTRIBUTE_UNIX_MODE
,
142 G_FILE_ATTRIBUTE_TYPE_UINT32
,
143 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE
|
144 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
147 g_file_attribute_info_list_add (list
,
148 G_FILE_ATTRIBUTE_UNIX_UID
,
149 G_FILE_ATTRIBUTE_TYPE_UINT32
,
150 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
151 g_file_attribute_info_list_add (list
,
152 G_FILE_ATTRIBUTE_UNIX_GID
,
153 G_FILE_ATTRIBUTE_TYPE_UINT32
,
154 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
158 g_file_attribute_info_list_add (list
,
159 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET
,
160 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING
,
165 g_file_attribute_info_list_add (list
,
166 G_FILE_ATTRIBUTE_TIME_MODIFIED
,
167 G_FILE_ATTRIBUTE_TYPE_UINT64
,
168 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE
|
169 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
170 g_file_attribute_info_list_add (list
,
171 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC
,
172 G_FILE_ATTRIBUTE_TYPE_UINT32
,
173 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE
|
174 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
175 /* When copying, the target file is accessed. Replicating
176 * the source access time does not make sense in this case.
178 g_file_attribute_info_list_add (list
,
179 G_FILE_ATTRIBUTE_TIME_ACCESS
,
180 G_FILE_ATTRIBUTE_TYPE_UINT64
,
181 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
182 g_file_attribute_info_list_add (list
,
183 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC
,
184 G_FILE_ATTRIBUTE_TYPE_UINT32
,
185 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
188 local_writable_attributes
= list
;
192 g_local_file_init (GLocalFile
*local
)
197 _g_local_file_get_filename (GLocalFile
*file
)
199 return file
->filename
;
203 _g_local_file_new (const char *filename
)
207 local
= g_object_new (G_TYPE_LOCAL_FILE
, NULL
);
208 local
->filename
= g_canonicalize_filename (filename
, NULL
);
210 return G_FILE (local
);
214 * g_local_file_new_from_dirname_and_basename:
215 * @dirname: an absolute, canonical directory name
216 * @basename: the name of a child inside @dirname
218 * Creates a #GFile from @dirname and @basename.
220 * This is more efficient than pasting the fields together for yourself
221 * and creating a #GFile from the result, and also more efficient than
222 * creating a #GFile for the dirname and using g_file_get_child().
224 * @dirname must be canonical, as per GLocalFile's opinion of what
225 * canonical means. This means that you should only pass strings that
226 * were returned by _g_local_file_get_filename().
231 g_local_file_new_from_dirname_and_basename (const gchar
*dirname
,
232 const gchar
*basename
)
236 g_return_val_if_fail (dirname
!= NULL
, NULL
);
237 g_return_val_if_fail (basename
&& basename
[0] && !strchr (basename
, '/'), NULL
);
239 local
= g_object_new (G_TYPE_LOCAL_FILE
, NULL
);
240 local
->filename
= g_build_filename (dirname
, basename
, NULL
);
242 return G_FILE (local
);
246 g_local_file_is_native (GFile
*file
)
252 g_local_file_has_uri_scheme (GFile
*file
,
253 const char *uri_scheme
)
255 return g_ascii_strcasecmp (uri_scheme
, "file") == 0;
259 g_local_file_get_uri_scheme (GFile
*file
)
261 return g_strdup ("file");
265 g_local_file_get_basename (GFile
*file
)
267 return g_path_get_basename (G_LOCAL_FILE (file
)->filename
);
271 g_local_file_get_path (GFile
*file
)
273 return g_strdup (G_LOCAL_FILE (file
)->filename
);
277 g_local_file_get_uri (GFile
*file
)
279 return g_filename_to_uri (G_LOCAL_FILE (file
)->filename
, NULL
, NULL
);
283 get_filename_charset (const gchar
**filename_charset
)
285 const gchar
**charsets
;
288 is_utf8
= g_get_filename_charsets (&charsets
);
290 if (filename_charset
)
291 *filename_charset
= charsets
[0];
297 name_is_valid_for_display (const char *string
,
298 gboolean is_valid_utf8
)
302 if (!is_valid_utf8
&&
303 !g_utf8_validate (string
, -1, NULL
))
306 while ((c
= *string
++) != 0)
308 if (g_ascii_iscntrl (c
))
316 g_local_file_get_parse_name (GFile
*file
)
318 const char *filename
;
320 const gchar
*charset
;
322 char *roundtripped_filename
;
323 gboolean free_utf8_filename
;
324 gboolean is_valid_utf8
;
327 filename
= G_LOCAL_FILE (file
)->filename
;
328 if (get_filename_charset (&charset
))
330 utf8_filename
= (char *)filename
;
331 free_utf8_filename
= FALSE
;
332 is_valid_utf8
= FALSE
; /* Can't guarantee this */
336 utf8_filename
= g_convert (filename
, -1,
337 "UTF-8", charset
, NULL
, NULL
, NULL
);
338 free_utf8_filename
= TRUE
;
339 is_valid_utf8
= TRUE
;
341 if (utf8_filename
!= NULL
)
343 /* Make sure we can roundtrip: */
344 roundtripped_filename
= g_convert (utf8_filename
, -1,
345 charset
, "UTF-8", NULL
, NULL
, NULL
);
347 if (roundtripped_filename
== NULL
||
348 strcmp (filename
, roundtripped_filename
) != 0)
350 g_free (utf8_filename
);
351 utf8_filename
= NULL
;
354 g_free (roundtripped_filename
);
358 if (utf8_filename
!= NULL
&&
359 name_is_valid_for_display (utf8_filename
, is_valid_utf8
))
361 if (free_utf8_filename
)
362 parse_name
= utf8_filename
;
364 parse_name
= g_strdup (utf8_filename
);
369 char *dup_filename
, *p
, *backslash
;
371 /* Turn backslashes into forward slashes like
372 * g_filename_to_uri() would do (but we can't use that because
373 * it doesn't output IRIs).
375 dup_filename
= g_strdup (filename
);
376 filename
= p
= dup_filename
;
378 while ((backslash
= strchr (p
, '\\')) != NULL
)
385 escaped_path
= g_uri_escape_string (filename
,
386 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT
"/",
388 parse_name
= g_strconcat ("file://",
389 (*escaped_path
!= '/') ? "/" : "",
393 g_free (escaped_path
);
395 g_free (dup_filename
);
397 if (free_utf8_filename
)
398 g_free (utf8_filename
);
405 g_local_file_get_parent (GFile
*file
)
407 GLocalFile
*local
= G_LOCAL_FILE (file
);
408 const char *non_root
;
412 /* Check for root; local->filename is guaranteed to be absolute, so
413 * g_path_skip_root() should never return NULL. */
414 non_root
= g_path_skip_root (local
->filename
);
415 g_assert (non_root
!= NULL
);
420 dirname
= g_path_get_dirname (local
->filename
);
421 parent
= _g_local_file_new (dirname
);
427 g_local_file_dup (GFile
*file
)
429 GLocalFile
*local
= G_LOCAL_FILE (file
);
431 return _g_local_file_new (local
->filename
);
435 g_local_file_hash (GFile
*file
)
437 GLocalFile
*local
= G_LOCAL_FILE (file
);
439 return g_str_hash (local
->filename
);
443 g_local_file_equal (GFile
*file1
,
446 GLocalFile
*local1
= G_LOCAL_FILE (file1
);
447 GLocalFile
*local2
= G_LOCAL_FILE (file2
);
449 return g_str_equal (local1
->filename
, local2
->filename
);
453 match_prefix (const char *path
,
458 prefix_len
= strlen (prefix
);
459 if (strncmp (path
, prefix
, prefix_len
) != 0)
462 /* Handle the case where prefix is the root, so that
463 * the IS_DIR_SEPRARATOR check below works */
464 if (prefix_len
> 0 &&
465 G_IS_DIR_SEPARATOR (prefix
[prefix_len
-1]))
468 return path
+ prefix_len
;
472 g_local_file_prefix_matches (GFile
*parent
,
475 GLocalFile
*parent_local
= G_LOCAL_FILE (parent
);
476 GLocalFile
*descendant_local
= G_LOCAL_FILE (descendant
);
477 const char *remainder
;
479 remainder
= match_prefix (descendant_local
->filename
, parent_local
->filename
);
480 if (remainder
!= NULL
&& G_IS_DIR_SEPARATOR (*remainder
))
486 g_local_file_get_relative_path (GFile
*parent
,
489 GLocalFile
*parent_local
= G_LOCAL_FILE (parent
);
490 GLocalFile
*descendant_local
= G_LOCAL_FILE (descendant
);
491 const char *remainder
;
493 remainder
= match_prefix (descendant_local
->filename
, parent_local
->filename
);
495 if (remainder
!= NULL
&& G_IS_DIR_SEPARATOR (*remainder
))
496 return g_strdup (remainder
+ 1);
501 g_local_file_resolve_relative_path (GFile
*file
,
502 const char *relative_path
)
504 GLocalFile
*local
= G_LOCAL_FILE (file
);
508 if (g_path_is_absolute (relative_path
))
509 return _g_local_file_new (relative_path
);
511 filename
= g_build_filename (local
->filename
, relative_path
, NULL
);
512 child
= _g_local_file_new (filename
);
518 static GFileEnumerator
*
519 g_local_file_enumerate_children (GFile
*file
,
520 const char *attributes
,
521 GFileQueryInfoFlags flags
,
522 GCancellable
*cancellable
,
525 GLocalFile
*local
= G_LOCAL_FILE (file
);
526 return _g_local_file_enumerator_new (local
,
532 g_local_file_get_child_for_display_name (GFile
*file
,
533 const char *display_name
,
539 basename
= g_filename_from_utf8 (display_name
, -1, NULL
, NULL
, NULL
);
540 if (basename
== NULL
)
542 g_set_error (error
, G_IO_ERROR
,
543 G_IO_ERROR_INVALID_FILENAME
,
544 _("Invalid filename %s"), display_name
);
548 new_file
= g_file_get_child (file
, basename
);
554 #if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
556 get_fs_type (long f_type
)
558 /* filesystem ids taken from linux manpage */
576 return "binfmt_misc";
580 return "btrfs_test_fs";
733 G_LOCK_DEFINE_STATIC(mount_info_hash
);
734 static GHashTable
*mount_info_hash
= NULL
;
735 static guint64 mount_info_hash_cache_time
= 0;
738 MOUNT_INFO_READONLY
= 1<<0
742 device_equal (gconstpointer v1
,
745 return *(dev_t
*)v1
== *(dev_t
*)v2
;
749 device_hash (gconstpointer v
)
751 return (guint
) *(dev_t
*)v
;
755 get_mount_info (GFileInfo
*fs_info
,
757 GFileAttributeMatcher
*matcher
)
761 gpointer info_as_ptr
;
765 GUnixMountEntry
*mount
;
768 if (g_lstat (path
, &buf
) != 0)
771 G_LOCK (mount_info_hash
);
773 if (mount_info_hash
== NULL
)
774 mount_info_hash
= g_hash_table_new_full (device_hash
, device_equal
,
778 if (g_unix_mounts_changed_since (mount_info_hash_cache_time
))
779 g_hash_table_remove_all (mount_info_hash
);
781 got_info
= g_hash_table_lookup_extended (mount_info_hash
,
786 G_UNLOCK (mount_info_hash
);
788 mount_info
= GPOINTER_TO_UINT (info_as_ptr
);
794 mountpoint
= find_mountpoint_for (path
, buf
.st_dev
);
795 if (mountpoint
== NULL
)
796 mountpoint
= g_strdup ("/");
798 mount
= g_unix_mount_at (mountpoint
, &cache_time
);
801 if (g_unix_mount_is_readonly (mount
))
802 mount_info
|= MOUNT_INFO_READONLY
;
804 g_unix_mount_free (mount
);
809 dev
= g_new0 (dev_t
, 1);
812 G_LOCK (mount_info_hash
);
813 mount_info_hash_cache_time
= cache_time
;
814 g_hash_table_insert (mount_info_hash
, dev
, GUINT_TO_POINTER (mount_info
));
815 G_UNLOCK (mount_info_hash
);
818 if (mount_info
& MOUNT_INFO_READONLY
)
819 g_file_info_set_attribute_boolean (fs_info
, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY
, TRUE
);
827 is_xp_or_later (void)
829 static int result
= -1;
834 OSVERSIONINFOEX ver_info
= {0};
835 DWORDLONG cond_mask
= 0;
836 int op
= VER_GREATER_EQUAL
;
838 ver_info
.dwOSVersionInfoSize
= sizeof ver_info
;
839 ver_info
.dwMajorVersion
= 5;
840 ver_info
.dwMinorVersion
= 1;
842 VER_SET_CONDITION (cond_mask
, VER_MAJORVERSION
, op
);
843 VER_SET_CONDITION (cond_mask
, VER_MINORVERSION
, op
);
845 result
= VerifyVersionInfo (&ver_info
,
846 VER_MAJORVERSION
| VER_MINORVERSION
,
849 result
= ((DWORD
)(LOBYTE (LOWORD (GetVersion ())))) >= 5;
857 get_volume_for_path (const char *path
)
863 wpath
= g_utf8_to_utf16 (path
, -1, NULL
, NULL
, NULL
);
864 result
= g_new (wchar_t, MAX_PATH
);
866 if (!GetVolumePathNameW (wpath
, result
, MAX_PATH
))
868 char *msg
= g_win32_error_message (GetLastError ());
869 g_critical ("GetVolumePathName failed: %s", msg
);
876 len
= wcslen (result
);
877 if (len
> 0 && result
[len
-1] != L
'\\')
879 result
= g_renew (wchar_t, result
, len
+ 2);
889 find_mountpoint_for (const char *file
, dev_t dev
)
894 wpath
= get_volume_for_path (file
);
898 utf8_path
= g_utf16_to_utf8 (wpath
, -1, NULL
, NULL
, NULL
);
905 get_filesystem_readonly (GFileInfo
*info
,
910 rootdir
= get_volume_for_path (path
);
914 if (is_xp_or_later ())
917 if (GetVolumeInformationW (rootdir
, NULL
, 0, NULL
, NULL
, &flags
, NULL
, 0))
918 g_file_info_set_attribute_boolean (info
, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY
,
919 (flags
& FILE_READ_ONLY_VOLUME
) != 0);
923 if (GetDriveTypeW (rootdir
) == DRIVE_CDROM
)
924 g_file_info_set_attribute_boolean (info
, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY
, TRUE
);
931 #endif /* G_OS_WIN32 */
933 #pragma GCC diagnostic push
934 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
936 g_set_io_error (GError
**error
,
941 GLocalFile
*local
= G_LOCAL_FILE (file
);
944 display_name
= g_filename_display_name (local
->filename
);
945 g_set_error (error
, G_IO_ERROR
, g_io_error_from_errno (errsv
),
946 msg
, display_name
, g_strerror (errsv
));
947 g_free (display_name
);
949 #pragma GCC diagnostic pop
952 g_local_file_query_filesystem_info (GFile
*file
,
953 const char *attributes
,
954 GCancellable
*cancellable
,
957 GLocalFile
*local
= G_LOCAL_FILE (file
);
959 int statfs_result
= 0;
965 struct statfs statfs_buffer
;
966 #elif defined(USE_STATVFS)
968 struct statvfs statfs_buffer
;
969 #endif /* USE_STATFS */
970 #endif /* G_OS_WIN32 */
971 GFileAttributeMatcher
*attribute_matcher
;
978 statfs_result
= statfs (local
->filename
, &statfs_buffer
);
979 #elif STATFS_ARGS == 4
980 statfs_result
= statfs (local
->filename
, &statfs_buffer
,
981 sizeof (statfs_buffer
), 0);
982 #endif /* STATFS_ARGS == 2 */
983 block_size
= statfs_buffer
.f_bsize
;
985 /* Many backends can't report free size (for instance the gvfs fuse
986 * backend for backend not supporting this), and set f_bfree to 0,
987 * but it can be 0 for real too. We treat the available == 0 and
988 * free == 0 case as "both of these are invalid", but only on file systems
989 * which are known to not support this (otherwise we can omit metadata for
990 * systems which are legitimately full). */
991 #if defined(__linux__)
992 if (statfs_result
== 0 &&
993 statfs_buffer
.f_bavail
== 0 && statfs_buffer
.f_bfree
== 0 &&
994 (/* linux/ncp_fs.h: NCP_SUPER_MAGIC == 0x564c */
995 statfs_buffer
.f_type
== 0x564c ||
996 /* man statfs: FUSE_SUPER_MAGIC == 0x65735546 */
997 statfs_buffer
.f_type
== 0x65735546))
999 #endif /* __linux__ */
1001 #elif defined(USE_STATVFS)
1002 statfs_result
= statvfs (local
->filename
, &statfs_buffer
);
1003 block_size
= statfs_buffer
.f_frsize
;
1004 #endif /* USE_STATFS */
1006 if (statfs_result
== -1)
1010 g_set_io_error (error
,
1011 _("Error getting filesystem info for %s: %s"),
1016 info
= g_file_info_new ();
1018 attribute_matcher
= g_file_attribute_matcher_new (attributes
);
1021 g_file_attribute_matcher_matches (attribute_matcher
,
1022 G_FILE_ATTRIBUTE_FILESYSTEM_FREE
))
1025 gchar
*localdir
= g_path_get_dirname (local
->filename
);
1026 wchar_t *wdirname
= g_utf8_to_utf16 (localdir
, -1, NULL
, NULL
, NULL
);
1030 if (GetDiskFreeSpaceExW (wdirname
, &li
, NULL
, NULL
))
1031 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_FREE
, (guint64
)li
.QuadPart
);
1034 #if defined(USE_STATFS) || defined(USE_STATVFS)
1035 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_FREE
, block_size
* statfs_buffer
.f_bavail
);
1040 g_file_attribute_matcher_matches (attribute_matcher
,
1041 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
))
1044 gchar
*localdir
= g_path_get_dirname (local
->filename
);
1045 wchar_t *wdirname
= g_utf8_to_utf16 (localdir
, -1, NULL
, NULL
, NULL
);
1049 if (GetDiskFreeSpaceExW (wdirname
, NULL
, &li
, NULL
))
1050 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
, (guint64
)li
.QuadPart
);
1053 #if defined(USE_STATFS) || defined(USE_STATVFS)
1054 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
, block_size
* statfs_buffer
.f_blocks
);
1056 #endif /* G_OS_WIN32 */
1060 g_file_attribute_matcher_matches (attribute_matcher
,
1061 G_FILE_ATTRIBUTE_FILESYSTEM_USED
))
1064 gchar
*localdir
= g_path_get_dirname (local
->filename
);
1065 wchar_t *wdirname
= g_utf8_to_utf16 (localdir
, -1, NULL
, NULL
, NULL
);
1066 ULARGE_INTEGER li_free
;
1067 ULARGE_INTEGER li_total
;
1070 if (GetDiskFreeSpaceExW (wdirname
, &li_free
, &li_total
, NULL
))
1071 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_USED
, (guint64
)li_total
.QuadPart
- (guint64
)li_free
.QuadPart
);
1074 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_USED
, block_size
* (statfs_buffer
.f_blocks
- statfs_buffer
.f_bfree
));
1075 #endif /* G_OS_WIN32 */
1080 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1081 fstype
= statfs_buffer
.f_fstypename
;
1083 fstype
= get_fs_type (statfs_buffer
.f_type
);
1086 #elif defined(USE_STATVFS)
1087 #if defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
1088 fstype
= statfs_buffer
.f_fstypename
;
1089 #elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1090 fstype
= statfs_buffer
.f_basetype
;
1094 #endif /* USE_STATFS */
1097 g_file_attribute_matcher_matches (attribute_matcher
,
1098 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE
))
1099 g_file_info_set_attribute_string (info
, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE
, fstype
);
1100 #endif /* G_OS_WIN32 */
1102 if (g_file_attribute_matcher_matches (attribute_matcher
,
1103 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY
))
1106 get_filesystem_readonly (info
, local
->filename
);
1108 get_mount_info (info
, local
->filename
, attribute_matcher
);
1109 #endif /* G_OS_WIN32 */
1112 if (g_file_attribute_matcher_matches (attribute_matcher
,
1113 G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE
))
1114 g_file_info_set_attribute_boolean (info
, G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE
,
1115 g_local_file_is_remote (local
->filename
));
1117 g_file_attribute_matcher_unref (attribute_matcher
);
1123 g_local_file_find_enclosing_mount (GFile
*file
,
1124 GCancellable
*cancellable
,
1127 GLocalFile
*local
= G_LOCAL_FILE (file
);
1132 if (g_lstat (local
->filename
, &buf
) != 0)
1135 mountpoint
= find_mountpoint_for (local
->filename
, buf
.st_dev
);
1136 if (mountpoint
== NULL
)
1139 mount
= _g_mount_get_for_mount_path (mountpoint
, cancellable
);
1140 g_free (mountpoint
);
1145 g_set_io_error (error
,
1146 /* Translators: This is an error message when trying to find
1147 * the enclosing (user visible) mount of a file, but none
1150 _("Containing mount for file %s not found"),
1157 g_local_file_set_display_name (GFile
*file
,
1158 const char *display_name
,
1159 GCancellable
*cancellable
,
1162 GLocalFile
*local
, *new_local
;
1163 GFile
*new_file
, *parent
;
1169 parent
= g_file_get_parent (file
);
1172 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
1173 _("Can’t rename root directory"));
1177 new_file
= g_file_get_child_for_display_name (parent
, display_name
, error
);
1178 g_object_unref (parent
);
1180 if (new_file
== NULL
)
1182 local
= G_LOCAL_FILE (file
);
1183 new_local
= G_LOCAL_FILE (new_file
);
1185 if (g_lstat (new_local
->filename
, &statbuf
) == -1)
1189 if (errsv
!= ENOENT
)
1191 g_set_io_error (error
, _("Error renaming file %s: %s"), new_file
, errsv
);
1197 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_EXISTS
,
1198 _("Can’t rename file, filename already exists"));
1202 if (g_rename (local
->filename
, new_local
->filename
) == -1)
1206 if (errsv
== EINVAL
)
1207 /* We can't get a rename file into itself error here,
1208 * so this must be an invalid filename, on e.g. FAT
1210 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_FILENAME
,
1211 _("Invalid filename"));
1213 g_set_io_error (error
,
1214 _("Error renaming file %s: %s"),
1216 g_object_unref (new_file
);
1220 vfs
= g_vfs_get_default ();
1221 class = G_VFS_GET_CLASS (vfs
);
1222 if (class->local_file_moved
)
1223 class->local_file_moved (vfs
, local
->filename
, new_local
->filename
);
1229 g_local_file_query_info (GFile
*file
,
1230 const char *attributes
,
1231 GFileQueryInfoFlags flags
,
1232 GCancellable
*cancellable
,
1235 GLocalFile
*local
= G_LOCAL_FILE (file
);
1237 GFileAttributeMatcher
*matcher
;
1238 char *basename
, *dirname
;
1239 GLocalParentFileInfo parent_info
;
1241 matcher
= g_file_attribute_matcher_new (attributes
);
1243 basename
= g_path_get_basename (local
->filename
);
1245 dirname
= g_path_get_dirname (local
->filename
);
1246 _g_local_file_info_get_parent_info (dirname
, matcher
, &parent_info
);
1249 info
= _g_local_file_info_get (basename
, local
->filename
,
1250 matcher
, flags
, &parent_info
,
1254 _g_local_file_info_free_parent_info (&parent_info
);
1257 g_file_attribute_matcher_unref (matcher
);
1262 static GFileAttributeInfoList
*
1263 g_local_file_query_settable_attributes (GFile
*file
,
1264 GCancellable
*cancellable
,
1267 return g_file_attribute_info_list_ref (local_writable_attributes
);
1270 static GFileAttributeInfoList
*
1271 g_local_file_query_writable_namespaces (GFile
*file
,
1272 GCancellable
*cancellable
,
1275 GFileAttributeInfoList
*list
;
1279 if (g_once_init_enter (&local_writable_namespaces
))
1281 /* Writable namespaces: */
1283 list
= g_file_attribute_info_list_new ();
1286 g_file_attribute_info_list_add (list
,
1288 G_FILE_ATTRIBUTE_TYPE_STRING
,
1289 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE
|
1290 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
1291 g_file_attribute_info_list_add (list
,
1293 G_FILE_ATTRIBUTE_TYPE_STRING
,
1294 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
1297 vfs
= g_vfs_get_default ();
1298 class = G_VFS_GET_CLASS (vfs
);
1299 if (class->add_writable_namespaces
)
1300 class->add_writable_namespaces (vfs
, list
);
1302 g_once_init_leave (&local_writable_namespaces
, (gsize
)list
);
1304 list
= (GFileAttributeInfoList
*)local_writable_namespaces
;
1306 return g_file_attribute_info_list_ref (list
);
1310 g_local_file_set_attribute (GFile
*file
,
1311 const char *attribute
,
1312 GFileAttributeType type
,
1314 GFileQueryInfoFlags flags
,
1315 GCancellable
*cancellable
,
1318 GLocalFile
*local
= G_LOCAL_FILE (file
);
1320 return _g_local_file_info_set_attribute (local
->filename
,
1330 g_local_file_set_attributes_from_info (GFile
*file
,
1332 GFileQueryInfoFlags flags
,
1333 GCancellable
*cancellable
,
1336 GLocalFile
*local
= G_LOCAL_FILE (file
);
1337 int res
, chained_res
;
1338 GFileIface
*default_iface
;
1340 res
= _g_local_file_info_set_attributes (local
->filename
,
1346 error
= NULL
; /* Don't write over error if further errors */
1348 default_iface
= g_type_default_interface_peek (G_TYPE_FILE
);
1350 chained_res
= (default_iface
->set_attributes_from_info
) (file
, info
, flags
, cancellable
, error
);
1352 return res
&& chained_res
;
1355 static GFileInputStream
*
1356 g_local_file_read (GFile
*file
,
1357 GCancellable
*cancellable
,
1360 GLocalFile
*local
= G_LOCAL_FILE (file
);
1364 fd
= g_open (local
->filename
, O_RDONLY
|O_BINARY
, 0);
1370 if (errsv
== EACCES
)
1372 /* Exploit the fact that on W32 the glib filename encoding is UTF8 */
1373 ret
= GLIB_PRIVATE_CALL (g_win32_stat_utf8
) (local
->filename
, &buf
);
1374 if (ret
== 0 && S_ISDIR (buf
.st_mode
))
1378 g_set_io_error (error
,
1379 _("Error opening file %s: %s"),
1385 ret
= GLIB_PRIVATE_CALL (g_win32_fstat
) (fd
, &buf
);
1387 ret
= fstat (fd
, &buf
);
1390 if (ret
== 0 && S_ISDIR (buf
.st_mode
))
1392 (void) g_close (fd
, NULL
);
1393 g_set_io_error (error
,
1394 _("Error opening file %s: %s"),
1399 return _g_local_file_input_stream_new (fd
);
1402 static GFileOutputStream
*
1403 g_local_file_append_to (GFile
*file
,
1404 GFileCreateFlags flags
,
1405 GCancellable
*cancellable
,
1408 return _g_local_file_output_stream_append (G_LOCAL_FILE (file
)->filename
,
1409 flags
, cancellable
, error
);
1412 static GFileOutputStream
*
1413 g_local_file_create (GFile
*file
,
1414 GFileCreateFlags flags
,
1415 GCancellable
*cancellable
,
1418 return _g_local_file_output_stream_create (G_LOCAL_FILE (file
)->filename
,
1420 cancellable
, error
);
1423 static GFileOutputStream
*
1424 g_local_file_replace (GFile
*file
,
1426 gboolean make_backup
,
1427 GFileCreateFlags flags
,
1428 GCancellable
*cancellable
,
1431 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file
)->filename
,
1433 etag
, make_backup
, flags
, NULL
,
1434 cancellable
, error
);
1437 static GFileIOStream
*
1438 g_local_file_open_readwrite (GFile
*file
,
1439 GCancellable
*cancellable
,
1442 GFileOutputStream
*output
;
1445 output
= _g_local_file_output_stream_open (G_LOCAL_FILE (file
)->filename
,
1447 cancellable
, error
);
1451 res
= _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output
));
1452 g_object_unref (output
);
1456 static GFileIOStream
*
1457 g_local_file_create_readwrite (GFile
*file
,
1458 GFileCreateFlags flags
,
1459 GCancellable
*cancellable
,
1462 GFileOutputStream
*output
;
1465 output
= _g_local_file_output_stream_create (G_LOCAL_FILE (file
)->filename
,
1467 cancellable
, error
);
1471 res
= _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output
));
1472 g_object_unref (output
);
1476 static GFileIOStream
*
1477 g_local_file_replace_readwrite (GFile
*file
,
1479 gboolean make_backup
,
1480 GFileCreateFlags flags
,
1481 GCancellable
*cancellable
,
1484 GFileOutputStream
*output
;
1487 output
= _g_local_file_output_stream_replace (G_LOCAL_FILE (file
)->filename
,
1489 etag
, make_backup
, flags
, NULL
,
1490 cancellable
, error
);
1494 res
= _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output
));
1495 g_object_unref (output
);
1500 g_local_file_delete (GFile
*file
,
1501 GCancellable
*cancellable
,
1504 GLocalFile
*local
= G_LOCAL_FILE (file
);
1508 if (g_remove (local
->filename
) == -1)
1512 /* Posix allows EEXIST too, but the more sane error
1513 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1515 if (errsv
== EEXIST
)
1518 g_set_io_error (error
,
1519 _("Error removing file %s: %s"),
1524 vfs
= g_vfs_get_default ();
1525 class = G_VFS_GET_CLASS (vfs
);
1526 if (class->local_file_removed
)
1527 class->local_file_removed (vfs
, local
->filename
);
1535 strip_trailing_slashes (const char *path
)
1540 path_copy
= g_strdup (path
);
1541 len
= strlen (path_copy
);
1542 while (len
> 1 && path_copy
[len
-1] == '/')
1543 path_copy
[--len
] = 0;
1549 expand_symlink (const char *link
)
1551 char *resolved
, *canonical
, *parent
, *link2
;
1552 char symlink_value
[4096];
1560 res
= readlink (link
, symlink_value
, sizeof (symlink_value
) - 1);
1563 return g_strdup (link
);
1564 symlink_value
[res
] = 0;
1567 if (g_path_is_absolute (symlink_value
))
1568 return g_canonicalize_filename (symlink_value
, NULL
);
1571 link2
= strip_trailing_slashes (link
);
1572 parent
= g_path_get_dirname (link2
);
1575 resolved
= g_build_filename (parent
, symlink_value
, NULL
);
1578 canonical
= g_canonicalize_filename (resolved
, NULL
);
1587 get_parent (const char *path
,
1591 GStatBuf parent_stat
;
1595 path_copy
= strip_trailing_slashes (path
);
1597 parent
= g_path_get_dirname (path_copy
);
1598 if (strcmp (parent
, ".") == 0 ||
1599 strcmp (parent
, path_copy
) == 0)
1609 if (g_lstat (parent
, &parent_stat
) != 0)
1615 if (S_ISLNK (parent_stat
.st_mode
))
1618 parent
= expand_symlink (parent
);
1623 if (num_recursions
> 12)
1628 } while (S_ISLNK (parent_stat
.st_mode
));
1630 *parent_dev
= parent_stat
.st_dev
;
1636 expand_all_symlinks (const char *path
)
1638 char *parent
, *parent_expanded
;
1639 char *basename
, *res
;
1642 parent
= get_parent (path
, &parent_dev
);
1645 parent_expanded
= expand_all_symlinks (parent
);
1647 basename
= g_path_get_basename (path
);
1648 res
= g_build_filename (parent_expanded
, basename
, NULL
);
1650 g_free (parent_expanded
);
1653 res
= g_strdup (path
);
1659 find_mountpoint_for (const char *file
,
1663 dev_t dir_dev
, parent_dev
;
1665 dir
= g_strdup (file
);
1670 parent
= get_parent (dir
, &parent_dev
);
1674 if (parent_dev
!= dir_dev
)
1686 _g_local_file_find_topdir_for (const char *file
)
1689 char *mountpoint
= NULL
;
1692 dir
= get_parent (file
, &dir_dev
);
1696 mountpoint
= find_mountpoint_for (dir
, dir_dev
);
1703 get_unique_filename (const char *basename
,
1709 return g_strdup (basename
);
1711 dot
= strchr (basename
, '.');
1713 return g_strdup_printf ("%.*s.%d%s", (int)(dot
- basename
), basename
, id
, dot
);
1715 return g_strdup_printf ("%s.%d", basename
, id
);
1719 path_has_prefix (const char *path
,
1727 prefix_len
= strlen (prefix
);
1729 if (strncmp (path
, prefix
, prefix_len
) == 0 &&
1730 (prefix_len
== 0 || /* empty prefix always matches */
1731 prefix
[prefix_len
- 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1732 path
[prefix_len
] == 0 ||
1733 path
[prefix_len
] == '/'))
1740 try_make_relative (const char *path
,
1743 char *path2
, *base2
;
1746 path2
= expand_all_symlinks (path
);
1747 base2
= expand_all_symlinks (base
);
1750 if (path_has_prefix (path2
, base2
))
1752 relative
= path2
+ strlen (base2
);
1753 while (*relative
== '/')
1755 relative
= g_strdup (relative
);
1763 /* Failed, use abs path */
1764 return g_strdup (path
);
1768 _g_local_file_has_trash_dir (const char *dirname
, dev_t dir_dev
)
1770 static gsize home_dev_set
= 0;
1771 static dev_t home_dev
;
1772 char *topdir
, *globaldir
, *trashdir
, *tmpname
;
1775 GStatBuf global_stat
, trash_stat
;
1777 GUnixMountEntry
*mount
;
1779 if (g_once_init_enter (&home_dev_set
))
1783 g_stat (g_get_home_dir (), &home_stat
);
1784 home_dev
= home_stat
.st_dev
;
1785 g_once_init_leave (&home_dev_set
, 1);
1788 /* Assume we can trash to the home */
1789 if (dir_dev
== home_dev
)
1792 topdir
= find_mountpoint_for (dirname
, dir_dev
);
1796 mount
= g_unix_mount_at (topdir
, NULL
);
1797 if (mount
== NULL
|| g_unix_mount_is_system_internal (mount
))
1799 g_clear_pointer (&mount
, g_unix_mount_free
);
1805 g_clear_pointer (&mount
, g_unix_mount_free
);
1807 globaldir
= g_build_filename (topdir
, ".Trash", NULL
);
1808 if (g_lstat (globaldir
, &global_stat
) == 0 &&
1809 S_ISDIR (global_stat
.st_mode
) &&
1810 (global_stat
.st_mode
& S_ISVTX
) != 0)
1812 /* got a toplevel sysadmin created dir, assume we
1813 * can trash to it (we should be able to create a dir)
1814 * This fails for the FAT case where the ownership of
1815 * that dir would be wrong though..
1823 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1825 g_snprintf (uid_str
, sizeof (uid_str
), "%lu", (unsigned long) uid
);
1827 tmpname
= g_strdup_printf (".Trash-%s", uid_str
);
1828 trashdir
= g_build_filename (topdir
, tmpname
, NULL
);
1831 if (g_lstat (trashdir
, &trash_stat
) == 0)
1835 return S_ISDIR (trash_stat
.st_mode
) &&
1836 trash_stat
.st_uid
== uid
;
1840 /* User specific trash didn't exist, can we create it? */
1841 res
= g_access (topdir
, W_OK
) == 0;
1849 _g_local_file_is_lost_found_dir (const char *path
, dev_t path_dev
)
1851 gboolean ret
= FALSE
;
1852 gchar
*mount_dir
= NULL
;
1853 size_t mount_dir_len
;
1856 if (!g_str_has_suffix (path
, "/lost+found"))
1859 mount_dir
= find_mountpoint_for (path
, path_dev
);
1860 if (mount_dir
== NULL
)
1863 mount_dir_len
= strlen (mount_dir
);
1864 /* We special-case rootfs ('/') since it's the only case where
1865 * mount_dir ends in '/'
1867 if (mount_dir_len
== 1)
1869 if (mount_dir_len
+ strlen ("/lost+found") != strlen (path
))
1872 if (g_lstat (path
, &statbuf
) != 0)
1875 if (!(S_ISDIR (statbuf
.st_mode
) &&
1876 statbuf
.st_uid
== 0 &&
1877 statbuf
.st_gid
== 0))
1889 g_local_file_trash (GFile
*file
,
1890 GCancellable
*cancellable
,
1893 GLocalFile
*local
= G_LOCAL_FILE (file
);
1894 GStatBuf file_stat
, home_stat
;
1895 const char *homedir
;
1896 char *trashdir
, *topdir
, *infodir
, *filesdir
;
1897 char *basename
, *trashname
, *trashfile
, *infoname
, *infofile
;
1898 char *original_name
, *original_name_escaped
;
1902 gboolean is_homedir_trash
;
1903 char *delete_time
= NULL
;
1905 GStatBuf trash_stat
, global_stat
;
1906 char *dirname
, *globaldir
;
1911 if (g_lstat (local
->filename
, &file_stat
) != 0)
1915 g_set_io_error (error
,
1916 _("Error trashing file %s: %s"),
1921 homedir
= g_get_home_dir ();
1922 g_stat (homedir
, &home_stat
);
1924 is_homedir_trash
= FALSE
;
1927 /* On overlayfs, a file's st_dev will be different to the home directory's.
1928 * We still want to create our trash directory under the home directory, so
1929 * instead we should stat the directory that the file we're deleting is in as
1930 * this will have the same st_dev.
1932 if (!S_ISDIR (file_stat
.st_mode
))
1934 path
= g_path_get_dirname (local
->filename
);
1935 /* If the parent is a symlink to a different device then it might have
1936 * st_dev equal to the home directory's, in which case we will end up
1937 * trying to rename across a filesystem boundary, which doesn't work. So
1938 * we use g_stat here instead of g_lstat, to know where the symlink
1940 g_stat (path
, &file_stat
);
1944 if (file_stat
.st_dev
== home_stat
.st_dev
)
1946 is_homedir_trash
= TRUE
;
1948 trashdir
= g_build_filename (g_get_user_data_dir (), "Trash", NULL
);
1949 if (g_mkdir_with_parents (trashdir
, 0700) < 0)
1954 display_name
= g_filename_display_name (trashdir
);
1955 g_set_error (error
, G_IO_ERROR
,
1956 g_io_error_from_errno (errsv
),
1957 _("Unable to create trash dir %s: %s"),
1958 display_name
, g_strerror (errsv
));
1959 g_free (display_name
);
1963 topdir
= g_strdup (g_get_user_data_dir ());
1969 GUnixMountEntry
*mount
;
1972 g_snprintf (uid_str
, sizeof (uid_str
), "%lu", (unsigned long)uid
);
1974 topdir
= _g_local_file_find_topdir_for (local
->filename
);
1977 g_set_io_error (error
,
1978 _("Unable to find toplevel directory to trash %s"),
1983 mount
= g_unix_mount_at (topdir
, NULL
);
1984 if (mount
== NULL
|| g_unix_mount_is_system_internal (mount
))
1986 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
1987 _("Trashing on system internal mounts is not supported"));
1989 g_clear_pointer (&mount
, g_unix_mount_free
);
1995 g_clear_pointer (&mount
, g_unix_mount_free
);
1997 /* Try looking for global trash dir $topdir/.Trash/$uid */
1998 globaldir
= g_build_filename (topdir
, ".Trash", NULL
);
1999 if (g_lstat (globaldir
, &global_stat
) == 0 &&
2000 S_ISDIR (global_stat
.st_mode
) &&
2001 (global_stat
.st_mode
& S_ISVTX
) != 0)
2003 trashdir
= g_build_filename (globaldir
, uid_str
, NULL
);
2005 if (g_lstat (trashdir
, &trash_stat
) == 0)
2007 if (!S_ISDIR (trash_stat
.st_mode
) ||
2008 trash_stat
.st_uid
!= uid
)
2010 /* Not a directory or not owned by user, ignore */
2015 else if (g_mkdir (trashdir
, 0700) == -1)
2023 if (trashdir
== NULL
)
2025 gboolean tried_create
;
2027 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
2028 dirname
= g_strdup_printf (".Trash-%s", uid_str
);
2029 trashdir
= g_build_filename (topdir
, dirname
, NULL
);
2032 tried_create
= FALSE
;
2035 if (g_lstat (trashdir
, &trash_stat
) == 0)
2037 if (!S_ISDIR (trash_stat
.st_mode
) ||
2038 trash_stat
.st_uid
!= uid
)
2040 /* Remove the failed directory */
2042 g_remove (trashdir
);
2044 /* Not a directory or not owned by user, ignore */
2051 if (!tried_create
&&
2052 g_mkdir (trashdir
, 0700) != -1)
2054 /* Ensure that the created dir has the right uid etc.
2055 This might fail on e.g. a FAT dir */
2056 tried_create
= TRUE
;
2067 if (trashdir
== NULL
)
2070 g_set_io_error (error
,
2071 _("Unable to find or create trash directory for %s"),
2072 file
, G_IO_ERROR_NOT_SUPPORTED
);
2077 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
2079 infodir
= g_build_filename (trashdir
, "info", NULL
);
2080 filesdir
= g_build_filename (trashdir
, "files", NULL
);
2083 /* Make sure we have the subdirectories */
2084 if ((g_mkdir (infodir
, 0700) == -1 && errno
!= EEXIST
) ||
2085 (g_mkdir (filesdir
, 0700) == -1 && errno
!= EEXIST
))
2090 g_set_io_error (error
,
2091 _("Unable to find or create trash directory for %s"),
2092 file
, G_IO_ERROR_NOT_SUPPORTED
);
2096 basename
= g_path_get_basename (local
->filename
);
2104 trashname
= get_unique_filename (basename
, i
++);
2105 infoname
= g_strconcat (trashname
, ".trashinfo", NULL
);
2106 infofile
= g_build_filename (infodir
, infoname
, NULL
);
2109 fd
= g_open (infofile
, O_CREAT
| O_EXCL
, 0666);
2111 } while (fd
== -1 && errsv
== EEXIST
);
2125 g_set_io_error (error
,
2126 _("Unable to create trashing info file for %s: %s"),
2131 (void) g_close (fd
, NULL
);
2133 /* Write the full content of the info file before trashing to make
2134 * sure someone doesn't read an empty file. See #749314
2137 /* Use absolute names for homedir */
2138 if (is_homedir_trash
)
2139 original_name
= g_strdup (local
->filename
);
2141 original_name
= try_make_relative (local
->filename
, topdir
);
2142 original_name_escaped
= g_uri_escape_string (original_name
, "/", FALSE
);
2144 g_free (original_name
);
2148 GDateTime
*now
= g_date_time_new_now_local ();
2150 delete_time
= g_date_time_format (now
, "%Y-%m-%dT%H:%M:%S");
2152 delete_time
= g_strdup ("9999-12-31T23:59:59");
2153 g_date_time_unref (now
);
2156 data
= g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2157 original_name_escaped
, delete_time
);
2158 g_free (delete_time
);
2160 g_file_set_contents (infofile
, data
, -1, NULL
);
2162 /* TODO: Maybe we should verify that you can delete the file from the trash
2163 * before moving it? OTOH, that is hard, as it needs a recursive scan
2166 trashfile
= g_build_filename (filesdir
, trashname
, NULL
);
2170 if (g_rename (local
->filename
, trashfile
) == -1)
2174 g_unlink (infofile
);
2181 /* The trash dir was actually on another fs anyway!?
2182 * This can happen when the same device is mounted multiple
2183 * times, or with bind mounts of the same fs.
2185 g_set_io_error (error
,
2186 _("Unable to trash file %s across filesystem boundaries"),
2189 g_set_io_error (error
,
2190 _("Unable to trash file %s: %s"),
2195 vfs
= g_vfs_get_default ();
2196 class = G_VFS_GET_CLASS (vfs
);
2197 if (class->local_file_moved
)
2198 class->local_file_moved (vfs
, local
->filename
, trashfile
);
2202 /* TODO: Do we need to update mtime/atime here after the move? */
2207 g_free (original_name_escaped
);
2212 #else /* G_OS_WIN32 */
2214 _g_local_file_has_trash_dir (const char *dirname
, dev_t dir_dev
)
2216 return FALSE
; /* XXX ??? */
2220 g_local_file_trash (GFile
*file
,
2221 GCancellable
*cancellable
,
2224 GLocalFile
*local
= G_LOCAL_FILE (file
);
2225 SHFILEOPSTRUCTW op
= {0};
2230 wfilename
= g_utf8_to_utf16 (local
->filename
, -1, NULL
, &len
, NULL
);
2231 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2232 wfilename
= g_renew (wchar_t, wfilename
, len
+ 2);
2233 wfilename
[len
+ 1] = 0;
2235 op
.wFunc
= FO_DELETE
;
2236 op
.pFrom
= wfilename
;
2237 op
.fFlags
= FOF_ALLOWUNDO
;
2239 success
= SHFileOperationW (&op
) == 0;
2241 if (success
&& op
.fAnyOperationsAborted
)
2243 if (cancellable
&& !g_cancellable_is_cancelled (cancellable
))
2244 g_cancellable_cancel (cancellable
);
2245 g_set_io_error (error
,
2246 _("Unable to trash file %s: %s"),
2251 g_set_io_error (error
,
2252 _("Unable to trash file %s"),
2258 #endif /* G_OS_WIN32 */
2261 g_local_file_make_directory (GFile
*file
,
2262 GCancellable
*cancellable
,
2265 GLocalFile
*local
= G_LOCAL_FILE (file
);
2267 if (g_mkdir (local
->filename
, 0777) == -1)
2271 if (errsv
== EINVAL
)
2272 /* This must be an invalid filename, on e.g. FAT */
2273 g_set_error_literal (error
, G_IO_ERROR
,
2274 G_IO_ERROR_INVALID_FILENAME
,
2275 _("Invalid filename"));
2277 g_set_io_error (error
,
2278 _("Error creating directory %s: %s"),
2287 g_local_file_make_symbolic_link (GFile
*file
,
2288 const char *symlink_value
,
2289 GCancellable
*cancellable
,
2293 GLocalFile
*local
= G_LOCAL_FILE (file
);
2295 if (symlink (symlink_value
, local
->filename
) == -1)
2299 if (errsv
== EINVAL
)
2300 /* This must be an invalid filename, on e.g. FAT */
2301 g_set_error_literal (error
, G_IO_ERROR
,
2302 G_IO_ERROR_INVALID_FILENAME
,
2303 _("Invalid filename"));
2304 else if (errsv
== EPERM
)
2305 g_set_error (error
, G_IO_ERROR
,
2306 G_IO_ERROR_NOT_SUPPORTED
,
2307 _("Filesystem does not support symbolic links"));
2309 g_set_io_error (error
,
2310 _("Error making symbolic link %s: %s"),
2316 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
, _("Symbolic links not supported"));
2323 g_local_file_copy (GFile
*source
,
2325 GFileCopyFlags flags
,
2326 GCancellable
*cancellable
,
2327 GFileProgressCallback progress_callback
,
2328 gpointer progress_callback_data
,
2331 /* Fall back to default copy */
2332 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
, "Copy not supported");
2337 g_local_file_move (GFile
*source
,
2339 GFileCopyFlags flags
,
2340 GCancellable
*cancellable
,
2341 GFileProgressCallback progress_callback
,
2342 gpointer progress_callback_data
,
2345 GLocalFile
*local_source
, *local_destination
;
2347 gboolean destination_exist
, source_is_dir
;
2354 if (!G_IS_LOCAL_FILE (source
) ||
2355 !G_IS_LOCAL_FILE (destination
))
2357 /* Fall back to default move */
2358 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
, "Move not supported");
2362 local_source
= G_LOCAL_FILE (source
);
2363 local_destination
= G_LOCAL_FILE (destination
);
2365 res
= g_lstat (local_source
->filename
, &statbuf
);
2370 g_set_io_error (error
,
2371 _("Error moving file %s: %s"),
2376 source_is_dir
= S_ISDIR (statbuf
.st_mode
);
2377 source_size
= statbuf
.st_size
;
2379 destination_exist
= FALSE
;
2380 res
= g_lstat (local_destination
->filename
, &statbuf
);
2383 destination_exist
= TRUE
; /* Target file exists */
2385 if (flags
& G_FILE_COPY_OVERWRITE
)
2387 /* Always fail on dirs, even with overwrite */
2388 if (S_ISDIR (statbuf
.st_mode
))
2391 g_set_error_literal (error
,
2393 G_IO_ERROR_WOULD_MERGE
,
2394 _("Can’t move directory over directory"));
2396 g_set_error_literal (error
,
2398 G_IO_ERROR_IS_DIRECTORY
,
2399 _("Can’t copy over directory"));
2405 g_set_io_error (error
,
2406 _("Error moving file %s: %s"),
2412 if (flags
& G_FILE_COPY_BACKUP
&& destination_exist
)
2414 backup_name
= g_strconcat (local_destination
->filename
, "~", NULL
);
2415 if (g_rename (local_destination
->filename
, backup_name
) == -1)
2417 g_set_error_literal (error
,
2419 G_IO_ERROR_CANT_CREATE_BACKUP
,
2420 _("Backup file creation failed"));
2421 g_free (backup_name
);
2424 g_free (backup_name
);
2425 destination_exist
= FALSE
; /* It did, but no more */
2428 if (source_is_dir
&& destination_exist
&& (flags
& G_FILE_COPY_OVERWRITE
))
2430 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2431 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2432 res
= g_unlink (local_destination
->filename
);
2437 g_set_error (error
, G_IO_ERROR
,
2438 g_io_error_from_errno (errsv
),
2439 _("Error removing target file: %s"),
2440 g_strerror (errsv
));
2445 if (g_rename (local_source
->filename
, local_destination
->filename
) == -1)
2450 /* This will cause the fallback code to run */
2451 g_set_error_literal (error
, G_IO_ERROR
,
2452 G_IO_ERROR_NOT_SUPPORTED
,
2453 _("Move between mounts not supported"));
2454 else if (errsv
== EINVAL
)
2455 /* This must be an invalid filename, on e.g. FAT, or
2456 we're trying to move the file into itself...
2457 We return invalid filename for both... */
2458 g_set_error_literal (error
, G_IO_ERROR
,
2459 G_IO_ERROR_INVALID_FILENAME
,
2460 _("Invalid filename"));
2462 g_set_io_error (error
,
2463 _("Error moving file %s: %s"),
2468 vfs
= g_vfs_get_default ();
2469 class = G_VFS_GET_CLASS (vfs
);
2470 if (class->local_file_moved
)
2471 class->local_file_moved (vfs
, local_source
->filename
, local_destination
->filename
);
2473 /* Make sure we send full copied size */
2474 if (progress_callback
)
2475 progress_callback (source_size
, source_size
, progress_callback_data
);
2483 g_local_file_is_remote (const gchar
*filename
)
2491 is_remote_fs (const gchar
*filename
)
2493 const char *fsname
= NULL
;
2496 struct statfs statfs_buffer
;
2497 int statfs_result
= 0;
2499 #if STATFS_ARGS == 2
2500 statfs_result
= statfs (filename
, &statfs_buffer
);
2501 #elif STATFS_ARGS == 4
2502 statfs_result
= statfs (filename
, &statfs_buffer
, sizeof (statfs_buffer
), 0);
2505 #elif defined(USE_STATVFS)
2506 struct statvfs statfs_buffer
;
2507 int statfs_result
= 0;
2509 statfs_result
= statvfs (filename
, &statfs_buffer
);
2514 if (statfs_result
== -1)
2518 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
2519 fsname
= statfs_buffer
.f_fstypename
;
2521 fsname
= get_fs_type (statfs_buffer
.f_type
);
2524 #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
2525 fsname
= statfs_buffer
.f_basetype
;
2530 if (strcmp (fsname
, "nfs") == 0)
2532 if (strcmp (fsname
, "nfs4") == 0)
2540 g_local_file_is_remote (const gchar
*filename
)
2542 static gboolean remote_home
;
2543 static gsize initialized
;
2546 home
= g_get_home_dir ();
2547 if (path_has_prefix (filename
, home
))
2549 if (g_once_init_enter (&initialized
))
2551 remote_home
= is_remote_fs (home
);
2552 g_once_init_leave (&initialized
, TRUE
);
2559 #endif /* !G_OS_WIN32 */
2561 static GFileMonitor
*
2562 g_local_file_monitor_dir (GFile
*file
,
2563 GFileMonitorFlags flags
,
2564 GCancellable
*cancellable
,
2567 GLocalFile
*local_file
= G_LOCAL_FILE (file
);
2569 return g_local_file_monitor_new_for_path (local_file
->filename
, TRUE
, flags
, error
);
2572 static GFileMonitor
*
2573 g_local_file_monitor_file (GFile
*file
,
2574 GFileMonitorFlags flags
,
2575 GCancellable
*cancellable
,
2578 GLocalFile
*local_file
= G_LOCAL_FILE (file
);
2580 return g_local_file_monitor_new_for_path (local_file
->filename
, FALSE
, flags
, error
);
2583 /* Here is the GLocalFile implementation of g_file_measure_disk_usage().
2585 * If available, we use fopenat() in preference to filenames for
2586 * efficiency and safety reasons. We know that fopenat() is available
2587 * based on if AT_FDCWD is defined. POSIX guarantees that this will be
2588 * defined as a macro.
2590 * We use a linked list of stack-allocated GSList nodes in order to be
2591 * able to reconstruct the filename for error messages. We actually
2592 * pass the filename to operate on through the top node of the list.
2594 * In case we're using openat(), this top filename will be a basename
2595 * which should be opened in the directory which has also had its fd
2596 * passed along. If we're not using openat() then it will be a full
2597 * absolute filename.
2601 g_local_file_measure_size_error (GFileMeasureFlags flags
,
2606 /* Only report an error if we were at the toplevel or if the caller
2607 * requested reporting of all errors.
2609 if ((name
->next
== NULL
) || (flags
& G_FILE_MEASURE_REPORT_ANY_ERROR
))
2614 /* Skip some work if there is no error return */
2619 /* If using openat() we need to rebuild the filename for the message */
2620 filename
= g_string_new (name
->data
);
2621 for (node
= name
->next
; node
; node
= node
->next
)
2625 g_string_prepend_c (filename
, G_DIR_SEPARATOR
);
2626 utf8
= g_filename_display_name (node
->data
);
2627 g_string_prepend (filename
, utf8
);
2634 /* Otherwise, we already have it, so just use it. */
2636 filename
= g_string_new (NULL
);
2637 utf8
= g_filename_display_name (node
->data
);
2638 g_string_append (filename
, utf8
);
2643 g_set_error (error
, G_IO_ERROR
, g_io_error_from_errno (saved_errno
),
2644 _("Could not determine the disk usage of %s: %s"),
2645 filename
->str
, g_strerror (saved_errno
));
2647 g_string_free (filename
, TRUE
);
2653 /* We're not reporting this error... */
2659 GFileMeasureFlags flags
;
2661 GCancellable
*cancellable
;
2663 GFileMeasureProgressCallback progress_callback
;
2664 gpointer progress_data
;
2670 guint64 last_progress_report
;
2674 g_local_file_measure_size_of_contents (gint fd
,
2676 MeasureState
*state
,
2680 g_local_file_measure_size_of_file (gint parent_fd
,
2682 MeasureState
*state
,
2687 if (g_cancellable_set_error_if_cancelled (state
->cancellable
, error
))
2690 #if defined (AT_FDCWD)
2691 if (fstatat (parent_fd
, name
->data
, &buf
, AT_SYMLINK_NOFOLLOW
) != 0)
2694 return g_local_file_measure_size_error (state
->flags
, errsv
, name
, error
);
2696 #elif defined (HAVE_LSTAT) || !defined (G_OS_WIN32)
2697 if (g_lstat (name
->data
, &buf
) != 0)
2700 return g_local_file_measure_size_error (state
->flags
, errsv
, name
, error
);
2702 #else /* !AT_FDCWD && !HAVE_LSTAT && G_OS_WIN32 */
2703 if (GLIB_PRIVATE_CALL (g_win32_lstat_utf8
) (name
->data
, &buf
) != 0)
2706 return g_local_file_measure_size_error (state
->flags
, errsv
, name
, error
);
2712 /* If not at the toplevel, check for a device boundary. */
2714 if (state
->flags
& G_FILE_MEASURE_NO_XDEV
)
2715 if (state
->contained_on
!= buf
.st_dev
)
2720 /* If, however, this is the toplevel, set the device number so
2721 * that recursive invocations can compare against it.
2723 state
->contained_on
= buf
.st_dev
;
2726 #if defined (G_OS_WIN32)
2727 if (~state
->flags
& G_FILE_MEASURE_APPARENT_SIZE
)
2728 state
->disk_usage
+= buf
.allocated_size
;
2730 #elif defined (HAVE_STRUCT_STAT_ST_BLOCKS)
2731 if (~state
->flags
& G_FILE_MEASURE_APPARENT_SIZE
)
2732 state
->disk_usage
+= buf
.st_blocks
* G_GUINT64_CONSTANT (512);
2735 state
->disk_usage
+= buf
.st_size
;
2737 if (S_ISDIR (buf
.st_mode
))
2742 if (state
->progress_callback
)
2744 /* We could attempt to do some cleverness here in order to avoid
2745 * calling clock_gettime() so much, but we're doing stats and opens
2746 * all over the place already...
2748 if (state
->last_progress_report
)
2752 now
= g_get_monotonic_time ();
2754 if (state
->last_progress_report
+ 200 * G_TIME_SPAN_MILLISECOND
< now
)
2756 (* state
->progress_callback
) (TRUE
,
2757 state
->disk_usage
, state
->num_dirs
, state
->num_files
,
2758 state
->progress_data
);
2759 state
->last_progress_report
= now
;
2764 /* We must do an initial report to inform that more reports
2767 (* state
->progress_callback
) (TRUE
, 0, 0, 0, state
->progress_data
);
2768 state
->last_progress_report
= g_get_monotonic_time ();
2772 if (S_ISDIR (buf
.st_mode
))
2779 if (g_cancellable_set_error_if_cancelled (state
->cancellable
, error
))
2783 #ifdef HAVE_OPEN_O_DIRECTORY
2784 dir_fd
= openat (parent_fd
, name
->data
, O_RDONLY
|O_DIRECTORY
);
2786 dir_fd
= openat (parent_fd
, name
->data
, O_RDONLY
);
2790 return g_local_file_measure_size_error (state
->flags
, errsv
, name
, error
);
2793 if (!g_local_file_measure_size_of_contents (dir_fd
, name
, state
, error
))
2801 g_local_file_measure_size_of_contents (gint fd
,
2803 MeasureState
*state
,
2806 gboolean success
= TRUE
;
2812 /* If this fails, we want to preserve the errno from fopendir() */
2814 dirp
= fdopendir (fd
);
2815 dir
= dirp
? GLIB_PRIVATE_CALL(g_dir_new_from_dirp
) (dirp
) : NULL
;
2818 dir
= GLIB_PRIVATE_CALL(g_dir_open_with_errno
) (dir_name
->data
, 0);
2823 gint saved_errno
= errno
;
2829 return g_local_file_measure_size_error (state
->flags
, saved_errno
, dir_name
, error
);
2832 while (success
&& (name
= g_dir_read_name (dir
)))
2836 node
.next
= dir_name
;
2838 node
.data
= (gchar
*) name
;
2840 node
.data
= g_build_filename (dir_name
->data
, name
, NULL
);
2843 success
= g_local_file_measure_size_of_file (fd
, &node
, state
, error
);
2856 g_local_file_measure_disk_usage (GFile
*file
,
2857 GFileMeasureFlags flags
,
2858 GCancellable
*cancellable
,
2859 GFileMeasureProgressCallback progress_callback
,
2860 gpointer progress_data
,
2861 guint64
*disk_usage
,
2866 GLocalFile
*local_file
= G_LOCAL_FILE (file
);
2867 MeasureState state
= { 0, };
2871 state
.flags
= flags
;
2872 state
.cancellable
= cancellable
;
2873 state
.progress_callback
= progress_callback
;
2874 state
.progress_data
= progress_data
;
2880 node
.data
= local_file
->filename
;
2883 if (!g_local_file_measure_size_of_file (root_fd
, &node
, &state
, error
))
2887 *disk_usage
= state
.disk_usage
;
2890 *num_dirs
= state
.num_dirs
;
2893 *num_files
= state
.num_files
;
2899 g_local_file_file_iface_init (GFileIface
*iface
)
2901 iface
->dup
= g_local_file_dup
;
2902 iface
->hash
= g_local_file_hash
;
2903 iface
->equal
= g_local_file_equal
;
2904 iface
->is_native
= g_local_file_is_native
;
2905 iface
->has_uri_scheme
= g_local_file_has_uri_scheme
;
2906 iface
->get_uri_scheme
= g_local_file_get_uri_scheme
;
2907 iface
->get_basename
= g_local_file_get_basename
;
2908 iface
->get_path
= g_local_file_get_path
;
2909 iface
->get_uri
= g_local_file_get_uri
;
2910 iface
->get_parse_name
= g_local_file_get_parse_name
;
2911 iface
->get_parent
= g_local_file_get_parent
;
2912 iface
->prefix_matches
= g_local_file_prefix_matches
;
2913 iface
->get_relative_path
= g_local_file_get_relative_path
;
2914 iface
->resolve_relative_path
= g_local_file_resolve_relative_path
;
2915 iface
->get_child_for_display_name
= g_local_file_get_child_for_display_name
;
2916 iface
->set_display_name
= g_local_file_set_display_name
;
2917 iface
->enumerate_children
= g_local_file_enumerate_children
;
2918 iface
->query_info
= g_local_file_query_info
;
2919 iface
->query_filesystem_info
= g_local_file_query_filesystem_info
;
2920 iface
->find_enclosing_mount
= g_local_file_find_enclosing_mount
;
2921 iface
->query_settable_attributes
= g_local_file_query_settable_attributes
;
2922 iface
->query_writable_namespaces
= g_local_file_query_writable_namespaces
;
2923 iface
->set_attribute
= g_local_file_set_attribute
;
2924 iface
->set_attributes_from_info
= g_local_file_set_attributes_from_info
;
2925 iface
->read_fn
= g_local_file_read
;
2926 iface
->append_to
= g_local_file_append_to
;
2927 iface
->create
= g_local_file_create
;
2928 iface
->replace
= g_local_file_replace
;
2929 iface
->open_readwrite
= g_local_file_open_readwrite
;
2930 iface
->create_readwrite
= g_local_file_create_readwrite
;
2931 iface
->replace_readwrite
= g_local_file_replace_readwrite
;
2932 iface
->delete_file
= g_local_file_delete
;
2933 iface
->trash
= g_local_file_trash
;
2934 iface
->make_directory
= g_local_file_make_directory
;
2935 iface
->make_symbolic_link
= g_local_file_make_symbolic_link
;
2936 iface
->copy
= g_local_file_copy
;
2937 iface
->move
= g_local_file_move
;
2938 iface
->monitor_dir
= g_local_file_monitor_dir
;
2939 iface
->monitor_file
= g_local_file_monitor_file
;
2940 iface
->measure_disk_usage
= g_local_file_measure_disk_usage
;
2942 iface
->supports_thread_contexts
= TRUE
;