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>
77 #define X_OK 0 /* not really */
80 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
83 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
86 #define S_IXUSR _S_IEXEC
90 #include "glocalfileinfo.h"
92 #include "gthemedicon.h"
93 #include "gcontenttype.h"
94 #include "gcontenttypeprivate.h"
97 struct ThumbMD5Context {
100 unsigned char in[64];
110 G_LOCK_DEFINE_STATIC (uid_cache);
111 static GHashTable *uid_cache = NULL;
113 G_LOCK_DEFINE_STATIC (gid_cache);
114 static GHashTable *gid_cache = NULL;
116 #endif /* !G_OS_WIN32 */
119 _g_local_file_info_create_etag (GLocalFileStat *statbuf)
123 sec = statbuf->st_mtime;
124 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
125 usec = statbuf->st_mtimensec / 1000;
126 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
127 usec = statbuf->st_mtim.tv_nsec / 1000;
132 return g_strdup_printf ("%lu:%lu", sec, usec);
136 _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
138 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
139 (guint64) statbuf->st_dev,
140 (guint64) statbuf->st_ino);
144 _g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
146 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
147 (guint64) statbuf->st_dev);
154 read_link (const gchar *full_name)
161 buffer = g_malloc (size);
167 read_size = readlink (full_name, buffer, size);
173 if (read_size < size)
175 buffer[read_size] = 0;
179 buffer = g_realloc (buffer, size);
189 /* Get the SELinux security context */
191 get_selinux_context (const char *path,
193 GFileAttributeMatcher *attribute_matcher,
194 gboolean follow_symlinks)
198 if (!_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT))
201 if (is_selinux_enabled ())
205 if (lgetfilecon_raw (path, &context) < 0)
210 if (getfilecon_raw (path, &context) < 0)
216 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
225 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
226 * (Mac) getxattr(..., XATTR_NOFOLLOW)
228 #ifdef HAVE_XATTR_NOFOLLOW
229 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
230 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
231 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
233 #define g_fgetxattr fgetxattr
234 #define g_flistxattr flistxattr
235 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
239 g_getxattr (const char *path, const char *name, void *value, size_t size,
240 gboolean follow_symlinks)
242 #ifdef HAVE_XATTR_NOFOLLOW
243 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
246 return getxattr (path, name, value, size);
248 return lgetxattr (path, name, value, size);
253 g_listxattr(const char *path, char *namebuf, size_t size,
254 gboolean follow_symlinks)
256 #ifdef HAVE_XATTR_NOFOLLOW
257 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
260 return listxattr (path, namebuf, size);
262 return llistxattr (path, namebuf, size);
269 return c >= 32 && c <= 126 && c != '\\';
273 name_is_valid (const char *str)
277 if (!valid_char (*str++))
284 hex_escape_string (const char *str,
285 gboolean *free_return)
288 char *escaped_str, *p;
290 static char *hex_digits = "0123456789abcdef";
296 for (i = 0; i < len; i++)
298 if (!valid_char (str[i]))
302 if (num_invalid == 0)
304 *free_return = FALSE;
308 escaped_str = g_malloc (len + num_invalid*3 + 1);
311 for (i = 0; i < len; i++)
313 if (valid_char (str[i]))
320 *p++ = hex_digits[(c >> 4) & 0xf];
321 *p++ = hex_digits[c & 0xf];
331 hex_unescape_string (const char *str,
333 gboolean *free_return)
336 char *unescaped_str, *p;
342 if (strchr (str, '\\') == NULL)
346 *free_return = FALSE;
350 unescaped_str = g_malloc (len + 1);
353 for (i = 0; i < len; i++)
355 if (str[i] == '\\' &&
360 (g_ascii_xdigit_value (str[i+2]) << 4) |
361 g_ascii_xdigit_value (str[i+3]);
371 *out_len = p - unescaped_str;
373 return unescaped_str;
377 escape_xattr (GFileInfo *info,
378 const char *gio_attr, /* gio attribute name */
379 const char *value, /* Is zero terminated */
380 size_t len /* not including zero termination */)
383 gboolean free_escaped_val;
385 escaped_val = hex_escape_string (value, &free_escaped_val);
387 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
389 if (free_escaped_val)
390 g_free (escaped_val);
394 get_one_xattr (const char *path,
396 const char *gio_attr,
398 gboolean follow_symlinks)
404 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
409 else if (len == -1 && errno == ERANGE)
411 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
416 value_p = g_malloc (len+1);
418 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
432 escape_xattr (info, gio_attr, value_p, len);
434 if (value_p != value)
438 #endif /* defined HAVE_XATTR */
441 get_xattrs (const char *path,
444 GFileAttributeMatcher *matcher,
445 gboolean follow_symlinks)
450 ssize_t list_res_size;
453 const char *attr, *attr2;
456 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
458 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
462 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
464 if (list_res_size == -1 ||
468 list_size = list_res_size;
469 list = g_malloc (list_size);
473 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
475 if (list_res_size == -1 && errno == ERANGE)
477 list_size = list_size * 2;
478 list = g_realloc (list, list_size);
482 if (list_res_size == -1)
486 while (list_res_size > 0)
488 if ((user && g_str_has_prefix (attr, "user.")) ||
489 (!user && !g_str_has_prefix (attr, "user.")))
491 char *escaped_attr, *gio_attr;
492 gboolean free_escaped_attr;
496 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
497 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
501 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
502 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
505 if (free_escaped_attr)
506 g_free (escaped_attr);
508 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
513 len = strlen (attr) + 1;
515 list_res_size -= len;
522 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
524 char *unescaped_attribute, *a;
525 gboolean free_unescaped_attribute;
527 attr2 = strchr (attr, ':');
530 attr2 += 2; /* Skip '::' */
531 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
533 a = g_strconcat ("user.", unescaped_attribute, NULL);
535 a = unescaped_attribute;
537 get_one_xattr (path, info, attr, a, follow_symlinks);
542 if (free_unescaped_attribute)
543 g_free (unescaped_attribute);
547 #endif /* defined HAVE_XATTR */
552 get_one_xattr_from_fd (int fd,
554 const char *gio_attr,
561 len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
566 else if (len == -1 && errno == ERANGE)
568 len = g_fgetxattr (fd, xattr, NULL, 0);
573 value_p = g_malloc (len + 1);
575 len = g_fgetxattr (fd, xattr, value_p, len);
589 escape_xattr (info, gio_attr, value_p, len);
591 if (value_p != value)
594 #endif /* defined HAVE_XATTR */
597 get_xattrs_from_fd (int fd,
600 GFileAttributeMatcher *matcher)
605 ssize_t list_res_size;
608 const char *attr, *attr2;
611 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
613 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
617 list_res_size = g_flistxattr (fd, NULL, 0);
619 if (list_res_size == -1 ||
623 list_size = list_res_size;
624 list = g_malloc (list_size);
628 list_res_size = g_flistxattr (fd, list, list_size);
630 if (list_res_size == -1 && errno == ERANGE)
632 list_size = list_size * 2;
633 list = g_realloc (list, list_size);
637 if (list_res_size == -1)
641 while (list_res_size > 0)
643 if ((user && g_str_has_prefix (attr, "user.")) ||
644 (!user && !g_str_has_prefix (attr, "user.")))
646 char *escaped_attr, *gio_attr;
647 gboolean free_escaped_attr;
651 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
652 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
656 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
657 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
660 if (free_escaped_attr)
661 g_free (escaped_attr);
663 get_one_xattr_from_fd (fd, info, gio_attr, attr);
666 len = strlen (attr) + 1;
668 list_res_size -= len;
675 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
677 char *unescaped_attribute, *a;
678 gboolean free_unescaped_attribute;
680 attr2 = strchr (attr, ':');
683 attr2++; /* Skip ':' */
684 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
686 a = g_strconcat ("user.", unescaped_attribute, NULL);
688 a = unescaped_attribute;
690 get_one_xattr_from_fd (fd, info, attr, a);
695 if (free_unescaped_attribute)
696 g_free (unescaped_attribute);
700 #endif /* defined HAVE_XATTR */
705 set_xattr (char *filename,
706 const char *escaped_attribute,
707 const GFileAttributeValue *attr_value,
710 char *attribute, *value;
711 gboolean free_attribute, free_value;
712 int val_len, res, errsv;
716 if (attr_value == NULL)
718 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
719 _("Attribute value must be non-NULL"));
723 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
725 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
726 _("Invalid attribute type (string expected)"));
730 if (!name_is_valid (escaped_attribute))
732 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
733 _("Invalid extended attribute name"));
737 if (g_str_has_prefix (escaped_attribute, "xattr::"))
739 escaped_attribute += strlen ("xattr::");
744 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
745 escaped_attribute += strlen ("xattr-sys::");
749 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
750 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
753 a = g_strconcat ("user.", attribute, NULL);
757 res = g_setxattr (filename, a, value, val_len);
771 g_set_error (error, G_IO_ERROR,
772 g_io_error_from_errno (errsv),
773 _("Error setting extended attribute '%s': %s"),
774 escaped_attribute, g_strerror (errsv));
785 _g_local_file_info_get_parent_info (const char *dir,
786 GFileAttributeMatcher *attribute_matcher,
787 GLocalParentFileInfo *parent_info)
792 parent_info->extra_data = NULL;
793 parent_info->free_extra_data = NULL;
794 parent_info->writable = FALSE;
795 parent_info->is_sticky = FALSE;
796 parent_info->has_trash_dir = FALSE;
797 parent_info->device = 0;
799 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME) ||
800 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE) ||
801 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH) ||
802 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT))
804 /* FIXME: Windows: The underlying _waccess() call in the C
805 * library is mostly pointless as it only looks at the READONLY
806 * FAT-style attribute of the file, it doesn't check the ACL at
809 parent_info->writable = (g_access (dir, W_OK) == 0);
811 res = g_stat (dir, &statbuf);
814 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
815 * renamed or deleted only by the owner of the file, by the owner of the directory, and
816 * by a privileged process.
821 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
823 parent_info->is_sticky = FALSE;
825 parent_info->owner = statbuf.st_uid;
826 parent_info->device = statbuf.st_dev;
827 /* No need to find trash dir if it's not writable anyway */
828 if (parent_info->writable &&
829 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
830 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
836 _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info)
838 if (parent_info->extra_data &&
839 parent_info->free_extra_data)
840 parent_info->free_extra_data (parent_info->extra_data);
844 get_access_rights (GFileAttributeMatcher *attribute_matcher,
847 GLocalFileStat *statbuf,
848 GLocalParentFileInfo *parent_info)
850 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
851 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
852 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ))
853 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ,
854 g_access (path, R_OK) == 0);
856 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
857 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE))
858 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE,
859 g_access (path, W_OK) == 0);
861 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
862 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE))
863 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE,
864 g_access (path, X_OK) == 0);
872 if (parent_info->writable)
874 if (parent_info->is_sticky)
877 uid_t uid = geteuid ();
879 if (uid == statbuf->st_uid ||
880 uid == parent_info->owner ||
889 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME))
890 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME,
893 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE))
894 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE,
897 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
898 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH,
899 writable && parent_info->has_trash_dir);
904 set_info_from_stat (GFileInfo *info,
905 GLocalFileStat *statbuf,
906 GFileAttributeMatcher *attribute_matcher)
910 file_type = G_FILE_TYPE_UNKNOWN;
912 if (S_ISREG (statbuf->st_mode))
913 file_type = G_FILE_TYPE_REGULAR;
914 else if (S_ISDIR (statbuf->st_mode))
915 file_type = G_FILE_TYPE_DIRECTORY;
917 else if (S_ISCHR (statbuf->st_mode) ||
918 S_ISBLK (statbuf->st_mode) ||
919 S_ISFIFO (statbuf->st_mode)
921 || S_ISSOCK (statbuf->st_mode)
924 file_type = G_FILE_TYPE_SPECIAL;
927 else if (S_ISLNK (statbuf->st_mode))
928 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
931 g_file_info_set_file_type (info, file_type);
932 g_file_info_set_size (info, statbuf->st_size);
934 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
936 /* Pointless setting these on Windows even if they exist in the struct */
937 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
938 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
939 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
940 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
941 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
943 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
944 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
945 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
946 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
948 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
949 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
950 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
951 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
954 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
955 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
956 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
957 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
958 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
961 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
962 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
963 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
964 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
965 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
968 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
969 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
970 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
971 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
972 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
975 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
976 G_FILE_ATTRIBUTE_ID_ETAG_VALUE))
978 char *etag = _g_local_file_info_create_etag (statbuf);
979 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ETAG_VALUE, etag);
983 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
984 G_FILE_ATTRIBUTE_ID_ID_FILE))
986 char *id = _g_local_file_info_create_file_id (statbuf);
987 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILE, id);
991 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
992 G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM))
994 char *id = _g_local_file_info_create_fs_id (statbuf);
995 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM, id);
1003 make_valid_utf8 (const char *name)
1006 const gchar *remainder, *invalid;
1007 gint remaining_bytes, valid_bytes;
1011 remaining_bytes = strlen (name);
1013 while (remaining_bytes != 0)
1015 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1017 valid_bytes = invalid - remainder;
1020 string = g_string_sized_new (remaining_bytes);
1022 g_string_append_len (string, remainder, valid_bytes);
1023 /* append U+FFFD REPLACEMENT CHARACTER */
1024 g_string_append (string, "\357\277\275");
1026 remaining_bytes -= valid_bytes + 1;
1027 remainder = invalid + 1;
1031 return g_strdup (name);
1033 g_string_append (string, remainder);
1035 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1037 return g_string_free (string, FALSE);
1041 convert_pwd_string_to_utf8 (char *pwd_str)
1045 if (!g_utf8_validate (pwd_str, -1, NULL))
1047 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1048 if (utf8_string == NULL)
1049 utf8_string = make_valid_utf8 (pwd_str);
1052 utf8_string = g_strdup (pwd_str);
1058 uid_data_free (UidData *data)
1060 g_free (data->user_name);
1061 g_free (data->real_name);
1065 /* called with lock held */
1067 lookup_uid_data (uid_t uid)
1071 struct passwd pwbuf;
1072 struct passwd *pwbufp;
1073 char *gecos, *comma;
1075 if (uid_cache == NULL)
1076 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1078 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1083 data = g_new0 (UidData, 1);
1085 #if defined(HAVE_POSIX_GETPWUID_R)
1086 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1087 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1088 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1090 pwbufp = getpwuid (uid);
1095 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1096 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1098 gecos = pwbufp->pw_gecos;
1102 comma = strchr (gecos, ',');
1105 data->real_name = convert_pwd_string_to_utf8 (gecos);
1109 /* Default fallbacks */
1110 if (data->real_name == NULL)
1112 if (data->user_name != NULL)
1113 data->real_name = g_strdup (data->user_name);
1115 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1118 if (data->user_name == NULL)
1119 data->user_name = g_strdup_printf ("%d", (int)uid);
1121 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1127 get_username_from_uid (uid_t uid)
1133 data = lookup_uid_data (uid);
1134 res = g_strdup (data->user_name);
1135 G_UNLOCK (uid_cache);
1141 get_realname_from_uid (uid_t uid)
1147 data = lookup_uid_data (uid);
1148 res = g_strdup (data->real_name);
1149 G_UNLOCK (uid_cache);
1154 /* called with lock held */
1156 lookup_gid_name (gid_t gid)
1161 struct group *gbufp;
1163 if (gid_cache == NULL)
1164 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1166 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1171 #if defined (HAVE_POSIX_GETGRGID_R)
1172 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1173 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1174 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1176 gbufp = getgrgid (gid);
1179 if (gbufp != NULL &&
1180 gbufp->gr_name != NULL &&
1181 gbufp->gr_name[0] != 0)
1182 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1184 name = g_strdup_printf("%d", (int)gid);
1186 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1192 get_groupname_from_gid (gid_t gid)
1198 name = lookup_gid_name (gid);
1199 res = g_strdup (name);
1200 G_UNLOCK (gid_cache);
1204 #endif /* !G_OS_WIN32 */
1207 get_content_type (const char *basename,
1209 GLocalFileStat *statbuf,
1210 gboolean is_symlink,
1211 gboolean symlink_broken,
1212 GFileQueryInfoFlags flags,
1216 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1217 return g_strdup ("inode/symlink");
1218 else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
1219 return g_strdup ("inode/directory");
1221 else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
1222 return g_strdup ("inode/chardevice");
1223 else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
1224 return g_strdup ("inode/blockdevice");
1225 else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
1226 return g_strdup ("inode/fifo");
1229 else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
1230 return g_strdup ("inode/socket");
1235 gboolean result_uncertain;
1237 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1240 if (!fast && result_uncertain && path != NULL)
1242 guchar sniff_buffer[4096];
1246 sniff_length = _g_unix_content_type_get_sniff_len ();
1247 if (sniff_length > 4096)
1248 sniff_length = 4096;
1251 fd = open (path, O_RDONLY | O_NOATIME);
1252 if (fd < 0 && errno == EPERM)
1254 fd = open (path, O_RDONLY);
1260 res = read (fd, sniff_buffer, sniff_length);
1264 g_free (content_type);
1265 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1271 return content_type;
1277 get_thumbnail_attributes (const char *path,
1280 GChecksum *checksum;
1285 uri = g_filename_to_uri (path, NULL, NULL);
1287 checksum = g_checksum_new (G_CHECKSUM_MD5);
1288 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1292 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1293 g_checksum_free (checksum);
1295 filename = g_build_filename (g_get_home_dir (),
1296 ".thumbnails", "normal", basename,
1299 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1300 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1304 filename = g_build_filename (g_get_home_dir (),
1305 ".thumbnails", "fail",
1306 "gnome-thumbnail-factory",
1310 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1311 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1319 win32_get_file_user_info (const gchar *filename,
1324 PSECURITY_DESCRIPTOR psd = NULL;
1325 DWORD sd_size = 0; /* first call calculates the size required */
1327 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1328 if ((GetFileSecurityW (wfilename,
1329 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1332 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1333 (psd = g_try_malloc (sd_size)) != NULL &&
1334 GetFileSecurityW (wfilename,
1335 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1342 SID_NAME_USE name_use = 0; /* don't care? */
1343 wchar_t *name = NULL;
1344 wchar_t *domain = NULL;
1346 DWORD domain_len = 0;
1347 /* get the user name */
1351 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1353 if (!LookupAccountSidW (NULL, /* local machine */
1356 domain, &domain_len, /* no domain info yet */
1357 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1359 name = g_try_malloc (name_len * sizeof (wchar_t));
1360 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1361 if (name && domain &&
1362 LookupAccountSidW (NULL, /* local machine */
1365 domain, &domain_len, /* no domain info yet */
1368 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1374 /* get the group name */
1378 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1380 if (!LookupAccountSidW (NULL, /* local machine */
1383 domain, &domain_len, /* no domain info yet */
1384 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1386 name = g_try_malloc (name_len * sizeof (wchar_t));
1387 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1388 if (name && domain &&
1389 LookupAccountSidW (NULL, /* local machine */
1392 domain, &domain_len, /* no domain info yet */
1395 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1401 /* TODO: get real name */
1407 #endif /* G_OS_WIN32 */
1410 _g_local_file_info_get (const char *basename,
1412 GFileAttributeMatcher *attribute_matcher,
1413 GFileQueryInfoFlags flags,
1414 GLocalParentFileInfo *parent_info,
1418 GLocalFileStat statbuf;
1420 struct stat statbuf2;
1424 gboolean is_symlink, symlink_broken;
1426 DWORD dos_attributes;
1428 char *symlink_target;
1433 info = g_file_info_new ();
1435 /* Make sure we don't set any unwanted attributes */
1436 g_file_info_set_attribute_mask (info, attribute_matcher);
1438 g_file_info_set_name (info, basename);
1440 /* Avoid stat in trivial case */
1441 if (attribute_matcher == NULL)
1443 g_file_info_unset_attribute_mask (info);
1448 res = g_lstat (path, &statbuf);
1451 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1456 g_object_unref (info);
1460 len = wcslen (wpath);
1461 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1464 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1467 res = _wstati64 (wpath, &statbuf);
1468 dos_attributes = GetFileAttributesW (wpath);
1478 /* Don't bail out if we get Permission denied (SELinux?) */
1479 if (errsv != EACCES)
1481 char *display_name = g_filename_display_name (path);
1482 g_object_unref (info);
1483 g_set_error (error, G_IO_ERROR,
1484 g_io_error_from_errno (errsv),
1485 _("Error when getting information for file '%s': %s"),
1486 display_name, g_strerror (errsv));
1487 g_free (display_name);
1492 /* Even if stat() fails, try to get as much as other attributes possible */
1493 stat_ok = res != -1;
1496 device = statbuf.st_dev;
1501 is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
1505 symlink_broken = FALSE;
1509 g_file_info_set_is_symlink (info, TRUE);
1511 /* Unless NOFOLLOW was set we default to following symlinks */
1512 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1514 res = stat (path, &statbuf2);
1516 /* Report broken links as symlinks */
1523 symlink_broken = TRUE;
1529 set_info_from_stat (info, &statbuf, attribute_matcher);
1532 if (basename != NULL && basename[0] == '.')
1533 g_file_info_set_is_hidden (info, TRUE);
1535 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1536 (stat_ok && S_ISREG (statbuf.st_mode)))
1537 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1539 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1540 g_file_info_set_is_hidden (info, TRUE);
1542 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1543 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1545 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1546 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1549 symlink_target = NULL;
1553 symlink_target = read_link (path);
1554 if (symlink_target &&
1555 _g_file_attribute_matcher_matches_id (attribute_matcher,
1556 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1557 g_file_info_set_symlink_target (info, symlink_target);
1560 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1561 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1563 char *display_name = g_filename_display_basename (path);
1565 /* look for U+FFFD REPLACEMENT CHARACTER */
1566 if (strstr (display_name, "\357\277\275") != NULL)
1568 char *p = display_name;
1569 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1572 g_file_info_set_display_name (info, display_name);
1573 g_free (display_name);
1576 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1577 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1579 char *edit_name = g_filename_display_basename (path);
1580 g_file_info_set_edit_name (info, edit_name);
1585 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1586 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1588 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1590 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1594 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1595 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1596 _g_file_attribute_matcher_matches_id (attribute_matcher,
1597 G_FILE_ATTRIBUTE_ID_STANDARD_ICON))
1599 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
1603 g_file_info_set_content_type (info, content_type);
1605 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1606 G_FILE_ATTRIBUTE_ID_STANDARD_ICON))
1610 if (strcmp (path, g_get_home_dir ()) == 0)
1611 icon = g_themed_icon_new ("user-home");
1612 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1613 icon = g_themed_icon_new ("user-desktop");
1614 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1615 icon = g_themed_icon_new_with_default_fallbacks ("folder-documents");
1616 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1617 icon = g_themed_icon_new_with_default_fallbacks ("folder-download");
1618 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1619 icon = g_themed_icon_new_with_default_fallbacks ("folder-music");
1620 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1621 icon = g_themed_icon_new_with_default_fallbacks ("folder-pictures");
1622 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1623 icon = g_themed_icon_new_with_default_fallbacks ("folder-publicshare");
1624 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1625 icon = g_themed_icon_new_with_default_fallbacks ("folder-templates");
1626 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1627 icon = g_themed_icon_new_with_default_fallbacks ("folder-videos");
1630 icon = g_content_type_get_icon (content_type);
1631 if (G_IS_THEMED_ICON (icon))
1633 const char *type_icon = NULL;
1635 if (S_ISDIR (statbuf.st_mode))
1636 type_icon = "folder";
1638 g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
1644 g_file_info_set_icon (info, icon);
1645 g_object_unref (icon);
1649 g_free (content_type);
1653 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1654 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1656 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
1660 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1661 g_free (content_type);
1665 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1666 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1671 win32_get_file_user_info (path, NULL, &name, NULL);
1674 name = get_username_from_uid (statbuf.st_uid);
1677 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1681 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1682 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1686 win32_get_file_user_info (path, NULL, NULL, &name);
1689 name = get_realname_from_uid (statbuf.st_uid);
1692 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1696 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1697 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1701 win32_get_file_user_info (path, &name, NULL, NULL);
1704 name = get_groupname_from_gid (statbuf.st_gid);
1707 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1711 if (stat_ok && parent_info && parent_info->device != 0 &&
1712 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1713 statbuf.st_dev != parent_info->device)
1714 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1717 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1720 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1722 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1723 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1725 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1726 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH))
1727 get_thumbnail_attributes (path, info);
1729 vfs = g_vfs_get_default ();
1730 class = G_VFS_GET_CLASS (vfs);
1731 if (class->local_file_add_info)
1733 class->local_file_add_info (vfs,
1739 &parent_info->extra_data,
1740 &parent_info->free_extra_data);
1743 g_file_info_unset_attribute_mask (info);
1745 g_free (symlink_target);
1751 _g_local_file_info_get_from_fd (int fd,
1752 const char *attributes,
1755 GLocalFileStat stat_buf;
1756 GFileAttributeMatcher *matcher;
1760 #define FSTAT _fstati64
1765 if (FSTAT (fd, &stat_buf) == -1)
1769 g_set_error (error, G_IO_ERROR,
1770 g_io_error_from_errno (errsv),
1771 _("Error when getting information for file descriptor: %s"),
1772 g_strerror (errsv));
1776 info = g_file_info_new ();
1778 matcher = g_file_attribute_matcher_new (attributes);
1780 /* Make sure we don't set any unwanted attributes */
1781 g_file_info_set_attribute_mask (info, matcher);
1783 set_info_from_stat (info, &stat_buf, matcher);
1786 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
1787 is_selinux_enabled ())
1790 if (fgetfilecon_raw (fd, &context) >= 0)
1792 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
1798 get_xattrs_from_fd (fd, TRUE, info, matcher);
1799 get_xattrs_from_fd (fd, FALSE, info, matcher);
1801 g_file_attribute_matcher_unref (matcher);
1803 g_file_info_unset_attribute_mask (info);
1809 get_uint32 (const GFileAttributeValue *value,
1813 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1815 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1816 _("Invalid attribute type (uint32 expected)"));
1820 *val_out = value->u.uint32;
1827 get_uint64 (const GFileAttributeValue *value,
1831 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1833 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1834 _("Invalid attribute type (uint64 expected)"));
1838 *val_out = value->u.uint64;
1844 #if defined(HAVE_SYMLINK)
1846 get_byte_string (const GFileAttributeValue *value,
1847 const char **val_out,
1850 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1852 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1853 _("Invalid attribute type (byte string expected)"));
1857 *val_out = value->u.string;
1865 get_string (const GFileAttributeValue *value,
1866 const char **val_out,
1869 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
1871 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1872 _("Invalid attribute type (byte string expected)"));
1876 *val_out = value->u.string;
1883 set_unix_mode (char *filename,
1884 GFileQueryInfoFlags flags,
1885 const GFileAttributeValue *value,
1891 if (!get_uint32 (value, &val, error))
1895 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
1897 res = lchmod (filename, val);
1899 struct stat statbuf;
1900 /* Calling chmod on a symlink changes permissions on the symlink.
1901 * We don't want to do this, so we need to check for a symlink */
1902 res = g_lstat (filename, &statbuf);
1903 if (res == 0 && S_ISLNK (statbuf.st_mode))
1905 g_set_error_literal (error, G_IO_ERROR,
1906 G_IO_ERROR_NOT_SUPPORTED,
1907 _("Cannot set permissions on symlinks"));
1911 res = g_chmod (filename, val);
1915 res = g_chmod (filename, val);
1921 g_set_error (error, G_IO_ERROR,
1922 g_io_error_from_errno (errsv),
1923 _("Error setting permissions: %s"),
1924 g_strerror (errsv));
1932 set_unix_uid_gid (char *filename,
1933 const GFileAttributeValue *uid_value,
1934 const GFileAttributeValue *gid_value,
1935 GFileQueryInfoFlags flags,
1945 if (!get_uint32 (uid_value, &val, error))
1954 if (!get_uint32 (gid_value, &val, error))
1962 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1963 res = lchown (filename, uid, gid);
1966 res = chown (filename, uid, gid);
1972 g_set_error (error, G_IO_ERROR,
1973 g_io_error_from_errno (errsv),
1974 _("Error setting owner: %s"),
1975 g_strerror (errsv));
1984 set_symlink (char *filename,
1985 const GFileAttributeValue *value,
1989 struct stat statbuf;
1991 if (!get_byte_string (value, &val, error))
1996 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1997 _("symlink must be non-NULL"));
2001 if (g_lstat (filename, &statbuf))
2005 g_set_error (error, G_IO_ERROR,
2006 g_io_error_from_errno (errsv),
2007 _("Error setting symlink: %s"),
2008 g_strerror (errsv));
2012 if (!S_ISLNK (statbuf.st_mode))
2014 g_set_error_literal (error, G_IO_ERROR,
2015 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2016 _("Error setting symlink: file is not a symlink"));
2020 if (g_unlink (filename))
2024 g_set_error (error, G_IO_ERROR,
2025 g_io_error_from_errno (errsv),
2026 _("Error setting symlink: %s"),
2027 g_strerror (errsv));
2031 if (symlink (filename, val) != 0)
2035 g_set_error (error, G_IO_ERROR,
2036 g_io_error_from_errno (errsv),
2037 _("Error setting symlink: %s"),
2038 g_strerror (errsv));
2048 lazy_stat (char *filename,
2049 struct stat *statbuf,
2050 gboolean *called_stat)
2057 res = g_stat (filename, statbuf);
2060 *called_stat = TRUE;
2067 set_mtime_atime (char *filename,
2068 const GFileAttributeValue *mtime_value,
2069 const GFileAttributeValue *mtime_usec_value,
2070 const GFileAttributeValue *atime_value,
2071 const GFileAttributeValue *atime_usec_value,
2076 guint32 val_usec = 0;
2077 struct stat statbuf;
2078 gboolean got_stat = FALSE;
2079 struct timeval times[2] = { {0, 0}, {0, 0} };
2084 if (!get_uint64 (atime_value, &val, error))
2086 times[0].tv_sec = val;
2090 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2092 times[0].tv_sec = statbuf.st_mtime;
2093 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2094 times[0].tv_usec = statbuf.st_atimensec / 1000;
2095 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2096 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2101 if (atime_usec_value)
2103 if (!get_uint32 (atime_usec_value, &val_usec, error))
2105 times[0].tv_usec = val_usec;
2111 if (!get_uint64 (mtime_value, &val, error))
2113 times[1].tv_sec = val;
2117 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2119 times[1].tv_sec = statbuf.st_mtime;
2120 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2121 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2122 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2123 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2128 if (mtime_usec_value)
2130 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2132 times[1].tv_usec = val_usec;
2135 res = utimes (filename, times);
2140 g_set_error (error, G_IO_ERROR,
2141 g_io_error_from_errno (errsv),
2142 _("Error setting modification or access time: %s"),
2143 g_strerror (errsv));
2153 set_selinux_context (char *filename,
2154 const GFileAttributeValue *value,
2159 if (!get_string (value, &val, error))
2164 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2165 _("SELinux context must be non-NULL"));
2169 if (is_selinux_enabled ()) {
2170 security_context_t val_s;
2172 val_s = g_strdup (val);
2174 if (setfilecon_raw (filename, val_s) < 0)
2178 g_set_error (error, G_IO_ERROR,
2179 g_io_error_from_errno (errsv),
2180 _("Error setting SELinux context: %s"),
2181 g_strerror (errsv));
2186 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2187 _("SELinux is not enabled on this system"));
2197 _g_local_file_info_set_attribute (char *filename,
2198 const char *attribute,
2199 GFileAttributeType type,
2201 GFileQueryInfoFlags flags,
2202 GCancellable *cancellable,
2205 GFileAttributeValue value = { 0 };
2209 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2211 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2212 return set_unix_mode (filename, flags, &value, error);
2215 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2216 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2217 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2218 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2222 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2223 return set_symlink (filename, &value, error);
2227 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2228 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2229 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2230 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2231 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2232 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2233 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2234 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2238 else if (g_str_has_prefix (attribute, "xattr::"))
2239 return set_xattr (filename, attribute, &value, error);
2240 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2241 return set_xattr (filename, attribute, &value, error);
2245 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2246 return set_selinux_context (filename, &value, error);
2249 vfs = g_vfs_get_default ();
2250 class = G_VFS_GET_CLASS (vfs);
2251 if (class->local_file_set_attributes)
2255 info = g_file_info_new ();
2256 g_file_info_set_attribute (info,
2260 if (!class->local_file_set_attributes (vfs, filename,
2265 g_object_unref (info);
2269 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2271 g_object_unref (info);
2275 g_object_unref (info);
2278 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2279 _("Setting attribute %s not supported"), attribute);
2284 _g_local_file_info_set_attributes (char *filename,
2286 GFileQueryInfoFlags flags,
2287 GCancellable *cancellable,
2290 GFileAttributeValue *value;
2292 GFileAttributeValue *uid, *gid;
2295 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2297 #if defined (HAVE_CHOWN) || defined (HAVE_UTIMES)
2298 GFileAttributeStatus status;
2304 /* Handles setting multiple specified data in a single set, and takes care
2305 of ordering restrictions when setting attributes */
2309 /* Set symlink first, since this recreates the file */
2311 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2314 if (!set_symlink (filename, value, error))
2316 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2318 /* Don't set error multiple times */
2322 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2328 /* Group uid and gid setting into one call
2329 * Change ownership before permissions, since ownership changes can
2330 change permissions (e.g. setuid)
2332 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2333 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2337 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2339 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2341 /* Don't set error multiple times */
2345 status = G_FILE_ATTRIBUTE_STATUS_SET;
2347 uid->status = status;
2349 gid->status = status;
2353 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2356 if (!set_unix_mode (filename, flags, value, error))
2358 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2360 /* Don't set error multiple times */
2364 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2369 /* Group all time settings into one call
2370 * Change times as the last thing to avoid it changing due to metadata changes
2373 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2374 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2375 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2376 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2378 if (mtime || mtime_usec || atime || atime_usec)
2380 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2382 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2384 /* Don't set error multiple times */
2388 status = G_FILE_ATTRIBUTE_STATUS_SET;
2391 mtime->status = status;
2393 mtime_usec->status = status;
2395 atime->status = status;
2397 atime_usec->status = status;
2401 /* xattrs are handled by default callback */
2404 /* SELinux context */
2406 if (is_selinux_enabled ()) {
2407 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2410 if (!set_selinux_context (filename, value, error))
2412 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2414 /* Don't set error multiple times */
2418 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2423 vfs = g_vfs_get_default ();
2424 class = G_VFS_GET_CLASS (vfs);
2425 if (class->local_file_set_attributes)
2427 if (!class->local_file_set_attributes (vfs, filename,
2433 /* Don't set error multiple times */