5ee7790d51e9f11da243ff2d6a13077af8db2ba3
[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   g_print ("get fstype for %ld\n", f_type);
623
624   /* filesystem ids taken from linux manpage */
625   switch (f_type) 
626     {
627     case 0xadf5:
628       return "adfs";
629     case 0x5346414f:
630       return "afs";
631     case 0x0187:
632       return "autofs";
633     case 0xADFF:
634       return "affs";
635     case 0x42465331:
636       return "befs";
637     case 0x1BADFACE:
638       return "bfs";
639     case 0x9123683E:
640       return "btrfs";
641     case 0xFF534D42:
642       return "cifs";
643     case 0x73757245:
644       return "coda";
645     case 0x012FF7B7:
646       return "coh";
647     case 0x28cd3d45:
648       return "cramfs";
649     case 0x1373:
650       return "devfs";
651     case 0x00414A53:
652       return "efs";
653     case 0x137D:
654       return "ext";
655     case 0xEF51:
656       return "ext2";
657     case 0xEF53:
658       return "ext3";
659     case 0x4244:
660       return "hfs";
661     case 0xF995E849:
662       return "hpfs";
663     case 0x958458f6:
664       return "hugetlbfs";
665     case 0x9660:
666       return "isofs";
667     case 0x72b6:
668       return "jffs2";
669     case 0x3153464a:
670       return "jfs";
671     case 0x137F:
672       return "minix";
673     case 0x138F:
674       return "minix2";
675     case 0x2468:
676       return "minix2";
677     case 0x2478:
678       return "minix22";
679     case 0x4d44:
680       return "msdos";
681     case 0x564c:
682       return "ncp";
683     case 0x6969:
684       return "nfs";
685     case 0x5346544e:
686       return "ntfs";
687     case 0x9fa1:
688       return "openprom";
689     case 0x9fa0:
690       return "proc";
691     case 0x002f:
692       return "qnx4";
693     case 0x52654973:
694       return "reiserfs";
695     case 0x7275:
696       return "romfs";
697     case 0x517B:
698       return "smb";
699     case 0x73717368:
700       return "squashfs";
701     case 0x012FF7B6:
702       return "sysv2";
703     case 0x012FF7B5:
704       return "sysv4";
705     case 0x01021994:
706       return "tmpfs";
707     case 0x15013346:
708       return "udf";
709     case 0x00011954:
710       return "ufs";
711     case 0x9fa2:
712       return "usbdevice";
713     case 0xa501FCF5:
714       return "vxfs";
715     case 0x012FF7B4:
716       return "xenix";
717     case 0x58465342:
718       return "xfs";
719     case 0x012FD16D:
720       return "xiafs";
721     default:
722       return NULL;
723     }
724 }
725 #endif
726
727 #ifndef G_OS_WIN32
728
729 G_LOCK_DEFINE_STATIC(mount_info_hash);
730 static GHashTable *mount_info_hash = NULL;
731 static guint64 mount_info_hash_cache_time = 0;
732
733 typedef enum {
734   MOUNT_INFO_READONLY = 1<<0
735 } MountInfo;
736
737 static gboolean
738 device_equal (gconstpointer v1,
739               gconstpointer v2)
740 {
741   return *(dev_t *)v1 == *(dev_t *)v2;
742 }
743
744 static guint
745 device_hash (gconstpointer v)
746 {
747   return (guint) *(dev_t *)v;
748 }
749
750 static void
751 get_mount_info (GFileInfo             *fs_info,
752                 const char            *path,
753                 GFileAttributeMatcher *matcher)
754 {
755   struct stat buf;
756   gboolean got_info;
757   gpointer info_as_ptr;
758   guint mount_info;
759   char *mountpoint;
760   dev_t *dev;
761   GUnixMountEntry *mount;
762   guint64 cache_time;
763
764   if (g_lstat (path, &buf) != 0)
765     return;
766
767   G_LOCK (mount_info_hash);
768
769   if (mount_info_hash == NULL)
770     mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
771                                              g_free, NULL);
772
773
774   if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
775     g_hash_table_remove_all (mount_info_hash);
776   
777   got_info = g_hash_table_lookup_extended (mount_info_hash,
778                                            &buf.st_dev,
779                                            NULL,
780                                            &info_as_ptr);
781   
782   G_UNLOCK (mount_info_hash);
783   
784   mount_info = GPOINTER_TO_UINT (info_as_ptr);
785   
786   if (!got_info)
787     {
788       mount_info = 0;
789
790       mountpoint = find_mountpoint_for (path, buf.st_dev);
791       if (mountpoint == NULL)
792         mountpoint = g_strdup ("/");
793
794       mount = g_unix_mount_at (mountpoint, &cache_time);
795       if (mount)
796         {
797           if (g_unix_mount_is_readonly (mount))
798             mount_info |= MOUNT_INFO_READONLY;
799           
800           g_unix_mount_free (mount);
801         }
802
803       g_free (mountpoint);
804
805       dev = g_new0 (dev_t, 1);
806       *dev = buf.st_dev;
807       
808       G_LOCK (mount_info_hash);
809       mount_info_hash_cache_time = cache_time;
810       g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
811       G_UNLOCK (mount_info_hash);
812     }
813
814   if (mount_info & MOUNT_INFO_READONLY)
815     g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
816 }
817
818 #endif
819
820 #ifdef G_OS_WIN32
821
822 static gboolean
823 is_xp_or_later (void)
824 {
825   static int result = -1;
826
827   if (result == -1)
828     {
829 #ifndef _MSC_VER    
830       OSVERSIONINFOEX ver_info = {0};
831       DWORDLONG cond_mask = 0;
832       int op = VER_GREATER_EQUAL;
833
834       ver_info.dwOSVersionInfoSize = sizeof ver_info;
835       ver_info.dwMajorVersion = 5;
836       ver_info.dwMinorVersion = 1;
837
838       VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
839       VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
840
841       result = VerifyVersionInfo (&ver_info,
842                                   VER_MAJORVERSION | VER_MINORVERSION, 
843                                   cond_mask) != 0;
844 #else
845       result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;  
846 #endif
847     }
848
849   return result;
850 }
851
852 static wchar_t *
853 get_volume_for_path (const char *path)
854 {
855   long len;
856   wchar_t *wpath;
857   wchar_t *result;
858
859   wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
860   result = g_new (wchar_t, MAX_PATH);
861
862   if (!GetVolumePathNameW (wpath, result, MAX_PATH))
863     {
864       char *msg = g_win32_error_message (GetLastError ());
865       g_critical ("GetVolumePathName failed: %s", msg);
866       g_free (msg);
867       g_free (result);
868       g_free (wpath);
869       return NULL;
870     }
871
872   len = wcslen (result);
873   if (len > 0 && result[len-1] != L'\\')
874     {
875       result = g_renew (wchar_t, result, len + 2);
876       result[len] = L'\\';
877       result[len + 1] = 0;
878     }
879
880   g_free (wpath);
881   return result;
882 }
883
884 static char *
885 find_mountpoint_for (const char *file, dev_t dev)
886 {
887   wchar_t *wpath;
888   char *utf8_path;
889
890   wpath = get_volume_for_path (file);
891   if (!wpath)
892     return NULL;
893
894   utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
895
896   g_free (wpath);
897   return utf8_path;
898 }
899
900 static void
901 get_filesystem_readonly (GFileInfo  *info,
902                          const char *path)
903 {
904   wchar_t *rootdir;
905
906   rootdir = get_volume_for_path (path);
907
908   if (rootdir)
909     {
910       if (is_xp_or_later ())
911         {
912           DWORD flags;
913           if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
914             g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
915                                                (flags & FILE_READ_ONLY_VOLUME) != 0);
916         }
917       else
918         {
919           if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
920             g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
921         }
922     }
923
924   g_free (rootdir);
925 }
926
927 #endif /* G_OS_WIN32 */
928
929 static GFileInfo *
930 g_local_file_query_filesystem_info (GFile         *file,
931                                     const char    *attributes,
932                                     GCancellable  *cancellable,
933                                     GError       **error)
934 {
935   GLocalFile *local = G_LOCAL_FILE (file);
936   GFileInfo *info;
937   int statfs_result = 0;
938   gboolean no_size;
939 #ifndef G_OS_WIN32
940   guint64 block_size;
941   const char *fstype;
942 #ifdef USE_STATFS
943   struct statfs statfs_buffer;
944 #elif defined(USE_STATVFS)
945   struct statvfs statfs_buffer;
946 #endif
947 #endif
948   GFileAttributeMatcher *attribute_matcher;
949         
950   g_print ("g_local_file_query_filesystem_info\n");
951   no_size = FALSE;
952   
953 #ifdef USE_STATFS
954   
955 #if STATFS_ARGS == 2
956   statfs_result = statfs (local->filename, &statfs_buffer);
957 #elif STATFS_ARGS == 4
958   statfs_result = statfs (local->filename, &statfs_buffer,
959                           sizeof (statfs_buffer), 0);
960 #endif
961   block_size = statfs_buffer.f_bsize;
962   
963 #if defined(__linux__)
964   /* ncpfs does not know the amount of available and free space *
965    * assuming ncpfs is linux specific, if you are on a non-linux platform
966    * where ncpfs is available, please file a bug about it on bugzilla.gnome.org
967    */
968   if (statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0 &&
969       /* linux/ncp_fs.h: NCP_SUPER_MAGIC == 0x564c */
970       statfs_buffer.f_type == 0x564c)
971     no_size = TRUE;
972 #endif
973   
974 #elif defined(USE_STATVFS)
975   statfs_result = statvfs (local->filename, &statfs_buffer);
976   block_size = statfs_buffer.f_frsize; 
977 #endif
978
979   if (statfs_result == -1)
980     {
981       int errsv = errno;
982
983       g_set_error (error, G_IO_ERROR,
984                    g_io_error_from_errno (errsv),
985                    _("Error getting filesystem info: %s"),
986                    g_strerror (errsv));
987       return NULL;
988     }
989
990   info = g_file_info_new ();
991
992   attribute_matcher = g_file_attribute_matcher_new (attributes);
993   
994   if (!no_size &&
995       g_file_attribute_matcher_matches (attribute_matcher,
996                                         G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
997     {
998 #ifdef G_OS_WIN32
999       gchar *localdir = g_path_get_dirname (local->filename);
1000       wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1001       ULARGE_INTEGER li;
1002       
1003       g_free (localdir);
1004       if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
1005         g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
1006       g_free (wdirname);
1007 #else
1008       g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
1009 #endif
1010     }
1011   if (!no_size &&
1012       g_file_attribute_matcher_matches (attribute_matcher,
1013                                         G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
1014     {
1015 #ifdef G_OS_WIN32
1016       gchar *localdir = g_path_get_dirname (local->filename);
1017       wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
1018       ULARGE_INTEGER li;
1019       
1020       g_free (localdir);
1021       if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
1022         g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE,  (guint64)li.QuadPart);
1023       g_free (wdirname);
1024 #else
1025       g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
1026 #endif
1027     }
1028 #ifdef USE_STATFS
1029 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
1030   fstype = g_strdup(statfs_buffer.f_fstypename);
1031   g_print ("using f_fstypename %s\n", fstype);
1032 #else
1033   fstype = get_fs_type (statfs_buffer.f_type);
1034   g_print ("using f_type %s\n", fstype);
1035 #endif
1036
1037 #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
1038   fstype = g_strdup(statfs_buffer.f_basetype); 
1039 #endif
1040
1041 #ifndef G_OS_WIN32
1042   if (fstype &&
1043       g_file_attribute_matcher_matches (attribute_matcher,
1044                                         G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
1045     g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
1046 #endif  
1047
1048   if (g_file_attribute_matcher_matches (attribute_matcher,
1049                                         G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
1050     {
1051 #ifdef G_OS_WIN32
1052       get_filesystem_readonly (info, local->filename);
1053 #else
1054       get_mount_info (info, local->filename, attribute_matcher);
1055 #endif
1056     }
1057   
1058   g_file_attribute_matcher_unref (attribute_matcher);
1059   
1060   return info;
1061 }
1062
1063 static GMount *
1064 g_local_file_find_enclosing_mount (GFile         *file,
1065                                    GCancellable  *cancellable,
1066                                    GError       **error)
1067 {
1068   GLocalFile *local = G_LOCAL_FILE (file);
1069   struct stat buf;
1070   char *mountpoint;
1071   GMount *mount;
1072
1073   if (g_lstat (local->filename, &buf) != 0)
1074     {
1075       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1076                       /* Translators: This is an error message when trying to
1077                        * find the enclosing (user visible) mount of a file, but
1078                        * none exists. */
1079                       _("Containing mount does not exist"));
1080       return NULL;
1081     }
1082
1083   mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
1084   if (mountpoint == NULL)
1085     {
1086       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1087                       /* Translators: This is an error message when trying to
1088                        * find the enclosing (user visible) mount of a file, but
1089                        * none exists. */
1090                       _("Containing mount does not exist"));
1091       return NULL;
1092     }
1093
1094   mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
1095   g_free (mountpoint);
1096   if (mount)
1097     return mount;
1098
1099   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1100                   /* Translators: This is an error message when trying to find
1101                    * the enclosing (user visible) mount of a file, but none
1102                    * exists. */
1103                   _("Containing mount does not exist"));
1104   return NULL;
1105 }
1106
1107 static GFile *
1108 g_local_file_set_display_name (GFile         *file,
1109                                const char    *display_name,
1110                                GCancellable  *cancellable,
1111                                GError       **error)
1112 {
1113   GLocalFile *local, *new_local;
1114   GFile *new_file, *parent;
1115   struct stat statbuf;
1116   int errsv;
1117
1118   parent = g_file_get_parent (file);
1119   if (parent == NULL)
1120     {
1121       g_set_error_literal (error, G_IO_ERROR,
1122                            G_IO_ERROR_FAILED,
1123                            _("Can't rename root directory"));
1124       return NULL;
1125     }
1126   
1127   new_file = g_file_get_child_for_display_name (parent, display_name, error);
1128   g_object_unref (parent);
1129   
1130   if (new_file == NULL)
1131     return NULL;
1132   local = G_LOCAL_FILE (file);
1133   new_local = G_LOCAL_FILE (new_file);
1134
1135   if (g_lstat (new_local->filename, &statbuf) == -1) 
1136     {
1137       errsv = errno;
1138
1139       if (errsv != ENOENT)
1140         {
1141           g_set_error (error, G_IO_ERROR,
1142                        g_io_error_from_errno (errsv),
1143                        _("Error renaming file: %s"),
1144                        g_strerror (errsv));
1145           return NULL;
1146         }
1147     }
1148   else
1149     {
1150       g_set_error_literal (error, G_IO_ERROR,
1151                            G_IO_ERROR_EXISTS,
1152                            _("Can't rename file, filename already exist"));
1153       return NULL;
1154     }
1155
1156   if (g_rename (local->filename, new_local->filename) == -1)
1157     {
1158       errsv = errno;
1159
1160       if (errsv == EINVAL)
1161         /* We can't get a rename file into itself error herer,
1162            so this must be an invalid filename, on e.g. FAT */
1163         g_set_error_literal (error, G_IO_ERROR,
1164                              G_IO_ERROR_INVALID_FILENAME,
1165                              _("Invalid filename"));
1166       else
1167         g_set_error (error, G_IO_ERROR,
1168                      g_io_error_from_errno (errsv),
1169                      _("Error renaming file: %s"),
1170                      g_strerror (errsv));
1171       g_object_unref (new_file);
1172       return NULL;
1173     }
1174   
1175   return new_file;
1176 }
1177
1178 static GFileInfo *
1179 g_local_file_query_info (GFile                *file,
1180                          const char           *attributes,
1181                          GFileQueryInfoFlags   flags,
1182                          GCancellable         *cancellable,
1183                          GError              **error)
1184 {
1185   GLocalFile *local = G_LOCAL_FILE (file);
1186   GFileInfo *info;
1187   GFileAttributeMatcher *matcher;
1188   char *basename, *dirname;
1189   GLocalParentFileInfo parent_info;
1190
1191   matcher = g_file_attribute_matcher_new (attributes);
1192   
1193   basename = g_path_get_basename (local->filename);
1194   
1195   dirname = g_path_get_dirname (local->filename);
1196   _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
1197   g_free (dirname);
1198   
1199   info = _g_local_file_info_get (basename, local->filename,
1200                                  matcher, flags, &parent_info,
1201                                  error);
1202   
1203   g_free (basename);
1204
1205   g_file_attribute_matcher_unref (matcher);
1206
1207   return info;
1208 }
1209
1210 static GFileAttributeInfoList *
1211 g_local_file_query_settable_attributes (GFile         *file,
1212                                         GCancellable  *cancellable,
1213                                         GError       **error)
1214 {
1215   return g_file_attribute_info_list_ref (local_writable_attributes);
1216 }
1217
1218 static GFileAttributeInfoList *
1219 g_local_file_query_writable_namespaces (GFile         *file,
1220                                         GCancellable  *cancellable,
1221                                         GError       **error)
1222 {
1223   return g_file_attribute_info_list_ref (local_writable_namespaces);
1224 }
1225
1226 static gboolean
1227 g_local_file_set_attribute (GFile                *file,
1228                             const char           *attribute,
1229                             GFileAttributeType    type,
1230                             gpointer              value_p,
1231                             GFileQueryInfoFlags   flags,
1232                             GCancellable         *cancellable,
1233                             GError              **error)
1234 {
1235   GLocalFile *local = G_LOCAL_FILE (file);
1236
1237   return _g_local_file_info_set_attribute (local->filename,
1238                                            attribute,
1239                                            type,
1240                                            value_p,
1241                                            flags,
1242                                            cancellable,
1243                                            error);
1244 }
1245
1246 static gboolean
1247 g_local_file_set_attributes_from_info (GFile                *file,
1248                                        GFileInfo            *info,
1249                                        GFileQueryInfoFlags   flags,
1250                                        GCancellable         *cancellable,
1251                                        GError              **error)
1252 {
1253   GLocalFile *local = G_LOCAL_FILE (file);
1254   int res, chained_res;
1255   GFileIface *default_iface;
1256
1257   res = _g_local_file_info_set_attributes (local->filename,
1258                                            info, flags, 
1259                                            cancellable,
1260                                            error);
1261
1262   if (!res)
1263     error = NULL; /* Don't write over error if further errors */
1264
1265   default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1266
1267   chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1268   
1269   return res && chained_res;
1270 }
1271
1272 static GFileInputStream *
1273 g_local_file_read (GFile         *file,
1274                    GCancellable  *cancellable,
1275                    GError       **error)
1276 {
1277   GLocalFile *local = G_LOCAL_FILE (file);
1278   int fd;
1279   struct stat buf;
1280   
1281   fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
1282   if (fd == -1)
1283     {
1284       int errsv = errno;
1285
1286       g_set_error (error, G_IO_ERROR,
1287                    g_io_error_from_errno (errsv),
1288                    _("Error opening file: %s"),
1289                    g_strerror (errsv));
1290       return NULL;
1291     }
1292
1293   if (fstat(fd, &buf) == 0 && S_ISDIR (buf.st_mode))
1294     {
1295       close (fd);
1296       g_set_error_literal (error, G_IO_ERROR,
1297                            G_IO_ERROR_IS_DIRECTORY,
1298                            _("Can't open directory"));
1299       return NULL;
1300     }
1301   
1302   return _g_local_file_input_stream_new (fd);
1303 }
1304
1305 static GFileOutputStream *
1306 g_local_file_append_to (GFile             *file,
1307                         GFileCreateFlags   flags,
1308                         GCancellable      *cancellable,
1309                         GError           **error)
1310 {
1311   return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1312                                              flags, cancellable, error);
1313 }
1314
1315 static GFileOutputStream *
1316 g_local_file_create (GFile             *file,
1317                      GFileCreateFlags   flags,
1318                      GCancellable      *cancellable,
1319                      GError           **error)
1320 {
1321   return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1322                                              flags, cancellable, error);
1323 }
1324
1325 static GFileOutputStream *
1326 g_local_file_replace (GFile             *file,
1327                       const char        *etag,
1328                       gboolean           make_backup,
1329                       GFileCreateFlags   flags,
1330                       GCancellable      *cancellable,
1331                       GError           **error)
1332 {
1333   return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1334                                               etag, make_backup, flags,
1335                                               cancellable, error);
1336 }
1337
1338
1339 static gboolean
1340 g_local_file_delete (GFile         *file,
1341                      GCancellable  *cancellable,
1342                      GError       **error)
1343 {
1344   GLocalFile *local = G_LOCAL_FILE (file);
1345   
1346   if (g_remove (local->filename) == -1)
1347     {
1348       int errsv = errno;
1349
1350       /* Posix allows EEXIST too, but the more sane error
1351          is G_IO_ERROR_NOT_FOUND, and it's what nautilus
1352          expects */
1353       if (errsv == EEXIST)
1354         errsv = ENOTEMPTY;
1355
1356       g_set_error (error, G_IO_ERROR,
1357                    g_io_error_from_errno (errsv),
1358                    _("Error removing file: %s"),
1359                    g_strerror (errsv));
1360       return FALSE;
1361     }
1362   
1363   return TRUE;
1364 }
1365
1366 static char *
1367 strip_trailing_slashes (const char *path)
1368 {
1369   char *path_copy;
1370   int len;
1371
1372   path_copy = g_strdup (path);
1373   len = strlen (path_copy);
1374   while (len > 1 && path_copy[len-1] == '/')
1375     path_copy[--len] = 0;
1376
1377   return path_copy;
1378  }
1379
1380 static char *
1381 expand_symlink (const char *link)
1382 {
1383   char *resolved, *canonical, *parent, *link2;
1384   char symlink_value[4096];
1385 #ifdef G_OS_WIN32
1386 #else
1387   ssize_t res;
1388 #endif
1389   
1390 #ifdef G_OS_WIN32
1391 #else
1392   res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1393   
1394   if (res == -1)
1395     return g_strdup (link);
1396   symlink_value[res] = 0;
1397 #endif
1398   
1399   if (g_path_is_absolute (symlink_value))
1400     return canonicalize_filename (symlink_value);
1401   else
1402     {
1403       link2 = strip_trailing_slashes (link);
1404       parent = g_path_get_dirname (link2);
1405       g_free (link2);
1406       
1407       resolved = g_build_filename (parent, symlink_value, NULL);
1408       g_free (parent);
1409       
1410       canonical = canonicalize_filename (resolved);
1411       
1412       g_free (resolved);
1413
1414       return canonical;
1415     }
1416 }
1417
1418 static char *
1419 get_parent (const char *path, 
1420             dev_t      *parent_dev)
1421 {
1422   char *parent, *tmp;
1423   struct stat parent_stat;
1424   int num_recursions;
1425   char *path_copy;
1426
1427   path_copy = strip_trailing_slashes (path);
1428   
1429   parent = g_path_get_dirname (path_copy);
1430   if (strcmp (parent, ".") == 0 ||
1431       strcmp (parent, path_copy) == 0)
1432     {
1433       g_free (parent);
1434       g_free (path_copy);
1435       return NULL;
1436     }
1437   g_free (path_copy);
1438
1439   num_recursions = 0;
1440   do {
1441     if (g_lstat (parent, &parent_stat) != 0)
1442       {
1443         g_free (parent);
1444         return NULL;
1445       }
1446     
1447     if (S_ISLNK (parent_stat.st_mode))
1448       {
1449         tmp = parent;
1450         parent = expand_symlink (parent);
1451         g_free (tmp);
1452       }
1453     
1454     num_recursions++;
1455     if (num_recursions > 12)
1456       {
1457         g_free (parent);
1458         return NULL;
1459       }
1460   } while (S_ISLNK (parent_stat.st_mode));
1461
1462   *parent_dev = parent_stat.st_dev;
1463   
1464   return parent;
1465 }
1466
1467 static char *
1468 expand_all_symlinks (const char *path)
1469 {
1470   char *parent, *parent_expanded;
1471   char *basename, *res;
1472   dev_t parent_dev;
1473
1474   parent = get_parent (path, &parent_dev);
1475   if (parent)
1476     {
1477       parent_expanded = expand_all_symlinks (parent);
1478       g_free (parent);
1479       basename = g_path_get_basename (path);
1480       res = g_build_filename (parent_expanded, basename, NULL);
1481       g_free (basename);
1482       g_free (parent_expanded);
1483     }
1484   else
1485     res = g_strdup (path);
1486   
1487   return res;
1488 }
1489
1490 #ifndef G_OS_WIN32
1491
1492 static char *
1493 find_mountpoint_for (const char *file, 
1494                      dev_t       dev)
1495 {
1496   char *dir, *parent;
1497   dev_t dir_dev, parent_dev;
1498
1499   dir = g_strdup (file);
1500   dir_dev = dev;
1501
1502   while (1) 
1503     {
1504       parent = get_parent (dir, &parent_dev);
1505       if (parent == NULL)
1506         return dir;
1507     
1508       if (parent_dev != dir_dev)
1509         {
1510           g_free (parent);
1511           return dir;
1512         }
1513     
1514       g_free (dir);
1515       dir = parent;
1516     }
1517 }
1518
1519 static char *
1520 find_topdir_for (const char *file)
1521 {
1522   char *dir;
1523   dev_t dir_dev;
1524
1525   dir = get_parent (file, &dir_dev);
1526   if (dir == NULL)
1527     return NULL;
1528
1529   return find_mountpoint_for (dir, dir_dev);
1530 }
1531
1532 static char *
1533 get_unique_filename (const char *basename, 
1534                      int         id)
1535 {
1536   const char *dot;
1537       
1538   if (id == 1)
1539     return g_strdup (basename);
1540
1541   dot = strchr (basename, '.');
1542   if (dot)
1543     return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
1544   else
1545     return g_strdup_printf ("%s.%d", basename, id);
1546 }
1547
1548 static gboolean
1549 path_has_prefix (const char *path, 
1550                  const char *prefix)
1551 {
1552   int prefix_len;
1553
1554   if (prefix == NULL)
1555     return TRUE;
1556
1557   prefix_len = strlen (prefix);
1558   
1559   if (strncmp (path, prefix, prefix_len) == 0 &&
1560       (prefix_len == 0 || /* empty prefix always matches */
1561        prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1562        path[prefix_len] == 0 ||
1563        path[prefix_len] == '/'))
1564     return TRUE;
1565   
1566   return FALSE;
1567 }
1568
1569 static char *
1570 try_make_relative (const char *path, 
1571                    const char *base)
1572 {
1573   char *path2, *base2;
1574   char *relative;
1575
1576   path2 = expand_all_symlinks (path);
1577   base2 = expand_all_symlinks (base);
1578
1579   relative = NULL;
1580   if (path_has_prefix (path2, base2))
1581     {
1582       relative = path2 + strlen (base2);
1583       while (*relative == '/')
1584         relative ++;
1585       relative = g_strdup (relative);
1586     }
1587   g_free (path2);
1588   g_free (base2);
1589
1590   if (relative)
1591     return relative;
1592   
1593   /* Failed, use abs path */
1594   return g_strdup (path);
1595 }
1596
1597 static char *
1598 escape_trash_name (char *name)
1599 {
1600   GString *str;
1601   const gchar hex[16] = "0123456789ABCDEF";
1602   
1603   str = g_string_new ("");
1604
1605   while (*name != 0)
1606     {
1607       char c;
1608
1609       c = *name++;
1610
1611       if (g_ascii_isprint (c))
1612         g_string_append_c (str, c);
1613       else
1614         {
1615           g_string_append_c (str, '%');
1616           g_string_append_c (str, hex[((guchar)c) >> 4]);
1617           g_string_append_c (str, hex[((guchar)c) & 0xf]);
1618         }
1619     }
1620
1621   return g_string_free (str, FALSE);
1622 }
1623
1624 gboolean
1625 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1626 {
1627   static gsize home_dev_set = 0;
1628   static dev_t home_dev;
1629   char *topdir, *globaldir, *trashdir, *tmpname;
1630   uid_t uid;
1631   char uid_str[32];
1632   struct stat global_stat, trash_stat;
1633   gboolean res;
1634   int statres;
1635       
1636   if (g_once_init_enter (&home_dev_set))
1637     {
1638       struct stat home_stat;
1639       
1640       g_stat (g_get_home_dir (), &home_stat);
1641       home_dev = home_stat.st_dev;
1642       g_once_init_leave (&home_dev_set, 1);
1643     }
1644
1645   /* Assume we can trash to the home */
1646   if (dir_dev == home_dev)
1647     return TRUE;
1648
1649   topdir = find_mountpoint_for (dirname, dir_dev);
1650   if (topdir == NULL)
1651     return FALSE;
1652
1653   globaldir = g_build_filename (topdir, ".Trash", NULL); 
1654   statres = g_lstat (globaldir, &global_stat);
1655  if (g_lstat (globaldir, &global_stat) == 0 &&
1656       S_ISDIR (global_stat.st_mode) &&
1657       (global_stat.st_mode & S_ISVTX) != 0)
1658     {
1659       /* got a toplevel sysadmin created dir, assume we
1660        * can trash to it (we should be able to create a dir)
1661        * This fails for the FAT case where the ownership of
1662        * that dir would be wrong though..
1663        */
1664       g_free (globaldir);
1665       g_free (topdir);
1666       return TRUE;
1667     }
1668   g_free (globaldir);
1669
1670   /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1671   uid = geteuid ();
1672   g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
1673   
1674   tmpname = g_strdup_printf (".Trash-%s", uid_str);
1675   trashdir = g_build_filename (topdir, tmpname, NULL);
1676   g_free (tmpname);
1677
1678   if (g_lstat (trashdir, &trash_stat) == 0)
1679     {
1680       g_free (topdir);
1681       g_free (trashdir);
1682       return
1683         S_ISDIR (trash_stat.st_mode) &&
1684         trash_stat.st_uid == uid;
1685     }
1686   g_free (trashdir);
1687
1688   /* User specific trash didn't exist, can we create it? */
1689   res = g_access (topdir, W_OK) == 0;
1690   
1691   g_free (topdir);
1692   
1693   return res;
1694 }
1695
1696
1697 static gboolean
1698 g_local_file_trash (GFile         *file,
1699                     GCancellable  *cancellable,
1700                     GError       **error)
1701 {
1702   GLocalFile *local = G_LOCAL_FILE (file);
1703   struct stat file_stat, home_stat;
1704   const char *homedir;
1705   char *trashdir, *topdir, *infodir, *filesdir;
1706   char *basename, *trashname, *trashfile, *infoname, *infofile;
1707   char *original_name, *original_name_escaped;
1708   int i;
1709   char *data;
1710   gboolean is_homedir_trash;
1711   char delete_time[32];
1712   int fd;
1713   struct stat trash_stat, global_stat;
1714   char *dirname, *globaldir;
1715   
1716   if (g_lstat (local->filename, &file_stat) != 0)
1717     {
1718       int errsv = errno;
1719
1720       g_set_error (error, G_IO_ERROR,
1721                    g_io_error_from_errno (errsv),
1722                    _("Error trashing file: %s"),
1723                    g_strerror (errsv));
1724       return FALSE;
1725     }
1726     
1727   homedir = g_get_home_dir ();
1728   g_stat (homedir, &home_stat);
1729
1730   is_homedir_trash = FALSE;
1731   trashdir = NULL;
1732   if (file_stat.st_dev == home_stat.st_dev)
1733     {
1734       is_homedir_trash = TRUE;
1735       errno = 0;
1736       trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1737       if (g_mkdir_with_parents (trashdir, 0700) < 0)
1738         {
1739           char *display_name;
1740           int errsv = errno;
1741
1742           display_name = g_filename_display_name (trashdir);
1743           g_set_error (error, G_IO_ERROR,
1744                        g_io_error_from_errno (errsv),
1745                        _("Unable to create trash dir %s: %s"),
1746                        display_name, g_strerror (errsv));
1747           g_free (display_name);
1748           g_free (trashdir);
1749           return FALSE;
1750         }
1751       topdir = g_strdup (g_get_user_data_dir ());
1752     }
1753   else
1754     {
1755       uid_t uid;
1756       char uid_str[32];
1757
1758       uid = geteuid ();
1759       g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1760       
1761       topdir = find_topdir_for (local->filename);
1762       if (topdir == NULL)
1763         {
1764           g_set_error_literal (error, G_IO_ERROR,
1765                                G_IO_ERROR_NOT_SUPPORTED,
1766                                _("Unable to find toplevel directory for trash"));
1767           return FALSE;
1768         }
1769       
1770       /* Try looking for global trash dir $topdir/.Trash/$uid */
1771       globaldir = g_build_filename (topdir, ".Trash", NULL);
1772       if (g_lstat (globaldir, &global_stat) == 0 &&
1773           S_ISDIR (global_stat.st_mode) &&
1774           (global_stat.st_mode & S_ISVTX) != 0)
1775         {
1776           trashdir = g_build_filename (globaldir, uid_str, NULL);
1777
1778           if (g_lstat (trashdir, &trash_stat) == 0)
1779             {
1780               if (!S_ISDIR (trash_stat.st_mode) ||
1781                   trash_stat.st_uid != uid)
1782                 {
1783                   /* Not a directory or not owned by user, ignore */
1784                   g_free (trashdir);
1785                   trashdir = NULL;
1786                 }
1787             }
1788           else if (g_mkdir (trashdir, 0700) == -1)
1789             {
1790               g_free (trashdir);
1791               trashdir = NULL;
1792             }
1793         }
1794       g_free (globaldir);
1795
1796       if (trashdir == NULL)
1797         {
1798           gboolean tried_create;
1799           
1800           /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1801           dirname = g_strdup_printf (".Trash-%s", uid_str);
1802           trashdir = g_build_filename (topdir, dirname, NULL);
1803           g_free (dirname);
1804
1805           tried_create = FALSE;
1806
1807         retry:
1808           if (g_lstat (trashdir, &trash_stat) == 0)
1809             {
1810               if (!S_ISDIR (trash_stat.st_mode) ||
1811                   trash_stat.st_uid != uid)
1812                 {
1813                   /* Remove the failed directory */
1814                   if (tried_create)
1815                     g_remove (trashdir);
1816                   
1817                   /* Not a directory or not owned by user, ignore */
1818                   g_free (trashdir);
1819                   trashdir = NULL;
1820                 }
1821             }
1822           else
1823             {
1824               if (!tried_create &&
1825                   g_mkdir (trashdir, 0700) != -1)
1826                 {
1827                   /* Ensure that the created dir has the right uid etc.
1828                      This might fail on e.g. a FAT dir */
1829                   tried_create = TRUE;
1830                   goto retry;
1831                 }
1832               else
1833                 {
1834                   g_free (trashdir);
1835                   trashdir = NULL;
1836                 }
1837             }
1838         }
1839
1840       if (trashdir == NULL)
1841         {
1842           g_free (topdir);
1843           g_set_error_literal (error, G_IO_ERROR,
1844                                G_IO_ERROR_NOT_SUPPORTED,
1845                                _("Unable to find or create trash directory"));
1846           return FALSE;
1847         }
1848     }
1849
1850   /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1851
1852   infodir = g_build_filename (trashdir, "info", NULL);
1853   filesdir = g_build_filename (trashdir, "files", NULL);
1854   g_free (trashdir);
1855
1856   /* Make sure we have the subdirectories */
1857   if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1858       (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
1859     {
1860       g_free (topdir);
1861       g_free (infodir);
1862       g_free (filesdir);
1863       g_set_error_literal (error, G_IO_ERROR,
1864                            G_IO_ERROR_NOT_SUPPORTED,
1865                            _("Unable to find or create trash directory"));
1866       return FALSE;
1867     }  
1868
1869   basename = g_path_get_basename (local->filename);
1870   i = 1;
1871   trashname = NULL;
1872   infofile = NULL;
1873   do {
1874     g_free (trashname);
1875     g_free (infofile);
1876     
1877     trashname = get_unique_filename (basename, i++);
1878     infoname = g_strconcat (trashname, ".trashinfo", NULL);
1879     infofile = g_build_filename (infodir, infoname, NULL);
1880     g_free (infoname);
1881
1882     fd = open (infofile, O_CREAT | O_EXCL, 0666);
1883   } while (fd == -1 && errno == EEXIST);
1884
1885   g_free (basename);
1886   g_free (infodir);
1887
1888   if (fd == -1)
1889     {
1890       int errsv = errno;
1891
1892       g_free (filesdir);
1893       g_free (topdir);
1894       g_free (trashname);
1895       g_free (infofile);
1896       
1897       g_set_error (error, G_IO_ERROR,
1898                    g_io_error_from_errno (errsv),
1899                    _("Unable to create trashing info file: %s"),
1900                    g_strerror (errsv));
1901       return FALSE;
1902     }
1903
1904   close (fd);
1905
1906   /* TODO: Maybe we should verify that you can delete the file from the trash
1907      before moving it? OTOH, that is hard, as it needs a recursive scan */
1908
1909   trashfile = g_build_filename (filesdir, trashname, NULL);
1910
1911   g_free (filesdir);
1912
1913   if (g_rename (local->filename, trashfile) == -1)
1914     {
1915       int errsv = errno;
1916
1917       g_free (topdir);
1918       g_free (trashname);
1919       g_free (infofile);
1920       g_free (trashfile);
1921       
1922       g_set_error (error, G_IO_ERROR,
1923                    g_io_error_from_errno (errsv),
1924                    _("Unable to trash file: %s"),
1925                    g_strerror (errsv));
1926       return FALSE;
1927     }
1928
1929   g_free (trashfile);
1930
1931   /* TODO: Do we need to update mtime/atime here after the move? */
1932
1933   /* Use absolute names for homedir */
1934   if (is_homedir_trash)
1935     original_name = g_strdup (local->filename);
1936   else
1937     original_name = try_make_relative (local->filename, topdir);
1938   original_name_escaped = escape_trash_name (original_name);
1939   
1940   g_free (original_name);
1941   g_free (topdir);
1942   
1943   {
1944     time_t t;
1945     struct tm now;
1946     t = time (NULL);
1947     localtime_r (&t, &now);
1948     delete_time[0] = 0;
1949     strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
1950   }
1951
1952   data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
1953                           original_name_escaped, delete_time);
1954
1955   g_file_set_contents (infofile, data, -1, NULL);
1956   g_free (infofile);
1957   g_free (data);
1958   
1959   g_free (original_name_escaped);
1960   g_free (trashname);
1961   
1962   return TRUE;
1963 }
1964 #else /* G_OS_WIN32 */
1965 gboolean
1966 _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
1967 {
1968   return FALSE;                 /* XXX ??? */
1969 }
1970
1971 static gboolean
1972 g_local_file_trash (GFile         *file,
1973                     GCancellable  *cancellable,
1974                     GError       **error)
1975 {
1976   GLocalFile *local = G_LOCAL_FILE (file);
1977   SHFILEOPSTRUCTW op = {0};
1978   gboolean success;
1979   wchar_t *wfilename;
1980   long len;
1981
1982   wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
1983   /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
1984   wfilename = g_renew (wchar_t, wfilename, len + 2);
1985   wfilename[len + 1] = 0;
1986
1987   op.wFunc = FO_DELETE;
1988   op.pFrom = wfilename;
1989   op.fFlags = FOF_ALLOWUNDO;
1990
1991   success = SHFileOperationW (&op) == 0;
1992
1993   if (success && op.fAnyOperationsAborted)
1994     {
1995       if (cancellable && !g_cancellable_is_cancelled (cancellable))
1996         g_cancellable_cancel (cancellable);
1997       g_set_error (error, G_IO_ERROR,
1998                    G_IO_ERROR_CANCELLED,
1999                    _("Unable to trash file: %s"),
2000                    _("Operation was cancelled"));
2001       success = FALSE;
2002     }
2003   else if (!success)
2004     g_set_error (error, G_IO_ERROR,
2005                  G_IO_ERROR_FAILED,
2006                  _("Unable to trash file: %s"),
2007                  _("internal error"));
2008
2009   g_free (wfilename);
2010   return success;
2011 }
2012 #endif /* G_OS_WIN32 */
2013
2014 static gboolean
2015 g_local_file_make_directory (GFile         *file,
2016                              GCancellable  *cancellable,
2017                              GError       **error)
2018 {
2019   GLocalFile *local = G_LOCAL_FILE (file);
2020   
2021   if (g_mkdir (local->filename, 0777) == -1)
2022     {
2023       int errsv = errno;
2024
2025       if (errsv == EINVAL)
2026         /* This must be an invalid filename, on e.g. FAT */
2027         g_set_error_literal (error, G_IO_ERROR,
2028                              G_IO_ERROR_INVALID_FILENAME,
2029                              _("Invalid filename"));
2030       else
2031         g_set_error (error, G_IO_ERROR,
2032                      g_io_error_from_errno (errsv),
2033                      _("Error creating directory: %s"),
2034                      g_strerror (errsv));
2035       return FALSE;
2036     }
2037   
2038   return TRUE;
2039 }
2040
2041 static gboolean
2042 g_local_file_make_symbolic_link (GFile         *file,
2043                                  const char    *symlink_value,
2044                                  GCancellable  *cancellable,
2045                                  GError       **error)
2046 {
2047 #ifdef HAVE_SYMLINK
2048   GLocalFile *local = G_LOCAL_FILE (file);
2049   
2050   if (symlink (symlink_value, local->filename) == -1)
2051     {
2052       int errsv = errno;
2053
2054       if (errsv == EINVAL)
2055         /* This must be an invalid filename, on e.g. FAT */
2056         g_set_error_literal (error, G_IO_ERROR,
2057                              G_IO_ERROR_INVALID_FILENAME,
2058                              _("Invalid filename"));
2059       else
2060         g_set_error (error, G_IO_ERROR,
2061                      g_io_error_from_errno (errsv),
2062                      _("Error making symbolic link: %s"),
2063                      g_strerror (errsv));
2064       return FALSE;
2065     }
2066   return TRUE;
2067 #else
2068   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
2069   return FALSE;
2070 #endif
2071 }
2072
2073
2074 static gboolean
2075 g_local_file_copy (GFile                  *source,
2076                    GFile                  *destination,
2077                    GFileCopyFlags          flags,
2078                    GCancellable           *cancellable,
2079                    GFileProgressCallback   progress_callback,
2080                    gpointer                progress_callback_data,
2081                    GError                **error)
2082 {
2083   /* Fall back to default copy */
2084   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
2085   return FALSE;
2086 }
2087
2088 static gboolean
2089 g_local_file_move (GFile                  *source,
2090                    GFile                  *destination,
2091                    GFileCopyFlags          flags,
2092                    GCancellable           *cancellable,
2093                    GFileProgressCallback   progress_callback,
2094                    gpointer                progress_callback_data,
2095                    GError                **error)
2096 {
2097   GLocalFile *local_source, *local_destination;
2098   struct stat statbuf;
2099   gboolean destination_exist, source_is_dir;
2100   char *backup_name;
2101   int res;
2102   off_t source_size;
2103   
2104   if (!G_IS_LOCAL_FILE (source) ||
2105       !G_IS_LOCAL_FILE (destination))
2106     {
2107       /* Fall back to default move */
2108       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
2109       return FALSE;
2110     }
2111   
2112   local_source = G_LOCAL_FILE (source);
2113   local_destination = G_LOCAL_FILE (destination);
2114   
2115   res = g_lstat (local_source->filename, &statbuf);
2116   if (res == -1)
2117     {
2118       int errsv = errno;
2119
2120       g_set_error (error, G_IO_ERROR,
2121                    g_io_error_from_errno (errsv),
2122                    _("Error moving file: %s"),
2123                    g_strerror (errsv));
2124       return FALSE;
2125     }
2126
2127   source_is_dir = S_ISDIR (statbuf.st_mode);
2128   source_size = statbuf.st_size;
2129   
2130   destination_exist = FALSE;
2131   res = g_lstat (local_destination->filename, &statbuf);
2132   if (res == 0)
2133     {
2134       destination_exist = TRUE; /* Target file exists */
2135
2136       if (flags & G_FILE_COPY_OVERWRITE)
2137         {
2138           /* Always fail on dirs, even with overwrite */
2139           if (S_ISDIR (statbuf.st_mode))
2140             {
2141               if (source_is_dir)
2142                 g_set_error_literal (error,
2143                                      G_IO_ERROR,
2144                                      G_IO_ERROR_WOULD_MERGE,
2145                                      _("Can't move directory over directory"));
2146               else
2147                 g_set_error_literal (error,
2148                                      G_IO_ERROR,
2149                                      G_IO_ERROR_IS_DIRECTORY,
2150                                      _("Can't copy over directory"));
2151               return FALSE;
2152             }
2153         }
2154       else
2155         {
2156           g_set_error_literal (error,
2157                                G_IO_ERROR,
2158                                G_IO_ERROR_EXISTS,
2159                                _("Target file exists"));
2160           return FALSE;
2161         }
2162     }
2163   
2164   if (flags & G_FILE_COPY_BACKUP && destination_exist)
2165     {
2166       backup_name = g_strconcat (local_destination->filename, "~", NULL);
2167       if (g_rename (local_destination->filename, backup_name) == -1)
2168         {
2169           g_set_error_literal (error,
2170                                G_IO_ERROR,
2171                                G_IO_ERROR_CANT_CREATE_BACKUP,
2172                                _("Backup file creation failed"));
2173           g_free (backup_name);
2174           return FALSE;
2175         }
2176       g_free (backup_name);
2177       destination_exist = FALSE; /* It did, but no more */
2178     }
2179
2180   if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
2181     {
2182       /* Source is a dir, destination exists (and is not a dir, because that would have failed
2183          earlier), and we're overwriting. Manually remove the target so we can do the rename. */
2184       res = g_unlink (local_destination->filename);
2185       if (res == -1)
2186         {
2187           int errsv = errno;
2188
2189           g_set_error (error, G_IO_ERROR,
2190                        g_io_error_from_errno (errsv),
2191                        _("Error removing target file: %s"),
2192                        g_strerror (errsv));
2193           return FALSE;
2194         }
2195     }
2196   
2197   if (g_rename (local_source->filename, local_destination->filename) == -1)
2198     {
2199       int errsv = errno;
2200
2201       if (errsv == EXDEV)
2202         /* This will cause the fallback code to run */
2203         g_set_error_literal (error, G_IO_ERROR,
2204                              G_IO_ERROR_NOT_SUPPORTED,
2205                              _("Move between mounts not supported"));
2206       else if (errsv == EINVAL)
2207         /* This must be an invalid filename, on e.g. FAT, or
2208            we're trying to move the file into itself...
2209            We return invalid filename for both... */
2210         g_set_error_literal (error, G_IO_ERROR,
2211                              G_IO_ERROR_INVALID_FILENAME,
2212                              _("Invalid filename"));
2213       else
2214         g_set_error (error, G_IO_ERROR,
2215                      g_io_error_from_errno (errsv),
2216                      _("Error moving file: %s"),
2217                      g_strerror (errsv));
2218       return FALSE;
2219     }
2220
2221   /* Make sure we send full copied size */
2222   if (progress_callback)
2223     progress_callback (source_size, source_size, progress_callback_data);
2224   
2225   return TRUE;
2226 }
2227
2228 static GFileMonitor*
2229 g_local_file_monitor_dir (GFile             *file,
2230                           GFileMonitorFlags  flags,
2231                           GCancellable      *cancellable,
2232                           GError           **error)
2233 {
2234   GLocalFile* local_file = G_LOCAL_FILE(file);
2235   return _g_local_directory_monitor_new (local_file->filename, flags, error);
2236 }
2237
2238 static GFileMonitor*
2239 g_local_file_monitor_file (GFile             *file,
2240                            GFileMonitorFlags  flags,
2241                            GCancellable      *cancellable,
2242                            GError           **error)
2243 {
2244   GLocalFile* local_file = G_LOCAL_FILE(file);
2245   return _g_local_file_monitor_new (local_file->filename, flags, error);
2246 }
2247
2248 static void
2249 g_local_file_file_iface_init (GFileIface *iface)
2250 {
2251   iface->dup = g_local_file_dup;
2252   iface->hash = g_local_file_hash;
2253   iface->equal = g_local_file_equal;
2254   iface->is_native = g_local_file_is_native;
2255   iface->has_uri_scheme = g_local_file_has_uri_scheme;
2256   iface->get_uri_scheme = g_local_file_get_uri_scheme;
2257   iface->get_basename = g_local_file_get_basename;
2258   iface->get_path = g_local_file_get_path;
2259   iface->get_uri = g_local_file_get_uri;
2260   iface->get_parse_name = g_local_file_get_parse_name;
2261   iface->get_parent = g_local_file_get_parent;
2262   iface->prefix_matches = g_local_file_prefix_matches;
2263   iface->get_relative_path = g_local_file_get_relative_path;
2264   iface->resolve_relative_path = g_local_file_resolve_relative_path;
2265   iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
2266   iface->set_display_name = g_local_file_set_display_name;
2267   iface->enumerate_children = g_local_file_enumerate_children;
2268   iface->query_info = g_local_file_query_info;
2269   iface->query_filesystem_info = g_local_file_query_filesystem_info;
2270   iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
2271   iface->query_settable_attributes = g_local_file_query_settable_attributes;
2272   iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
2273   iface->set_attribute = g_local_file_set_attribute;
2274   iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
2275   iface->read_fn = g_local_file_read;
2276   iface->append_to = g_local_file_append_to;
2277   iface->create = g_local_file_create;
2278   iface->replace = g_local_file_replace;
2279   iface->delete_file = g_local_file_delete;
2280   iface->trash = g_local_file_trash;
2281   iface->make_directory = g_local_file_make_directory;
2282   iface->make_symbolic_link = g_local_file_make_symbolic_link;
2283   iface->copy = g_local_file_copy;
2284   iface->move = g_local_file_move;
2285   iface->monitor_dir = g_local_file_monitor_dir;
2286   iface->monitor_file = g_local_file_monitor_file;
2287 }