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