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