gio/ docs/reference/gio Merged gio-standalone into glib.
[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 #include <unistd.h>
31
32 #if HAVE_SYS_STATFS_H
33 #include <sys/statfs.h>
34 #endif
35 #if HAVE_SYS_STATVFS_H
36 #include <sys/statvfs.h>
37 #endif
38 #if HAVE_SYS_VFS_H
39 #include <sys/vfs.h>
40 #elif HAVE_SYS_MOUNT_H
41 #if HAVE_SYS_PARAM_H
42 #include <sys/param.h>
43 #endif
44 #include <sys/mount.h>
45 #endif
46
47 #if defined(HAVE_STATFS) && defined(HAVE_STATVFS)
48 /* Some systems have both statfs and statvfs, pick the
49    most "native" for these */
50 # if defined(sun) && defined(__SVR4)
51    /* on solaris, statfs doesn't even have the
52       f_bavail field */
53 #  define USE_STATVFS
54 # else
55   /* at least on linux, statfs is the actual syscall */
56 #  define USE_STATFS
57 # endif
58
59 #elif defined(HAVE_STATFS)
60
61 # define USE_STATFS
62
63 #elif defined(HAVE_STATVFS)
64
65 # define USE_STATVFS
66
67 #endif
68
69 #include "glocalfile.h"
70 #include "glocalfileinfo.h"
71 #include "glocalfileenumerator.h"
72 #include "glocalfileinputstream.h"
73 #include "glocalfileoutputstream.h"
74 #include "glocaldirectorymonitor.h"
75 #include "glocalfilemonitor.h"
76 #include "gvolumeprivate.h"
77 #include <glib/gstdio.h>
78 #include "glibintl.h"
79
80 static void g_local_file_file_iface_init (GFileIface       *iface);
81
82 static GFileAttributeInfoList *local_writable_attributes = NULL;
83 static GFileAttributeInfoList *local_writable_namespaces = NULL;
84
85 struct _GLocalFile
86 {
87   GObject parent_instance;
88
89   char *filename;
90 };
91
92 G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
93                          G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
94                                                 g_local_file_file_iface_init))
95
96 static char * find_mountpoint_for (const char *file, dev_t dev);
97
98 static void
99 g_local_file_finalize (GObject *object)
100 {
101   GLocalFile *local;
102
103   local = G_LOCAL_FILE (object);
104
105   g_free (local->filename);
106   
107   if (G_OBJECT_CLASS (g_local_file_parent_class)->finalize)
108     (*G_OBJECT_CLASS (g_local_file_parent_class)->finalize) (object);
109 }
110
111 static void
112 g_local_file_class_init (GLocalFileClass *klass)
113 {
114   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
115   GFileAttributeInfoList *list;
116
117   gobject_class->finalize = g_local_file_finalize;
118
119   /* Set up attribute lists */
120
121   /* Writable attributes: */
122
123   list = g_file_attribute_info_list_new ();
124
125   g_file_attribute_info_list_add (list,
126                                   G_FILE_ATTRIBUTE_UNIX_MODE,
127                                   G_FILE_ATTRIBUTE_TYPE_UINT32,
128                                   G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
129   
130 #ifdef HAVE_CHOWN
131   g_file_attribute_info_list_add (list,
132                                   G_FILE_ATTRIBUTE_UNIX_UID,
133                                   G_FILE_ATTRIBUTE_TYPE_UINT32,
134                                   G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
135   g_file_attribute_info_list_add (list,
136                                   G_FILE_ATTRIBUTE_UNIX_GID,
137                                   G_FILE_ATTRIBUTE_TYPE_UINT32,
138                                   G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
139 #endif
140   
141 #ifdef HAVE_SYMLINK
142   g_file_attribute_info_list_add (list,
143                                   G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET,
144                                   G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
145                                   0);
146 #endif
147   
148 #ifdef HAVE_UTIMES
149   g_file_attribute_info_list_add (list,
150                                   G_FILE_ATTRIBUTE_TIME_MODIFIED,
151                                   G_FILE_ATTRIBUTE_TYPE_UINT64,
152                                   G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
153   g_file_attribute_info_list_add (list,
154                                   G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
155                                   G_FILE_ATTRIBUTE_TYPE_UINT32,
156                                   G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
157   g_file_attribute_info_list_add (list,
158                                   G_FILE_ATTRIBUTE_TIME_ACCESS,
159                                   G_FILE_ATTRIBUTE_TYPE_UINT64,
160                                   G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
161   g_file_attribute_info_list_add (list,
162                                   G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
163                                   G_FILE_ATTRIBUTE_TYPE_UINT32,
164                                   G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
165 #endif
166
167   local_writable_attributes = list;
168
169   /* Writable namespaces: */
170   
171   list = g_file_attribute_info_list_new ();
172
173 #ifdef HAVE_XATTR
174   g_file_attribute_info_list_add (list,
175                                   "xattr",
176                                   G_FILE_ATTRIBUTE_TYPE_STRING,
177                                   G_FILE_ATTRIBUTE_FLAGS_COPY_WITH_FILE |
178                                   G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
179   g_file_attribute_info_list_add (list,
180                                   "xattr_sys",
181                                   G_FILE_ATTRIBUTE_TYPE_STRING,
182                                   G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED);
183 #endif
184
185   local_writable_namespaces = list;
186 }
187
188 static void
189 g_local_file_init (GLocalFile *local)
190 {
191 }
192
193
194 static char *
195 canonicalize_filename (const char *filename)
196 {
197   char *canon, *start, *p, *q;
198   char *cwd;
199   
200   if (!g_path_is_absolute (filename))
201     {
202       cwd = g_get_current_dir ();
203       canon = g_build_filename (cwd, filename, NULL);
204       g_free (cwd);
205     }
206   else
207     canon = g_strdup (filename);
208
209   start = (char *)g_path_skip_root (canon);
210
211   p = start;
212   while (*p != 0)
213     {
214       if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
215         {
216           memmove (p, p+1, strlen (p+1)+1);
217         }
218       else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
219         {
220           q = p + 2;
221           /* Skip previous separator */
222           p = p - 2;
223           if (p < start)
224             p = start;
225           while (p > start && !G_IS_DIR_SEPARATOR (*p))
226             p--;
227           if (G_IS_DIR_SEPARATOR (*p))
228             *p++ = G_DIR_SEPARATOR;
229           memmove (p, q, strlen (q)+1);
230         }
231       else
232         {
233           /* Skip until next separator */
234           while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
235             p++;
236           
237           if (*p != 0)
238             {
239               /* Canonicalize one separator */
240               *p++ = G_DIR_SEPARATOR;
241             }
242         }
243
244       /* Remove additional separators */
245       q = p;
246       while (*q && G_IS_DIR_SEPARATOR (*q))
247         q++;
248
249       if (p != q)
250         memmove (p, q, strlen (q)+1);
251     }
252
253   /* Remove trailing slashes */
254   if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
255     *(p-1) = 0;
256   
257   return canon;
258 }
259
260 /**
261  * g_local_file_new:
262  * @filename: filename of the file to create.
263  * 
264  * Returns: new local #GFile.
265  **/
266 GFile *
267 g_local_file_new (const char *filename)
268 {
269   GLocalFile *local;
270
271   local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
272   local->filename = canonicalize_filename (filename);
273   
274   return G_FILE (local);
275 }
276
277 static gboolean
278 g_local_file_is_native (GFile *file)
279 {
280   return TRUE;
281 }
282
283 static gboolean
284 g_local_file_has_uri_scheme (GFile *file,
285                              const char *uri_scheme)
286 {
287   return g_ascii_strcasecmp (uri_scheme, "file") == 0;
288 }
289
290 static char *
291 g_local_file_get_uri_scheme (GFile *file)
292 {
293   return g_strdup ("file");
294 }
295
296 static char *
297 g_local_file_get_basename (GFile *file)
298 {
299   return g_path_get_basename (G_LOCAL_FILE (file)->filename);
300 }
301
302 static char *
303 g_local_file_get_path (GFile *file)
304 {
305   return g_strdup (G_LOCAL_FILE (file)->filename);
306 }
307
308 static char *
309 g_local_file_get_uri (GFile *file)
310 {
311   return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
312 }
313
314 static gboolean
315 get_filename_charset (const gchar **filename_charset)
316 {
317   const gchar **charsets;
318   gboolean is_utf8;
319   
320   is_utf8 = g_get_filename_charsets (&charsets);
321
322   if (filename_charset)
323     *filename_charset = charsets[0];
324   
325   return is_utf8;
326 }
327
328 static gboolean
329 name_is_valid_for_display (const char *string,
330                            gboolean is_valid_utf8)
331 {
332   char c;
333   
334   if (!is_valid_utf8 &&
335       !g_utf8_validate (string, -1, NULL))
336     return FALSE;
337
338   while ((c = *string++) != 0)
339     {
340       if (g_ascii_iscntrl(c))
341         return FALSE;
342     }
343
344   return TRUE;
345 }
346
347 static char *
348 g_local_file_get_parse_name (GFile *file)
349 {
350   const char *filename;
351   char *parse_name;
352   const gchar *charset;
353   char *utf8_filename;
354   char *roundtripped_filename;
355   gboolean free_utf8_filename;
356   gboolean is_valid_utf8;
357
358   filename = G_LOCAL_FILE (file)->filename;
359   if (get_filename_charset (&charset))
360     {
361       utf8_filename = (char *)filename;
362       free_utf8_filename = FALSE;
363       is_valid_utf8 = FALSE; /* Can't guarantee this */
364     }
365   else
366     {
367       utf8_filename = g_convert (filename, -1, 
368                                  "UTF-8", charset, NULL, NULL, NULL);
369       free_utf8_filename = TRUE;
370       is_valid_utf8 = TRUE;
371
372       if (utf8_filename != NULL)
373         {
374           /* Make sure we can roundtrip: */
375           roundtripped_filename = g_convert (utf8_filename, -1,
376                                              charset, "UTF-8", NULL, NULL, NULL);
377           
378           if (roundtripped_filename == NULL ||
379               strcmp (utf8_filename, roundtripped_filename) != 0)
380             {
381               g_free (utf8_filename);
382               utf8_filename = NULL;
383             }
384         }
385     }
386
387
388   if (utf8_filename != NULL &&
389       name_is_valid_for_display (utf8_filename, is_valid_utf8))
390     {
391       if (free_utf8_filename)
392         parse_name = utf8_filename;
393       else
394         parse_name = g_strdup (utf8_filename);
395     }
396   else
397     {
398       parse_name = g_filename_to_uri (filename, NULL, NULL);
399       if (free_utf8_filename)
400         g_free (utf8_filename);
401     }
402   
403   return parse_name;
404 }
405
406 static GFile *
407 g_local_file_get_parent (GFile *file)
408 {
409   GLocalFile *local = G_LOCAL_FILE (file);
410   const char *non_root;
411   char *dirname;
412   GFile *parent;
413
414   /* Check for root */
415   non_root = g_path_skip_root (local->filename);
416   if (*non_root == 0)
417     return NULL;
418
419   dirname = g_path_get_dirname (local->filename);
420   parent = g_local_file_new (dirname);
421   g_free (dirname);
422   return parent;
423 }
424
425 static GFile *
426 g_local_file_dup (GFile *file)
427 {
428   GLocalFile *local = G_LOCAL_FILE (file);
429
430   return g_local_file_new (local->filename);
431 }
432
433 static guint
434 g_local_file_hash (GFile *file)
435 {
436   GLocalFile *local = G_LOCAL_FILE (file);
437   
438   return g_str_hash (local->filename);
439 }
440
441 static gboolean
442 g_local_file_equal (GFile *file1,
443                     GFile *file2)
444 {
445   GLocalFile *local1 = G_LOCAL_FILE (file1);
446   GLocalFile *local2 = G_LOCAL_FILE (file2);
447
448   return g_str_equal (local1->filename, local2->filename);
449 }
450
451 static const char *
452 match_prefix (const char *path, const char *prefix)
453 {
454   int prefix_len;
455
456   prefix_len = strlen (prefix);
457   if (strncmp (path, prefix, prefix_len) != 0)
458     return NULL;
459   return path + prefix_len;
460 }
461
462 static gboolean
463 g_local_file_contains_file (GFile *parent,
464                             GFile *descendant)
465 {
466   GLocalFile *parent_local = G_LOCAL_FILE (parent);
467   GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
468   const char *remainder;
469
470   remainder = match_prefix (descendant_local->filename, parent_local->filename);
471   if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
472     return TRUE;
473   return FALSE;
474 }
475
476 static char *
477 g_local_file_get_relative_path (GFile *parent,
478                                 GFile *descendant)
479 {
480   GLocalFile *parent_local = G_LOCAL_FILE (parent);
481   GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
482   const char *remainder;
483
484   remainder = match_prefix (descendant_local->filename, parent_local->filename);
485   
486   if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
487     return g_strdup (remainder + 1);
488   return NULL;
489 }
490
491 static GFile *
492 g_local_file_resolve_relative_path (GFile *file,
493                                     const char *relative_path)
494 {
495   GLocalFile *local = G_LOCAL_FILE (file);
496   char *filename;
497   GFile *child;
498
499   if (g_path_is_absolute (relative_path))
500     return g_local_file_new (relative_path);
501   
502   filename = g_build_filename (local->filename, relative_path, NULL);
503   child = g_local_file_new (filename);
504   g_free (filename);
505   
506   return child;
507 }
508
509 static GFileEnumerator *
510 g_local_file_enumerate_children (GFile *file,
511                                  const char *attributes,
512                                  GFileQueryInfoFlags flags,
513                                  GCancellable *cancellable,
514                                  GError **error)
515 {
516   GLocalFile *local = G_LOCAL_FILE (file);
517   return g_local_file_enumerator_new (local->filename,
518                                       attributes, flags,
519                                       cancellable, error);
520 }
521
522 static GFile *
523 g_local_file_get_child_for_display_name (GFile        *file,
524                                          const char   *display_name,
525                                          GError      **error)
526 {
527   GFile *parent, *new_file;
528   char *basename;
529
530   basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
531   if (basename == NULL)
532     {
533       g_set_error (error, G_IO_ERROR,
534                    G_IO_ERROR_INVALID_FILENAME,
535                    _("Invalid filename %s"), display_name);
536       return NULL;
537     }
538
539   parent = g_file_get_parent (file);
540   new_file = g_file_get_child (file, basename);
541   g_object_unref (parent);
542   g_free (basename);
543   
544   return new_file;
545 }
546
547 #ifdef USE_STATFS
548 static const char *
549 get_fs_type (long f_type)
550 {
551
552   /* filesystem ids taken from linux manpage */
553   switch (f_type) {
554   case 0xadf5:
555     return "adfs";
556   case 0xADFF:
557     return "affs";
558   case 0x42465331:
559     return "befs";
560   case 0x1BADFACE:
561     return "bfs";
562   case 0xFF534D42:
563     return "cifs";
564   case 0x73757245:
565     return "coda";
566   case 0x012FF7B7:
567     return "coh";
568   case 0x28cd3d45:
569     return "cramfs";
570   case 0x1373:
571     return "devfs";
572   case 0x00414A53:
573     return "efs";
574   case 0x137D:
575     return "ext";
576   case 0xEF51:
577     return "ext2";
578   case 0xEF53:
579     return "ext3";
580   case 0x4244:
581     return "hfs";
582   case 0xF995E849:
583     return "hpfs";
584   case 0x958458f6:
585     return "hugetlbfs";
586   case 0x9660:
587     return "isofs";
588   case 0x72b6:
589     return "jffs2";
590   case 0x3153464a:
591     return "jfs";
592   case 0x137F:
593     return "minix";
594   case 0x138F:
595     return "minix2";
596   case 0x2468:
597     return "minix2";
598   case 0x2478:
599     return "minix22";
600   case 0x4d44:
601     return "msdos";
602   case 0x564c:
603     return "ncp";
604   case 0x6969:
605     return "nfs";
606   case 0x5346544e:
607     return "ntfs";
608   case 0x9fa1:
609     return "openprom";
610   case 0x9fa0:
611     return "proc";
612   case 0x002f:
613     return "qnx4";
614   case 0x52654973:
615     return "reiserfs";
616   case 0x7275:
617     return "romfs";
618   case 0x517B:
619     return "smb";
620   case 0x012FF7B6:
621     return "sysv2";
622   case 0x012FF7B5:
623     return "sysv4";
624   case 0x01021994:
625     return "tmpfs";
626   case 0x15013346:
627     return "udf";
628   case 0x00011954:
629     return "ufs";
630   case 0x9fa2:
631     return "usbdevice";
632   case 0xa501FCF5:
633     return "vxfs";
634   case 0x012FF7B4:
635     return "xenix";
636   case 0x58465342:
637     return "xfs";
638   case 0x012FD16D:
639     return "xiafs";
640   default:
641     return NULL;
642   }
643 }
644 #endif
645
646 G_LOCK_DEFINE_STATIC(mount_info_hash);
647 static GHashTable *mount_info_hash = NULL;
648 guint64 mount_info_hash_cache_time = 0;
649
650 typedef enum {
651   MOUNT_INFO_READONLY = 1<<0
652 } MountInfo;
653
654 static gboolean
655 device_equal (gconstpointer v1,
656            gconstpointer v2)
657 {
658   return *(dev_t *)v1 == * (dev_t *)v2;
659 }
660
661 static guint
662 device_hash (gconstpointer  v)
663 {
664   return (guint) *(dev_t *)v;
665 }
666
667 static void
668 get_mount_info (GFileInfo *fs_info,
669                 const char *path,
670                 GFileAttributeMatcher *matcher)
671 {
672   struct stat buf;
673   gboolean got_info;
674   gpointer info_as_ptr;
675   guint mount_info;
676   char *mountpoint;
677   dev_t *dev;
678   GUnixMount *mount;
679   guint64 cache_time;
680
681   if (lstat (path, &buf) != 0)
682     return;
683
684   G_LOCK (mount_info_hash);
685
686   if (mount_info_hash == NULL)
687     mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
688                                              g_free, NULL);
689
690
691   if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
692     g_hash_table_remove_all (mount_info_hash);
693   
694   got_info = g_hash_table_lookup_extended (mount_info_hash,
695                                            &buf.st_dev,
696                                            NULL,
697                                            &info_as_ptr);
698   
699   G_UNLOCK (mount_info_hash);
700   
701   mount_info = GPOINTER_TO_UINT (info_as_ptr);
702   
703   if (!got_info)
704     {
705       mount_info = 0;
706
707       mountpoint = find_mountpoint_for (path, buf.st_dev);
708       if (mountpoint == NULL)
709         mountpoint = "/";
710
711       mount = g_get_unix_mount_at (mountpoint, &cache_time);
712       if (mount)
713         {
714           if (g_unix_mount_is_readonly (mount))
715             mount_info |= MOUNT_INFO_READONLY;
716           
717           g_unix_mount_free (mount);
718         }
719
720       dev = g_new0 (dev_t, 1);
721       *dev = buf.st_dev;
722       
723       G_LOCK (mount_info_hash);
724       mount_info_hash_cache_time = cache_time;
725       g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
726       G_UNLOCK (mount_info_hash);
727     }
728
729   if (mount_info & MOUNT_INFO_READONLY)
730     g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FS_READONLY, TRUE);
731 }
732
733 static GFileInfo *
734 g_local_file_query_filesystem_info (GFile                *file,
735                                     const char           *attributes,
736                                     GCancellable         *cancellable,
737                                     GError              **error)
738 {
739   GLocalFile *local = G_LOCAL_FILE (file);
740   GFileInfo *info;
741   int statfs_result;
742   gboolean no_size;
743   guint64 block_size;
744 #ifdef USE_STATFS
745   struct statfs statfs_buffer;
746   const char *fstype;
747 #elif defined(USE_STATVFS)
748   struct statvfs statfs_buffer;
749 #endif
750   GFileAttributeMatcher *attribute_matcher;
751         
752   no_size = FALSE;
753   
754 #ifdef USE_STATFS
755   
756 #if STATFS_ARGS == 2
757   statfs_result = statfs (local->filename, &statfs_buffer);
758 #elif STATFS_ARGS == 4
759   statfs_result = statfs (local->filename, &statfs_buffer,
760                           sizeof (statfs_buffer), 0);
761 #endif
762   block_size = statfs_buffer.f_bsize;
763   
764 #if defined(__linux__)
765   /* ncpfs does not know the amount of available and free space *
766    * assuming ncpfs is linux specific, if you are on a non-linux platform
767    * where ncpfs is available, please file a bug about it on bugzilla.gnome.org
768    */
769   if (statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0 &&
770       /* linux/ncp_fs.h: NCP_SUPER_MAGIC == 0x564c */
771       statfs_buffer.f_type == 0x564c)
772     no_size = TRUE;
773 #endif
774   
775 #elif defined(USE_STATVFS)
776   statfs_result = statvfs (local->filename, &statfs_buffer);
777   block_size = statfs_buffer.f_frsize; 
778 #endif
779
780   if (statfs_result == -1)
781     {
782       g_set_error (error, G_IO_ERROR,
783                    g_io_error_from_errno (errno),
784                    _("Error getting filesystem info: %s"),
785                    g_strerror (errno));
786       return NULL;
787     }
788
789   info = g_file_info_new ();
790
791   attribute_matcher = g_file_attribute_matcher_new (attributes);
792   
793   if (g_file_attribute_matcher_matches (attribute_matcher,
794                                         G_FILE_ATTRIBUTE_FS_FREE))
795     g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FS_FREE, block_size * statfs_buffer.f_bavail);
796   if (g_file_attribute_matcher_matches (attribute_matcher,
797                                         G_FILE_ATTRIBUTE_FS_SIZE))
798     g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FS_SIZE, block_size * statfs_buffer.f_blocks);
799
800 #ifdef USE_STATFS
801   fstype = get_fs_type (statfs_buffer.f_type);
802   if (fstype &&
803       g_file_attribute_matcher_matches (attribute_matcher,
804                                         G_FILE_ATTRIBUTE_FS_TYPE))
805     g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FS_TYPE, fstype);
806 #endif  
807
808   if (g_file_attribute_matcher_matches (attribute_matcher,
809                                         G_FILE_ATTRIBUTE_FS_READONLY))
810     {
811       get_mount_info (info, local->filename, attribute_matcher);
812     }
813   
814   g_file_attribute_matcher_unref (attribute_matcher);
815   
816   return info;
817 }
818
819 static GVolume *
820 g_local_file_find_enclosing_volume (GFile *file,
821                                     GCancellable *cancellable,
822                                     GError **error)
823 {
824   GLocalFile *local = G_LOCAL_FILE (file);
825   struct stat buf;
826   char *mountpoint;
827   GVolume *volume;
828
829   if (lstat (local->filename, &buf) != 0)
830     {
831       g_set_error (error, G_IO_ERROR,
832                    G_IO_ERROR_NOT_FOUND,
833                    _("Containing volume does not exist"));
834       return NULL;
835     }
836
837   mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
838   if (mountpoint == NULL)
839     {
840       g_set_error (error, G_IO_ERROR,
841                    G_IO_ERROR_NOT_FOUND,
842                    _("Containing volume does not exist"));
843       return NULL;
844     }
845
846   volume = g_volume_get_for_mount_path (mountpoint);
847   g_free (mountpoint);
848   if (volume)
849     return volume;
850
851   g_set_error (error, G_IO_ERROR,
852                G_IO_ERROR_NOT_FOUND,
853                _("Containing volume does not exist"));
854   return NULL;
855 }
856
857 static GFile *
858 g_local_file_set_display_name (GFile *file,
859                                const char *display_name,
860                                GCancellable *cancellable,
861                                GError **error)
862 {
863   GLocalFile *local, *new_local;
864   GFile *new_file, *parent;
865   struct stat statbuf;
866   int errsv;
867
868   parent = g_file_get_parent (file);
869   if (parent == NULL)
870     {
871       g_set_error (error, G_IO_ERROR,
872                    G_IO_ERROR_FAILED,
873                    _("Can't rename root directory"));
874       return NULL;
875     }
876   
877   new_file = g_file_get_child_for_display_name  (parent, display_name, error);
878   g_object_unref (parent);
879   
880   if (new_file == NULL)
881     return NULL;
882   
883   local = G_LOCAL_FILE (file);
884   new_local = G_LOCAL_FILE (new_file);
885
886   if (!(g_lstat (new_local->filename, &statbuf) == -1 &&
887         errno == ENOENT))
888     {
889       g_set_error (error, G_IO_ERROR,
890                    G_IO_ERROR_EXISTS,
891                    _("Can't rename file, filename already exist"));
892       return NULL;
893     }
894
895   if (rename (local->filename, new_local->filename) == -1)
896     {
897       errsv = errno;
898
899       if (errsv == EINVAL)
900         /* We can't get a rename file into itself error herer,
901            so this must be an invalid filename, on e.g. FAT */
902         g_set_error (error, G_IO_ERROR,
903                      G_IO_ERROR_INVALID_FILENAME,
904                      _("Invalid filename"));
905       else
906         g_set_error (error, G_IO_ERROR,
907                      g_io_error_from_errno (errsv),
908                      _("Error renaming file: %s"),
909                      g_strerror (errsv));
910       g_object_unref (new_file);
911       return NULL;
912     }
913   
914   return new_file;
915 }
916
917 static GFileInfo *
918 g_local_file_query_info (GFile                *file,
919                          const char           *attributes,
920                          GFileQueryInfoFlags   flags,
921                          GCancellable         *cancellable,
922                          GError              **error)
923 {
924   GLocalFile *local = G_LOCAL_FILE (file);
925   GFileInfo *info;
926   GFileAttributeMatcher *matcher;
927   char *basename, *dirname;
928   GLocalParentFileInfo parent_info;
929
930   matcher = g_file_attribute_matcher_new (attributes);
931   
932   basename = g_path_get_basename (local->filename);
933   
934   dirname = g_path_get_dirname (local->filename);
935   _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
936   g_free (dirname);
937   
938   info = _g_local_file_info_get (basename, local->filename,
939                                  matcher, flags, &parent_info,
940                                  error);
941   
942   g_free (basename);
943
944   g_file_attribute_matcher_unref (matcher);
945
946   return info;
947 }
948
949 static GFileAttributeInfoList *
950 g_local_file_query_settable_attributes (GFile                      *file,
951                                         GCancellable               *cancellable,
952                                         GError                    **error)
953 {
954   return g_file_attribute_info_list_ref (local_writable_attributes);
955 }
956
957 static GFileAttributeInfoList *
958 g_local_file_query_writable_namespaces (GFile *file,
959                                         GCancellable *cancellable,
960                                         GError **error)
961 {
962   return g_file_attribute_info_list_ref (local_writable_namespaces);
963 }
964
965 static gboolean
966 g_local_file_set_attribute (GFile *file,
967                             const char *attribute,
968                             const GFileAttributeValue *value,
969                             GFileQueryInfoFlags flags,
970                             GCancellable *cancellable,
971                             GError **error)
972 {
973   GLocalFile *local = G_LOCAL_FILE (file);
974
975   return _g_local_file_info_set_attribute (local->filename,
976                                            attribute,
977                                            value,
978                                            flags,
979                                            cancellable,
980                                            error);
981 }
982
983 static gboolean
984 g_local_file_set_attributes_from_info (GFile *file,
985                                        GFileInfo *info,
986                                        GFileQueryInfoFlags flags,
987                                        GCancellable *cancellable,
988                                        GError **error)
989 {
990   GLocalFile *local = G_LOCAL_FILE (file);
991   int res, chained_res;
992   GFileIface* default_iface;
993
994   res = _g_local_file_info_set_attributes (local->filename,
995                                            info, flags, 
996                                            cancellable,
997                                            error);
998
999   if (!res)
1000     error = NULL; /* Don't write over error if further errors */
1001
1002   default_iface = g_type_default_interface_peek (G_TYPE_FILE);
1003
1004   chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
1005   
1006   return res && chained_res;
1007 }
1008
1009 static GFileInputStream *
1010 g_local_file_read (GFile *file,
1011                    GCancellable *cancellable,
1012                    GError **error)
1013 {
1014   GLocalFile *local = G_LOCAL_FILE (file);
1015   int fd;
1016   struct stat buf;
1017   
1018   fd = g_open (local->filename, O_RDONLY, 0);
1019   if (fd == -1)
1020     {
1021       g_set_error (error, G_IO_ERROR,
1022                    g_io_error_from_errno (errno),
1023                    _("Error opening file: %s"),
1024                    g_strerror (errno));
1025       return NULL;
1026     }
1027
1028   if (fstat(fd, &buf) == 0 && S_ISDIR (buf.st_mode))
1029     {
1030       close (fd);
1031       g_set_error (error, G_IO_ERROR,
1032                    G_IO_ERROR_IS_DIRECTORY,
1033                    _("Can't open directory"));
1034       return NULL;
1035     }
1036   
1037   return g_local_file_input_stream_new (fd);
1038 }
1039
1040 static GFileOutputStream *
1041 g_local_file_append_to (GFile *file,
1042                         GFileCreateFlags flags,
1043                         GCancellable *cancellable,
1044                         GError **error)
1045 {
1046   return g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
1047                                             flags, cancellable, error);
1048 }
1049
1050 static GFileOutputStream *
1051 g_local_file_create (GFile *file,
1052                      GFileCreateFlags flags,
1053                      GCancellable *cancellable,
1054                      GError **error)
1055 {
1056   return g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
1057                                             flags, cancellable, error);
1058 }
1059
1060 static GFileOutputStream *
1061 g_local_file_replace (GFile *file,
1062                       const char *etag,
1063                       gboolean make_backup,
1064                       GFileCreateFlags flags,
1065                       GCancellable *cancellable,
1066                       GError **error)
1067 {
1068   return g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
1069                                              etag, make_backup, flags,
1070                                              cancellable, error);
1071 }
1072
1073
1074 static gboolean
1075 g_local_file_delete (GFile *file,
1076                      GCancellable *cancellable,
1077                      GError **error)
1078 {
1079   GLocalFile *local = G_LOCAL_FILE (file);
1080   
1081   if (g_remove (local->filename) == -1)
1082     {
1083       g_set_error (error, G_IO_ERROR,
1084                    g_io_error_from_errno (errno),
1085                    _("Error removing file: %s"),
1086                    g_strerror (errno));
1087       return FALSE;
1088     }
1089   
1090   return TRUE;
1091 }
1092
1093 static char *
1094 strip_trailing_slashes (const char *path)
1095 {
1096   char *path_copy;
1097   int len;
1098
1099   path_copy = g_strdup (path);
1100   len = strlen (path_copy);
1101   while (len > 1 && path_copy[len-1] == '/')
1102     path_copy[--len] = 0;
1103
1104   return path_copy;
1105  }
1106
1107 static char *
1108 expand_symlink (const char *link)
1109 {
1110   char *resolved, *canonical, *parent, *link2;
1111   char symlink_value[4096];
1112   ssize_t res;
1113   
1114   res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
1115   if (res == -1)
1116     return g_strdup (link);
1117   symlink_value[res] = 0;
1118   
1119   if (g_path_is_absolute (symlink_value))
1120     return canonicalize_filename (symlink_value);
1121   else
1122     {
1123       link2 = strip_trailing_slashes (link);
1124       parent = g_path_get_dirname (link2);
1125       g_free (link2);
1126       
1127       resolved = g_build_filename (parent, symlink_value, NULL);
1128       g_free (parent);
1129       
1130       canonical = canonicalize_filename (resolved);
1131       
1132       g_free (resolved);
1133
1134       return canonical;
1135     }
1136 }
1137
1138 static char *
1139 get_parent (const char *path, dev_t *parent_dev)
1140 {
1141   char *parent, *tmp;
1142   struct stat parent_stat;
1143   int num_recursions;
1144   char *path_copy;
1145
1146   path_copy = strip_trailing_slashes (path);
1147   
1148   parent = g_path_get_dirname (path_copy);
1149   if (strcmp (parent, ".") == 0 ||
1150       strcmp (parent, path_copy) == 0)
1151     {
1152       g_free (path_copy);
1153       return NULL;
1154     }
1155   g_free (path_copy);
1156
1157   num_recursions = 0;
1158   do {
1159     if (g_lstat (parent, &parent_stat) != 0)
1160       {
1161         g_free (parent);
1162         return NULL;
1163       }
1164     
1165     if (S_ISLNK (parent_stat.st_mode))
1166       {
1167         tmp = parent;
1168         parent = expand_symlink (parent);
1169         g_free (tmp);
1170       }
1171     
1172     num_recursions++;
1173     if (num_recursions > 12)
1174       {
1175         g_free (parent);
1176         return NULL;
1177       }
1178   } while (S_ISLNK (parent_stat.st_mode));
1179
1180   *parent_dev = parent_stat.st_dev;
1181   
1182   return parent;
1183 }
1184
1185 static char *
1186 expand_all_symlinks (const char *path)
1187 {
1188   char *parent, *parent_expanded;
1189   char *basename, *res;
1190   dev_t parent_dev;
1191
1192   parent = get_parent (path, &parent_dev);
1193   if (parent)
1194     {
1195       parent_expanded = expand_all_symlinks (parent);
1196       g_free (parent);
1197       basename = g_path_get_basename (path);
1198       res = g_build_filename (parent_expanded, basename, NULL);
1199       g_free (basename);
1200       g_free (parent_expanded);
1201     }
1202   else
1203     res = g_strdup (path);
1204   
1205   return res;
1206 }
1207
1208 static char *
1209 find_mountpoint_for (const char *file, dev_t dev)
1210 {
1211   char *dir, *parent;
1212   dev_t dir_dev, parent_dev;
1213
1214   dir = g_strdup (file);
1215   dir_dev = dev;
1216
1217   while (1) {
1218     parent = get_parent (dir, &parent_dev);
1219     if (parent == NULL)
1220       return dir;
1221     
1222     if (parent_dev != dir_dev)
1223       {
1224         g_free (parent);
1225         return dir;
1226       }
1227     
1228     g_free (dir);
1229     dir = parent;
1230   }
1231 }
1232
1233 static char *
1234 find_topdir_for (const char *file)
1235 {
1236   char *dir;
1237   dev_t dir_dev;
1238
1239   dir = get_parent (file, &dir_dev);
1240   if (dir == NULL)
1241     return NULL;
1242
1243   return find_mountpoint_for (dir, dir_dev);
1244 }
1245
1246 static char *
1247 get_unique_filename (const char *basename, int id)
1248 {
1249   const char *dot;
1250       
1251   if (id == 1)
1252     return g_strdup (basename);
1253
1254   dot = strchr (basename, '.');
1255   if (dot)
1256     return g_strdup_printf ("%.*s.%d%s", dot - basename, basename, id, dot);
1257   else
1258     return g_strdup_printf ("%s.%d", basename, id);
1259 }
1260
1261 static gboolean
1262 path_has_prefix (const char *path, const char *prefix)
1263 {
1264   int prefix_len;
1265
1266   if (prefix == NULL)
1267     return TRUE;
1268
1269   prefix_len = strlen (prefix);
1270   
1271   if (strncmp (path, prefix, prefix_len) == 0 &&
1272       (prefix_len == 0 || /* empty prefix always matches */
1273        prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
1274        path[prefix_len] == 0 ||
1275        path[prefix_len] == '/'))
1276     return TRUE;
1277   
1278   return FALSE;
1279 }
1280
1281 static char *
1282 try_make_relative (const char *path, const char *base)
1283 {
1284   char *path2, *base2;
1285   char *relative;
1286
1287   path2 = expand_all_symlinks (path);
1288   base2 = expand_all_symlinks (base);
1289
1290   relative = NULL;
1291   if (path_has_prefix (path2, base2))
1292     {
1293       relative = path2 + strlen (base2);
1294       while (*relative == '/')
1295         relative ++;
1296       relative = g_strdup (relative);
1297     }
1298   g_free (path2);
1299   g_free (base2);
1300
1301   if (relative)
1302     return relative;
1303   
1304   /* Failed, use abs path */
1305   return g_strdup (path);
1306 }
1307
1308 static char *
1309 escape_trash_name (char *name)
1310 {
1311   GString *str;
1312   const gchar hex[16] = "0123456789ABCDEF";
1313   
1314   str = g_string_new ("");
1315
1316   while (*name != 0)
1317     {
1318       char c;
1319
1320       c = *name++;
1321
1322       if (g_ascii_isprint (c))
1323         g_string_append_c (str, c);
1324       else
1325         {
1326           g_string_append_c (str, '%');
1327           g_string_append_c (str, hex[((guchar)c) >> 4]);
1328           g_string_append_c (str, hex[((guchar)c) & 0xf]);
1329         }
1330     }
1331
1332   return g_string_free (str, FALSE);
1333 }
1334
1335 static gboolean
1336 g_local_file_trash (GFile *file,
1337                     GCancellable *cancellable,
1338                     GError **error)
1339 {
1340   GLocalFile *local = G_LOCAL_FILE (file);
1341   struct stat file_stat, home_stat, trash_stat, global_stat;
1342   const char *homedir;
1343   char *dirname;
1344   char *trashdir, *globaldir, *topdir, *infodir, *filesdir;
1345   char *basename, *trashname, *trashfile, *infoname, *infofile;
1346   char *original_name, *original_name_escaped;
1347   uid_t uid;
1348   char uid_str[32];
1349   int i;
1350   char *data;
1351   gboolean is_homedir_trash;
1352   time_t t;
1353   struct tm now;
1354   char delete_time[32];
1355   int fd;
1356   
1357   if (g_lstat (local->filename, &file_stat) != 0)
1358     {
1359       g_set_error (error, G_IO_ERROR,
1360                    g_io_error_from_errno (errno),
1361                    _("Error trashing file: %s"),
1362                    g_strerror (errno));
1363       return FALSE;
1364     }
1365     
1366   homedir = g_get_home_dir ();
1367   g_stat (homedir, &home_stat);
1368
1369   is_homedir_trash = FALSE;
1370   trashdir = NULL;
1371   if (file_stat.st_dev == home_stat.st_dev)
1372     {
1373       is_homedir_trash = TRUE;
1374       errno = 0;
1375       trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
1376       if (g_mkdir_with_parents (trashdir, 0700) < 0)
1377         {
1378           char *display_name;
1379           int err;
1380
1381           err = errno;
1382           display_name = g_filename_display_name (trashdir);
1383           g_set_error (error, G_IO_ERROR,
1384                        g_io_error_from_errno (err),
1385                        _("Unable to create trash dir %s: %s"),
1386                        display_name, g_strerror (err));
1387           g_free (display_name);
1388           g_free (trashdir);
1389           return FALSE;
1390         }
1391       topdir = g_strdup (g_get_user_data_dir ());
1392     }
1393   else
1394     {
1395       uid = geteuid ();
1396       g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
1397       
1398       topdir = find_topdir_for (local->filename);
1399       if (topdir == NULL)
1400         {
1401           g_set_error (error, G_IO_ERROR,
1402                        G_IO_ERROR_NOT_SUPPORTED,
1403                        _("Unable to find toplevel directory for trash"));
1404           return FALSE;
1405         }
1406       
1407       /* Try looking for global trash dir $topdir/.Trash/$uid */
1408       globaldir = g_build_filename (topdir, ".Trash", NULL);
1409       if (g_lstat (globaldir, &global_stat) == 0 &&
1410           S_ISDIR (global_stat.st_mode) &&
1411           (global_stat.st_mode & S_ISVTX) != 0)
1412         {
1413           trashdir = g_build_filename (globaldir, uid_str, NULL);
1414
1415           if (g_lstat (trashdir, &trash_stat) == 0)
1416             {
1417               if (!S_ISDIR (trash_stat.st_mode) ||
1418                   trash_stat.st_uid != uid)
1419                 {
1420                   /* Not a directory or not owned by user, ignore */
1421                   g_free (trashdir);
1422                   trashdir = NULL;
1423                 }
1424             }
1425           else if (g_mkdir (trashdir, 0700) == -1)
1426             {
1427               g_free (trashdir);
1428               trashdir = NULL;
1429             }
1430         }
1431       g_free (globaldir);
1432
1433       if (trashdir == NULL)
1434         {
1435           /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
1436           dirname = g_strdup_printf (".Trash-%s", uid_str);
1437           trashdir = g_build_filename (topdir, dirname, NULL);
1438           g_free (dirname);
1439           
1440           if (g_lstat (trashdir, &trash_stat) == 0)
1441             {
1442               if (!S_ISDIR (trash_stat.st_mode) ||
1443                   trash_stat.st_uid != uid)
1444                 {
1445                   /* Not a directory or not owned by user, ignore */
1446                   g_free (trashdir);
1447                   trashdir = NULL;
1448                 }
1449             }
1450           else if (g_mkdir (trashdir, 0700) == -1)
1451             {
1452               g_free (trashdir);
1453               trashdir = NULL;
1454             }
1455         }
1456
1457       if (trashdir == NULL)
1458         {
1459           g_free (topdir);
1460           g_set_error (error, G_IO_ERROR,
1461                        G_IO_ERROR_NOT_SUPPORTED,
1462                        _("Unable to find or create trash directory"));
1463           return FALSE;
1464         }
1465     }
1466
1467   /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
1468
1469   infodir = g_build_filename (trashdir, "info", NULL);
1470   filesdir = g_build_filename (trashdir, "files", NULL);
1471   g_free (trashdir);
1472
1473   /* Make sure we have the subdirectories */
1474   if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
1475       (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
1476     {
1477       g_free (topdir);
1478       g_free (infodir);
1479       g_free (filesdir);
1480       g_set_error (error, G_IO_ERROR,
1481                    G_IO_ERROR_NOT_SUPPORTED,
1482                    _("Unable to find or create trash directory"));
1483       return FALSE;
1484     }  
1485
1486   basename = g_path_get_basename (local->filename);
1487   i = 1;
1488   trashname = NULL;
1489   infofile = NULL;
1490   do {
1491     g_free (trashname);
1492     g_free (infofile);
1493     
1494     trashname = get_unique_filename (basename, i++);
1495     infoname = g_strconcat (trashname, ".trashinfo", NULL);
1496     infofile = g_build_filename (infodir, infoname, NULL);
1497     g_free (infoname);
1498
1499     fd = open (infofile, O_CREAT | O_EXCL, 0666);
1500   } while (fd == -1 && errno == EEXIST);
1501
1502   g_free (basename);
1503   g_free (infodir);
1504
1505   if (fd == -1)
1506     {
1507       g_free (filesdir);
1508       g_free (topdir);
1509       g_free (trashname);
1510       g_free (infofile);
1511       
1512       g_set_error (error, G_IO_ERROR,
1513                    g_io_error_from_errno (errno),
1514                    _("Unable to create trashed file: %s"),
1515                    g_strerror (errno));
1516       return FALSE;
1517     }
1518
1519   close (fd);
1520
1521   /* TODO: Maybe we should verify that you can delete the file from the trash
1522      before moving it? OTOH, that is hard, as it needs a recursive scan */
1523
1524   trashfile = g_build_filename (filesdir, trashname, NULL);
1525
1526   g_free (filesdir);
1527
1528   if (g_rename (local->filename, trashfile) == -1)
1529     {
1530       g_free (topdir);
1531       g_free (trashname);
1532       g_free (infofile);
1533       g_free (trashfile);
1534       
1535       g_set_error (error, G_IO_ERROR,
1536                    g_io_error_from_errno (errno),
1537                    _("Unable to trash file: %s"),
1538                    g_strerror (errno));
1539       return FALSE;
1540     }
1541
1542   g_free (trashfile);
1543
1544   /* TODO: Do we need to update mtime/atime here after the move? */
1545
1546   /* Use absolute names for homedir */
1547   if (is_homedir_trash)
1548     original_name = g_strdup (local->filename);
1549   else
1550     original_name = try_make_relative (local->filename, topdir);
1551   original_name_escaped = escape_trash_name (original_name);
1552   
1553   g_free (original_name);
1554   g_free (topdir);
1555   
1556   t = time (NULL);
1557   localtime_r (&t, &now);
1558   delete_time[0] = 0;
1559   strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
1560
1561   data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
1562                           original_name_escaped, delete_time);
1563
1564   g_file_set_contents (infofile, data, -1, NULL);
1565   g_free (infofile);
1566   g_free (data);
1567   
1568   g_free (original_name_escaped);
1569   g_free (trashname);
1570   
1571   return TRUE;
1572 }
1573
1574 static gboolean
1575 g_local_file_make_directory (GFile *file,
1576                              GCancellable *cancellable,
1577                              GError **error)
1578 {
1579   GLocalFile *local = G_LOCAL_FILE (file);
1580   
1581   if (g_mkdir (local->filename, 0755) == -1)
1582     {
1583       int errsv = errno;
1584
1585       if (errsv == EINVAL)
1586         /* This must be an invalid filename, on e.g. FAT */
1587         g_set_error (error, G_IO_ERROR,
1588                      G_IO_ERROR_INVALID_FILENAME,
1589                      _("Invalid filename"));
1590       else
1591         g_set_error (error, G_IO_ERROR,
1592                      g_io_error_from_errno (errsv),
1593                      _("Error removing file: %s"),
1594                      g_strerror (errsv));
1595       return FALSE;
1596     }
1597   
1598   return TRUE;
1599 }
1600
1601 static gboolean
1602 g_local_file_make_symbolic_link (GFile *file,
1603                                  const char *symlink_value,
1604                                  GCancellable *cancellable,
1605                                  GError **error)
1606 {
1607 #ifdef HAVE_SYMLINK
1608   GLocalFile *local = G_LOCAL_FILE (file);
1609   
1610   if (symlink (symlink_value, local->filename) == -1)
1611     {
1612       int errsv = errno;
1613
1614       if (errsv == EINVAL)
1615         /* This must be an invalid filename, on e.g. FAT */
1616         g_set_error (error, G_IO_ERROR,
1617                      G_IO_ERROR_INVALID_FILENAME,
1618                      _("Invalid filename"));
1619       else
1620         g_set_error (error, G_IO_ERROR,
1621                      g_io_error_from_errno (errsv),
1622                      _("Error making symbolic link: %s"),
1623                      g_strerror (errsv));
1624       return FALSE;
1625     }
1626   return TRUE;
1627 #else
1628   g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported");
1629   return FALSE;
1630 #endif
1631 }
1632
1633
1634 static gboolean
1635 g_local_file_copy (GFile                *source,
1636                    GFile                *destination,
1637                    GFileCopyFlags        flags,
1638                    GCancellable         *cancellable,
1639                    GFileProgressCallback progress_callback,
1640                    gpointer              progress_callback_data,
1641                    GError              **error)
1642 {
1643   /* Fall back to default copy */
1644   g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
1645   return FALSE;
1646 }
1647
1648 static gboolean
1649 g_local_file_move (GFile                *source,
1650                    GFile                *destination,
1651                    GFileCopyFlags        flags,
1652                    GCancellable         *cancellable,
1653                    GFileProgressCallback progress_callback,
1654                    gpointer              progress_callback_data,
1655                    GError              **error)
1656 {
1657   GLocalFile *local_source = G_LOCAL_FILE (source);
1658   GLocalFile *local_destination = G_LOCAL_FILE (destination);
1659   struct stat statbuf;
1660   gboolean destination_exist, source_is_dir;
1661   char *backup_name;
1662   int res;
1663
1664   res = g_lstat (local_source->filename, &statbuf);
1665   if (res == -1)
1666     {
1667       g_set_error (error, G_IO_ERROR,
1668                    g_io_error_from_errno (errno),
1669                    _("Error moving file: %s"),
1670                    g_strerror (errno));
1671       return FALSE;
1672     }
1673   else
1674     source_is_dir = S_ISDIR (statbuf.st_mode);
1675   
1676   destination_exist = FALSE;
1677   res = g_lstat (local_destination->filename, &statbuf);
1678   if (res == 0)
1679     {
1680       destination_exist = TRUE; /* Target file exists */
1681
1682       if (flags & G_FILE_COPY_OVERWRITE)
1683         {
1684           /* Always fail on dirs, even with overwrite */
1685           if (S_ISDIR (statbuf.st_mode))
1686             {
1687               g_set_error (error,
1688                            G_IO_ERROR,
1689                            G_IO_ERROR_WOULD_MERGE,
1690                            _("Can't move directory over directory"));
1691               return FALSE;
1692             }
1693         }
1694       else
1695         {
1696           g_set_error (error,
1697                        G_IO_ERROR,
1698                        G_IO_ERROR_EXISTS,
1699                        _("Target file already exists"));
1700           return FALSE;
1701         }
1702     }
1703   
1704   if (flags & G_FILE_COPY_BACKUP && destination_exist)
1705     {
1706       backup_name = g_strconcat (local_destination->filename, "~", NULL);
1707       if (rename (local_destination->filename, backup_name) == -1)
1708         {
1709           g_set_error (error,
1710                        G_IO_ERROR,
1711                        G_IO_ERROR_CANT_CREATE_BACKUP,
1712                        _("Backup file creation failed"));
1713           g_free (backup_name);
1714           return FALSE;
1715         }
1716       g_free (backup_name);
1717       destination_exist = FALSE; /* It did, but no more */
1718     }
1719
1720   if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
1721     {
1722       /* Source is a dir, destination exists (and is not a dir, because that would have failed
1723          earlier), and we're overwriting. Manually remove the target so we can do the rename. */
1724       res = unlink (local_destination->filename);
1725       if (res == -1)
1726         {
1727           g_set_error (error, G_IO_ERROR,
1728                        g_io_error_from_errno (errno),
1729                        _("Error removing target file: %s"),
1730                        g_strerror (errno));
1731           return FALSE;
1732         }
1733     }
1734   
1735   if (rename (local_source->filename, local_destination->filename) == -1)
1736     {
1737       int errsv = errno;
1738       if (errsv == EXDEV)
1739         goto fallback;
1740
1741       if (errsv == EINVAL)
1742         /* This must be an invalid filename, on e.g. FAT, or
1743            we're trying to move the file into itself...
1744            We return invalid filename for both... */
1745         g_set_error (error, G_IO_ERROR,
1746                      G_IO_ERROR_INVALID_FILENAME,
1747                      _("Invalid filename"));
1748       else
1749         g_set_error (error, G_IO_ERROR,
1750                      g_io_error_from_errno (errsv),
1751                      _("Error moving file: %s"),
1752                      g_strerror (errsv));
1753       return FALSE;
1754
1755     }
1756   return TRUE;
1757
1758  fallback:
1759
1760   if (!g_file_copy (source, destination, G_FILE_COPY_OVERWRITE | G_FILE_COPY_ALL_METADATA, cancellable,
1761                     progress_callback, progress_callback_data,
1762                     error))
1763     return FALSE;
1764   
1765   return g_file_delete (source, cancellable, error);
1766 }
1767
1768
1769 static GDirectoryMonitor*
1770 g_local_file_monitor_dir (GFile* file,
1771                           GFileMonitorFlags flags,
1772                           GCancellable *cancellable)
1773 {
1774   GLocalFile* local_file = G_LOCAL_FILE(file);
1775   return g_local_directory_monitor_new (local_file->filename, flags);
1776 }
1777
1778 static GFileMonitor*
1779 g_local_file_monitor_file (GFile* file,
1780                            GFileMonitorFlags flags,
1781                            GCancellable *cancellable)
1782 {
1783   GLocalFile* local_file = G_LOCAL_FILE(file);
1784   return g_local_file_monitor_new (local_file->filename, flags);
1785 }
1786
1787 static void
1788 g_local_file_file_iface_init (GFileIface *iface)
1789 {
1790   iface->dup = g_local_file_dup;
1791   iface->hash = g_local_file_hash;
1792   iface->equal = g_local_file_equal;
1793   iface->is_native = g_local_file_is_native;
1794   iface->has_uri_scheme = g_local_file_has_uri_scheme;
1795   iface->get_uri_scheme = g_local_file_get_uri_scheme;
1796   iface->get_basename = g_local_file_get_basename;
1797   iface->get_path = g_local_file_get_path;
1798   iface->get_uri = g_local_file_get_uri;
1799   iface->get_parse_name = g_local_file_get_parse_name;
1800   iface->get_parent = g_local_file_get_parent;
1801   iface->contains_file = g_local_file_contains_file;
1802   iface->get_relative_path = g_local_file_get_relative_path;
1803   iface->resolve_relative_path = g_local_file_resolve_relative_path;
1804   iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
1805   iface->set_display_name = g_local_file_set_display_name;
1806   iface->enumerate_children = g_local_file_enumerate_children;
1807   iface->query_info = g_local_file_query_info;
1808   iface->query_filesystem_info = g_local_file_query_filesystem_info;
1809   iface->find_enclosing_volume = g_local_file_find_enclosing_volume;
1810   iface->query_settable_attributes = g_local_file_query_settable_attributes;
1811   iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
1812   iface->set_attribute = g_local_file_set_attribute;
1813   iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
1814   iface->read = g_local_file_read;
1815   iface->append_to = g_local_file_append_to;
1816   iface->create = g_local_file_create;
1817   iface->replace = g_local_file_replace;
1818   iface->delete_file = g_local_file_delete;
1819   iface->trash = g_local_file_trash;
1820   iface->make_directory = g_local_file_make_directory;
1821   iface->make_symbolic_link = g_local_file_make_symbolic_link;
1822   iface->copy = g_local_file_copy;
1823   iface->move = g_local_file_move;
1824   iface->monitor_dir = g_local_file_monitor_dir;
1825   iface->monitor_file = g_local_file_monitor_file;
1826 }