Handle filesystems no supporting reporting how much is free. This fixes
[platform/upstream/glib.git] / gio / glocalfile.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #if HAVE_SYS_STATFS_H
35 #include <sys/statfs.h>
36 #endif
37 #if HAVE_SYS_STATVFS_H
38 #include <sys/statvfs.h>
39 #endif
40 #if HAVE_SYS_VFS_H
41 #include <sys/vfs.h>
42 #elif HAVE_SYS_MOUNT_H
43 #if HAVE_SYS_PARAM_H
44 #include <sys/param.h>
45 #endif
46 #include <sys/mount.h>
47 #endif
48
49 #ifndef O_BINARY
50 #define O_BINARY 0
51 #endif
52
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(HAVE_STRUCT_STATFS_F_BAVAIL)
57    /* on solaris and irix, statfs doesn't even have the
58       f_bavail field */
59 #  define USE_STATVFS
60 # else
61   /* at least on linux, statfs is the actual syscall */
62 #  define USE_STATFS
63 # endif
64
65 #elif defined(HAVE_STATFS)
66
67 # define USE_STATFS
68
69 #elif defined(HAVE_STATVFS)
70
71 # define USE_STATVFS
72
73 #endif
74
75 #include "gfileattribute.h"
76 #include "glocalfile.h"
77 #include "glocalfileinfo.h"
78 #include "glocalfileenumerator.h"
79 #include "glocalfileinputstream.h"
80 #include "glocalfileoutputstream.h"
81 #include "glocaldirectorymonitor.h"
82 #include "glocalfilemonitor.h"
83 #include "gmountprivate.h"
84 #include "gunixmounts.h"
85 #include "gioerror.h"
86 #include <glib/gstdio.h>
87 #include "glibintl.h"
88
89 #ifdef G_OS_WIN32
90 #define _WIN32_WINNT 0x0500
91 #include <windows.h>
92 #include <io.h>
93 #include <direct.h>
94
95 #ifndef FILE_READ_ONLY_VOLUME
96 #define FILE_READ_ONLY_VOLUME           0x00080000
97 #endif
98
99 #ifndef S_ISDIR
100 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
101 #endif
102 #ifndef S_ISLNK
103 #define S_ISLNK(m) (0)
104 #endif
105 #endif
106
107 #include "gioalias.h"
108
109 static void g_local_file_file_iface_init (GFileIface *iface);
110
111 static GFileAttributeInfoList *local_writable_attributes = NULL;
112 static GFileAttributeInfoList *local_writable_namespaces = NULL;
113
114 struct _GLocalFile
115 {
116   GObject parent_instance;
117
118   char *filename;
119 };
120
121 #define g_local_file_get_type _g_local_file_get_type
122 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
123                          G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
124                                                 g_local_file_file_iface_init))
125
126 static char *find_mountpoint_for (const char *file, dev_t dev);
127
128 static void
129 g_local_file_finalize (GObject *object)
130 {
131   GLocalFile *local;
132
133   local = G_LOCAL_FILE (object);
134
135   g_free (local->filename);
136
137   G_OBJECT_CLASS (g_local_file_parent_class)->finalize (object);
138 }
139
140 static void
141 g_local_file_class_init (GLocalFileClass *klass)
142 {
143   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
144   GFileAttributeInfoList *list;
145
146   gobject_class->finalize = g_local_file_finalize;
147
148   /* Set up attribute lists */
149
150   /* Writable attributes: */
151
152   list = g_file_attribute_info_list_new ();
153
154   g_file_attribute_info_list_add (list,
155                                   G_FILE_ATTRIBUTE_UNIX_MODE,
156                                   G_FILE_ATTRIBUTE_TYPE_UINT32,
157                                   G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
158                                   G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
159   
160 #ifdef HAVE_CHOWN
161   g_file_attribute_info_list_add (list,
162                                   G_FILE_ATTRIBUTE_UNIX_UID,
163                                   G_FILE_ATTRIBUTE_TYPE_UINT32,
164                                   G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
165   g_file_attribute_info_list_add (list,
166                                   G_FILE_ATTRIBUTE_UNIX_GID,
167                                   G_FILE_ATTRIBUTE_TYPE_UINT32,
168                                   G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
169 #endif
170   
171 #ifdef HAVE_SYMLINK
172   g_file_attribute_info_list_add (list,
173                                   G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
174                                   G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
175                                   0);
176 #endif
177   
178 #ifdef HAVE_UTIMES
179   g_file_attribute_info_list_add (list,
180                                   G_FILE_ATTRIBUTE_TIME_MODIFIED,
181                                   G_FILE_ATTRIBUTE_TYPE_UINT64,
182                                   G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
183                                   G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
184   g_file_attribute_info_list_add (list,
185                                   G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
186                                   G_FILE_ATTRIBUTE_TYPE_UINT32,
187                                   G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
188                                   G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
189   /* When copying, the target file is accessed. Replicating
190    * the source access time does not make sense in this case.
191    */
192   g_file_attribute_info_list_add (list,
193                                   G_FILE_ATTRIBUTE_TIME_ACCESS,
194                                   G_FILE_ATTRIBUTE_TYPE_UINT64,
195                                   G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
196   g_file_attribute_info_list_add (list,
197                                   G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
198                                   G_FILE_ATTRIBUTE_TYPE_UINT32,
199                                   G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
200 #endif
201
202   local_writable_attributes = list;
203
204   /* Writable namespaces: */
205   
206   list = g_file_attribute_info_list_new ();
207
208 #ifdef HAVE_XATTR
209   g_file_attribute_info_list_add (list,
210                                   "xattr",
211                                   G_FILE_ATTRIBUTE_TYPE_STRING,
212                                   G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
213                                   G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
214   g_file_attribute_info_list_add (list,
215                                   "xattr-sys",
216                                   G_FILE_ATTRIBUTE_TYPE_STRING,
217                                   G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
218 #endif
219
220   local_writable_namespaces = list;
221 }
222
223 static void
224 g_local_file_init (GLocalFile *local)
225 {
226 }
227
228
229 static char *
230 canonicalize_filename (const char *filename)
231 {
232   char *canon, *start, *p, *q;
233   char *cwd;
234   int i;
235   
236   if (!g_path_is_absolute (filename))
237     {
238       cwd = g_get_current_dir ();
239       canon = g_build_filename (cwd, filename, NULL);
240       g_free (cwd);
241     }
242   else
243     canon = g_strdup (filename);
244
245   start = (char *)g_path_skip_root (canon);
246
247   /* POSIX allows double slashes at the start to
248    * mean something special (as does windows too).
249    * So, "//" != "/", but more than two slashes
250    * is treated as "/".
251    */
252   i = 0;
253   for (p = start - 1;
254        (p >= canon) &&
255          G_IS_DIR_SEPARATOR (*p);
256        p--)
257     i++;
258   if (i > 2)
259     {
260       i -= 1;
261       start -= i;
262       memmove (start, start+i, strlen (start+i)+1);
263     }
264   
265   p = start;
266   while (*p != 0)
267     {
268       if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
269         {
270           memmove (p, p+1, strlen (p+1)+1);
271         }
272       else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
273         {
274           q = p + 2;
275           /* Skip previous separator */
276           p = p - 2;
277           if (p < start)
278             p = start;
279           while (p > start && !G_IS_DIR_SEPARATOR (*p))
280             p--;
281           if (G_IS_DIR_SEPARATOR (*p))
282             *p++ = G_DIR_SEPARATOR;
283           memmove (p, q, strlen (q)+1);
284         }
285       else
286         {
287           /* Skip until next separator */
288           while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
289             p++;
290           
291           if (*p != 0)
292             {
293               /* Canonicalize one separator */
294               *p++ = G_DIR_SEPARATOR;
295             }
296         }
297
298       /* Remove additional separators */
299       q = p;
300       while (*q && G_IS_DIR_SEPARATOR (*q))
301         q++;
302
303       if (p != q)
304         memmove (p, q, strlen (q)+1);
305     }
306
307   /* Remove trailing slashes */
308   if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
309     *(p-1) = 0;
310   
311   return canon;
312 }
313
314 /**
315  * _g_local_file_new:
316  * @filename: filename of the file to create.
317  * 
318  * Returns: new local #GFile.
319  **/
320 GFile *
321 _g_local_file_new (const char *filename)
322 {
323   GLocalFile *local;
324
325   local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
326   local->filename = canonicalize_filename (filename);
327   
328   return G_FILE (local);
329 }
330
331 static gboolean
332 g_local_file_is_native (GFile *file)
333 {
334   return TRUE;
335 }
336
337 static gboolean
338 g_local_file_has_uri_scheme (GFile      *file,
339                              const char *uri_scheme)
340 {
341   return g_ascii_strcasecmp (uri_scheme, "file") == 0;
342 }
343
344 static char *
345 g_local_file_get_uri_scheme (GFile *file)
346 {
347   return g_strdup ("file");
348 }
349
350 static char *
351 g_local_file_get_basename (GFile *file)
352 {
353   return g_path_get_basename (G_LOCAL_FILE (file)->filename);
354 }
355
356 static char *
357 g_local_file_get_path (GFile *file)
358 {
359   return g_strdup (G_LOCAL_FILE (file)->filename);
360 }
361
362 static char *
363 g_local_file_get_uri (GFile *file)
364 {
365   return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
366 }
367
368 static gboolean
369 get_filename_charset (const gchar **filename_charset)
370 {
371   const gchar **charsets;
372   gboolean is_utf8;
373   
374   is_utf8 = g_get_filename_charsets (&charsets);
375
376   if (filename_charset)
377     *filename_charset = charsets[0];
378   
379   return is_utf8;
380 }
381
382 static gboolean
383 name_is_valid_for_display (const char *string,
384                            gboolean    is_valid_utf8)
385 {
386   char c;
387
388   if (!is_valid_utf8 &&
389       !g_utf8_validate (string, -1, NULL))
390     return FALSE;
391
392   while ((c = *string++) != 0)
393     {
394       if (g_ascii_iscntrl (c))
395         return FALSE;
396     }
397
398   return TRUE;
399 }
400
401 static char *
402 g_local_file_get_parse_name (GFile *file)
403 {
404   const char *filename;
405   char *parse_name;
406   const gchar *charset;
407   char *utf8_filename;
408   char *roundtripped_filename;
409   gboolean free_utf8_filename;
410   gboolean is_valid_utf8;
411   char *escaped_path;
412   
413   filename = G_LOCAL_FILE (file)->filename;
414   if (get_filename_charset (&charset))
415     {
416       utf8_filename = (char *)filename;
417       free_utf8_filename = FALSE;
418       is_valid_utf8 = FALSE; /* Can't guarantee this */
419     }
420   else
421     {
422       utf8_filename = g_convert (filename, -1, 
423                                  "UTF-8", charset, NULL, NULL, NULL);
424       free_utf8_filename = TRUE;
425       is_valid_utf8 = TRUE;
426
427       if (utf8_filename != NULL)
428         {
429           /* Make sure we can roundtrip: */
430           roundtripped_filename = g_convert (utf8_filename, -1,
431                                              charset, "UTF-8", NULL, NULL, NULL);
432           
433           if (roundtripped_filename == NULL ||
434               strcmp (utf8_filename, roundtripped_filename) != 0)
435             {
436               g_free (utf8_filename);
437               utf8_filename = NULL;
438             }
439
440           g_free (roundtripped_filename);
441         }
442     }
443
444   if (utf8_filename != NULL &&
445       name_is_valid_for_display (utf8_filename, is_valid_utf8))
446     {
447       if (free_utf8_filename)
448         parse_name = utf8_filename;
449       else
450         parse_name = g_strdup (utf8_filename);
451     }
452   else
453     {
454       escaped_path = g_uri_escape_string (filename,
455                                           G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
456                                           TRUE);
457       parse_name = g_strconcat ("file://",
458                                 (*escaped_path != '/') ? "/" : "",
459                                 escaped_path,
460                                 NULL);
461       
462       g_free (escaped_path);
463
464       if (free_utf8_filename)
465         g_free (utf8_filename);
466     }
467   
468   return parse_name;
469 }
470
471 static GFile *
472 g_local_file_get_parent (GFile *file)
473 {
474   GLocalFile *local = G_LOCAL_FILE (file);
475   const char *non_root;
476   char *dirname;
477   GFile *parent;
478
479   /* Check for root */
480   non_root = g_path_skip_root (local->filename);
481   if (*non_root == 0)
482     return NULL;
483
484   dirname = g_path_get_dirname (local->filename);
485   parent = _g_local_file_new (dirname);
486   g_free (dirname);
487   return parent;
488 }
489
490 static GFile *
491 g_local_file_dup (GFile *file)
492 {
493   GLocalFile *local = G_LOCAL_FILE (file);
494
495   return _g_local_file_new (local->filename);
496 }
497
498 static guint
499 g_local_file_hash (GFile *file)
500 {
501   GLocalFile *local = G_LOCAL_FILE (file);
502   
503   return g_str_hash (local->filename);
504 }
505
506 static gboolean
507 g_local_file_equal (GFile *file1,
508                     GFile *file2)
509 {
510   GLocalFile *local1 = G_LOCAL_FILE (file1);
511   GLocalFile *local2 = G_LOCAL_FILE (file2);
512
513   return g_str_equal (local1->filename, local2->filename);
514 }
515
516 static const char *
517 match_prefix (const char *path, 
518               const char *prefix)
519 {
520   int prefix_len;
521
522   prefix_len = strlen (prefix);
523   if (strncmp (path, prefix, prefix_len) != 0)
524     return NULL;
525   
526   /* Handle the case where prefix is the root, so that
527    * the IS_DIR_SEPRARATOR check below works */
528   if (prefix_len > 0 &&
529       G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
530     prefix_len--;
531   
532   return path + prefix_len;
533 }
534
535 static gboolean
536 g_local_file_prefix_matches (GFile *parent,
537                              GFile *descendant)
538 {
539   GLocalFile *parent_local = G_LOCAL_FILE (parent);
540   GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
541   const char *remainder;
542
543   remainder = match_prefix (descendant_local->filename, parent_local->filename);
544   if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
545     return TRUE;
546   return FALSE;
547 }
548
549 static char *
550 g_local_file_get_relative_path (GFile *parent,
551                                 GFile *descendant)
552 {
553   GLocalFile *parent_local = G_LOCAL_FILE (parent);
554   GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
555   const char *remainder;
556
557   remainder = match_prefix (descendant_local->filename, parent_local->filename);
558   
559   if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
560     return g_strdup (remainder + 1);
561   return NULL;
562 }
563
564 static GFile *
565 g_local_file_resolve_relative_path (GFile      *file,
566                                     const char *relative_path)
567 {
568   GLocalFile *local = G_LOCAL_FILE (file);
569   char *filename;
570   GFile *child;
571
572   if (g_path_is_absolute (relative_path))
573     return _g_local_file_new (relative_path);
574   
575   filename = g_build_filename (local->filename, relative_path, NULL);
576   child = _g_local_file_new (filename);
577   g_free (filename);
578   
579   return child;
580 }
581
582 static GFileEnumerator *
583 g_local_file_enumerate_children (GFile                *file,
584                                  const char           *attributes,
585                                  GFileQueryInfoFlags   flags,
586                                  GCancellable         *cancellable,
587                                  GError              **error)
588 {
589   GLocalFile *local = G_LOCAL_FILE (file);
590   return _g_local_file_enumerator_new (local,
591                                        attributes, flags,
592                                        cancellable, error);
593 }
594
595 static GFile *
596 g_local_file_get_child_for_display_name (GFile        *file,
597                                          const char   *display_name,
598                                          GError      **error)
599 {
600   GFile *new_file;
601   char *basename;
602
603   basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
604   if (basename == NULL)
605     {
606       g_set_error (error, G_IO_ERROR,
607                    G_IO_ERROR_INVALID_FILENAME,
608                    _("Invalid filename %s"), display_name);
609       return NULL;
610     }
611
612   new_file = g_file_get_child (file, basename);
613   g_free (basename);
614   
615   return new_file;
616 }
617
618 #ifdef USE_STATFS
619 static const char *
620 get_fs_type (long f_type)
621 {
622   /* filesystem ids taken from linux manpage */
623   switch (f_type) 
624     {
625     case 0xadf5:
626       return "adfs";
627     case 0x5346414f:
628       return "afs";
629     case 0x0187:
630       return "autofs";
631     case 0xADFF:
632       return "affs";
633     case 0x42465331:
634       return "befs";
635     case 0x1BADFACE:
636       return "bfs";
637     case 0x9123683E:
638       return "btrfs";
639     case 0xFF534D42:
640       return "cifs";
641     case 0x73757245:
642       return "coda";
643     case 0x012FF7B7:
644       return "coh";
645     case 0x28cd3d45:
646       return "cramfs";
647     case 0x1373:
648       return "devfs";
649     case 0x00414A53:
650       return "efs";
651     case 0x137D:
652       return "ext";
653     case 0xEF51:
654       return "ext2";
655     case 0xEF53:
656       return "ext3";
657     case 0x4244:
658       return "hfs";
659     case 0xF995E849:
660       return "hpfs";
661     case 0x958458f6:
662       return "hugetlbfs";
663     case 0x9660:
664       return "isofs";
665     case 0x72b6:
666       return "jffs2";
667     case 0x3153464a:
668       return "jfs";
669     case 0x137F:
670       return "minix";
671     case 0x138F:
672       return "minix2";
673     case 0x2468:
674       return "minix2";
675     case 0x2478:
676       return "minix22";
677     case 0x4d44:
678       return "msdos";
679     case 0x564c:
680       return "ncp";
681     case 0x6969:
682       return "nfs";
683     case 0x5346544e:
684       return "ntfs";
685     case 0x9fa1:
686       return "openprom";
687     case 0x9fa0:
688       return "proc";
689     case 0x002f:
690       return "qnx4";
691     case 0x52654973:
692       return "reiserfs";
693     case 0x7275:
694       return "romfs";
695     case 0x517B:
696       return "smb";
697     case 0x73717368:
698       return "squashfs";
699     case 0x012FF7B6:
700       return "sysv2";
701     case 0x012FF7B5:
702       return "sysv4";
703     case 0x01021994:
704       return "tmpfs";
705     case 0x15013346:
706       return "udf";
707     case 0x00011954:
708       return "ufs";
709     case 0x9fa2:
710       return "usbdevice";
711     case 0xa501FCF5:
712       return "vxfs";
713     case 0x012FF7B4:
714       return "xenix";
715     case 0x58465342:
716       return "xfs";
717     case 0x012FD16D:
718       return "xiafs";
719     default:
720       return NULL;
721     }
722 }
723 #endif
724
725 #ifndef G_OS_WIN32
726
727 G_LOCK_DEFINE_STATIC(mount_info_hash);
728 static GHashTable *mount_info_hash = NULL;
729 static guint64 mount_info_hash_cache_time = 0;
730
731 typedef enum {
732   MOUNT_INFO_READONLY = 1<<0
733 } MountInfo;
734
735 static gboolean
736 device_equal (gconstpointer v1,
737               gconstpointer v2)
738 {
739   return *(dev_t *)v1 == *(dev_t *)v2;
740 }
741
742 static guint
743 device_hash (gconstpointer v)
744 {
745   return (guint) *(dev_t *)v;
746 }
747
748 static void
749 get_mount_info (GFileInfo             *fs_info,
750                 const char            *path,
751                 GFileAttributeMatcher *matcher)
752 {
753   struct stat buf;
754   gboolean got_info;
755   gpointer info_as_ptr;
756   guint mount_info;
757   char *mountpoint;
758   dev_t *dev;
759   GUnixMountEntry *mount;
760   guint64 cache_time;
761
762   if (g_lstat (path, &buf) != 0)
763     return;
764
765   G_LOCK (mount_info_hash);
766
767   if (mount_info_hash == NULL)
768     mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
769                                              g_free, NULL);
770
771
772   if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
773     g_hash_table_remove_all (mount_info_hash);
774   
775   got_info = g_hash_table_lookup_extended (mount_info_hash,
776                                            &buf.st_dev,
777                                            NULL,
778                                            &info_as_ptr);
779   
780   G_UNLOCK (mount_info_hash);
781   
782   mount_info = GPOINTER_TO_UINT (info_as_ptr);
783   
784   if (!got_info)
785     {
786       mount_info = 0;
787
788       mountpoint = find_mountpoint_for (path, buf.st_dev);
789       if (mountpoint == NULL)
790         mountpoint = g_strdup ("/");
791
792       mount = g_unix_mount_at (mountpoint, &cache_time);
793       if (mount)
794         {
795           if (g_unix_mount_is_readonly (mount))
796             mount_info |= MOUNT_INFO_READONLY;
797           
798           g_unix_mount_free (mount);
799         }
800
801       g_free (mountpoint);
802
803       dev = g_new0 (dev_t, 1);
804       *dev = buf.st_dev;
805       
806       G_LOCK (mount_info_hash);
807       mount_info_hash_cache_time = cache_time;
808       g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
809       G_UNLOCK (mount_info_hash);
810     }
811
812   if (mount_info & MOUNT_INFO_READONLY)
813     g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
814 }
815
816 #endif
817
818 #ifdef G_OS_WIN32
819
820 static gboolean
821 is_xp_or_later (void)
822 {
823   static int result = -1;
824
825   if (result == -1)
826     {
827 #ifndef _MSC_VER    
828       OSVERSIONINFOEX ver_info = {0};
829       DWORDLONG cond_mask = 0;
830       int op = VER_GREATER_EQUAL;
831
832       ver_info.dwOSVersionInfoSize = sizeof ver_info;
833       ver_info.dwMajorVersion = 5;
834       ver_info.dwMinorVersion = 1;
835
836       VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
837       VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
838
839       result = VerifyVersionInfo (&ver_info,
840                                   VER_MAJORVERSION | VER_MINORVERSION, 
841                                   cond_mask) != 0;
842 #else
843       result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;  
844 #endif
845     }
846
847   return result;
848 }
849
850 static wchar_t *
851 get_volume_for_path (const char *path)
852 {
853   long len;
854   wchar_t *wpath;
855   wchar_t *result;
856
857   wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
858   result = g_new (wchar_t, MAX_PATH);
859
860   if (!GetVolumePathNameW (wpath, result, MAX_PATH))
861     {
862       char *msg = g_win32_error_message (GetLastError ());
863       g_critical ("GetVolumePathName failed: %s", msg);
864       g_free (msg);
865       g_free (result);
866       g_free (wpath);
867       return NULL;
868     }
869
870   len = wcslen (result);
871   if (len > 0 && result[len-1] != L'\\')
872     {
873       result = g_renew (wchar_t, result, len + 2);
874       result[len] = L'\\';
875       result[len + 1] = 0;
876     }
877
878   g_free (wpath);
879   return result;
880 }
881
882 static char *
883 find_mountpoint_for (const char *file, dev_t dev)
884 {
885   wchar_t *wpath;
886   char *utf8_path;
887
888   wpath = get_volume_for_path (file);
889   if (!wpath)
890     return NULL;
891
892   utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
893
894   g_free (wpath);
895   return utf8_path;
896 }
897
898 static void
899 get_filesystem_readonly (GFileInfo  *info,
900                          const char *path)
901 {
902   wchar_t *rootdir;
903
904   rootdir = get_volume_for_path (path);
905
906   if (rootdir)
907     {
908       if (is_xp_or_later ())
909         {
910           DWORD flags;
911           if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
912             g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
913                                                (flags & FILE_READ_ONLY_VOLUME) != 0);
914         }
915       else
916         {
917           if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
918             g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
919         }
920     }
921
922   g_free (rootdir);
923 }
924
925 #endif /* G_OS_WIN32 */
926
927 static GFileInfo *
928 g_local_file_query_filesystem_info (GFile         *file,
929                                     const char    *attributes,
930                                     GCancellable  *cancellable,
931                                     GError       **error)
932 {
933   GLocalFile *local = G_LOCAL_FILE (file);
934   GFileInfo *info;
935   int statfs_result = 0;
936   gboolean no_size;
937 #ifndef G_OS_WIN32
938   guint64 block_size;
939   const char *fstype;
940 #ifdef USE_STATFS
941   struct statfs statfs_buffer;
942 #elif defined(USE_STATVFS)
943   struct statvfs statfs_buffer;
944 #endif
945 #endif
946   GFileAttributeMatcher *attribute_matcher;
947         
948   no_size = FALSE;
949   
950 #ifdef USE_STATFS
951   
952 #if STATFS_ARGS == 2
953   statfs_result = statfs (local->filename, &statfs_buffer);
954 #elif STATFS_ARGS == 4
955   statfs_result = statfs (local->filename, &statfs_buffer,
956                           sizeof (statfs_buffer), 0);
957 #endif
958   block_size = statfs_buffer.f_bsize;
959   
960   /* Many backends can't report free size (for instance the gvfs fuse
961      backend for backend not supporting this), and set f_bfree to 0,
962      but it can be 0 for real too. We treat the availible == 0 and
963      free == 0 case as "both of these are invalid".
964    */
965 #ifndef G_OS_WIN32
966   if (statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0)
967     no_size = TRUE;
968 #endif
969   
970 #elif defined(USE_STATVFS)
971   statfs_result = statvfs (local->filename, &statfs_buffer);
972   block_size = statfs_buffer.f_frsize; 
973 #endif
974
975   if (statfs_result == -1)
976     {
977       int errsv = errno;
978
979       g_set_error (error, G_IO_ERROR,
980                    g_io_error_from_errno (errsv),
981                    _("Error getting filesystem info: %s"),
982                    g_strerror (errsv));
983       return NULL;
984     }
985
986   info = g_file_info_new ();
987
988   attribute_matcher = g_file_attribute_matcher_new (attributes);
989   
990   if (!no_size &&
991       g_file_attribute_matcher_matches (attribute_matcher,
992                                         G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
993     {
994 #ifdef G_OS_WIN32
995       gchar *localdir = g_path_get_dirname (local->filename);
996       wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
997       ULARGE_INTEGER li;
998       
999       g_free (localdir);
1000       if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
1001         g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
1002       g_free (wdirname);
1003 #else
1004       g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
1005 #endif
1006     }
1007   if (!no_size &&
1008       g_file_attribute_matcher_matches (attribute_matcher,
1009                                         G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
1010     {
1011 #ifdef G_OS_WIN32
1012       gchar *localdir = g_path_get_dirname (local->filename);
1013       wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1014       ULARGE_INTEGER li;
1015       
1016       g_free (localdir);
1017       if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
1018         g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE,  (guint64)li.QuadPart);
1019       g_free (wdirname);
1020 #else
1021       g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
1022 #endif
1023     }
1024 #ifdef USE_STATFS
1025 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1026   fstype = g_strdup(statfs_buffer.f_fstypename);
1027 #else
1028   fstype = get_fs_type (statfs_buffer.f_type);
1029 #endif
1030
1031 #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1032   fstype = g_strdup(statfs_buffer.f_basetype); 
1033 #endif
1034
1035 #ifndef G_OS_WIN32
1036   if (fstype &&
1037       g_file_attribute_matcher_matches (attribute_matcher,
1038                                         G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1039     g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1040 #endif  
1041
1042   if (g_file_attribute_matcher_matches (attribute_matcher,
1043                                         G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
1044     {
1045 #ifdef G_OS_WIN32
1046       get_filesystem_readonly (info, local->filename);
1047 #else
1048       get_mount_info (info, local->filename, attribute_matcher);
1049 #endif
1050     }
1051   
1052   g_file_attribute_matcher_unref (attribute_matcher);
1053   
1054   return info;
1055 }
1056
1057 static GMount *
1058 g_local_file_find_enclosing_mount (GFile         *file,
1059                                    GCancellable  *cancellable,
1060                                    GError       **error)
1061 {
1062   GLocalFile *local = G_LOCAL_FILE (file);
1063   struct stat buf;
1064   char *mountpoint;
1065   GMount *mount;
1066
1067   if (g_lstat (local->filename, &buf) != 0)
1068     {
1069       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1070                       /* Translators: This is an error message when trying to
1071                        * find the enclosing (user visible) mount of a file, but
1072                        * none exists. */
1073                       _("Containing mount does not exist"));
1074       return NULL;
1075     }
1076
1077   mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
1078   if (mountpoint == NULL)
1079     {
1080       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1081                       /* Translators: This is an error message when trying to
1082                        * find the enclosing (user visible) mount of a file, but
1083                        * none exists. */
1084                       _("Containing mount does not exist"));
1085       return NULL;
1086     }
1087
1088   mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1089   g_free (mountpoint);
1090   if (mount)
1091     return mount;
1092
1093   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1094                   /* Translators: This is an error message when trying to find
1095                    * the enclosing (user visible) mount of a file, but none
1096                    * exists. */
1097                   _("Containing mount does not exist"));
1098   return NULL;
1099 }
1100
1101 static GFile *
1102 g_local_file_set_display_name (GFile         *file,
1103                                const char    *display_name,
1104                                GCancellable  *cancellable,
1105                                GError       **error)
1106 {
1107   GLocalFile *local, *new_local;
1108   GFile *new_file, *parent;
1109   struct stat statbuf;
1110   int errsv;
1111
1112   parent = g_file_get_parent (file);
1113   if (parent == NULL)
1114     {
1115       g_set_error_literal (error, G_IO_ERROR,
1116                            G_IO_ERROR_FAILED,
1117                            _("Can't rename root directory"));
1118       return NULL;
1119     }
1120   
1121   new_file = g_file_get_child_for_display_name (parent, display_name, error);
1122   g_object_unref (parent);
1123   
1124   if (new_file == NULL)
1125     return NULL;
1126   local = G_LOCAL_FILE (file);
1127   new_local = G_LOCAL_FILE (new_file);
1128
1129   if (g_lstat (new_local->filename, &statbuf) == -1) 
1130     {
1131       errsv = errno;
1132
1133       if (errsv != ENOENT)
1134         {
1135           g_set_error (error, G_IO_ERROR,
1136                        g_io_error_from_errno (errsv),
1137                        _("Error renaming file: %s"),
1138                        g_strerror (errsv));
1139           return NULL;
1140         }
1141     }
1142   else
1143     {
1144       g_set_error_literal (error, G_IO_ERROR,
1145                            G_IO_ERROR_EXISTS,
1146                            _("Can't rename file, filename already exist"));
1147       return NULL;
1148     }
1149
1150   if (g_rename (local->filename, new_local->filename) == -1)
1151     {
1152       errsv = errno;
1153
1154       if (errsv == EINVAL)
1155         /* We can't get a rename file into itself error herer,
1156            so this must be an invalid filename, on e.g. FAT */
1157         g_set_error_literal (error, G_IO_ERROR,
1158                              G_IO_ERROR_INVALID_FILENAME,
1159                              _("Invalid filename"));
1160       else
1161         g_set_error (error, G_IO_ERROR,
1162                      g_io_error_from_errno (errsv),
1163                      _("Error renaming file: %s"),
1164                      g_strerror (errsv));
1165       g_object_unref (new_file);
1166       return NULL;
1167     }
1168   
1169   return new_file;
1170 }
1171
1172 static GFileInfo *
1173 g_local_file_query_info (GFile                *file,
1174                          const char           *attributes,
1175                          GFileQueryInfoFlags   flags,
1176                          GCancellable         *cancellable,
1177                          GError              **error)
1178 {
1179   GLocalFile *local = G_LOCAL_FILE (file);
1180   GFileInfo *info;
1181   GFileAttributeMatcher *matcher;
1182   char *basename, *dirname;
1183   GLocalParentFileInfo parent_info;
1184
1185   matcher = g_file_attribute_matcher_new (attributes);
1186   
1187   basename = g_path_get_basename (local->filename);
1188   
1189   dirname = g_path_get_dirname (local->filename);
1190   _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1191   g_free (dirname);
1192   
1193   info = _g_local_file_info_get (basename, local->filename,
1194                                  matcher, flags, &parent_info,
1195                                  error);
1196   
1197   g_free (basename);
1198
1199   g_file_attribute_matcher_unref (matcher);
1200
1201   return info;
1202 }
1203
1204 static GFileAttributeInfoList *
1205 g_local_file_query_settable_attributes (GFile         *file,
1206                                         GCancellable  *cancellable,
1207                                         GError       **error)
1208 {
1209   return g_file_attribute_info_list_ref (local_writable_attributes);
1210 }
1211
1212 static GFileAttributeInfoList *
1213 g_local_file_query_writable_namespaces (GFile         *file,
1214                                         GCancellable  *cancellable,
1215                                         GError       **error)
1216 {
1217   return g_file_attribute_info_list_ref (local_writable_namespaces);
1218 }
1219
1220 static gboolean
1221 g_local_file_set_attribute (GFile                *file,
1222                             const char           *attribute,
1223                             GFileAttributeType    type,
1224                             gpointer              value_p,
1225                             GFileQueryInfoFlags   flags,
1226                             GCancellable         *cancellable,
1227                             GError              **error)
1228 {
1229   GLocalFile *local = G_LOCAL_FILE (file);
1230
1231   return _g_local_file_info_set_attribute (local->filename,
1232                                            attribute,
1233                                            type,
1234                                            value_p,
1235                                            flags,
1236                                            cancellable,
1237                                            error);
1238 }
1239
1240 static gboolean
1241 g_local_file_set_attributes_from_info (GFile                *file,
1242                                        GFileInfo            *info,
1243                                        GFileQueryInfoFlags   flags,
1244                                        GCancellable         *cancellable,
1245                                        GError              **error)
1246 {
1247   GLocalFile *local = G_LOCAL_FILE (file);
1248   int res, chained_res;
1249   GFileIface *default_iface;
1250
1251   res = _g_local_file_info_set_attributes (local->filename,
1252                                            info, flags, 
1253                                            cancellable,
1254                                            error);
1255
1256   if (!res)
1257     error = NULL; /* Don't write over error if further errors */
1258
1259   default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1260
1261   chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1262   
1263   return res && chained_res;
1264 }
1265
1266 static GFileInputStream *
1267 g_local_file_read (GFile         *file,
1268                    GCancellable  *cancellable,
1269                    GError       **error)
1270 {
1271   GLocalFile *local = G_LOCAL_FILE (file);
1272   int fd;
1273   struct stat buf;
1274   
1275   fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1276   if (fd == -1)
1277     {
1278       int errsv = errno;
1279
1280       g_set_error (error, G_IO_ERROR,
1281                    g_io_error_from_errno (errsv),
1282                    _("Error opening file: %s"),
1283                    g_strerror (errsv));
1284       return NULL;
1285     }
1286
1287   if (fstat(fd, &buf) == 0 && S_ISDIR (buf.st_mode))
1288     {
1289       close (fd);
1290       g_set_error_literal (error, G_IO_ERROR,
1291                            G_IO_ERROR_IS_DIRECTORY,
1292                            _("Can't open directory"));
1293       return NULL;
1294     }
1295   
1296   return _g_local_file_input_stream_new (fd);
1297 }
1298
1299 static GFileOutputStream *
1300 g_local_file_append_to (GFile             *file,
1301                         GFileCreateFlags   flags,
1302                         GCancellable      *cancellable,
1303                         GError           **error)
1304 {
1305   return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1306                                              flags, cancellable, error);
1307 }
1308
1309 static GFileOutputStream *
1310 g_local_file_create (GFile             *file,
1311                      GFileCreateFlags   flags,
1312                      GCancellable      *cancellable,
1313                      GError           **error)
1314 {
1315   return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1316                                              flags, cancellable, error);
1317 }
1318
1319 static GFileOutputStream *
1320 g_local_file_replace (GFile             *file,
1321                       const char        *etag,
1322                       gboolean           make_backup,
1323                       GFileCreateFlags   flags,
1324                       GCancellable      *cancellable,
1325                       GError           **error)
1326 {
1327   return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1328                                               etag, make_backup, flags,
1329                                               cancellable, error);
1330 }
1331
1332
1333 static gboolean
1334 g_local_file_delete (GFile         *file,
1335                      GCancellable  *cancellable,
1336                      GError       **error)
1337 {
1338   GLocalFile *local = G_LOCAL_FILE (file);
1339   
1340   if (g_remove (local->filename) == -1)
1341     {
1342       int errsv = errno;
1343
1344       /* Posix allows EEXIST too, but the more sane error
1345          is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1346          expects */
1347       if (errsv == EEXIST)
1348         errsv = ENOTEMPTY;
1349
1350       g_set_error (error, G_IO_ERROR,
1351                    g_io_error_from_errno (errsv),
1352                    _("Error removing file: %s"),
1353                    g_strerror (errsv));
1354       return FALSE;
1355     }
1356   
1357   return TRUE;
1358 }
1359
1360 static char *
1361 strip_trailing_slashes (const char *path)
1362 {
1363   char *path_copy;
1364   int len;
1365
1366   path_copy = g_strdup (path);
1367   len = strlen (path_copy);
1368   while (len > 1 && path_copy[len-1] == '/')
1369     path_copy[--len] = 0;
1370
1371   return path_copy;
1372  }
1373
1374 static char *
1375 expand_symlink (const char *link)
1376 {
1377   char *resolved, *canonical, *parent, *link2;
1378   char symlink_value[4096];
1379 #ifdef G_OS_WIN32
1380 #else
1381   ssize_t res;
1382 #endif
1383   
1384 #ifdef G_OS_WIN32
1385 #else
1386   res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1387   
1388   if (res == -1)
1389     return g_strdup (link);
1390   symlink_value[res] = 0;
1391 #endif
1392   
1393   if (g_path_is_absolute (symlink_value))
1394     return canonicalize_filename (symlink_value);
1395   else
1396     {
1397       link2 = strip_trailing_slashes (link);
1398       parent = g_path_get_dirname (link2);
1399       g_free (link2);
1400       
1401       resolved = g_build_filename (parent, symlink_value, NULL);
1402       g_free (parent);
1403       
1404       canonical = canonicalize_filename (resolved);
1405       
1406       g_free (resolved);
1407
1408       return canonical;
1409     }
1410 }
1411
1412 static char *
1413 get_parent (const char *path, 
1414             dev_t      *parent_dev)
1415 {
1416   char *parent, *tmp;
1417   struct stat parent_stat;
1418   int num_recursions;
1419   char *path_copy;
1420
1421   path_copy = strip_trailing_slashes (path);
1422   
1423   parent = g_path_get_dirname (path_copy);
1424   if (strcmp (parent, ".") == 0 ||
1425       strcmp (parent, path_copy) == 0)
1426     {
1427       g_free (parent);
1428       g_free (path_copy);
1429       return NULL;
1430     }
1431   g_free (path_copy);
1432
1433   num_recursions = 0;
1434   do {
1435     if (g_lstat (parent, &parent_stat) != 0)
1436       {
1437         g_free (parent);
1438         return NULL;
1439       }
1440     
1441     if (S_ISLNK (parent_stat.st_mode))
1442       {
1443         tmp = parent;
1444         parent = expand_symlink (parent);
1445         g_free (tmp);
1446       }
1447     
1448     num_recursions++;
1449     if (num_recursions > 12)
1450       {
1451         g_free (parent);
1452         return NULL;
1453       }
1454   } while (S_ISLNK (parent_stat.st_mode));
1455
1456   *parent_dev = parent_stat.st_dev;
1457   
1458   return parent;
1459 }
1460
1461 static char *
1462 expand_all_symlinks (const char *path)
1463 {
1464   char *parent, *parent_expanded;
1465   char *basename, *res;
1466   dev_t parent_dev;
1467
1468   parent = get_parent (path, &parent_dev);
1469   if (parent)
1470     {
1471       parent_expanded = expand_all_symlinks (parent);
1472       g_free (parent);
1473       basename = g_path_get_basename (path);
1474       res = g_build_filename (parent_expanded, basename, NULL);
1475       g_free (basename);
1476       g_free (parent_expanded);
1477     }
1478   else
1479     res = g_strdup (path);
1480   
1481   return res;
1482 }
1483
1484 #ifndef G_OS_WIN32
1485
1486 static char *
1487 find_mountpoint_for (const char *file, 
1488                      dev_t       dev)
1489 {
1490   char *dir, *parent;
1491   dev_t dir_dev, parent_dev;
1492
1493   dir = g_strdup (file);
1494   dir_dev = dev;
1495
1496   while (1) 
1497     {
1498       parent = get_parent (dir, &parent_dev);
1499       if (parent == NULL)
1500         return dir;
1501     
1502       if (parent_dev != dir_dev)
1503         {
1504           g_free (parent);
1505           return dir;
1506         }
1507     
1508       g_free (dir);
1509       dir = parent;
1510     }
1511 }
1512
1513 static char *
1514 find_topdir_for (const char *file)
1515 {
1516   char *dir;
1517   dev_t dir_dev;
1518
1519   dir = get_parent (file, &dir_dev);
1520   if (dir == NULL)
1521     return NULL;
1522
1523   return find_mountpoint_for (dir, dir_dev);
1524 }
1525
1526 static char *
1527 get_unique_filename (const char *basename, 
1528                      int         id)
1529 {
1530   const char *dot;
1531       
1532   if (id == 1)
1533     return g_strdup (basename);
1534
1535   dot = strchr (basename, '.');
1536   if (dot)
1537     return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1538   else
1539     return g_strdup_printf ("%s.%d", basename, id);
1540 }
1541
1542 static gboolean
1543 path_has_prefix (const char *path, 
1544                  const char *prefix)
1545 {
1546   int prefix_len;
1547
1548   if (prefix == NULL)
1549     return TRUE;
1550
1551   prefix_len = strlen (prefix);
1552   
1553   if (strncmp (path, prefix, prefix_len) == 0 &&
1554       (prefix_len == 0 || /* empty prefix always matches */
1555        prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1556        path[prefix_len] == 0 ||
1557        path[prefix_len] == '/'))
1558     return TRUE;
1559   
1560   return FALSE;
1561 }
1562
1563 static char *
1564 try_make_relative (const char *path, 
1565                    const char *base)
1566 {
1567   char *path2, *base2;
1568   char *relative;
1569
1570   path2 = expand_all_symlinks (path);
1571   base2 = expand_all_symlinks (base);
1572
1573   relative = NULL;
1574   if (path_has_prefix (path2, base2))
1575     {
1576       relative = path2 + strlen (base2);
1577       while (*relative == '/')
1578         relative ++;
1579       relative = g_strdup (relative);
1580     }
1581   g_free (path2);
1582   g_free (base2);
1583
1584   if (relative)
1585     return relative;
1586   
1587   /* Failed, use abs path */
1588   return g_strdup (path);
1589 }
1590
1591 static char *
1592 escape_trash_name (char *name)
1593 {
1594   GString *str;
1595   const gchar hex[16] = "0123456789ABCDEF";
1596   
1597   str = g_string_new ("");
1598
1599   while (*name != 0)
1600     {
1601       char c;
1602
1603       c = *name++;
1604
1605       if (g_ascii_isprint (c))
1606         g_string_append_c (str, c);
1607       else
1608         {
1609           g_string_append_c (str, '%');
1610           g_string_append_c (str, hex[((guchar)c) >> 4]);
1611           g_string_append_c (str, hex[((guchar)c) & 0xf]);
1612         }
1613     }
1614
1615   return g_string_free (str, FALSE);
1616 }
1617
1618 gboolean
1619 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1620 {
1621   static gsize home_dev_set = 0;
1622   static dev_t home_dev;
1623   char *topdir, *globaldir, *trashdir, *tmpname;
1624   uid_t uid;
1625   char uid_str[32];
1626   struct stat global_stat, trash_stat;
1627   gboolean res;
1628   int statres;
1629       
1630   if (g_once_init_enter (&home_dev_set))
1631     {
1632       struct stat home_stat;
1633       
1634       g_stat (g_get_home_dir (), &home_stat);
1635       home_dev = home_stat.st_dev;
1636       g_once_init_leave (&home_dev_set, 1);
1637     }
1638
1639   /* Assume we can trash to the home */
1640   if (dir_dev == home_dev)
1641     return TRUE;
1642
1643   topdir = find_mountpoint_for (dirname, dir_dev);
1644   if (topdir == NULL)
1645     return FALSE;
1646
1647   globaldir = g_build_filename (topdir, ".Trash", NULL); 
1648   statres = g_lstat (globaldir, &global_stat);
1649  if (g_lstat (globaldir, &global_stat) == 0 &&
1650       S_ISDIR (global_stat.st_mode) &&
1651       (global_stat.st_mode & S_ISVTX) != 0)
1652     {
1653       /* got a toplevel sysadmin created dir, assume we
1654        * can trash to it (we should be able to create a dir)
1655        * This fails for the FAT case where the ownership of
1656        * that dir would be wrong though..
1657        */
1658       g_free (globaldir);
1659       g_free (topdir);
1660       return TRUE;
1661     }
1662   g_free (globaldir);
1663
1664   /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1665   uid = geteuid ();
1666   g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1667   
1668   tmpname = g_strdup_printf (".Trash-%s", uid_str);
1669   trashdir = g_build_filename (topdir, tmpname, NULL);
1670   g_free (tmpname);
1671
1672   if (g_lstat (trashdir, &trash_stat) == 0)
1673     {
1674       g_free (topdir);
1675       g_free (trashdir);
1676       return
1677         S_ISDIR (trash_stat.st_mode) &&
1678         trash_stat.st_uid == uid;
1679     }
1680   g_free (trashdir);
1681
1682   /* User specific trash didn't exist, can we create it? */
1683   res = g_access (topdir, W_OK) == 0;
1684   
1685   g_free (topdir);
1686   
1687   return res;
1688 }
1689
1690
1691 static gboolean
1692 g_local_file_trash (GFile         *file,
1693                     GCancellable  *cancellable,
1694                     GError       **error)
1695 {
1696   GLocalFile *local = G_LOCAL_FILE (file);
1697   struct stat file_stat, home_stat;
1698   const char *homedir;
1699   char *trashdir, *topdir, *infodir, *filesdir;
1700   char *basename, *trashname, *trashfile, *infoname, *infofile;
1701   char *original_name, *original_name_escaped;
1702   int i;
1703   char *data;
1704   gboolean is_homedir_trash;
1705   char delete_time[32];
1706   int fd;
1707   struct stat trash_stat, global_stat;
1708   char *dirname, *globaldir;
1709   
1710   if (g_lstat (local->filename, &file_stat) != 0)
1711     {
1712       int errsv = errno;
1713
1714       g_set_error (error, G_IO_ERROR,
1715                    g_io_error_from_errno (errsv),
1716                    _("Error trashing file: %s"),
1717                    g_strerror (errsv));
1718       return FALSE;
1719     }
1720     
1721   homedir = g_get_home_dir ();
1722   g_stat (homedir, &home_stat);
1723
1724   is_homedir_trash = FALSE;
1725   trashdir = NULL;
1726   if (file_stat.st_dev == home_stat.st_dev)
1727     {
1728       is_homedir_trash = TRUE;
1729       errno = 0;
1730       trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1731       if (g_mkdir_with_parents (trashdir, 0700) < 0)
1732         {
1733           char *display_name;
1734           int errsv = errno;
1735
1736           display_name = g_filename_display_name (trashdir);
1737           g_set_error (error, G_IO_ERROR,
1738                        g_io_error_from_errno (errsv),
1739                        _("Unable to create trash dir %s: %s"),
1740                        display_name, g_strerror (errsv));
1741           g_free (display_name);
1742           g_free (trashdir);
1743           return FALSE;
1744         }
1745       topdir = g_strdup (g_get_user_data_dir ());
1746     }
1747   else
1748     {
1749       uid_t uid;
1750       char uid_str[32];
1751
1752       uid = geteuid ();
1753       g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1754       
1755       topdir = find_topdir_for (local->filename);
1756       if (topdir == NULL)
1757         {
1758           g_set_error_literal (error, G_IO_ERROR,
1759                                G_IO_ERROR_NOT_SUPPORTED,
1760                                _("Unable to find toplevel directory for trash"));
1761           return FALSE;
1762         }
1763       
1764       /* Try looking for global trash dir $topdir/.Trash/$uid */
1765       globaldir = g_build_filename (topdir, ".Trash", NULL);
1766       if (g_lstat (globaldir, &global_stat) == 0 &&
1767           S_ISDIR (global_stat.st_mode) &&
1768           (global_stat.st_mode & S_ISVTX) != 0)
1769         {
1770           trashdir = g_build_filename (globaldir, uid_str, NULL);
1771
1772           if (g_lstat (trashdir, &trash_stat) == 0)
1773             {
1774               if (!S_ISDIR (trash_stat.st_mode) ||
1775                   trash_stat.st_uid != uid)
1776                 {
1777                   /* Not a directory or not owned by user, ignore */
1778                   g_free (trashdir);
1779                   trashdir = NULL;
1780                 }
1781             }
1782           else if (g_mkdir (trashdir, 0700) == -1)
1783             {
1784               g_free (trashdir);
1785               trashdir = NULL;
1786             }
1787         }
1788       g_free (globaldir);
1789
1790       if (trashdir == NULL)
1791         {
1792           gboolean tried_create;
1793           
1794           /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1795           dirname = g_strdup_printf (".Trash-%s", uid_str);
1796           trashdir = g_build_filename (topdir, dirname, NULL);
1797           g_free (dirname);
1798
1799           tried_create = FALSE;
1800
1801         retry:
1802           if (g_lstat (trashdir, &trash_stat) == 0)
1803             {
1804               if (!S_ISDIR (trash_stat.st_mode) ||
1805                   trash_stat.st_uid != uid)
1806                 {
1807                   /* Remove the failed directory */
1808                   if (tried_create)
1809                     g_remove (trashdir);
1810                   
1811                   /* Not a directory or not owned by user, ignore */
1812                   g_free (trashdir);
1813                   trashdir = NULL;
1814                 }
1815             }
1816           else
1817             {
1818               if (!tried_create &&
1819                   g_mkdir (trashdir, 0700) != -1)
1820                 {
1821                   /* Ensure that the created dir has the right uid etc.
1822                      This might fail on e.g. a FAT dir */
1823                   tried_create = TRUE;
1824                   goto retry;
1825                 }
1826               else
1827                 {
1828                   g_free (trashdir);
1829                   trashdir = NULL;
1830                 }
1831             }
1832         }
1833
1834       if (trashdir == NULL)
1835         {
1836           g_free (topdir);
1837           g_set_error_literal (error, G_IO_ERROR,
1838                                G_IO_ERROR_NOT_SUPPORTED,
1839                                _("Unable to find or create trash directory"));
1840           return FALSE;
1841         }
1842     }
1843
1844   /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1845
1846   infodir = g_build_filename (trashdir, "info", NULL);
1847   filesdir = g_build_filename (trashdir, "files", NULL);
1848   g_free (trashdir);
1849
1850   /* Make sure we have the subdirectories */
1851   if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1852       (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
1853     {
1854       g_free (topdir);
1855       g_free (infodir);
1856       g_free (filesdir);
1857       g_set_error_literal (error, G_IO_ERROR,
1858                            G_IO_ERROR_NOT_SUPPORTED,
1859                            _("Unable to find or create trash directory"));
1860       return FALSE;
1861     }  
1862
1863   basename = g_path_get_basename (local->filename);
1864   i = 1;
1865   trashname = NULL;
1866   infofile = NULL;
1867   do {
1868     g_free (trashname);
1869     g_free (infofile);
1870     
1871     trashname = get_unique_filename (basename, i++);
1872     infoname = g_strconcat (trashname, ".trashinfo", NULL);
1873     infofile = g_build_filename (infodir, infoname, NULL);
1874     g_free (infoname);
1875
1876     fd = open (infofile, O_CREAT | O_EXCL, 0666);
1877   } while (fd == -1 && errno == EEXIST);
1878
1879   g_free (basename);
1880   g_free (infodir);
1881
1882   if (fd == -1)
1883     {
1884       int errsv = errno;
1885
1886       g_free (filesdir);
1887       g_free (topdir);
1888       g_free (trashname);
1889       g_free (infofile);
1890       
1891       g_set_error (error, G_IO_ERROR,
1892                    g_io_error_from_errno (errsv),
1893                    _("Unable to create trashing info file: %s"),
1894                    g_strerror (errsv));
1895       return FALSE;
1896     }
1897
1898   close (fd);
1899
1900   /* TODO: Maybe we should verify that you can delete the file from the trash
1901      before moving it? OTOH, that is hard, as it needs a recursive scan */
1902
1903   trashfile = g_build_filename (filesdir, trashname, NULL);
1904
1905   g_free (filesdir);
1906
1907   if (g_rename (local->filename, trashfile) == -1)
1908     {
1909       int errsv = errno;
1910
1911       g_free (topdir);
1912       g_free (trashname);
1913       g_free (infofile);
1914       g_free (trashfile);
1915       
1916       g_set_error (error, G_IO_ERROR,
1917                    g_io_error_from_errno (errsv),
1918                    _("Unable to trash file: %s"),
1919                    g_strerror (errsv));
1920       return FALSE;
1921     }
1922
1923   g_free (trashfile);
1924
1925   /* TODO: Do we need to update mtime/atime here after the move? */
1926
1927   /* Use absolute names for homedir */
1928   if (is_homedir_trash)
1929     original_name = g_strdup (local->filename);
1930   else
1931     original_name = try_make_relative (local->filename, topdir);
1932   original_name_escaped = escape_trash_name (original_name);
1933   
1934   g_free (original_name);
1935   g_free (topdir);
1936   
1937   {
1938     time_t t;
1939     struct tm now;
1940     t = time (NULL);
1941     localtime_r (&t, &now);
1942     delete_time[0] = 0;
1943     strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
1944   }
1945
1946   data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
1947                           original_name_escaped, delete_time);
1948
1949   g_file_set_contents (infofile, data, -1, NULL);
1950   g_free (infofile);
1951   g_free (data);
1952   
1953   g_free (original_name_escaped);
1954   g_free (trashname);
1955   
1956   return TRUE;
1957 }
1958 #else /* G_OS_WIN32 */
1959 gboolean
1960 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1961 {
1962   return FALSE;                 /* XXX ??? */
1963 }
1964
1965 static gboolean
1966 g_local_file_trash (GFile         *file,
1967                     GCancellable  *cancellable,
1968                     GError       **error)
1969 {
1970   GLocalFile *local = G_LOCAL_FILE (file);
1971   SHFILEOPSTRUCTW op = {0};
1972   gboolean success;
1973   wchar_t *wfilename;
1974   long len;
1975
1976   wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
1977   /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
1978   wfilename = g_renew (wchar_t, wfilename, len + 2);
1979   wfilename[len + 1] = 0;
1980
1981   op.wFunc = FO_DELETE;
1982   op.pFrom = wfilename;
1983   op.fFlags = FOF_ALLOWUNDO;
1984
1985   success = SHFileOperationW (&op) == 0;
1986
1987   if (success && op.fAnyOperationsAborted)
1988     {
1989       if (cancellable && !g_cancellable_is_cancelled (cancellable))
1990         g_cancellable_cancel (cancellable);
1991       g_set_error (error, G_IO_ERROR,
1992                    G_IO_ERROR_CANCELLED,
1993                    _("Unable to trash file: %s"),
1994                    _("Operation was cancelled"));
1995       success = FALSE;
1996     }
1997   else if (!success)
1998     g_set_error (error, G_IO_ERROR,
1999                  G_IO_ERROR_FAILED,
2000                  _("Unable to trash file: %s"),
2001                  _("internal error"));
2002
2003   g_free (wfilename);
2004   return success;
2005 }
2006 #endif /* G_OS_WIN32 */
2007
2008 static gboolean
2009 g_local_file_make_directory (GFile         *file,
2010                              GCancellable  *cancellable,
2011                              GError       **error)
2012 {
2013   GLocalFile *local = G_LOCAL_FILE (file);
2014   
2015   if (g_mkdir (local->filename, 0777) == -1)
2016     {
2017       int errsv = errno;
2018
2019       if (errsv == EINVAL)
2020         /* This must be an invalid filename, on e.g. FAT */
2021         g_set_error_literal (error, G_IO_ERROR,
2022                              G_IO_ERROR_INVALID_FILENAME,
2023                              _("Invalid filename"));
2024       else
2025         g_set_error (error, G_IO_ERROR,
2026                      g_io_error_from_errno (errsv),
2027                      _("Error creating directory: %s"),
2028                      g_strerror (errsv));
2029       return FALSE;
2030     }
2031   
2032   return TRUE;
2033 }
2034
2035 static gboolean
2036 g_local_file_make_symbolic_link (GFile         *file,
2037                                  const char    *symlink_value,
2038                                  GCancellable  *cancellable,
2039                                  GError       **error)
2040 {
2041 #ifdef HAVE_SYMLINK
2042   GLocalFile *local = G_LOCAL_FILE (file);
2043   
2044   if (symlink (symlink_value, local->filename) == -1)
2045     {
2046       int errsv = errno;
2047
2048       if (errsv == EINVAL)
2049         /* This must be an invalid filename, on e.g. FAT */
2050         g_set_error_literal (error, G_IO_ERROR,
2051                              G_IO_ERROR_INVALID_FILENAME,
2052                              _("Invalid filename"));
2053       else
2054         g_set_error (error, G_IO_ERROR,
2055                      g_io_error_from_errno (errsv),
2056                      _("Error making symbolic link: %s"),
2057                      g_strerror (errsv));
2058       return FALSE;
2059     }
2060   return TRUE;
2061 #else
2062   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
2063   return FALSE;
2064 #endif
2065 }
2066
2067
2068 static gboolean
2069 g_local_file_copy (GFile                  *source,
2070                    GFile                  *destination,
2071                    GFileCopyFlags          flags,
2072                    GCancellable           *cancellable,
2073                    GFileProgressCallback   progress_callback,
2074                    gpointer                progress_callback_data,
2075                    GError                **error)
2076 {
2077   /* Fall back to default copy */
2078   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
2079   return FALSE;
2080 }
2081
2082 static gboolean
2083 g_local_file_move (GFile                  *source,
2084                    GFile                  *destination,
2085                    GFileCopyFlags          flags,
2086                    GCancellable           *cancellable,
2087                    GFileProgressCallback   progress_callback,
2088                    gpointer                progress_callback_data,
2089                    GError                **error)
2090 {
2091   GLocalFile *local_source, *local_destination;
2092   struct stat statbuf;
2093   gboolean destination_exist, source_is_dir;
2094   char *backup_name;
2095   int res;
2096   off_t source_size;
2097   
2098   if (!G_IS_LOCAL_FILE (source) ||
2099       !G_IS_LOCAL_FILE (destination))
2100     {
2101       /* Fall back to default move */
2102       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2103       return FALSE;
2104     }
2105   
2106   local_source = G_LOCAL_FILE (source);
2107   local_destination = G_LOCAL_FILE (destination);
2108   
2109   res = g_lstat (local_source->filename, &statbuf);
2110   if (res == -1)
2111     {
2112       int errsv = errno;
2113
2114       g_set_error (error, G_IO_ERROR,
2115                    g_io_error_from_errno (errsv),
2116                    _("Error moving file: %s"),
2117                    g_strerror (errsv));
2118       return FALSE;
2119     }
2120
2121   source_is_dir = S_ISDIR (statbuf.st_mode);
2122   source_size = statbuf.st_size;
2123   
2124   destination_exist = FALSE;
2125   res = g_lstat (local_destination->filename, &statbuf);
2126   if (res == 0)
2127     {
2128       destination_exist = TRUE; /* Target file exists */
2129
2130       if (flags & G_FILE_COPY_OVERWRITE)
2131         {
2132           /* Always fail on dirs, even with overwrite */
2133           if (S_ISDIR (statbuf.st_mode))
2134             {
2135               if (source_is_dir)
2136                 g_set_error_literal (error,
2137                                      G_IO_ERROR,
2138                                      G_IO_ERROR_WOULD_MERGE,
2139                                      _("Can't move directory over directory"));
2140               else
2141                 g_set_error_literal (error,
2142                                      G_IO_ERROR,
2143                                      G_IO_ERROR_IS_DIRECTORY,
2144                                      _("Can't copy over directory"));
2145               return FALSE;
2146             }
2147         }
2148       else
2149         {
2150           g_set_error_literal (error,
2151                                G_IO_ERROR,
2152                                G_IO_ERROR_EXISTS,
2153                                _("Target file exists"));
2154           return FALSE;
2155         }
2156     }
2157   
2158   if (flags & G_FILE_COPY_BACKUP && destination_exist)
2159     {
2160       backup_name = g_strconcat (local_destination->filename, "~", NULL);
2161       if (g_rename (local_destination->filename, backup_name) == -1)
2162         {
2163           g_set_error_literal (error,
2164                                G_IO_ERROR,
2165                                G_IO_ERROR_CANT_CREATE_BACKUP,
2166                                _("Backup file creation failed"));
2167           g_free (backup_name);
2168           return FALSE;
2169         }
2170       g_free (backup_name);
2171       destination_exist = FALSE; /* It did, but no more */
2172     }
2173
2174   if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2175     {
2176       /* Source is a dir, destination exists (and is not a dir, because that would have failed
2177          earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2178       res = g_unlink (local_destination->filename);
2179       if (res == -1)
2180         {
2181           int errsv = errno;
2182
2183           g_set_error (error, G_IO_ERROR,
2184                        g_io_error_from_errno (errsv),
2185                        _("Error removing target file: %s"),
2186                        g_strerror (errsv));
2187           return FALSE;
2188         }
2189     }
2190   
2191   if (g_rename (local_source->filename, local_destination->filename) == -1)
2192     {
2193       int errsv = errno;
2194
2195       if (errsv == EXDEV)
2196         /* This will cause the fallback code to run */
2197         g_set_error_literal (error, G_IO_ERROR,
2198                              G_IO_ERROR_NOT_SUPPORTED,
2199                              _("Move between mounts not supported"));
2200       else if (errsv == EINVAL)
2201         /* This must be an invalid filename, on e.g. FAT, or
2202            we're trying to move the file into itself...
2203            We return invalid filename for both... */
2204         g_set_error_literal (error, G_IO_ERROR,
2205                              G_IO_ERROR_INVALID_FILENAME,
2206                              _("Invalid filename"));
2207       else
2208         g_set_error (error, G_IO_ERROR,
2209                      g_io_error_from_errno (errsv),
2210                      _("Error moving file: %s"),
2211                      g_strerror (errsv));
2212       return FALSE;
2213     }
2214
2215   /* Make sure we send full copied size */
2216   if (progress_callback)
2217     progress_callback (source_size, source_size, progress_callback_data);
2218   
2219   return TRUE;
2220 }
2221
2222 static GFileMonitor*
2223 g_local_file_monitor_dir (GFile             *file,
2224                           GFileMonitorFlags  flags,
2225                           GCancellable      *cancellable,
2226                           GError           **error)
2227 {
2228   GLocalFile* local_file = G_LOCAL_FILE(file);
2229   return _g_local_directory_monitor_new (local_file->filename, flags, error);
2230 }
2231
2232 static GFileMonitor*
2233 g_local_file_monitor_file (GFile             *file,
2234                            GFileMonitorFlags  flags,
2235                            GCancellable      *cancellable,
2236                            GError           **error)
2237 {
2238   GLocalFile* local_file = G_LOCAL_FILE(file);
2239   return _g_local_file_monitor_new (local_file->filename, flags, error);
2240 }
2241
2242 static void
2243 g_local_file_file_iface_init (GFileIface *iface)
2244 {
2245   iface->dup = g_local_file_dup;
2246   iface->hash = g_local_file_hash;
2247   iface->equal = g_local_file_equal;
2248   iface->is_native = g_local_file_is_native;
2249   iface->has_uri_scheme = g_local_file_has_uri_scheme;
2250   iface->get_uri_scheme = g_local_file_get_uri_scheme;
2251   iface->get_basename = g_local_file_get_basename;
2252   iface->get_path = g_local_file_get_path;
2253   iface->get_uri = g_local_file_get_uri;
2254   iface->get_parse_name = g_local_file_get_parse_name;
2255   iface->get_parent = g_local_file_get_parent;
2256   iface->prefix_matches = g_local_file_prefix_matches;
2257   iface->get_relative_path = g_local_file_get_relative_path;
2258   iface->resolve_relative_path = g_local_file_resolve_relative_path;
2259   iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2260   iface->set_display_name = g_local_file_set_display_name;
2261   iface->enumerate_children = g_local_file_enumerate_children;
2262   iface->query_info = g_local_file_query_info;
2263   iface->query_filesystem_info = g_local_file_query_filesystem_info;
2264   iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2265   iface->query_settable_attributes = g_local_file_query_settable_attributes;
2266   iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2267   iface->set_attribute = g_local_file_set_attribute;
2268   iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2269   iface->read_fn = g_local_file_read;
2270   iface->append_to = g_local_file_append_to;
2271   iface->create = g_local_file_create;
2272   iface->replace = g_local_file_replace;
2273   iface->delete_file = g_local_file_delete;
2274   iface->trash = g_local_file_trash;
2275   iface->make_directory = g_local_file_make_directory;
2276   iface->make_symbolic_link = g_local_file_make_symbolic_link;
2277   iface->copy = g_local_file_copy;
2278   iface->move = g_local_file_move;
2279   iface->monitor_dir = g_local_file_monitor_dir;
2280   iface->monitor_file = g_local_file_monitor_file;
2281 }