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, 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 "glocalfileinfo.h"
55 #include "glocalfileenumerator.h"
56 #include "glocalfileinputstream.h"
57 #include "glocalfileoutputstream.h"
58 #include "glocalfileiostream.h"
59 #include "glocaldirectorymonitor.h"
60 #include "glocalfilemonitor.h"
61 #include "gmountprivate.h"
62 #include "gunixmounts.h"
64 #include <glib/gstdio.h>
67 #include "glib-unix.h"
69 #include "glib-private.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)
91 static void g_local_file_file_iface_init (GFileIface
*iface
);
93 static GFileAttributeInfoList
*local_writable_attributes
= NULL
;
94 static /* GFileAttributeInfoList * */ gsize local_writable_namespaces
= 0;
98 GObject parent_instance
;
103 #define g_local_file_get_type _g_local_file_get_type
104 G_DEFINE_TYPE_WITH_CODE (GLocalFile
, g_local_file
, G_TYPE_OBJECT
,
105 G_IMPLEMENT_INTERFACE (G_TYPE_FILE
,
106 g_local_file_file_iface_init
))
108 static char *find_mountpoint_for (const char *file
, dev_t dev
);
111 g_local_file_finalize (GObject
*object
)
115 local
= G_LOCAL_FILE (object
);
117 g_free (local
->filename
);
119 G_OBJECT_CLASS (g_local_file_parent_class
)->finalize (object
);
123 g_local_file_class_init (GLocalFileClass
*klass
)
125 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
126 GFileAttributeInfoList
*list
;
128 gobject_class
->finalize
= g_local_file_finalize
;
130 /* Set up attribute lists */
132 /* Writable attributes: */
134 list
= g_file_attribute_info_list_new ();
136 g_file_attribute_info_list_add (list
,
137 G_FILE_ATTRIBUTE_UNIX_MODE
,
138 G_FILE_ATTRIBUTE_TYPE_UINT32
,
139 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE
|
140 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
143 g_file_attribute_info_list_add (list
,
144 G_FILE_ATTRIBUTE_UNIX_UID
,
145 G_FILE_ATTRIBUTE_TYPE_UINT32
,
146 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
147 g_file_attribute_info_list_add (list
,
148 G_FILE_ATTRIBUTE_UNIX_GID
,
149 G_FILE_ATTRIBUTE_TYPE_UINT32
,
150 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
154 g_file_attribute_info_list_add (list
,
155 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET
,
156 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING
,
161 g_file_attribute_info_list_add (list
,
162 G_FILE_ATTRIBUTE_TIME_MODIFIED
,
163 G_FILE_ATTRIBUTE_TYPE_UINT64
,
164 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE
|
165 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
166 g_file_attribute_info_list_add (list
,
167 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC
,
168 G_FILE_ATTRIBUTE_TYPE_UINT32
,
169 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE
|
170 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
171 /* When copying, the target file is accessed. Replicating
172 * the source access time does not make sense in this case.
174 g_file_attribute_info_list_add (list
,
175 G_FILE_ATTRIBUTE_TIME_ACCESS
,
176 G_FILE_ATTRIBUTE_TYPE_UINT64
,
177 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
178 g_file_attribute_info_list_add (list
,
179 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC
,
180 G_FILE_ATTRIBUTE_TYPE_UINT32
,
181 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
184 local_writable_attributes
= list
;
188 g_local_file_init (GLocalFile
*local
)
193 _g_local_file_get_filename (GLocalFile
*file
)
195 return file
->filename
;
199 canonicalize_filename (const char *filename
)
201 char *canon
, *start
, *p
, *q
;
205 if (!g_path_is_absolute (filename
))
207 cwd
= g_get_current_dir ();
208 canon
= g_build_filename (cwd
, filename
, NULL
);
212 canon
= g_strdup (filename
);
214 start
= (char *)g_path_skip_root (canon
);
218 /* This shouldn't really happen, as g_get_current_dir() should
219 return an absolute pathname, but bug 573843 shows this is
220 not always happening */
222 return g_build_filename (G_DIR_SEPARATOR_S
, filename
, NULL
);
225 /* POSIX allows double slashes at the start to
226 * mean something special (as does windows too).
227 * So, "//" != "/", but more than two slashes
233 G_IS_DIR_SEPARATOR (*p
);
240 memmove (start
, start
+i
, strlen (start
+i
)+1);
243 /* Make sure we're using the canonical dir separator */
245 while (p
< start
&& G_IS_DIR_SEPARATOR (*p
))
246 *p
++ = G_DIR_SEPARATOR
;
251 if (p
[0] == '.' && (p
[1] == 0 || G_IS_DIR_SEPARATOR (p
[1])))
253 memmove (p
, p
+1, strlen (p
+1)+1);
255 else if (p
[0] == '.' && p
[1] == '.' && (p
[2] == 0 || G_IS_DIR_SEPARATOR (p
[2])))
258 /* Skip previous separator */
262 while (p
> start
&& !G_IS_DIR_SEPARATOR (*p
))
264 if (G_IS_DIR_SEPARATOR (*p
))
265 *p
++ = G_DIR_SEPARATOR
;
266 memmove (p
, q
, strlen (q
)+1);
270 /* Skip until next separator */
271 while (*p
!= 0 && !G_IS_DIR_SEPARATOR (*p
))
276 /* Canonicalize one separator */
277 *p
++ = G_DIR_SEPARATOR
;
281 /* Remove additional separators */
283 while (*q
&& G_IS_DIR_SEPARATOR (*q
))
287 memmove (p
, q
, strlen (q
)+1);
290 /* Remove trailing slashes */
291 if (p
> start
&& G_IS_DIR_SEPARATOR (*(p
-1)))
298 _g_local_file_new (const char *filename
)
302 local
= g_object_new (G_TYPE_LOCAL_FILE
, NULL
);
303 local
->filename
= canonicalize_filename (filename
);
305 return G_FILE (local
);
309 g_local_file_is_native (GFile
*file
)
315 g_local_file_has_uri_scheme (GFile
*file
,
316 const char *uri_scheme
)
318 return g_ascii_strcasecmp (uri_scheme
, "file") == 0;
322 g_local_file_get_uri_scheme (GFile
*file
)
324 return g_strdup ("file");
328 g_local_file_get_basename (GFile
*file
)
330 return g_path_get_basename (G_LOCAL_FILE (file
)->filename
);
334 g_local_file_get_path (GFile
*file
)
336 return g_strdup (G_LOCAL_FILE (file
)->filename
);
340 g_local_file_get_uri (GFile
*file
)
342 return g_filename_to_uri (G_LOCAL_FILE (file
)->filename
, NULL
, NULL
);
346 get_filename_charset (const gchar
**filename_charset
)
348 const gchar
**charsets
;
351 is_utf8
= g_get_filename_charsets (&charsets
);
353 if (filename_charset
)
354 *filename_charset
= charsets
[0];
360 name_is_valid_for_display (const char *string
,
361 gboolean is_valid_utf8
)
365 if (!is_valid_utf8
&&
366 !g_utf8_validate (string
, -1, NULL
))
369 while ((c
= *string
++) != 0)
371 if (g_ascii_iscntrl (c
))
379 g_local_file_get_parse_name (GFile
*file
)
381 const char *filename
;
383 const gchar
*charset
;
385 char *roundtripped_filename
;
386 gboolean free_utf8_filename
;
387 gboolean is_valid_utf8
;
390 filename
= G_LOCAL_FILE (file
)->filename
;
391 if (get_filename_charset (&charset
))
393 utf8_filename
= (char *)filename
;
394 free_utf8_filename
= FALSE
;
395 is_valid_utf8
= FALSE
; /* Can't guarantee this */
399 utf8_filename
= g_convert (filename
, -1,
400 "UTF-8", charset
, NULL
, NULL
, NULL
);
401 free_utf8_filename
= TRUE
;
402 is_valid_utf8
= TRUE
;
404 if (utf8_filename
!= NULL
)
406 /* Make sure we can roundtrip: */
407 roundtripped_filename
= g_convert (utf8_filename
, -1,
408 charset
, "UTF-8", NULL
, NULL
, NULL
);
410 if (roundtripped_filename
== NULL
||
411 strcmp (filename
, roundtripped_filename
) != 0)
413 g_free (utf8_filename
);
414 utf8_filename
= NULL
;
417 g_free (roundtripped_filename
);
421 if (utf8_filename
!= NULL
&&
422 name_is_valid_for_display (utf8_filename
, is_valid_utf8
))
424 if (free_utf8_filename
)
425 parse_name
= utf8_filename
;
427 parse_name
= g_strdup (utf8_filename
);
432 char *dup_filename
, *p
, *backslash
;
434 /* Turn backslashes into forward slashes like
435 * g_filename_to_uri() would do (but we can't use that because
436 * it doesn't output IRIs).
438 dup_filename
= g_strdup (filename
);
439 filename
= p
= dup_filename
;
441 while ((backslash
= strchr (p
, '\\')) != NULL
)
448 escaped_path
= g_uri_escape_string (filename
,
449 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT
"/",
451 parse_name
= g_strconcat ("file://",
452 (*escaped_path
!= '/') ? "/" : "",
456 g_free (escaped_path
);
458 g_free (dup_filename
);
460 if (free_utf8_filename
)
461 g_free (utf8_filename
);
468 g_local_file_get_parent (GFile
*file
)
470 GLocalFile
*local
= G_LOCAL_FILE (file
);
471 const char *non_root
;
476 non_root
= g_path_skip_root (local
->filename
);
480 dirname
= g_path_get_dirname (local
->filename
);
481 parent
= _g_local_file_new (dirname
);
487 g_local_file_dup (GFile
*file
)
489 GLocalFile
*local
= G_LOCAL_FILE (file
);
491 return _g_local_file_new (local
->filename
);
495 g_local_file_hash (GFile
*file
)
497 GLocalFile
*local
= G_LOCAL_FILE (file
);
499 return g_str_hash (local
->filename
);
503 g_local_file_equal (GFile
*file1
,
506 GLocalFile
*local1
= G_LOCAL_FILE (file1
);
507 GLocalFile
*local2
= G_LOCAL_FILE (file2
);
509 return g_str_equal (local1
->filename
, local2
->filename
);
513 match_prefix (const char *path
,
518 prefix_len
= strlen (prefix
);
519 if (strncmp (path
, prefix
, prefix_len
) != 0)
522 /* Handle the case where prefix is the root, so that
523 * the IS_DIR_SEPRARATOR check below works */
524 if (prefix_len
> 0 &&
525 G_IS_DIR_SEPARATOR (prefix
[prefix_len
-1]))
528 return path
+ prefix_len
;
532 g_local_file_prefix_matches (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
);
540 if (remainder
!= NULL
&& G_IS_DIR_SEPARATOR (*remainder
))
546 g_local_file_get_relative_path (GFile
*parent
,
549 GLocalFile
*parent_local
= G_LOCAL_FILE (parent
);
550 GLocalFile
*descendant_local
= G_LOCAL_FILE (descendant
);
551 const char *remainder
;
553 remainder
= match_prefix (descendant_local
->filename
, parent_local
->filename
);
555 if (remainder
!= NULL
&& G_IS_DIR_SEPARATOR (*remainder
))
556 return g_strdup (remainder
+ 1);
561 g_local_file_resolve_relative_path (GFile
*file
,
562 const char *relative_path
)
564 GLocalFile
*local
= G_LOCAL_FILE (file
);
568 if (g_path_is_absolute (relative_path
))
569 return _g_local_file_new (relative_path
);
571 filename
= g_build_filename (local
->filename
, relative_path
, NULL
);
572 child
= _g_local_file_new (filename
);
578 static GFileEnumerator
*
579 g_local_file_enumerate_children (GFile
*file
,
580 const char *attributes
,
581 GFileQueryInfoFlags flags
,
582 GCancellable
*cancellable
,
585 GLocalFile
*local
= G_LOCAL_FILE (file
);
586 return _g_local_file_enumerator_new (local
,
592 g_local_file_get_child_for_display_name (GFile
*file
,
593 const char *display_name
,
599 basename
= g_filename_from_utf8 (display_name
, -1, NULL
, NULL
, NULL
);
600 if (basename
== NULL
)
602 g_set_error (error
, G_IO_ERROR
,
603 G_IO_ERROR_INVALID_FILENAME
,
604 _("Invalid filename %s"), display_name
);
608 new_file
= g_file_get_child (file
, basename
);
614 #if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
616 get_fs_type (long f_type
)
618 /* filesystem ids taken from linux manpage */
725 G_LOCK_DEFINE_STATIC(mount_info_hash
);
726 static GHashTable
*mount_info_hash
= NULL
;
727 static guint64 mount_info_hash_cache_time
= 0;
730 MOUNT_INFO_READONLY
= 1<<0
734 device_equal (gconstpointer v1
,
737 return *(dev_t
*)v1
== *(dev_t
*)v2
;
741 device_hash (gconstpointer v
)
743 return (guint
) *(dev_t
*)v
;
747 get_mount_info (GFileInfo
*fs_info
,
749 GFileAttributeMatcher
*matcher
)
753 gpointer info_as_ptr
;
757 GUnixMountEntry
*mount
;
760 if (g_lstat (path
, &buf
) != 0)
763 G_LOCK (mount_info_hash
);
765 if (mount_info_hash
== NULL
)
766 mount_info_hash
= g_hash_table_new_full (device_hash
, device_equal
,
770 if (g_unix_mounts_changed_since (mount_info_hash_cache_time
))
771 g_hash_table_remove_all (mount_info_hash
);
773 got_info
= g_hash_table_lookup_extended (mount_info_hash
,
778 G_UNLOCK (mount_info_hash
);
780 mount_info
= GPOINTER_TO_UINT (info_as_ptr
);
786 mountpoint
= find_mountpoint_for (path
, buf
.st_dev
);
787 if (mountpoint
== NULL
)
788 mountpoint
= g_strdup ("/");
790 mount
= g_unix_mount_at (mountpoint
, &cache_time
);
793 if (g_unix_mount_is_readonly (mount
))
794 mount_info
|= MOUNT_INFO_READONLY
;
796 g_unix_mount_free (mount
);
801 dev
= g_new0 (dev_t
, 1);
804 G_LOCK (mount_info_hash
);
805 mount_info_hash_cache_time
= cache_time
;
806 g_hash_table_insert (mount_info_hash
, dev
, GUINT_TO_POINTER (mount_info
));
807 G_UNLOCK (mount_info_hash
);
810 if (mount_info
& MOUNT_INFO_READONLY
)
811 g_file_info_set_attribute_boolean (fs_info
, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY
, TRUE
);
819 is_xp_or_later (void)
821 static int result
= -1;
826 OSVERSIONINFOEX ver_info
= {0};
827 DWORDLONG cond_mask
= 0;
828 int op
= VER_GREATER_EQUAL
;
830 ver_info
.dwOSVersionInfoSize
= sizeof ver_info
;
831 ver_info
.dwMajorVersion
= 5;
832 ver_info
.dwMinorVersion
= 1;
834 VER_SET_CONDITION (cond_mask
, VER_MAJORVERSION
, op
);
835 VER_SET_CONDITION (cond_mask
, VER_MINORVERSION
, op
);
837 result
= VerifyVersionInfo (&ver_info
,
838 VER_MAJORVERSION
| VER_MINORVERSION
,
841 result
= ((DWORD
)(LOBYTE (LOWORD (GetVersion ())))) >= 5;
849 get_volume_for_path (const char *path
)
855 wpath
= g_utf8_to_utf16 (path
, -1, NULL
, NULL
, NULL
);
856 result
= g_new (wchar_t, MAX_PATH
);
858 if (!GetVolumePathNameW (wpath
, result
, MAX_PATH
))
860 char *msg
= g_win32_error_message (GetLastError ());
861 g_critical ("GetVolumePathName failed: %s", msg
);
868 len
= wcslen (result
);
869 if (len
> 0 && result
[len
-1] != L
'\\')
871 result
= g_renew (wchar_t, result
, len
+ 2);
881 find_mountpoint_for (const char *file
, dev_t dev
)
886 wpath
= get_volume_for_path (file
);
890 utf8_path
= g_utf16_to_utf8 (wpath
, -1, NULL
, NULL
, NULL
);
897 get_filesystem_readonly (GFileInfo
*info
,
902 rootdir
= get_volume_for_path (path
);
906 if (is_xp_or_later ())
909 if (GetVolumeInformationW (rootdir
, NULL
, 0, NULL
, NULL
, &flags
, NULL
, 0))
910 g_file_info_set_attribute_boolean (info
, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY
,
911 (flags
& FILE_READ_ONLY_VOLUME
) != 0);
915 if (GetDriveTypeW (rootdir
) == DRIVE_CDROM
)
916 g_file_info_set_attribute_boolean (info
, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY
, TRUE
);
923 #endif /* G_OS_WIN32 */
926 g_local_file_query_filesystem_info (GFile
*file
,
927 const char *attributes
,
928 GCancellable
*cancellable
,
931 GLocalFile
*local
= G_LOCAL_FILE (file
);
933 int statfs_result
= 0;
939 struct statfs statfs_buffer
;
940 #elif defined(USE_STATVFS)
942 struct statvfs statfs_buffer
;
943 #endif /* USE_STATFS */
944 #endif /* G_OS_WIN32 */
945 GFileAttributeMatcher
*attribute_matcher
;
952 statfs_result
= statfs (local
->filename
, &statfs_buffer
);
953 #elif STATFS_ARGS == 4
954 statfs_result
= statfs (local
->filename
, &statfs_buffer
,
955 sizeof (statfs_buffer
), 0);
956 #endif /* STATFS_ARGS == 2 */
957 block_size
= statfs_buffer
.f_bsize
;
959 /* Many backends can't report free size (for instance the gvfs fuse
960 backend for backend not supporting this), and set f_bfree to 0,
961 but it can be 0 for real too. We treat the available == 0 and
962 free == 0 case as "both of these are invalid".
965 if (statfs_result
== 0 &&
966 statfs_buffer
.f_bavail
== 0 && statfs_buffer
.f_bfree
== 0)
968 #endif /* G_OS_WIN32 */
970 #elif defined(USE_STATVFS)
971 statfs_result
= statvfs (local
->filename
, &statfs_buffer
);
972 block_size
= statfs_buffer
.f_frsize
;
973 #endif /* USE_STATFS */
975 if (statfs_result
== -1)
979 g_set_error (error
, G_IO_ERROR
,
980 g_io_error_from_errno (errsv
),
981 _("Error getting filesystem info: %s"),
986 info
= g_file_info_new ();
988 attribute_matcher
= g_file_attribute_matcher_new (attributes
);
991 g_file_attribute_matcher_matches (attribute_matcher
,
992 G_FILE_ATTRIBUTE_FILESYSTEM_FREE
))
995 gchar
*localdir
= g_path_get_dirname (local
->filename
);
996 wchar_t *wdirname
= g_utf8_to_utf16 (localdir
, -1, NULL
, NULL
, NULL
);
1000 if (GetDiskFreeSpaceExW (wdirname
, &li
, NULL
, NULL
))
1001 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_FREE
, (guint64
)li
.QuadPart
);
1004 #if defined(USE_STATFS) || defined(USE_STATVFS)
1005 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_FREE
, block_size
* statfs_buffer
.f_bavail
);
1010 g_file_attribute_matcher_matches (attribute_matcher
,
1011 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
))
1014 gchar
*localdir
= g_path_get_dirname (local
->filename
);
1015 wchar_t *wdirname
= g_utf8_to_utf16 (localdir
, -1, NULL
, NULL
, NULL
);
1019 if (GetDiskFreeSpaceExW (wdirname
, NULL
, &li
, NULL
))
1020 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
, (guint64
)li
.QuadPart
);
1023 #if defined(USE_STATFS) || defined(USE_STATVFS)
1024 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
, block_size
* statfs_buffer
.f_blocks
);
1026 #endif /* G_OS_WIN32 */
1030 g_file_attribute_matcher_matches (attribute_matcher
,
1031 G_FILE_ATTRIBUTE_FILESYSTEM_USED
))
1034 gchar
*localdir
= g_path_get_dirname (local
->filename
);
1035 wchar_t *wdirname
= g_utf8_to_utf16 (localdir
, -1, NULL
, NULL
, NULL
);
1036 ULARGE_INTEGER li_free
;
1037 ULARGE_INTEGER li_total
;
1040 if (GetDiskFreeSpaceExW (wdirname
, &li_free
, &li_total
, NULL
))
1041 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_USED
, (guint64
)li_total
.QuadPart
- (guint64
)li_free
.QuadPart
);
1044 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_USED
, block_size
* (statfs_buffer
.f_blocks
- statfs_buffer
.f_bfree
));
1045 #endif /* G_OS_WIN32 */
1050 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1051 fstype
= g_strdup (statfs_buffer
.f_fstypename
);
1053 fstype
= get_fs_type (statfs_buffer
.f_type
);
1056 #elif defined(USE_STATVFS)
1057 #if defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
1058 fstype
= g_strdup (statfs_buffer
.f_fstypename
);
1059 #elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1060 fstype
= g_strdup (statfs_buffer
.f_basetype
);
1064 #endif /* USE_STATFS */
1067 g_file_attribute_matcher_matches (attribute_matcher
,
1068 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE
))
1069 g_file_info_set_attribute_string (info
, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE
, fstype
);
1070 #endif /* G_OS_WIN32 */
1072 if (g_file_attribute_matcher_matches (attribute_matcher
,
1073 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY
))
1076 get_filesystem_readonly (info
, local
->filename
);
1078 get_mount_info (info
, local
->filename
, attribute_matcher
);
1079 #endif /* G_OS_WIN32 */
1082 g_file_attribute_matcher_unref (attribute_matcher
);
1088 g_local_file_find_enclosing_mount (GFile
*file
,
1089 GCancellable
*cancellable
,
1092 GLocalFile
*local
= G_LOCAL_FILE (file
);
1097 if (g_lstat (local
->filename
, &buf
) != 0)
1099 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
,
1100 /* Translators: This is an error message when trying to
1101 * find the enclosing (user visible) mount of a file, but
1103 _("Containing mount does not exist"));
1107 mountpoint
= find_mountpoint_for (local
->filename
, buf
.st_dev
);
1108 if (mountpoint
== NULL
)
1110 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
,
1111 /* Translators: This is an error message when trying to
1112 * find the enclosing (user visible) mount of a file, but
1114 _("Containing mount does not exist"));
1118 mount
= _g_mount_get_for_mount_path (mountpoint
, cancellable
);
1119 g_free (mountpoint
);
1123 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
,
1124 /* Translators: This is an error message when trying to find
1125 * the enclosing (user visible) mount of a file, but none
1127 _("Containing mount does not exist"));
1132 g_local_file_set_display_name (GFile
*file
,
1133 const char *display_name
,
1134 GCancellable
*cancellable
,
1137 GLocalFile
*local
, *new_local
;
1138 GFile
*new_file
, *parent
;
1144 parent
= g_file_get_parent (file
);
1147 g_set_error_literal (error
, G_IO_ERROR
,
1149 _("Can't rename root directory"));
1153 new_file
= g_file_get_child_for_display_name (parent
, display_name
, error
);
1154 g_object_unref (parent
);
1156 if (new_file
== NULL
)
1158 local
= G_LOCAL_FILE (file
);
1159 new_local
= G_LOCAL_FILE (new_file
);
1161 if (g_lstat (new_local
->filename
, &statbuf
) == -1)
1165 if (errsv
!= ENOENT
)
1167 g_set_error (error
, G_IO_ERROR
,
1168 g_io_error_from_errno (errsv
),
1169 _("Error renaming file: %s"),
1170 g_strerror (errsv
));
1176 g_set_error_literal (error
, G_IO_ERROR
,
1178 _("Can't rename file, filename already exists"));
1182 if (g_rename (local
->filename
, new_local
->filename
) == -1)
1186 if (errsv
== EINVAL
)
1187 /* We can't get a rename file into itself error herer,
1188 so this must be an invalid filename, on e.g. FAT */
1189 g_set_error_literal (error
, G_IO_ERROR
,
1190 G_IO_ERROR_INVALID_FILENAME
,
1191 _("Invalid filename"));
1193 g_set_error (error
, G_IO_ERROR
,
1194 g_io_error_from_errno (errsv
),
1195 _("Error renaming file: %s"),
1196 g_strerror (errsv
));
1197 g_object_unref (new_file
);
1201 vfs
= g_vfs_get_default ();
1202 class = G_VFS_GET_CLASS (vfs
);
1203 if (class->local_file_moved
)
1204 class->local_file_moved (vfs
, local
->filename
, new_local
->filename
);
1210 g_local_file_query_info (GFile
*file
,
1211 const char *attributes
,
1212 GFileQueryInfoFlags flags
,
1213 GCancellable
*cancellable
,
1216 GLocalFile
*local
= G_LOCAL_FILE (file
);
1218 GFileAttributeMatcher
*matcher
;
1219 char *basename
, *dirname
;
1220 GLocalParentFileInfo parent_info
;
1222 matcher
= g_file_attribute_matcher_new (attributes
);
1224 basename
= g_path_get_basename (local
->filename
);
1226 dirname
= g_path_get_dirname (local
->filename
);
1227 _g_local_file_info_get_parent_info (dirname
, matcher
, &parent_info
);
1230 info
= _g_local_file_info_get (basename
, local
->filename
,
1231 matcher
, flags
, &parent_info
,
1235 _g_local_file_info_free_parent_info (&parent_info
);
1238 g_file_attribute_matcher_unref (matcher
);
1243 static GFileAttributeInfoList
*
1244 g_local_file_query_settable_attributes (GFile
*file
,
1245 GCancellable
*cancellable
,
1248 return g_file_attribute_info_list_ref (local_writable_attributes
);
1251 static GFileAttributeInfoList
*
1252 g_local_file_query_writable_namespaces (GFile
*file
,
1253 GCancellable
*cancellable
,
1256 GFileAttributeInfoList
*list
;
1260 if (g_once_init_enter (&local_writable_namespaces
))
1262 /* Writable namespaces: */
1264 list
= g_file_attribute_info_list_new ();
1267 g_file_attribute_info_list_add (list
,
1269 G_FILE_ATTRIBUTE_TYPE_STRING
,
1270 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE
|
1271 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
1272 g_file_attribute_info_list_add (list
,
1274 G_FILE_ATTRIBUTE_TYPE_STRING
,
1275 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
1278 vfs
= g_vfs_get_default ();
1279 class = G_VFS_GET_CLASS (vfs
);
1280 if (class->add_writable_namespaces
)
1281 class->add_writable_namespaces (vfs
, list
);
1283 g_once_init_leave (&local_writable_namespaces
, (gsize
)list
);
1285 list
= (GFileAttributeInfoList
*)local_writable_namespaces
;
1287 return g_file_attribute_info_list_ref (list
);
1291 g_local_file_set_attribute (GFile
*file
,
1292 const char *attribute
,
1293 GFileAttributeType type
,
1295 GFileQueryInfoFlags flags
,
1296 GCancellable
*cancellable
,
1299 GLocalFile
*local
= G_LOCAL_FILE (file
);
1301 return _g_local_file_info_set_attribute (local
->filename
,
1311 g_local_file_set_attributes_from_info (GFile
*file
,
1313 GFileQueryInfoFlags flags
,
1314 GCancellable
*cancellable
,
1317 GLocalFile
*local
= G_LOCAL_FILE (file
);
1318 int res
, chained_res
;
1319 GFileIface
*default_iface
;
1321 res
= _g_local_file_info_set_attributes (local
->filename
,
1327 error
= NULL
; /* Don't write over error if further errors */
1329 default_iface
= g_type_default_interface_peek (G_TYPE_FILE
);
1331 chained_res
= (default_iface
->set_attributes_from_info
) (file
, info
, flags
, cancellable
, error
);
1333 return res
&& chained_res
;
1336 static GFileInputStream
*
1337 g_local_file_read (GFile
*file
,
1338 GCancellable
*cancellable
,
1341 GLocalFile
*local
= G_LOCAL_FILE (file
);
1345 fd
= g_open (local
->filename
, O_RDONLY
|O_BINARY
, 0);
1351 if (errsv
== EACCES
)
1353 ret
= _stati64 (local
->filename
, &buf
);
1354 if (ret
== 0 && S_ISDIR (buf
.st_mode
))
1356 g_set_error_literal (error
, G_IO_ERROR
,
1357 G_IO_ERROR_IS_DIRECTORY
,
1358 _("Can't open directory"));
1364 g_set_error (error
, G_IO_ERROR
,
1365 g_io_error_from_errno (errsv
),
1366 _("Error opening file: %s"),
1367 g_strerror (errsv
));
1372 ret
= _fstati64 (fd
, &buf
);
1374 ret
= fstat (fd
, &buf
);
1377 if (ret
== 0 && S_ISDIR (buf
.st_mode
))
1379 (void) g_close (fd
, NULL
);
1380 g_set_error_literal (error
, G_IO_ERROR
,
1381 G_IO_ERROR_IS_DIRECTORY
,
1382 _("Can't open directory"));
1386 return _g_local_file_input_stream_new (fd
);
1389 static GFileOutputStream
*
1390 g_local_file_append_to (GFile
*file
,
1391 GFileCreateFlags flags
,
1392 GCancellable
*cancellable
,
1395 return _g_local_file_output_stream_append (G_LOCAL_FILE (file
)->filename
,
1396 flags
, cancellable
, error
);
1399 static GFileOutputStream
*
1400 g_local_file_create (GFile
*file
,
1401 GFileCreateFlags flags
,
1402 GCancellable
*cancellable
,
1405 return _g_local_file_output_stream_create (G_LOCAL_FILE (file
)->filename
,
1407 cancellable
, error
);
1410 static GFileOutputStream
*
1411 g_local_file_replace (GFile
*file
,
1413 gboolean make_backup
,
1414 GFileCreateFlags flags
,
1415 GCancellable
*cancellable
,
1418 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file
)->filename
,
1420 etag
, make_backup
, flags
, NULL
,
1421 cancellable
, error
);
1424 static GFileIOStream
*
1425 g_local_file_open_readwrite (GFile
*file
,
1426 GCancellable
*cancellable
,
1429 GFileOutputStream
*output
;
1432 output
= _g_local_file_output_stream_open (G_LOCAL_FILE (file
)->filename
,
1434 cancellable
, error
);
1438 res
= _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output
));
1439 g_object_unref (output
);
1443 static GFileIOStream
*
1444 g_local_file_create_readwrite (GFile
*file
,
1445 GFileCreateFlags flags
,
1446 GCancellable
*cancellable
,
1449 GFileOutputStream
*output
;
1452 output
= _g_local_file_output_stream_create (G_LOCAL_FILE (file
)->filename
,
1454 cancellable
, error
);
1458 res
= _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output
));
1459 g_object_unref (output
);
1463 static GFileIOStream
*
1464 g_local_file_replace_readwrite (GFile
*file
,
1466 gboolean make_backup
,
1467 GFileCreateFlags flags
,
1468 GCancellable
*cancellable
,
1471 GFileOutputStream
*output
;
1474 output
= _g_local_file_output_stream_replace (G_LOCAL_FILE (file
)->filename
,
1476 etag
, make_backup
, flags
, NULL
,
1477 cancellable
, error
);
1481 res
= _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output
));
1482 g_object_unref (output
);
1487 g_local_file_delete (GFile
*file
,
1488 GCancellable
*cancellable
,
1491 GLocalFile
*local
= G_LOCAL_FILE (file
);
1495 if (g_remove (local
->filename
) == -1)
1499 /* Posix allows EEXIST too, but the more sane error
1500 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1502 if (errsv
== EEXIST
)
1505 g_set_error (error
, G_IO_ERROR
,
1506 g_io_error_from_errno (errsv
),
1507 _("Error removing file: %s"),
1508 g_strerror (errsv
));
1512 vfs
= g_vfs_get_default ();
1513 class = G_VFS_GET_CLASS (vfs
);
1514 if (class->local_file_removed
)
1515 class->local_file_removed (vfs
, local
->filename
);
1523 strip_trailing_slashes (const char *path
)
1528 path_copy
= g_strdup (path
);
1529 len
= strlen (path_copy
);
1530 while (len
> 1 && path_copy
[len
-1] == '/')
1531 path_copy
[--len
] = 0;
1537 expand_symlink (const char *link
)
1539 char *resolved
, *canonical
, *parent
, *link2
;
1540 char symlink_value
[4096];
1548 res
= readlink (link
, symlink_value
, sizeof (symlink_value
) - 1);
1551 return g_strdup (link
);
1552 symlink_value
[res
] = 0;
1555 if (g_path_is_absolute (symlink_value
))
1556 return canonicalize_filename (symlink_value
);
1559 link2
= strip_trailing_slashes (link
);
1560 parent
= g_path_get_dirname (link2
);
1563 resolved
= g_build_filename (parent
, symlink_value
, NULL
);
1566 canonical
= canonicalize_filename (resolved
);
1575 get_parent (const char *path
,
1579 GStatBuf parent_stat
;
1583 path_copy
= strip_trailing_slashes (path
);
1585 parent
= g_path_get_dirname (path_copy
);
1586 if (strcmp (parent
, ".") == 0 ||
1587 strcmp (parent
, path_copy
) == 0)
1597 if (g_lstat (parent
, &parent_stat
) != 0)
1603 if (S_ISLNK (parent_stat
.st_mode
))
1606 parent
= expand_symlink (parent
);
1611 if (num_recursions
> 12)
1616 } while (S_ISLNK (parent_stat
.st_mode
));
1618 *parent_dev
= parent_stat
.st_dev
;
1624 expand_all_symlinks (const char *path
)
1626 char *parent
, *parent_expanded
;
1627 char *basename
, *res
;
1630 parent
= get_parent (path
, &parent_dev
);
1633 parent_expanded
= expand_all_symlinks (parent
);
1635 basename
= g_path_get_basename (path
);
1636 res
= g_build_filename (parent_expanded
, basename
, NULL
);
1638 g_free (parent_expanded
);
1641 res
= g_strdup (path
);
1647 find_mountpoint_for (const char *file
,
1651 dev_t dir_dev
, parent_dev
;
1653 dir
= g_strdup (file
);
1658 parent
= get_parent (dir
, &parent_dev
);
1662 if (parent_dev
!= dir_dev
)
1674 find_topdir_for (const char *file
)
1679 dir
= get_parent (file
, &dir_dev
);
1683 return find_mountpoint_for (dir
, dir_dev
);
1687 get_unique_filename (const char *basename
,
1693 return g_strdup (basename
);
1695 dot
= strchr (basename
, '.');
1697 return g_strdup_printf ("%.*s.%d%s", (int)(dot
- basename
), basename
, id
, dot
);
1699 return g_strdup_printf ("%s.%d", basename
, id
);
1703 path_has_prefix (const char *path
,
1711 prefix_len
= strlen (prefix
);
1713 if (strncmp (path
, prefix
, prefix_len
) == 0 &&
1714 (prefix_len
== 0 || /* empty prefix always matches */
1715 prefix
[prefix_len
- 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1716 path
[prefix_len
] == 0 ||
1717 path
[prefix_len
] == '/'))
1724 try_make_relative (const char *path
,
1727 char *path2
, *base2
;
1730 path2
= expand_all_symlinks (path
);
1731 base2
= expand_all_symlinks (base
);
1734 if (path_has_prefix (path2
, base2
))
1736 relative
= path2
+ strlen (base2
);
1737 while (*relative
== '/')
1739 relative
= g_strdup (relative
);
1747 /* Failed, use abs path */
1748 return g_strdup (path
);
1752 _g_local_file_has_trash_dir (const char *dirname
, dev_t dir_dev
)
1754 static gsize home_dev_set
= 0;
1755 static dev_t home_dev
;
1756 char *topdir
, *globaldir
, *trashdir
, *tmpname
;
1759 GStatBuf global_stat
, trash_stat
;
1762 if (g_once_init_enter (&home_dev_set
))
1766 g_stat (g_get_home_dir (), &home_stat
);
1767 home_dev
= home_stat
.st_dev
;
1768 g_once_init_leave (&home_dev_set
, 1);
1771 /* Assume we can trash to the home */
1772 if (dir_dev
== home_dev
)
1775 topdir
= find_mountpoint_for (dirname
, dir_dev
);
1779 globaldir
= g_build_filename (topdir
, ".Trash", NULL
);
1780 if (g_lstat (globaldir
, &global_stat
) == 0 &&
1781 S_ISDIR (global_stat
.st_mode
) &&
1782 (global_stat
.st_mode
& S_ISVTX
) != 0)
1784 /* got a toplevel sysadmin created dir, assume we
1785 * can trash to it (we should be able to create a dir)
1786 * This fails for the FAT case where the ownership of
1787 * that dir would be wrong though..
1795 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1797 g_snprintf (uid_str
, sizeof (uid_str
), "%lu", (unsigned long) uid
);
1799 tmpname
= g_strdup_printf (".Trash-%s", uid_str
);
1800 trashdir
= g_build_filename (topdir
, tmpname
, NULL
);
1803 if (g_lstat (trashdir
, &trash_stat
) == 0)
1807 return S_ISDIR (trash_stat
.st_mode
) &&
1808 trash_stat
.st_uid
== uid
;
1812 /* User specific trash didn't exist, can we create it? */
1813 res
= g_access (topdir
, W_OK
) == 0;
1821 _g_local_file_is_lost_found_dir (const char *path
, dev_t path_dev
)
1823 gboolean ret
= FALSE
;
1824 gchar
*mount_dir
= NULL
;
1825 size_t mount_dir_len
;
1828 if (!g_str_has_suffix (path
, "/lost+found"))
1831 mount_dir
= find_mountpoint_for (path
, path_dev
);
1832 if (mount_dir
== NULL
)
1835 mount_dir_len
= strlen (mount_dir
);
1836 /* We special-case rootfs ('/') since it's the only case where
1837 * mount_dir ends in '/'
1839 if (mount_dir_len
== 1)
1841 if (mount_dir_len
+ strlen ("/lost+found") != strlen (path
))
1844 if (g_lstat (path
, &statbuf
) != 0)
1847 if (!(S_ISDIR (statbuf
.st_mode
) &&
1848 statbuf
.st_uid
== 0 &&
1849 statbuf
.st_gid
== 0))
1861 g_local_file_trash (GFile
*file
,
1862 GCancellable
*cancellable
,
1865 GLocalFile
*local
= G_LOCAL_FILE (file
);
1866 GStatBuf file_stat
, home_stat
;
1867 const char *homedir
;
1868 char *trashdir
, *topdir
, *infodir
, *filesdir
;
1869 char *basename
, *trashname
, *trashfile
, *infoname
, *infofile
;
1870 char *original_name
, *original_name_escaped
;
1873 gboolean is_homedir_trash
;
1874 char delete_time
[32];
1876 GStatBuf trash_stat
, global_stat
;
1877 char *dirname
, *globaldir
;
1881 if (g_lstat (local
->filename
, &file_stat
) != 0)
1885 g_set_error (error
, G_IO_ERROR
,
1886 g_io_error_from_errno (errsv
),
1887 _("Error trashing file: %s"),
1888 g_strerror (errsv
));
1892 homedir
= g_get_home_dir ();
1893 g_stat (homedir
, &home_stat
);
1895 is_homedir_trash
= FALSE
;
1897 if (file_stat
.st_dev
== home_stat
.st_dev
)
1899 is_homedir_trash
= TRUE
;
1901 trashdir
= g_build_filename (g_get_user_data_dir (), "Trash", NULL
);
1902 if (g_mkdir_with_parents (trashdir
, 0700) < 0)
1907 display_name
= g_filename_display_name (trashdir
);
1908 g_set_error (error
, G_IO_ERROR
,
1909 g_io_error_from_errno (errsv
),
1910 _("Unable to create trash dir %s: %s"),
1911 display_name
, g_strerror (errsv
));
1912 g_free (display_name
);
1916 topdir
= g_strdup (g_get_user_data_dir ());
1924 g_snprintf (uid_str
, sizeof (uid_str
), "%lu", (unsigned long)uid
);
1926 topdir
= find_topdir_for (local
->filename
);
1929 g_set_error_literal (error
, G_IO_ERROR
,
1930 G_IO_ERROR_NOT_SUPPORTED
,
1931 _("Unable to find toplevel directory for trash"));
1935 /* Try looking for global trash dir $topdir/.Trash/$uid */
1936 globaldir
= g_build_filename (topdir
, ".Trash", NULL
);
1937 if (g_lstat (globaldir
, &global_stat
) == 0 &&
1938 S_ISDIR (global_stat
.st_mode
) &&
1939 (global_stat
.st_mode
& S_ISVTX
) != 0)
1941 trashdir
= g_build_filename (globaldir
, uid_str
, NULL
);
1943 if (g_lstat (trashdir
, &trash_stat
) == 0)
1945 if (!S_ISDIR (trash_stat
.st_mode
) ||
1946 trash_stat
.st_uid
!= uid
)
1948 /* Not a directory or not owned by user, ignore */
1953 else if (g_mkdir (trashdir
, 0700) == -1)
1961 if (trashdir
== NULL
)
1963 gboolean tried_create
;
1965 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1966 dirname
= g_strdup_printf (".Trash-%s", uid_str
);
1967 trashdir
= g_build_filename (topdir
, dirname
, NULL
);
1970 tried_create
= FALSE
;
1973 if (g_lstat (trashdir
, &trash_stat
) == 0)
1975 if (!S_ISDIR (trash_stat
.st_mode
) ||
1976 trash_stat
.st_uid
!= uid
)
1978 /* Remove the failed directory */
1980 g_remove (trashdir
);
1982 /* Not a directory or not owned by user, ignore */
1989 if (!tried_create
&&
1990 g_mkdir (trashdir
, 0700) != -1)
1992 /* Ensure that the created dir has the right uid etc.
1993 This might fail on e.g. a FAT dir */
1994 tried_create
= TRUE
;
2005 if (trashdir
== NULL
)
2008 g_set_error_literal (error
, G_IO_ERROR
,
2009 G_IO_ERROR_NOT_SUPPORTED
,
2010 _("Unable to find or create trash directory"));
2015 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
2017 infodir
= g_build_filename (trashdir
, "info", NULL
);
2018 filesdir
= g_build_filename (trashdir
, "files", NULL
);
2021 /* Make sure we have the subdirectories */
2022 if ((g_mkdir (infodir
, 0700) == -1 && errno
!= EEXIST
) ||
2023 (g_mkdir (filesdir
, 0700) == -1 && errno
!= EEXIST
))
2028 g_set_error_literal (error
, G_IO_ERROR
,
2029 G_IO_ERROR_NOT_SUPPORTED
,
2030 _("Unable to find or create trash directory"));
2034 basename
= g_path_get_basename (local
->filename
);
2042 trashname
= get_unique_filename (basename
, i
++);
2043 infoname
= g_strconcat (trashname
, ".trashinfo", NULL
);
2044 infofile
= g_build_filename (infodir
, infoname
, NULL
);
2047 fd
= g_open (infofile
, O_CREAT
| O_EXCL
, 0666);
2048 } while (fd
== -1 && errno
== EEXIST
);
2062 g_set_error (error
, G_IO_ERROR
,
2063 g_io_error_from_errno (errsv
),
2064 _("Unable to create trashing info file: %s"),
2065 g_strerror (errsv
));
2069 (void) g_close (fd
, NULL
);
2071 /* TODO: Maybe we should verify that you can delete the file from the trash
2072 before moving it? OTOH, that is hard, as it needs a recursive scan */
2074 trashfile
= g_build_filename (filesdir
, trashname
, NULL
);
2078 if (g_rename (local
->filename
, trashfile
) == -1)
2082 g_unlink (infofile
);
2090 /* The trash dir was actually on another fs anyway!?
2091 This can happen when the same device is mounted multiple
2092 times, or with bind mounts of the same fs. */
2093 g_set_error (error
, G_IO_ERROR
,
2094 G_IO_ERROR_NOT_SUPPORTED
,
2095 _("Unable to trash file: %s"),
2096 g_strerror (errsv
));
2098 g_set_error (error
, G_IO_ERROR
,
2099 g_io_error_from_errno (errsv
),
2100 _("Unable to trash file: %s"),
2101 g_strerror (errsv
));
2105 vfs
= g_vfs_get_default ();
2106 class = G_VFS_GET_CLASS (vfs
);
2107 if (class->local_file_moved
)
2108 class->local_file_moved (vfs
, local
->filename
, trashfile
);
2112 /* TODO: Do we need to update mtime/atime here after the move? */
2114 /* Use absolute names for homedir */
2115 if (is_homedir_trash
)
2116 original_name
= g_strdup (local
->filename
);
2118 original_name
= try_make_relative (local
->filename
, topdir
);
2119 original_name_escaped
= g_uri_escape_string (original_name
, "/", FALSE
);
2121 g_free (original_name
);
2128 localtime_r (&t
, &now
);
2130 strftime(delete_time
, sizeof (delete_time
), "%Y-%m-%dT%H:%M:%S", &now
);
2133 data
= g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
2134 original_name_escaped
, delete_time
);
2136 g_file_set_contents (infofile
, data
, -1, NULL
);
2140 g_free (original_name_escaped
);
2145 #else /* G_OS_WIN32 */
2147 _g_local_file_has_trash_dir (const char *dirname
, dev_t dir_dev
)
2149 return FALSE
; /* XXX ??? */
2153 g_local_file_trash (GFile
*file
,
2154 GCancellable
*cancellable
,
2157 GLocalFile
*local
= G_LOCAL_FILE (file
);
2158 SHFILEOPSTRUCTW op
= {0};
2163 wfilename
= g_utf8_to_utf16 (local
->filename
, -1, NULL
, &len
, NULL
);
2164 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
2165 wfilename
= g_renew (wchar_t, wfilename
, len
+ 2);
2166 wfilename
[len
+ 1] = 0;
2168 op
.wFunc
= FO_DELETE
;
2169 op
.pFrom
= wfilename
;
2170 op
.fFlags
= FOF_ALLOWUNDO
;
2172 success
= SHFileOperationW (&op
) == 0;
2174 if (success
&& op
.fAnyOperationsAborted
)
2176 if (cancellable
&& !g_cancellable_is_cancelled (cancellable
))
2177 g_cancellable_cancel (cancellable
);
2178 g_set_error (error
, G_IO_ERROR
,
2179 G_IO_ERROR_CANCELLED
,
2180 _("Unable to trash file: %s"),
2181 _("Operation was cancelled"));
2185 g_set_error (error
, G_IO_ERROR
,
2187 _("Unable to trash file: %s"),
2188 _("internal error"));
2193 #endif /* G_OS_WIN32 */
2196 g_local_file_make_directory (GFile
*file
,
2197 GCancellable
*cancellable
,
2200 GLocalFile
*local
= G_LOCAL_FILE (file
);
2202 if (g_mkdir (local
->filename
, 0777) == -1)
2206 if (errsv
== EINVAL
)
2207 /* This must be an invalid filename, on e.g. FAT */
2208 g_set_error_literal (error
, G_IO_ERROR
,
2209 G_IO_ERROR_INVALID_FILENAME
,
2210 _("Invalid filename"));
2212 g_set_error (error
, G_IO_ERROR
,
2213 g_io_error_from_errno (errsv
),
2214 _("Error creating directory: %s"),
2215 g_strerror (errsv
));
2223 g_local_file_make_symbolic_link (GFile
*file
,
2224 const char *symlink_value
,
2225 GCancellable
*cancellable
,
2229 GLocalFile
*local
= G_LOCAL_FILE (file
);
2231 if (symlink (symlink_value
, local
->filename
) == -1)
2235 if (errsv
== EINVAL
)
2236 /* This must be an invalid filename, on e.g. FAT */
2237 g_set_error_literal (error
, G_IO_ERROR
,
2238 G_IO_ERROR_INVALID_FILENAME
,
2239 _("Invalid filename"));
2240 else if (errsv
== EPERM
)
2241 g_set_error (error
, G_IO_ERROR
,
2242 G_IO_ERROR_NOT_SUPPORTED
,
2243 _("Filesystem does not support symbolic links"));
2245 g_set_error (error
, G_IO_ERROR
,
2246 g_io_error_from_errno (errsv
),
2247 _("Error making symbolic link: %s"),
2248 g_strerror (errsv
));
2253 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
, "Symlinks not supported");
2260 g_local_file_copy (GFile
*source
,
2262 GFileCopyFlags flags
,
2263 GCancellable
*cancellable
,
2264 GFileProgressCallback progress_callback
,
2265 gpointer progress_callback_data
,
2268 /* Fall back to default copy */
2269 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
, "Copy not supported");
2274 g_local_file_move (GFile
*source
,
2276 GFileCopyFlags flags
,
2277 GCancellable
*cancellable
,
2278 GFileProgressCallback progress_callback
,
2279 gpointer progress_callback_data
,
2282 GLocalFile
*local_source
, *local_destination
;
2284 gboolean destination_exist
, source_is_dir
;
2291 if (!G_IS_LOCAL_FILE (source
) ||
2292 !G_IS_LOCAL_FILE (destination
))
2294 /* Fall back to default move */
2295 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
, "Move not supported");
2299 local_source
= G_LOCAL_FILE (source
);
2300 local_destination
= G_LOCAL_FILE (destination
);
2302 res
= g_lstat (local_source
->filename
, &statbuf
);
2307 g_set_error (error
, G_IO_ERROR
,
2308 g_io_error_from_errno (errsv
),
2309 _("Error moving file: %s"),
2310 g_strerror (errsv
));
2314 source_is_dir
= S_ISDIR (statbuf
.st_mode
);
2315 source_size
= statbuf
.st_size
;
2317 destination_exist
= FALSE
;
2318 res
= g_lstat (local_destination
->filename
, &statbuf
);
2321 destination_exist
= TRUE
; /* Target file exists */
2323 if (flags
& G_FILE_COPY_OVERWRITE
)
2325 /* Always fail on dirs, even with overwrite */
2326 if (S_ISDIR (statbuf
.st_mode
))
2329 g_set_error_literal (error
,
2331 G_IO_ERROR_WOULD_MERGE
,
2332 _("Can't move directory over directory"));
2334 g_set_error_literal (error
,
2336 G_IO_ERROR_IS_DIRECTORY
,
2337 _("Can't copy over directory"));
2343 g_set_error_literal (error
,
2346 _("Target file exists"));
2351 if (flags
& G_FILE_COPY_BACKUP
&& destination_exist
)
2353 backup_name
= g_strconcat (local_destination
->filename
, "~", NULL
);
2354 if (g_rename (local_destination
->filename
, backup_name
) == -1)
2356 g_set_error_literal (error
,
2358 G_IO_ERROR_CANT_CREATE_BACKUP
,
2359 _("Backup file creation failed"));
2360 g_free (backup_name
);
2363 g_free (backup_name
);
2364 destination_exist
= FALSE
; /* It did, but no more */
2367 if (source_is_dir
&& destination_exist
&& (flags
& G_FILE_COPY_OVERWRITE
))
2369 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2370 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2371 res
= g_unlink (local_destination
->filename
);
2376 g_set_error (error
, G_IO_ERROR
,
2377 g_io_error_from_errno (errsv
),
2378 _("Error removing target file: %s"),
2379 g_strerror (errsv
));
2384 if (g_rename (local_source
->filename
, local_destination
->filename
) == -1)
2389 /* This will cause the fallback code to run */
2390 g_set_error_literal (error
, G_IO_ERROR
,
2391 G_IO_ERROR_NOT_SUPPORTED
,
2392 _("Move between mounts not supported"));
2393 else if (errsv
== EINVAL
)
2394 /* This must be an invalid filename, on e.g. FAT, or
2395 we're trying to move the file into itself...
2396 We return invalid filename for both... */
2397 g_set_error_literal (error
, G_IO_ERROR
,
2398 G_IO_ERROR_INVALID_FILENAME
,
2399 _("Invalid filename"));
2401 g_set_error (error
, G_IO_ERROR
,
2402 g_io_error_from_errno (errsv
),
2403 _("Error moving file: %s"),
2404 g_strerror (errsv
));
2408 vfs
= g_vfs_get_default ();
2409 class = G_VFS_GET_CLASS (vfs
);
2410 if (class->local_file_moved
)
2411 class->local_file_moved (vfs
, local_source
->filename
, local_destination
->filename
);
2413 /* Make sure we send full copied size */
2414 if (progress_callback
)
2415 progress_callback (source_size
, source_size
, progress_callback_data
);
2423 is_remote (const gchar
*filename
)
2431 is_remote_fs (const gchar
*filename
)
2433 const char *fsname
= NULL
;
2436 struct statfs statfs_buffer
;
2437 int statfs_result
= 0;
2439 #if STATFS_ARGS == 2
2440 statfs_result
= statfs (filename
, &statfs_buffer
);
2441 #elif STATFS_ARGS == 4
2442 statfs_result
= statfs (filename
, &statfs_buffer
, sizeof (statfs_buffer
), 0);
2445 #elif defined(USE_STATVFS)
2446 struct statvfs statfs_buffer
;
2447 int statfs_result
= 0;
2449 statfs_result
= statvfs (filename
, &statfs_buffer
);
2454 if (statfs_result
== -1)
2458 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
2459 fsname
= statfs_buffer
.f_fstypename
;
2461 fsname
= get_fs_type (statfs_buffer
.f_type
);
2464 #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
2465 fsname
= statfs_buffer
.f_basetype
;
2470 if (strcmp (fsname
, "nfs") == 0)
2472 if (strcmp (fsname
, "nfs4") == 0)
2480 is_remote (const gchar
*filename
)
2482 static gboolean remote_home
;
2483 static gsize initialized
;
2486 home
= g_get_home_dir ();
2487 if (path_has_prefix (filename
, home
))
2489 if (g_once_init_enter (&initialized
))
2491 remote_home
= is_remote_fs (home
);
2492 g_once_init_leave (&initialized
, TRUE
);
2499 #endif /* !G_OS_WIN32 */
2501 static GFileMonitor
*
2502 g_local_file_monitor_dir (GFile
*file
,
2503 GFileMonitorFlags flags
,
2504 GCancellable
*cancellable
,
2507 GLocalFile
* local_file
= G_LOCAL_FILE(file
);
2508 return _g_local_directory_monitor_new (local_file
->filename
, flags
, NULL
, is_remote (local_file
->filename
), TRUE
, error
);
2511 static GFileMonitor
*
2512 g_local_file_monitor_file (GFile
*file
,
2513 GFileMonitorFlags flags
,
2514 GCancellable
*cancellable
,
2517 GLocalFile
* local_file
= G_LOCAL_FILE(file
);
2518 return _g_local_file_monitor_new (local_file
->filename
, flags
, NULL
, is_remote (local_file
->filename
), TRUE
, error
);
2521 GLocalDirectoryMonitor
*
2522 g_local_directory_monitor_new_in_worker (const char *pathname
,
2523 GFileMonitorFlags flags
,
2526 return (gpointer
) _g_local_directory_monitor_new (pathname
, flags
,
2527 GLIB_PRIVATE_CALL (g_get_worker_context
) (),
2528 is_remote (pathname
), FALSE
, error
);
2532 g_local_file_monitor_new_in_worker (const char *pathname
,
2533 GFileMonitorFlags flags
,
2536 return (gpointer
) _g_local_file_monitor_new (pathname
, flags
,
2537 GLIB_PRIVATE_CALL (g_get_worker_context
) (),
2538 is_remote (pathname
), FALSE
, error
);
2542 /* Here is the GLocalFile implementation of g_file_measure_disk_usage().
2544 * If available, we use fopenat() in preference to filenames for
2545 * efficiency and safety reasons. We know that fopenat() is available
2546 * based on if AT_FDCWD is defined. POSIX guarantees that this will be
2547 * defined as a macro.
2549 * We use a linked list of stack-allocated GSList nodes in order to be
2550 * able to reconstruct the filename for error messages. We actually
2551 * pass the filename to operate on through the top node of the list.
2553 * In case we're using openat(), this top filename will be a basename
2554 * which should be opened in the directory which has also had its fd
2555 * passed along. If we're not using openat() then it will be a full
2556 * absolute filename.
2560 g_local_file_measure_size_error (GFileMeasureFlags flags
,
2565 /* Only report an error if we were at the toplevel or if the caller
2566 * requested reporting of all errors.
2568 if ((name
->next
== NULL
) || (flags
& G_FILE_MEASURE_REPORT_ANY_ERROR
))
2573 /* Skip some work if there is no error return */
2578 /* If using openat() we need to rebuild the filename for the message */
2579 filename
= g_string_new (name
->data
);
2580 for (node
= name
->next
; node
; node
= node
->next
)
2584 g_string_prepend_c (filename
, G_DIR_SEPARATOR
);
2585 utf8
= g_filename_display_name (node
->data
);
2586 g_string_prepend (filename
, utf8
);
2593 /* Otherwise, we already have it, so just use it. */
2595 filename
= g_string_new (NULL
);
2596 utf8
= g_filename_display_name (node
->data
);
2597 g_string_append (filename
, utf8
);
2602 g_set_error (error
, G_IO_ERROR
, g_io_error_from_errno (saved_errno
),
2603 _("Could not determine the disk usage of %s: %s"),
2604 filename
->str
, g_strerror (saved_errno
));
2606 g_string_free (filename
, TRUE
);
2612 /* We're not reporting this error... */
2618 GFileMeasureFlags flags
;
2620 GCancellable
*cancellable
;
2622 GFileMeasureProgressCallback progress_callback
;
2623 gpointer progress_data
;
2629 guint64 last_progress_report
;
2633 g_local_file_measure_size_of_contents (gint fd
,
2635 MeasureState
*state
,
2639 g_local_file_measure_size_of_file (gint parent_fd
,
2641 MeasureState
*state
,
2646 if (g_cancellable_set_error_if_cancelled (state
->cancellable
, error
))
2649 #if defined (AT_FDCWD)
2650 if (fstatat (parent_fd
, name
->data
, &buf
, AT_SYMLINK_NOFOLLOW
) != 0)
2652 if (g_lstat (name
->data
, &buf
) != 0)
2654 return g_local_file_measure_size_error (state
->flags
, errno
, name
, error
);
2658 /* If not at the toplevel, check for a device boundary. */
2660 if (state
->flags
& G_FILE_MEASURE_NO_XDEV
)
2661 if (state
->contained_on
!= buf
.st_dev
)
2666 /* If, however, this is the toplevel, set the device number so
2667 * that recursive invocations can compare against it.
2669 state
->contained_on
= buf
.st_dev
;
2672 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
2673 if (~state
->flags
& G_FILE_MEASURE_APPARENT_SIZE
)
2674 state
->disk_usage
+= buf
.st_blocks
* G_GUINT64_CONSTANT (512);
2677 state
->disk_usage
+= buf
.st_size
;
2679 if (S_ISDIR (buf
.st_mode
))
2684 if (state
->progress_callback
)
2686 /* We could attempt to do some cleverness here in order to avoid
2687 * calling clock_gettime() so much, but we're doing stats and opens
2688 * all over the place already...
2690 if (state
->last_progress_report
)
2694 now
= g_get_monotonic_time ();
2696 if (state
->last_progress_report
+ 200 * G_TIME_SPAN_MILLISECOND
< now
)
2698 (* state
->progress_callback
) (TRUE
,
2699 state
->disk_usage
, state
->num_dirs
, state
->num_files
,
2700 state
->progress_data
);
2701 state
->last_progress_report
= now
;
2706 /* We must do an initial report to inform that more reports
2709 (* state
->progress_callback
) (TRUE
, 0, 0, 0, state
->progress_data
);
2710 state
->last_progress_report
= g_get_monotonic_time ();
2714 if (S_ISDIR (buf
.st_mode
))
2718 if (g_cancellable_set_error_if_cancelled (state
->cancellable
, error
))
2722 #ifdef HAVE_OPEN_O_DIRECTORY
2723 dir_fd
= openat (parent_fd
, name
->data
, O_RDONLY
|O_DIRECTORY
);
2725 dir_fd
= openat (parent_fd
, name
->data
, O_RDONLY
);
2728 return g_local_file_measure_size_error (state
->flags
, errno
, name
, error
);
2731 if (!g_local_file_measure_size_of_contents (dir_fd
, name
, state
, error
))
2739 g_local_file_measure_size_of_contents (gint fd
,
2741 MeasureState
*state
,
2744 gboolean success
= TRUE
;
2750 /* If this fails, we want to preserve the errno from fopendir() */
2752 dirp
= fdopendir (fd
);
2753 dir
= dirp
? GLIB_PRIVATE_CALL(g_dir_new_from_dirp
) (dirp
) : NULL
;
2756 dir
= GLIB_PRIVATE_CALL(g_dir_open_with_errno
) (dir_name
->data
, 0);
2761 gint saved_errno
= errno
;
2767 return g_local_file_measure_size_error (state
->flags
, saved_errno
, dir_name
, error
);
2770 while (success
&& (name
= g_dir_read_name (dir
)))
2774 node
.next
= dir_name
;
2776 node
.data
= (gchar
*) name
;
2778 node
.data
= g_build_filename (dir_name
->data
, name
, NULL
);
2781 success
= g_local_file_measure_size_of_file (fd
, &node
, state
, error
);
2794 g_local_file_measure_disk_usage (GFile
*file
,
2795 GFileMeasureFlags flags
,
2796 GCancellable
*cancellable
,
2797 GFileMeasureProgressCallback progress_callback
,
2798 gpointer progress_data
,
2799 guint64
*disk_usage
,
2804 GLocalFile
*local_file
= G_LOCAL_FILE (file
);
2805 MeasureState state
= { 0, };
2809 state
.flags
= flags
;
2810 state
.cancellable
= cancellable
;
2811 state
.progress_callback
= progress_callback
;
2812 state
.progress_data
= progress_data
;
2818 node
.data
= local_file
->filename
;
2821 if (!g_local_file_measure_size_of_file (root_fd
, &node
, &state
, error
))
2825 *disk_usage
= state
.disk_usage
;
2828 *num_dirs
= state
.num_dirs
;
2831 *num_files
= state
.num_files
;
2837 g_local_file_file_iface_init (GFileIface
*iface
)
2839 iface
->dup
= g_local_file_dup
;
2840 iface
->hash
= g_local_file_hash
;
2841 iface
->equal
= g_local_file_equal
;
2842 iface
->is_native
= g_local_file_is_native
;
2843 iface
->has_uri_scheme
= g_local_file_has_uri_scheme
;
2844 iface
->get_uri_scheme
= g_local_file_get_uri_scheme
;
2845 iface
->get_basename
= g_local_file_get_basename
;
2846 iface
->get_path
= g_local_file_get_path
;
2847 iface
->get_uri
= g_local_file_get_uri
;
2848 iface
->get_parse_name
= g_local_file_get_parse_name
;
2849 iface
->get_parent
= g_local_file_get_parent
;
2850 iface
->prefix_matches
= g_local_file_prefix_matches
;
2851 iface
->get_relative_path
= g_local_file_get_relative_path
;
2852 iface
->resolve_relative_path
= g_local_file_resolve_relative_path
;
2853 iface
->get_child_for_display_name
= g_local_file_get_child_for_display_name
;
2854 iface
->set_display_name
= g_local_file_set_display_name
;
2855 iface
->enumerate_children
= g_local_file_enumerate_children
;
2856 iface
->query_info
= g_local_file_query_info
;
2857 iface
->query_filesystem_info
= g_local_file_query_filesystem_info
;
2858 iface
->find_enclosing_mount
= g_local_file_find_enclosing_mount
;
2859 iface
->query_settable_attributes
= g_local_file_query_settable_attributes
;
2860 iface
->query_writable_namespaces
= g_local_file_query_writable_namespaces
;
2861 iface
->set_attribute
= g_local_file_set_attribute
;
2862 iface
->set_attributes_from_info
= g_local_file_set_attributes_from_info
;
2863 iface
->read_fn
= g_local_file_read
;
2864 iface
->append_to
= g_local_file_append_to
;
2865 iface
->create
= g_local_file_create
;
2866 iface
->replace
= g_local_file_replace
;
2867 iface
->open_readwrite
= g_local_file_open_readwrite
;
2868 iface
->create_readwrite
= g_local_file_create_readwrite
;
2869 iface
->replace_readwrite
= g_local_file_replace_readwrite
;
2870 iface
->delete_file
= g_local_file_delete
;
2871 iface
->trash
= g_local_file_trash
;
2872 iface
->make_directory
= g_local_file_make_directory
;
2873 iface
->make_symbolic_link
= g_local_file_make_symbolic_link
;
2874 iface
->copy
= g_local_file_copy
;
2875 iface
->move
= g_local_file_move
;
2876 iface
->monitor_dir
= g_local_file_monitor_dir
;
2877 iface
->monitor_file
= g_local_file_monitor_file
;
2878 iface
->measure_disk_usage
= g_local_file_measure_disk_usage
;
2880 iface
->supports_thread_contexts
= TRUE
;