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 "gcontenttypeprivate.h"
96 struct ThumbMD5Context {
109 G_LOCK_DEFINE_STATIC (uid_cache);
110 static GHashTable *uid_cache = NULL;
112 G_LOCK_DEFINE_STATIC (gid_cache);
113 static GHashTable *gid_cache = NULL;
115 #endif /* !G_OS_WIN32 */
118 _g_local_file_info_create_etag (GLocalFileStat *statbuf)
122 sec = statbuf->st_mtime;
123 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
124 usec = statbuf->st_mtimensec / 1000;
125 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
126 usec = statbuf->st_mtim.tv_nsec / 1000;
131 return g_strdup_printf ("%lu:%lu", sec, usec);
135 _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
137 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
138 (guint64) statbuf->st_dev,
139 (guint64) statbuf->st_ino);
143 _g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
145 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
146 (guint64) statbuf->st_dev);
153 read_link (const gchar *full_name)
160 buffer = g_malloc (size);
166 read_size = readlink (full_name, buffer, size);
172 if (read_size < size)
174 buffer[read_size] = 0;
178 buffer = g_realloc (buffer, size);
188 /* Get the SELinux security context */
190 get_selinux_context (const char *path,
192 GFileAttributeMatcher *attribute_matcher,
193 gboolean follow_symlinks)
197 if (!_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT))
200 if (is_selinux_enabled ())
204 if (lgetfilecon_raw (path, &context) < 0)
209 if (getfilecon_raw (path, &context) < 0)
215 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
224 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
225 * (Mac) getxattr(..., XATTR_NOFOLLOW)
227 #ifdef HAVE_XATTR_NOFOLLOW
228 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
229 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
230 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
232 #define g_fgetxattr fgetxattr
233 #define g_flistxattr flistxattr
234 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
238 g_getxattr (const char *path, const char *name, void *value, size_t size,
239 gboolean follow_symlinks)
241 #ifdef HAVE_XATTR_NOFOLLOW
242 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
245 return getxattr (path, name, value, size);
247 return lgetxattr (path, name, value, size);
252 g_listxattr(const char *path, char *namebuf, size_t size,
253 gboolean follow_symlinks)
255 #ifdef HAVE_XATTR_NOFOLLOW
256 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
259 return listxattr (path, namebuf, size);
261 return llistxattr (path, namebuf, size);
268 return c >= 32 && c <= 126 && c != '\\';
272 name_is_valid (const char *str)
276 if (!valid_char (*str++))
283 hex_escape_string (const char *str,
284 gboolean *free_return)
287 char *escaped_str, *p;
289 static char *hex_digits = "0123456789abcdef";
295 for (i = 0; i < len; i++)
297 if (!valid_char (str[i]))
301 if (num_invalid == 0)
303 *free_return = FALSE;
307 escaped_str = g_malloc (len + num_invalid*3 + 1);
310 for (i = 0; i < len; i++)
312 if (valid_char (str[i]))
319 *p++ = hex_digits[(c >> 4) & 0xf];
320 *p++ = hex_digits[c & 0xf];
330 hex_unescape_string (const char *str,
332 gboolean *free_return)
335 char *unescaped_str, *p;
341 if (strchr (str, '\\') == NULL)
345 *free_return = FALSE;
349 unescaped_str = g_malloc (len + 1);
352 for (i = 0; i < len; i++)
354 if (str[i] == '\\' &&
359 (g_ascii_xdigit_value (str[i+2]) << 4) |
360 g_ascii_xdigit_value (str[i+3]);
370 *out_len = p - unescaped_str;
372 return unescaped_str;
376 escape_xattr (GFileInfo *info,
377 const char *gio_attr, /* gio attribute name */
378 const char *value, /* Is zero terminated */
379 size_t len /* not including zero termination */)
382 gboolean free_escaped_val;
384 escaped_val = hex_escape_string (value, &free_escaped_val);
386 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
388 if (free_escaped_val)
389 g_free (escaped_val);
393 get_one_xattr (const char *path,
395 const char *gio_attr,
397 gboolean follow_symlinks)
403 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
408 else if (len == -1 && errno == ERANGE)
410 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
415 value_p = g_malloc (len+1);
417 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
431 escape_xattr (info, gio_attr, value_p, len);
433 if (value_p != value)
437 #endif /* defined HAVE_XATTR */
440 get_xattrs (const char *path,
443 GFileAttributeMatcher *matcher,
444 gboolean follow_symlinks)
449 ssize_t list_res_size;
452 const char *attr, *attr2;
455 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
457 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
461 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
463 if (list_res_size == -1 ||
467 list_size = list_res_size;
468 list = g_malloc (list_size);
472 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
474 if (list_res_size == -1 && errno == ERANGE)
476 list_size = list_size * 2;
477 list = g_realloc (list, list_size);
481 if (list_res_size == -1)
485 while (list_res_size > 0)
487 if ((user && g_str_has_prefix (attr, "user.")) ||
488 (!user && !g_str_has_prefix (attr, "user.")))
490 char *escaped_attr, *gio_attr;
491 gboolean free_escaped_attr;
495 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
496 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
500 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
501 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
504 if (free_escaped_attr)
505 g_free (escaped_attr);
507 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
512 len = strlen (attr) + 1;
514 list_res_size -= len;
521 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
523 char *unescaped_attribute, *a;
524 gboolean free_unescaped_attribute;
526 attr2 = strchr (attr, ':');
529 attr2 += 2; /* Skip '::' */
530 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
532 a = g_strconcat ("user.", unescaped_attribute, NULL);
534 a = unescaped_attribute;
536 get_one_xattr (path, info, attr, a, follow_symlinks);
541 if (free_unescaped_attribute)
542 g_free (unescaped_attribute);
546 #endif /* defined HAVE_XATTR */
551 get_one_xattr_from_fd (int fd,
553 const char *gio_attr,
560 len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
565 else if (len == -1 && errno == ERANGE)
567 len = g_fgetxattr (fd, xattr, NULL, 0);
572 value_p = g_malloc (len + 1);
574 len = g_fgetxattr (fd, xattr, value_p, len);
588 escape_xattr (info, gio_attr, value_p, len);
590 if (value_p != value)
593 #endif /* defined HAVE_XATTR */
596 get_xattrs_from_fd (int fd,
599 GFileAttributeMatcher *matcher)
604 ssize_t list_res_size;
607 const char *attr, *attr2;
610 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
612 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
616 list_res_size = g_flistxattr (fd, NULL, 0);
618 if (list_res_size == -1 ||
622 list_size = list_res_size;
623 list = g_malloc (list_size);
627 list_res_size = g_flistxattr (fd, list, list_size);
629 if (list_res_size == -1 && errno == ERANGE)
631 list_size = list_size * 2;
632 list = g_realloc (list, list_size);
636 if (list_res_size == -1)
640 while (list_res_size > 0)
642 if ((user && g_str_has_prefix (attr, "user.")) ||
643 (!user && !g_str_has_prefix (attr, "user.")))
645 char *escaped_attr, *gio_attr;
646 gboolean free_escaped_attr;
650 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
651 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
655 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
656 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
659 if (free_escaped_attr)
660 g_free (escaped_attr);
662 get_one_xattr_from_fd (fd, info, gio_attr, attr);
665 len = strlen (attr) + 1;
667 list_res_size -= len;
674 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
676 char *unescaped_attribute, *a;
677 gboolean free_unescaped_attribute;
679 attr2 = strchr (attr, ':');
682 attr2++; /* Skip ':' */
683 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
685 a = g_strconcat ("user.", unescaped_attribute, NULL);
687 a = unescaped_attribute;
689 get_one_xattr_from_fd (fd, info, attr, a);
694 if (free_unescaped_attribute)
695 g_free (unescaped_attribute);
699 #endif /* defined HAVE_XATTR */
704 set_xattr (char *filename,
705 const char *escaped_attribute,
706 const GFileAttributeValue *attr_value,
709 char *attribute, *value;
710 gboolean free_attribute, free_value;
711 int val_len, res, errsv;
715 if (attr_value == NULL)
717 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
718 _("Attribute value must be non-NULL"));
722 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
724 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
725 _("Invalid attribute type (string expected)"));
729 if (!name_is_valid (escaped_attribute))
731 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
732 _("Invalid extended attribute name"));
736 if (g_str_has_prefix (escaped_attribute, "xattr::"))
738 escaped_attribute += strlen ("xattr::");
743 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
744 escaped_attribute += strlen ("xattr-sys::");
748 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
749 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
752 a = g_strconcat ("user.", attribute, NULL);
756 res = g_setxattr (filename, a, value, val_len);
770 g_set_error (error, G_IO_ERROR,
771 g_io_error_from_errno (errsv),
772 _("Error setting extended attribute '%s': %s"),
773 escaped_attribute, g_strerror (errsv));
784 _g_local_file_info_get_parent_info (const char *dir,
785 GFileAttributeMatcher *attribute_matcher,
786 GLocalParentFileInfo *parent_info)
791 parent_info->extra_data = NULL;
792 parent_info->free_extra_data = NULL;
793 parent_info->writable = FALSE;
794 parent_info->is_sticky = FALSE;
795 parent_info->has_trash_dir = FALSE;
796 parent_info->device = 0;
798 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME) ||
799 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE) ||
800 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH) ||
801 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT))
803 /* FIXME: Windows: The underlying _waccess() call in the C
804 * library is mostly pointless as it only looks at the READONLY
805 * FAT-style attribute of the file, it doesn't check the ACL at
808 parent_info->writable = (g_access (dir, W_OK) == 0);
810 res = g_stat (dir, &statbuf);
813 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
814 * renamed or deleted only by the owner of the file, by the owner of the directory, and
815 * by a privileged process.
820 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
822 parent_info->is_sticky = FALSE;
824 parent_info->owner = statbuf.st_uid;
825 parent_info->device = statbuf.st_dev;
826 /* No need to find trash dir if it's not writable anyway */
827 if (parent_info->writable &&
828 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
829 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
835 _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info)
837 if (parent_info->extra_data &&
838 parent_info->free_extra_data)
839 parent_info->free_extra_data (parent_info->extra_data);
843 get_access_rights (GFileAttributeMatcher *attribute_matcher,
846 GLocalFileStat *statbuf,
847 GLocalParentFileInfo *parent_info)
849 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
850 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
851 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ))
852 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ,
853 g_access (path, R_OK) == 0);
855 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
856 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE))
857 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE,
858 g_access (path, W_OK) == 0);
860 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
861 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE))
862 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE,
863 g_access (path, X_OK) == 0);
871 if (parent_info->writable)
873 if (parent_info->is_sticky)
876 uid_t uid = geteuid ();
878 if (uid == statbuf->st_uid ||
879 uid == parent_info->owner ||
888 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME))
889 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME,
892 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE))
893 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE,
896 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
897 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH,
898 writable && parent_info->has_trash_dir);
903 set_info_from_stat (GFileInfo *info,
904 GLocalFileStat *statbuf,
905 GFileAttributeMatcher *attribute_matcher)
909 file_type = G_FILE_TYPE_UNKNOWN;
911 if (S_ISREG (statbuf->st_mode))
912 file_type = G_FILE_TYPE_REGULAR;
913 else if (S_ISDIR (statbuf->st_mode))
914 file_type = G_FILE_TYPE_DIRECTORY;
916 else if (S_ISCHR (statbuf->st_mode) ||
917 S_ISBLK (statbuf->st_mode) ||
918 S_ISFIFO (statbuf->st_mode)
920 || S_ISSOCK (statbuf->st_mode)
923 file_type = G_FILE_TYPE_SPECIAL;
926 else if (S_ISLNK (statbuf->st_mode))
927 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
930 g_file_info_set_file_type (info, file_type);
931 g_file_info_set_size (info, statbuf->st_size);
933 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
935 /* Pointless setting these on Windows even if they exist in the struct */
936 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
937 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
938 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
939 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
940 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
942 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
943 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
944 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
945 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
947 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
948 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
949 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
950 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
953 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
954 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
955 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
956 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
957 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
960 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
961 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
962 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
963 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
964 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
967 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
968 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
969 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
970 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
971 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
974 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
975 G_FILE_ATTRIBUTE_ID_ETAG_VALUE))
977 char *etag = _g_local_file_info_create_etag (statbuf);
978 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ETAG_VALUE, etag);
982 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
983 G_FILE_ATTRIBUTE_ID_ID_FILE))
985 char *id = _g_local_file_info_create_file_id (statbuf);
986 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILE, id);
990 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
991 G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM))
993 char *id = _g_local_file_info_create_fs_id (statbuf);
994 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM, id);
1002 make_valid_utf8 (const char *name)
1005 const gchar *remainder, *invalid;
1006 gint remaining_bytes, valid_bytes;
1010 remaining_bytes = strlen (name);
1012 while (remaining_bytes != 0)
1014 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1016 valid_bytes = invalid - remainder;
1019 string = g_string_sized_new (remaining_bytes);
1021 g_string_append_len (string, remainder, valid_bytes);
1022 /* append U+FFFD REPLACEMENT CHARACTER */
1023 g_string_append (string, "\357\277\275");
1025 remaining_bytes -= valid_bytes + 1;
1026 remainder = invalid + 1;
1030 return g_strdup (name);
1032 g_string_append (string, remainder);
1034 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1036 return g_string_free (string, FALSE);
1040 convert_pwd_string_to_utf8 (char *pwd_str)
1044 if (!g_utf8_validate (pwd_str, -1, NULL))
1046 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1047 if (utf8_string == NULL)
1048 utf8_string = make_valid_utf8 (pwd_str);
1051 utf8_string = g_strdup (pwd_str);
1057 uid_data_free (UidData *data)
1059 g_free (data->user_name);
1060 g_free (data->real_name);
1064 /* called with lock held */
1066 lookup_uid_data (uid_t uid)
1070 struct passwd pwbuf;
1071 struct passwd *pwbufp;
1072 char *gecos, *comma;
1074 if (uid_cache == NULL)
1075 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1077 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1082 data = g_new0 (UidData, 1);
1084 #if defined(HAVE_POSIX_GETPWUID_R)
1085 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1086 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1087 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1089 pwbufp = getpwuid (uid);
1094 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1095 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1097 gecos = pwbufp->pw_gecos;
1101 comma = strchr (gecos, ',');
1104 data->real_name = convert_pwd_string_to_utf8 (gecos);
1108 /* Default fallbacks */
1109 if (data->real_name == NULL)
1111 if (data->user_name != NULL)
1112 data->real_name = g_strdup (data->user_name);
1114 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1117 if (data->user_name == NULL)
1118 data->user_name = g_strdup_printf ("%d", (int)uid);
1120 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1126 get_username_from_uid (uid_t uid)
1132 data = lookup_uid_data (uid);
1133 res = g_strdup (data->user_name);
1134 G_UNLOCK (uid_cache);
1140 get_realname_from_uid (uid_t uid)
1146 data = lookup_uid_data (uid);
1147 res = g_strdup (data->real_name);
1148 G_UNLOCK (uid_cache);
1153 /* called with lock held */
1155 lookup_gid_name (gid_t gid)
1160 struct group *gbufp;
1162 if (gid_cache == NULL)
1163 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1165 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1170 #if defined (HAVE_POSIX_GETGRGID_R)
1171 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1172 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1173 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1175 gbufp = getgrgid (gid);
1178 if (gbufp != NULL &&
1179 gbufp->gr_name != NULL &&
1180 gbufp->gr_name[0] != 0)
1181 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1183 name = g_strdup_printf("%d", (int)gid);
1185 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1191 get_groupname_from_gid (gid_t gid)
1197 name = lookup_gid_name (gid);
1198 res = g_strdup (name);
1199 G_UNLOCK (gid_cache);
1203 #endif /* !G_OS_WIN32 */
1206 get_content_type (const char *basename,
1208 GLocalFileStat *statbuf,
1209 gboolean is_symlink,
1210 gboolean symlink_broken,
1211 GFileQueryInfoFlags flags,
1215 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1216 return g_strdup ("inode/symlink");
1217 else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
1218 return g_strdup ("inode/directory");
1220 else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
1221 return g_strdup ("inode/chardevice");
1222 else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
1223 return g_strdup ("inode/blockdevice");
1224 else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
1225 return g_strdup ("inode/fifo");
1228 else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
1229 return g_strdup ("inode/socket");
1234 gboolean result_uncertain;
1236 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1239 if (!fast && result_uncertain && path != NULL)
1241 guchar sniff_buffer[4096];
1245 sniff_length = _g_unix_content_type_get_sniff_len ();
1246 if (sniff_length > 4096)
1247 sniff_length = 4096;
1250 fd = open (path, O_RDONLY | O_NOATIME);
1251 if (fd < 0 && errno == EPERM)
1253 fd = open (path, O_RDONLY);
1259 res = read (fd, sniff_buffer, sniff_length);
1263 g_free (content_type);
1264 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1270 return content_type;
1276 get_thumbnail_attributes (const char *path,
1279 GChecksum *checksum;
1284 uri = g_filename_to_uri (path, NULL, NULL);
1286 checksum = g_checksum_new (G_CHECKSUM_MD5);
1287 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1291 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1292 g_checksum_free (checksum);
1294 filename = g_build_filename (g_get_user_cache_dir (),
1295 "thumbnails", "normal", basename,
1298 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1299 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1303 filename = g_build_filename (g_get_user_cache_dir (),
1304 "thumbnails", "fail",
1305 "gnome-thumbnail-factory",
1309 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1310 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1318 win32_get_file_user_info (const gchar *filename,
1323 PSECURITY_DESCRIPTOR psd = NULL;
1324 DWORD sd_size = 0; /* first call calculates the size required */
1326 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1327 if ((GetFileSecurityW (wfilename,
1328 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1331 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1332 (psd = g_try_malloc (sd_size)) != NULL &&
1333 GetFileSecurityW (wfilename,
1334 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1341 SID_NAME_USE name_use = 0; /* don't care? */
1342 wchar_t *name = NULL;
1343 wchar_t *domain = NULL;
1345 DWORD domain_len = 0;
1346 /* get the user name */
1350 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1352 if (!LookupAccountSidW (NULL, /* local machine */
1355 domain, &domain_len, /* no domain info yet */
1356 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1358 name = g_try_malloc (name_len * sizeof (wchar_t));
1359 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1360 if (name && domain &&
1361 LookupAccountSidW (NULL, /* local machine */
1364 domain, &domain_len, /* no domain info yet */
1367 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1373 /* get the group name */
1377 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1379 if (!LookupAccountSidW (NULL, /* local machine */
1382 domain, &domain_len, /* no domain info yet */
1383 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1385 name = g_try_malloc (name_len * sizeof (wchar_t));
1386 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1387 if (name && domain &&
1388 LookupAccountSidW (NULL, /* local machine */
1391 domain, &domain_len, /* no domain info yet */
1394 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1400 /* TODO: get real name */
1406 #endif /* G_OS_WIN32 */
1409 _g_local_file_info_get_nostat (GFileInfo *info,
1410 const char *basename,
1412 GFileAttributeMatcher *attribute_matcher)
1414 g_file_info_set_name (info, basename);
1416 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1417 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1419 char *display_name = g_filename_display_basename (path);
1421 /* look for U+FFFD REPLACEMENT CHARACTER */
1422 if (strstr (display_name, "\357\277\275") != NULL)
1424 char *p = display_name;
1425 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1428 g_file_info_set_display_name (info, display_name);
1429 g_free (display_name);
1432 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1433 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1435 char *edit_name = g_filename_display_basename (path);
1436 g_file_info_set_edit_name (info, edit_name);
1441 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1442 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1444 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1446 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1452 _g_local_file_info_get (const char *basename,
1454 GFileAttributeMatcher *attribute_matcher,
1455 GFileQueryInfoFlags flags,
1456 GLocalParentFileInfo *parent_info,
1460 GLocalFileStat statbuf;
1462 struct stat statbuf2;
1466 gboolean is_symlink, symlink_broken;
1468 DWORD dos_attributes;
1470 char *symlink_target;
1475 info = g_file_info_new ();
1477 /* Make sure we don't set any unwanted attributes */
1478 g_file_info_set_attribute_mask (info, attribute_matcher);
1480 _g_local_file_info_get_nostat (info, basename, path, attribute_matcher);
1482 if (attribute_matcher == NULL)
1484 g_file_info_unset_attribute_mask (info);
1489 res = g_lstat (path, &statbuf);
1492 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1497 g_object_unref (info);
1501 len = wcslen (wpath);
1502 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1505 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1508 res = _wstati64 (wpath, &statbuf);
1509 dos_attributes = GetFileAttributesW (wpath);
1519 /* Don't bail out if we get Permission denied (SELinux?) */
1520 if (errsv != EACCES)
1522 char *display_name = g_filename_display_name (path);
1523 g_object_unref (info);
1524 g_set_error (error, G_IO_ERROR,
1525 g_io_error_from_errno (errsv),
1526 _("Error when getting information for file '%s': %s"),
1527 display_name, g_strerror (errsv));
1528 g_free (display_name);
1533 /* Even if stat() fails, try to get as much as other attributes possible */
1534 stat_ok = res != -1;
1537 device = statbuf.st_dev;
1542 is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
1546 symlink_broken = FALSE;
1550 g_file_info_set_is_symlink (info, TRUE);
1552 /* Unless NOFOLLOW was set we default to following symlinks */
1553 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1555 res = stat (path, &statbuf2);
1557 /* Report broken links as symlinks */
1564 symlink_broken = TRUE;
1570 set_info_from_stat (info, &statbuf, attribute_matcher);
1573 if (basename != NULL && basename[0] == '.')
1574 g_file_info_set_is_hidden (info, TRUE);
1576 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1577 (stat_ok && S_ISREG (statbuf.st_mode)))
1578 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1580 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1581 g_file_info_set_is_hidden (info, TRUE);
1583 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1584 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1586 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1587 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1590 symlink_target = NULL;
1594 symlink_target = read_link (path);
1595 if (symlink_target &&
1596 _g_file_attribute_matcher_matches_id (attribute_matcher,
1597 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1598 g_file_info_set_symlink_target (info, symlink_target);
1601 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1602 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1603 _g_file_attribute_matcher_matches_id (attribute_matcher,
1604 G_FILE_ATTRIBUTE_ID_STANDARD_ICON))
1606 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
1610 g_file_info_set_content_type (info, content_type);
1612 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1613 G_FILE_ATTRIBUTE_ID_STANDARD_ICON))
1617 if (strcmp (path, g_get_home_dir ()) == 0)
1618 icon = g_themed_icon_new ("user-home");
1619 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1620 icon = g_themed_icon_new ("user-desktop");
1621 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1622 icon = g_themed_icon_new_with_default_fallbacks ("folder-documents");
1623 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1624 icon = g_themed_icon_new_with_default_fallbacks ("folder-download");
1625 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1626 icon = g_themed_icon_new_with_default_fallbacks ("folder-music");
1627 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1628 icon = g_themed_icon_new_with_default_fallbacks ("folder-pictures");
1629 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1630 icon = g_themed_icon_new_with_default_fallbacks ("folder-publicshare");
1631 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1632 icon = g_themed_icon_new_with_default_fallbacks ("folder-templates");
1633 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1634 icon = g_themed_icon_new_with_default_fallbacks ("folder-videos");
1637 icon = g_content_type_get_icon (content_type);
1638 if (G_IS_THEMED_ICON (icon))
1640 const char *type_icon = NULL;
1642 if (S_ISDIR (statbuf.st_mode))
1643 type_icon = "folder";
1645 g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
1651 g_file_info_set_icon (info, icon);
1652 g_object_unref (icon);
1656 g_free (content_type);
1660 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1661 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1663 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
1667 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1668 g_free (content_type);
1672 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1673 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1678 win32_get_file_user_info (path, NULL, &name, NULL);
1681 name = get_username_from_uid (statbuf.st_uid);
1684 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1688 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1689 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1693 win32_get_file_user_info (path, NULL, NULL, &name);
1696 name = get_realname_from_uid (statbuf.st_uid);
1699 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1703 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1704 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1708 win32_get_file_user_info (path, &name, NULL, NULL);
1711 name = get_groupname_from_gid (statbuf.st_gid);
1714 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1718 if (stat_ok && parent_info && parent_info->device != 0 &&
1719 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1720 statbuf.st_dev != parent_info->device)
1721 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1724 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1727 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1729 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1730 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1732 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1733 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH))
1734 get_thumbnail_attributes (path, info);
1736 vfs = g_vfs_get_default ();
1737 class = G_VFS_GET_CLASS (vfs);
1738 if (class->local_file_add_info)
1740 class->local_file_add_info (vfs,
1746 &parent_info->extra_data,
1747 &parent_info->free_extra_data);
1750 g_file_info_unset_attribute_mask (info);
1752 g_free (symlink_target);
1758 _g_local_file_info_get_from_fd (int fd,
1759 const char *attributes,
1762 GLocalFileStat stat_buf;
1763 GFileAttributeMatcher *matcher;
1767 #define FSTAT _fstati64
1772 if (FSTAT (fd, &stat_buf) == -1)
1776 g_set_error (error, G_IO_ERROR,
1777 g_io_error_from_errno (errsv),
1778 _("Error when getting information for file descriptor: %s"),
1779 g_strerror (errsv));
1783 info = g_file_info_new ();
1785 matcher = g_file_attribute_matcher_new (attributes);
1787 /* Make sure we don't set any unwanted attributes */
1788 g_file_info_set_attribute_mask (info, matcher);
1790 set_info_from_stat (info, &stat_buf, matcher);
1793 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
1794 is_selinux_enabled ())
1797 if (fgetfilecon_raw (fd, &context) >= 0)
1799 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
1805 get_xattrs_from_fd (fd, TRUE, info, matcher);
1806 get_xattrs_from_fd (fd, FALSE, info, matcher);
1808 g_file_attribute_matcher_unref (matcher);
1810 g_file_info_unset_attribute_mask (info);
1816 get_uint32 (const GFileAttributeValue *value,
1820 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1822 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1823 _("Invalid attribute type (uint32 expected)"));
1827 *val_out = value->u.uint32;
1834 get_uint64 (const GFileAttributeValue *value,
1838 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1840 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1841 _("Invalid attribute type (uint64 expected)"));
1845 *val_out = value->u.uint64;
1851 #if defined(HAVE_SYMLINK)
1853 get_byte_string (const GFileAttributeValue *value,
1854 const char **val_out,
1857 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1859 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1860 _("Invalid attribute type (byte string expected)"));
1864 *val_out = value->u.string;
1872 get_string (const GFileAttributeValue *value,
1873 const char **val_out,
1876 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
1878 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1879 _("Invalid attribute type (byte string expected)"));
1883 *val_out = value->u.string;
1890 set_unix_mode (char *filename,
1891 GFileQueryInfoFlags flags,
1892 const GFileAttributeValue *value,
1898 if (!get_uint32 (value, &val, error))
1902 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
1904 res = lchmod (filename, val);
1906 struct stat statbuf;
1907 /* Calling chmod on a symlink changes permissions on the symlink.
1908 * We don't want to do this, so we need to check for a symlink */
1909 res = g_lstat (filename, &statbuf);
1910 if (res == 0 && S_ISLNK (statbuf.st_mode))
1912 g_set_error_literal (error, G_IO_ERROR,
1913 G_IO_ERROR_NOT_SUPPORTED,
1914 _("Cannot set permissions on symlinks"));
1918 res = g_chmod (filename, val);
1922 res = g_chmod (filename, val);
1928 g_set_error (error, G_IO_ERROR,
1929 g_io_error_from_errno (errsv),
1930 _("Error setting permissions: %s"),
1931 g_strerror (errsv));
1939 set_unix_uid_gid (char *filename,
1940 const GFileAttributeValue *uid_value,
1941 const GFileAttributeValue *gid_value,
1942 GFileQueryInfoFlags flags,
1952 if (!get_uint32 (uid_value, &val, error))
1961 if (!get_uint32 (gid_value, &val, error))
1969 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1970 res = lchown (filename, uid, gid);
1973 res = chown (filename, uid, gid);
1979 g_set_error (error, G_IO_ERROR,
1980 g_io_error_from_errno (errsv),
1981 _("Error setting owner: %s"),
1982 g_strerror (errsv));
1991 set_symlink (char *filename,
1992 const GFileAttributeValue *value,
1996 struct stat statbuf;
1998 if (!get_byte_string (value, &val, error))
2003 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2004 _("symlink must be non-NULL"));
2008 if (g_lstat (filename, &statbuf))
2012 g_set_error (error, G_IO_ERROR,
2013 g_io_error_from_errno (errsv),
2014 _("Error setting symlink: %s"),
2015 g_strerror (errsv));
2019 if (!S_ISLNK (statbuf.st_mode))
2021 g_set_error_literal (error, G_IO_ERROR,
2022 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2023 _("Error setting symlink: file is not a symlink"));
2027 if (g_unlink (filename))
2031 g_set_error (error, G_IO_ERROR,
2032 g_io_error_from_errno (errsv),
2033 _("Error setting symlink: %s"),
2034 g_strerror (errsv));
2038 if (symlink (filename, val) != 0)
2042 g_set_error (error, G_IO_ERROR,
2043 g_io_error_from_errno (errsv),
2044 _("Error setting symlink: %s"),
2045 g_strerror (errsv));
2055 lazy_stat (char *filename,
2056 struct stat *statbuf,
2057 gboolean *called_stat)
2064 res = g_stat (filename, statbuf);
2067 *called_stat = TRUE;
2074 set_mtime_atime (char *filename,
2075 const GFileAttributeValue *mtime_value,
2076 const GFileAttributeValue *mtime_usec_value,
2077 const GFileAttributeValue *atime_value,
2078 const GFileAttributeValue *atime_usec_value,
2083 guint32 val_usec = 0;
2084 struct stat statbuf;
2085 gboolean got_stat = FALSE;
2086 struct timeval times[2] = { {0, 0}, {0, 0} };
2091 if (!get_uint64 (atime_value, &val, error))
2093 times[0].tv_sec = val;
2097 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2099 times[0].tv_sec = statbuf.st_mtime;
2100 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2101 times[0].tv_usec = statbuf.st_atimensec / 1000;
2102 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2103 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2108 if (atime_usec_value)
2110 if (!get_uint32 (atime_usec_value, &val_usec, error))
2112 times[0].tv_usec = val_usec;
2118 if (!get_uint64 (mtime_value, &val, error))
2120 times[1].tv_sec = val;
2124 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2126 times[1].tv_sec = statbuf.st_mtime;
2127 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2128 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2129 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2130 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2135 if (mtime_usec_value)
2137 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2139 times[1].tv_usec = val_usec;
2142 res = utimes (filename, times);
2147 g_set_error (error, G_IO_ERROR,
2148 g_io_error_from_errno (errsv),
2149 _("Error setting modification or access time: %s"),
2150 g_strerror (errsv));
2160 set_selinux_context (char *filename,
2161 const GFileAttributeValue *value,
2166 if (!get_string (value, &val, error))
2171 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2172 _("SELinux context must be non-NULL"));
2176 if (is_selinux_enabled ()) {
2177 security_context_t val_s;
2179 val_s = g_strdup (val);
2181 if (setfilecon_raw (filename, val_s) < 0)
2185 g_set_error (error, G_IO_ERROR,
2186 g_io_error_from_errno (errsv),
2187 _("Error setting SELinux context: %s"),
2188 g_strerror (errsv));
2193 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2194 _("SELinux is not enabled on this system"));
2204 _g_local_file_info_set_attribute (char *filename,
2205 const char *attribute,
2206 GFileAttributeType type,
2208 GFileQueryInfoFlags flags,
2209 GCancellable *cancellable,
2212 GFileAttributeValue value = { 0 };
2216 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2218 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2219 return set_unix_mode (filename, flags, &value, error);
2222 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2223 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2224 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2225 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2229 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2230 return set_symlink (filename, &value, error);
2234 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2235 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2236 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2237 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2238 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2239 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2240 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2241 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2245 else if (g_str_has_prefix (attribute, "xattr::"))
2246 return set_xattr (filename, attribute, &value, error);
2247 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2248 return set_xattr (filename, attribute, &value, error);
2252 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2253 return set_selinux_context (filename, &value, error);
2256 vfs = g_vfs_get_default ();
2257 class = G_VFS_GET_CLASS (vfs);
2258 if (class->local_file_set_attributes)
2262 info = g_file_info_new ();
2263 g_file_info_set_attribute (info,
2267 if (!class->local_file_set_attributes (vfs, filename,
2272 g_object_unref (info);
2276 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2278 g_object_unref (info);
2282 g_object_unref (info);
2285 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2286 _("Setting attribute %s not supported"), attribute);
2291 _g_local_file_info_set_attributes (char *filename,
2293 GFileQueryInfoFlags flags,
2294 GCancellable *cancellable,
2297 GFileAttributeValue *value;
2299 GFileAttributeValue *uid, *gid;
2302 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2304 #if defined (HAVE_CHOWN) || defined (HAVE_UTIMES)
2305 GFileAttributeStatus status;
2311 /* Handles setting multiple specified data in a single set, and takes care
2312 of ordering restrictions when setting attributes */
2316 /* Set symlink first, since this recreates the file */
2318 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2321 if (!set_symlink (filename, value, error))
2323 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2325 /* Don't set error multiple times */
2329 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2335 /* Group uid and gid setting into one call
2336 * Change ownership before permissions, since ownership changes can
2337 change permissions (e.g. setuid)
2339 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2340 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2344 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2346 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2348 /* Don't set error multiple times */
2352 status = G_FILE_ATTRIBUTE_STATUS_SET;
2354 uid->status = status;
2356 gid->status = status;
2360 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2363 if (!set_unix_mode (filename, flags, value, error))
2365 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2367 /* Don't set error multiple times */
2371 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2376 /* Group all time settings into one call
2377 * Change times as the last thing to avoid it changing due to metadata changes
2380 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2381 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2382 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2383 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2385 if (mtime || mtime_usec || atime || atime_usec)
2387 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2389 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2391 /* Don't set error multiple times */
2395 status = G_FILE_ATTRIBUTE_STATUS_SET;
2398 mtime->status = status;
2400 mtime_usec->status = status;
2402 atime->status = status;
2404 atime_usec->status = status;
2408 /* xattrs are handled by default callback */
2411 /* SELinux context */
2413 if (is_selinux_enabled ()) {
2414 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2417 if (!set_selinux_context (filename, value, error))
2419 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2421 /* Don't set error multiple times */
2425 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2430 vfs = g_vfs_get_default ();
2431 class = G_VFS_GET_CLASS (vfs);
2432 if (class->local_file_set_attributes)
2434 if (!class->local_file_set_attributes (vfs, filename,
2440 /* Don't set error multiple times */