Use g_set_error_literal where appropriate. Patch from bug #535947.
[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   
1469 #ifdef S_ISLNK
1470   if (is_symlink)
1471     {
1472       g_file_info_set_is_symlink (info, TRUE);
1473
1474       /* Unless NOFOLLOW was set we default to following symlinks */
1475       if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1476         {
1477           res = stat (path, &statbuf2);
1478
1479             /* Report broken links as symlinks */
1480           if (res != -1)
1481             statbuf = statbuf2;
1482           else
1483             symlink_broken = TRUE;
1484         }
1485     }
1486 #endif
1487
1488   set_info_from_stat (info, &statbuf, attribute_matcher);
1489   
1490 #ifndef G_OS_WIN32
1491   if (basename != NULL && basename[0] == '.')
1492     g_file_info_set_is_hidden (info, TRUE);
1493
1494   if (basename != NULL && basename[strlen (basename) -1] == '~')
1495     g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP, TRUE);
1496 #else
1497   if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1498     g_file_info_set_is_hidden (info, TRUE);
1499
1500   if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1501     g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_DOS_IS_ARCHIVE, TRUE);
1502
1503   if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1504     g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_DOS_IS_SYSTEM, TRUE);
1505 #endif
1506
1507 #ifdef S_ISLNK
1508   if (is_symlink &&
1509       g_file_attribute_matcher_matches (attribute_matcher,
1510                                         G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET))
1511     {
1512       char *link = read_link (path);
1513       g_file_info_set_symlink_target (info, link);
1514       g_free (link);
1515     }
1516 #endif
1517
1518   if (g_file_attribute_matcher_matches (attribute_matcher,
1519                                         G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME))
1520     {
1521       char *display_name = g_filename_display_basename (path);
1522      
1523       /* look for U+FFFD REPLACEMENT CHARACTER */ 
1524       if (strstr (display_name, "\357\277\275") != NULL)
1525         {
1526           char *p = display_name;
1527           display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1528           g_free (p);
1529         }
1530       g_file_info_set_display_name (info, display_name);
1531       g_free (display_name);
1532     }
1533   
1534   if (g_file_attribute_matcher_matches (attribute_matcher,
1535                                         G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME))
1536     {
1537       char *edit_name = g_filename_display_basename (path);
1538       g_file_info_set_edit_name (info, edit_name);
1539       g_free (edit_name);
1540     }
1541
1542   
1543   if (g_file_attribute_matcher_matches (attribute_matcher,
1544                                         G_FILE_ATTRIBUTE_STANDARD_COPY_NAME))
1545     {
1546       char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1547       if (copy_name)
1548         g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME, copy_name);
1549       g_free (copy_name);
1550     }
1551
1552   if (g_file_attribute_matcher_matches (attribute_matcher,
1553                                         G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE) ||
1554       g_file_attribute_matcher_matches (attribute_matcher,
1555                                         G_FILE_ATTRIBUTE_STANDARD_ICON))
1556     {
1557       char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1558
1559       if (content_type)
1560         {
1561           g_file_info_set_content_type (info, content_type);
1562
1563           if (g_file_attribute_matcher_matches (attribute_matcher,
1564                                                 G_FILE_ATTRIBUTE_STANDARD_ICON))
1565             {
1566               GIcon *icon;
1567
1568               icon = g_content_type_get_icon (content_type);
1569               if (icon != NULL)
1570                 {
1571                   if (G_IS_THEMED_ICON (icon))
1572                     {
1573                       const char *preferred_icon = NULL;
1574                       const char *type_icon = NULL;
1575
1576                       if (strcmp (path, g_get_home_dir ()) == 0)
1577                         preferred_icon = "user-home";
1578                       else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0) 
1579                         preferred_icon = "user-desktop";
1580                       if (S_ISDIR (statbuf.st_mode)) 
1581                         type_icon = "folder";
1582                       else if (statbuf.st_mode & S_IXUSR)
1583                         type_icon = "application-x-executable";
1584                       else
1585                         type_icon = "text-x-generic";
1586
1587                       if (preferred_icon)
1588                         g_themed_icon_prepend_name (G_THEMED_ICON (icon), preferred_icon);
1589                       if (type_icon) 
1590                         g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
1591                     }
1592
1593                   g_file_info_set_icon (info, icon);
1594                   g_object_unref (icon);
1595                 }
1596             }
1597           
1598           g_free (content_type);
1599         }
1600     }
1601
1602   if (g_file_attribute_matcher_matches (attribute_matcher,
1603                                         G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE))
1604     {
1605       char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1606       
1607       if (content_type)
1608         {
1609           g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE, content_type);
1610           g_free (content_type);
1611         }
1612     }
1613
1614   if (g_file_attribute_matcher_matches (attribute_matcher,
1615                                         G_FILE_ATTRIBUTE_OWNER_USER))
1616     {
1617       char *name = NULL;
1618       
1619 #ifdef G_OS_WIN32
1620       win32_get_file_user_info (path, NULL, &name, NULL);
1621 #else
1622       name = get_username_from_uid (statbuf.st_uid);
1623 #endif
1624       if (name)
1625         g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1626       g_free (name);
1627     }
1628
1629   if (g_file_attribute_matcher_matches (attribute_matcher,
1630                                         G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1631     {
1632       char *name = NULL;
1633 #ifdef G_OS_WIN32
1634       win32_get_file_user_info (path, NULL, NULL, &name);
1635 #else
1636       name = get_realname_from_uid (statbuf.st_uid);
1637 #endif
1638       if (name)
1639         g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1640       g_free (name);
1641     }
1642   
1643   if (g_file_attribute_matcher_matches (attribute_matcher,
1644                                         G_FILE_ATTRIBUTE_OWNER_GROUP))
1645     {
1646       char *name = NULL;
1647 #ifdef G_OS_WIN32
1648       win32_get_file_user_info (path, &name, NULL, NULL);
1649 #else
1650       name = get_groupname_from_gid (statbuf.st_gid);
1651 #endif
1652       if (name)
1653         g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1654       g_free (name);
1655     }
1656
1657   if (parent_info && parent_info->device != 0 &&
1658       g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1659       statbuf.st_dev != parent_info->device) 
1660     g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1661   
1662   get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1663   
1664   get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1665   get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1666   get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1667
1668   if (g_file_attribute_matcher_matches (attribute_matcher,
1669                                         G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1670     get_thumbnail_attributes (path, info);
1671   
1672   g_file_info_unset_attribute_mask (info);
1673
1674   return info;
1675 }
1676
1677 GFileInfo *
1678 _g_local_file_info_get_from_fd (int      fd,
1679                                 char    *attributes,
1680                                 GError **error)
1681 {
1682   GLocalFileStat stat_buf;
1683   GFileAttributeMatcher *matcher;
1684   GFileInfo *info;
1685   
1686 #ifdef G_OS_WIN32
1687 #define FSTAT _fstati64
1688 #else
1689 #define FSTAT fstat
1690 #endif
1691
1692   if (FSTAT (fd, &stat_buf) == -1)
1693     {
1694       int errsv = errno;
1695
1696       g_set_error (error, G_IO_ERROR,
1697                    g_io_error_from_errno (errsv),
1698                    _("Error stating file descriptor: %s"),
1699                    g_strerror (errsv));
1700       return NULL;
1701     }
1702
1703   info = g_file_info_new ();
1704
1705   matcher = g_file_attribute_matcher_new (attributes);
1706
1707   /* Make sure we don't set any unwanted attributes */
1708   g_file_info_set_attribute_mask (info, matcher);
1709   
1710   set_info_from_stat (info, &stat_buf, matcher);
1711   
1712 #ifdef HAVE_SELINUX
1713   if (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) &&
1714       is_selinux_enabled ())
1715     {
1716       char *context;
1717       if (fgetfilecon_raw (fd, &context) >= 0)
1718         {
1719           g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
1720           freecon (context);
1721         }
1722     }
1723 #endif
1724
1725   get_xattrs_from_fd (fd, TRUE, info, matcher);
1726   get_xattrs_from_fd (fd, FALSE, info, matcher);
1727   
1728   g_file_attribute_matcher_unref (matcher);
1729
1730   g_file_info_unset_attribute_mask (info);
1731   
1732   return info;
1733 }
1734
1735 static gboolean
1736 get_uint32 (const GFileAttributeValue  *value,
1737             guint32                    *val_out,
1738             GError                    **error)
1739 {
1740   if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1741     {
1742       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1743                            _("Invalid attribute type (uint32 expected)"));
1744       return FALSE;
1745     }
1746
1747   *val_out = value->u.uint32;
1748   
1749   return TRUE;
1750 }
1751
1752 #ifdef HAVE_UTIMES
1753 static gboolean
1754 get_uint64 (const GFileAttributeValue  *value,
1755             guint64                    *val_out,
1756             GError                    **error)
1757 {
1758   if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1759     {
1760       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1761                            _("Invalid attribute type (uint64 expected)"));
1762       return FALSE;
1763     }
1764
1765   *val_out = value->u.uint64;
1766   
1767   return TRUE;
1768 }
1769 #endif
1770
1771 #if defined(HAVE_SYMLINK)
1772 static gboolean
1773 get_byte_string (const GFileAttributeValue  *value,
1774                  const char                **val_out,
1775                  GError                    **error)
1776 {
1777   if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1778     {
1779       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1780                            _("Invalid attribute type (byte string expected)"));
1781       return FALSE;
1782     }
1783
1784   *val_out = value->u.string;
1785   
1786   return TRUE;
1787 }
1788 #endif
1789
1790 static gboolean
1791 set_unix_mode (char                       *filename,
1792                const GFileAttributeValue  *value,
1793                GError                    **error)
1794 {
1795   guint32 val;
1796   
1797   if (!get_uint32 (value, &val, error))
1798     return FALSE;
1799   
1800   if (g_chmod (filename, val) == -1)
1801     {
1802       int errsv = errno;
1803
1804       g_set_error (error, G_IO_ERROR,
1805                    g_io_error_from_errno (errsv),
1806                    _("Error setting permissions: %s"),
1807                    g_strerror (errsv));
1808       return FALSE;
1809     }
1810   return TRUE;
1811 }
1812
1813 #ifdef HAVE_CHOWN
1814 static gboolean
1815 set_unix_uid_gid (char                       *filename,
1816                   const GFileAttributeValue  *uid_value,
1817                   const GFileAttributeValue  *gid_value,
1818                   GFileQueryInfoFlags         flags,
1819                   GError                    **error)
1820 {
1821   int res;
1822   guint32 val;
1823   uid_t uid;
1824   gid_t gid;
1825   
1826   if (uid_value)
1827     {
1828       if (!get_uint32 (uid_value, &val, error))
1829         return FALSE;
1830       uid = val;
1831     }
1832   else
1833     uid = -1;
1834   
1835   if (gid_value)
1836     {
1837       if (!get_uint32 (gid_value, &val, error))
1838         return FALSE;
1839       gid = val;
1840     }
1841   else
1842     gid = -1;
1843   
1844 #ifdef HAVE_LCHOWN
1845   if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1846     res = lchown (filename, uid, gid);
1847   else
1848 #endif
1849     res = chown (filename, uid, gid);
1850   
1851   if (res == -1)
1852     {
1853       int errsv = errno;
1854
1855       g_set_error (error, G_IO_ERROR,
1856                    g_io_error_from_errno (errsv),
1857                    _("Error setting owner: %s"),
1858                    g_strerror (errsv));
1859           return FALSE;
1860     }
1861   return TRUE;
1862 }
1863 #endif
1864
1865 #ifdef HAVE_SYMLINK
1866 static gboolean
1867 set_symlink (char                       *filename,
1868              const GFileAttributeValue  *value,
1869              GError                    **error)
1870 {
1871   const char *val;
1872   struct stat statbuf;
1873   
1874   if (!get_byte_string (value, &val, error))
1875     return FALSE;
1876   
1877   if (val == NULL)
1878     {
1879       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1880                            _("symlink must be non-NULL"));
1881       return FALSE;
1882     }
1883   
1884   if (g_lstat (filename, &statbuf))
1885     {
1886       int errsv = errno;
1887
1888       g_set_error (error, G_IO_ERROR,
1889                    g_io_error_from_errno (errsv),
1890                    _("Error setting symlink: %s"),
1891                    g_strerror (errsv));
1892       return FALSE;
1893     }
1894   
1895   if (!S_ISLNK (statbuf.st_mode))
1896     {
1897       g_set_error_literal (error, G_IO_ERROR,
1898                            G_IO_ERROR_NOT_SYMBOLIC_LINK,
1899                            _("Error setting symlink: file is not a symlink"));
1900       return FALSE;
1901     }
1902   
1903   if (g_unlink (filename))
1904     {
1905       int errsv = errno;
1906
1907       g_set_error (error, G_IO_ERROR,
1908                    g_io_error_from_errno (errsv),
1909                    _("Error setting symlink: %s"),
1910                    g_strerror (errsv));
1911       return FALSE;
1912     }
1913   
1914   if (symlink (filename, val) != 0)
1915     {
1916       int errsv = errno;
1917
1918       g_set_error (error, G_IO_ERROR,
1919                    g_io_error_from_errno (errsv),
1920                    _("Error setting symlink: %s"),
1921                    g_strerror (errsv));
1922       return FALSE;
1923     }
1924   
1925   return TRUE;
1926 }
1927 #endif
1928
1929 #ifdef HAVE_UTIMES
1930 static int
1931 lazy_stat (char        *filename, 
1932            struct stat *statbuf, 
1933            gboolean    *called_stat)
1934 {
1935   int res;
1936
1937   if (*called_stat)
1938     return 0;
1939   
1940   res = g_stat (filename, statbuf);
1941   
1942   if (res == 0)
1943     *called_stat = TRUE;
1944   
1945   return res;
1946 }
1947
1948
1949 static gboolean
1950 set_mtime_atime (char                       *filename,
1951                  const GFileAttributeValue  *mtime_value,
1952                  const GFileAttributeValue  *mtime_usec_value,
1953                  const GFileAttributeValue  *atime_value,
1954                  const GFileAttributeValue  *atime_usec_value,
1955                  GError                    **error)
1956 {
1957   int res;
1958   guint64 val;
1959   guint32 val_usec;
1960   struct stat statbuf;
1961   gboolean got_stat = FALSE;
1962   struct timeval times[2] = { {0, 0}, {0, 0} };
1963
1964   /* ATIME */
1965   if (atime_value)
1966     {
1967       if (!get_uint64 (atime_value, &val, error))
1968         return FALSE;
1969       times[0].tv_sec = val;
1970     }
1971   else
1972     {
1973       if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1974         {
1975           times[0].tv_sec = statbuf.st_mtime;
1976 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1977           times[0].tv_usec = statbuf.st_atimensec / 1000;
1978 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1979           times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
1980 #endif
1981         }
1982     }
1983   
1984   if (atime_usec_value)
1985     {
1986       if (!get_uint32 (atime_usec_value, &val_usec, error))
1987         return FALSE;
1988       times[0].tv_usec = val_usec;
1989     }
1990
1991   /* MTIME */
1992   if (mtime_value)
1993     {
1994       if (!get_uint64 (mtime_value, &val, error))
1995         return FALSE;
1996       times[1].tv_sec = val;
1997     }
1998   else
1999     {
2000       if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2001         {
2002           times[1].tv_sec = statbuf.st_mtime;
2003 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2004           times[1].tv_usec = statbuf.st_mtimensec / 1000;
2005 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2006           times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2007 #endif
2008         }
2009     }
2010   
2011   if (mtime_usec_value)
2012     {
2013       if (!get_uint32 (mtime_usec_value, &val_usec, error))
2014         return FALSE;
2015       times[1].tv_usec = val_usec;
2016     }
2017   
2018   res = utimes (filename, times);
2019   if (res == -1)
2020     {
2021       int errsv = errno;
2022
2023       g_set_error (error, G_IO_ERROR,
2024                    g_io_error_from_errno (errsv),
2025                    _("Error setting owner: %s"),
2026                    g_strerror (errsv));
2027           return FALSE;
2028     }
2029   return TRUE;
2030 }
2031 #endif
2032
2033 gboolean
2034 _g_local_file_info_set_attribute (char                 *filename,
2035                                   const char           *attribute,
2036                                   GFileAttributeType    type,
2037                                   gpointer              value_p,
2038                                   GFileQueryInfoFlags   flags,
2039                                   GCancellable         *cancellable,
2040                                   GError              **error)
2041 {
2042   GFileAttributeValue value = { 0 };
2043
2044   _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2045   
2046   if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2047     return set_unix_mode (filename, &value, error);
2048   
2049 #ifdef HAVE_CHOWN
2050   else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2051     return set_unix_uid_gid (filename, &value, NULL, flags, error);
2052   else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2053     return set_unix_uid_gid (filename, NULL, &value, flags, error);
2054 #endif
2055   
2056 #ifdef HAVE_SYMLINK
2057   else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2058     return set_symlink (filename, &value, error);
2059 #endif
2060
2061 #ifdef HAVE_UTIMES
2062   else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2063     return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2064   else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2065     return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2066   else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2067     return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2068   else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2069     return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2070 #endif
2071
2072 #ifdef HAVE_XATTR
2073   else if (g_str_has_prefix (attribute, "xattr::"))
2074     return set_xattr (filename, attribute, &value, error);
2075   else if (g_str_has_prefix (attribute, "xattr-sys::"))
2076     return set_xattr (filename, attribute, &value, error);
2077 #endif
2078   
2079   g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2080                _("Setting attribute %s not supported"), attribute);
2081   return FALSE;
2082 }
2083
2084 gboolean
2085 _g_local_file_info_set_attributes  (char                 *filename,
2086                                     GFileInfo            *info,
2087                                     GFileQueryInfoFlags   flags,
2088                                     GCancellable         *cancellable,
2089                                     GError              **error)
2090 {
2091   GFileAttributeValue *value;
2092 #ifdef HAVE_CHOWN
2093   GFileAttributeValue *uid, *gid;
2094 #endif
2095 #ifdef HAVE_UTIMES
2096   GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2097 #endif
2098 #if defined (HAVE_CHOWN) && defined (HAVE_UTIMES)
2099   GFileAttributeStatus status;
2100 #endif
2101   gboolean res;
2102   
2103   /* Handles setting multiple specified data in a single set, and takes care
2104      of ordering restrictions when setting attributes */
2105
2106   res = TRUE;
2107
2108   /* Set symlink first, since this recreates the file */
2109 #ifdef HAVE_SYMLINK
2110   value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2111   if (value)
2112     {
2113       if (!set_symlink (filename, value, error))
2114         {
2115           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2116           res = FALSE;
2117           /* Don't set error multiple times */
2118           error = NULL;
2119         }
2120       else
2121         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2122         
2123     }
2124 #endif
2125
2126 #ifdef HAVE_CHOWN
2127   /* Group uid and gid setting into one call
2128    * Change ownership before permissions, since ownership changes can
2129      change permissions (e.g. setuid)
2130    */
2131   uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2132   gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2133   
2134   if (uid || gid)
2135     {
2136       if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2137         {
2138           status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2139           res = FALSE;
2140           /* Don't set error multiple times */
2141           error = NULL;
2142         }
2143       else
2144         status = G_FILE_ATTRIBUTE_STATUS_SET;
2145       if (uid)
2146         uid->status = status;
2147       if (gid)
2148         gid->status = status;
2149     }
2150 #endif
2151   
2152   value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2153   if (value)
2154     {
2155       if (!set_unix_mode (filename, value, error))
2156         {
2157           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2158           res = FALSE;
2159           /* Don't set error multiple times */
2160           error = NULL;
2161         }
2162       else
2163         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2164         
2165     }
2166
2167 #ifdef HAVE_UTIMES
2168   /* Group all time settings into one call
2169    * Change times as the last thing to avoid it changing due to metadata changes
2170    */
2171   
2172   mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2173   mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2174   atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2175   atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2176
2177   if (mtime || mtime_usec || atime || atime_usec)
2178     {
2179       if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2180         {
2181           status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2182           res = FALSE;
2183           /* Don't set error multiple times */
2184           error = NULL;
2185         }
2186       else
2187         status = G_FILE_ATTRIBUTE_STATUS_SET;
2188       
2189       if (mtime)
2190         mtime->status = status;
2191       if (mtime_usec)
2192         mtime_usec->status = status;
2193       if (atime)
2194         atime->status = status;
2195       if (atime_usec)
2196         atime_usec->status = status;
2197     }
2198 #endif
2199
2200   /* xattrs are handled by default callback */
2201
2202   return res;
2203 }