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 <glib/gchecksum.h>
62 #include <gfileattribute-priv.h>
76 #define X_OK 0 /* not really */
79 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
82 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
85 #define S_IXUSR _S_IEXEC
89 #include "glocalfileinfo.h"
91 #include "gthemedicon.h"
92 #include "gcontenttype.h"
93 #include "gcontenttypeprivate.h"
97 struct ThumbMD5Context {
100 unsigned char in[64];
108 G_LOCK_DEFINE_STATIC (uid_cache);
109 static GHashTable *uid_cache = NULL;
111 G_LOCK_DEFINE_STATIC (gid_cache);
112 static GHashTable *gid_cache = NULL;
115 _g_local_file_info_create_etag (struct stat *statbuf)
119 tv.tv_sec = statbuf->st_mtime;
120 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
121 tv.tv_usec = statbuf->st_mtimensec / 1000;
122 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
123 tv.tv_usec = statbuf->st_mtim.tv_nsec / 1000;
128 return g_strdup_printf ("%lu:%lu", tv.tv_sec, tv.tv_usec);
132 _g_local_file_info_create_file_id (struct stat *statbuf)
134 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
135 (guint64) statbuf->st_dev,
136 (guint64) statbuf->st_ino);
140 _g_local_file_info_create_fs_id (struct stat *statbuf)
142 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
143 (guint64) statbuf->st_dev);
148 read_link (const gchar *full_name)
155 buffer = g_malloc (size);
161 read_size = readlink (full_name, buffer, size);
167 if (read_size < size)
169 buffer[read_size] = 0;
173 buffer = g_realloc (buffer, size);
180 /* Get the SELinux security context */
182 get_selinux_context (const char *path,
184 GFileAttributeMatcher *attribute_matcher,
185 gboolean follow_symlinks)
190 if (!g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT))
193 if (is_selinux_enabled ())
197 if (lgetfilecon_raw (path, &context) < 0)
202 if (getfilecon_raw (path, &context) < 0)
208 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
217 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
218 * (Mac) getxattr(..., XATTR_NOFOLLOW)
220 #ifdef HAVE_XATTR_NOFOLLOW
221 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
222 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
223 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
225 #define g_fgetxattr fgetxattr
226 #define g_flistxattr flistxattr
227 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
231 g_getxattr (const char *path, const char *name, void *value, size_t size,
232 gboolean follow_symlinks)
234 #ifdef HAVE_XATTR_NOFOLLOW
235 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
238 return getxattr (path, name, value, size);
240 return lgetxattr (path, name, value, size);
245 g_listxattr(const char *path, char *namebuf, size_t size,
246 gboolean follow_symlinks)
248 #ifdef HAVE_XATTR_NOFOLLOW
249 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
252 return listxattr (path, namebuf, size);
254 return llistxattr (path, namebuf, size);
261 return c >= 32 && c <= 126 && c != '\\';
265 name_is_valid (const char *str)
269 if (!valid_char (*str++))
276 hex_escape_string (const char *str,
277 gboolean *free_return)
280 char *escaped_str, *p;
282 static char *hex_digits = "0123456789abcdef";
288 for (i = 0; i < len; i++)
290 if (!valid_char (str[i]))
294 if (num_invalid == 0)
296 *free_return = FALSE;
300 escaped_str = g_malloc (len + num_invalid*3 + 1);
303 for (i = 0; i < len; i++)
305 if (valid_char (str[i]))
312 *p++ = hex_digits[(c >> 4) & 0xf];
313 *p++ = hex_digits[c & 0xf];
323 hex_unescape_string (const char *str,
325 gboolean *free_return)
328 char *unescaped_str, *p;
334 if (strchr (str, '\\') == NULL)
338 *free_return = FALSE;
342 unescaped_str = g_malloc (len + 1);
345 for (i = 0; i < len; i++)
347 if (str[i] == '\\' &&
352 (g_ascii_xdigit_value (str[i+2]) << 4) |
353 g_ascii_xdigit_value (str[i+3]);
363 *out_len = p - unescaped_str;
365 return unescaped_str;
369 escape_xattr (GFileInfo *info,
370 const char *gio_attr, /* gio attribute name */
371 const char *value, /* Is zero terminated */
372 size_t len /* not including zero termination */)
375 gboolean free_escaped_val;
377 escaped_val = hex_escape_string (value, &free_escaped_val);
379 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
381 if (free_escaped_val)
382 g_free (escaped_val);
386 get_one_xattr (const char *path,
388 const char *gio_attr,
390 gboolean follow_symlinks)
396 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
401 else if (len == -1 && errno == ERANGE)
403 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
408 value_p = g_malloc (len+1);
410 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
424 escape_xattr (info, gio_attr, value_p, len);
426 if (value_p != value)
430 #endif /* defined HAVE_XATTR */
433 get_xattrs (const char *path,
436 GFileAttributeMatcher *matcher,
437 gboolean follow_symlinks)
442 ssize_t list_res_size;
445 const char *attr, *attr2;
448 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
450 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
454 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
456 if (list_res_size == -1 ||
460 list_size = list_res_size;
461 list = g_malloc (list_size);
465 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
467 if (list_res_size == -1 && errno == ERANGE)
469 list_size = list_size * 2;
470 list = g_realloc (list, list_size);
474 if (list_res_size == -1)
478 while (list_res_size > 0)
480 if ((user && g_str_has_prefix (attr, "user.")) ||
481 (!user && !g_str_has_prefix (attr, "user.")))
483 char *escaped_attr, *gio_attr;
484 gboolean free_escaped_attr;
488 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
489 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
493 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
494 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
497 if (free_escaped_attr)
498 g_free (escaped_attr);
500 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
503 len = strlen (attr) + 1;
505 list_res_size -= len;
512 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
514 char *unescaped_attribute, *a;
515 gboolean free_unescaped_attribute;
517 attr2 = strchr (attr, ':');
520 attr2++; /* Skip ':' */
521 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
523 a = g_strconcat ("user.", unescaped_attribute, NULL);
525 a = unescaped_attribute;
527 get_one_xattr (path, info, attr, a, follow_symlinks);
532 if (free_unescaped_attribute)
533 g_free (unescaped_attribute);
537 #endif /* defined HAVE_XATTR */
542 get_one_xattr_from_fd (int fd,
544 const char *gio_attr,
551 len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
556 else if (len == -1 && errno == ERANGE)
558 len = g_fgetxattr (fd, xattr, NULL, 0);
563 value_p = g_malloc (len + 1);
565 len = g_fgetxattr (fd, xattr, value_p, len);
579 escape_xattr (info, gio_attr, value_p, len);
581 if (value_p != value)
584 #endif /* defined HAVE_XATTR */
587 get_xattrs_from_fd (int fd,
590 GFileAttributeMatcher *matcher)
595 ssize_t list_res_size;
598 const char *attr, *attr2;
601 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
603 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
607 list_res_size = g_flistxattr (fd, NULL, 0);
609 if (list_res_size == -1 ||
613 list_size = list_res_size;
614 list = g_malloc (list_size);
618 list_res_size = g_flistxattr (fd, list, list_size);
620 if (list_res_size == -1 && errno == ERANGE)
622 list_size = list_size * 2;
623 list = g_realloc (list, list_size);
627 if (list_res_size == -1)
631 while (list_res_size > 0)
633 if ((user && g_str_has_prefix (attr, "user.")) ||
634 (!user && !g_str_has_prefix (attr, "user.")))
636 char *escaped_attr, *gio_attr;
637 gboolean free_escaped_attr;
641 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
642 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
646 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
647 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
650 if (free_escaped_attr)
651 g_free (escaped_attr);
653 get_one_xattr_from_fd (fd, info, gio_attr, attr);
656 len = strlen (attr) + 1;
658 list_res_size -= len;
665 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
667 char *unescaped_attribute, *a;
668 gboolean free_unescaped_attribute;
670 attr2 = strchr (attr, ':');
673 attr2++; /* Skip ':' */
674 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
676 a = g_strconcat ("user.", unescaped_attribute, NULL);
678 a = unescaped_attribute;
680 get_one_xattr_from_fd (fd, info, attr, a);
685 if (free_unescaped_attribute)
686 g_free (unescaped_attribute);
690 #endif /* defined HAVE_XATTR */
695 set_xattr (char *filename,
696 const char *escaped_attribute,
697 const GFileAttributeValue *attr_value,
700 char *attribute, *value;
701 gboolean free_attribute, free_value;
702 int val_len, res, errsv;
706 if (attr_value == NULL)
708 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
709 _("Attribute value must be non-NULL"));
713 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
715 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
716 _("Invalid attribute type (string expected)"));
720 if (!name_is_valid (escaped_attribute))
722 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
723 _("Invalid extended attribute name"));
727 if (g_str_has_prefix (escaped_attribute, "xattr::"))
729 escaped_attribute += 6;
734 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
735 escaped_attribute += 10;
739 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
740 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
743 a = g_strconcat ("user.", attribute, NULL);
747 res = g_setxattr (filename, a, value, val_len);
761 g_set_error (error, G_IO_ERROR,
762 g_io_error_from_errno (errsv),
763 _("Error setting extended attribute '%s': %s"),
764 escaped_attribute, g_strerror (errsv));
775 _g_local_file_info_get_parent_info (const char *dir,
776 GFileAttributeMatcher *attribute_matcher,
777 GLocalParentFileInfo *parent_info)
782 parent_info->writable = FALSE;
783 parent_info->is_sticky = FALSE;
784 parent_info->has_trash_dir = FALSE;
785 parent_info->device = 0;
787 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME) ||
788 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE) ||
789 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH) ||
790 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT))
792 parent_info->writable = (g_access (dir, W_OK) == 0);
794 res = g_stat (dir, &statbuf);
797 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
798 * renamed or deleted only by the owner of the file, by the owner of the directory, and
799 * by a privileged process.
804 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
806 parent_info->is_sticky = FALSE;
808 parent_info->owner = statbuf.st_uid;
809 parent_info->device = statbuf.st_dev;
810 /* No need to find trash dir if its not writable anyway */
811 if (parent_info->writable &&
812 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
813 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
819 get_access_rights (GFileAttributeMatcher *attribute_matcher,
822 struct stat *statbuf,
823 GLocalParentFileInfo *parent_info)
825 if (g_file_attribute_matcher_matches (attribute_matcher,
826 G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
827 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
828 g_access (path, R_OK) == 0);
830 if (g_file_attribute_matcher_matches (attribute_matcher,
831 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
832 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
833 g_access (path, W_OK) == 0);
835 if (g_file_attribute_matcher_matches (attribute_matcher,
836 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
837 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE,
838 g_access (path, X_OK) == 0);
846 if (parent_info->writable)
848 if (parent_info->is_sticky)
851 uid_t uid = geteuid ();
853 if (uid == statbuf->st_uid ||
854 uid == parent_info->owner ||
863 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME))
864 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME,
867 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE))
868 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE,
871 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
872 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH,
873 writable && parent_info->has_trash_dir);
878 set_info_from_stat (GFileInfo *info,
879 struct stat *statbuf,
880 GFileAttributeMatcher *attribute_matcher)
884 file_type = G_FILE_TYPE_UNKNOWN;
886 if (S_ISREG (statbuf->st_mode))
887 file_type = G_FILE_TYPE_REGULAR;
888 else if (S_ISDIR (statbuf->st_mode))
889 file_type = G_FILE_TYPE_DIRECTORY;
891 else if (S_ISCHR (statbuf->st_mode) ||
892 S_ISBLK (statbuf->st_mode) ||
893 S_ISFIFO (statbuf->st_mode)
895 || S_ISSOCK (statbuf->st_mode)
898 file_type = G_FILE_TYPE_SPECIAL;
901 else if (S_ISLNK (statbuf->st_mode))
902 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
905 g_file_info_set_file_type (info, file_type);
906 g_file_info_set_size (info, statbuf->st_size);
908 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE, statbuf->st_dev);
909 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_INODE, statbuf->st_ino);
910 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, statbuf->st_mode);
911 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_NLINK, statbuf->st_nlink);
912 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_UID, statbuf->st_uid);
913 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, statbuf->st_gid);
914 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_RDEV, statbuf->st_rdev);
915 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
916 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE, statbuf->st_blksize);
918 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
919 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_BLOCKS, statbuf->st_blocks);
922 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED, statbuf->st_mtime);
923 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
924 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
925 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
926 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
929 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS, statbuf->st_atime);
930 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
931 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
932 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
933 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
936 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_CHANGED, statbuf->st_ctime);
937 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
938 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
939 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
940 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
943 if (g_file_attribute_matcher_matches (attribute_matcher,
944 G_FILE_ATTRIBUTE_ETAG_VALUE))
946 char *etag = _g_local_file_info_create_etag (statbuf);
947 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ETAG_VALUE, etag);
951 if (g_file_attribute_matcher_matches (attribute_matcher,
952 G_FILE_ATTRIBUTE_ID_FILE))
954 char *id = _g_local_file_info_create_file_id (statbuf);
955 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE, id);
959 if (g_file_attribute_matcher_matches (attribute_matcher,
960 G_FILE_ATTRIBUTE_ID_FILESYSTEM))
962 char *id = _g_local_file_info_create_fs_id (statbuf);
963 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILESYSTEM, id);
969 make_valid_utf8 (const char *name)
972 const gchar *remainder, *invalid;
973 gint remaining_bytes, valid_bytes;
977 remaining_bytes = strlen (name);
979 while (remaining_bytes != 0)
981 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
983 valid_bytes = invalid - remainder;
986 string = g_string_sized_new (remaining_bytes);
988 g_string_append_len (string, remainder, valid_bytes);
989 /* append U+FFFD REPLACEMENT CHARACTER */
990 g_string_append (string, "\357\277\275");
992 remaining_bytes -= valid_bytes + 1;
993 remainder = invalid + 1;
997 return g_strdup (name);
999 g_string_append (string, remainder);
1001 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1003 return g_string_free (string, FALSE);
1007 convert_pwd_string_to_utf8 (char *pwd_str)
1011 if (!g_utf8_validate (pwd_str, -1, NULL))
1013 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1014 if (utf8_string == NULL)
1015 utf8_string = make_valid_utf8 (pwd_str);
1018 utf8_string = g_strdup (pwd_str);
1024 uid_data_free (UidData *data)
1026 g_free (data->user_name);
1027 g_free (data->real_name);
1031 /* called with lock held */
1033 lookup_uid_data (uid_t uid)
1037 struct passwd pwbuf;
1038 struct passwd *pwbufp;
1039 char *gecos, *comma;
1041 if (uid_cache == NULL)
1042 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1044 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1049 data = g_new0 (UidData, 1);
1051 #if defined(HAVE_POSIX_GETPWUID_R)
1052 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1053 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1054 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1056 pwbufp = getpwuid (uid);
1061 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1062 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1064 gecos = pwbufp->pw_gecos;
1068 comma = strchr (gecos, ',');
1071 data->real_name = convert_pwd_string_to_utf8 (gecos);
1075 /* Default fallbacks */
1076 if (data->real_name == NULL)
1078 if (data->user_name != NULL)
1079 data->real_name = g_strdup (data->user_name);
1081 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1084 if (data->user_name == NULL)
1085 data->user_name = g_strdup_printf ("%d", (int)uid);
1087 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1093 get_username_from_uid (uid_t uid)
1099 data = lookup_uid_data (uid);
1100 res = g_strdup (data->user_name);
1101 G_UNLOCK (uid_cache);
1107 get_realname_from_uid (uid_t uid)
1113 data = lookup_uid_data (uid);
1114 res = g_strdup (data->real_name);
1115 G_UNLOCK (uid_cache);
1120 /* called with lock held */
1122 lookup_gid_name (gid_t gid)
1127 struct group *gbufp;
1129 if (gid_cache == NULL)
1130 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1132 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1137 #if defined (HAVE_POSIX_GETGRGID_R)
1138 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1139 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1140 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1142 gbufp = getgrgid (gid);
1145 if (gbufp != NULL &&
1146 gbufp->gr_name != NULL &&
1147 gbufp->gr_name[0] != 0)
1148 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1150 name = g_strdup_printf("%d", (int)gid);
1152 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1158 get_groupname_from_gid (gid_t gid)
1164 name = lookup_gid_name (gid);
1165 res = g_strdup (name);
1166 G_UNLOCK (gid_cache);
1169 #endif /* !G_OS_WIN32 */
1172 get_content_type (const char *basename,
1174 struct stat *statbuf,
1175 gboolean is_symlink,
1176 gboolean symlink_broken,
1177 GFileQueryInfoFlags flags,
1181 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1182 return g_strdup ("inode/symlink");
1183 else if (S_ISDIR(statbuf->st_mode))
1184 return g_strdup ("inode/directory");
1186 else if (S_ISCHR(statbuf->st_mode))
1187 return g_strdup ("inode/chardevice");
1188 else if (S_ISBLK(statbuf->st_mode))
1189 return g_strdup ("inode/blockdevice");
1190 else if (S_ISFIFO(statbuf->st_mode))
1191 return g_strdup ("inode/fifo");
1194 else if (S_ISSOCK(statbuf->st_mode))
1195 return g_strdup ("inode/socket");
1200 gboolean result_uncertain;
1202 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1205 if (!fast && result_uncertain && path != NULL)
1207 guchar sniff_buffer[4096];
1211 sniff_length = _g_unix_content_type_get_sniff_len ();
1212 if (sniff_length > 4096)
1213 sniff_length = 4096;
1215 fd = open (path, O_RDONLY);
1220 res = read (fd, sniff_buffer, sniff_length);
1224 g_free (content_type);
1225 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1231 return content_type;
1237 get_thumbnail_attributes (const char *path,
1240 GChecksum *checksum;
1245 uri = g_filename_to_uri (path, NULL, NULL);
1247 checksum = g_checksum_new (G_CHECKSUM_MD5);
1248 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1252 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1253 g_checksum_free (checksum);
1255 filename = g_build_filename (g_get_home_dir (),
1256 ".thumbnails", "normal", basename,
1259 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1260 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1264 filename = g_build_filename (g_get_home_dir (),
1265 ".thumbnails", "fail",
1266 "gnome-thumbnail-factory",
1270 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1271 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1279 win32_get_file_user_info (const gchar *filename,
1284 PSECURITY_DESCRIPTOR psd = NULL;
1285 DWORD sd_size = 0; /* first call calculates the size required */
1287 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1288 if ((GetFileSecurityW (wfilename,
1289 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1292 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1293 (psd = g_try_malloc (sd_size)) != NULL &&
1294 GetFileSecurityW (wfilename,
1295 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1302 SID_NAME_USE name_use = 0; /* don't care? */
1303 wchar_t *name = NULL;
1304 wchar_t *domain = NULL;
1306 DWORD domain_len = 0;
1307 /* get the user name */
1311 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1313 if (!LookupAccountSidW (NULL, /* local machine */
1316 domain, &domain_len, /* no domain info yet */
1317 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1319 name = g_try_malloc (name_len * sizeof (wchar_t));
1320 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1321 if (name && domain &&
1322 LookupAccountSidW (NULL, /* local machine */
1325 domain, &domain_len, /* no domain info yet */
1328 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1334 /* get the group name */
1338 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1340 if (!LookupAccountSidW (NULL, /* local machine */
1343 domain, &domain_len, /* no domain info yet */
1344 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1346 name = g_try_malloc (name_len * sizeof (wchar_t));
1347 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1348 if (name && domain &&
1349 LookupAccountSidW (NULL, /* local machine */
1352 domain, &domain_len, /* no domain info yet */
1355 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1361 /* TODO: get real name */
1367 #endif /* G_OS_WIN32 */
1370 _g_local_file_info_get (const char *basename,
1372 GFileAttributeMatcher *attribute_matcher,
1373 GFileQueryInfoFlags flags,
1374 GLocalParentFileInfo *parent_info,
1378 struct stat statbuf;
1379 struct stat statbuf2;
1381 gboolean is_symlink, symlink_broken;
1383 info = g_file_info_new ();
1385 /* Make sure we don't set any unwanted attributes */
1386 g_file_info_set_attribute_mask (info, attribute_matcher);
1388 g_file_info_set_name (info, basename);
1390 /* Avoid stat in trivial case */
1391 if (attribute_matcher == NULL)
1394 res = g_lstat (path, &statbuf);
1398 char *display_name = g_filename_display_name (path);
1399 g_object_unref (info);
1400 g_set_error (error, G_IO_ERROR,
1401 g_io_error_from_errno (errsv),
1402 _("Error stating file '%s': %s"),
1403 display_name, g_strerror (errsv));
1404 g_free (display_name);
1409 is_symlink = S_ISLNK (statbuf.st_mode);
1413 symlink_broken = FALSE;
1417 g_file_info_set_is_symlink (info, TRUE);
1419 /* Unless NOFOLLOW was set we default to following symlinks */
1420 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1422 res = stat (path, &statbuf2);
1424 /* Report broken links as symlinks */
1428 symlink_broken = TRUE;
1432 set_info_from_stat (info, &statbuf, attribute_matcher);
1434 if (basename != NULL && basename[0] == '.')
1435 g_file_info_set_is_hidden (info, TRUE);
1437 if (basename != NULL && basename[strlen (basename) -1] == '~')
1438 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP, TRUE);
1441 g_file_attribute_matcher_matches (attribute_matcher,
1442 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET))
1444 char *link = read_link (path);
1445 g_file_info_set_symlink_target (info, link);
1449 if (g_file_attribute_matcher_matches (attribute_matcher,
1450 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME))
1452 char *display_name = g_filename_display_basename (path);
1454 /* look for U+FFFD REPLACEMENT CHARACTER */
1455 if (strstr (display_name, "\357\277\275") != NULL)
1457 char *p = display_name;
1458 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1461 g_file_info_set_display_name (info, display_name);
1462 g_free (display_name);
1465 if (g_file_attribute_matcher_matches (attribute_matcher,
1466 G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME))
1468 char *edit_name = g_filename_display_basename (path);
1469 g_file_info_set_edit_name (info, edit_name);
1474 if (g_file_attribute_matcher_matches (attribute_matcher,
1475 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME))
1477 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1479 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME, copy_name);
1483 if (g_file_attribute_matcher_matches (attribute_matcher,
1484 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE) ||
1485 g_file_attribute_matcher_matches (attribute_matcher,
1486 G_FILE_ATTRIBUTE_STANDARD_ICON))
1488 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1492 g_file_info_set_content_type (info, content_type);
1494 if (g_file_attribute_matcher_matches (attribute_matcher,
1495 G_FILE_ATTRIBUTE_STANDARD_ICON))
1499 icon = g_content_type_get_icon (content_type);
1502 if (G_IS_THEMED_ICON (icon))
1504 const char *type_icon;
1506 /* TODO: Special case desktop dir? That could be expensive with xdg dirs... */
1507 if (strcmp (path, g_get_home_dir ()) == 0)
1508 type_icon = "user-home";
1509 else if (S_ISDIR (statbuf.st_mode))
1510 type_icon = "folder";
1511 else if (statbuf.st_mode & S_IXUSR)
1512 type_icon = "application-x-executable";
1514 type_icon = "text-x-generic";
1516 g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
1519 g_file_info_set_icon (info, icon);
1520 g_object_unref (icon);
1524 g_free (content_type);
1528 if (g_file_attribute_matcher_matches (attribute_matcher,
1529 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE))
1531 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1535 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE, content_type);
1536 g_free (content_type);
1540 if (g_file_attribute_matcher_matches (attribute_matcher,
1541 G_FILE_ATTRIBUTE_OWNER_USER))
1546 win32_get_file_user_info (path, NULL, &name, NULL);
1548 name = get_username_from_uid (statbuf.st_uid);
1551 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1555 if (g_file_attribute_matcher_matches (attribute_matcher,
1556 G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1560 win32_get_file_user_info (path, NULL, NULL, &name);
1562 name = get_realname_from_uid (statbuf.st_uid);
1565 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1569 if (g_file_attribute_matcher_matches (attribute_matcher,
1570 G_FILE_ATTRIBUTE_OWNER_GROUP))
1574 win32_get_file_user_info (path, &name, NULL, NULL);
1576 name = get_groupname_from_gid (statbuf.st_gid);
1579 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1583 if (parent_info && parent_info->device != 0 &&
1584 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1585 statbuf.st_dev != parent_info->device)
1586 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1588 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1590 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1591 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1592 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1594 if (g_file_attribute_matcher_matches (attribute_matcher,
1595 G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1596 get_thumbnail_attributes (path, info);
1598 g_file_info_unset_attribute_mask (info);
1604 _g_local_file_info_get_from_fd (int fd,
1608 struct stat stat_buf;
1609 GFileAttributeMatcher *matcher;
1612 if (fstat (fd, &stat_buf) == -1)
1616 g_set_error (error, G_IO_ERROR,
1617 g_io_error_from_errno (errsv),
1618 _("Error stating file descriptor: %s"),
1619 g_strerror (errsv));
1623 info = g_file_info_new ();
1625 matcher = g_file_attribute_matcher_new (attributes);
1627 /* Make sure we don't set any unwanted attributes */
1628 g_file_info_set_attribute_mask (info, matcher);
1630 set_info_from_stat (info, &stat_buf, matcher);
1633 if (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) &&
1634 is_selinux_enabled ())
1637 if (fgetfilecon_raw (fd, &context) >= 0)
1639 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
1645 get_xattrs_from_fd (fd, TRUE, info, matcher);
1646 get_xattrs_from_fd (fd, FALSE, info, matcher);
1648 g_file_attribute_matcher_unref (matcher);
1650 g_file_info_unset_attribute_mask (info);
1656 get_uint32 (const GFileAttributeValue *value,
1660 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1662 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1663 _("Invalid attribute type (uint32 expected)"));
1667 *val_out = value->u.uint32;
1673 get_uint64 (const GFileAttributeValue *value,
1677 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1679 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1680 _("Invalid attribute type (uint64 expected)"));
1684 *val_out = value->u.uint64;
1689 #if defined(HAVE_SYMLINK)
1691 get_byte_string (const GFileAttributeValue *value,
1692 const char **val_out,
1695 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1697 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1698 _("Invalid attribute type (byte string expected)"));
1702 *val_out = value->u.string;
1709 set_unix_mode (char *filename,
1710 const GFileAttributeValue *value,
1715 if (!get_uint32 (value, &val, error))
1718 if (g_chmod (filename, val) == -1)
1722 g_set_error (error, G_IO_ERROR,
1723 g_io_error_from_errno (errsv),
1724 _("Error setting permissions: %s"),
1725 g_strerror (errsv));
1733 set_unix_uid_gid (char *filename,
1734 const GFileAttributeValue *uid_value,
1735 const GFileAttributeValue *gid_value,
1736 GFileQueryInfoFlags flags,
1746 if (!get_uint32 (uid_value, &val, error))
1755 if (!get_uint32 (gid_value, &val, error))
1763 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1764 res = lchown (filename, uid, gid);
1767 res = chown (filename, uid, gid);
1773 g_set_error (error, G_IO_ERROR,
1774 g_io_error_from_errno (errsv),
1775 _("Error setting owner: %s"),
1776 g_strerror (errsv));
1785 set_symlink (char *filename,
1786 const GFileAttributeValue *value,
1790 struct stat statbuf;
1792 if (!get_byte_string (value, &val, error))
1797 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1798 _("symlink must be non-NULL"));
1802 if (g_lstat (filename, &statbuf))
1806 g_set_error (error, G_IO_ERROR,
1807 g_io_error_from_errno (errsv),
1808 _("Error setting symlink: %s"),
1809 g_strerror (errsv));
1813 if (!S_ISLNK (statbuf.st_mode))
1815 g_set_error (error, G_IO_ERROR,
1816 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1817 _("Error setting symlink: file is not a symlink"));
1821 if (g_unlink (filename))
1825 g_set_error (error, G_IO_ERROR,
1826 g_io_error_from_errno (errsv),
1827 _("Error setting symlink: %s"),
1828 g_strerror (errsv));
1832 if (symlink (filename, val) != 0)
1836 g_set_error (error, G_IO_ERROR,
1837 g_io_error_from_errno (errsv),
1838 _("Error setting symlink: %s"),
1839 g_strerror (errsv));
1848 lazy_stat (char *filename,
1849 struct stat *statbuf,
1850 gboolean *called_stat)
1857 res = g_stat (filename, statbuf);
1860 *called_stat = TRUE;
1868 set_mtime_atime (char *filename,
1869 const GFileAttributeValue *mtime_value,
1870 const GFileAttributeValue *mtime_usec_value,
1871 const GFileAttributeValue *atime_value,
1872 const GFileAttributeValue *atime_usec_value,
1878 struct stat statbuf;
1879 gboolean got_stat = FALSE;
1880 struct timeval times[2] = { {0, 0}, {0, 0} };
1885 if (!get_uint64 (atime_value, &val, error))
1887 times[0].tv_sec = val;
1891 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1893 times[0].tv_sec = statbuf.st_mtime;
1894 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1895 times[0].tv_usec = statbuf.st_atimensec / 1000;
1896 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1897 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
1902 if (atime_usec_value)
1904 if (!get_uint32 (atime_usec_value, &val_usec, error))
1906 times[0].tv_usec = val_usec;
1912 if (!get_uint64 (mtime_value, &val, error))
1914 times[1].tv_sec = val;
1918 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1920 times[1].tv_sec = statbuf.st_mtime;
1921 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
1922 times[1].tv_usec = statbuf.st_mtimensec / 1000;
1923 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
1924 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
1929 if (mtime_usec_value)
1931 if (!get_uint32 (mtime_usec_value, &val_usec, error))
1933 times[1].tv_usec = val_usec;
1936 res = utimes (filename, times);
1941 g_set_error (error, G_IO_ERROR,
1942 g_io_error_from_errno (errsv),
1943 _("Error setting owner: %s"),
1944 g_strerror (errsv));
1952 _g_local_file_info_set_attribute (char *filename,
1953 const char *attribute,
1954 GFileAttributeType type,
1956 GFileQueryInfoFlags flags,
1957 GCancellable *cancellable,
1960 GFileAttributeValue value = { 0 };
1962 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
1964 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
1965 return set_unix_mode (filename, &value, error);
1968 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
1969 return set_unix_uid_gid (filename, &value, NULL, flags, error);
1970 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
1971 return set_unix_uid_gid (filename, NULL, &value, flags, error);
1975 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
1976 return set_symlink (filename, &value, error);
1980 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
1981 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
1982 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
1983 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
1984 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
1985 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
1986 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
1987 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
1991 else if (g_str_has_prefix (attribute, "xattr::"))
1992 return set_xattr (filename, attribute, &value, error);
1993 else if (g_str_has_prefix (attribute, "xattr-sys::"))
1994 return set_xattr (filename, attribute, &value, error);
1997 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1998 _("Setting attribute %s not supported"), attribute);
2003 _g_local_file_info_set_attributes (char *filename,
2005 GFileQueryInfoFlags flags,
2006 GCancellable *cancellable,
2009 GFileAttributeValue *value, *uid, *gid;
2010 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2011 GFileAttributeStatus status;
2014 /* Handles setting multiple specified data in a single set, and takes care
2015 of ordering restrictions when setting attributes */
2019 /* Set symlink first, since this recreates the file */
2021 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2024 if (!set_symlink (filename, value, error))
2026 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2028 /* Don't set error multiple times */
2032 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2038 /* Group uid and gid setting into one call
2039 * Change ownership before permissions, since ownership changes can
2040 change permissions (e.g. setuid)
2042 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2043 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2047 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2049 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2051 /* Don't set error multiple times */
2055 status = G_FILE_ATTRIBUTE_STATUS_SET;
2057 uid->status = status;
2059 gid->status = status;
2063 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2066 if (!set_unix_mode (filename, value, error))
2068 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2070 /* Don't set error multiple times */
2074 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2079 /* Group all time settings into one call
2080 * Change times as the last thing to avoid it changing due to metadata changes
2083 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2084 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2085 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2086 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2088 if (mtime || mtime_usec || atime || atime_usec)
2090 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2092 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2094 /* Don't set error multiple times */
2098 status = G_FILE_ATTRIBUTE_STATUS_SET;
2101 mtime->status = status;
2103 mtime_usec->status = status;
2105 atime->status = status;
2107 atime_usec->status = status;
2111 /* xattrs are handled by default callback */