1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
35 #include <sys/statfs.h>
37 #if HAVE_SYS_STATVFS_H
38 #include <sys/statvfs.h>
42 #elif HAVE_SYS_MOUNT_H
44 #include <sys/param.h>
46 #include <sys/mount.h>
53 #if defined(HAVE_STATFS) && defined(HAVE_STATVFS)
54 /* Some systems have both statfs and statvfs, pick the
55 most "native" for these */
56 # if defined(sun) && defined(__SVR4)
57 /* on solaris, statfs doesn't even have the
61 /* at least on linux, statfs is the actual syscall */
65 #elif defined(HAVE_STATFS)
69 #elif defined(HAVE_STATVFS)
75 #include "glocalfile.h"
76 #include "glocalfileinfo.h"
77 #include "glocalfileenumerator.h"
78 #include "glocalfileinputstream.h"
79 #include "glocalfileoutputstream.h"
80 #include "glocaldirectorymonitor.h"
81 #include "glocalfilemonitor.h"
82 #include "gmountprivate.h"
83 #include <glib/gstdio.h>
87 #define _WIN32_WINNT 0x0500
92 #ifndef FILE_READ_ONLY_VOLUME
93 #define FILE_READ_ONLY_VOLUME 0x00080000
97 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
100 #define S_ISLNK(m) (0)
104 #include "gioalias.h"
106 static void g_local_file_file_iface_init (GFileIface
*iface
);
108 static GFileAttributeInfoList
*local_writable_attributes
= NULL
;
109 static GFileAttributeInfoList
*local_writable_namespaces
= NULL
;
113 GObject parent_instance
;
118 #define g_local_file_get_type _g_local_file_get_type
119 G_DEFINE_TYPE_WITH_CODE (GLocalFile
, g_local_file
, G_TYPE_OBJECT
,
120 G_IMPLEMENT_INTERFACE (G_TYPE_FILE
,
121 g_local_file_file_iface_init
))
123 static char *find_mountpoint_for (const char *file
, dev_t dev
);
126 g_local_file_finalize (GObject
*object
)
130 local
= G_LOCAL_FILE (object
);
132 g_free (local
->filename
);
134 if (G_OBJECT_CLASS (g_local_file_parent_class
)->finalize
)
135 (*G_OBJECT_CLASS (g_local_file_parent_class
)->finalize
) (object
);
139 g_local_file_class_init (GLocalFileClass
*klass
)
141 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
142 GFileAttributeInfoList
*list
;
144 gobject_class
->finalize
= g_local_file_finalize
;
146 /* Set up attribute lists */
148 /* Writable attributes: */
150 list
= g_file_attribute_info_list_new ();
152 g_file_attribute_info_list_add (list
,
153 G_FILE_ATTRIBUTE_UNIX_MODE
,
154 G_FILE_ATTRIBUTE_TYPE_UINT32
,
155 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE
|
156 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
159 g_file_attribute_info_list_add (list
,
160 G_FILE_ATTRIBUTE_UNIX_UID
,
161 G_FILE_ATTRIBUTE_TYPE_UINT32
,
162 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
163 g_file_attribute_info_list_add (list
,
164 G_FILE_ATTRIBUTE_UNIX_GID
,
165 G_FILE_ATTRIBUTE_TYPE_UINT32
,
166 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
170 g_file_attribute_info_list_add (list
,
171 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET
,
172 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING
,
177 g_file_attribute_info_list_add (list
,
178 G_FILE_ATTRIBUTE_TIME_MODIFIED
,
179 G_FILE_ATTRIBUTE_TYPE_UINT64
,
180 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
181 g_file_attribute_info_list_add (list
,
182 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC
,
183 G_FILE_ATTRIBUTE_TYPE_UINT32
,
184 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
185 g_file_attribute_info_list_add (list
,
186 G_FILE_ATTRIBUTE_TIME_ACCESS
,
187 G_FILE_ATTRIBUTE_TYPE_UINT64
,
188 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
189 g_file_attribute_info_list_add (list
,
190 G_FILE_ATTRIBUTE_TIME_ACCESS_USEC
,
191 G_FILE_ATTRIBUTE_TYPE_UINT32
,
192 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
195 local_writable_attributes
= list
;
197 /* Writable namespaces: */
199 list
= g_file_attribute_info_list_new ();
202 g_file_attribute_info_list_add (list
,
204 G_FILE_ATTRIBUTE_TYPE_STRING
,
205 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE
|
206 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
207 g_file_attribute_info_list_add (list
,
209 G_FILE_ATTRIBUTE_TYPE_STRING
,
210 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
);
213 local_writable_namespaces
= list
;
217 g_local_file_init (GLocalFile
*local
)
223 canonicalize_filename (const char *filename
)
225 char *canon
, *start
, *p
, *q
;
229 if (!g_path_is_absolute (filename
))
231 cwd
= g_get_current_dir ();
232 canon
= g_build_filename (cwd
, filename
, NULL
);
236 canon
= g_strdup (filename
);
238 start
= (char *)g_path_skip_root (canon
);
240 /* POSIX allows double slashes at the start to
241 * mean something special (as does windows too).
242 * So, "//" != "/", but more than two slashes
248 G_IS_DIR_SEPARATOR (*p
);
255 memmove (start
, start
+i
, strlen (start
+i
)+1);
261 if (p
[0] == '.' && (p
[1] == 0 || G_IS_DIR_SEPARATOR (p
[1])))
263 memmove (p
, p
+1, strlen (p
+1)+1);
265 else if (p
[0] == '.' && p
[1] == '.' && (p
[2] == 0 || G_IS_DIR_SEPARATOR (p
[2])))
268 /* Skip previous separator */
272 while (p
> start
&& !G_IS_DIR_SEPARATOR (*p
))
274 if (G_IS_DIR_SEPARATOR (*p
))
275 *p
++ = G_DIR_SEPARATOR
;
276 memmove (p
, q
, strlen (q
)+1);
280 /* Skip until next separator */
281 while (*p
!= 0 && !G_IS_DIR_SEPARATOR (*p
))
286 /* Canonicalize one separator */
287 *p
++ = G_DIR_SEPARATOR
;
291 /* Remove additional separators */
293 while (*q
&& G_IS_DIR_SEPARATOR (*q
))
297 memmove (p
, q
, strlen (q
)+1);
300 /* Remove trailing slashes */
301 if (p
> start
&& G_IS_DIR_SEPARATOR (*(p
-1)))
309 * @filename: filename of the file to create.
311 * Returns: new local #GFile.
314 _g_local_file_new (const char *filename
)
318 local
= g_object_new (G_TYPE_LOCAL_FILE
, NULL
);
319 local
->filename
= canonicalize_filename (filename
);
321 return G_FILE (local
);
325 g_local_file_is_native (GFile
*file
)
331 g_local_file_has_uri_scheme (GFile
*file
,
332 const char *uri_scheme
)
334 return g_ascii_strcasecmp (uri_scheme
, "file") == 0;
338 g_local_file_get_uri_scheme (GFile
*file
)
340 return g_strdup ("file");
344 g_local_file_get_basename (GFile
*file
)
346 return g_path_get_basename (G_LOCAL_FILE (file
)->filename
);
350 g_local_file_get_path (GFile
*file
)
352 return g_strdup (G_LOCAL_FILE (file
)->filename
);
356 g_local_file_get_uri (GFile
*file
)
358 return g_filename_to_uri (G_LOCAL_FILE (file
)->filename
, NULL
, NULL
);
362 get_filename_charset (const gchar
**filename_charset
)
364 const gchar
**charsets
;
367 is_utf8
= g_get_filename_charsets (&charsets
);
369 if (filename_charset
)
370 *filename_charset
= charsets
[0];
376 name_is_valid_for_display (const char *string
,
377 gboolean is_valid_utf8
)
381 if (!is_valid_utf8
&&
382 !g_utf8_validate (string
, -1, NULL
))
385 while ((c
= *string
++) != 0)
387 if (g_ascii_iscntrl (c
))
395 g_local_file_get_parse_name (GFile
*file
)
397 const char *filename
;
399 const gchar
*charset
;
401 char *roundtripped_filename
;
402 gboolean free_utf8_filename
;
403 gboolean is_valid_utf8
;
406 filename
= G_LOCAL_FILE (file
)->filename
;
407 if (get_filename_charset (&charset
))
409 utf8_filename
= (char *)filename
;
410 free_utf8_filename
= FALSE
;
411 is_valid_utf8
= FALSE
; /* Can't guarantee this */
415 utf8_filename
= g_convert (filename
, -1,
416 "UTF-8", charset
, NULL
, NULL
, NULL
);
417 free_utf8_filename
= TRUE
;
418 is_valid_utf8
= TRUE
;
420 if (utf8_filename
!= NULL
)
422 /* Make sure we can roundtrip: */
423 roundtripped_filename
= g_convert (utf8_filename
, -1,
424 charset
, "UTF-8", NULL
, NULL
, NULL
);
426 if (roundtripped_filename
== NULL
||
427 strcmp (utf8_filename
, roundtripped_filename
) != 0)
429 g_free (utf8_filename
);
430 utf8_filename
= NULL
;
435 if (utf8_filename
!= NULL
&&
436 name_is_valid_for_display (utf8_filename
, is_valid_utf8
))
438 if (free_utf8_filename
)
439 parse_name
= utf8_filename
;
441 parse_name
= g_strdup (utf8_filename
);
445 escaped_path
= g_uri_escape_string (filename
,
446 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT
"/",
448 parse_name
= g_strconcat ("file://",
449 (*escaped_path
!= '/') ? "/" : "",
453 g_free (escaped_path
);
455 if (free_utf8_filename
)
456 g_free (utf8_filename
);
463 g_local_file_get_parent (GFile
*file
)
465 GLocalFile
*local
= G_LOCAL_FILE (file
);
466 const char *non_root
;
471 non_root
= g_path_skip_root (local
->filename
);
475 dirname
= g_path_get_dirname (local
->filename
);
476 parent
= _g_local_file_new (dirname
);
482 g_local_file_dup (GFile
*file
)
484 GLocalFile
*local
= G_LOCAL_FILE (file
);
486 return _g_local_file_new (local
->filename
);
490 g_local_file_hash (GFile
*file
)
492 GLocalFile
*local
= G_LOCAL_FILE (file
);
494 return g_str_hash (local
->filename
);
498 g_local_file_equal (GFile
*file1
,
501 GLocalFile
*local1
= G_LOCAL_FILE (file1
);
502 GLocalFile
*local2
= G_LOCAL_FILE (file2
);
504 return g_str_equal (local1
->filename
, local2
->filename
);
508 match_prefix (const char *path
,
513 prefix_len
= strlen (prefix
);
514 if (strncmp (path
, prefix
, prefix_len
) != 0)
517 /* Handle the case where prefix is the root, so that
518 * the IS_DIR_SEPRARATOR check below works */
519 if (prefix_len
> 0 &&
520 G_IS_DIR_SEPARATOR (prefix
[prefix_len
-1]))
523 return path
+ prefix_len
;
527 g_local_file_prefix_matches (GFile
*parent
,
530 GLocalFile
*parent_local
= G_LOCAL_FILE (parent
);
531 GLocalFile
*descendant_local
= G_LOCAL_FILE (descendant
);
532 const char *remainder
;
534 remainder
= match_prefix (descendant_local
->filename
, parent_local
->filename
);
535 if (remainder
!= NULL
&& G_IS_DIR_SEPARATOR (*remainder
))
541 g_local_file_get_relative_path (GFile
*parent
,
544 GLocalFile
*parent_local
= G_LOCAL_FILE (parent
);
545 GLocalFile
*descendant_local
= G_LOCAL_FILE (descendant
);
546 const char *remainder
;
548 remainder
= match_prefix (descendant_local
->filename
, parent_local
->filename
);
550 if (remainder
!= NULL
&& G_IS_DIR_SEPARATOR (*remainder
))
551 return g_strdup (remainder
+ 1);
556 g_local_file_resolve_relative_path (GFile
*file
,
557 const char *relative_path
)
559 GLocalFile
*local
= G_LOCAL_FILE (file
);
563 if (g_path_is_absolute (relative_path
))
564 return _g_local_file_new (relative_path
);
566 filename
= g_build_filename (local
->filename
, relative_path
, NULL
);
567 child
= _g_local_file_new (filename
);
573 static GFileEnumerator
*
574 g_local_file_enumerate_children (GFile
*file
,
575 const char *attributes
,
576 GFileQueryInfoFlags flags
,
577 GCancellable
*cancellable
,
580 GLocalFile
*local
= G_LOCAL_FILE (file
);
581 return _g_local_file_enumerator_new (local
->filename
,
587 g_local_file_get_child_for_display_name (GFile
*file
,
588 const char *display_name
,
591 GFile
*parent
, *new_file
;
594 basename
= g_filename_from_utf8 (display_name
, -1, NULL
, NULL
, NULL
);
595 if (basename
== NULL
)
597 g_set_error (error
, G_IO_ERROR
,
598 G_IO_ERROR_INVALID_FILENAME
,
599 _("Invalid filename %s"), display_name
);
603 parent
= g_file_get_parent (file
);
604 new_file
= g_file_get_child (file
, basename
);
605 g_object_unref (parent
);
613 get_fs_type (long f_type
)
616 /* filesystem ids taken from linux manpage */
713 G_LOCK_DEFINE_STATIC(mount_info_hash
);
714 static GHashTable
*mount_info_hash
= NULL
;
715 static guint64 mount_info_hash_cache_time
= 0;
718 MOUNT_INFO_READONLY
= 1<<0
722 device_equal (gconstpointer v1
,
725 return *(dev_t
*)v1
== *(dev_t
*)v2
;
729 device_hash (gconstpointer v
)
731 return (guint
) *(dev_t
*)v
;
735 get_mount_info (GFileInfo
*fs_info
,
737 GFileAttributeMatcher
*matcher
)
741 gpointer info_as_ptr
;
745 GUnixMountEntry
*mount
;
748 if (g_lstat (path
, &buf
) != 0)
751 G_LOCK (mount_info_hash
);
753 if (mount_info_hash
== NULL
)
754 mount_info_hash
= g_hash_table_new_full (device_hash
, device_equal
,
758 if (g_unix_mounts_changed_since (mount_info_hash_cache_time
))
759 g_hash_table_remove_all (mount_info_hash
);
761 got_info
= g_hash_table_lookup_extended (mount_info_hash
,
766 G_UNLOCK (mount_info_hash
);
768 mount_info
= GPOINTER_TO_UINT (info_as_ptr
);
774 mountpoint
= find_mountpoint_for (path
, buf
.st_dev
);
775 if (mountpoint
== NULL
)
778 mount
= g_unix_mount_at (mountpoint
, &cache_time
);
781 if (g_unix_mount_is_readonly (mount
))
782 mount_info
|= MOUNT_INFO_READONLY
;
784 g_unix_mount_free (mount
);
787 dev
= g_new0 (dev_t
, 1);
790 G_LOCK (mount_info_hash
);
791 mount_info_hash_cache_time
= cache_time
;
792 g_hash_table_insert (mount_info_hash
, dev
, GUINT_TO_POINTER (mount_info
));
793 G_UNLOCK (mount_info_hash
);
796 if (mount_info
& MOUNT_INFO_READONLY
)
797 g_file_info_set_attribute_boolean (fs_info
, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY
, TRUE
);
805 is_xp_or_later (void)
807 static int result
= -1;
811 OSVERSIONINFOEX ver_info
= {0};
812 DWORDLONG cond_mask
= 0;
813 int op
= VER_GREATER_EQUAL
;
815 ver_info
.dwOSVersionInfoSize
= sizeof ver_info
;
816 ver_info
.dwMajorVersion
= 5;
817 ver_info
.dwMinorVersion
= 1;
819 VER_SET_CONDITION (cond_mask
, VER_MAJORVERSION
, op
);
820 VER_SET_CONDITION (cond_mask
, VER_MINORVERSION
, op
);
822 result
= VerifyVersionInfo (&ver_info
,
823 VER_MAJORVERSION
| VER_MINORVERSION
,
831 get_volume_for_path (const char *path
)
837 wpath
= g_utf8_to_utf16 (path
, -1, NULL
, &len
, NULL
);
838 result
= g_new (wchar_t, len
+ 2);
840 if (!GetVolumePathNameW (wpath
, result
, len
+ 2))
842 char *msg
= g_win32_error_message (GetLastError ());
843 g_critical ("GetVolumePathName failed: %s", msg
);
850 len
= wcslen (result
);
851 if (len
> 0 && result
[len
-1] != L
'\\')
853 result
= g_renew (wchar_t, result
, len
+ 2);
863 find_mountpoint_for (const char *file
, dev_t dev
)
868 wpath
= get_volume_for_path (file
);
872 utf8_path
= g_utf16_to_utf8 (wpath
, -1, NULL
, NULL
, NULL
);
879 get_filesystem_readonly (GFileInfo
*info
,
884 rootdir
= get_volume_for_path (path
);
888 if (is_xp_or_later ())
891 if (GetVolumeInformationW (rootdir
, NULL
, 0, NULL
, NULL
, &flags
, NULL
, 0))
892 g_file_info_set_attribute_boolean (info
, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY
,
893 (flags
& FILE_READ_ONLY_VOLUME
) != 0);
897 if (GetDriveTypeW (rootdir
) == DRIVE_CDROM
)
898 g_file_info_set_attribute_boolean (info
, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY
, TRUE
);
905 #endif /* G_OS_WIN32 */
908 g_local_file_query_filesystem_info (GFile
*file
,
909 const char *attributes
,
910 GCancellable
*cancellable
,
913 GLocalFile
*local
= G_LOCAL_FILE (file
);
921 struct statfs statfs_buffer
;
922 #elif defined(USE_STATVFS)
923 struct statvfs statfs_buffer
;
926 GFileAttributeMatcher
*attribute_matcher
;
933 statfs_result
= statfs (local
->filename
, &statfs_buffer
);
934 #elif STATFS_ARGS == 4
935 statfs_result
= statfs (local
->filename
, &statfs_buffer
,
936 sizeof (statfs_buffer
), 0);
938 block_size
= statfs_buffer
.f_bsize
;
940 #if defined(__linux__)
941 /* ncpfs does not know the amount of available and free space *
942 * assuming ncpfs is linux specific, if you are on a non-linux platform
943 * where ncpfs is available, please file a bug about it on bugzilla.gnome.org
945 if (statfs_buffer
.f_bavail
== 0 && statfs_buffer
.f_bfree
== 0 &&
946 /* linux/ncp_fs.h: NCP_SUPER_MAGIC == 0x564c */
947 statfs_buffer
.f_type
== 0x564c)
951 #elif defined(USE_STATVFS)
952 statfs_result
= statvfs (local
->filename
, &statfs_buffer
);
953 block_size
= statfs_buffer
.f_frsize
;
956 if (statfs_result
== -1)
960 g_set_error (error
, G_IO_ERROR
,
961 g_io_error_from_errno (errsv
),
962 _("Error getting filesystem info: %s"),
967 info
= g_file_info_new ();
969 attribute_matcher
= g_file_attribute_matcher_new (attributes
);
972 g_file_attribute_matcher_matches (attribute_matcher
,
973 G_FILE_ATTRIBUTE_FILESYSTEM_FREE
))
976 gchar
*localdir
= g_path_get_dirname (local
->filename
);
977 wchar_t *wdirname
= g_utf8_to_utf16 (localdir
, -1, NULL
, NULL
, NULL
);
981 if (GetDiskFreeSpaceExW (wdirname
, &li
, NULL
, NULL
))
982 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_FREE
, (guint64
)li
.QuadPart
);
985 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_FREE
, block_size
* statfs_buffer
.f_bavail
);
989 g_file_attribute_matcher_matches (attribute_matcher
,
990 G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
))
993 gchar
*localdir
= g_path_get_dirname (local
->filename
);
994 wchar_t *wdirname
= g_utf8_to_utf16 (localdir
, -1, NULL
, NULL
, NULL
);
998 if (GetDiskFreeSpaceExW (wdirname
, NULL
, &li
, NULL
))
999 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
, (guint64
)li
.QuadPart
);
1002 g_file_info_set_attribute_uint64 (info
, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
, block_size
* statfs_buffer
.f_blocks
);
1006 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1007 fstype
= g_strdup(statfs_buffer
.f_fstypename
);
1009 fstype
= get_fs_type (statfs_buffer
.f_type
);
1012 #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1013 fstype
= g_strdup(statfs_buffer
.f_basetype
);
1018 g_file_attribute_matcher_matches (attribute_matcher
,
1019 G_FILE_ATTRIBUTE_FILESYSTEM_TYPE
))
1020 g_file_info_set_attribute_string (info
, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE
, fstype
);
1023 if (g_file_attribute_matcher_matches (attribute_matcher
,
1024 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY
))
1027 get_filesystem_readonly (info
, local
->filename
);
1029 get_mount_info (info
, local
->filename
, attribute_matcher
);
1033 g_file_attribute_matcher_unref (attribute_matcher
);
1039 g_local_file_find_enclosing_mount (GFile
*file
,
1040 GCancellable
*cancellable
,
1043 GLocalFile
*local
= G_LOCAL_FILE (file
);
1048 if (g_lstat (local
->filename
, &buf
) != 0)
1050 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
,
1051 /* Translators: This is an error message when trying to
1052 * find the enclosing (user visible) mount of a file, but
1054 _("Containing mount does not exist"));
1058 mountpoint
= find_mountpoint_for (local
->filename
, buf
.st_dev
);
1059 if (mountpoint
== NULL
)
1061 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
,
1062 /* Translators: This is an error message when trying to
1063 * find the enclosing (user visible) mount of a file, but
1065 _("Containing mount does not exist"));
1069 mount
= _g_mount_get_for_mount_path (mountpoint
, cancellable
);
1070 g_free (mountpoint
);
1074 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
,
1075 /* Translators: This is an error message when trying to find
1076 * the enclosing (user visible) mount of a file, but none
1078 _("Containing mount does not exist"));
1083 g_local_file_set_display_name (GFile
*file
,
1084 const char *display_name
,
1085 GCancellable
*cancellable
,
1088 GLocalFile
*local
, *new_local
;
1089 GFile
*new_file
, *parent
;
1090 struct stat statbuf
;
1093 parent
= g_file_get_parent (file
);
1096 g_set_error (error
, G_IO_ERROR
,
1098 _("Can't rename root directory"));
1102 new_file
= g_file_get_child_for_display_name (parent
, display_name
, error
);
1103 g_object_unref (parent
);
1105 if (new_file
== NULL
)
1108 local
= G_LOCAL_FILE (file
);
1109 new_local
= G_LOCAL_FILE (new_file
);
1111 if (!(g_lstat (new_local
->filename
, &statbuf
) == -1 &&
1114 g_set_error (error
, G_IO_ERROR
,
1116 _("Can't rename file, filename already exist"));
1120 if (g_rename (local
->filename
, new_local
->filename
) == -1)
1124 if (errsv
== EINVAL
)
1125 /* We can't get a rename file into itself error herer,
1126 so this must be an invalid filename, on e.g. FAT */
1127 g_set_error (error
, G_IO_ERROR
,
1128 G_IO_ERROR_INVALID_FILENAME
,
1129 _("Invalid filename"));
1131 g_set_error (error
, G_IO_ERROR
,
1132 g_io_error_from_errno (errsv
),
1133 _("Error renaming file: %s"),
1134 g_strerror (errsv
));
1135 g_object_unref (new_file
);
1143 g_local_file_query_info (GFile
*file
,
1144 const char *attributes
,
1145 GFileQueryInfoFlags flags
,
1146 GCancellable
*cancellable
,
1149 GLocalFile
*local
= G_LOCAL_FILE (file
);
1151 GFileAttributeMatcher
*matcher
;
1152 char *basename
, *dirname
;
1153 GLocalParentFileInfo parent_info
;
1155 matcher
= g_file_attribute_matcher_new (attributes
);
1157 basename
= g_path_get_basename (local
->filename
);
1159 dirname
= g_path_get_dirname (local
->filename
);
1160 _g_local_file_info_get_parent_info (dirname
, matcher
, &parent_info
);
1163 info
= _g_local_file_info_get (basename
, local
->filename
,
1164 matcher
, flags
, &parent_info
,
1169 g_file_attribute_matcher_unref (matcher
);
1174 static GFileAttributeInfoList
*
1175 g_local_file_query_settable_attributes (GFile
*file
,
1176 GCancellable
*cancellable
,
1179 return g_file_attribute_info_list_ref (local_writable_attributes
);
1182 static GFileAttributeInfoList
*
1183 g_local_file_query_writable_namespaces (GFile
*file
,
1184 GCancellable
*cancellable
,
1187 return g_file_attribute_info_list_ref (local_writable_namespaces
);
1191 g_local_file_set_attribute (GFile
*file
,
1192 const char *attribute
,
1193 GFileAttributeType type
,
1195 GFileQueryInfoFlags flags
,
1196 GCancellable
*cancellable
,
1199 GLocalFile
*local
= G_LOCAL_FILE (file
);
1201 return _g_local_file_info_set_attribute (local
->filename
,
1211 g_local_file_set_attributes_from_info (GFile
*file
,
1213 GFileQueryInfoFlags flags
,
1214 GCancellable
*cancellable
,
1217 GLocalFile
*local
= G_LOCAL_FILE (file
);
1218 int res
, chained_res
;
1219 GFileIface
*default_iface
;
1221 res
= _g_local_file_info_set_attributes (local
->filename
,
1227 error
= NULL
; /* Don't write over error if further errors */
1229 default_iface
= g_type_default_interface_peek (G_TYPE_FILE
);
1231 chained_res
= (default_iface
->set_attributes_from_info
) (file
, info
, flags
, cancellable
, error
);
1233 return res
&& chained_res
;
1236 static GFileInputStream
*
1237 g_local_file_read (GFile
*file
,
1238 GCancellable
*cancellable
,
1241 GLocalFile
*local
= G_LOCAL_FILE (file
);
1245 fd
= g_open (local
->filename
, O_RDONLY
|O_BINARY
, 0);
1250 g_set_error (error
, G_IO_ERROR
,
1251 g_io_error_from_errno (errsv
),
1252 _("Error opening file: %s"),
1253 g_strerror (errsv
));
1257 if (fstat(fd
, &buf
) == 0 && S_ISDIR (buf
.st_mode
))
1260 g_set_error (error
, G_IO_ERROR
,
1261 G_IO_ERROR_IS_DIRECTORY
,
1262 _("Can't open directory"));
1266 return _g_local_file_input_stream_new (fd
);
1269 static GFileOutputStream
*
1270 g_local_file_append_to (GFile
*file
,
1271 GFileCreateFlags flags
,
1272 GCancellable
*cancellable
,
1275 return _g_local_file_output_stream_append (G_LOCAL_FILE (file
)->filename
,
1276 flags
, cancellable
, error
);
1279 static GFileOutputStream
*
1280 g_local_file_create (GFile
*file
,
1281 GFileCreateFlags flags
,
1282 GCancellable
*cancellable
,
1285 return _g_local_file_output_stream_create (G_LOCAL_FILE (file
)->filename
,
1286 flags
, cancellable
, error
);
1289 static GFileOutputStream
*
1290 g_local_file_replace (GFile
*file
,
1292 gboolean make_backup
,
1293 GFileCreateFlags flags
,
1294 GCancellable
*cancellable
,
1297 return _g_local_file_output_stream_replace (G_LOCAL_FILE (file
)->filename
,
1298 etag
, make_backup
, flags
,
1299 cancellable
, error
);
1304 g_local_file_delete (GFile
*file
,
1305 GCancellable
*cancellable
,
1308 GLocalFile
*local
= G_LOCAL_FILE (file
);
1310 if (g_remove (local
->filename
) == -1)
1314 /* Posix allows EEXIST too, but the more sane error
1315 is G_IO_ERROR_NOT_FOUND, and its what nautilus
1317 if (errsv
== EEXIST
)
1320 g_set_error (error
, G_IO_ERROR
,
1321 g_io_error_from_errno (errsv
),
1322 _("Error removing file: %s"),
1323 g_strerror (errsv
));
1331 strip_trailing_slashes (const char *path
)
1336 path_copy
= g_strdup (path
);
1337 len
= strlen (path_copy
);
1338 while (len
> 1 && path_copy
[len
-1] == '/')
1339 path_copy
[--len
] = 0;
1345 expand_symlink (const char *link
)
1347 char *resolved
, *canonical
, *parent
, *link2
;
1348 char symlink_value
[4096];
1356 res
= readlink (link
, symlink_value
, sizeof (symlink_value
) - 1);
1359 return g_strdup (link
);
1360 symlink_value
[res
] = 0;
1363 if (g_path_is_absolute (symlink_value
))
1364 return canonicalize_filename (symlink_value
);
1367 link2
= strip_trailing_slashes (link
);
1368 parent
= g_path_get_dirname (link2
);
1371 resolved
= g_build_filename (parent
, symlink_value
, NULL
);
1374 canonical
= canonicalize_filename (resolved
);
1383 get_parent (const char *path
,
1387 struct stat parent_stat
;
1391 path_copy
= strip_trailing_slashes (path
);
1393 parent
= g_path_get_dirname (path_copy
);
1394 if (strcmp (parent
, ".") == 0 ||
1395 strcmp (parent
, path_copy
) == 0)
1405 if (g_lstat (parent
, &parent_stat
) != 0)
1411 if (S_ISLNK (parent_stat
.st_mode
))
1414 parent
= expand_symlink (parent
);
1419 if (num_recursions
> 12)
1424 } while (S_ISLNK (parent_stat
.st_mode
));
1426 *parent_dev
= parent_stat
.st_dev
;
1432 expand_all_symlinks (const char *path
)
1434 char *parent
, *parent_expanded
;
1435 char *basename
, *res
;
1438 parent
= get_parent (path
, &parent_dev
);
1441 parent_expanded
= expand_all_symlinks (parent
);
1443 basename
= g_path_get_basename (path
);
1444 res
= g_build_filename (parent_expanded
, basename
, NULL
);
1446 g_free (parent_expanded
);
1449 res
= g_strdup (path
);
1457 find_mountpoint_for (const char *file
,
1461 dev_t dir_dev
, parent_dev
;
1463 dir
= g_strdup (file
);
1468 parent
= get_parent (dir
, &parent_dev
);
1472 if (parent_dev
!= dir_dev
)
1484 find_topdir_for (const char *file
)
1489 dir
= get_parent (file
, &dir_dev
);
1493 return find_mountpoint_for (dir
, dir_dev
);
1497 get_unique_filename (const char *basename
,
1503 return g_strdup (basename
);
1505 dot
= strchr (basename
, '.');
1507 return g_strdup_printf ("%.*s.%d%s", (int)(dot
- basename
), basename
, id
, dot
);
1509 return g_strdup_printf ("%s.%d", basename
, id
);
1513 path_has_prefix (const char *path
,
1521 prefix_len
= strlen (prefix
);
1523 if (strncmp (path
, prefix
, prefix_len
) == 0 &&
1524 (prefix_len
== 0 || /* empty prefix always matches */
1525 prefix
[prefix_len
- 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1526 path
[prefix_len
] == 0 ||
1527 path
[prefix_len
] == '/'))
1534 try_make_relative (const char *path
,
1537 char *path2
, *base2
;
1540 path2
= expand_all_symlinks (path
);
1541 base2
= expand_all_symlinks (base
);
1544 if (path_has_prefix (path2
, base2
))
1546 relative
= path2
+ strlen (base2
);
1547 while (*relative
== '/')
1549 relative
= g_strdup (relative
);
1557 /* Failed, use abs path */
1558 return g_strdup (path
);
1562 escape_trash_name (char *name
)
1565 const gchar hex
[16] = "0123456789ABCDEF";
1567 str
= g_string_new ("");
1575 if (g_ascii_isprint (c
))
1576 g_string_append_c (str
, c
);
1579 g_string_append_c (str
, '%');
1580 g_string_append_c (str
, hex
[((guchar
)c
) >> 4]);
1581 g_string_append_c (str
, hex
[((guchar
)c
) & 0xf]);
1585 return g_string_free (str
, FALSE
);
1589 _g_local_file_has_trash_dir (const char *dirname
, dev_t dir_dev
)
1591 static gsize home_dev_set
= 0;
1592 static dev_t home_dev
;
1593 char *topdir
, *globaldir
, *trashdir
, *tmpname
;
1596 struct stat global_stat
, trash_stat
;
1600 if (g_once_init_enter (&home_dev_set
))
1602 struct stat home_stat
;
1604 g_stat (g_get_home_dir (), &home_stat
);
1605 home_dev
= home_stat
.st_dev
;
1606 g_once_init_leave (&home_dev_set
, 1);
1609 /* Assume we can trash to the home */
1610 if (dir_dev
== home_dev
)
1613 topdir
= find_mountpoint_for (dirname
, dir_dev
);
1617 globaldir
= g_build_filename (topdir
, ".Trash", NULL
);
1618 statres
= g_lstat (globaldir
, &global_stat
);
1619 if (g_lstat (globaldir
, &global_stat
) == 0 &&
1620 S_ISDIR (global_stat
.st_mode
) &&
1621 (global_stat
.st_mode
& S_ISVTX
) != 0)
1623 /* got a toplevel sysadmin created dir, assume we
1624 * can trash to it (we should be able to create a dir)
1625 * This fails for the FAT case where the ownership of
1626 * that dir would be wrong though..
1634 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1636 g_snprintf (uid_str
, sizeof (uid_str
), "%lu", (unsigned long) uid
);
1638 tmpname
= g_strdup_printf (".Trash-%s", uid_str
);
1639 trashdir
= g_build_filename (topdir
, tmpname
, NULL
);
1642 if (g_lstat (trashdir
, &trash_stat
) == 0)
1647 S_ISDIR (trash_stat
.st_mode
) &&
1648 trash_stat
.st_uid
== uid
;
1652 /* User specific trash didn't exist, can we create it? */
1653 res
= g_access (topdir
, W_OK
) == 0;
1662 g_local_file_trash (GFile
*file
,
1663 GCancellable
*cancellable
,
1666 GLocalFile
*local
= G_LOCAL_FILE (file
);
1667 struct stat file_stat
, home_stat
;
1668 const char *homedir
;
1669 char *trashdir
, *topdir
, *infodir
, *filesdir
;
1670 char *basename
, *trashname
, *trashfile
, *infoname
, *infofile
;
1671 char *original_name
, *original_name_escaped
;
1674 gboolean is_homedir_trash
;
1675 char delete_time
[32];
1677 struct stat trash_stat
, global_stat
;
1678 char *dirname
, *globaldir
;
1680 if (g_lstat (local
->filename
, &file_stat
) != 0)
1684 g_set_error (error
, G_IO_ERROR
,
1685 g_io_error_from_errno (errsv
),
1686 _("Error trashing file: %s"),
1687 g_strerror (errsv
));
1691 homedir
= g_get_home_dir ();
1692 g_stat (homedir
, &home_stat
);
1694 is_homedir_trash
= FALSE
;
1696 if (file_stat
.st_dev
== home_stat
.st_dev
)
1698 is_homedir_trash
= TRUE
;
1700 trashdir
= g_build_filename (g_get_user_data_dir (), "Trash", NULL
);
1701 if (g_mkdir_with_parents (trashdir
, 0700) < 0)
1706 display_name
= g_filename_display_name (trashdir
);
1707 g_set_error (error
, G_IO_ERROR
,
1708 g_io_error_from_errno (errsv
),
1709 _("Unable to create trash dir %s: %s"),
1710 display_name
, g_strerror (errsv
));
1711 g_free (display_name
);
1715 topdir
= g_strdup (g_get_user_data_dir ());
1723 g_snprintf (uid_str
, sizeof (uid_str
), "%lu", (unsigned long)uid
);
1725 topdir
= find_topdir_for (local
->filename
);
1728 g_set_error (error
, G_IO_ERROR
,
1729 G_IO_ERROR_NOT_SUPPORTED
,
1730 _("Unable to find toplevel directory for trash"));
1734 /* Try looking for global trash dir $topdir/.Trash/$uid */
1735 globaldir
= g_build_filename (topdir
, ".Trash", NULL
);
1736 if (g_lstat (globaldir
, &global_stat
) == 0 &&
1737 S_ISDIR (global_stat
.st_mode
) &&
1738 (global_stat
.st_mode
& S_ISVTX
) != 0)
1740 trashdir
= g_build_filename (globaldir
, uid_str
, NULL
);
1742 if (g_lstat (trashdir
, &trash_stat
) == 0)
1744 if (!S_ISDIR (trash_stat
.st_mode
) ||
1745 trash_stat
.st_uid
!= uid
)
1747 /* Not a directory or not owned by user, ignore */
1752 else if (g_mkdir (trashdir
, 0700) == -1)
1760 if (trashdir
== NULL
)
1762 gboolean tried_create
;
1764 /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1765 dirname
= g_strdup_printf (".Trash-%s", uid_str
);
1766 trashdir
= g_build_filename (topdir
, dirname
, NULL
);
1769 tried_create
= FALSE
;
1772 if (g_lstat (trashdir
, &trash_stat
) == 0)
1774 if (!S_ISDIR (trash_stat
.st_mode
) ||
1775 trash_stat
.st_uid
!= uid
)
1777 /* Remove the failed directory */
1779 g_remove (trashdir
);
1781 /* Not a directory or not owned by user, ignore */
1788 if (!tried_create
&&
1789 g_mkdir (trashdir
, 0700) != -1)
1791 /* Ensure that the created dir has the right uid etc.
1792 This might fail on e.g. a FAT dir */
1793 tried_create
= TRUE
;
1804 if (trashdir
== NULL
)
1807 g_set_error (error
, G_IO_ERROR
,
1808 G_IO_ERROR_NOT_SUPPORTED
,
1809 _("Unable to find or create trash directory"));
1814 /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1816 infodir
= g_build_filename (trashdir
, "info", NULL
);
1817 filesdir
= g_build_filename (trashdir
, "files", NULL
);
1820 /* Make sure we have the subdirectories */
1821 if ((g_mkdir (infodir
, 0700) == -1 && errno
!= EEXIST
) ||
1822 (g_mkdir (filesdir
, 0700) == -1 && errno
!= EEXIST
))
1827 g_set_error (error
, G_IO_ERROR
,
1828 G_IO_ERROR_NOT_SUPPORTED
,
1829 _("Unable to find or create trash directory"));
1833 basename
= g_path_get_basename (local
->filename
);
1841 trashname
= get_unique_filename (basename
, i
++);
1842 infoname
= g_strconcat (trashname
, ".trashinfo", NULL
);
1843 infofile
= g_build_filename (infodir
, infoname
, NULL
);
1846 fd
= open (infofile
, O_CREAT
| O_EXCL
, 0666);
1847 } while (fd
== -1 && errno
== EEXIST
);
1861 g_set_error (error
, G_IO_ERROR
,
1862 g_io_error_from_errno (errsv
),
1863 _("Unable to create trashing info file: %s"),
1864 g_strerror (errsv
));
1870 /* TODO: Maybe we should verify that you can delete the file from the trash
1871 before moving it? OTOH, that is hard, as it needs a recursive scan */
1873 trashfile
= g_build_filename (filesdir
, trashname
, NULL
);
1877 if (g_rename (local
->filename
, trashfile
) == -1)
1886 g_set_error (error
, G_IO_ERROR
,
1887 g_io_error_from_errno (errsv
),
1888 _("Unable to trash file: %s"),
1889 g_strerror (errsv
));
1895 /* TODO: Do we need to update mtime/atime here after the move? */
1897 /* Use absolute names for homedir */
1898 if (is_homedir_trash
)
1899 original_name
= g_strdup (local
->filename
);
1901 original_name
= try_make_relative (local
->filename
, topdir
);
1902 original_name_escaped
= escape_trash_name (original_name
);
1904 g_free (original_name
);
1911 localtime_r (&t
, &now
);
1913 strftime(delete_time
, sizeof (delete_time
), "%Y-%m-%dT%H:%M:%S", &now
);
1916 data
= g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
1917 original_name_escaped
, delete_time
);
1919 g_file_set_contents (infofile
, data
, -1, NULL
);
1923 g_free (original_name_escaped
);
1928 #else /* G_OS_WIN32 */
1930 _g_local_file_has_trash_dir (const char *dirname
, dev_t dir_dev
)
1932 return FALSE
; /* XXX ??? */
1936 g_local_file_trash (GFile
*file
,
1937 GCancellable
*cancellable
,
1940 GLocalFile
*local
= G_LOCAL_FILE (file
);
1941 SHFILEOPSTRUCTW op
= {0};
1946 wfilename
= g_utf8_to_utf16 (local
->filename
, -1, NULL
, &len
, NULL
);
1947 /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
1948 wfilename
= g_renew (wchar_t, wfilename
, len
+ 2);
1949 wfilename
[len
+ 1] = 0;
1951 op
.wFunc
= FO_DELETE
;
1952 op
.pFrom
= wfilename
;
1953 op
.fFlags
= FOF_ALLOWUNDO
;
1955 success
= SHFileOperationW (&op
) == 0;
1957 if (success
&& op
.fAnyOperationsAborted
)
1959 if (cancellable
&& !g_cancellable_is_cancelled (cancellable
))
1960 g_cancellable_cancel (cancellable
);
1961 g_set_error (error
, G_IO_ERROR
,
1962 G_IO_ERROR_CANCELLED
,
1963 _("Unable to trash file: %s"),
1964 _("Operation was cancelled"));
1968 g_set_error (error
, G_IO_ERROR
,
1970 _("Unable to trash file: %s"),
1971 _("internal error"));
1976 #endif /* G_OS_WIN32 */
1979 g_local_file_make_directory (GFile
*file
,
1980 GCancellable
*cancellable
,
1983 GLocalFile
*local
= G_LOCAL_FILE (file
);
1985 if (g_mkdir (local
->filename
, 0755) == -1)
1989 if (errsv
== EINVAL
)
1990 /* This must be an invalid filename, on e.g. FAT */
1991 g_set_error (error
, G_IO_ERROR
,
1992 G_IO_ERROR_INVALID_FILENAME
,
1993 _("Invalid filename"));
1995 g_set_error (error
, G_IO_ERROR
,
1996 g_io_error_from_errno (errsv
),
1997 _("Error creating directory: %s"),
1998 g_strerror (errsv
));
2006 g_local_file_make_symbolic_link (GFile
*file
,
2007 const char *symlink_value
,
2008 GCancellable
*cancellable
,
2012 GLocalFile
*local
= G_LOCAL_FILE (file
);
2014 if (symlink (symlink_value
, local
->filename
) == -1)
2018 if (errsv
== EINVAL
)
2019 /* This must be an invalid filename, on e.g. FAT */
2020 g_set_error (error
, G_IO_ERROR
,
2021 G_IO_ERROR_INVALID_FILENAME
,
2022 _("Invalid filename"));
2024 g_set_error (error
, G_IO_ERROR
,
2025 g_io_error_from_errno (errsv
),
2026 _("Error making symbolic link: %s"),
2027 g_strerror (errsv
));
2032 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
, "Symlinks not supported");
2039 g_local_file_copy (GFile
*source
,
2041 GFileCopyFlags flags
,
2042 GCancellable
*cancellable
,
2043 GFileProgressCallback progress_callback
,
2044 gpointer progress_callback_data
,
2047 /* Fall back to default copy */
2048 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
, "Copy not supported");
2053 g_local_file_move (GFile
*source
,
2055 GFileCopyFlags flags
,
2056 GCancellable
*cancellable
,
2057 GFileProgressCallback progress_callback
,
2058 gpointer progress_callback_data
,
2061 GLocalFile
*local_source
, *local_destination
;
2062 struct stat statbuf
;
2063 gboolean destination_exist
, source_is_dir
;
2068 if (!G_IS_LOCAL_FILE (source
) ||
2069 !G_IS_LOCAL_FILE (destination
))
2071 /* Fall back to default move */
2072 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
, "Move not supported");
2076 local_source
= G_LOCAL_FILE (source
);
2077 local_destination
= G_LOCAL_FILE (destination
);
2079 res
= g_lstat (local_source
->filename
, &statbuf
);
2084 g_set_error (error
, G_IO_ERROR
,
2085 g_io_error_from_errno (errsv
),
2086 _("Error moving file: %s"),
2087 g_strerror (errsv
));
2091 source_is_dir
= S_ISDIR (statbuf
.st_mode
);
2092 source_size
= statbuf
.st_size
;
2094 destination_exist
= FALSE
;
2095 res
= g_lstat (local_destination
->filename
, &statbuf
);
2098 destination_exist
= TRUE
; /* Target file exists */
2100 if (flags
& G_FILE_COPY_OVERWRITE
)
2102 /* Always fail on dirs, even with overwrite */
2103 if (S_ISDIR (statbuf
.st_mode
))
2108 G_IO_ERROR_WOULD_MERGE
,
2109 _("Can't move directory over directory"));
2113 G_IO_ERROR_IS_DIRECTORY
,
2114 _("Can't copy over directory"));
2123 _("Target file exists"));
2128 if (flags
& G_FILE_COPY_BACKUP
&& destination_exist
)
2130 backup_name
= g_strconcat (local_destination
->filename
, "~", NULL
);
2131 if (g_rename (local_destination
->filename
, backup_name
) == -1)
2135 G_IO_ERROR_CANT_CREATE_BACKUP
,
2136 _("Backup file creation failed"));
2137 g_free (backup_name
);
2140 g_free (backup_name
);
2141 destination_exist
= FALSE
; /* It did, but no more */
2144 if (source_is_dir
&& destination_exist
&& (flags
& G_FILE_COPY_OVERWRITE
))
2146 /* Source is a dir, destination exists (and is not a dir, because that would have failed
2147 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2148 res
= g_unlink (local_destination
->filename
);
2153 g_set_error (error
, G_IO_ERROR
,
2154 g_io_error_from_errno (errsv
),
2155 _("Error removing target file: %s"),
2156 g_strerror (errsv
));
2161 if (g_rename (local_source
->filename
, local_destination
->filename
) == -1)
2166 /* This will cause the fallback code to run */
2167 g_set_error (error
, G_IO_ERROR
,
2168 G_IO_ERROR_NOT_SUPPORTED
,
2169 _("Move between mounts not supported"));
2170 else if (errsv
== EINVAL
)
2171 /* This must be an invalid filename, on e.g. FAT, or
2172 we're trying to move the file into itself...
2173 We return invalid filename for both... */
2174 g_set_error (error
, G_IO_ERROR
,
2175 G_IO_ERROR_INVALID_FILENAME
,
2176 _("Invalid filename"));
2178 g_set_error (error
, G_IO_ERROR
,
2179 g_io_error_from_errno (errsv
),
2180 _("Error moving file: %s"),
2181 g_strerror (errsv
));
2185 /* Make sure we send full copied size */
2186 if (progress_callback
)
2187 progress_callback (source_size
, source_size
, progress_callback_data
);
2192 static GFileMonitor
*
2193 g_local_file_monitor_dir (GFile
*file
,
2194 GFileMonitorFlags flags
,
2195 GCancellable
*cancellable
,
2198 GLocalFile
* local_file
= G_LOCAL_FILE(file
);
2199 return _g_local_directory_monitor_new (local_file
->filename
, flags
, error
);
2202 static GFileMonitor
*
2203 g_local_file_monitor_file (GFile
*file
,
2204 GFileMonitorFlags flags
,
2205 GCancellable
*cancellable
,
2208 GLocalFile
* local_file
= G_LOCAL_FILE(file
);
2209 return _g_local_file_monitor_new (local_file
->filename
, flags
, error
);
2213 g_local_file_file_iface_init (GFileIface
*iface
)
2215 iface
->dup
= g_local_file_dup
;
2216 iface
->hash
= g_local_file_hash
;
2217 iface
->equal
= g_local_file_equal
;
2218 iface
->is_native
= g_local_file_is_native
;
2219 iface
->has_uri_scheme
= g_local_file_has_uri_scheme
;
2220 iface
->get_uri_scheme
= g_local_file_get_uri_scheme
;
2221 iface
->get_basename
= g_local_file_get_basename
;
2222 iface
->get_path
= g_local_file_get_path
;
2223 iface
->get_uri
= g_local_file_get_uri
;
2224 iface
->get_parse_name
= g_local_file_get_parse_name
;
2225 iface
->get_parent
= g_local_file_get_parent
;
2226 iface
->prefix_matches
= g_local_file_prefix_matches
;
2227 iface
->get_relative_path
= g_local_file_get_relative_path
;
2228 iface
->resolve_relative_path
= g_local_file_resolve_relative_path
;
2229 iface
->get_child_for_display_name
= g_local_file_get_child_for_display_name
;
2230 iface
->set_display_name
= g_local_file_set_display_name
;
2231 iface
->enumerate_children
= g_local_file_enumerate_children
;
2232 iface
->query_info
= g_local_file_query_info
;
2233 iface
->query_filesystem_info
= g_local_file_query_filesystem_info
;
2234 iface
->find_enclosing_mount
= g_local_file_find_enclosing_mount
;
2235 iface
->query_settable_attributes
= g_local_file_query_settable_attributes
;
2236 iface
->query_writable_namespaces
= g_local_file_query_writable_namespaces
;
2237 iface
->set_attribute
= g_local_file_set_attribute
;
2238 iface
->set_attributes_from_info
= g_local_file_set_attributes_from_info
;
2239 iface
->read_fn
= g_local_file_read
;
2240 iface
->append_to
= g_local_file_append_to
;
2241 iface
->create
= g_local_file_create
;
2242 iface
->replace
= g_local_file_replace
;
2243 iface
->delete_file
= g_local_file_delete
;
2244 iface
->trash
= g_local_file_trash
;
2245 iface
->make_directory
= g_local_file_make_directory
;
2246 iface
->make_symbolic_link
= g_local_file_make_symbolic_link
;
2247 iface
->copy
= g_local_file_copy
;
2248 iface
->move
= g_local_file_move
;
2249 iface
->monitor_dir
= g_local_file_monitor_dir
;
2250 iface
->monitor_file
= g_local_file_monitor_file
;