1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
27 #ifdef HAVE_SYS_TIME_H
30 #include <sys/types.h>
45 #include <selinux/selinux.h>
50 #if defined HAVE_SYS_XATTR_H
51 #include <sys/xattr.h>
52 #elif defined HAVE_ATTR_XATTR_H
53 #include <attr/xattr.h>
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 */
58 #endif /* HAVE_XATTR */
60 #include <glib/gstdio.h>
61 #include <gfileattribute-priv.h>
62 #include <gfileinfo-priv.h>
66 #include "glib-unix.h"
67 #include "glib-private.h"
81 #define X_OK 0 /* not really */
84 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
87 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
90 #define S_IXUSR _S_IEXEC
94 #include "glocalfileinfo.h"
96 #include "gthemedicon.h"
97 #include "gcontenttypeprivate.h"
100 struct ThumbMD5Context {
103 unsigned char in[64];
113 G_LOCK_DEFINE_STATIC (uid_cache);
114 static GHashTable *uid_cache = NULL;
116 G_LOCK_DEFINE_STATIC (gid_cache);
117 static GHashTable *gid_cache = NULL;
119 #endif /* !G_OS_WIN32 */
122 _g_local_file_info_create_etag (GLocalFileStat *statbuf)
126 sec = statbuf->st_mtime;
127 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
128 usec = statbuf->st_mtimensec / 1000;
129 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
130 usec = statbuf->st_mtim.tv_nsec / 1000;
135 return g_strdup_printf ("%lu:%lu", sec, usec);
139 _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
141 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
142 (guint64) statbuf->st_dev,
143 (guint64) statbuf->st_ino);
147 _g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
149 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
150 (guint64) statbuf->st_dev);
157 read_link (const gchar *full_name)
164 buffer = g_malloc (size);
170 read_size = readlink (full_name, buffer, size);
176 if (read_size < size)
178 buffer[read_size] = 0;
182 buffer = g_realloc (buffer, size);
192 /* Get the SELinux security context */
194 get_selinux_context (const char *path,
196 GFileAttributeMatcher *attribute_matcher,
197 gboolean follow_symlinks)
201 if (!_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT))
204 if (is_selinux_enabled ())
208 if (lgetfilecon_raw (path, &context) < 0)
213 if (getfilecon_raw (path, &context) < 0)
219 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
228 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
229 * (Mac) getxattr(..., XATTR_NOFOLLOW)
231 #ifdef HAVE_XATTR_NOFOLLOW
232 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
233 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
234 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
236 #define g_fgetxattr fgetxattr
237 #define g_flistxattr flistxattr
238 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
242 g_getxattr (const char *path, const char *name, void *value, size_t size,
243 gboolean follow_symlinks)
245 #ifdef HAVE_XATTR_NOFOLLOW
246 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
249 return getxattr (path, name, value, size);
251 return lgetxattr (path, name, value, size);
256 g_listxattr(const char *path, char *namebuf, size_t size,
257 gboolean follow_symlinks)
259 #ifdef HAVE_XATTR_NOFOLLOW
260 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
263 return listxattr (path, namebuf, size);
265 return llistxattr (path, namebuf, size);
272 return c >= 32 && c <= 126 && c != '\\';
276 name_is_valid (const char *str)
280 if (!valid_char (*str++))
287 hex_escape_string (const char *str,
288 gboolean *free_return)
291 char *escaped_str, *p;
293 static char *hex_digits = "0123456789abcdef";
299 for (i = 0; i < len; i++)
301 if (!valid_char (str[i]))
305 if (num_invalid == 0)
307 *free_return = FALSE;
311 escaped_str = g_malloc (len + num_invalid*3 + 1);
314 for (i = 0; i < len; i++)
316 if (valid_char (str[i]))
323 *p++ = hex_digits[(c >> 4) & 0xf];
324 *p++ = hex_digits[c & 0xf];
334 hex_unescape_string (const char *str,
336 gboolean *free_return)
339 char *unescaped_str, *p;
345 if (strchr (str, '\\') == NULL)
349 *free_return = FALSE;
353 unescaped_str = g_malloc (len + 1);
356 for (i = 0; i < len; i++)
358 if (str[i] == '\\' &&
363 (g_ascii_xdigit_value (str[i+2]) << 4) |
364 g_ascii_xdigit_value (str[i+3]);
374 *out_len = p - unescaped_str;
376 return unescaped_str;
380 escape_xattr (GFileInfo *info,
381 const char *gio_attr, /* gio attribute name */
382 const char *value, /* Is zero terminated */
383 size_t len /* not including zero termination */)
386 gboolean free_escaped_val;
388 escaped_val = hex_escape_string (value, &free_escaped_val);
390 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
392 if (free_escaped_val)
393 g_free (escaped_val);
397 get_one_xattr (const char *path,
399 const char *gio_attr,
401 gboolean follow_symlinks)
407 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
412 else if (len == -1 && errno == ERANGE)
414 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
419 value_p = g_malloc (len+1);
421 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
435 escape_xattr (info, gio_attr, value_p, len);
437 if (value_p != value)
441 #endif /* defined HAVE_XATTR */
444 get_xattrs (const char *path,
447 GFileAttributeMatcher *matcher,
448 gboolean follow_symlinks)
453 ssize_t list_res_size;
456 const char *attr, *attr2;
459 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
461 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
465 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
467 if (list_res_size == -1 ||
471 list_size = list_res_size;
472 list = g_malloc (list_size);
476 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
478 if (list_res_size == -1 && errno == ERANGE)
480 list_size = list_size * 2;
481 list = g_realloc (list, list_size);
485 if (list_res_size == -1)
489 while (list_res_size > 0)
491 if ((user && g_str_has_prefix (attr, "user.")) ||
492 (!user && !g_str_has_prefix (attr, "user.")))
494 char *escaped_attr, *gio_attr;
495 gboolean free_escaped_attr;
499 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
500 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
504 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
505 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
508 if (free_escaped_attr)
509 g_free (escaped_attr);
511 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
516 len = strlen (attr) + 1;
518 list_res_size -= len;
525 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
527 char *unescaped_attribute, *a;
528 gboolean free_unescaped_attribute;
530 attr2 = strchr (attr, ':');
533 attr2 += 2; /* Skip '::' */
534 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
536 a = g_strconcat ("user.", unescaped_attribute, NULL);
538 a = unescaped_attribute;
540 get_one_xattr (path, info, attr, a, follow_symlinks);
545 if (free_unescaped_attribute)
546 g_free (unescaped_attribute);
550 #endif /* defined HAVE_XATTR */
555 get_one_xattr_from_fd (int fd,
557 const char *gio_attr,
564 len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
569 else if (len == -1 && errno == ERANGE)
571 len = g_fgetxattr (fd, xattr, NULL, 0);
576 value_p = g_malloc (len + 1);
578 len = g_fgetxattr (fd, xattr, value_p, len);
592 escape_xattr (info, gio_attr, value_p, len);
594 if (value_p != value)
597 #endif /* defined HAVE_XATTR */
600 get_xattrs_from_fd (int fd,
603 GFileAttributeMatcher *matcher)
608 ssize_t list_res_size;
611 const char *attr, *attr2;
614 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
616 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
620 list_res_size = g_flistxattr (fd, NULL, 0);
622 if (list_res_size == -1 ||
626 list_size = list_res_size;
627 list = g_malloc (list_size);
631 list_res_size = g_flistxattr (fd, list, list_size);
633 if (list_res_size == -1 && errno == ERANGE)
635 list_size = list_size * 2;
636 list = g_realloc (list, list_size);
640 if (list_res_size == -1)
644 while (list_res_size > 0)
646 if ((user && g_str_has_prefix (attr, "user.")) ||
647 (!user && !g_str_has_prefix (attr, "user.")))
649 char *escaped_attr, *gio_attr;
650 gboolean free_escaped_attr;
654 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
655 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
659 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
660 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
663 if (free_escaped_attr)
664 g_free (escaped_attr);
666 get_one_xattr_from_fd (fd, info, gio_attr, attr);
669 len = strlen (attr) + 1;
671 list_res_size -= len;
678 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
680 char *unescaped_attribute, *a;
681 gboolean free_unescaped_attribute;
683 attr2 = strchr (attr, ':');
686 attr2++; /* Skip ':' */
687 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
689 a = g_strconcat ("user.", unescaped_attribute, NULL);
691 a = unescaped_attribute;
693 get_one_xattr_from_fd (fd, info, attr, a);
698 if (free_unescaped_attribute)
699 g_free (unescaped_attribute);
703 #endif /* defined HAVE_XATTR */
708 set_xattr (char *filename,
709 const char *escaped_attribute,
710 const GFileAttributeValue *attr_value,
713 char *attribute, *value;
714 gboolean free_attribute, free_value;
715 int val_len, res, errsv;
719 if (attr_value == NULL)
721 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
722 _("Attribute value must be non-NULL"));
726 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
728 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
729 _("Invalid attribute type (string expected)"));
733 if (!name_is_valid (escaped_attribute))
735 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
736 _("Invalid extended attribute name"));
740 if (g_str_has_prefix (escaped_attribute, "xattr::"))
742 escaped_attribute += strlen ("xattr::");
747 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
748 escaped_attribute += strlen ("xattr-sys::");
752 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
753 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
756 a = g_strconcat ("user.", attribute, NULL);
760 res = g_setxattr (filename, a, value, val_len);
774 g_set_error (error, G_IO_ERROR,
775 g_io_error_from_errno (errsv),
776 _("Error setting extended attribute '%s': %s"),
777 escaped_attribute, g_strerror (errsv));
788 _g_local_file_info_get_parent_info (const char *dir,
789 GFileAttributeMatcher *attribute_matcher,
790 GLocalParentFileInfo *parent_info)
795 parent_info->extra_data = NULL;
796 parent_info->free_extra_data = NULL;
797 parent_info->writable = FALSE;
798 parent_info->is_sticky = FALSE;
799 parent_info->has_trash_dir = FALSE;
800 parent_info->device = 0;
802 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME) ||
803 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE) ||
804 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH) ||
805 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT))
807 /* FIXME: Windows: The underlying _waccess() call in the C
808 * library is mostly pointless as it only looks at the READONLY
809 * FAT-style attribute of the file, it doesn't check the ACL at
812 parent_info->writable = (g_access (dir, W_OK) == 0);
814 res = g_stat (dir, &statbuf);
817 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
818 * renamed or deleted only by the owner of the file, by the owner of the directory, and
819 * by a privileged process.
824 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
826 parent_info->is_sticky = FALSE;
828 parent_info->owner = statbuf.st_uid;
829 parent_info->device = statbuf.st_dev;
830 /* No need to find trash dir if it's not writable anyway */
831 if (parent_info->writable &&
832 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
833 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
839 _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info)
841 if (parent_info->extra_data &&
842 parent_info->free_extra_data)
843 parent_info->free_extra_data (parent_info->extra_data);
847 get_access_rights (GFileAttributeMatcher *attribute_matcher,
850 GLocalFileStat *statbuf,
851 GLocalParentFileInfo *parent_info)
853 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
854 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
855 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ))
856 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ,
857 g_access (path, R_OK) == 0);
859 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
860 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE))
861 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE,
862 g_access (path, W_OK) == 0);
864 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
865 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE))
866 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE,
867 g_access (path, X_OK) == 0);
875 if (parent_info->writable)
877 if (parent_info->is_sticky)
880 uid_t uid = geteuid ();
882 if (uid == statbuf->st_uid ||
883 uid == parent_info->owner ||
892 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME))
893 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME,
896 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE))
897 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE,
900 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
901 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH,
902 writable && parent_info->has_trash_dir);
907 set_info_from_stat (GFileInfo *info,
908 GLocalFileStat *statbuf,
909 GFileAttributeMatcher *attribute_matcher)
913 file_type = G_FILE_TYPE_UNKNOWN;
915 if (S_ISREG (statbuf->st_mode))
916 file_type = G_FILE_TYPE_REGULAR;
917 else if (S_ISDIR (statbuf->st_mode))
918 file_type = G_FILE_TYPE_DIRECTORY;
920 else if (S_ISCHR (statbuf->st_mode) ||
921 S_ISBLK (statbuf->st_mode) ||
922 S_ISFIFO (statbuf->st_mode)
924 || S_ISSOCK (statbuf->st_mode)
927 file_type = G_FILE_TYPE_SPECIAL;
930 else if (S_ISLNK (statbuf->st_mode))
931 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
934 g_file_info_set_file_type (info, file_type);
935 g_file_info_set_size (info, statbuf->st_size);
937 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
939 /* Pointless setting these on Windows even if they exist in the struct */
940 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
941 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
942 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
943 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
944 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
946 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
947 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
948 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
949 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
951 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
952 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
953 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
954 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
957 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
958 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
959 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
960 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
961 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
964 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
965 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
966 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
967 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
968 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
971 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
972 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
973 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
974 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
975 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
978 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
979 G_FILE_ATTRIBUTE_ID_ETAG_VALUE))
981 char *etag = _g_local_file_info_create_etag (statbuf);
982 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ETAG_VALUE, etag);
986 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
987 G_FILE_ATTRIBUTE_ID_ID_FILE))
989 char *id = _g_local_file_info_create_file_id (statbuf);
990 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILE, id);
994 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
995 G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM))
997 char *id = _g_local_file_info_create_fs_id (statbuf);
998 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM, id);
1006 make_valid_utf8 (const char *name)
1009 const gchar *remainder, *invalid;
1010 gint remaining_bytes, valid_bytes;
1014 remaining_bytes = strlen (name);
1016 while (remaining_bytes != 0)
1018 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1020 valid_bytes = invalid - remainder;
1023 string = g_string_sized_new (remaining_bytes);
1025 g_string_append_len (string, remainder, valid_bytes);
1026 /* append U+FFFD REPLACEMENT CHARACTER */
1027 g_string_append (string, "\357\277\275");
1029 remaining_bytes -= valid_bytes + 1;
1030 remainder = invalid + 1;
1034 return g_strdup (name);
1036 g_string_append (string, remainder);
1038 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1040 return g_string_free (string, FALSE);
1044 convert_pwd_string_to_utf8 (char *pwd_str)
1048 if (!g_utf8_validate (pwd_str, -1, NULL))
1050 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1051 if (utf8_string == NULL)
1052 utf8_string = make_valid_utf8 (pwd_str);
1055 utf8_string = g_strdup (pwd_str);
1061 uid_data_free (UidData *data)
1063 g_free (data->user_name);
1064 g_free (data->real_name);
1068 /* called with lock held */
1070 lookup_uid_data (uid_t uid)
1074 struct passwd pwbuf;
1075 struct passwd *pwbufp;
1076 char *gecos, *comma;
1078 if (uid_cache == NULL)
1079 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1081 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1086 data = g_new0 (UidData, 1);
1088 #if defined(HAVE_POSIX_GETPWUID_R)
1089 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1090 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1091 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1093 pwbufp = getpwuid (uid);
1098 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1099 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1101 gecos = pwbufp->pw_gecos;
1105 comma = strchr (gecos, ',');
1108 data->real_name = convert_pwd_string_to_utf8 (gecos);
1112 /* Default fallbacks */
1113 if (data->real_name == NULL)
1115 if (data->user_name != NULL)
1116 data->real_name = g_strdup (data->user_name);
1118 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1121 if (data->user_name == NULL)
1122 data->user_name = g_strdup_printf ("%d", (int)uid);
1124 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1130 get_username_from_uid (uid_t uid)
1136 data = lookup_uid_data (uid);
1137 res = g_strdup (data->user_name);
1138 G_UNLOCK (uid_cache);
1144 get_realname_from_uid (uid_t uid)
1150 data = lookup_uid_data (uid);
1151 res = g_strdup (data->real_name);
1152 G_UNLOCK (uid_cache);
1157 /* called with lock held */
1159 lookup_gid_name (gid_t gid)
1164 struct group *gbufp;
1166 if (gid_cache == NULL)
1167 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1169 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1174 #if defined (HAVE_POSIX_GETGRGID_R)
1175 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1176 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1177 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1179 gbufp = getgrgid (gid);
1182 if (gbufp != NULL &&
1183 gbufp->gr_name != NULL &&
1184 gbufp->gr_name[0] != 0)
1185 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1187 name = g_strdup_printf("%d", (int)gid);
1189 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1195 get_groupname_from_gid (gid_t gid)
1201 name = lookup_gid_name (gid);
1202 res = g_strdup (name);
1203 G_UNLOCK (gid_cache);
1207 #endif /* !G_OS_WIN32 */
1210 get_content_type (const char *basename,
1212 GLocalFileStat *statbuf,
1213 gboolean is_symlink,
1214 gboolean symlink_broken,
1215 GFileQueryInfoFlags flags,
1219 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1220 return g_strdup ("inode/symlink");
1221 else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
1222 return g_strdup ("inode/directory");
1224 else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
1225 return g_strdup ("inode/chardevice");
1226 else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
1227 return g_strdup ("inode/blockdevice");
1228 else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
1229 return g_strdup ("inode/fifo");
1232 else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
1233 return g_strdup ("inode/socket");
1238 gboolean result_uncertain;
1240 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1243 if (!fast && result_uncertain && path != NULL)
1245 guchar sniff_buffer[4096];
1249 sniff_length = _g_unix_content_type_get_sniff_len ();
1250 if (sniff_length > 4096)
1251 sniff_length = 4096;
1254 fd = g_open (path, O_RDONLY | O_NOATIME, 0);
1255 if (fd < 0 && errno == EPERM)
1257 fd = g_open (path, O_RDONLY, 0);
1263 res = read (fd, sniff_buffer, sniff_length);
1264 (void) g_close (fd, NULL);
1267 g_free (content_type);
1268 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1274 return content_type;
1280 get_thumbnail_attributes (const char *path,
1283 GChecksum *checksum;
1288 uri = g_filename_to_uri (path, NULL, NULL);
1290 checksum = g_checksum_new (G_CHECKSUM_MD5);
1291 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1295 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1296 g_checksum_free (checksum);
1298 filename = g_build_filename (g_get_user_cache_dir (),
1299 "thumbnails", "large", basename,
1302 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1303 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1307 filename = g_build_filename (g_get_user_cache_dir (),
1308 "thumbnails", "normal", basename,
1311 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1312 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1316 filename = g_build_filename (g_get_user_cache_dir (),
1317 "thumbnails", "fail",
1318 "gnome-thumbnail-factory",
1322 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1323 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1332 win32_get_file_user_info (const gchar *filename,
1337 PSECURITY_DESCRIPTOR psd = NULL;
1338 DWORD sd_size = 0; /* first call calculates the size required */
1340 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1341 if ((GetFileSecurityW (wfilename,
1342 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1345 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1346 (psd = g_try_malloc (sd_size)) != NULL &&
1347 GetFileSecurityW (wfilename,
1348 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1355 SID_NAME_USE name_use = 0; /* don't care? */
1356 wchar_t *name = NULL;
1357 wchar_t *domain = NULL;
1359 DWORD domain_len = 0;
1360 /* get the user name */
1364 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1366 if (!LookupAccountSidW (NULL, /* local machine */
1369 domain, &domain_len, /* no domain info yet */
1370 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1372 name = g_try_malloc (name_len * sizeof (wchar_t));
1373 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1374 if (name && domain &&
1375 LookupAccountSidW (NULL, /* local machine */
1378 domain, &domain_len, /* no domain info yet */
1381 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1387 /* get the group name */
1391 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1393 if (!LookupAccountSidW (NULL, /* local machine */
1396 domain, &domain_len, /* no domain info yet */
1397 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1399 name = g_try_malloc (name_len * sizeof (wchar_t));
1400 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1401 if (name && domain &&
1402 LookupAccountSidW (NULL, /* local machine */
1405 domain, &domain_len, /* no domain info yet */
1408 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1414 /* TODO: get real name */
1420 #endif /* G_OS_WIN32 */
1423 /* support for '.hidden' files */
1424 G_LOCK_DEFINE_STATIC (hidden_cache);
1425 static GHashTable *hidden_cache;
1428 remove_from_hidden_cache (gpointer user_data)
1430 G_LOCK (hidden_cache);
1431 g_hash_table_remove (hidden_cache, user_data);
1432 G_UNLOCK (hidden_cache);
1438 read_hidden_file (const gchar *dirname)
1443 filename = g_build_path ("/", dirname, ".hidden", NULL);
1444 hidden = fopen (filename, "r");
1449 gchar buffer[PATH_MAX + 2]; /* \n\0 */
1452 table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1454 while (fgets (buffer, sizeof buffer, hidden))
1458 if ((newline = strchr (buffer, '\n')) != NULL)
1462 g_hash_table_insert (table,
1463 g_memdup (buffer, newline - buffer),
1464 GINT_TO_POINTER (TRUE));
1477 maybe_unref_hash_table (gpointer data)
1480 g_hash_table_unref (data);
1484 file_is_hidden (const gchar *path,
1485 const gchar *basename)
1491 dirname = g_path_get_dirname (path);
1493 G_LOCK (hidden_cache);
1495 if G_UNLIKELY (hidden_cache == NULL)
1496 hidden_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1497 g_free, maybe_unref_hash_table);
1499 if (!g_hash_table_lookup_extended (hidden_cache, dirname,
1503 GSource *remove_from_cache_source;
1505 g_hash_table_insert (hidden_cache,
1506 mydirname = g_strdup (dirname),
1507 table = read_hidden_file (dirname));
1509 remove_from_cache_source = g_timeout_source_new_seconds (5);
1510 g_source_set_priority (remove_from_cache_source, G_PRIORITY_DEFAULT);
1511 g_source_set_callback (remove_from_cache_source,
1512 remove_from_hidden_cache,
1515 g_source_attach (remove_from_cache_source,
1516 GLIB_PRIVATE_CALL (g_get_worker_context) ());
1517 g_source_unref (remove_from_cache_source);
1520 result = table != NULL &&
1521 GPOINTER_TO_INT (g_hash_table_lookup (table, basename));
1523 G_UNLOCK (hidden_cache);
1529 #endif /* !G_OS_WIN32 */
1532 _g_local_file_info_get_nostat (GFileInfo *info,
1533 const char *basename,
1535 GFileAttributeMatcher *attribute_matcher)
1537 g_file_info_set_name (info, basename);
1539 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1540 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1542 char *display_name = g_filename_display_basename (path);
1544 /* look for U+FFFD REPLACEMENT CHARACTER */
1545 if (strstr (display_name, "\357\277\275") != NULL)
1547 char *p = display_name;
1548 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1551 g_file_info_set_display_name (info, display_name);
1552 g_free (display_name);
1555 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1556 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1558 char *edit_name = g_filename_display_basename (path);
1559 g_file_info_set_edit_name (info, edit_name);
1564 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1565 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1567 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1569 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1575 get_icon_name (const char *path,
1576 gboolean use_symbolic,
1577 gboolean *with_fallbacks_out)
1579 const char *name = NULL;
1580 gboolean with_fallbacks = TRUE;
1582 if (strcmp (path, g_get_home_dir ()) == 0)
1584 name = use_symbolic ? "user-home-symbolic" : "user-home";
1585 with_fallbacks = FALSE;
1587 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1589 name = use_symbolic ? "user-desktop-symbolic" : "user-desktop";
1590 with_fallbacks = FALSE;
1592 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1594 name = use_symbolic ? "folder-documents-symbolic" : "folder-documents";
1596 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1598 name = use_symbolic ? "folder-download-symbolic" : "folder-download";
1600 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1602 name = use_symbolic ? "folder-music-symbolic" : "folder-music";
1604 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1606 name = use_symbolic ? "folder-pictures-symbolic" : "folder-pictures";
1608 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1610 name = use_symbolic ? "folder-publicshare-symbolic" : "folder-publicshare";
1612 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1614 name = use_symbolic ? "folder-templates-symbolic" : "folder-templates";
1616 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1618 name = use_symbolic ? "folder-videos-symbolic" : "folder-videos";
1625 if (with_fallbacks_out != NULL)
1626 *with_fallbacks_out = with_fallbacks;
1632 get_icon (const char *path,
1633 const char *content_type,
1635 gboolean use_symbolic)
1638 const char *icon_name;
1639 gboolean with_fallbacks;
1641 icon_name = get_icon_name (path, use_symbolic, &with_fallbacks);
1642 if (icon_name != NULL)
1645 icon = g_themed_icon_new_with_default_fallbacks (icon_name);
1647 icon = g_themed_icon_new (icon_name);
1652 icon = g_content_type_get_symbolic_icon (content_type);
1654 icon = g_content_type_get_icon (content_type);
1656 if (G_IS_THEMED_ICON (icon) && is_folder)
1658 g_themed_icon_append_name (G_THEMED_ICON (icon), use_symbolic ? "folder-symbolic" : "folder");
1666 _g_local_file_info_get (const char *basename,
1668 GFileAttributeMatcher *attribute_matcher,
1669 GFileQueryInfoFlags flags,
1670 GLocalParentFileInfo *parent_info,
1674 GLocalFileStat statbuf;
1676 struct stat statbuf2;
1680 gboolean is_symlink, symlink_broken;
1682 DWORD dos_attributes;
1684 char *symlink_target;
1689 info = g_file_info_new ();
1691 /* Make sure we don't set any unwanted attributes */
1692 g_file_info_set_attribute_mask (info, attribute_matcher);
1694 _g_local_file_info_get_nostat (info, basename, path, attribute_matcher);
1696 if (attribute_matcher == NULL)
1698 g_file_info_unset_attribute_mask (info);
1703 res = g_lstat (path, &statbuf);
1706 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1711 g_object_unref (info);
1715 len = wcslen (wpath);
1716 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1719 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1722 res = _wstati64 (wpath, &statbuf);
1723 dos_attributes = GetFileAttributesW (wpath);
1733 /* Don't bail out if we get Permission denied (SELinux?) */
1734 if (errsv != EACCES)
1736 char *display_name = g_filename_display_name (path);
1737 g_object_unref (info);
1738 g_set_error (error, G_IO_ERROR,
1739 g_io_error_from_errno (errsv),
1740 _("Error when getting information for file '%s': %s"),
1741 display_name, g_strerror (errsv));
1742 g_free (display_name);
1747 /* Even if stat() fails, try to get as much as other attributes possible */
1748 stat_ok = res != -1;
1751 device = statbuf.st_dev;
1756 is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
1760 symlink_broken = FALSE;
1764 g_file_info_set_is_symlink (info, TRUE);
1766 /* Unless NOFOLLOW was set we default to following symlinks */
1767 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1769 res = stat (path, &statbuf2);
1771 /* Report broken links as symlinks */
1778 symlink_broken = TRUE;
1784 set_info_from_stat (info, &statbuf, attribute_matcher);
1787 if (stat_ok && _g_local_file_is_lost_found_dir (path, statbuf.st_dev))
1788 g_file_info_set_is_hidden (info, TRUE);
1792 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1793 G_FILE_ATTRIBUTE_ID_STANDARD_IS_HIDDEN))
1795 if (basename != NULL &&
1796 (basename[0] == '.' ||
1797 file_is_hidden (path, basename)))
1798 g_file_info_set_is_hidden (info, TRUE);
1801 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1802 (stat_ok && S_ISREG (statbuf.st_mode)))
1803 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1805 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1806 g_file_info_set_is_hidden (info, TRUE);
1808 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1809 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1811 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1812 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1815 symlink_target = NULL;
1819 symlink_target = read_link (path);
1820 if (symlink_target &&
1821 _g_file_attribute_matcher_matches_id (attribute_matcher,
1822 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1823 g_file_info_set_symlink_target (info, symlink_target);
1826 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1827 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1828 _g_file_attribute_matcher_matches_id (attribute_matcher,
1829 G_FILE_ATTRIBUTE_ID_STANDARD_ICON) ||
1830 _g_file_attribute_matcher_matches_id (attribute_matcher,
1831 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1833 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
1837 g_file_info_set_content_type (info, content_type);
1839 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1840 G_FILE_ATTRIBUTE_ID_STANDARD_ICON)
1841 || _g_file_attribute_matcher_matches_id (attribute_matcher,
1842 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1846 /* non symbolic icon */
1847 icon = get_icon (path, content_type, S_ISDIR (statbuf.st_mode), FALSE);
1850 g_file_info_set_icon (info, icon);
1851 g_object_unref (icon);
1855 icon = get_icon (path, content_type, S_ISDIR (statbuf.st_mode), TRUE);
1858 g_file_info_set_symbolic_icon (info, icon);
1859 g_object_unref (icon);
1864 g_free (content_type);
1868 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1869 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1871 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
1875 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1876 g_free (content_type);
1880 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1881 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1886 win32_get_file_user_info (path, NULL, &name, NULL);
1889 name = get_username_from_uid (statbuf.st_uid);
1892 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1896 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1897 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1901 win32_get_file_user_info (path, NULL, NULL, &name);
1904 name = get_realname_from_uid (statbuf.st_uid);
1907 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1911 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1912 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1916 win32_get_file_user_info (path, &name, NULL, NULL);
1919 name = get_groupname_from_gid (statbuf.st_gid);
1922 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1926 if (stat_ok && parent_info && parent_info->device != 0 &&
1927 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1928 statbuf.st_dev != parent_info->device)
1929 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1932 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1935 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1937 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1938 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1940 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1941 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH))
1942 get_thumbnail_attributes (path, info);
1944 vfs = g_vfs_get_default ();
1945 class = G_VFS_GET_CLASS (vfs);
1946 if (class->local_file_add_info)
1948 class->local_file_add_info (vfs,
1954 &parent_info->extra_data,
1955 &parent_info->free_extra_data);
1958 g_file_info_unset_attribute_mask (info);
1960 g_free (symlink_target);
1966 _g_local_file_info_get_from_fd (int fd,
1967 const char *attributes,
1970 GLocalFileStat stat_buf;
1971 GFileAttributeMatcher *matcher;
1975 #define FSTAT _fstati64
1980 if (FSTAT (fd, &stat_buf) == -1)
1984 g_set_error (error, G_IO_ERROR,
1985 g_io_error_from_errno (errsv),
1986 _("Error when getting information for file descriptor: %s"),
1987 g_strerror (errsv));
1991 info = g_file_info_new ();
1993 matcher = g_file_attribute_matcher_new (attributes);
1995 /* Make sure we don't set any unwanted attributes */
1996 g_file_info_set_attribute_mask (info, matcher);
1998 set_info_from_stat (info, &stat_buf, matcher);
2001 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
2002 is_selinux_enabled ())
2005 if (fgetfilecon_raw (fd, &context) >= 0)
2007 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
2013 get_xattrs_from_fd (fd, TRUE, info, matcher);
2014 get_xattrs_from_fd (fd, FALSE, info, matcher);
2016 g_file_attribute_matcher_unref (matcher);
2018 g_file_info_unset_attribute_mask (info);
2024 get_uint32 (const GFileAttributeValue *value,
2028 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
2030 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2031 _("Invalid attribute type (uint32 expected)"));
2035 *val_out = value->u.uint32;
2042 get_uint64 (const GFileAttributeValue *value,
2046 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
2048 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2049 _("Invalid attribute type (uint64 expected)"));
2053 *val_out = value->u.uint64;
2059 #if defined(HAVE_SYMLINK)
2061 get_byte_string (const GFileAttributeValue *value,
2062 const char **val_out,
2065 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
2067 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2068 _("Invalid attribute type (byte string expected)"));
2072 *val_out = value->u.string;
2080 get_string (const GFileAttributeValue *value,
2081 const char **val_out,
2084 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
2086 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2087 _("Invalid attribute type (byte string expected)"));
2091 *val_out = value->u.string;
2098 set_unix_mode (char *filename,
2099 GFileQueryInfoFlags flags,
2100 const GFileAttributeValue *value,
2106 if (!get_uint32 (value, &val, error))
2110 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
2112 res = lchmod (filename, val);
2114 struct stat statbuf;
2115 /* Calling chmod on a symlink changes permissions on the symlink.
2116 * We don't want to do this, so we need to check for a symlink */
2117 res = g_lstat (filename, &statbuf);
2118 if (res == 0 && S_ISLNK (statbuf.st_mode))
2120 g_set_error_literal (error, G_IO_ERROR,
2121 G_IO_ERROR_NOT_SUPPORTED,
2122 _("Cannot set permissions on symlinks"));
2126 res = g_chmod (filename, val);
2130 res = g_chmod (filename, val);
2136 g_set_error (error, G_IO_ERROR,
2137 g_io_error_from_errno (errsv),
2138 _("Error setting permissions: %s"),
2139 g_strerror (errsv));
2147 set_unix_uid_gid (char *filename,
2148 const GFileAttributeValue *uid_value,
2149 const GFileAttributeValue *gid_value,
2150 GFileQueryInfoFlags flags,
2160 if (!get_uint32 (uid_value, &val, error))
2169 if (!get_uint32 (gid_value, &val, error))
2177 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
2178 res = lchown (filename, uid, gid);
2181 res = chown (filename, uid, gid);
2187 g_set_error (error, G_IO_ERROR,
2188 g_io_error_from_errno (errsv),
2189 _("Error setting owner: %s"),
2190 g_strerror (errsv));
2199 set_symlink (char *filename,
2200 const GFileAttributeValue *value,
2204 struct stat statbuf;
2206 if (!get_byte_string (value, &val, error))
2211 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2212 _("symlink must be non-NULL"));
2216 if (g_lstat (filename, &statbuf))
2220 g_set_error (error, G_IO_ERROR,
2221 g_io_error_from_errno (errsv),
2222 _("Error setting symlink: %s"),
2223 g_strerror (errsv));
2227 if (!S_ISLNK (statbuf.st_mode))
2229 g_set_error_literal (error, G_IO_ERROR,
2230 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2231 _("Error setting symlink: file is not a symlink"));
2235 if (g_unlink (filename))
2239 g_set_error (error, G_IO_ERROR,
2240 g_io_error_from_errno (errsv),
2241 _("Error setting symlink: %s"),
2242 g_strerror (errsv));
2246 if (symlink (filename, val) != 0)
2250 g_set_error (error, G_IO_ERROR,
2251 g_io_error_from_errno (errsv),
2252 _("Error setting symlink: %s"),
2253 g_strerror (errsv));
2263 lazy_stat (char *filename,
2264 struct stat *statbuf,
2265 gboolean *called_stat)
2272 res = g_stat (filename, statbuf);
2275 *called_stat = TRUE;
2282 set_mtime_atime (char *filename,
2283 const GFileAttributeValue *mtime_value,
2284 const GFileAttributeValue *mtime_usec_value,
2285 const GFileAttributeValue *atime_value,
2286 const GFileAttributeValue *atime_usec_value,
2291 guint32 val_usec = 0;
2292 struct stat statbuf;
2293 gboolean got_stat = FALSE;
2294 struct timeval times[2] = { {0, 0}, {0, 0} };
2299 if (!get_uint64 (atime_value, &val, error))
2301 times[0].tv_sec = val;
2305 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2307 times[0].tv_sec = statbuf.st_mtime;
2308 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2309 times[0].tv_usec = statbuf.st_atimensec / 1000;
2310 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2311 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2316 if (atime_usec_value)
2318 if (!get_uint32 (atime_usec_value, &val_usec, error))
2320 times[0].tv_usec = val_usec;
2326 if (!get_uint64 (mtime_value, &val, error))
2328 times[1].tv_sec = val;
2332 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2334 times[1].tv_sec = statbuf.st_mtime;
2335 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2336 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2337 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2338 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2343 if (mtime_usec_value)
2345 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2347 times[1].tv_usec = val_usec;
2350 res = utimes (filename, times);
2355 g_set_error (error, G_IO_ERROR,
2356 g_io_error_from_errno (errsv),
2357 _("Error setting modification or access time: %s"),
2358 g_strerror (errsv));
2368 set_selinux_context (char *filename,
2369 const GFileAttributeValue *value,
2374 if (!get_string (value, &val, error))
2379 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2380 _("SELinux context must be non-NULL"));
2384 if (is_selinux_enabled ()) {
2385 security_context_t val_s;
2387 val_s = g_strdup (val);
2389 if (setfilecon_raw (filename, val_s) < 0)
2393 g_set_error (error, G_IO_ERROR,
2394 g_io_error_from_errno (errsv),
2395 _("Error setting SELinux context: %s"),
2396 g_strerror (errsv));
2401 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2402 _("SELinux is not enabled on this system"));
2412 _g_local_file_info_set_attribute (char *filename,
2413 const char *attribute,
2414 GFileAttributeType type,
2416 GFileQueryInfoFlags flags,
2417 GCancellable *cancellable,
2420 GFileAttributeValue value = { 0 };
2424 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2426 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2427 return set_unix_mode (filename, flags, &value, error);
2430 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2431 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2432 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2433 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2437 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2438 return set_symlink (filename, &value, error);
2442 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2443 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2444 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2445 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2446 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2447 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2448 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2449 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2453 else if (g_str_has_prefix (attribute, "xattr::"))
2454 return set_xattr (filename, attribute, &value, error);
2455 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2456 return set_xattr (filename, attribute, &value, error);
2460 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2461 return set_selinux_context (filename, &value, error);
2464 vfs = g_vfs_get_default ();
2465 class = G_VFS_GET_CLASS (vfs);
2466 if (class->local_file_set_attributes)
2470 info = g_file_info_new ();
2471 g_file_info_set_attribute (info,
2475 if (!class->local_file_set_attributes (vfs, filename,
2480 g_object_unref (info);
2484 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2486 g_object_unref (info);
2490 g_object_unref (info);
2493 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2494 _("Setting attribute %s not supported"), attribute);
2499 _g_local_file_info_set_attributes (char *filename,
2501 GFileQueryInfoFlags flags,
2502 GCancellable *cancellable,
2505 GFileAttributeValue *value;
2507 GFileAttributeValue *uid, *gid;
2510 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2512 #if defined (HAVE_CHOWN) || defined (HAVE_UTIMES)
2513 GFileAttributeStatus status;
2519 /* Handles setting multiple specified data in a single set, and takes care
2520 of ordering restrictions when setting attributes */
2524 /* Set symlink first, since this recreates the file */
2526 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2529 if (!set_symlink (filename, value, error))
2531 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2533 /* Don't set error multiple times */
2537 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2543 /* Group uid and gid setting into one call
2544 * Change ownership before permissions, since ownership changes can
2545 change permissions (e.g. setuid)
2547 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2548 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2552 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2554 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2556 /* Don't set error multiple times */
2560 status = G_FILE_ATTRIBUTE_STATUS_SET;
2562 uid->status = status;
2564 gid->status = status;
2568 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2571 if (!set_unix_mode (filename, flags, value, error))
2573 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2575 /* Don't set error multiple times */
2579 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2584 /* Group all time settings into one call
2585 * Change times as the last thing to avoid it changing due to metadata changes
2588 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2589 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2590 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2591 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2593 if (mtime || mtime_usec || atime || atime_usec)
2595 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2597 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2599 /* Don't set error multiple times */
2603 status = G_FILE_ATTRIBUTE_STATUS_SET;
2606 mtime->status = status;
2608 mtime_usec->status = status;
2610 atime->status = status;
2612 atime_usec->status = status;
2616 /* xattrs are handled by default callback */
2619 /* SELinux context */
2621 if (is_selinux_enabled ()) {
2622 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2625 if (!set_selinux_context (filename, value, error))
2627 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2629 /* Don't set error multiple times */
2633 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2638 vfs = g_vfs_get_default ();
2639 class = G_VFS_GET_CLASS (vfs);
2640 if (class->local_file_set_attributes)
2642 if (!class->local_file_set_attributes (vfs, filename,
2648 /* Don't set error multiple times */