Don't do fallback for user-home and user-desktop to avoid problems with
[platform/upstream/glib.git] / gio / glocalfileinfo.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4  * 
5  * Copyright (C) 2006-2007 Red Hat, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * Author: Alexander Larsson <alexl@redhat.com>
23  */
24
25 #include <config.h>
26
27 #ifdef HAVE_SYS_TIME_H
28 #include <sys/time.h>
29 #endif
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <fcntl.h>
37 #include <errno.h>
38 #ifdef HAVE_GRP_H
39 #include <grp.h>
40 #endif
41 #ifdef HAVE_PWD_H
42 #include <pwd.h>
43 #endif
44 #ifdef HAVE_SELINUX
45 #include <selinux/selinux.h>
46 #endif
47
48 #ifdef HAVE_XATTR
49
50 #if defined HAVE_SYS_XATTR_H
51   #include <sys/xattr.h>
52 #elif defined HAVE_ATTR_XATTR_H
53   #include <attr/xattr.h>
54 #else
55   #error "Neither <sys/xattr.h> nor <attr/xattr.h> is present but extended attribute support is enabled."
56 #endif /* defined HAVE_SYS_XATTR_H || HAVE_ATTR_XATTR_H */
57
58 #endif /* HAVE_XATTR */
59
60 #include <glib/gstdio.h>
61 #include <gfileattribute-priv.h>
62
63 #include "glibintl.h"
64
65 #ifdef G_OS_WIN32
66 #include <windows.h>
67 #include <io.h>
68 #ifndef W_OK
69 #define W_OK 2
70 #endif
71 #ifndef R_OK
72 #define R_OK 4
73 #endif
74 #ifndef X_OK
75 #define X_OK 0 /* not really */
76 #endif
77 #ifndef S_ISREG
78 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
79 #endif
80 #ifndef S_ISDIR
81 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
82 #endif
83 #ifndef S_IXUSR
84 #define S_IXUSR _S_IEXEC
85 #endif
86 #endif
87
88 #include "glocalfileinfo.h"
89 #include "gioerror.h"
90 #include "gthemedicon.h"
91 #include "gcontenttype.h"
92 #include "gcontenttypeprivate.h"
93
94 #include "gioalias.h"
95
96 struct ThumbMD5Context {
97         guint32 buf[4];
98         guint32 bits[2];
99         unsigned char in[64];
100 };
101
102 #ifndef G_OS_WIN32
103
104 typedef struct {
105   char *user_name;
106   char *real_name;
107 } UidData;
108
109 G_LOCK_DEFINE_STATIC (uid_cache);
110 static GHashTable *uid_cache = NULL;
111
112 G_LOCK_DEFINE_STATIC (gid_cache);
113 static GHashTable *gid_cache = NULL;
114
115 #endif  /* !G_OS_WIN32 */
116
117 char *
118 _g_local_file_info_create_etag (GLocalFileStat *statbuf)
119 {
120   GTimeVal tv;
121   
122   tv.tv_sec = statbuf->st_mtime;
123 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
124   tv.tv_usec = statbuf->st_mtimensec / 1000;
125 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
126   tv.tv_usec = statbuf->st_mtim.tv_nsec / 1000;
127 #else
128   tv.tv_usec = 0;
129 #endif
130
131   return g_strdup_printf ("%lu:%lu", tv.tv_sec, tv.tv_usec);
132 }
133
134 static char *
135 _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
136 {
137   return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
138                           (guint64) statbuf->st_dev, 
139                           (guint64) statbuf->st_ino);
140 }
141
142 static char *
143 _g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
144 {
145   return g_strdup_printf ("l%" G_GUINT64_FORMAT,
146                           (guint64) statbuf->st_dev);
147 }
148
149
150 #ifdef S_ISLNK
151
152 static gchar *
153 read_link (const gchar *full_name)
154 {
155 #ifdef HAVE_READLINK
156   gchar *buffer;
157   guint size;
158   
159   size = 256;
160   buffer = g_malloc (size);
161   
162   while (1)
163     {
164       int read_size;
165       
166       read_size = readlink (full_name, buffer, size);
167       if (read_size < 0)
168         {
169           g_free (buffer);
170           return NULL;
171         }
172       if (read_size < size)
173         {
174           buffer[read_size] = 0;
175           return buffer;
176         }
177       size *= 2;
178       buffer = g_realloc (buffer, size);
179     }
180 #else
181   return NULL;
182 #endif
183 }
184
185 #endif  /* S_ISLNK */
186
187 /* Get the SELinux security context */
188 static void
189 get_selinux_context (const char            *path,
190                      GFileInfo             *info,
191                      GFileAttributeMatcher *attribute_matcher,
192                      gboolean               follow_symlinks)
193 {
194 #ifdef HAVE_SELINUX
195   char *context;
196
197   if (!g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT))
198     return;
199   
200   if (is_selinux_enabled ())
201     {
202       if (follow_symlinks)
203         {
204           if (lgetfilecon_raw (path, &context) < 0)
205             return;
206         }
207       else
208         {
209           if (getfilecon_raw (path, &context) < 0)
210             return;
211         }
212
213       if (context)
214         {
215           g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
216           freecon (context);
217         }
218     }
219 #endif
220 }
221
222 #ifdef HAVE_XATTR
223
224 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
225  * (Mac) getxattr(..., XATTR_NOFOLLOW)
226  */
227 #ifdef HAVE_XATTR_NOFOLLOW
228 #define g_fgetxattr(fd,name,value,size)  fgetxattr(fd,name,value,size,0,0)
229 #define g_flistxattr(fd,name,size)       flistxattr(fd,name,size,0)
230 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
231 #else
232 #define g_fgetxattr     fgetxattr
233 #define g_flistxattr    flistxattr
234 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
235 #endif
236
237 static ssize_t
238 g_getxattr (const char *path, const char *name, void *value, size_t size,
239             gboolean follow_symlinks)
240 {
241 #ifdef HAVE_XATTR_NOFOLLOW
242   return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
243 #else
244   if (follow_symlinks)
245     return getxattr (path, name, value, size);
246   else
247     return lgetxattr (path, name, value, size);
248 #endif
249 }
250
251 static ssize_t
252 g_listxattr(const char *path, char *namebuf, size_t size,
253             gboolean follow_symlinks)
254 {
255 #ifdef HAVE_XATTR_NOFOLLOW
256   return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
257 #else
258   if (follow_symlinks)
259     return listxattr (path, namebuf, size);
260   else
261     return llistxattr (path, namebuf, size);
262 #endif
263 }
264
265 static gboolean
266 valid_char (char c)
267 {
268   return c >= 32 && c <= 126 && c != '\\';
269 }
270
271 static gboolean
272 name_is_valid (const char *str)
273 {
274   while (*str)
275     {
276       if (!valid_char (*str++))
277         return FALSE;
278     }
279   return TRUE;
280 }
281
282 static char *
283 hex_escape_string (const char *str, 
284                    gboolean   *free_return)
285 {
286   int num_invalid, i;
287   char *escaped_str, *p;
288   unsigned char c;
289   static char *hex_digits = "0123456789abcdef";
290   int len;
291
292   len = strlen (str);
293   
294   num_invalid = 0;
295   for (i = 0; i < len; i++)
296     {
297       if (!valid_char (str[i]))
298         num_invalid++;
299     }
300
301   if (num_invalid == 0)
302     {
303       *free_return = FALSE;
304       return (char *)str;
305     }
306
307   escaped_str = g_malloc (len + num_invalid*3 + 1);
308
309   p = escaped_str;
310   for (i = 0; i < len; i++)
311     {
312       if (valid_char (str[i]))
313         *p++ = str[i];
314       else
315         {
316           c = str[i];
317           *p++ = '\\';
318           *p++ = 'x';
319           *p++ = hex_digits[(c >> 4) & 0xf];
320           *p++ = hex_digits[c & 0xf];
321         }
322     }
323   *p++ = 0;
324
325   *free_return = TRUE;
326   return escaped_str;
327 }
328
329 static char *
330 hex_unescape_string (const char *str, 
331                      int        *out_len, 
332                      gboolean   *free_return)
333 {
334   int i;
335   char *unescaped_str, *p;
336   unsigned char c;
337   int len;
338
339   len = strlen (str);
340   
341   if (strchr (str, '\\') == NULL)
342     {
343       if (out_len)
344         *out_len = len;
345       *free_return = FALSE;
346       return (char *)str;
347     }
348   
349   unescaped_str = g_malloc (len + 1);
350
351   p = unescaped_str;
352   for (i = 0; i < len; i++)
353     {
354       if (str[i] == '\\' &&
355           str[i+1] == 'x' &&
356           len - i >= 4)
357         {
358           c =
359             (g_ascii_xdigit_value (str[i+2]) << 4) |
360             g_ascii_xdigit_value (str[i+3]);
361           *p++ = c;
362           i += 3;
363         }
364       else
365         *p++ = str[i];
366     }
367   *p++ = 0;
368
369   if (out_len)
370     *out_len = p - unescaped_str;
371   *free_return = TRUE;
372   return unescaped_str;
373 }
374
375 static void
376 escape_xattr (GFileInfo  *info,
377               const char *gio_attr, /* gio attribute name */
378               const char *value, /* Is zero terminated */
379               size_t      len /* not including zero termination */)
380 {
381   char *escaped_val;
382   gboolean free_escaped_val;
383   
384   escaped_val = hex_escape_string (value, &free_escaped_val);
385   
386   g_file_info_set_attribute_string (info, gio_attr, escaped_val);
387   
388   if (free_escaped_val)
389     g_free (escaped_val);
390 }
391
392 static void
393 get_one_xattr (const char *path,
394                GFileInfo  *info,
395                const char *gio_attr,
396                const char *xattr,
397                gboolean    follow_symlinks)
398 {
399   char value[64];
400   char *value_p;
401   ssize_t len;
402
403   len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
404
405   value_p = NULL;
406   if (len >= 0)
407     value_p = value;
408   else if (len == -1 && errno == ERANGE)
409     {
410       len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
411
412       if (len < 0)
413         return;
414
415       value_p = g_malloc (len+1);
416
417       len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
418
419       if (len < 0)
420         {
421           g_free (value_p);
422           return;
423         }
424     }
425   else
426     return;
427   
428   /* Null terminate */
429   value_p[len] = 0;
430
431   escape_xattr (info, gio_attr, value_p, len);
432   
433   if (value_p != value)
434     g_free (value_p);
435 }
436
437 #endif /* defined HAVE_XATTR */
438
439 static void
440 get_xattrs (const char            *path,
441             gboolean               user,
442             GFileInfo             *info,
443             GFileAttributeMatcher *matcher,
444             gboolean               follow_symlinks)
445 {
446 #ifdef HAVE_XATTR
447   gboolean all;
448   gsize list_size;
449   ssize_t list_res_size;
450   size_t len;
451   char *list;
452   const char *attr, *attr2;
453
454   if (user)
455     all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
456   else
457     all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
458
459   if (all)
460     {
461       list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
462
463       if (list_res_size == -1 ||
464           list_res_size == 0)
465         return;
466
467       list_size = list_res_size;
468       list = g_malloc (list_size);
469
470     retry:
471       
472       list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
473       
474       if (list_res_size == -1 && errno == ERANGE)
475         {
476           list_size = list_size * 2;
477           list = g_realloc (list, list_size);
478           goto retry;
479         }
480
481       if (list_res_size == -1)
482         return;
483
484       attr = list;
485       while (list_res_size > 0)
486         {
487           if ((user && g_str_has_prefix (attr, "user.")) ||
488               (!user && !g_str_has_prefix (attr, "user.")))
489             {
490               char *escaped_attr, *gio_attr;
491               gboolean free_escaped_attr;
492               
493               if (user)
494                 {
495                   escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
496                   gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
497                 }
498               else
499                 {
500                   escaped_attr = hex_escape_string (attr, &free_escaped_attr);
501                   gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
502                 }
503               
504               if (free_escaped_attr)
505                 g_free (escaped_attr);
506               
507               get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
508             }
509               
510           len = strlen (attr) + 1;
511           attr += len;
512           list_res_size -= len;
513         }
514
515       g_free (list);
516     }
517   else
518     {
519       while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
520         {
521           char *unescaped_attribute, *a;
522           gboolean free_unescaped_attribute;
523
524           attr2 = strchr (attr, ':');
525           if (attr2)
526             {
527               attr2++; /* Skip ':' */
528               unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
529               if (user)
530                 a = g_strconcat ("user.", unescaped_attribute, NULL);
531               else
532                 a = unescaped_attribute;
533               
534               get_one_xattr (path, info, attr, a, follow_symlinks);
535
536               if (user)
537                 g_free (a);
538               
539               if (free_unescaped_attribute)
540                 g_free (unescaped_attribute);
541             }
542         }
543     }
544 #endif /* defined HAVE_XATTR */
545 }
546
547 #ifdef HAVE_XATTR
548 static void
549 get_one_xattr_from_fd (int         fd,
550                        GFileInfo  *info,
551                        const char *gio_attr,
552                        const char *xattr)
553 {
554   char value[64];
555   char *value_p;
556   ssize_t len;
557
558   len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
559
560   value_p = NULL;
561   if (len >= 0)
562     value_p = value;
563   else if (len == -1 && errno == ERANGE)
564     {
565       len = g_fgetxattr (fd, xattr, NULL, 0);
566
567       if (len < 0)
568         return;
569
570       value_p = g_malloc (len + 1);
571
572       len = g_fgetxattr (fd, xattr, value_p, len);
573
574       if (len < 0)
575         {
576           g_free (value_p);
577           return;
578         }
579     }
580   else
581     return;
582   
583   /* Null terminate */
584   value_p[len] = 0;
585
586   escape_xattr (info, gio_attr, value_p, len);
587   
588   if (value_p != value)
589     g_free (value_p);
590 }
591 #endif /* defined HAVE_XATTR */
592
593 static void
594 get_xattrs_from_fd (int                    fd,
595                     gboolean               user,
596                     GFileInfo             *info,
597                     GFileAttributeMatcher *matcher)
598 {
599 #ifdef HAVE_XATTR
600   gboolean all;
601   gsize list_size;
602   ssize_t list_res_size;
603   size_t len;
604   char *list;
605   const char *attr, *attr2;
606
607   if (user)
608     all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
609   else
610     all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
611
612   if (all)
613     {
614       list_res_size = g_flistxattr (fd, NULL, 0);
615
616       if (list_res_size == -1 ||
617           list_res_size == 0)
618         return;
619
620       list_size = list_res_size;
621       list = g_malloc (list_size);
622
623     retry:
624       
625       list_res_size = g_flistxattr (fd, list, list_size);
626       
627       if (list_res_size == -1 && errno == ERANGE)
628         {
629           list_size = list_size * 2;
630           list = g_realloc (list, list_size);
631           goto retry;
632         }
633
634       if (list_res_size == -1)
635         return;
636
637       attr = list;
638       while (list_res_size > 0)
639         {
640           if ((user && g_str_has_prefix (attr, "user.")) ||
641               (!user && !g_str_has_prefix (attr, "user.")))
642             {
643               char *escaped_attr, *gio_attr;
644               gboolean free_escaped_attr;
645               
646               if (user)
647                 {
648                   escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
649                   gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
650                 }
651               else
652                 {
653                   escaped_attr = hex_escape_string (attr, &free_escaped_attr);
654                   gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
655                 }
656               
657               if (free_escaped_attr)
658                 g_free (escaped_attr);
659               
660               get_one_xattr_from_fd (fd, info, gio_attr, attr);
661             }
662           
663           len = strlen (attr) + 1;
664           attr += len;
665           list_res_size -= len;
666         }
667
668       g_free (list);
669     }
670   else
671     {
672       while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
673         {
674           char *unescaped_attribute, *a;
675           gboolean free_unescaped_attribute;
676
677           attr2 = strchr (attr, ':');
678           if (attr2)
679             {
680               attr2++; /* Skip ':' */
681               unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
682               if (user)
683                 a = g_strconcat ("user.", unescaped_attribute, NULL);
684               else
685                 a = unescaped_attribute;
686               
687               get_one_xattr_from_fd (fd, info, attr, a);
688
689               if (user)
690                 g_free (a);
691               
692               if (free_unescaped_attribute)
693                 g_free (unescaped_attribute);
694             }
695         }
696     }
697 #endif /* defined HAVE_XATTR */
698 }
699
700 #ifdef HAVE_XATTR
701 static gboolean
702 set_xattr (char                       *filename,
703            const char                 *escaped_attribute,
704            const GFileAttributeValue  *attr_value,
705            GError                    **error)
706 {
707   char *attribute, *value;
708   gboolean free_attribute, free_value;
709   int val_len, res, errsv;
710   gboolean is_user;
711   char *a;
712
713   if (attr_value == NULL)
714     {
715       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
716                            _("Attribute value must be non-NULL"));
717       return FALSE;
718     }
719
720   if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
721     {
722       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
723                            _("Invalid attribute type (string expected)"));
724       return FALSE;
725     }
726
727   if (!name_is_valid (escaped_attribute))
728     {
729       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
730                            _("Invalid extended attribute name"));
731       return FALSE;
732     }
733
734   if (g_str_has_prefix (escaped_attribute, "xattr::"))
735     {
736       escaped_attribute += strlen ("xattr::");
737       is_user = TRUE;
738     }
739   else
740     {
741       g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
742       escaped_attribute += strlen ("xattr-sys::");
743       is_user = FALSE;
744     }
745   
746   attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
747   value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
748
749   if (is_user)
750     a = g_strconcat ("user.", attribute, NULL);
751   else
752     a = attribute;
753   
754   res = g_setxattr (filename, a, value, val_len);
755   errsv = errno;
756   
757   if (is_user)
758     g_free (a);
759   
760   if (free_attribute)
761     g_free (attribute);
762   
763   if (free_value)
764     g_free (value);
765
766   if (res == -1)
767     {
768       g_set_error (error, G_IO_ERROR,
769                    g_io_error_from_errno (errsv),
770                    _("Error setting extended attribute '%s': %s"),
771                    escaped_attribute, g_strerror (errsv));
772       return FALSE;
773     }
774   
775   return TRUE;
776 }
777
778 #endif
779
780
781 void
782 _g_local_file_info_get_parent_info (const char            *dir,
783                                     GFileAttributeMatcher *attribute_matcher,
784                                     GLocalParentFileInfo  *parent_info)
785 {
786   /* Use plain struct stat for now as long as we only look at the
787    * S_ISVTX bit which doesn't exist on Win32 anyway.
788    */
789   struct stat statbuf;
790   int res;
791   
792   parent_info->writable = FALSE;
793   parent_info->is_sticky = FALSE;
794   parent_info->has_trash_dir = FALSE;
795   parent_info->device = 0;
796
797   if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME) ||
798       g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE) ||
799       g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH) ||
800       g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT))
801     {
802       /* FIXME: Windows: The underlying _waccess() call in the C
803        * library is mostly pointless as it only looks at the READONLY
804        * FAT-style attribute of the file, it doesn't check the ACL at
805        * all.
806        */
807       parent_info->writable = (g_access (dir, W_OK) == 0);
808       
809       res = g_stat (dir, &statbuf);
810
811       /*
812        * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
813        * renamed or deleted only by the owner of the file, by the owner of the directory, and
814        * by a privileged process.
815        */
816       if (res == 0)
817         {
818 #ifdef S_ISVTX
819           parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
820 #else
821           parent_info->is_sticky = FALSE;
822 #endif
823           parent_info->owner = statbuf.st_uid;
824           parent_info->device = statbuf.st_dev;
825           /* No need to find trash dir if its not writable anyway */
826           if (parent_info->writable &&
827               g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
828             parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
829         }
830     }
831 }
832
833 static void
834 get_access_rights (GFileAttributeMatcher *attribute_matcher,
835                    GFileInfo             *info,
836                    const gchar           *path,
837                    GLocalFileStat        *statbuf,
838                    GLocalParentFileInfo  *parent_info)
839 {
840   /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
841   if (g_file_attribute_matcher_matches (attribute_matcher,
842                                         G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
843     g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
844                                        g_access (path, R_OK) == 0);
845   
846   if (g_file_attribute_matcher_matches (attribute_matcher,
847                                         G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
848     g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
849                                        g_access (path, W_OK) == 0);
850   
851   if (g_file_attribute_matcher_matches (attribute_matcher,
852                                         G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
853     g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE,
854                                        g_access (path, X_OK) == 0);
855
856
857   if (parent_info)
858     {
859       gboolean writable;
860
861       writable = FALSE;
862       if (parent_info->writable)
863         {
864           if (parent_info->is_sticky)
865             {
866 #ifndef G_OS_WIN32
867               uid_t uid = geteuid ();
868
869               if (uid == statbuf->st_uid ||
870                   uid == parent_info->owner ||
871                   uid == 0)
872 #endif
873                 writable = TRUE;
874             }
875           else
876             writable = TRUE;
877         }
878
879       if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME))
880         g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME,
881                                            writable);
882       
883       if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE))
884         g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE,
885                                            writable);
886
887       if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
888         g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH,
889                                            writable && parent_info->has_trash_dir);
890     }
891 }
892
893 static void
894 set_info_from_stat (GFileInfo             *info, 
895                     GLocalFileStat        *statbuf,
896                     GFileAttributeMatcher *attribute_matcher)
897 {
898   GFileType file_type;
899
900   file_type = G_FILE_TYPE_UNKNOWN;
901
902   if (S_ISREG (statbuf->st_mode))
903     file_type = G_FILE_TYPE_REGULAR;
904   else if (S_ISDIR (statbuf->st_mode))
905     file_type = G_FILE_TYPE_DIRECTORY;
906 #ifndef G_OS_WIN32
907   else if (S_ISCHR (statbuf->st_mode) ||
908            S_ISBLK (statbuf->st_mode) ||
909            S_ISFIFO (statbuf->st_mode)
910 #ifdef S_ISSOCK
911            || S_ISSOCK (statbuf->st_mode)
912 #endif
913            )
914     file_type = G_FILE_TYPE_SPECIAL;
915 #endif
916 #ifdef S_ISLNK
917   else if (S_ISLNK (statbuf->st_mode))
918     file_type = G_FILE_TYPE_SYMBOLIC_LINK;
919 #endif
920
921   g_file_info_set_file_type (info, file_type);
922   g_file_info_set_size (info, statbuf->st_size);
923
924   g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE, statbuf->st_dev);
925 #ifndef G_OS_WIN32
926   /* Pointless setting these on Windows even if they exist in the struct */
927   g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_INODE, statbuf->st_ino);
928   g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_NLINK, statbuf->st_nlink);
929   g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_UID, statbuf->st_uid);
930   g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, statbuf->st_gid);
931   g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_RDEV, statbuf->st_rdev);
932 #endif
933   /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
934   g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, statbuf->st_mode);
935 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
936   g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE, statbuf->st_blksize);
937 #endif
938 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
939   g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_BLOCKS, statbuf->st_blocks);
940 #endif
941   
942   g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED, statbuf->st_mtime);
943 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
944   g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
945 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
946   g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
947 #endif
948   
949   g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS, statbuf->st_atime);
950 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
951   g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
952 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
953   g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
954 #endif
955   
956   g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_CHANGED, statbuf->st_ctime);
957 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
958   g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
959 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
960   g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
961 #endif
962
963   if (g_file_attribute_matcher_matches (attribute_matcher,
964                                         G_FILE_ATTRIBUTE_ETAG_VALUE))
965     {
966       char *etag = _g_local_file_info_create_etag (statbuf);
967       g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ETAG_VALUE, etag);
968       g_free (etag);
969     }
970
971   if (g_file_attribute_matcher_matches (attribute_matcher,
972                                         G_FILE_ATTRIBUTE_ID_FILE))
973     {
974       char *id = _g_local_file_info_create_file_id (statbuf);
975       g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE, id);
976       g_free (id);
977     }
978
979   if (g_file_attribute_matcher_matches (attribute_matcher,
980                                         G_FILE_ATTRIBUTE_ID_FILESYSTEM))
981     {
982       char *id = _g_local_file_info_create_fs_id (statbuf);
983       g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILESYSTEM, id);
984       g_free (id);
985     }
986 }
987
988 #ifndef G_OS_WIN32
989
990 static char *
991 make_valid_utf8 (const char *name)
992 {
993   GString *string;
994   const gchar *remainder, *invalid;
995   gint remaining_bytes, valid_bytes;
996   
997   string = NULL;
998   remainder = name;
999   remaining_bytes = strlen (name);
1000   
1001   while (remaining_bytes != 0) 
1002     {
1003       if (g_utf8_validate (remainder, remaining_bytes, &invalid)) 
1004         break;
1005       valid_bytes = invalid - remainder;
1006     
1007       if (string == NULL) 
1008         string = g_string_sized_new (remaining_bytes);
1009
1010       g_string_append_len (string, remainder, valid_bytes);
1011       /* append U+FFFD REPLACEMENT CHARACTER */
1012       g_string_append (string, "\357\277\275");
1013       
1014       remaining_bytes -= valid_bytes + 1;
1015       remainder = invalid + 1;
1016     }
1017   
1018   if (string == NULL)
1019     return g_strdup (name);
1020   
1021   g_string_append (string, remainder);
1022
1023   g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1024   
1025   return g_string_free (string, FALSE);
1026 }
1027
1028 static char *
1029 convert_pwd_string_to_utf8 (char *pwd_str)
1030 {
1031   char *utf8_string;
1032   
1033   if (!g_utf8_validate (pwd_str, -1, NULL))
1034     {
1035       utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1036       if (utf8_string == NULL)
1037         utf8_string = make_valid_utf8 (pwd_str);
1038     }
1039   else 
1040     utf8_string = g_strdup (pwd_str);
1041   
1042   return utf8_string;
1043 }
1044
1045 static void
1046 uid_data_free (UidData *data)
1047 {
1048   g_free (data->user_name);
1049   g_free (data->real_name);
1050   g_free (data);
1051 }
1052
1053 /* called with lock held */
1054 static UidData *
1055 lookup_uid_data (uid_t uid)
1056 {
1057   UidData *data;
1058   char buffer[4096];
1059   struct passwd pwbuf;
1060   struct passwd *pwbufp;
1061   char *gecos, *comma;
1062   
1063   if (uid_cache == NULL)
1064     uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1065
1066   data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1067
1068   if (data)
1069     return data;
1070
1071   data = g_new0 (UidData, 1);
1072
1073 #if defined(HAVE_POSIX_GETPWUID_R)
1074   getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1075 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1076   pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1077 #else
1078   pwbufp = getpwuid (uid);
1079 #endif
1080
1081   if (pwbufp != NULL)
1082     {
1083       if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1084         data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1085
1086       gecos = pwbufp->pw_gecos;
1087
1088       if (gecos)
1089         {
1090           comma = strchr (gecos, ',');
1091           if (comma)
1092             *comma = 0;
1093           data->real_name = convert_pwd_string_to_utf8 (gecos);
1094         }
1095     }
1096
1097   /* Default fallbacks */
1098   if (data->real_name == NULL)
1099     {
1100       if (data->user_name != NULL)
1101         data->real_name = g_strdup (data->user_name);
1102       else
1103         data->real_name = g_strdup_printf ("user #%d", (int)uid);
1104     }
1105   
1106   if (data->user_name == NULL)
1107     data->user_name = g_strdup_printf ("%d", (int)uid);
1108   
1109   g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1110   
1111   return data;
1112 }
1113
1114 static char *
1115 get_username_from_uid (uid_t uid)
1116 {
1117   char *res;
1118   UidData *data;
1119   
1120   G_LOCK (uid_cache);
1121   data = lookup_uid_data (uid);
1122   res = g_strdup (data->user_name);  
1123   G_UNLOCK (uid_cache);
1124
1125   return res;
1126 }
1127
1128 static char *
1129 get_realname_from_uid (uid_t uid)
1130 {
1131   char *res;
1132   UidData *data;
1133   
1134   G_LOCK (uid_cache);
1135   data = lookup_uid_data (uid);
1136   res = g_strdup (data->real_name);  
1137   G_UNLOCK (uid_cache);
1138   
1139   return res;
1140 }
1141
1142 /* called with lock held */
1143 static char *
1144 lookup_gid_name (gid_t gid)
1145 {
1146   char *name;
1147   char buffer[4096];
1148   struct group gbuf;
1149   struct group *gbufp;
1150   
1151   if (gid_cache == NULL)
1152     gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1153
1154   name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1155
1156   if (name)
1157     return name;
1158
1159 #if defined (HAVE_POSIX_GETGRGID_R)
1160   getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1161 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1162   gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1163 #else
1164   gbufp = getgrgid (gid);
1165 #endif
1166
1167   if (gbufp != NULL &&
1168       gbufp->gr_name != NULL &&
1169       gbufp->gr_name[0] != 0)
1170     name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1171   else
1172     name = g_strdup_printf("%d", (int)gid);
1173   
1174   g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1175   
1176   return name;
1177 }
1178
1179 static char *
1180 get_groupname_from_gid (gid_t gid)
1181 {
1182   char *res;
1183   char *name;
1184   
1185   G_LOCK (gid_cache);
1186   name = lookup_gid_name (gid);
1187   res = g_strdup (name);  
1188   G_UNLOCK (gid_cache);
1189   return res;
1190 }
1191
1192 #endif /* !G_OS_WIN32 */
1193
1194 static char *
1195 get_content_type (const char          *basename,
1196                   const char          *path,
1197                   GLocalFileStat      *statbuf,
1198                   gboolean             is_symlink,
1199                   gboolean             symlink_broken,
1200                   GFileQueryInfoFlags  flags,
1201                   gboolean             fast)
1202 {
1203   if (is_symlink &&
1204       (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1205     return g_strdup  ("inode/symlink");
1206   else if (S_ISDIR(statbuf->st_mode))
1207     return g_strdup ("inode/directory");
1208 #ifndef G_OS_WIN32
1209   else if (S_ISCHR(statbuf->st_mode))
1210     return g_strdup ("inode/chardevice");
1211   else if (S_ISBLK(statbuf->st_mode))
1212     return g_strdup ("inode/blockdevice");
1213   else if (S_ISFIFO(statbuf->st_mode))
1214     return g_strdup ("inode/fifo");
1215 #endif
1216 #ifdef S_ISSOCK
1217   else if (S_ISSOCK(statbuf->st_mode))
1218     return g_strdup ("inode/socket");
1219 #endif
1220   else
1221     {
1222       char *content_type;
1223       gboolean result_uncertain;
1224       
1225       content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1226       
1227 #ifndef G_OS_WIN32
1228       if (!fast && result_uncertain && path != NULL)
1229         {
1230           guchar sniff_buffer[4096];
1231           gsize sniff_length;
1232           int fd;
1233
1234           sniff_length = _g_unix_content_type_get_sniff_len ();
1235           if (sniff_length > 4096)
1236             sniff_length = 4096;
1237           
1238           fd = open (path, O_RDONLY);
1239           if (fd != -1)
1240             {
1241               ssize_t res;
1242               
1243               res = read (fd, sniff_buffer, sniff_length);
1244               close (fd);
1245               if (res >= 0)
1246                 {
1247                   g_free (content_type);
1248                   content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1249                 }
1250             }
1251         }
1252 #endif
1253       
1254       return content_type;
1255     }
1256   
1257 }
1258
1259 static void
1260 get_thumbnail_attributes (const char *path,
1261                           GFileInfo  *info)
1262 {
1263   GChecksum *checksum;
1264   char *uri;
1265   char *filename;
1266   char *basename;
1267
1268   uri = g_filename_to_uri (path, NULL, NULL);
1269
1270   checksum = g_checksum_new (G_CHECKSUM_MD5);
1271   g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1272   
1273   g_free (uri);
1274
1275   basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1276   g_checksum_free (checksum);
1277
1278   filename = g_build_filename (g_get_home_dir (),
1279                                ".thumbnails", "normal", basename,
1280                                NULL);
1281
1282   if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1283     g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1284   else
1285     {
1286       g_free (filename);
1287       filename = g_build_filename (g_get_home_dir (),
1288                                    ".thumbnails", "fail",
1289                                    "gnome-thumbnail-factory",
1290                                    basename,
1291                                    NULL);
1292
1293       if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1294         g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1295     }
1296   g_free (basename);
1297   g_free (filename);
1298 }
1299
1300 #ifdef G_OS_WIN32
1301 static void
1302 win32_get_file_user_info (const gchar  *filename,
1303                           gchar       **group_name, 
1304                           gchar       **user_name, 
1305                           gchar       **real_name)
1306 {
1307   PSECURITY_DESCRIPTOR psd = NULL;
1308   DWORD sd_size = 0; /* first call calculates the size required */
1309   
1310   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1311   if ((GetFileSecurityW (wfilename, 
1312                         GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1313                         NULL,
1314                         sd_size,
1315                         &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1316      (psd = g_try_malloc (sd_size)) != NULL &&
1317      GetFileSecurityW (wfilename, 
1318                        GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1319                        psd,
1320                        sd_size,
1321                        &sd_size))
1322     {
1323       PSID psid = 0;
1324       BOOL defaulted;
1325       SID_NAME_USE name_use = 0; /* don't care? */
1326       wchar_t *name = NULL;
1327       wchar_t *domain = NULL;
1328       DWORD name_len = 0;
1329       DWORD domain_len = 0;
1330       /* get the user name */
1331       do {
1332         if (!user_name)
1333           break;
1334         if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1335           break;
1336         if (!LookupAccountSidW (NULL, /* local machine */
1337                                 psid, 
1338                                 name, &name_len,
1339                                 domain, &domain_len, /* no domain info yet */
1340                                 &name_use)  && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1341           break;
1342         name = g_try_malloc (name_len * sizeof (wchar_t));
1343         domain = g_try_malloc (domain_len * sizeof (wchar_t));
1344         if (name && domain &&
1345             LookupAccountSidW (NULL, /* local machine */
1346                                psid, 
1347                                name, &name_len,
1348                                domain, &domain_len, /* no domain info yet */
1349                                &name_use))
1350           {
1351             *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1352           }
1353         g_free (name);
1354         g_free (domain);
1355       } while (FALSE);
1356
1357       /* get the group name */
1358       do {
1359         if (!group_name)
1360           break;
1361         if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1362           break;
1363         if (!LookupAccountSidW (NULL, /* local machine */
1364                                 psid, 
1365                                 name, &name_len,
1366                                 domain, &domain_len, /* no domain info yet */
1367                                 &name_use)  && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1368           break;
1369         name = g_try_malloc (name_len * sizeof (wchar_t));
1370         domain = g_try_malloc (domain_len * sizeof (wchar_t));
1371         if (name && domain &&
1372             LookupAccountSidW (NULL, /* local machine */
1373                                psid, 
1374                                name, &name_len,
1375                                domain, &domain_len, /* no domain info yet */
1376                                &name_use))
1377           {
1378             *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1379           }
1380         g_free (name);
1381         g_free (domain);
1382       } while (FALSE);
1383
1384       /* TODO: get real name */
1385
1386       g_free (psd);
1387     }
1388   g_free (wfilename);
1389 }
1390 #endif /* G_OS_WIN32 */
1391
1392 GFileInfo *
1393 _g_local_file_info_get (const char             *basename,
1394                         const char             *path,
1395                         GFileAttributeMatcher  *attribute_matcher,
1396                         GFileQueryInfoFlags     flags,
1397                         GLocalParentFileInfo   *parent_info,
1398                         GError                **error)
1399 {
1400   GFileInfo *info;
1401   GLocalFileStat statbuf;
1402 #ifdef S_ISLNK
1403   struct stat statbuf2;
1404 #endif
1405   int res;
1406   gboolean is_symlink, symlink_broken;
1407 #ifdef G_OS_WIN32
1408   DWORD dos_attributes;
1409 #endif
1410
1411   info = g_file_info_new ();
1412
1413   /* Make sure we don't set any unwanted attributes */
1414   g_file_info_set_attribute_mask (info, attribute_matcher);
1415   
1416   g_file_info_set_name (info, basename);
1417
1418   /* Avoid stat in trivial case */
1419   if (attribute_matcher == NULL)
1420     return info;
1421
1422 #ifndef G_OS_WIN32
1423   res = g_lstat (path, &statbuf);
1424 #else
1425   {
1426     wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1427     int len;
1428
1429     if (wpath == NULL)
1430       {
1431         g_object_unref (info);
1432         return NULL;
1433       }
1434
1435     len = wcslen (wpath);
1436     while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1437       len--;
1438     if (len > 0 &&
1439         (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1440       wpath[len] = '\0';
1441
1442     res = _wstati64 (wpath, &statbuf);
1443     dos_attributes = GetFileAttributesW (wpath);
1444
1445     g_free (wpath);
1446   }
1447 #endif
1448
1449   if (res == -1)
1450     {
1451       int errsv = errno;
1452       char *display_name = g_filename_display_name (path);
1453       g_object_unref (info);
1454       g_set_error (error, G_IO_ERROR,
1455                    g_io_error_from_errno (errsv),
1456                    _("Error stating file '%s': %s"),
1457                    display_name, g_strerror (errsv));
1458       g_free (display_name);
1459       return NULL;
1460     }
1461   
1462 #ifdef S_ISLNK
1463   is_symlink = S_ISLNK (statbuf.st_mode);
1464 #else
1465   is_symlink = FALSE;
1466 #endif
1467   symlink_broken = FALSE;
1468 #ifdef S_ISLNK
1469   if (is_symlink)
1470     {
1471       g_file_info_set_is_symlink (info, TRUE);
1472
1473       /* Unless NOFOLLOW was set we default to following symlinks */
1474       if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1475         {
1476           res = stat (path, &statbuf2);
1477
1478             /* Report broken links as symlinks */
1479           if (res != -1)
1480             statbuf = statbuf2;
1481           else
1482             symlink_broken = TRUE;
1483         }
1484     }
1485 #endif
1486
1487   set_info_from_stat (info, &statbuf, attribute_matcher);
1488   
1489 #ifndef G_OS_WIN32
1490   if (basename != NULL && basename[0] == '.')
1491     g_file_info_set_is_hidden (info, TRUE);
1492
1493   if (basename != NULL && basename[strlen (basename) -1] == '~')
1494     g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP, TRUE);
1495 #else
1496   if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1497     g_file_info_set_is_hidden (info, TRUE);
1498
1499   if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1500     g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_DOS_IS_ARCHIVE, TRUE);
1501
1502   if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1503     g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_DOS_IS_SYSTEM, TRUE);
1504 #endif
1505
1506 #ifdef S_ISLNK
1507   if (is_symlink &&
1508       g_file_attribute_matcher_matches (attribute_matcher,
1509                                         G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET))
1510     {
1511       char *link = read_link (path);
1512       g_file_info_set_symlink_target (info, link);
1513       g_free (link);
1514     }
1515 #endif
1516
1517   if (g_file_attribute_matcher_matches (attribute_matcher,
1518                                         G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME))
1519     {
1520       char *display_name = g_filename_display_basename (path);
1521      
1522       /* look for U+FFFD REPLACEMENT CHARACTER */ 
1523       if (strstr (display_name, "\357\277\275") != NULL)
1524         {
1525           char *p = display_name;
1526           display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1527           g_free (p);
1528         }
1529       g_file_info_set_display_name (info, display_name);
1530       g_free (display_name);
1531     }
1532   
1533   if (g_file_attribute_matcher_matches (attribute_matcher,
1534                                         G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME))
1535     {
1536       char *edit_name = g_filename_display_basename (path);
1537       g_file_info_set_edit_name (info, edit_name);
1538       g_free (edit_name);
1539     }
1540
1541   
1542   if (g_file_attribute_matcher_matches (attribute_matcher,
1543                                         G_FILE_ATTRIBUTE_STANDARD_COPY_NAME))
1544     {
1545       char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1546       if (copy_name)
1547         g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME, copy_name);
1548       g_free (copy_name);
1549     }
1550
1551   if (g_file_attribute_matcher_matches (attribute_matcher,
1552                                         G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE) ||
1553       g_file_attribute_matcher_matches (attribute_matcher,
1554                                         G_FILE_ATTRIBUTE_STANDARD_ICON))
1555     {
1556       char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1557
1558       if (content_type)
1559         {
1560           g_file_info_set_content_type (info, content_type);
1561
1562           if (g_file_attribute_matcher_matches (attribute_matcher,
1563                                                 G_FILE_ATTRIBUTE_STANDARD_ICON))
1564             {
1565               GIcon *icon;
1566
1567               if (strcmp (path, g_get_home_dir ()) == 0)
1568                 icon = g_themed_icon_new ("user-home");
1569               else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0) 
1570                 icon = g_themed_icon_new ("user-desktop");
1571               else 
1572                 {
1573                   icon = g_content_type_get_icon (content_type);
1574                   if (G_IS_THEMED_ICON (icon))
1575                     {
1576                       const char *type_icon = NULL;
1577
1578                       if (S_ISDIR (statbuf.st_mode)) 
1579                         type_icon = "folder";
1580                       else if (statbuf.st_mode & S_IXUSR)
1581                         type_icon = "application-x-executable";
1582                       else
1583                         type_icon = "text-x-generic";
1584
1585                       g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
1586                     }
1587                 }
1588
1589               if (icon != NULL)
1590                 {
1591                   g_file_info_set_icon (info, icon);
1592                   g_object_unref (icon);
1593                 }
1594             }
1595           
1596           g_free (content_type);
1597         }
1598     }
1599
1600   if (g_file_attribute_matcher_matches (attribute_matcher,
1601                                         G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE))
1602     {
1603       char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1604       
1605       if (content_type)
1606         {
1607           g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE, content_type);
1608           g_free (content_type);
1609         }
1610     }
1611
1612   if (g_file_attribute_matcher_matches (attribute_matcher,
1613                                         G_FILE_ATTRIBUTE_OWNER_USER))
1614     {
1615       char *name = NULL;
1616       
1617 #ifdef G_OS_WIN32
1618       win32_get_file_user_info (path, NULL, &name, NULL);
1619 #else
1620       name = get_username_from_uid (statbuf.st_uid);
1621 #endif
1622       if (name)
1623         g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1624       g_free (name);
1625     }
1626
1627   if (g_file_attribute_matcher_matches (attribute_matcher,
1628                                         G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1629     {
1630       char *name = NULL;
1631 #ifdef G_OS_WIN32
1632       win32_get_file_user_info (path, NULL, NULL, &name);
1633 #else
1634       name = get_realname_from_uid (statbuf.st_uid);
1635 #endif
1636       if (name)
1637         g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1638       g_free (name);
1639     }
1640   
1641   if (g_file_attribute_matcher_matches (attribute_matcher,
1642                                         G_FILE_ATTRIBUTE_OWNER_GROUP))
1643     {
1644       char *name = NULL;
1645 #ifdef G_OS_WIN32
1646       win32_get_file_user_info (path, &name, NULL, NULL);
1647 #else
1648       name = get_groupname_from_gid (statbuf.st_gid);
1649 #endif
1650       if (name)
1651         g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1652       g_free (name);
1653     }
1654
1655   if (parent_info && parent_info->device != 0 &&
1656       g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1657       statbuf.st_dev != parent_info->device) 
1658     g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1659   
1660   get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1661   
1662   get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1663   get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1664   get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1665
1666   if (g_file_attribute_matcher_matches (attribute_matcher,
1667                                         G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1668     get_thumbnail_attributes (path, info);
1669   
1670   g_file_info_unset_attribute_mask (info);
1671
1672   return info;
1673 }
1674
1675 GFileInfo *
1676 _g_local_file_info_get_from_fd (int      fd,
1677                                 char    *attributes,
1678                                 GError **error)
1679 {
1680   GLocalFileStat stat_buf;
1681   GFileAttributeMatcher *matcher;
1682   GFileInfo *info;
1683   
1684 #ifdef G_OS_WIN32
1685 #define FSTAT _fstati64
1686 #else
1687 #define FSTAT fstat
1688 #endif
1689
1690   if (FSTAT (fd, &stat_buf) == -1)
1691     {
1692       int errsv = errno;
1693
1694       g_set_error (error, G_IO_ERROR,
1695                    g_io_error_from_errno (errsv),
1696                    _("Error stating file descriptor: %s"),
1697                    g_strerror (errsv));
1698       return NULL;
1699     }
1700
1701   info = g_file_info_new ();
1702
1703   matcher = g_file_attribute_matcher_new (attributes);
1704
1705   /* Make sure we don't set any unwanted attributes */
1706   g_file_info_set_attribute_mask (info, matcher);
1707   
1708   set_info_from_stat (info, &stat_buf, matcher);
1709   
1710 #ifdef HAVE_SELINUX
1711   if (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) &&
1712       is_selinux_enabled ())
1713     {
1714       char *context;
1715       if (fgetfilecon_raw (fd, &context) >= 0)
1716         {
1717           g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
1718           freecon (context);
1719         }
1720     }
1721 #endif
1722
1723   get_xattrs_from_fd (fd, TRUE, info, matcher);
1724   get_xattrs_from_fd (fd, FALSE, info, matcher);
1725   
1726   g_file_attribute_matcher_unref (matcher);
1727
1728   g_file_info_unset_attribute_mask (info);
1729   
1730   return info;
1731 }
1732
1733 static gboolean
1734 get_uint32 (const GFileAttributeValue  *value,
1735             guint32                    *val_out,
1736             GError                    **error)
1737 {
1738   if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1739     {
1740       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1741                            _("Invalid attribute type (uint32 expected)"));
1742       return FALSE;
1743     }
1744
1745   *val_out = value->u.uint32;
1746   
1747   return TRUE;
1748 }
1749
1750 #ifdef HAVE_UTIMES
1751 static gboolean
1752 get_uint64 (const GFileAttributeValue  *value,
1753             guint64                    *val_out,
1754             GError                    **error)
1755 {
1756   if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1757     {
1758       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1759                            _("Invalid attribute type (uint64 expected)"));
1760       return FALSE;
1761     }
1762
1763   *val_out = value->u.uint64;
1764   
1765   return TRUE;
1766 }
1767 #endif
1768
1769 #if defined(HAVE_SYMLINK)
1770 static gboolean
1771 get_byte_string (const GFileAttributeValue  *value,
1772                  const char                **val_out,
1773                  GError                    **error)
1774 {
1775   if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1776     {
1777       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1778                            _("Invalid attribute type (byte string expected)"));
1779       return FALSE;
1780     }
1781
1782   *val_out = value->u.string;
1783   
1784   return TRUE;
1785 }
1786 #endif
1787
1788 static gboolean
1789 set_unix_mode (char                       *filename,
1790                const GFileAttributeValue  *value,
1791                GError                    **error)
1792 {
1793   guint32 val;
1794   
1795   if (!get_uint32 (value, &val, error))
1796     return FALSE;
1797   
1798   if (g_chmod (filename, val) == -1)
1799     {
1800       int errsv = errno;
1801
1802       g_set_error (error, G_IO_ERROR,
1803                    g_io_error_from_errno (errsv),
1804                    _("Error setting permissions: %s"),
1805                    g_strerror (errsv));
1806       return FALSE;
1807     }
1808   return TRUE;
1809 }
1810
1811 #ifdef HAVE_CHOWN
1812 static gboolean
1813 set_unix_uid_gid (char                       *filename,
1814                   const GFileAttributeValue  *uid_value,
1815                   const GFileAttributeValue  *gid_value,
1816                   GFileQueryInfoFlags         flags,
1817                   GError                    **error)
1818 {
1819   int res;
1820   guint32 val;
1821   uid_t uid;
1822   gid_t gid;
1823   
1824   if (uid_value)
1825     {
1826       if (!get_uint32 (uid_value, &val, error))
1827         return FALSE;
1828       uid = val;
1829     }
1830   else
1831     uid = -1;
1832   
1833   if (gid_value)
1834     {
1835       if (!get_uint32 (gid_value, &val, error))
1836         return FALSE;
1837       gid = val;
1838     }
1839   else
1840     gid = -1;
1841   
1842 #ifdef HAVE_LCHOWN
1843   if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1844     res = lchown (filename, uid, gid);
1845   else
1846 #endif
1847     res = chown (filename, uid, gid);
1848   
1849   if (res == -1)
1850     {
1851       int errsv = errno;
1852
1853       g_set_error (error, G_IO_ERROR,
1854                    g_io_error_from_errno (errsv),
1855                    _("Error setting owner: %s"),
1856                    g_strerror (errsv));
1857           return FALSE;
1858     }
1859   return TRUE;
1860 }
1861 #endif
1862
1863 #ifdef HAVE_SYMLINK
1864 static gboolean
1865 set_symlink (char                       *filename,
1866              const GFileAttributeValue  *value,
1867              GError                    **error)
1868 {
1869   const char *val;
1870   struct stat statbuf;
1871   
1872   if (!get_byte_string (value, &val, error))
1873     return FALSE;
1874   
1875   if (val == NULL)
1876     {
1877       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1878                            _("symlink must be non-NULL"));
1879       return FALSE;
1880     }
1881   
1882   if (g_lstat (filename, &statbuf))
1883     {
1884       int errsv = errno;
1885
1886       g_set_error (error, G_IO_ERROR,
1887                    g_io_error_from_errno (errsv),
1888                    _("Error setting symlink: %s"),
1889                    g_strerror (errsv));
1890       return FALSE;
1891     }
1892   
1893   if (!S_ISLNK (statbuf.st_mode))
1894     {
1895       g_set_error_literal (error, G_IO_ERROR,
1896                            G_IO_ERROR_NOT_SYMBOLIC_LINK,
1897                            _("Error setting symlink: file is not a symlink"));
1898       return FALSE;
1899     }
1900   
1901   if (g_unlink (filename))
1902     {
1903       int errsv = errno;
1904
1905       g_set_error (error, G_IO_ERROR,
1906                    g_io_error_from_errno (errsv),
1907                    _("Error setting symlink: %s"),
1908                    g_strerror (errsv));
1909       return FALSE;
1910     }
1911   
1912   if (symlink (filename, val) != 0)
1913     {
1914       int errsv = errno;
1915
1916       g_set_error (error, G_IO_ERROR,
1917                    g_io_error_from_errno (errsv),
1918                    _("Error setting symlink: %s"),
1919                    g_strerror (errsv));
1920       return FALSE;
1921     }
1922   
1923   return TRUE;
1924 }
1925 #endif
1926
1927 #ifdef HAVE_UTIMES
1928 static int
1929 lazy_stat (char        *filename, 
1930            struct stat *statbuf, 
1931            gboolean    *called_stat)
1932 {
1933   int res;
1934
1935   if (*called_stat)
1936     return 0;
1937   
1938   res = g_stat (filename, statbuf);
1939   
1940   if (res == 0)
1941     *called_stat = TRUE;
1942   
1943   return res;
1944 }
1945
1946
1947 static gboolean
1948 set_mtime_atime (char                       *filename,
1949                  const GFileAttributeValue  *mtime_value,
1950                  const GFileAttributeValue  *mtime_usec_value,
1951                  const GFileAttributeValue  *atime_value,
1952                  const GFileAttributeValue  *atime_usec_value,
1953                  GError                    **error)
1954 {
1955   int res;
1956   guint64 val;
1957   guint32 val_usec;
1958   struct stat statbuf;
1959   gboolean got_stat = FALSE;
1960   struct timeval times[2] = { {0, 0}, {0, 0} };
1961
1962   /* ATIME */
1963   if (atime_value)
1964     {
1965       if (!get_uint64 (atime_value, &val, error))
1966         return FALSE;
1967       times[0].tv_sec = val;
1968     }
1969   else
1970     {
1971       if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1972         {
1973           times[0].tv_sec = statbuf.st_mtime;
1974 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1975           times[0].tv_usec = statbuf.st_atimensec / 1000;
1976 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1977           times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
1978 #endif
1979         }
1980     }
1981   
1982   if (atime_usec_value)
1983     {
1984       if (!get_uint32 (atime_usec_value, &val_usec, error))
1985         return FALSE;
1986       times[0].tv_usec = val_usec;
1987     }
1988
1989   /* MTIME */
1990   if (mtime_value)
1991     {
1992       if (!get_uint64 (mtime_value, &val, error))
1993         return FALSE;
1994       times[1].tv_sec = val;
1995     }
1996   else
1997     {
1998       if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1999         {
2000           times[1].tv_sec = statbuf.st_mtime;
2001 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2002           times[1].tv_usec = statbuf.st_mtimensec / 1000;
2003 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2004           times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2005 #endif
2006         }
2007     }
2008   
2009   if (mtime_usec_value)
2010     {
2011       if (!get_uint32 (mtime_usec_value, &val_usec, error))
2012         return FALSE;
2013       times[1].tv_usec = val_usec;
2014     }
2015   
2016   res = utimes (filename, times);
2017   if (res == -1)
2018     {
2019       int errsv = errno;
2020
2021       g_set_error (error, G_IO_ERROR,
2022                    g_io_error_from_errno (errsv),
2023                    _("Error setting owner: %s"),
2024                    g_strerror (errsv));
2025           return FALSE;
2026     }
2027   return TRUE;
2028 }
2029 #endif
2030
2031 gboolean
2032 _g_local_file_info_set_attribute (char                 *filename,
2033                                   const char           *attribute,
2034                                   GFileAttributeType    type,
2035                                   gpointer              value_p,
2036                                   GFileQueryInfoFlags   flags,
2037                                   GCancellable         *cancellable,
2038                                   GError              **error)
2039 {
2040   GFileAttributeValue value = { 0 };
2041
2042   _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2043   
2044   if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2045     return set_unix_mode (filename, &value, error);
2046   
2047 #ifdef HAVE_CHOWN
2048   else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2049     return set_unix_uid_gid (filename, &value, NULL, flags, error);
2050   else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2051     return set_unix_uid_gid (filename, NULL, &value, flags, error);
2052 #endif
2053   
2054 #ifdef HAVE_SYMLINK
2055   else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2056     return set_symlink (filename, &value, error);
2057 #endif
2058
2059 #ifdef HAVE_UTIMES
2060   else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2061     return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2062   else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2063     return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2064   else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2065     return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2066   else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2067     return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2068 #endif
2069
2070 #ifdef HAVE_XATTR
2071   else if (g_str_has_prefix (attribute, "xattr::"))
2072     return set_xattr (filename, attribute, &value, error);
2073   else if (g_str_has_prefix (attribute, "xattr-sys::"))
2074     return set_xattr (filename, attribute, &value, error);
2075 #endif
2076   
2077   g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2078                _("Setting attribute %s not supported"), attribute);
2079   return FALSE;
2080 }
2081
2082 gboolean
2083 _g_local_file_info_set_attributes  (char                 *filename,
2084                                     GFileInfo            *info,
2085                                     GFileQueryInfoFlags   flags,
2086                                     GCancellable         *cancellable,
2087                                     GError              **error)
2088 {
2089   GFileAttributeValue *value;
2090 #ifdef HAVE_CHOWN
2091   GFileAttributeValue *uid, *gid;
2092 #endif
2093 #ifdef HAVE_UTIMES
2094   GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2095 #endif
2096 #if defined (HAVE_CHOWN) && defined (HAVE_UTIMES)
2097   GFileAttributeStatus status;
2098 #endif
2099   gboolean res;
2100   
2101   /* Handles setting multiple specified data in a single set, and takes care
2102      of ordering restrictions when setting attributes */
2103
2104   res = TRUE;
2105
2106   /* Set symlink first, since this recreates the file */
2107 #ifdef HAVE_SYMLINK
2108   value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2109   if (value)
2110     {
2111       if (!set_symlink (filename, value, error))
2112         {
2113           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2114           res = FALSE;
2115           /* Don't set error multiple times */
2116           error = NULL;
2117         }
2118       else
2119         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2120         
2121     }
2122 #endif
2123
2124 #ifdef HAVE_CHOWN
2125   /* Group uid and gid setting into one call
2126    * Change ownership before permissions, since ownership changes can
2127      change permissions (e.g. setuid)
2128    */
2129   uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2130   gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2131   
2132   if (uid || gid)
2133     {
2134       if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2135         {
2136           status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2137           res = FALSE;
2138           /* Don't set error multiple times */
2139           error = NULL;
2140         }
2141       else
2142         status = G_FILE_ATTRIBUTE_STATUS_SET;
2143       if (uid)
2144         uid->status = status;
2145       if (gid)
2146         gid->status = status;
2147     }
2148 #endif
2149   
2150   value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2151   if (value)
2152     {
2153       if (!set_unix_mode (filename, value, error))
2154         {
2155           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2156           res = FALSE;
2157           /* Don't set error multiple times */
2158           error = NULL;
2159         }
2160       else
2161         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2162         
2163     }
2164
2165 #ifdef HAVE_UTIMES
2166   /* Group all time settings into one call
2167    * Change times as the last thing to avoid it changing due to metadata changes
2168    */
2169   
2170   mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2171   mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2172   atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2173   atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2174
2175   if (mtime || mtime_usec || atime || atime_usec)
2176     {
2177       if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2178         {
2179           status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2180           res = FALSE;
2181           /* Don't set error multiple times */
2182           error = NULL;
2183         }
2184       else
2185         status = G_FILE_ATTRIBUTE_STATUS_SET;
2186       
2187       if (mtime)
2188         mtime->status = status;
2189       if (mtime_usec)
2190         mtime_usec->status = status;
2191       if (atime)
2192         atime->status = status;
2193       if (atime_usec)
2194         atime_usec->status = status;
2195     }
2196 #endif
2197
2198   /* xattrs are handled by default callback */
2199
2200   return res;
2201 }