A simple test for polling on a file-like descriptor
[glib.git] / gio / glocalfileinfo.c
blob71ba7287d8d93df345cb04ee22826c8243c1ce18
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
5 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "config.h"
25 #include <glib.h>
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 #include <fcntl.h>
34 #include <errno.h>
35 #ifdef G_OS_UNIX
36 #include <grp.h>
37 #include <pwd.h>
38 #endif
39 #ifdef HAVE_SELINUX
40 #include <selinux/selinux.h>
41 #endif
43 #ifdef HAVE_XATTR
45 #if defined HAVE_SYS_XATTR_H
46 #include <sys/xattr.h>
47 #elif defined HAVE_ATTR_XATTR_H
48 #include <attr/xattr.h>
49 #else
50 #error "Neither <sys/xattr.h> nor <attr/xattr.h> is present but extended attribute support is enabled."
51 #endif /* defined HAVE_SYS_XATTR_H || HAVE_ATTR_XATTR_H */
53 #endif /* HAVE_XATTR */
55 #include <glib/gstdio.h>
56 #include <gfileattribute-priv.h>
57 #include <gfileinfo-priv.h>
58 #include <gvfs.h>
60 #ifdef G_OS_UNIX
61 #include <unistd.h>
62 #include "glib-unix.h"
63 #include "glib-private.h"
64 #endif
66 #include "thumbnail-verify.h"
68 #ifdef G_OS_WIN32
69 #include <windows.h>
70 #include <io.h>
71 #ifndef W_OK
72 #define W_OK 2
73 #endif
74 #ifndef R_OK
75 #define R_OK 4
76 #endif
77 #ifndef X_OK
78 #define X_OK 0 /* not really */
79 #endif
80 #ifndef S_ISREG
81 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
82 #endif
83 #ifndef S_ISDIR
84 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
85 #endif
86 #ifndef S_IXUSR
87 #define S_IXUSR _S_IEXEC
88 #endif
89 #endif
91 #include "glocalfileinfo.h"
92 #include "gioerror.h"
93 #include "gthemedicon.h"
94 #include "gcontenttypeprivate.h"
95 #include "glibintl.h"
98 struct ThumbMD5Context {
99 guint32 buf[4];
100 guint32 bits[2];
101 unsigned char in[64];
104 #ifndef G_OS_WIN32
106 typedef struct {
107 char *user_name;
108 char *real_name;
109 } UidData;
111 G_LOCK_DEFINE_STATIC (uid_cache);
112 static GHashTable *uid_cache = NULL;
114 G_LOCK_DEFINE_STATIC (gid_cache);
115 static GHashTable *gid_cache = NULL;
117 #endif /* !G_OS_WIN32 */
119 char *
120 _g_local_file_info_create_etag (GLocalFileStat *statbuf)
122 glong sec, usec;
124 sec = statbuf->st_mtime;
125 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
126 usec = statbuf->st_mtimensec / 1000;
127 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
128 usec = statbuf->st_mtim.tv_nsec / 1000;
129 #else
130 usec = 0;
131 #endif
133 return g_strdup_printf ("%lu:%lu", sec, usec);
136 static char *
137 _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
139 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
140 (guint64) statbuf->st_dev,
141 (guint64) statbuf->st_ino);
144 static char *
145 _g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
147 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
148 (guint64) statbuf->st_dev);
152 #ifdef S_ISLNK
154 static gchar *
155 read_link (const gchar *full_name)
157 #ifdef HAVE_READLINK
158 gchar *buffer;
159 guint size;
161 size = 256;
162 buffer = g_malloc (size);
164 while (1)
166 int read_size;
168 read_size = readlink (full_name, buffer, size);
169 if (read_size < 0)
171 g_free (buffer);
172 return NULL;
174 if (read_size < size)
176 buffer[read_size] = 0;
177 return buffer;
179 size *= 2;
180 buffer = g_realloc (buffer, size);
182 #else
183 return NULL;
184 #endif
187 #endif /* S_ISLNK */
189 #ifdef HAVE_SELINUX
190 /* Get the SELinux security context */
191 static void
192 get_selinux_context (const char *path,
193 GFileInfo *info,
194 GFileAttributeMatcher *attribute_matcher,
195 gboolean follow_symlinks)
197 char *context;
199 if (!_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT))
200 return;
202 if (is_selinux_enabled ())
204 if (follow_symlinks)
206 if (lgetfilecon_raw (path, &context) < 0)
207 return;
209 else
211 if (getfilecon_raw (path, &context) < 0)
212 return;
215 if (context)
217 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
218 freecon (context);
222 #endif
224 #ifdef HAVE_XATTR
226 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
227 * (Mac) getxattr(..., XATTR_NOFOLLOW)
229 #ifdef HAVE_XATTR_NOFOLLOW
230 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
231 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
232 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
233 #else
234 #define g_fgetxattr fgetxattr
235 #define g_flistxattr flistxattr
236 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
237 #endif
239 static ssize_t
240 g_getxattr (const char *path, const char *name, void *value, size_t size,
241 gboolean follow_symlinks)
243 #ifdef HAVE_XATTR_NOFOLLOW
244 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
245 #else
246 if (follow_symlinks)
247 return getxattr (path, name, value, size);
248 else
249 return lgetxattr (path, name, value, size);
250 #endif
253 static ssize_t
254 g_listxattr(const char *path, char *namebuf, size_t size,
255 gboolean follow_symlinks)
257 #ifdef HAVE_XATTR_NOFOLLOW
258 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
259 #else
260 if (follow_symlinks)
261 return listxattr (path, namebuf, size);
262 else
263 return llistxattr (path, namebuf, size);
264 #endif
267 static gboolean
268 valid_char (char c)
270 return c >= 32 && c <= 126 && c != '\\';
273 static gboolean
274 name_is_valid (const char *str)
276 while (*str)
278 if (!valid_char (*str++))
279 return FALSE;
281 return TRUE;
284 static char *
285 hex_escape_string (const char *str,
286 gboolean *free_return)
288 int num_invalid, i;
289 char *escaped_str, *p;
290 unsigned char c;
291 static char *hex_digits = "0123456789abcdef";
292 int len;
294 len = strlen (str);
296 num_invalid = 0;
297 for (i = 0; i < len; i++)
299 if (!valid_char (str[i]))
300 num_invalid++;
303 if (num_invalid == 0)
305 *free_return = FALSE;
306 return (char *)str;
309 escaped_str = g_malloc (len + num_invalid*3 + 1);
311 p = escaped_str;
312 for (i = 0; i < len; i++)
314 if (valid_char (str[i]))
315 *p++ = str[i];
316 else
318 c = str[i];
319 *p++ = '\\';
320 *p++ = 'x';
321 *p++ = hex_digits[(c >> 4) & 0xf];
322 *p++ = hex_digits[c & 0xf];
325 *p = 0;
327 *free_return = TRUE;
328 return escaped_str;
331 static char *
332 hex_unescape_string (const char *str,
333 int *out_len,
334 gboolean *free_return)
336 int i;
337 char *unescaped_str, *p;
338 unsigned char c;
339 int len;
341 len = strlen (str);
343 if (strchr (str, '\\') == NULL)
345 if (out_len)
346 *out_len = len;
347 *free_return = FALSE;
348 return (char *)str;
351 unescaped_str = g_malloc (len + 1);
353 p = unescaped_str;
354 for (i = 0; i < len; i++)
356 if (str[i] == '\\' &&
357 str[i+1] == 'x' &&
358 len - i >= 4)
361 (g_ascii_xdigit_value (str[i+2]) << 4) |
362 g_ascii_xdigit_value (str[i+3]);
363 *p++ = c;
364 i += 3;
366 else
367 *p++ = str[i];
369 *p++ = 0;
371 if (out_len)
372 *out_len = p - unescaped_str;
373 *free_return = TRUE;
374 return unescaped_str;
377 static void
378 escape_xattr (GFileInfo *info,
379 const char *gio_attr, /* gio attribute name */
380 const char *value, /* Is zero terminated */
381 size_t len /* not including zero termination */)
383 char *escaped_val;
384 gboolean free_escaped_val;
386 escaped_val = hex_escape_string (value, &free_escaped_val);
388 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
390 if (free_escaped_val)
391 g_free (escaped_val);
394 static void
395 get_one_xattr (const char *path,
396 GFileInfo *info,
397 const char *gio_attr,
398 const char *xattr,
399 gboolean follow_symlinks)
401 char value[64];
402 char *value_p;
403 ssize_t len;
405 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
407 value_p = NULL;
408 if (len >= 0)
409 value_p = value;
410 else if (len == -1 && errno == ERANGE)
412 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
414 if (len < 0)
415 return;
417 value_p = g_malloc (len+1);
419 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
421 if (len < 0)
423 g_free (value_p);
424 return;
427 else
428 return;
430 /* Null terminate */
431 value_p[len] = 0;
433 escape_xattr (info, gio_attr, value_p, len);
435 if (value_p != value)
436 g_free (value_p);
439 #endif /* defined HAVE_XATTR */
441 static void
442 get_xattrs (const char *path,
443 gboolean user,
444 GFileInfo *info,
445 GFileAttributeMatcher *matcher,
446 gboolean follow_symlinks)
448 #ifdef HAVE_XATTR
449 gboolean all;
450 gsize list_size;
451 ssize_t list_res_size;
452 size_t len;
453 char *list;
454 const char *attr, *attr2;
456 if (user)
457 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
458 else
459 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
461 if (all)
463 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
465 if (list_res_size == -1 ||
466 list_res_size == 0)
467 return;
469 list_size = list_res_size;
470 list = g_malloc (list_size);
472 retry:
474 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
476 if (list_res_size == -1 && errno == ERANGE)
478 list_size = list_size * 2;
479 list = g_realloc (list, list_size);
480 goto retry;
483 if (list_res_size == -1)
484 return;
486 attr = list;
487 while (list_res_size > 0)
489 if ((user && g_str_has_prefix (attr, "user.")) ||
490 (!user && !g_str_has_prefix (attr, "user.")))
492 char *escaped_attr, *gio_attr;
493 gboolean free_escaped_attr;
495 if (user)
497 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
498 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
500 else
502 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
503 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
506 if (free_escaped_attr)
507 g_free (escaped_attr);
509 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
511 g_free (gio_attr);
514 len = strlen (attr) + 1;
515 attr += len;
516 list_res_size -= len;
519 g_free (list);
521 else
523 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
525 char *unescaped_attribute, *a;
526 gboolean free_unescaped_attribute;
528 attr2 = strchr (attr, ':');
529 if (attr2)
531 attr2 += 2; /* Skip '::' */
532 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
533 if (user)
534 a = g_strconcat ("user.", unescaped_attribute, NULL);
535 else
536 a = unescaped_attribute;
538 get_one_xattr (path, info, attr, a, follow_symlinks);
540 if (user)
541 g_free (a);
543 if (free_unescaped_attribute)
544 g_free (unescaped_attribute);
548 #endif /* defined HAVE_XATTR */
551 #ifdef HAVE_XATTR
552 static void
553 get_one_xattr_from_fd (int fd,
554 GFileInfo *info,
555 const char *gio_attr,
556 const char *xattr)
558 char value[64];
559 char *value_p;
560 ssize_t len;
562 len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
564 value_p = NULL;
565 if (len >= 0)
566 value_p = value;
567 else if (len == -1 && errno == ERANGE)
569 len = g_fgetxattr (fd, xattr, NULL, 0);
571 if (len < 0)
572 return;
574 value_p = g_malloc (len + 1);
576 len = g_fgetxattr (fd, xattr, value_p, len);
578 if (len < 0)
580 g_free (value_p);
581 return;
584 else
585 return;
587 /* Null terminate */
588 value_p[len] = 0;
590 escape_xattr (info, gio_attr, value_p, len);
592 if (value_p != value)
593 g_free (value_p);
595 #endif /* defined HAVE_XATTR */
597 static void
598 get_xattrs_from_fd (int fd,
599 gboolean user,
600 GFileInfo *info,
601 GFileAttributeMatcher *matcher)
603 #ifdef HAVE_XATTR
604 gboolean all;
605 gsize list_size;
606 ssize_t list_res_size;
607 size_t len;
608 char *list;
609 const char *attr, *attr2;
611 if (user)
612 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
613 else
614 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
616 if (all)
618 list_res_size = g_flistxattr (fd, NULL, 0);
620 if (list_res_size == -1 ||
621 list_res_size == 0)
622 return;
624 list_size = list_res_size;
625 list = g_malloc (list_size);
627 retry:
629 list_res_size = g_flistxattr (fd, list, list_size);
631 if (list_res_size == -1 && errno == ERANGE)
633 list_size = list_size * 2;
634 list = g_realloc (list, list_size);
635 goto retry;
638 if (list_res_size == -1)
639 return;
641 attr = list;
642 while (list_res_size > 0)
644 if ((user && g_str_has_prefix (attr, "user.")) ||
645 (!user && !g_str_has_prefix (attr, "user.")))
647 char *escaped_attr, *gio_attr;
648 gboolean free_escaped_attr;
650 if (user)
652 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
653 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
655 else
657 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
658 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
661 if (free_escaped_attr)
662 g_free (escaped_attr);
664 get_one_xattr_from_fd (fd, info, gio_attr, attr);
665 g_free (gio_attr);
668 len = strlen (attr) + 1;
669 attr += len;
670 list_res_size -= len;
673 g_free (list);
675 else
677 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
679 char *unescaped_attribute, *a;
680 gboolean free_unescaped_attribute;
682 attr2 = strchr (attr, ':');
683 if (attr2)
685 attr2++; /* Skip ':' */
686 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
687 if (user)
688 a = g_strconcat ("user.", unescaped_attribute, NULL);
689 else
690 a = unescaped_attribute;
692 get_one_xattr_from_fd (fd, info, attr, a);
694 if (user)
695 g_free (a);
697 if (free_unescaped_attribute)
698 g_free (unescaped_attribute);
702 #endif /* defined HAVE_XATTR */
705 #ifdef HAVE_XATTR
706 static gboolean
707 set_xattr (char *filename,
708 const char *escaped_attribute,
709 const GFileAttributeValue *attr_value,
710 GError **error)
712 char *attribute, *value;
713 gboolean free_attribute, free_value;
714 int val_len, res, errsv;
715 gboolean is_user;
716 char *a;
718 if (attr_value == NULL)
720 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
721 _("Attribute value must be non-NULL"));
722 return FALSE;
725 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
727 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
728 _("Invalid attribute type (string expected)"));
729 return FALSE;
732 if (!name_is_valid (escaped_attribute))
734 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
735 _("Invalid extended attribute name"));
736 return FALSE;
739 if (g_str_has_prefix (escaped_attribute, "xattr::"))
741 escaped_attribute += strlen ("xattr::");
742 is_user = TRUE;
744 else
746 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
747 escaped_attribute += strlen ("xattr-sys::");
748 is_user = FALSE;
751 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
752 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
754 if (is_user)
755 a = g_strconcat ("user.", attribute, NULL);
756 else
757 a = attribute;
759 res = g_setxattr (filename, a, value, val_len);
760 errsv = errno;
762 if (is_user)
763 g_free (a);
765 if (free_attribute)
766 g_free (attribute);
768 if (free_value)
769 g_free (value);
771 if (res == -1)
773 g_set_error (error, G_IO_ERROR,
774 g_io_error_from_errno (errsv),
775 _("Error setting extended attribute '%s': %s"),
776 escaped_attribute, g_strerror (errsv));
777 return FALSE;
780 return TRUE;
783 #endif
786 void
787 _g_local_file_info_get_parent_info (const char *dir,
788 GFileAttributeMatcher *attribute_matcher,
789 GLocalParentFileInfo *parent_info)
791 GStatBuf statbuf;
792 int res;
794 parent_info->extra_data = NULL;
795 parent_info->free_extra_data = NULL;
796 parent_info->writable = FALSE;
797 parent_info->is_sticky = FALSE;
798 parent_info->has_trash_dir = FALSE;
799 parent_info->device = 0;
801 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME) ||
802 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE) ||
803 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH) ||
804 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT))
806 /* FIXME: Windows: The underlying _waccess() call in the C
807 * library is mostly pointless as it only looks at the READONLY
808 * FAT-style attribute of the file, it doesn't check the ACL at
809 * all.
811 parent_info->writable = (g_access (dir, W_OK) == 0);
813 res = g_stat (dir, &statbuf);
816 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
817 * renamed or deleted only by the owner of the file, by the owner of the directory, and
818 * by a privileged process.
820 if (res == 0)
822 #ifdef S_ISVTX
823 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
824 #else
825 parent_info->is_sticky = FALSE;
826 #endif
827 parent_info->owner = statbuf.st_uid;
828 parent_info->device = statbuf.st_dev;
829 /* No need to find trash dir if it's not writable anyway */
830 if (parent_info->writable &&
831 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
832 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
837 void
838 _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info)
840 if (parent_info->extra_data &&
841 parent_info->free_extra_data)
842 parent_info->free_extra_data (parent_info->extra_data);
845 static void
846 get_access_rights (GFileAttributeMatcher *attribute_matcher,
847 GFileInfo *info,
848 const gchar *path,
849 GLocalFileStat *statbuf,
850 GLocalParentFileInfo *parent_info)
852 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
853 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
854 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ))
855 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ,
856 g_access (path, R_OK) == 0);
858 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
859 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE))
860 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE,
861 g_access (path, W_OK) == 0);
863 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
864 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE))
865 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE,
866 g_access (path, X_OK) == 0);
869 if (parent_info)
871 gboolean writable;
873 writable = FALSE;
874 if (parent_info->writable)
876 if (parent_info->is_sticky)
878 #ifndef G_OS_WIN32
879 uid_t uid = geteuid ();
881 if (uid == statbuf->st_uid ||
882 uid == parent_info->owner ||
883 uid == 0)
884 #endif
885 writable = TRUE;
887 else
888 writable = TRUE;
891 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME))
892 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME,
893 writable);
895 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE))
896 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE,
897 writable);
899 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
900 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH,
901 writable && parent_info->has_trash_dir);
905 static void
906 set_info_from_stat (GFileInfo *info,
907 GLocalFileStat *statbuf,
908 GFileAttributeMatcher *attribute_matcher)
910 GFileType file_type;
912 file_type = G_FILE_TYPE_UNKNOWN;
914 if (S_ISREG (statbuf->st_mode))
915 file_type = G_FILE_TYPE_REGULAR;
916 else if (S_ISDIR (statbuf->st_mode))
917 file_type = G_FILE_TYPE_DIRECTORY;
918 #ifndef G_OS_WIN32
919 else if (S_ISCHR (statbuf->st_mode) ||
920 S_ISBLK (statbuf->st_mode) ||
921 S_ISFIFO (statbuf->st_mode)
922 #ifdef S_ISSOCK
923 || S_ISSOCK (statbuf->st_mode)
924 #endif
926 file_type = G_FILE_TYPE_SPECIAL;
927 #endif
928 #ifdef S_ISLNK
929 else if (S_ISLNK (statbuf->st_mode))
930 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
931 #endif
933 g_file_info_set_file_type (info, file_type);
934 g_file_info_set_size (info, statbuf->st_size);
936 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
937 #ifndef G_OS_WIN32
938 /* Pointless setting these on Windows even if they exist in the struct */
939 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
940 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
941 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
942 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
943 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
944 #endif
945 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
946 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
947 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
948 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
949 #endif
950 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
951 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
952 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
953 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
954 #endif
956 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
957 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
958 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
959 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
960 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
961 #endif
963 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
964 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
965 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
966 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
967 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
968 #endif
970 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
971 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
972 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
973 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
974 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
975 #endif
977 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
978 G_FILE_ATTRIBUTE_ID_ETAG_VALUE))
980 char *etag = _g_local_file_info_create_etag (statbuf);
981 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ETAG_VALUE, etag);
982 g_free (etag);
985 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
986 G_FILE_ATTRIBUTE_ID_ID_FILE))
988 char *id = _g_local_file_info_create_file_id (statbuf);
989 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILE, id);
990 g_free (id);
993 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
994 G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM))
996 char *id = _g_local_file_info_create_fs_id (statbuf);
997 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM, id);
998 g_free (id);
1002 #ifndef G_OS_WIN32
1004 static char *
1005 make_valid_utf8 (const char *name)
1007 GString *string;
1008 const gchar *remainder, *invalid;
1009 gint remaining_bytes, valid_bytes;
1011 string = NULL;
1012 remainder = name;
1013 remaining_bytes = strlen (name);
1015 while (remaining_bytes != 0)
1017 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1018 break;
1019 valid_bytes = invalid - remainder;
1021 if (string == NULL)
1022 string = g_string_sized_new (remaining_bytes);
1024 g_string_append_len (string, remainder, valid_bytes);
1025 /* append U+FFFD REPLACEMENT CHARACTER */
1026 g_string_append (string, "\357\277\275");
1028 remaining_bytes -= valid_bytes + 1;
1029 remainder = invalid + 1;
1032 if (string == NULL)
1033 return g_strdup (name);
1035 g_string_append (string, remainder);
1037 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1039 return g_string_free (string, FALSE);
1042 static char *
1043 convert_pwd_string_to_utf8 (char *pwd_str)
1045 char *utf8_string;
1047 if (!g_utf8_validate (pwd_str, -1, NULL))
1049 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1050 if (utf8_string == NULL)
1051 utf8_string = make_valid_utf8 (pwd_str);
1053 else
1054 utf8_string = g_strdup (pwd_str);
1056 return utf8_string;
1059 static void
1060 uid_data_free (UidData *data)
1062 g_free (data->user_name);
1063 g_free (data->real_name);
1064 g_free (data);
1067 /* called with lock held */
1068 static UidData *
1069 lookup_uid_data (uid_t uid)
1071 UidData *data;
1072 char buffer[4096];
1073 struct passwd pwbuf;
1074 struct passwd *pwbufp;
1075 char *gecos, *comma;
1077 if (uid_cache == NULL)
1078 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1080 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1082 if (data)
1083 return data;
1085 data = g_new0 (UidData, 1);
1087 #if defined(HAVE_POSIX_GETPWUID_R)
1088 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1089 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1090 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1091 #else
1092 pwbufp = getpwuid (uid);
1093 #endif
1095 if (pwbufp != NULL)
1097 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1098 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1100 #ifndef __BIONIC__
1101 gecos = pwbufp->pw_gecos;
1103 if (gecos)
1105 comma = strchr (gecos, ',');
1106 if (comma)
1107 *comma = 0;
1108 data->real_name = convert_pwd_string_to_utf8 (gecos);
1110 #endif
1113 /* Default fallbacks */
1114 if (data->real_name == NULL)
1116 if (data->user_name != NULL)
1117 data->real_name = g_strdup (data->user_name);
1118 else
1119 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1122 if (data->user_name == NULL)
1123 data->user_name = g_strdup_printf ("%d", (int)uid);
1125 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1127 return data;
1130 static char *
1131 get_username_from_uid (uid_t uid)
1133 char *res;
1134 UidData *data;
1136 G_LOCK (uid_cache);
1137 data = lookup_uid_data (uid);
1138 res = g_strdup (data->user_name);
1139 G_UNLOCK (uid_cache);
1141 return res;
1144 static char *
1145 get_realname_from_uid (uid_t uid)
1147 char *res;
1148 UidData *data;
1150 G_LOCK (uid_cache);
1151 data = lookup_uid_data (uid);
1152 res = g_strdup (data->real_name);
1153 G_UNLOCK (uid_cache);
1155 return res;
1158 /* called with lock held */
1159 static char *
1160 lookup_gid_name (gid_t gid)
1162 char *name;
1163 char buffer[4096];
1164 struct group gbuf;
1165 struct group *gbufp;
1167 if (gid_cache == NULL)
1168 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1170 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1172 if (name)
1173 return name;
1175 #if defined (HAVE_POSIX_GETGRGID_R)
1176 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1177 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1178 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1179 #else
1180 gbufp = getgrgid (gid);
1181 #endif
1183 if (gbufp != NULL &&
1184 gbufp->gr_name != NULL &&
1185 gbufp->gr_name[0] != 0)
1186 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1187 else
1188 name = g_strdup_printf("%d", (int)gid);
1190 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1192 return name;
1195 static char *
1196 get_groupname_from_gid (gid_t gid)
1198 char *res;
1199 char *name;
1201 G_LOCK (gid_cache);
1202 name = lookup_gid_name (gid);
1203 res = g_strdup (name);
1204 G_UNLOCK (gid_cache);
1205 return res;
1208 #endif /* !G_OS_WIN32 */
1210 static char *
1211 get_content_type (const char *basename,
1212 const char *path,
1213 GLocalFileStat *statbuf,
1214 gboolean is_symlink,
1215 gboolean symlink_broken,
1216 GFileQueryInfoFlags flags,
1217 gboolean fast)
1219 if (is_symlink &&
1220 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1221 return g_strdup ("inode/symlink");
1222 else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
1223 return g_strdup ("inode/directory");
1224 #ifndef G_OS_WIN32
1225 else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
1226 return g_strdup ("inode/chardevice");
1227 else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
1228 return g_strdup ("inode/blockdevice");
1229 else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
1230 return g_strdup ("inode/fifo");
1231 #endif
1232 #ifdef S_ISSOCK
1233 else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
1234 return g_strdup ("inode/socket");
1235 #endif
1236 else
1238 char *content_type;
1239 gboolean result_uncertain;
1241 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1243 #ifndef G_OS_WIN32
1244 if (!fast && result_uncertain && path != NULL)
1246 guchar sniff_buffer[4096];
1247 gsize sniff_length;
1248 int fd;
1250 sniff_length = _g_unix_content_type_get_sniff_len ();
1251 if (sniff_length > 4096)
1252 sniff_length = 4096;
1254 #ifdef O_NOATIME
1255 fd = g_open (path, O_RDONLY | O_NOATIME, 0);
1256 if (fd < 0 && errno == EPERM)
1257 #endif
1258 fd = g_open (path, O_RDONLY, 0);
1260 if (fd != -1)
1262 ssize_t res;
1264 res = read (fd, sniff_buffer, sniff_length);
1265 (void) g_close (fd, NULL);
1266 if (res >= 0)
1268 g_free (content_type);
1269 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1273 #endif
1275 return content_type;
1280 /* @stat_buf is the pre-calculated result of stat(path), or %NULL if that failed. */
1281 static void
1282 get_thumbnail_attributes (const char *path,
1283 GFileInfo *info,
1284 const GLocalFileStat *stat_buf)
1286 GChecksum *checksum;
1287 char *uri;
1288 char *filename;
1289 char *basename;
1291 uri = g_filename_to_uri (path, NULL, NULL);
1293 checksum = g_checksum_new (G_CHECKSUM_MD5);
1294 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1296 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1297 g_checksum_free (checksum);
1299 filename = g_build_filename (g_get_user_cache_dir (),
1300 "thumbnails", "large", basename,
1301 NULL);
1303 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1305 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1306 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1307 thumbnail_verify (filename, uri, stat_buf));
1309 else
1311 g_free (filename);
1312 filename = g_build_filename (g_get_user_cache_dir (),
1313 "thumbnails", "normal", basename,
1314 NULL);
1316 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1318 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1319 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1320 thumbnail_verify (filename, uri, stat_buf));
1322 else
1324 g_free (filename);
1325 filename = g_build_filename (g_get_user_cache_dir (),
1326 "thumbnails", "fail",
1327 "gnome-thumbnail-factory",
1328 basename,
1329 NULL);
1331 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1333 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1334 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1335 thumbnail_verify (filename, uri, stat_buf));
1339 g_free (basename);
1340 g_free (filename);
1341 g_free (uri);
1344 #ifdef G_OS_WIN32
1345 static void
1346 win32_get_file_user_info (const gchar *filename,
1347 gchar **group_name,
1348 gchar **user_name,
1349 gchar **real_name)
1351 PSECURITY_DESCRIPTOR psd = NULL;
1352 DWORD sd_size = 0; /* first call calculates the size required */
1354 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1355 if ((GetFileSecurityW (wfilename,
1356 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1357 NULL,
1358 sd_size,
1359 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1360 (psd = g_try_malloc (sd_size)) != NULL &&
1361 GetFileSecurityW (wfilename,
1362 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1363 psd,
1364 sd_size,
1365 &sd_size))
1367 PSID psid = 0;
1368 BOOL defaulted;
1369 SID_NAME_USE name_use = 0; /* don't care? */
1370 wchar_t *name = NULL;
1371 wchar_t *domain = NULL;
1372 DWORD name_len = 0;
1373 DWORD domain_len = 0;
1374 /* get the user name */
1375 do {
1376 if (!user_name)
1377 break;
1378 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1379 break;
1380 if (!LookupAccountSidW (NULL, /* local machine */
1381 psid,
1382 name, &name_len,
1383 domain, &domain_len, /* no domain info yet */
1384 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1385 break;
1386 name = g_try_malloc (name_len * sizeof (wchar_t));
1387 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1388 if (name && domain &&
1389 LookupAccountSidW (NULL, /* local machine */
1390 psid,
1391 name, &name_len,
1392 domain, &domain_len, /* no domain info yet */
1393 &name_use))
1395 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1397 g_free (name);
1398 g_free (domain);
1399 } while (FALSE);
1401 /* get the group name */
1402 do {
1403 if (!group_name)
1404 break;
1405 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1406 break;
1407 if (!LookupAccountSidW (NULL, /* local machine */
1408 psid,
1409 name, &name_len,
1410 domain, &domain_len, /* no domain info yet */
1411 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1412 break;
1413 name = g_try_malloc (name_len * sizeof (wchar_t));
1414 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1415 if (name && domain &&
1416 LookupAccountSidW (NULL, /* local machine */
1417 psid,
1418 name, &name_len,
1419 domain, &domain_len, /* no domain info yet */
1420 &name_use))
1422 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1424 g_free (name);
1425 g_free (domain);
1426 } while (FALSE);
1428 /* TODO: get real name */
1430 g_free (psd);
1432 g_free (wfilename);
1434 #endif /* G_OS_WIN32 */
1436 #ifndef G_OS_WIN32
1437 /* support for '.hidden' files */
1438 G_LOCK_DEFINE_STATIC (hidden_cache);
1439 static GHashTable *hidden_cache;
1441 static gboolean
1442 remove_from_hidden_cache (gpointer user_data)
1444 G_LOCK (hidden_cache);
1445 g_hash_table_remove (hidden_cache, user_data);
1446 G_UNLOCK (hidden_cache);
1448 return FALSE;
1451 static GHashTable *
1452 read_hidden_file (const gchar *dirname)
1454 gchar *contents = NULL;
1455 gchar *filename;
1457 filename = g_build_path ("/", dirname, ".hidden", NULL);
1458 g_file_get_contents (filename, &contents, NULL, NULL);
1459 g_free (filename);
1461 if (contents != NULL)
1463 GHashTable *table;
1464 gchar **lines;
1465 gint i;
1467 table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1469 lines = g_strsplit (contents, "\n", 0);
1470 g_free (contents);
1472 for (i = 0; lines[i]; i++)
1473 /* hash table takes the individual strings... */
1474 g_hash_table_add (table, lines[i]);
1476 /* ... so we only free the container. */
1477 g_free (lines);
1479 return table;
1481 else
1482 return NULL;
1485 static void
1486 maybe_unref_hash_table (gpointer data)
1488 if (data != NULL)
1489 g_hash_table_unref (data);
1492 static gboolean
1493 file_is_hidden (const gchar *path,
1494 const gchar *basename)
1496 gboolean result;
1497 gchar *dirname;
1498 gpointer table;
1500 dirname = g_path_get_dirname (path);
1502 G_LOCK (hidden_cache);
1504 if G_UNLIKELY (hidden_cache == NULL)
1505 hidden_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1506 g_free, maybe_unref_hash_table);
1508 if (!g_hash_table_lookup_extended (hidden_cache, dirname,
1509 NULL, &table))
1511 gchar *mydirname;
1512 GSource *remove_from_cache_source;
1514 g_hash_table_insert (hidden_cache,
1515 mydirname = g_strdup (dirname),
1516 table = read_hidden_file (dirname));
1518 remove_from_cache_source = g_timeout_source_new_seconds (5);
1519 g_source_set_priority (remove_from_cache_source, G_PRIORITY_DEFAULT);
1520 g_source_set_callback (remove_from_cache_source,
1521 remove_from_hidden_cache,
1522 mydirname,
1523 NULL);
1524 g_source_attach (remove_from_cache_source,
1525 GLIB_PRIVATE_CALL (g_get_worker_context) ());
1526 g_source_unref (remove_from_cache_source);
1529 result = table != NULL && g_hash_table_contains (table, basename);
1531 G_UNLOCK (hidden_cache);
1533 g_free (dirname);
1535 return result;
1537 #endif /* !G_OS_WIN32 */
1539 void
1540 _g_local_file_info_get_nostat (GFileInfo *info,
1541 const char *basename,
1542 const char *path,
1543 GFileAttributeMatcher *attribute_matcher)
1545 g_file_info_set_name (info, basename);
1547 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1548 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1550 char *display_name = g_filename_display_basename (path);
1552 /* look for U+FFFD REPLACEMENT CHARACTER */
1553 if (strstr (display_name, "\357\277\275") != NULL)
1555 char *p = display_name;
1556 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1557 g_free (p);
1559 g_file_info_set_display_name (info, display_name);
1560 g_free (display_name);
1563 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1564 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1566 char *edit_name = g_filename_display_basename (path);
1567 g_file_info_set_edit_name (info, edit_name);
1568 g_free (edit_name);
1572 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1573 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1575 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1576 if (copy_name)
1577 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1578 g_free (copy_name);
1582 static const char *
1583 get_icon_name (const char *path,
1584 const char *content_type,
1585 gboolean use_symbolic,
1586 gboolean *with_fallbacks_out)
1588 const char *name = NULL;
1589 gboolean with_fallbacks = TRUE;
1591 if (strcmp (path, g_get_home_dir ()) == 0)
1593 name = use_symbolic ? "user-home-symbolic" : "user-home";
1594 with_fallbacks = FALSE;
1596 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1598 name = use_symbolic ? "user-desktop-symbolic" : "user-desktop";
1599 with_fallbacks = FALSE;
1601 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1603 name = use_symbolic ? "folder-documents-symbolic" : "folder-documents";
1605 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1607 name = use_symbolic ? "folder-download-symbolic" : "folder-download";
1609 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1611 name = use_symbolic ? "folder-music-symbolic" : "folder-music";
1613 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1615 name = use_symbolic ? "folder-pictures-symbolic" : "folder-pictures";
1617 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1619 name = use_symbolic ? "folder-publicshare-symbolic" : "folder-publicshare";
1621 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1623 name = use_symbolic ? "folder-templates-symbolic" : "folder-templates";
1625 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1627 name = use_symbolic ? "folder-videos-symbolic" : "folder-videos";
1629 else if (g_strcmp0 (content_type, "inode/directory") == 0)
1631 name = use_symbolic ? "folder-symbolic" : "folder";
1633 else
1635 name = NULL;
1638 if (with_fallbacks_out != NULL)
1639 *with_fallbacks_out = with_fallbacks;
1641 return name;
1644 static GIcon *
1645 get_icon (const char *path,
1646 const char *content_type,
1647 gboolean use_symbolic)
1649 GIcon *icon = NULL;
1650 const char *icon_name;
1651 gboolean with_fallbacks;
1653 icon_name = get_icon_name (path, content_type, use_symbolic, &with_fallbacks);
1654 if (icon_name != NULL)
1656 if (with_fallbacks)
1657 icon = g_themed_icon_new_with_default_fallbacks (icon_name);
1658 else
1659 icon = g_themed_icon_new (icon_name);
1661 else
1663 if (use_symbolic)
1664 icon = g_content_type_get_symbolic_icon (content_type);
1665 else
1666 icon = g_content_type_get_icon (content_type);
1669 return icon;
1672 GFileInfo *
1673 _g_local_file_info_get (const char *basename,
1674 const char *path,
1675 GFileAttributeMatcher *attribute_matcher,
1676 GFileQueryInfoFlags flags,
1677 GLocalParentFileInfo *parent_info,
1678 GError **error)
1680 GFileInfo *info;
1681 GLocalFileStat statbuf;
1682 #ifdef S_ISLNK
1683 struct stat statbuf2;
1684 #endif
1685 int res;
1686 gboolean stat_ok;
1687 gboolean is_symlink, symlink_broken;
1688 #ifdef G_OS_WIN32
1689 DWORD dos_attributes;
1690 #endif
1691 char *symlink_target;
1692 GVfs *vfs;
1693 GVfsClass *class;
1694 guint64 device;
1696 info = g_file_info_new ();
1698 /* Make sure we don't set any unwanted attributes */
1699 g_file_info_set_attribute_mask (info, attribute_matcher);
1701 _g_local_file_info_get_nostat (info, basename, path, attribute_matcher);
1703 if (attribute_matcher == NULL)
1705 g_file_info_unset_attribute_mask (info);
1706 return info;
1709 #ifndef G_OS_WIN32
1710 res = g_lstat (path, &statbuf);
1711 #else
1713 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1714 int len;
1716 if (wpath == NULL)
1718 g_object_unref (info);
1719 return NULL;
1722 len = wcslen (wpath);
1723 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1724 len--;
1725 if (len > 0 &&
1726 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1727 wpath[len] = '\0';
1729 res = _wstati64 (wpath, &statbuf);
1730 dos_attributes = GetFileAttributesW (wpath);
1732 g_free (wpath);
1734 #endif
1736 if (res == -1)
1738 int errsv = errno;
1740 /* Don't bail out if we get Permission denied (SELinux?) */
1741 if (errsv != EACCES)
1743 char *display_name = g_filename_display_name (path);
1744 g_object_unref (info);
1745 g_set_error (error, G_IO_ERROR,
1746 g_io_error_from_errno (errsv),
1747 _("Error when getting information for file '%s': %s"),
1748 display_name, g_strerror (errsv));
1749 g_free (display_name);
1750 return NULL;
1754 /* Even if stat() fails, try to get as much as other attributes possible */
1755 stat_ok = res != -1;
1757 if (stat_ok)
1758 device = statbuf.st_dev;
1759 else
1760 device = 0;
1762 #ifdef S_ISLNK
1763 is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
1764 #else
1765 is_symlink = FALSE;
1766 #endif
1767 symlink_broken = FALSE;
1768 #ifdef S_ISLNK
1769 if (is_symlink)
1771 g_file_info_set_is_symlink (info, TRUE);
1773 /* Unless NOFOLLOW was set we default to following symlinks */
1774 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1776 res = stat (path, &statbuf2);
1778 /* Report broken links as symlinks */
1779 if (res != -1)
1781 statbuf = statbuf2;
1782 stat_ok = TRUE;
1784 else
1785 symlink_broken = TRUE;
1788 #endif
1790 if (stat_ok)
1791 set_info_from_stat (info, &statbuf, attribute_matcher);
1793 #ifdef G_OS_UNIX
1794 if (stat_ok && _g_local_file_is_lost_found_dir (path, statbuf.st_dev))
1795 g_file_info_set_is_hidden (info, TRUE);
1796 #endif
1798 #ifndef G_OS_WIN32
1799 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1800 G_FILE_ATTRIBUTE_ID_STANDARD_IS_HIDDEN))
1802 if (basename != NULL &&
1803 (basename[0] == '.' ||
1804 file_is_hidden (path, basename)))
1805 g_file_info_set_is_hidden (info, TRUE);
1808 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1809 (stat_ok && S_ISREG (statbuf.st_mode)))
1810 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1811 #else
1812 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1813 g_file_info_set_is_hidden (info, TRUE);
1815 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1816 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1818 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1819 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1820 #endif
1822 symlink_target = NULL;
1823 #ifdef S_ISLNK
1824 if (is_symlink)
1826 symlink_target = read_link (path);
1827 if (symlink_target &&
1828 _g_file_attribute_matcher_matches_id (attribute_matcher,
1829 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1830 g_file_info_set_symlink_target (info, symlink_target);
1832 #endif
1833 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1834 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1835 _g_file_attribute_matcher_matches_id (attribute_matcher,
1836 G_FILE_ATTRIBUTE_ID_STANDARD_ICON) ||
1837 _g_file_attribute_matcher_matches_id (attribute_matcher,
1838 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1840 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
1842 if (content_type)
1844 g_file_info_set_content_type (info, content_type);
1846 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1847 G_FILE_ATTRIBUTE_ID_STANDARD_ICON)
1848 || _g_file_attribute_matcher_matches_id (attribute_matcher,
1849 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1851 GIcon *icon;
1853 /* non symbolic icon */
1854 icon = get_icon (path, content_type, FALSE);
1855 if (icon != NULL)
1857 g_file_info_set_icon (info, icon);
1858 g_object_unref (icon);
1861 /* symbolic icon */
1862 icon = get_icon (path, content_type, TRUE);
1863 if (icon != NULL)
1865 g_file_info_set_symbolic_icon (info, icon);
1866 g_object_unref (icon);
1871 g_free (content_type);
1875 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1876 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1878 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
1880 if (content_type)
1882 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1883 g_free (content_type);
1887 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1888 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1890 char *name = NULL;
1892 #ifdef G_OS_WIN32
1893 win32_get_file_user_info (path, NULL, &name, NULL);
1894 #else
1895 if (stat_ok)
1896 name = get_username_from_uid (statbuf.st_uid);
1897 #endif
1898 if (name)
1899 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1900 g_free (name);
1903 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1904 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1906 char *name = NULL;
1907 #ifdef G_OS_WIN32
1908 win32_get_file_user_info (path, NULL, NULL, &name);
1909 #else
1910 if (stat_ok)
1911 name = get_realname_from_uid (statbuf.st_uid);
1912 #endif
1913 if (name)
1914 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1915 g_free (name);
1918 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1919 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1921 char *name = NULL;
1922 #ifdef G_OS_WIN32
1923 win32_get_file_user_info (path, &name, NULL, NULL);
1924 #else
1925 if (stat_ok)
1926 name = get_groupname_from_gid (statbuf.st_gid);
1927 #endif
1928 if (name)
1929 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1930 g_free (name);
1933 if (stat_ok && parent_info && parent_info->device != 0 &&
1934 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1935 statbuf.st_dev != parent_info->device)
1936 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1938 if (stat_ok)
1939 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1941 #ifdef HAVE_SELINUX
1942 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1943 #endif
1944 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1945 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1947 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1948 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH))
1950 if (stat_ok)
1951 get_thumbnail_attributes (path, info, &statbuf);
1952 else
1953 get_thumbnail_attributes (path, info, NULL);
1956 vfs = g_vfs_get_default ();
1957 class = G_VFS_GET_CLASS (vfs);
1958 if (class->local_file_add_info)
1960 class->local_file_add_info (vfs,
1961 path,
1962 device,
1963 attribute_matcher,
1964 info,
1965 NULL,
1966 &parent_info->extra_data,
1967 &parent_info->free_extra_data);
1970 g_file_info_unset_attribute_mask (info);
1972 g_free (symlink_target);
1974 return info;
1977 GFileInfo *
1978 _g_local_file_info_get_from_fd (int fd,
1979 const char *attributes,
1980 GError **error)
1982 GLocalFileStat stat_buf;
1983 GFileAttributeMatcher *matcher;
1984 GFileInfo *info;
1986 #ifdef G_OS_WIN32
1987 #define FSTAT _fstati64
1988 #else
1989 #define FSTAT fstat
1990 #endif
1992 if (FSTAT (fd, &stat_buf) == -1)
1994 int errsv = errno;
1996 g_set_error (error, G_IO_ERROR,
1997 g_io_error_from_errno (errsv),
1998 _("Error when getting information for file descriptor: %s"),
1999 g_strerror (errsv));
2000 return NULL;
2003 info = g_file_info_new ();
2005 matcher = g_file_attribute_matcher_new (attributes);
2007 /* Make sure we don't set any unwanted attributes */
2008 g_file_info_set_attribute_mask (info, matcher);
2010 set_info_from_stat (info, &stat_buf, matcher);
2012 #ifdef HAVE_SELINUX
2013 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
2014 is_selinux_enabled ())
2016 char *context;
2017 if (fgetfilecon_raw (fd, &context) >= 0)
2019 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
2020 freecon (context);
2023 #endif
2025 get_xattrs_from_fd (fd, TRUE, info, matcher);
2026 get_xattrs_from_fd (fd, FALSE, info, matcher);
2028 g_file_attribute_matcher_unref (matcher);
2030 g_file_info_unset_attribute_mask (info);
2032 return info;
2035 static gboolean
2036 get_uint32 (const GFileAttributeValue *value,
2037 guint32 *val_out,
2038 GError **error)
2040 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
2042 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2043 _("Invalid attribute type (uint32 expected)"));
2044 return FALSE;
2047 *val_out = value->u.uint32;
2049 return TRUE;
2052 #ifdef HAVE_UTIMES
2053 static gboolean
2054 get_uint64 (const GFileAttributeValue *value,
2055 guint64 *val_out,
2056 GError **error)
2058 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
2060 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2061 _("Invalid attribute type (uint64 expected)"));
2062 return FALSE;
2065 *val_out = value->u.uint64;
2067 return TRUE;
2069 #endif
2071 #if defined(HAVE_SYMLINK)
2072 static gboolean
2073 get_byte_string (const GFileAttributeValue *value,
2074 const char **val_out,
2075 GError **error)
2077 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
2079 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2080 _("Invalid attribute type (byte string expected)"));
2081 return FALSE;
2084 *val_out = value->u.string;
2086 return TRUE;
2088 #endif
2090 #ifdef HAVE_SELINUX
2091 static gboolean
2092 get_string (const GFileAttributeValue *value,
2093 const char **val_out,
2094 GError **error)
2096 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
2098 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2099 _("Invalid attribute type (byte string expected)"));
2100 return FALSE;
2103 *val_out = value->u.string;
2105 return TRUE;
2107 #endif
2109 static gboolean
2110 set_unix_mode (char *filename,
2111 GFileQueryInfoFlags flags,
2112 const GFileAttributeValue *value,
2113 GError **error)
2115 guint32 val = 0;
2116 int res = 0;
2118 if (!get_uint32 (value, &val, error))
2119 return FALSE;
2121 #ifdef HAVE_SYMLINK
2122 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
2123 #ifdef HAVE_LCHMOD
2124 res = lchmod (filename, val);
2125 #else
2126 struct stat statbuf;
2127 /* Calling chmod on a symlink changes permissions on the symlink.
2128 * We don't want to do this, so we need to check for a symlink */
2129 res = g_lstat (filename, &statbuf);
2130 if (res == 0 && S_ISLNK (statbuf.st_mode))
2132 g_set_error_literal (error, G_IO_ERROR,
2133 G_IO_ERROR_NOT_SUPPORTED,
2134 _("Cannot set permissions on symlinks"));
2135 return FALSE;
2137 else if (res == 0)
2138 res = g_chmod (filename, val);
2139 #endif
2140 } else
2141 #endif
2142 res = g_chmod (filename, val);
2144 if (res == -1)
2146 int errsv = errno;
2148 g_set_error (error, G_IO_ERROR,
2149 g_io_error_from_errno (errsv),
2150 _("Error setting permissions: %s"),
2151 g_strerror (errsv));
2152 return FALSE;
2154 return TRUE;
2157 #ifdef G_OS_UNIX
2158 static gboolean
2159 set_unix_uid_gid (char *filename,
2160 const GFileAttributeValue *uid_value,
2161 const GFileAttributeValue *gid_value,
2162 GFileQueryInfoFlags flags,
2163 GError **error)
2165 int res;
2166 guint32 val = 0;
2167 uid_t uid;
2168 gid_t gid;
2170 if (uid_value)
2172 if (!get_uint32 (uid_value, &val, error))
2173 return FALSE;
2174 uid = val;
2176 else
2177 uid = -1;
2179 if (gid_value)
2181 if (!get_uint32 (gid_value, &val, error))
2182 return FALSE;
2183 gid = val;
2185 else
2186 gid = -1;
2188 #ifdef HAVE_LCHOWN
2189 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
2190 res = lchown (filename, uid, gid);
2191 else
2192 #endif
2193 res = chown (filename, uid, gid);
2195 if (res == -1)
2197 int errsv = errno;
2199 g_set_error (error, G_IO_ERROR,
2200 g_io_error_from_errno (errsv),
2201 _("Error setting owner: %s"),
2202 g_strerror (errsv));
2203 return FALSE;
2205 return TRUE;
2207 #endif
2209 #ifdef HAVE_SYMLINK
2210 static gboolean
2211 set_symlink (char *filename,
2212 const GFileAttributeValue *value,
2213 GError **error)
2215 const char *val;
2216 struct stat statbuf;
2218 if (!get_byte_string (value, &val, error))
2219 return FALSE;
2221 if (val == NULL)
2223 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2224 _("symlink must be non-NULL"));
2225 return FALSE;
2228 if (g_lstat (filename, &statbuf))
2230 int errsv = errno;
2232 g_set_error (error, G_IO_ERROR,
2233 g_io_error_from_errno (errsv),
2234 _("Error setting symlink: %s"),
2235 g_strerror (errsv));
2236 return FALSE;
2239 if (!S_ISLNK (statbuf.st_mode))
2241 g_set_error_literal (error, G_IO_ERROR,
2242 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2243 _("Error setting symlink: file is not a symlink"));
2244 return FALSE;
2247 if (g_unlink (filename))
2249 int errsv = errno;
2251 g_set_error (error, G_IO_ERROR,
2252 g_io_error_from_errno (errsv),
2253 _("Error setting symlink: %s"),
2254 g_strerror (errsv));
2255 return FALSE;
2258 if (symlink (filename, val) != 0)
2260 int errsv = errno;
2262 g_set_error (error, G_IO_ERROR,
2263 g_io_error_from_errno (errsv),
2264 _("Error setting symlink: %s"),
2265 g_strerror (errsv));
2266 return FALSE;
2269 return TRUE;
2271 #endif
2273 #ifdef HAVE_UTIMES
2274 static int
2275 lazy_stat (char *filename,
2276 struct stat *statbuf,
2277 gboolean *called_stat)
2279 int res;
2281 if (*called_stat)
2282 return 0;
2284 res = g_stat (filename, statbuf);
2286 if (res == 0)
2287 *called_stat = TRUE;
2289 return res;
2293 static gboolean
2294 set_mtime_atime (char *filename,
2295 const GFileAttributeValue *mtime_value,
2296 const GFileAttributeValue *mtime_usec_value,
2297 const GFileAttributeValue *atime_value,
2298 const GFileAttributeValue *atime_usec_value,
2299 GError **error)
2301 int res;
2302 guint64 val = 0;
2303 guint32 val_usec = 0;
2304 struct stat statbuf;
2305 gboolean got_stat = FALSE;
2306 struct timeval times[2] = { {0, 0}, {0, 0} };
2308 /* ATIME */
2309 if (atime_value)
2311 if (!get_uint64 (atime_value, &val, error))
2312 return FALSE;
2313 times[0].tv_sec = val;
2315 else
2317 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2319 times[0].tv_sec = statbuf.st_mtime;
2320 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2321 times[0].tv_usec = statbuf.st_atimensec / 1000;
2322 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2323 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2324 #endif
2328 if (atime_usec_value)
2330 if (!get_uint32 (atime_usec_value, &val_usec, error))
2331 return FALSE;
2332 times[0].tv_usec = val_usec;
2335 /* MTIME */
2336 if (mtime_value)
2338 if (!get_uint64 (mtime_value, &val, error))
2339 return FALSE;
2340 times[1].tv_sec = val;
2342 else
2344 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2346 times[1].tv_sec = statbuf.st_mtime;
2347 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2348 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2349 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2350 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2351 #endif
2355 if (mtime_usec_value)
2357 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2358 return FALSE;
2359 times[1].tv_usec = val_usec;
2362 res = utimes (filename, times);
2363 if (res == -1)
2365 int errsv = errno;
2367 g_set_error (error, G_IO_ERROR,
2368 g_io_error_from_errno (errsv),
2369 _("Error setting modification or access time: %s"),
2370 g_strerror (errsv));
2371 return FALSE;
2373 return TRUE;
2375 #endif
2378 #ifdef HAVE_SELINUX
2379 static gboolean
2380 set_selinux_context (char *filename,
2381 const GFileAttributeValue *value,
2382 GError **error)
2384 const char *val;
2386 if (!get_string (value, &val, error))
2387 return FALSE;
2389 if (val == NULL)
2391 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2392 _("SELinux context must be non-NULL"));
2393 return FALSE;
2396 if (is_selinux_enabled ()) {
2397 security_context_t val_s;
2399 val_s = g_strdup (val);
2401 if (setfilecon_raw (filename, val_s) < 0)
2403 int errsv = errno;
2405 g_set_error (error, G_IO_ERROR,
2406 g_io_error_from_errno (errsv),
2407 _("Error setting SELinux context: %s"),
2408 g_strerror (errsv));
2409 return FALSE;
2411 g_free (val_s);
2412 } else {
2413 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2414 _("SELinux is not enabled on this system"));
2415 return FALSE;
2418 return TRUE;
2420 #endif
2423 gboolean
2424 _g_local_file_info_set_attribute (char *filename,
2425 const char *attribute,
2426 GFileAttributeType type,
2427 gpointer value_p,
2428 GFileQueryInfoFlags flags,
2429 GCancellable *cancellable,
2430 GError **error)
2432 GFileAttributeValue value = { 0 };
2433 GVfsClass *class;
2434 GVfs *vfs;
2436 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2438 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2439 return set_unix_mode (filename, flags, &value, error);
2441 #ifdef G_OS_UNIX
2442 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2443 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2444 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2445 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2446 #endif
2448 #ifdef HAVE_SYMLINK
2449 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2450 return set_symlink (filename, &value, error);
2451 #endif
2453 #ifdef HAVE_UTIMES
2454 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2455 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2456 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2457 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2458 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2459 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2460 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2461 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2462 #endif
2464 #ifdef HAVE_XATTR
2465 else if (g_str_has_prefix (attribute, "xattr::"))
2466 return set_xattr (filename, attribute, &value, error);
2467 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2468 return set_xattr (filename, attribute, &value, error);
2469 #endif
2471 #ifdef HAVE_SELINUX
2472 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2473 return set_selinux_context (filename, &value, error);
2474 #endif
2476 vfs = g_vfs_get_default ();
2477 class = G_VFS_GET_CLASS (vfs);
2478 if (class->local_file_set_attributes)
2480 GFileInfo *info;
2482 info = g_file_info_new ();
2483 g_file_info_set_attribute (info,
2484 attribute,
2485 type,
2486 value_p);
2487 if (!class->local_file_set_attributes (vfs, filename,
2488 info,
2489 flags, cancellable,
2490 error))
2492 g_object_unref (info);
2493 return FALSE;
2496 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2498 g_object_unref (info);
2499 return TRUE;
2502 g_object_unref (info);
2505 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2506 _("Setting attribute %s not supported"), attribute);
2507 return FALSE;
2510 gboolean
2511 _g_local_file_info_set_attributes (char *filename,
2512 GFileInfo *info,
2513 GFileQueryInfoFlags flags,
2514 GCancellable *cancellable,
2515 GError **error)
2517 GFileAttributeValue *value;
2518 #ifdef G_OS_UNIX
2519 GFileAttributeValue *uid, *gid;
2520 #ifdef HAVE_UTIMES
2521 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2522 #endif
2523 GFileAttributeStatus status;
2524 #endif
2525 gboolean res;
2526 GVfsClass *class;
2527 GVfs *vfs;
2529 /* Handles setting multiple specified data in a single set, and takes care
2530 of ordering restrictions when setting attributes */
2532 res = TRUE;
2534 /* Set symlink first, since this recreates the file */
2535 #ifdef HAVE_SYMLINK
2536 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2537 if (value)
2539 if (!set_symlink (filename, value, error))
2541 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2542 res = FALSE;
2543 /* Don't set error multiple times */
2544 error = NULL;
2546 else
2547 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2550 #endif
2552 #ifdef G_OS_UNIX
2553 /* Group uid and gid setting into one call
2554 * Change ownership before permissions, since ownership changes can
2555 change permissions (e.g. setuid)
2557 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2558 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2560 if (uid || gid)
2562 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2564 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2565 res = FALSE;
2566 /* Don't set error multiple times */
2567 error = NULL;
2569 else
2570 status = G_FILE_ATTRIBUTE_STATUS_SET;
2571 if (uid)
2572 uid->status = status;
2573 if (gid)
2574 gid->status = status;
2576 #endif
2578 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2579 if (value)
2581 if (!set_unix_mode (filename, flags, value, error))
2583 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2584 res = FALSE;
2585 /* Don't set error multiple times */
2586 error = NULL;
2588 else
2589 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2593 #ifdef HAVE_UTIMES
2594 /* Group all time settings into one call
2595 * Change times as the last thing to avoid it changing due to metadata changes
2598 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2599 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2600 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2601 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2603 if (mtime || mtime_usec || atime || atime_usec)
2605 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2607 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2608 res = FALSE;
2609 /* Don't set error multiple times */
2610 error = NULL;
2612 else
2613 status = G_FILE_ATTRIBUTE_STATUS_SET;
2615 if (mtime)
2616 mtime->status = status;
2617 if (mtime_usec)
2618 mtime_usec->status = status;
2619 if (atime)
2620 atime->status = status;
2621 if (atime_usec)
2622 atime_usec->status = status;
2624 #endif
2626 /* xattrs are handled by default callback */
2629 /* SELinux context */
2630 #ifdef HAVE_SELINUX
2631 if (is_selinux_enabled ()) {
2632 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2633 if (value)
2635 if (!set_selinux_context (filename, value, error))
2637 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2638 res = FALSE;
2639 /* Don't set error multiple times */
2640 error = NULL;
2642 else
2643 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2646 #endif
2648 vfs = g_vfs_get_default ();
2649 class = G_VFS_GET_CLASS (vfs);
2650 if (class->local_file_set_attributes)
2652 if (!class->local_file_set_attributes (vfs, filename,
2653 info,
2654 flags, cancellable,
2655 error))
2657 res = FALSE;
2658 /* Don't set error multiple times */
2659 error = NULL;
2663 return res;