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>
29 #ifdef HAVE_SYS_TIME_H
32 #include <sys/types.h>
42 #include <selinux/selinux.h>
47 #if defined HAVE_SYS_XATTR_H
48 #include <sys/xattr.h>
49 #elif defined HAVE_ATTR_XATTR_H
50 #include <attr/xattr.h>
52 #error "Neither <sys/xattr.h> nor <attr/xattr.h> is present but extended attribute support is enabled."
53 #endif /* defined HAVE_SYS_XATTR_H || HAVE_ATTR_XATTR_H */
55 #endif /* HAVE_XATTR */
57 #include <glib/gstdio.h>
58 #include <gfileattribute-priv.h>
59 #include <gfileinfo-priv.h>
64 #include "glib-unix.h"
65 #include "glib-private.h"
68 #include "thumbnail-verify.h"
80 #define X_OK 0 /* not really */
83 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
86 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
89 #define S_IXUSR _S_IEXEC
93 #include "glocalfileinfo.h"
95 #include "gthemedicon.h"
96 #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);
670 len = strlen (attr) + 1;
672 list_res_size -= len;
679 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
681 char *unescaped_attribute, *a;
682 gboolean free_unescaped_attribute;
684 attr2 = strchr (attr, ':');
687 attr2++; /* Skip ':' */
688 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
690 a = g_strconcat ("user.", unescaped_attribute, NULL);
692 a = unescaped_attribute;
694 get_one_xattr_from_fd (fd, info, attr, a);
699 if (free_unescaped_attribute)
700 g_free (unescaped_attribute);
704 #endif /* defined HAVE_XATTR */
709 set_xattr (char *filename,
710 const char *escaped_attribute,
711 const GFileAttributeValue *attr_value,
714 char *attribute, *value;
715 gboolean free_attribute, free_value;
716 int val_len, res, errsv;
720 if (attr_value == NULL)
722 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
723 _("Attribute value must be non-NULL"));
727 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
729 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
730 _("Invalid attribute type (string expected)"));
734 if (!name_is_valid (escaped_attribute))
736 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
737 _("Invalid extended attribute name"));
741 if (g_str_has_prefix (escaped_attribute, "xattr::"))
743 escaped_attribute += strlen ("xattr::");
748 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
749 escaped_attribute += strlen ("xattr-sys::");
753 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
754 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
757 a = g_strconcat ("user.", attribute, NULL);
761 res = g_setxattr (filename, a, value, val_len);
775 g_set_error (error, G_IO_ERROR,
776 g_io_error_from_errno (errsv),
777 _("Error setting extended attribute '%s': %s"),
778 escaped_attribute, g_strerror (errsv));
789 _g_local_file_info_get_parent_info (const char *dir,
790 GFileAttributeMatcher *attribute_matcher,
791 GLocalParentFileInfo *parent_info)
796 parent_info->extra_data = NULL;
797 parent_info->free_extra_data = NULL;
798 parent_info->writable = FALSE;
799 parent_info->is_sticky = FALSE;
800 parent_info->has_trash_dir = FALSE;
801 parent_info->device = 0;
803 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME) ||
804 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE) ||
805 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH) ||
806 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT))
808 /* FIXME: Windows: The underlying _waccess() call in the C
809 * library is mostly pointless as it only looks at the READONLY
810 * FAT-style attribute of the file, it doesn't check the ACL at
813 parent_info->writable = (g_access (dir, W_OK) == 0);
815 res = g_stat (dir, &statbuf);
818 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
819 * renamed or deleted only by the owner of the file, by the owner of the directory, and
820 * by a privileged process.
825 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
827 parent_info->is_sticky = FALSE;
829 parent_info->owner = statbuf.st_uid;
830 parent_info->device = statbuf.st_dev;
831 /* No need to find trash dir if it's not writable anyway */
832 if (parent_info->writable &&
833 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
834 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
840 _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info)
842 if (parent_info->extra_data &&
843 parent_info->free_extra_data)
844 parent_info->free_extra_data (parent_info->extra_data);
848 get_access_rights (GFileAttributeMatcher *attribute_matcher,
851 GLocalFileStat *statbuf,
852 GLocalParentFileInfo *parent_info)
854 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
855 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
856 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ))
857 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ,
858 g_access (path, R_OK) == 0);
860 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
861 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE))
862 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE,
863 g_access (path, W_OK) == 0);
865 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
866 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE))
867 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE,
868 g_access (path, X_OK) == 0);
876 if (parent_info->writable)
878 if (parent_info->is_sticky)
881 uid_t uid = geteuid ();
883 if (uid == statbuf->st_uid ||
884 uid == parent_info->owner ||
893 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME))
894 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME,
897 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE))
898 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE,
901 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
902 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH,
903 writable && parent_info->has_trash_dir);
908 set_info_from_stat (GFileInfo *info,
909 GLocalFileStat *statbuf,
910 GFileAttributeMatcher *attribute_matcher)
914 file_type = G_FILE_TYPE_UNKNOWN;
916 if (S_ISREG (statbuf->st_mode))
917 file_type = G_FILE_TYPE_REGULAR;
918 else if (S_ISDIR (statbuf->st_mode))
919 file_type = G_FILE_TYPE_DIRECTORY;
921 else if (S_ISCHR (statbuf->st_mode) ||
922 S_ISBLK (statbuf->st_mode) ||
923 S_ISFIFO (statbuf->st_mode)
925 || S_ISSOCK (statbuf->st_mode)
928 file_type = G_FILE_TYPE_SPECIAL;
931 else if (S_ISLNK (statbuf->st_mode))
932 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
935 g_file_info_set_file_type (info, file_type);
936 g_file_info_set_size (info, statbuf->st_size);
938 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
940 /* Pointless setting these on Windows even if they exist in the struct */
941 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
942 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
943 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
944 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
945 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
947 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
948 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
949 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
950 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
952 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
953 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
954 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
955 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
958 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
959 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
960 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
961 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
962 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
965 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
966 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
967 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
968 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
969 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
972 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
973 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
974 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
975 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
976 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
979 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
980 G_FILE_ATTRIBUTE_ID_ETAG_VALUE))
982 char *etag = _g_local_file_info_create_etag (statbuf);
983 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ETAG_VALUE, etag);
987 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
988 G_FILE_ATTRIBUTE_ID_ID_FILE))
990 char *id = _g_local_file_info_create_file_id (statbuf);
991 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILE, id);
995 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
996 G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM))
998 char *id = _g_local_file_info_create_fs_id (statbuf);
999 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM, id);
1007 make_valid_utf8 (const char *name)
1010 const gchar *remainder, *invalid;
1011 gint remaining_bytes, valid_bytes;
1015 remaining_bytes = strlen (name);
1017 while (remaining_bytes != 0)
1019 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1021 valid_bytes = invalid - remainder;
1024 string = g_string_sized_new (remaining_bytes);
1026 g_string_append_len (string, remainder, valid_bytes);
1027 /* append U+FFFD REPLACEMENT CHARACTER */
1028 g_string_append (string, "\357\277\275");
1030 remaining_bytes -= valid_bytes + 1;
1031 remainder = invalid + 1;
1035 return g_strdup (name);
1037 g_string_append (string, remainder);
1039 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1041 return g_string_free (string, FALSE);
1045 convert_pwd_string_to_utf8 (char *pwd_str)
1049 if (!g_utf8_validate (pwd_str, -1, NULL))
1051 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1052 if (utf8_string == NULL)
1053 utf8_string = make_valid_utf8 (pwd_str);
1056 utf8_string = g_strdup (pwd_str);
1062 uid_data_free (UidData *data)
1064 g_free (data->user_name);
1065 g_free (data->real_name);
1069 /* called with lock held */
1071 lookup_uid_data (uid_t uid)
1075 struct passwd pwbuf;
1076 struct passwd *pwbufp;
1077 char *gecos, *comma;
1079 if (uid_cache == NULL)
1080 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1082 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1087 data = g_new0 (UidData, 1);
1089 #if defined(HAVE_POSIX_GETPWUID_R)
1090 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1091 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1092 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1094 pwbufp = getpwuid (uid);
1099 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1100 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1103 gecos = pwbufp->pw_gecos;
1107 comma = strchr (gecos, ',');
1110 data->real_name = convert_pwd_string_to_utf8 (gecos);
1115 /* Default fallbacks */
1116 if (data->real_name == NULL)
1118 if (data->user_name != NULL)
1119 data->real_name = g_strdup (data->user_name);
1121 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1124 if (data->user_name == NULL)
1125 data->user_name = g_strdup_printf ("%d", (int)uid);
1127 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1133 get_username_from_uid (uid_t uid)
1139 data = lookup_uid_data (uid);
1140 res = g_strdup (data->user_name);
1141 G_UNLOCK (uid_cache);
1147 get_realname_from_uid (uid_t uid)
1153 data = lookup_uid_data (uid);
1154 res = g_strdup (data->real_name);
1155 G_UNLOCK (uid_cache);
1160 /* called with lock held */
1162 lookup_gid_name (gid_t gid)
1167 struct group *gbufp;
1169 if (gid_cache == NULL)
1170 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1172 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1177 #if defined (HAVE_POSIX_GETGRGID_R)
1178 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1179 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1180 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1182 gbufp = getgrgid (gid);
1185 if (gbufp != NULL &&
1186 gbufp->gr_name != NULL &&
1187 gbufp->gr_name[0] != 0)
1188 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1190 name = g_strdup_printf("%d", (int)gid);
1192 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1198 get_groupname_from_gid (gid_t gid)
1204 name = lookup_gid_name (gid);
1205 res = g_strdup (name);
1206 G_UNLOCK (gid_cache);
1210 #endif /* !G_OS_WIN32 */
1213 get_content_type (const char *basename,
1215 GLocalFileStat *statbuf,
1216 gboolean is_symlink,
1217 gboolean symlink_broken,
1218 GFileQueryInfoFlags flags,
1222 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1223 return g_strdup ("inode/symlink");
1224 else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
1225 return g_strdup ("inode/directory");
1227 else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
1228 return g_strdup ("inode/chardevice");
1229 else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
1230 return g_strdup ("inode/blockdevice");
1231 else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
1232 return g_strdup ("inode/fifo");
1235 else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
1236 return g_strdup ("inode/socket");
1241 gboolean result_uncertain;
1243 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1246 if (!fast && result_uncertain && path != NULL)
1248 guchar sniff_buffer[4096];
1252 sniff_length = _g_unix_content_type_get_sniff_len ();
1253 if (sniff_length > 4096)
1254 sniff_length = 4096;
1257 fd = g_open (path, O_RDONLY | O_NOATIME, 0);
1258 if (fd < 0 && errno == EPERM)
1260 fd = g_open (path, O_RDONLY, 0);
1266 res = read (fd, sniff_buffer, sniff_length);
1267 (void) g_close (fd, NULL);
1270 g_free (content_type);
1271 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1277 return content_type;
1282 /* @stat_buf is the pre-calculated result of stat(path), or %NULL if that failed. */
1284 get_thumbnail_attributes (const char *path,
1286 const GLocalFileStat *stat_buf)
1288 GChecksum *checksum;
1293 uri = g_filename_to_uri (path, NULL, NULL);
1295 checksum = g_checksum_new (G_CHECKSUM_MD5);
1296 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1298 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1299 g_checksum_free (checksum);
1301 filename = g_build_filename (g_get_user_cache_dir (),
1302 "thumbnails", "large", basename,
1305 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1307 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1308 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1309 thumbnail_verify (filename, uri, stat_buf));
1314 filename = g_build_filename (g_get_user_cache_dir (),
1315 "thumbnails", "normal", basename,
1318 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1320 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1321 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1322 thumbnail_verify (filename, uri, stat_buf));
1327 filename = g_build_filename (g_get_user_cache_dir (),
1328 "thumbnails", "fail",
1329 "gnome-thumbnail-factory",
1333 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1335 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1336 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1337 thumbnail_verify (filename, uri, stat_buf));
1348 win32_get_file_user_info (const gchar *filename,
1353 PSECURITY_DESCRIPTOR psd = NULL;
1354 DWORD sd_size = 0; /* first call calculates the size required */
1356 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1357 if ((GetFileSecurityW (wfilename,
1358 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1361 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1362 (psd = g_try_malloc (sd_size)) != NULL &&
1363 GetFileSecurityW (wfilename,
1364 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1371 SID_NAME_USE name_use = 0; /* don't care? */
1372 wchar_t *name = NULL;
1373 wchar_t *domain = NULL;
1375 DWORD domain_len = 0;
1376 /* get the user name */
1380 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1382 if (!LookupAccountSidW (NULL, /* local machine */
1385 domain, &domain_len, /* no domain info yet */
1386 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1388 name = g_try_malloc (name_len * sizeof (wchar_t));
1389 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1390 if (name && domain &&
1391 LookupAccountSidW (NULL, /* local machine */
1394 domain, &domain_len, /* no domain info yet */
1397 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1403 /* get the group name */
1407 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1409 if (!LookupAccountSidW (NULL, /* local machine */
1412 domain, &domain_len, /* no domain info yet */
1413 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1415 name = g_try_malloc (name_len * sizeof (wchar_t));
1416 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1417 if (name && domain &&
1418 LookupAccountSidW (NULL, /* local machine */
1421 domain, &domain_len, /* no domain info yet */
1424 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1430 /* TODO: get real name */
1436 #endif /* G_OS_WIN32 */
1439 /* support for '.hidden' files */
1440 G_LOCK_DEFINE_STATIC (hidden_cache);
1441 static GHashTable *hidden_cache;
1444 remove_from_hidden_cache (gpointer user_data)
1446 G_LOCK (hidden_cache);
1447 g_hash_table_remove (hidden_cache, user_data);
1448 G_UNLOCK (hidden_cache);
1454 read_hidden_file (const gchar *dirname)
1456 gchar *contents = NULL;
1459 filename = g_build_path ("/", dirname, ".hidden", NULL);
1460 g_file_get_contents (filename, &contents, NULL, NULL);
1463 if (contents != NULL)
1469 table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1471 lines = g_strsplit (contents, "\n", 0);
1474 for (i = 0; lines[i]; i++)
1475 /* hash table takes the individual strings... */
1476 g_hash_table_add (table, lines[i]);
1478 /* ... so we only free the container. */
1488 maybe_unref_hash_table (gpointer data)
1491 g_hash_table_unref (data);
1495 file_is_hidden (const gchar *path,
1496 const gchar *basename)
1502 dirname = g_path_get_dirname (path);
1504 G_LOCK (hidden_cache);
1506 if G_UNLIKELY (hidden_cache == NULL)
1507 hidden_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1508 g_free, maybe_unref_hash_table);
1510 if (!g_hash_table_lookup_extended (hidden_cache, dirname,
1514 GSource *remove_from_cache_source;
1516 g_hash_table_insert (hidden_cache,
1517 mydirname = g_strdup (dirname),
1518 table = read_hidden_file (dirname));
1520 remove_from_cache_source = g_timeout_source_new_seconds (5);
1521 g_source_set_priority (remove_from_cache_source, G_PRIORITY_DEFAULT);
1522 g_source_set_callback (remove_from_cache_source,
1523 remove_from_hidden_cache,
1526 g_source_attach (remove_from_cache_source,
1527 GLIB_PRIVATE_CALL (g_get_worker_context) ());
1528 g_source_unref (remove_from_cache_source);
1531 result = table != NULL && g_hash_table_contains (table, basename);
1533 G_UNLOCK (hidden_cache);
1539 #endif /* !G_OS_WIN32 */
1542 _g_local_file_info_get_nostat (GFileInfo *info,
1543 const char *basename,
1545 GFileAttributeMatcher *attribute_matcher)
1547 g_file_info_set_name (info, basename);
1549 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1550 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1552 char *display_name = g_filename_display_basename (path);
1554 /* look for U+FFFD REPLACEMENT CHARACTER */
1555 if (strstr (display_name, "\357\277\275") != NULL)
1557 char *p = display_name;
1558 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1561 g_file_info_set_display_name (info, display_name);
1562 g_free (display_name);
1565 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1566 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1568 char *edit_name = g_filename_display_basename (path);
1569 g_file_info_set_edit_name (info, edit_name);
1574 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1575 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1577 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1579 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1585 get_icon_name (const char *path,
1586 gboolean use_symbolic,
1587 gboolean *with_fallbacks_out)
1589 const char *name = NULL;
1590 gboolean with_fallbacks = TRUE;
1592 if (strcmp (path, g_get_home_dir ()) == 0)
1594 name = use_symbolic ? "user-home-symbolic" : "user-home";
1595 with_fallbacks = FALSE;
1597 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1599 name = use_symbolic ? "user-desktop-symbolic" : "user-desktop";
1600 with_fallbacks = FALSE;
1602 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1604 name = use_symbolic ? "folder-documents-symbolic" : "folder-documents";
1606 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1608 name = use_symbolic ? "folder-download-symbolic" : "folder-download";
1610 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1612 name = use_symbolic ? "folder-music-symbolic" : "folder-music";
1614 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1616 name = use_symbolic ? "folder-pictures-symbolic" : "folder-pictures";
1618 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1620 name = use_symbolic ? "folder-publicshare-symbolic" : "folder-publicshare";
1622 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1624 name = use_symbolic ? "folder-templates-symbolic" : "folder-templates";
1626 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1628 name = use_symbolic ? "folder-videos-symbolic" : "folder-videos";
1635 if (with_fallbacks_out != NULL)
1636 *with_fallbacks_out = with_fallbacks;
1642 get_icon (const char *path,
1643 const char *content_type,
1645 gboolean use_symbolic)
1648 const char *icon_name;
1649 gboolean with_fallbacks;
1651 icon_name = get_icon_name (path, use_symbolic, &with_fallbacks);
1652 if (icon_name != NULL)
1655 icon = g_themed_icon_new_with_default_fallbacks (icon_name);
1657 icon = g_themed_icon_new (icon_name);
1662 icon = g_content_type_get_symbolic_icon (content_type);
1664 icon = g_content_type_get_icon (content_type);
1666 if (G_IS_THEMED_ICON (icon) && is_folder)
1668 g_themed_icon_append_name (G_THEMED_ICON (icon), use_symbolic ? "folder-symbolic" : "folder");
1676 _g_local_file_info_get (const char *basename,
1678 GFileAttributeMatcher *attribute_matcher,
1679 GFileQueryInfoFlags flags,
1680 GLocalParentFileInfo *parent_info,
1684 GLocalFileStat statbuf;
1686 struct stat statbuf2;
1690 gboolean is_symlink, symlink_broken;
1692 DWORD dos_attributes;
1694 char *symlink_target;
1699 info = g_file_info_new ();
1701 /* Make sure we don't set any unwanted attributes */
1702 g_file_info_set_attribute_mask (info, attribute_matcher);
1704 _g_local_file_info_get_nostat (info, basename, path, attribute_matcher);
1706 if (attribute_matcher == NULL)
1708 g_file_info_unset_attribute_mask (info);
1713 res = g_lstat (path, &statbuf);
1716 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1721 g_object_unref (info);
1725 len = wcslen (wpath);
1726 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1729 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1732 res = _wstati64 (wpath, &statbuf);
1733 dos_attributes = GetFileAttributesW (wpath);
1743 /* Don't bail out if we get Permission denied (SELinux?) */
1744 if (errsv != EACCES)
1746 char *display_name = g_filename_display_name (path);
1747 g_object_unref (info);
1748 g_set_error (error, G_IO_ERROR,
1749 g_io_error_from_errno (errsv),
1750 _("Error when getting information for file '%s': %s"),
1751 display_name, g_strerror (errsv));
1752 g_free (display_name);
1757 /* Even if stat() fails, try to get as much as other attributes possible */
1758 stat_ok = res != -1;
1761 device = statbuf.st_dev;
1766 is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
1770 symlink_broken = FALSE;
1774 g_file_info_set_is_symlink (info, TRUE);
1776 /* Unless NOFOLLOW was set we default to following symlinks */
1777 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1779 res = stat (path, &statbuf2);
1781 /* Report broken links as symlinks */
1788 symlink_broken = TRUE;
1794 set_info_from_stat (info, &statbuf, attribute_matcher);
1797 if (stat_ok && _g_local_file_is_lost_found_dir (path, statbuf.st_dev))
1798 g_file_info_set_is_hidden (info, TRUE);
1802 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1803 G_FILE_ATTRIBUTE_ID_STANDARD_IS_HIDDEN))
1805 if (basename != NULL &&
1806 (basename[0] == '.' ||
1807 file_is_hidden (path, basename)))
1808 g_file_info_set_is_hidden (info, TRUE);
1811 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1812 (stat_ok && S_ISREG (statbuf.st_mode)))
1813 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1815 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1816 g_file_info_set_is_hidden (info, TRUE);
1818 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1819 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1821 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1822 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1825 symlink_target = NULL;
1829 symlink_target = read_link (path);
1830 if (symlink_target &&
1831 _g_file_attribute_matcher_matches_id (attribute_matcher,
1832 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1833 g_file_info_set_symlink_target (info, symlink_target);
1836 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1837 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1838 _g_file_attribute_matcher_matches_id (attribute_matcher,
1839 G_FILE_ATTRIBUTE_ID_STANDARD_ICON) ||
1840 _g_file_attribute_matcher_matches_id (attribute_matcher,
1841 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1843 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
1847 g_file_info_set_content_type (info, content_type);
1849 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1850 G_FILE_ATTRIBUTE_ID_STANDARD_ICON)
1851 || _g_file_attribute_matcher_matches_id (attribute_matcher,
1852 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1856 /* non symbolic icon */
1857 icon = get_icon (path, content_type, S_ISDIR (statbuf.st_mode), FALSE);
1860 g_file_info_set_icon (info, icon);
1861 g_object_unref (icon);
1865 icon = get_icon (path, content_type, S_ISDIR (statbuf.st_mode), TRUE);
1868 g_file_info_set_symbolic_icon (info, icon);
1869 g_object_unref (icon);
1874 g_free (content_type);
1878 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1879 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1881 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
1885 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1886 g_free (content_type);
1890 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1891 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1896 win32_get_file_user_info (path, NULL, &name, NULL);
1899 name = get_username_from_uid (statbuf.st_uid);
1902 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1906 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1907 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1911 win32_get_file_user_info (path, NULL, NULL, &name);
1914 name = get_realname_from_uid (statbuf.st_uid);
1917 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1921 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1922 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1926 win32_get_file_user_info (path, &name, NULL, NULL);
1929 name = get_groupname_from_gid (statbuf.st_gid);
1932 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1936 if (stat_ok && parent_info && parent_info->device != 0 &&
1937 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1938 statbuf.st_dev != parent_info->device)
1939 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1942 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1945 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1947 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1948 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1950 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1951 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH))
1954 get_thumbnail_attributes (path, info, &statbuf);
1956 get_thumbnail_attributes (path, info, NULL);
1959 vfs = g_vfs_get_default ();
1960 class = G_VFS_GET_CLASS (vfs);
1961 if (class->local_file_add_info)
1963 class->local_file_add_info (vfs,
1969 &parent_info->extra_data,
1970 &parent_info->free_extra_data);
1973 g_file_info_unset_attribute_mask (info);
1975 g_free (symlink_target);
1981 _g_local_file_info_get_from_fd (int fd,
1982 const char *attributes,
1985 GLocalFileStat stat_buf;
1986 GFileAttributeMatcher *matcher;
1990 #define FSTAT _fstati64
1995 if (FSTAT (fd, &stat_buf) == -1)
1999 g_set_error (error, G_IO_ERROR,
2000 g_io_error_from_errno (errsv),
2001 _("Error when getting information for file descriptor: %s"),
2002 g_strerror (errsv));
2006 info = g_file_info_new ();
2008 matcher = g_file_attribute_matcher_new (attributes);
2010 /* Make sure we don't set any unwanted attributes */
2011 g_file_info_set_attribute_mask (info, matcher);
2013 set_info_from_stat (info, &stat_buf, matcher);
2016 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
2017 is_selinux_enabled ())
2020 if (fgetfilecon_raw (fd, &context) >= 0)
2022 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
2028 get_xattrs_from_fd (fd, TRUE, info, matcher);
2029 get_xattrs_from_fd (fd, FALSE, info, matcher);
2031 g_file_attribute_matcher_unref (matcher);
2033 g_file_info_unset_attribute_mask (info);
2039 get_uint32 (const GFileAttributeValue *value,
2043 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
2045 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2046 _("Invalid attribute type (uint32 expected)"));
2050 *val_out = value->u.uint32;
2057 get_uint64 (const GFileAttributeValue *value,
2061 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
2063 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2064 _("Invalid attribute type (uint64 expected)"));
2068 *val_out = value->u.uint64;
2074 #if defined(HAVE_SYMLINK)
2076 get_byte_string (const GFileAttributeValue *value,
2077 const char **val_out,
2080 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_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;
2095 get_string (const GFileAttributeValue *value,
2096 const char **val_out,
2099 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
2101 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2102 _("Invalid attribute type (byte string expected)"));
2106 *val_out = value->u.string;
2113 set_unix_mode (char *filename,
2114 GFileQueryInfoFlags flags,
2115 const GFileAttributeValue *value,
2121 if (!get_uint32 (value, &val, error))
2125 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
2127 res = lchmod (filename, val);
2129 struct stat statbuf;
2130 /* Calling chmod on a symlink changes permissions on the symlink.
2131 * We don't want to do this, so we need to check for a symlink */
2132 res = g_lstat (filename, &statbuf);
2133 if (res == 0 && S_ISLNK (statbuf.st_mode))
2135 g_set_error_literal (error, G_IO_ERROR,
2136 G_IO_ERROR_NOT_SUPPORTED,
2137 _("Cannot set permissions on symlinks"));
2141 res = g_chmod (filename, val);
2145 res = g_chmod (filename, val);
2151 g_set_error (error, G_IO_ERROR,
2152 g_io_error_from_errno (errsv),
2153 _("Error setting permissions: %s"),
2154 g_strerror (errsv));
2162 set_unix_uid_gid (char *filename,
2163 const GFileAttributeValue *uid_value,
2164 const GFileAttributeValue *gid_value,
2165 GFileQueryInfoFlags flags,
2175 if (!get_uint32 (uid_value, &val, error))
2184 if (!get_uint32 (gid_value, &val, error))
2192 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
2193 res = lchown (filename, uid, gid);
2196 res = chown (filename, uid, gid);
2202 g_set_error (error, G_IO_ERROR,
2203 g_io_error_from_errno (errsv),
2204 _("Error setting owner: %s"),
2205 g_strerror (errsv));
2214 set_symlink (char *filename,
2215 const GFileAttributeValue *value,
2219 struct stat statbuf;
2221 if (!get_byte_string (value, &val, error))
2226 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2227 _("symlink must be non-NULL"));
2231 if (g_lstat (filename, &statbuf))
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 (!S_ISLNK (statbuf.st_mode))
2244 g_set_error_literal (error, G_IO_ERROR,
2245 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2246 _("Error setting symlink: file is not a symlink"));
2250 if (g_unlink (filename))
2254 g_set_error (error, G_IO_ERROR,
2255 g_io_error_from_errno (errsv),
2256 _("Error setting symlink: %s"),
2257 g_strerror (errsv));
2261 if (symlink (filename, val) != 0)
2265 g_set_error (error, G_IO_ERROR,
2266 g_io_error_from_errno (errsv),
2267 _("Error setting symlink: %s"),
2268 g_strerror (errsv));
2278 lazy_stat (char *filename,
2279 struct stat *statbuf,
2280 gboolean *called_stat)
2287 res = g_stat (filename, statbuf);
2290 *called_stat = TRUE;
2297 set_mtime_atime (char *filename,
2298 const GFileAttributeValue *mtime_value,
2299 const GFileAttributeValue *mtime_usec_value,
2300 const GFileAttributeValue *atime_value,
2301 const GFileAttributeValue *atime_usec_value,
2306 guint32 val_usec = 0;
2307 struct stat statbuf;
2308 gboolean got_stat = FALSE;
2309 struct timeval times[2] = { {0, 0}, {0, 0} };
2314 if (!get_uint64 (atime_value, &val, error))
2316 times[0].tv_sec = val;
2320 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2322 times[0].tv_sec = statbuf.st_mtime;
2323 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2324 times[0].tv_usec = statbuf.st_atimensec / 1000;
2325 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2326 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2331 if (atime_usec_value)
2333 if (!get_uint32 (atime_usec_value, &val_usec, error))
2335 times[0].tv_usec = val_usec;
2341 if (!get_uint64 (mtime_value, &val, error))
2343 times[1].tv_sec = val;
2347 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2349 times[1].tv_sec = statbuf.st_mtime;
2350 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2351 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2352 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2353 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2358 if (mtime_usec_value)
2360 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2362 times[1].tv_usec = val_usec;
2365 res = utimes (filename, times);
2370 g_set_error (error, G_IO_ERROR,
2371 g_io_error_from_errno (errsv),
2372 _("Error setting modification or access time: %s"),
2373 g_strerror (errsv));
2383 set_selinux_context (char *filename,
2384 const GFileAttributeValue *value,
2389 if (!get_string (value, &val, error))
2394 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2395 _("SELinux context must be non-NULL"));
2399 if (is_selinux_enabled ()) {
2400 security_context_t val_s;
2402 val_s = g_strdup (val);
2404 if (setfilecon_raw (filename, val_s) < 0)
2408 g_set_error (error, G_IO_ERROR,
2409 g_io_error_from_errno (errsv),
2410 _("Error setting SELinux context: %s"),
2411 g_strerror (errsv));
2416 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2417 _("SELinux is not enabled on this system"));
2427 _g_local_file_info_set_attribute (char *filename,
2428 const char *attribute,
2429 GFileAttributeType type,
2431 GFileQueryInfoFlags flags,
2432 GCancellable *cancellable,
2435 GFileAttributeValue value = { 0 };
2439 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2441 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2442 return set_unix_mode (filename, flags, &value, error);
2445 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2446 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2447 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2448 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2452 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2453 return set_symlink (filename, &value, error);
2457 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2458 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2459 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2460 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2461 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2462 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2463 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2464 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2468 else if (g_str_has_prefix (attribute, "xattr::"))
2469 return set_xattr (filename, attribute, &value, error);
2470 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2471 return set_xattr (filename, attribute, &value, error);
2475 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2476 return set_selinux_context (filename, &value, error);
2479 vfs = g_vfs_get_default ();
2480 class = G_VFS_GET_CLASS (vfs);
2481 if (class->local_file_set_attributes)
2485 info = g_file_info_new ();
2486 g_file_info_set_attribute (info,
2490 if (!class->local_file_set_attributes (vfs, filename,
2495 g_object_unref (info);
2499 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2501 g_object_unref (info);
2505 g_object_unref (info);
2508 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2509 _("Setting attribute %s not supported"), attribute);
2514 _g_local_file_info_set_attributes (char *filename,
2516 GFileQueryInfoFlags flags,
2517 GCancellable *cancellable,
2520 GFileAttributeValue *value;
2522 GFileAttributeValue *uid, *gid;
2524 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2526 GFileAttributeStatus status;
2532 /* Handles setting multiple specified data in a single set, and takes care
2533 of ordering restrictions when setting attributes */
2537 /* Set symlink first, since this recreates the file */
2539 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2542 if (!set_symlink (filename, value, error))
2544 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2546 /* Don't set error multiple times */
2550 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2556 /* Group uid and gid setting into one call
2557 * Change ownership before permissions, since ownership changes can
2558 change permissions (e.g. setuid)
2560 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2561 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2565 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2567 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2569 /* Don't set error multiple times */
2573 status = G_FILE_ATTRIBUTE_STATUS_SET;
2575 uid->status = status;
2577 gid->status = status;
2581 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2584 if (!set_unix_mode (filename, flags, value, error))
2586 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2588 /* Don't set error multiple times */
2592 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2597 /* Group all time settings into one call
2598 * Change times as the last thing to avoid it changing due to metadata changes
2601 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2602 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2603 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2604 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2606 if (mtime || mtime_usec || atime || atime_usec)
2608 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2610 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2612 /* Don't set error multiple times */
2616 status = G_FILE_ATTRIBUTE_STATUS_SET;
2619 mtime->status = status;
2621 mtime_usec->status = status;
2623 atime->status = status;
2625 atime_usec->status = status;
2629 /* xattrs are handled by default callback */
2632 /* SELinux context */
2634 if (is_selinux_enabled ()) {
2635 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2638 if (!set_selinux_context (filename, value, error))
2640 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2642 /* Don't set error multiple times */
2646 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2651 vfs = g_vfs_get_default ();
2652 class = G_VFS_GET_CLASS (vfs);
2653 if (class->local_file_set_attributes)
2655 if (!class->local_file_set_attributes (vfs, filename,
2661 /* Don't set error multiple times */