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