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->device = 0;
786 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME) ||
787 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE) ||
788 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH) ||
789 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT))
791 parent_info->writable = (g_access (dir, W_OK) == 0);
793 res = g_stat (dir, &statbuf);
796 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
797 * renamed or deleted only by the owner of the file, by the owner of the directory, and
798 * by a privileged process.
803 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
805 parent_info->is_sticky = FALSE;
807 parent_info->owner = statbuf.st_uid;
808 parent_info->device = statbuf.st_dev;
814 get_access_rights (GFileAttributeMatcher *attribute_matcher,
817 struct stat *statbuf,
818 GLocalParentFileInfo *parent_info)
820 if (g_file_attribute_matcher_matches (attribute_matcher,
821 G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
822 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
823 g_access (path, R_OK) == 0);
825 if (g_file_attribute_matcher_matches (attribute_matcher,
826 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
827 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
828 g_access (path, W_OK) == 0);
830 if (g_file_attribute_matcher_matches (attribute_matcher,
831 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
832 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE,
833 g_access (path, X_OK) == 0);
841 if (parent_info->writable)
843 if (parent_info->is_sticky)
846 uid_t uid = geteuid ();
848 if (uid == statbuf->st_uid ||
849 uid == parent_info->owner ||
858 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME))
859 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME,
862 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE))
863 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE,
866 /* TODO: This means we can move it, but we should also look for a trash dir */
867 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
868 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH,
874 set_info_from_stat (GFileInfo *info,
875 struct stat *statbuf,
876 GFileAttributeMatcher *attribute_matcher)
880 file_type = G_FILE_TYPE_UNKNOWN;
882 if (S_ISREG (statbuf->st_mode))
883 file_type = G_FILE_TYPE_REGULAR;
884 else if (S_ISDIR (statbuf->st_mode))
885 file_type = G_FILE_TYPE_DIRECTORY;
887 else if (S_ISCHR (statbuf->st_mode) ||
888 S_ISBLK (statbuf->st_mode) ||
889 S_ISFIFO (statbuf->st_mode)
891 || S_ISSOCK (statbuf->st_mode)
894 file_type = G_FILE_TYPE_SPECIAL;
897 else if (S_ISLNK (statbuf->st_mode))
898 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
901 g_file_info_set_file_type (info, file_type);
902 g_file_info_set_size (info, statbuf->st_size);
904 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE, statbuf->st_dev);
905 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_INODE, statbuf->st_ino);
906 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, statbuf->st_mode);
907 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_NLINK, statbuf->st_nlink);
908 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_UID, statbuf->st_uid);
909 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, statbuf->st_gid);
910 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_RDEV, statbuf->st_rdev);
911 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
912 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE, statbuf->st_blksize);
914 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
915 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_BLOCKS, statbuf->st_blocks);
918 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED, statbuf->st_mtime);
919 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
920 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
921 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
922 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
925 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS, statbuf->st_atime);
926 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
927 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
928 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
929 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
932 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_CHANGED, statbuf->st_ctime);
933 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
934 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
935 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
936 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
939 if (g_file_attribute_matcher_matches (attribute_matcher,
940 G_FILE_ATTRIBUTE_ETAG_VALUE))
942 char *etag = _g_local_file_info_create_etag (statbuf);
943 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ETAG_VALUE, etag);
947 if (g_file_attribute_matcher_matches (attribute_matcher,
948 G_FILE_ATTRIBUTE_ID_FILE))
950 char *id = _g_local_file_info_create_file_id (statbuf);
951 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE, id);
955 if (g_file_attribute_matcher_matches (attribute_matcher,
956 G_FILE_ATTRIBUTE_ID_FILESYSTEM))
958 char *id = _g_local_file_info_create_fs_id (statbuf);
959 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILESYSTEM, id);
965 make_valid_utf8 (const char *name)
968 const gchar *remainder, *invalid;
969 gint remaining_bytes, valid_bytes;
973 remaining_bytes = strlen (name);
975 while (remaining_bytes != 0)
977 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
979 valid_bytes = invalid - remainder;
982 string = g_string_sized_new (remaining_bytes);
984 g_string_append_len (string, remainder, valid_bytes);
985 /* append U+FFFD REPLACEMENT CHARACTER */
986 g_string_append (string, "\357\277\275");
988 remaining_bytes -= valid_bytes + 1;
989 remainder = invalid + 1;
993 return g_strdup (name);
995 g_string_append (string, remainder);
997 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
999 return g_string_free (string, FALSE);
1003 convert_pwd_string_to_utf8 (char *pwd_str)
1007 if (!g_utf8_validate (pwd_str, -1, NULL))
1009 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1010 if (utf8_string == NULL)
1011 utf8_string = make_valid_utf8 (pwd_str);
1014 utf8_string = g_strdup (pwd_str);
1020 uid_data_free (UidData *data)
1022 g_free (data->user_name);
1023 g_free (data->real_name);
1027 /* called with lock held */
1029 lookup_uid_data (uid_t uid)
1033 struct passwd pwbuf;
1034 struct passwd *pwbufp;
1035 char *gecos, *comma;
1037 if (uid_cache == NULL)
1038 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1040 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1045 data = g_new0 (UidData, 1);
1047 #if defined(HAVE_POSIX_GETPWUID_R)
1048 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1049 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1050 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1052 pwbufp = getpwuid (uid);
1057 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1058 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1060 gecos = pwbufp->pw_gecos;
1064 comma = strchr (gecos, ',');
1067 data->real_name = convert_pwd_string_to_utf8 (gecos);
1071 /* Default fallbacks */
1072 if (data->real_name == NULL)
1074 if (data->user_name != NULL)
1075 data->real_name = g_strdup (data->user_name);
1077 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1080 if (data->user_name == NULL)
1081 data->user_name = g_strdup_printf ("%d", (int)uid);
1083 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1089 get_username_from_uid (uid_t uid)
1095 data = lookup_uid_data (uid);
1096 res = g_strdup (data->user_name);
1097 G_UNLOCK (uid_cache);
1103 get_realname_from_uid (uid_t uid)
1109 data = lookup_uid_data (uid);
1110 res = g_strdup (data->real_name);
1111 G_UNLOCK (uid_cache);
1116 /* called with lock held */
1118 lookup_gid_name (gid_t gid)
1123 struct group *gbufp;
1125 if (gid_cache == NULL)
1126 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1128 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1133 #if defined (HAVE_POSIX_GETGRGID_R)
1134 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1135 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1136 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1138 gbufp = getgrgid (gid);
1141 if (gbufp != NULL &&
1142 gbufp->gr_name != NULL &&
1143 gbufp->gr_name[0] != 0)
1144 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1146 name = g_strdup_printf("%d", (int)gid);
1148 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1154 get_groupname_from_gid (gid_t gid)
1160 name = lookup_gid_name (gid);
1161 res = g_strdup (name);
1162 G_UNLOCK (gid_cache);
1165 #endif /* !G_OS_WIN32 */
1168 get_content_type (const char *basename,
1170 struct stat *statbuf,
1171 gboolean is_symlink,
1172 gboolean symlink_broken,
1173 GFileQueryInfoFlags flags,
1177 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1178 return g_strdup ("inode/symlink");
1179 else if (S_ISDIR(statbuf->st_mode))
1180 return g_strdup ("inode/directory");
1182 else if (S_ISCHR(statbuf->st_mode))
1183 return g_strdup ("inode/chardevice");
1184 else if (S_ISBLK(statbuf->st_mode))
1185 return g_strdup ("inode/blockdevice");
1186 else if (S_ISFIFO(statbuf->st_mode))
1187 return g_strdup ("inode/fifo");
1190 else if (S_ISSOCK(statbuf->st_mode))
1191 return g_strdup ("inode/socket");
1196 gboolean result_uncertain;
1198 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1201 if (!fast && result_uncertain && path != NULL)
1203 guchar sniff_buffer[4096];
1207 sniff_length = _g_unix_content_type_get_sniff_len ();
1208 if (sniff_length > 4096)
1209 sniff_length = 4096;
1211 fd = open (path, O_RDONLY);
1216 res = read (fd, sniff_buffer, sniff_length);
1220 g_free (content_type);
1221 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1227 return content_type;
1233 get_thumbnail_attributes (const char *path,
1236 GChecksum *checksum;
1241 uri = g_filename_to_uri (path, NULL, NULL);
1243 checksum = g_checksum_new (G_CHECKSUM_MD5);
1244 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1248 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1249 g_checksum_free (checksum);
1251 filename = g_build_filename (g_get_home_dir (),
1252 ".thumbnails", "normal", basename,
1255 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1256 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1260 filename = g_build_filename (g_get_home_dir (),
1261 ".thumbnails", "fail",
1262 "gnome-thumbnail-factory",
1266 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1267 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1275 win32_get_file_user_info (const gchar *filename,
1280 PSECURITY_DESCRIPTOR psd = NULL;
1281 DWORD sd_size = 0; /* first call calculates the size required */
1283 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1284 if ((GetFileSecurityW (wfilename,
1285 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1288 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1289 (psd = g_try_malloc (sd_size)) != NULL &&
1290 GetFileSecurityW (wfilename,
1291 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1298 SID_NAME_USE name_use = 0; /* don't care? */
1299 wchar_t *name = NULL;
1300 wchar_t *domain = NULL;
1302 DWORD domain_len = 0;
1303 /* get the user name */
1307 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1309 if (!LookupAccountSidW (NULL, /* local machine */
1312 domain, &domain_len, /* no domain info yet */
1313 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1315 name = g_try_malloc (name_len * sizeof (wchar_t));
1316 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1317 if (name && domain &&
1318 LookupAccountSidW (NULL, /* local machine */
1321 domain, &domain_len, /* no domain info yet */
1324 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1330 /* get the group name */
1334 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1336 if (!LookupAccountSidW (NULL, /* local machine */
1339 domain, &domain_len, /* no domain info yet */
1340 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1342 name = g_try_malloc (name_len * sizeof (wchar_t));
1343 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1344 if (name && domain &&
1345 LookupAccountSidW (NULL, /* local machine */
1348 domain, &domain_len, /* no domain info yet */
1351 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1357 /* TODO: get real name */
1363 #endif /* G_OS_WIN32 */
1366 _g_local_file_info_get (const char *basename,
1368 GFileAttributeMatcher *attribute_matcher,
1369 GFileQueryInfoFlags flags,
1370 GLocalParentFileInfo *parent_info,
1374 struct stat statbuf;
1375 struct stat statbuf2;
1377 gboolean is_symlink, symlink_broken;
1379 info = g_file_info_new ();
1381 /* Make sure we don't set any unwanted attributes */
1382 g_file_info_set_attribute_mask (info, attribute_matcher);
1384 g_file_info_set_name (info, basename);
1386 /* Avoid stat in trivial case */
1387 if (attribute_matcher == NULL)
1390 res = g_lstat (path, &statbuf);
1394 char *display_name = g_filename_display_name (path);
1395 g_object_unref (info);
1396 g_set_error (error, G_IO_ERROR,
1397 g_io_error_from_errno (errsv),
1398 _("Error stating file '%s': %s"),
1399 display_name, g_strerror (errsv));
1400 g_free (display_name);
1405 is_symlink = S_ISLNK (statbuf.st_mode);
1409 symlink_broken = FALSE;
1413 g_file_info_set_is_symlink (info, TRUE);
1415 /* Unless NOFOLLOW was set we default to following symlinks */
1416 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1418 res = stat (path, &statbuf2);
1420 /* Report broken links as symlinks */
1424 symlink_broken = TRUE;
1428 set_info_from_stat (info, &statbuf, attribute_matcher);
1430 if (basename != NULL && basename[0] == '.')
1431 g_file_info_set_is_hidden (info, TRUE);
1433 if (basename != NULL && basename[strlen (basename) -1] == '~')
1434 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP, TRUE);
1437 g_file_attribute_matcher_matches (attribute_matcher,
1438 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET))
1440 char *link = read_link (path);
1441 g_file_info_set_symlink_target (info, link);
1445 if (g_file_attribute_matcher_matches (attribute_matcher,
1446 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME))
1448 char *display_name = g_filename_display_basename (path);
1450 /* look for U+FFFD REPLACEMENT CHARACTER */
1451 if (strstr (display_name, "\357\277\275") != NULL)
1453 char *p = display_name;
1454 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1457 g_file_info_set_display_name (info, display_name);
1458 g_free (display_name);
1461 if (g_file_attribute_matcher_matches (attribute_matcher,
1462 G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME))
1464 char *edit_name = g_filename_display_basename (path);
1465 g_file_info_set_edit_name (info, edit_name);
1470 if (g_file_attribute_matcher_matches (attribute_matcher,
1471 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME))
1473 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1475 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME, copy_name);
1479 if (g_file_attribute_matcher_matches (attribute_matcher,
1480 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE) ||
1481 g_file_attribute_matcher_matches (attribute_matcher,
1482 G_FILE_ATTRIBUTE_STANDARD_ICON))
1484 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1488 g_file_info_set_content_type (info, content_type);
1490 if (g_file_attribute_matcher_matches (attribute_matcher,
1491 G_FILE_ATTRIBUTE_STANDARD_ICON))
1495 icon = g_content_type_get_icon (content_type);
1498 if (G_IS_THEMED_ICON (icon))
1500 const char *type_icon;
1502 /* TODO: Special case desktop dir? That could be expensive with xdg dirs... */
1503 if (strcmp (path, g_get_home_dir ()) == 0)
1504 type_icon = "user-home";
1505 else if (S_ISDIR (statbuf.st_mode))
1506 type_icon = "folder";
1507 else if (statbuf.st_mode & S_IXUSR)
1508 type_icon = "application-x-executable";
1510 type_icon = "text-x-generic";
1512 g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
1515 g_file_info_set_icon (info, icon);
1516 g_object_unref (icon);
1520 g_free (content_type);
1524 if (g_file_attribute_matcher_matches (attribute_matcher,
1525 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE))
1527 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1531 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE, content_type);
1532 g_free (content_type);
1536 if (g_file_attribute_matcher_matches (attribute_matcher,
1537 G_FILE_ATTRIBUTE_OWNER_USER))
1542 win32_get_file_user_info (path, NULL, &name, NULL);
1544 name = get_username_from_uid (statbuf.st_uid);
1547 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1551 if (g_file_attribute_matcher_matches (attribute_matcher,
1552 G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1556 win32_get_file_user_info (path, NULL, NULL, &name);
1558 name = get_realname_from_uid (statbuf.st_uid);
1561 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1565 if (g_file_attribute_matcher_matches (attribute_matcher,
1566 G_FILE_ATTRIBUTE_OWNER_GROUP))
1570 win32_get_file_user_info (path, &name, NULL, NULL);
1572 name = get_groupname_from_gid (statbuf.st_gid);
1575 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1579 if (parent_info && parent_info->device != 0 &&
1580 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1581 statbuf.st_dev != parent_info->device)
1582 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1584 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1586 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1587 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1588 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1590 if (g_file_attribute_matcher_matches (attribute_matcher,
1591 G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1592 get_thumbnail_attributes (path, info);
1594 g_file_info_unset_attribute_mask (info);
1600 _g_local_file_info_get_from_fd (int fd,
1604 struct stat stat_buf;
1605 GFileAttributeMatcher *matcher;
1608 if (fstat (fd, &stat_buf) == -1)
1612 g_set_error (error, G_IO_ERROR,
1613 g_io_error_from_errno (errsv),
1614 _("Error stating file descriptor: %s"),
1615 g_strerror (errsv));
1619 info = g_file_info_new ();
1621 matcher = g_file_attribute_matcher_new (attributes);
1623 /* Make sure we don't set any unwanted attributes */
1624 g_file_info_set_attribute_mask (info, matcher);
1626 set_info_from_stat (info, &stat_buf, matcher);
1629 if (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) &&
1630 is_selinux_enabled ())
1633 if (fgetfilecon_raw (fd, &context) >= 0)
1635 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
1641 get_xattrs_from_fd (fd, TRUE, info, matcher);
1642 get_xattrs_from_fd (fd, FALSE, info, matcher);
1644 g_file_attribute_matcher_unref (matcher);
1646 g_file_info_unset_attribute_mask (info);
1652 get_uint32 (const GFileAttributeValue *value,
1656 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1658 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1659 _("Invalid attribute type (uint32 expected)"));
1663 *val_out = value->u.uint32;
1669 get_uint64 (const GFileAttributeValue *value,
1673 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1675 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1676 _("Invalid attribute type (uint64 expected)"));
1680 *val_out = value->u.uint64;
1685 #if defined(HAVE_SYMLINK)
1687 get_byte_string (const GFileAttributeValue *value,
1688 const char **val_out,
1691 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1693 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1694 _("Invalid attribute type (byte string expected)"));
1698 *val_out = value->u.string;
1705 set_unix_mode (char *filename,
1706 const GFileAttributeValue *value,
1711 if (!get_uint32 (value, &val, error))
1714 if (g_chmod (filename, val) == -1)
1718 g_set_error (error, G_IO_ERROR,
1719 g_io_error_from_errno (errsv),
1720 _("Error setting permissions: %s"),
1721 g_strerror (errsv));
1729 set_unix_uid_gid (char *filename,
1730 const GFileAttributeValue *uid_value,
1731 const GFileAttributeValue *gid_value,
1732 GFileQueryInfoFlags flags,
1742 if (!get_uint32 (uid_value, &val, error))
1751 if (!get_uint32 (gid_value, &val, error))
1759 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1760 res = lchown (filename, uid, gid);
1763 res = chown (filename, uid, gid);
1769 g_set_error (error, G_IO_ERROR,
1770 g_io_error_from_errno (errsv),
1771 _("Error setting owner: %s"),
1772 g_strerror (errsv));
1781 set_symlink (char *filename,
1782 const GFileAttributeValue *value,
1786 struct stat statbuf;
1788 if (!get_byte_string (value, &val, error))
1793 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1794 _("symlink must be non-NULL"));
1798 if (g_lstat (filename, &statbuf))
1802 g_set_error (error, G_IO_ERROR,
1803 g_io_error_from_errno (errsv),
1804 _("Error setting symlink: %s"),
1805 g_strerror (errsv));
1809 if (!S_ISLNK (statbuf.st_mode))
1811 g_set_error (error, G_IO_ERROR,
1812 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1813 _("Error setting symlink: file is not a symlink"));
1817 if (g_unlink (filename))
1821 g_set_error (error, G_IO_ERROR,
1822 g_io_error_from_errno (errsv),
1823 _("Error setting symlink: %s"),
1824 g_strerror (errsv));
1828 if (symlink (filename, val) != 0)
1832 g_set_error (error, G_IO_ERROR,
1833 g_io_error_from_errno (errsv),
1834 _("Error setting symlink: %s"),
1835 g_strerror (errsv));
1844 lazy_stat (char *filename,
1845 struct stat *statbuf,
1846 gboolean *called_stat)
1853 res = g_stat (filename, statbuf);
1856 *called_stat = TRUE;
1864 set_mtime_atime (char *filename,
1865 const GFileAttributeValue *mtime_value,
1866 const GFileAttributeValue *mtime_usec_value,
1867 const GFileAttributeValue *atime_value,
1868 const GFileAttributeValue *atime_usec_value,
1874 struct stat statbuf;
1875 gboolean got_stat = FALSE;
1876 struct timeval times[2] = { {0, 0}, {0, 0} };
1881 if (!get_uint64 (atime_value, &val, error))
1883 times[0].tv_sec = val;
1887 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1889 times[0].tv_sec = statbuf.st_mtime;
1890 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1891 times[0].tv_usec = statbuf.st_atimensec / 1000;
1892 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1893 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
1898 if (atime_usec_value)
1900 if (!get_uint32 (atime_usec_value, &val_usec, error))
1902 times[0].tv_usec = val_usec;
1908 if (!get_uint64 (mtime_value, &val, error))
1910 times[1].tv_sec = val;
1914 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1916 times[1].tv_sec = statbuf.st_mtime;
1917 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
1918 times[1].tv_usec = statbuf.st_mtimensec / 1000;
1919 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
1920 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
1925 if (mtime_usec_value)
1927 if (!get_uint32 (mtime_usec_value, &val_usec, error))
1929 times[1].tv_usec = val_usec;
1932 res = utimes (filename, times);
1937 g_set_error (error, G_IO_ERROR,
1938 g_io_error_from_errno (errsv),
1939 _("Error setting owner: %s"),
1940 g_strerror (errsv));
1948 _g_local_file_info_set_attribute (char *filename,
1949 const char *attribute,
1950 GFileAttributeType type,
1952 GFileQueryInfoFlags flags,
1953 GCancellable *cancellable,
1956 GFileAttributeValue value = { 0 };
1958 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
1960 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
1961 return set_unix_mode (filename, &value, error);
1964 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
1965 return set_unix_uid_gid (filename, &value, NULL, flags, error);
1966 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
1967 return set_unix_uid_gid (filename, NULL, &value, flags, error);
1971 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
1972 return set_symlink (filename, &value, error);
1976 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
1977 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
1978 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
1979 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
1980 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
1981 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
1982 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
1983 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
1987 else if (g_str_has_prefix (attribute, "xattr::"))
1988 return set_xattr (filename, attribute, &value, error);
1989 else if (g_str_has_prefix (attribute, "xattr-sys::"))
1990 return set_xattr (filename, attribute, &value, error);
1993 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1994 _("Setting attribute %s not supported"), attribute);
1999 _g_local_file_info_set_attributes (char *filename,
2001 GFileQueryInfoFlags flags,
2002 GCancellable *cancellable,
2005 GFileAttributeValue *value, *uid, *gid;
2006 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2007 GFileAttributeStatus status;
2010 /* Handles setting multiple specified data in a single set, and takes care
2011 of ordering restrictions when setting attributes */
2015 /* Set symlink first, since this recreates the file */
2017 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2020 if (!set_symlink (filename, value, error))
2022 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2024 /* Don't set error multiple times */
2028 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2034 /* Group uid and gid setting into one call
2035 * Change ownership before permissions, since ownership changes can
2036 change permissions (e.g. setuid)
2038 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2039 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2043 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2045 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2047 /* Don't set error multiple times */
2051 status = G_FILE_ATTRIBUTE_STATUS_SET;
2053 uid->status = status;
2055 gid->status = status;
2059 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2062 if (!set_unix_mode (filename, value, error))
2064 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2066 /* Don't set error multiple times */
2070 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2075 /* Group all time settings into one call
2076 * Change times as the last thing to avoid it changing due to metadata changes
2079 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2080 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2081 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2082 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2084 if (mtime || mtime_usec || atime || atime_usec)
2086 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2088 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2090 /* Don't set error multiple times */
2094 status = G_FILE_ATTRIBUTE_STATUS_SET;
2097 mtime->status = status;
2099 mtime_usec->status = status;
2101 atime->status = status;
2103 atime_usec->status = status;
2107 /* xattrs are handled by default callback */