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);
1102 gecos = pwbufp->pw_gecos;
1106 comma = strchr (gecos, ',');
1109 data->real_name = convert_pwd_string_to_utf8 (gecos);
1114 /* Default fallbacks */
1115 if (data->real_name == NULL)
1117 if (data->user_name != NULL)
1118 data->real_name = g_strdup (data->user_name);
1120 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1123 if (data->user_name == NULL)
1124 data->user_name = g_strdup_printf ("%d", (int)uid);
1126 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1132 get_username_from_uid (uid_t uid)
1138 data = lookup_uid_data (uid);
1139 res = g_strdup (data->user_name);
1140 G_UNLOCK (uid_cache);
1146 get_realname_from_uid (uid_t uid)
1152 data = lookup_uid_data (uid);
1153 res = g_strdup (data->real_name);
1154 G_UNLOCK (uid_cache);
1159 /* called with lock held */
1161 lookup_gid_name (gid_t gid)
1166 struct group *gbufp;
1168 if (gid_cache == NULL)
1169 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1171 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1176 #if defined (HAVE_POSIX_GETGRGID_R)
1177 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1178 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1179 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1181 gbufp = getgrgid (gid);
1184 if (gbufp != NULL &&
1185 gbufp->gr_name != NULL &&
1186 gbufp->gr_name[0] != 0)
1187 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1189 name = g_strdup_printf("%d", (int)gid);
1191 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1197 get_groupname_from_gid (gid_t gid)
1203 name = lookup_gid_name (gid);
1204 res = g_strdup (name);
1205 G_UNLOCK (gid_cache);
1209 #endif /* !G_OS_WIN32 */
1212 get_content_type (const char *basename,
1214 GLocalFileStat *statbuf,
1215 gboolean is_symlink,
1216 gboolean symlink_broken,
1217 GFileQueryInfoFlags flags,
1221 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1222 return g_strdup ("inode/symlink");
1223 else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
1224 return g_strdup ("inode/directory");
1226 else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
1227 return g_strdup ("inode/chardevice");
1228 else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
1229 return g_strdup ("inode/blockdevice");
1230 else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
1231 return g_strdup ("inode/fifo");
1234 else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
1235 return g_strdup ("inode/socket");
1240 gboolean result_uncertain;
1242 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1245 if (!fast && result_uncertain && path != NULL)
1247 guchar sniff_buffer[4096];
1251 sniff_length = _g_unix_content_type_get_sniff_len ();
1252 if (sniff_length > 4096)
1253 sniff_length = 4096;
1256 fd = g_open (path, O_RDONLY | O_NOATIME, 0);
1257 if (fd < 0 && errno == EPERM)
1259 fd = g_open (path, O_RDONLY, 0);
1265 res = read (fd, sniff_buffer, sniff_length);
1266 (void) g_close (fd, NULL);
1269 g_free (content_type);
1270 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1276 return content_type;
1282 get_thumbnail_attributes (const char *path,
1285 GChecksum *checksum;
1290 uri = g_filename_to_uri (path, NULL, NULL);
1292 checksum = g_checksum_new (G_CHECKSUM_MD5);
1293 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1297 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1298 g_checksum_free (checksum);
1300 filename = g_build_filename (g_get_user_cache_dir (),
1301 "thumbnails", "large", basename,
1304 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);
1309 filename = g_build_filename (g_get_user_cache_dir (),
1310 "thumbnails", "normal", basename,
1313 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1314 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1318 filename = g_build_filename (g_get_user_cache_dir (),
1319 "thumbnails", "fail",
1320 "gnome-thumbnail-factory",
1324 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1325 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1334 win32_get_file_user_info (const gchar *filename,
1339 PSECURITY_DESCRIPTOR psd = NULL;
1340 DWORD sd_size = 0; /* first call calculates the size required */
1342 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1343 if ((GetFileSecurityW (wfilename,
1344 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1347 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1348 (psd = g_try_malloc (sd_size)) != NULL &&
1349 GetFileSecurityW (wfilename,
1350 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1357 SID_NAME_USE name_use = 0; /* don't care? */
1358 wchar_t *name = NULL;
1359 wchar_t *domain = NULL;
1361 DWORD domain_len = 0;
1362 /* get the user name */
1366 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1368 if (!LookupAccountSidW (NULL, /* local machine */
1371 domain, &domain_len, /* no domain info yet */
1372 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1374 name = g_try_malloc (name_len * sizeof (wchar_t));
1375 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1376 if (name && domain &&
1377 LookupAccountSidW (NULL, /* local machine */
1380 domain, &domain_len, /* no domain info yet */
1383 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1389 /* get the group name */
1393 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1395 if (!LookupAccountSidW (NULL, /* local machine */
1398 domain, &domain_len, /* no domain info yet */
1399 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1401 name = g_try_malloc (name_len * sizeof (wchar_t));
1402 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1403 if (name && domain &&
1404 LookupAccountSidW (NULL, /* local machine */
1407 domain, &domain_len, /* no domain info yet */
1410 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1416 /* TODO: get real name */
1422 #endif /* G_OS_WIN32 */
1425 /* support for '.hidden' files */
1426 G_LOCK_DEFINE_STATIC (hidden_cache);
1427 static GHashTable *hidden_cache;
1430 remove_from_hidden_cache (gpointer user_data)
1432 G_LOCK (hidden_cache);
1433 g_hash_table_remove (hidden_cache, user_data);
1434 G_UNLOCK (hidden_cache);
1440 read_hidden_file (const gchar *dirname)
1442 gchar *contents = NULL;
1445 filename = g_build_path ("/", dirname, ".hidden", NULL);
1446 g_file_get_contents (filename, &contents, NULL, NULL);
1449 if (contents != NULL)
1455 table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1457 lines = g_strsplit (contents, "\n", 0);
1460 for (i = 0; lines[i]; i++)
1461 /* hash table takes the individual strings... */
1462 g_hash_table_add (table, lines[i]);
1464 /* ... so we only free the container. */
1474 maybe_unref_hash_table (gpointer data)
1477 g_hash_table_unref (data);
1481 file_is_hidden (const gchar *path,
1482 const gchar *basename)
1488 dirname = g_path_get_dirname (path);
1490 G_LOCK (hidden_cache);
1492 if G_UNLIKELY (hidden_cache == NULL)
1493 hidden_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1494 g_free, maybe_unref_hash_table);
1496 if (!g_hash_table_lookup_extended (hidden_cache, dirname,
1500 GSource *remove_from_cache_source;
1502 g_hash_table_insert (hidden_cache,
1503 mydirname = g_strdup (dirname),
1504 table = read_hidden_file (dirname));
1506 remove_from_cache_source = g_timeout_source_new_seconds (5);
1507 g_source_set_priority (remove_from_cache_source, G_PRIORITY_DEFAULT);
1508 g_source_set_callback (remove_from_cache_source,
1509 remove_from_hidden_cache,
1512 g_source_attach (remove_from_cache_source,
1513 GLIB_PRIVATE_CALL (g_get_worker_context) ());
1514 g_source_unref (remove_from_cache_source);
1517 result = table != NULL && g_hash_table_contains (table, basename);
1519 G_UNLOCK (hidden_cache);
1525 #endif /* !G_OS_WIN32 */
1528 _g_local_file_info_get_nostat (GFileInfo *info,
1529 const char *basename,
1531 GFileAttributeMatcher *attribute_matcher)
1533 g_file_info_set_name (info, basename);
1535 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1536 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1538 char *display_name = g_filename_display_basename (path);
1540 /* look for U+FFFD REPLACEMENT CHARACTER */
1541 if (strstr (display_name, "\357\277\275") != NULL)
1543 char *p = display_name;
1544 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1547 g_file_info_set_display_name (info, display_name);
1548 g_free (display_name);
1551 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1552 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1554 char *edit_name = g_filename_display_basename (path);
1555 g_file_info_set_edit_name (info, edit_name);
1560 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1561 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1563 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1565 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1571 get_icon_name (const char *path,
1572 gboolean use_symbolic,
1573 gboolean *with_fallbacks_out)
1575 const char *name = NULL;
1576 gboolean with_fallbacks = TRUE;
1578 if (strcmp (path, g_get_home_dir ()) == 0)
1580 name = use_symbolic ? "user-home-symbolic" : "user-home";
1581 with_fallbacks = FALSE;
1583 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1585 name = use_symbolic ? "user-desktop-symbolic" : "user-desktop";
1586 with_fallbacks = FALSE;
1588 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1590 name = use_symbolic ? "folder-documents-symbolic" : "folder-documents";
1592 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1594 name = use_symbolic ? "folder-download-symbolic" : "folder-download";
1596 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1598 name = use_symbolic ? "folder-music-symbolic" : "folder-music";
1600 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1602 name = use_symbolic ? "folder-pictures-symbolic" : "folder-pictures";
1604 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1606 name = use_symbolic ? "folder-publicshare-symbolic" : "folder-publicshare";
1608 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1610 name = use_symbolic ? "folder-templates-symbolic" : "folder-templates";
1612 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1614 name = use_symbolic ? "folder-videos-symbolic" : "folder-videos";
1621 if (with_fallbacks_out != NULL)
1622 *with_fallbacks_out = with_fallbacks;
1628 get_icon (const char *path,
1629 const char *content_type,
1631 gboolean use_symbolic)
1634 const char *icon_name;
1635 gboolean with_fallbacks;
1637 icon_name = get_icon_name (path, use_symbolic, &with_fallbacks);
1638 if (icon_name != NULL)
1641 icon = g_themed_icon_new_with_default_fallbacks (icon_name);
1643 icon = g_themed_icon_new (icon_name);
1648 icon = g_content_type_get_symbolic_icon (content_type);
1650 icon = g_content_type_get_icon (content_type);
1652 if (G_IS_THEMED_ICON (icon) && is_folder)
1654 g_themed_icon_append_name (G_THEMED_ICON (icon), use_symbolic ? "folder-symbolic" : "folder");
1662 _g_local_file_info_get (const char *basename,
1664 GFileAttributeMatcher *attribute_matcher,
1665 GFileQueryInfoFlags flags,
1666 GLocalParentFileInfo *parent_info,
1670 GLocalFileStat statbuf;
1672 struct stat statbuf2;
1676 gboolean is_symlink, symlink_broken;
1678 DWORD dos_attributes;
1680 char *symlink_target;
1685 info = g_file_info_new ();
1687 /* Make sure we don't set any unwanted attributes */
1688 g_file_info_set_attribute_mask (info, attribute_matcher);
1690 _g_local_file_info_get_nostat (info, basename, path, attribute_matcher);
1692 if (attribute_matcher == NULL)
1694 g_file_info_unset_attribute_mask (info);
1699 res = g_lstat (path, &statbuf);
1702 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1707 g_object_unref (info);
1711 len = wcslen (wpath);
1712 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1715 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1718 res = _wstati64 (wpath, &statbuf);
1719 dos_attributes = GetFileAttributesW (wpath);
1729 /* Don't bail out if we get Permission denied (SELinux?) */
1730 if (errsv != EACCES)
1732 char *display_name = g_filename_display_name (path);
1733 g_object_unref (info);
1734 g_set_error (error, G_IO_ERROR,
1735 g_io_error_from_errno (errsv),
1736 _("Error when getting information for file '%s': %s"),
1737 display_name, g_strerror (errsv));
1738 g_free (display_name);
1743 /* Even if stat() fails, try to get as much as other attributes possible */
1744 stat_ok = res != -1;
1747 device = statbuf.st_dev;
1752 is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
1756 symlink_broken = FALSE;
1760 g_file_info_set_is_symlink (info, TRUE);
1762 /* Unless NOFOLLOW was set we default to following symlinks */
1763 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1765 res = stat (path, &statbuf2);
1767 /* Report broken links as symlinks */
1774 symlink_broken = TRUE;
1780 set_info_from_stat (info, &statbuf, attribute_matcher);
1783 if (stat_ok && _g_local_file_is_lost_found_dir (path, statbuf.st_dev))
1784 g_file_info_set_is_hidden (info, TRUE);
1788 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1789 G_FILE_ATTRIBUTE_ID_STANDARD_IS_HIDDEN))
1791 if (basename != NULL &&
1792 (basename[0] == '.' ||
1793 file_is_hidden (path, basename)))
1794 g_file_info_set_is_hidden (info, TRUE);
1797 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1798 (stat_ok && S_ISREG (statbuf.st_mode)))
1799 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1801 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1802 g_file_info_set_is_hidden (info, TRUE);
1804 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1805 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1807 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1808 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1811 symlink_target = NULL;
1815 symlink_target = read_link (path);
1816 if (symlink_target &&
1817 _g_file_attribute_matcher_matches_id (attribute_matcher,
1818 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1819 g_file_info_set_symlink_target (info, symlink_target);
1822 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1823 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1824 _g_file_attribute_matcher_matches_id (attribute_matcher,
1825 G_FILE_ATTRIBUTE_ID_STANDARD_ICON) ||
1826 _g_file_attribute_matcher_matches_id (attribute_matcher,
1827 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1829 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
1833 g_file_info_set_content_type (info, content_type);
1835 if (_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))
1842 /* non symbolic icon */
1843 icon = get_icon (path, content_type, S_ISDIR (statbuf.st_mode), FALSE);
1846 g_file_info_set_icon (info, icon);
1847 g_object_unref (icon);
1851 icon = get_icon (path, content_type, S_ISDIR (statbuf.st_mode), TRUE);
1854 g_file_info_set_symbolic_icon (info, icon);
1855 g_object_unref (icon);
1860 g_free (content_type);
1864 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1865 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1867 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
1871 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1872 g_free (content_type);
1876 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1877 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1882 win32_get_file_user_info (path, NULL, &name, NULL);
1885 name = get_username_from_uid (statbuf.st_uid);
1888 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1892 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1893 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1897 win32_get_file_user_info (path, NULL, NULL, &name);
1900 name = get_realname_from_uid (statbuf.st_uid);
1903 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1907 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1908 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1912 win32_get_file_user_info (path, &name, NULL, NULL);
1915 name = get_groupname_from_gid (statbuf.st_gid);
1918 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1922 if (stat_ok && parent_info && parent_info->device != 0 &&
1923 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1924 statbuf.st_dev != parent_info->device)
1925 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1928 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1931 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1933 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1934 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1936 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1937 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH))
1938 get_thumbnail_attributes (path, info);
1940 vfs = g_vfs_get_default ();
1941 class = G_VFS_GET_CLASS (vfs);
1942 if (class->local_file_add_info)
1944 class->local_file_add_info (vfs,
1950 &parent_info->extra_data,
1951 &parent_info->free_extra_data);
1954 g_file_info_unset_attribute_mask (info);
1956 g_free (symlink_target);
1962 _g_local_file_info_get_from_fd (int fd,
1963 const char *attributes,
1966 GLocalFileStat stat_buf;
1967 GFileAttributeMatcher *matcher;
1971 #define FSTAT _fstati64
1976 if (FSTAT (fd, &stat_buf) == -1)
1980 g_set_error (error, G_IO_ERROR,
1981 g_io_error_from_errno (errsv),
1982 _("Error when getting information for file descriptor: %s"),
1983 g_strerror (errsv));
1987 info = g_file_info_new ();
1989 matcher = g_file_attribute_matcher_new (attributes);
1991 /* Make sure we don't set any unwanted attributes */
1992 g_file_info_set_attribute_mask (info, matcher);
1994 set_info_from_stat (info, &stat_buf, matcher);
1997 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
1998 is_selinux_enabled ())
2001 if (fgetfilecon_raw (fd, &context) >= 0)
2003 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
2009 get_xattrs_from_fd (fd, TRUE, info, matcher);
2010 get_xattrs_from_fd (fd, FALSE, info, matcher);
2012 g_file_attribute_matcher_unref (matcher);
2014 g_file_info_unset_attribute_mask (info);
2020 get_uint32 (const GFileAttributeValue *value,
2024 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
2026 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2027 _("Invalid attribute type (uint32 expected)"));
2031 *val_out = value->u.uint32;
2038 get_uint64 (const GFileAttributeValue *value,
2042 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
2044 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2045 _("Invalid attribute type (uint64 expected)"));
2049 *val_out = value->u.uint64;
2055 #if defined(HAVE_SYMLINK)
2057 get_byte_string (const GFileAttributeValue *value,
2058 const char **val_out,
2061 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
2063 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2064 _("Invalid attribute type (byte string expected)"));
2068 *val_out = value->u.string;
2076 get_string (const GFileAttributeValue *value,
2077 const char **val_out,
2080 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
2082 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2083 _("Invalid attribute type (byte string expected)"));
2087 *val_out = value->u.string;
2094 set_unix_mode (char *filename,
2095 GFileQueryInfoFlags flags,
2096 const GFileAttributeValue *value,
2102 if (!get_uint32 (value, &val, error))
2106 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
2108 res = lchmod (filename, val);
2110 struct stat statbuf;
2111 /* Calling chmod on a symlink changes permissions on the symlink.
2112 * We don't want to do this, so we need to check for a symlink */
2113 res = g_lstat (filename, &statbuf);
2114 if (res == 0 && S_ISLNK (statbuf.st_mode))
2116 g_set_error_literal (error, G_IO_ERROR,
2117 G_IO_ERROR_NOT_SUPPORTED,
2118 _("Cannot set permissions on symlinks"));
2122 res = g_chmod (filename, val);
2126 res = g_chmod (filename, val);
2132 g_set_error (error, G_IO_ERROR,
2133 g_io_error_from_errno (errsv),
2134 _("Error setting permissions: %s"),
2135 g_strerror (errsv));
2143 set_unix_uid_gid (char *filename,
2144 const GFileAttributeValue *uid_value,
2145 const GFileAttributeValue *gid_value,
2146 GFileQueryInfoFlags flags,
2156 if (!get_uint32 (uid_value, &val, error))
2165 if (!get_uint32 (gid_value, &val, error))
2173 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
2174 res = lchown (filename, uid, gid);
2177 res = chown (filename, uid, gid);
2183 g_set_error (error, G_IO_ERROR,
2184 g_io_error_from_errno (errsv),
2185 _("Error setting owner: %s"),
2186 g_strerror (errsv));
2195 set_symlink (char *filename,
2196 const GFileAttributeValue *value,
2200 struct stat statbuf;
2202 if (!get_byte_string (value, &val, error))
2207 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2208 _("symlink must be non-NULL"));
2212 if (g_lstat (filename, &statbuf))
2216 g_set_error (error, G_IO_ERROR,
2217 g_io_error_from_errno (errsv),
2218 _("Error setting symlink: %s"),
2219 g_strerror (errsv));
2223 if (!S_ISLNK (statbuf.st_mode))
2225 g_set_error_literal (error, G_IO_ERROR,
2226 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2227 _("Error setting symlink: file is not a symlink"));
2231 if (g_unlink (filename))
2235 g_set_error (error, G_IO_ERROR,
2236 g_io_error_from_errno (errsv),
2237 _("Error setting symlink: %s"),
2238 g_strerror (errsv));
2242 if (symlink (filename, val) != 0)
2246 g_set_error (error, G_IO_ERROR,
2247 g_io_error_from_errno (errsv),
2248 _("Error setting symlink: %s"),
2249 g_strerror (errsv));
2259 lazy_stat (char *filename,
2260 struct stat *statbuf,
2261 gboolean *called_stat)
2268 res = g_stat (filename, statbuf);
2271 *called_stat = TRUE;
2278 set_mtime_atime (char *filename,
2279 const GFileAttributeValue *mtime_value,
2280 const GFileAttributeValue *mtime_usec_value,
2281 const GFileAttributeValue *atime_value,
2282 const GFileAttributeValue *atime_usec_value,
2287 guint32 val_usec = 0;
2288 struct stat statbuf;
2289 gboolean got_stat = FALSE;
2290 struct timeval times[2] = { {0, 0}, {0, 0} };
2295 if (!get_uint64 (atime_value, &val, error))
2297 times[0].tv_sec = val;
2301 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2303 times[0].tv_sec = statbuf.st_mtime;
2304 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2305 times[0].tv_usec = statbuf.st_atimensec / 1000;
2306 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2307 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2312 if (atime_usec_value)
2314 if (!get_uint32 (atime_usec_value, &val_usec, error))
2316 times[0].tv_usec = val_usec;
2322 if (!get_uint64 (mtime_value, &val, error))
2324 times[1].tv_sec = val;
2328 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2330 times[1].tv_sec = statbuf.st_mtime;
2331 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2332 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2333 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2334 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2339 if (mtime_usec_value)
2341 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2343 times[1].tv_usec = val_usec;
2346 res = utimes (filename, times);
2351 g_set_error (error, G_IO_ERROR,
2352 g_io_error_from_errno (errsv),
2353 _("Error setting modification or access time: %s"),
2354 g_strerror (errsv));
2364 set_selinux_context (char *filename,
2365 const GFileAttributeValue *value,
2370 if (!get_string (value, &val, error))
2375 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2376 _("SELinux context must be non-NULL"));
2380 if (is_selinux_enabled ()) {
2381 security_context_t val_s;
2383 val_s = g_strdup (val);
2385 if (setfilecon_raw (filename, val_s) < 0)
2389 g_set_error (error, G_IO_ERROR,
2390 g_io_error_from_errno (errsv),
2391 _("Error setting SELinux context: %s"),
2392 g_strerror (errsv));
2397 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2398 _("SELinux is not enabled on this system"));
2408 _g_local_file_info_set_attribute (char *filename,
2409 const char *attribute,
2410 GFileAttributeType type,
2412 GFileQueryInfoFlags flags,
2413 GCancellable *cancellable,
2416 GFileAttributeValue value = { 0 };
2420 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2422 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2423 return set_unix_mode (filename, flags, &value, error);
2426 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2427 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2428 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2429 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2433 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2434 return set_symlink (filename, &value, error);
2438 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2439 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2440 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2441 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2442 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2443 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2444 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2445 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2449 else if (g_str_has_prefix (attribute, "xattr::"))
2450 return set_xattr (filename, attribute, &value, error);
2451 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2452 return set_xattr (filename, attribute, &value, error);
2456 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2457 return set_selinux_context (filename, &value, error);
2460 vfs = g_vfs_get_default ();
2461 class = G_VFS_GET_CLASS (vfs);
2462 if (class->local_file_set_attributes)
2466 info = g_file_info_new ();
2467 g_file_info_set_attribute (info,
2471 if (!class->local_file_set_attributes (vfs, filename,
2476 g_object_unref (info);
2480 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2482 g_object_unref (info);
2486 g_object_unref (info);
2489 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2490 _("Setting attribute %s not supported"), attribute);
2495 _g_local_file_info_set_attributes (char *filename,
2497 GFileQueryInfoFlags flags,
2498 GCancellable *cancellable,
2501 GFileAttributeValue *value;
2503 GFileAttributeValue *uid, *gid;
2506 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2508 #if defined (HAVE_CHOWN) || defined (HAVE_UTIMES)
2509 GFileAttributeStatus status;
2515 /* Handles setting multiple specified data in a single set, and takes care
2516 of ordering restrictions when setting attributes */
2520 /* Set symlink first, since this recreates the file */
2522 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2525 if (!set_symlink (filename, value, error))
2527 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2529 /* Don't set error multiple times */
2533 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2539 /* Group uid and gid setting into one call
2540 * Change ownership before permissions, since ownership changes can
2541 change permissions (e.g. setuid)
2543 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2544 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2548 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2550 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2552 /* Don't set error multiple times */
2556 status = G_FILE_ATTRIBUTE_STATUS_SET;
2558 uid->status = status;
2560 gid->status = status;
2564 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2567 if (!set_unix_mode (filename, flags, value, error))
2569 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2571 /* Don't set error multiple times */
2575 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2580 /* Group all time settings into one call
2581 * Change times as the last thing to avoid it changing due to metadata changes
2584 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2585 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2586 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2587 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2589 if (mtime || mtime_usec || atime || atime_usec)
2591 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2593 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2595 /* Don't set error multiple times */
2599 status = G_FILE_ATTRIBUTE_STATUS_SET;
2602 mtime->status = status;
2604 mtime_usec->status = status;
2606 atime->status = status;
2608 atime_usec->status = status;
2612 /* xattrs are handled by default callback */
2615 /* SELinux context */
2617 if (is_selinux_enabled ()) {
2618 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2621 if (!set_selinux_context (filename, value, error))
2623 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2625 /* Don't set error multiple times */
2629 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2634 vfs = g_vfs_get_default ();
2635 class = G_VFS_GET_CLASS (vfs);
2636 if (class->local_file_set_attributes)
2638 if (!class->local_file_set_attributes (vfs, filename,
2644 /* Don't set error multiple times */