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