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