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