1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #ifdef HAVE_SYS_TIME_H
28 #include <sys/types.h>
43 #include <selinux/selinux.h>
48 #if defined HAVE_SYS_XATTR_H
49 #include <sys/xattr.h>
50 #elif defined HAVE_ATTR_XATTR_H
51 #include <attr/xattr.h>
53 #error "Neither <sys/xattr.h> nor <attr/xattr.h> is present but extended attribute support is enabled."
54 #endif /* defined HAVE_SYS_XATTR_H || HAVE_ATTR_XATTR_H */
56 #endif /* HAVE_XATTR */
58 #include <glib/gstdio.h>
59 #include <glib/gchecksum.h>
73 #define X_OK 0 /* not really */
76 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
79 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
82 #define S_IXUSR _S_IEXEC
86 #include "glocalfileinfo.h"
88 #include "gthemedicon.h"
89 #include "gcontenttype.h"
90 #include "gcontenttypeprivate.h"
94 struct ThumbMD5Context {
105 G_LOCK_DEFINE_STATIC (uid_cache);
106 static GHashTable *uid_cache = NULL;
108 G_LOCK_DEFINE_STATIC (gid_cache);
109 static GHashTable *gid_cache = NULL;
112 _g_local_file_info_create_etag (struct stat *statbuf)
116 tv.tv_sec = statbuf->st_mtime;
117 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
118 tv.tv_usec = statbuf->st_mtimensec / 1000;
119 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
120 tv.tv_usec = statbuf->st_mtim.tv_nsec / 1000;
125 return g_strdup_printf ("%lu:%lu", tv.tv_sec, tv.tv_usec);
129 _g_local_file_info_create_file_id (struct stat *statbuf)
131 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
132 (guint64) statbuf->st_dev,
133 (guint64) statbuf->st_ino);
137 _g_local_file_info_create_fs_id (struct stat *statbuf)
139 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
140 (guint64) statbuf->st_dev);
145 read_link (const gchar *full_name)
152 buffer = g_malloc (size);
158 read_size = readlink (full_name, buffer, size);
164 if (read_size < size)
166 buffer[read_size] = 0;
170 buffer = g_realloc (buffer, size);
177 /* Get the SELinux security context */
179 get_selinux_context (const char *path,
181 GFileAttributeMatcher *attribute_matcher,
182 gboolean follow_symlinks)
187 if (!g_file_attribute_matcher_matches (attribute_matcher, "selinux::context"))
190 if (is_selinux_enabled ())
194 if (lgetfilecon_raw (path, &context) < 0)
199 if (getfilecon_raw (path, &context) < 0)
205 g_file_info_set_attribute_string (info, "selinux::context", context);
214 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
215 * (Mac) getxattr(..., XATTR_NOFOLLOW)
217 #ifdef HAVE_XATTR_NOFOLLOW
218 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
219 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
220 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
222 #define g_fgetxattr fgetxattr
223 #define g_flistxattr flistxattr
224 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
228 g_getxattr (const char *path, const char *name, void *value, size_t size,
229 gboolean follow_symlinks)
231 #ifdef HAVE_XATTR_NOFOLLOW
232 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
235 return getxattr (path, name, value, size);
237 return lgetxattr (path, name, value, size);
242 g_listxattr(const char *path, char *namebuf, size_t size,
243 gboolean follow_symlinks)
245 #ifdef HAVE_XATTR_NOFOLLOW
246 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
249 return listxattr (path, namebuf, size);
251 return llistxattr (path, namebuf, size);
258 return c >= 32 && c <= 126 && c != '\\';
262 name_is_valid (const char *str)
266 if (!valid_char (*str++))
273 hex_escape_string (const char *str,
274 gboolean *free_return)
277 char *escaped_str, *p;
279 static char *hex_digits = "0123456789abcdef";
285 for (i = 0; i < len; i++)
287 if (!valid_char (str[i]))
291 if (num_invalid == 0)
293 *free_return = FALSE;
297 escaped_str = g_malloc (len + num_invalid*3 + 1);
300 for (i = 0; i < len; i++)
302 if (valid_char (str[i]))
309 *p++ = hex_digits[(c >> 4) & 0xf];
310 *p++ = hex_digits[c & 0xf];
320 hex_unescape_string (const char *str,
322 gboolean *free_return)
325 char *unescaped_str, *p;
331 if (strchr (str, '\\') == NULL)
335 *free_return = FALSE;
339 unescaped_str = g_malloc (len + 1);
342 for (i = 0; i < len; i++)
344 if (str[i] == '\\' &&
349 (g_ascii_xdigit_value (str[i+2]) << 4) |
350 g_ascii_xdigit_value (str[i+3]);
360 *out_len = p - unescaped_str;
362 return unescaped_str;
366 escape_xattr (GFileInfo *info,
367 const char *gio_attr, /* gio attribute name */
368 const char *value, /* Is zero terminated */
369 size_t len /* not including zero termination */)
372 gboolean free_escaped_val;
374 escaped_val = hex_escape_string (value, &free_escaped_val);
376 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
378 if (free_escaped_val)
379 g_free (escaped_val);
383 get_one_xattr (const char *path,
385 const char *gio_attr,
387 gboolean follow_symlinks)
393 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
398 else if (len == -1 && errno == ERANGE)
400 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
405 value_p = g_malloc (len+1);
407 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
421 escape_xattr (info, gio_attr, value_p, len);
423 if (value_p != value)
427 #endif /* defined HAVE_XATTR */
430 get_xattrs (const char *path,
433 GFileAttributeMatcher *matcher,
434 gboolean follow_symlinks)
439 ssize_t list_res_size;
442 const char *attr, *attr2;
445 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
447 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr_sys");
451 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
453 if (list_res_size == -1 ||
457 list_size = list_res_size;
458 list = g_malloc (list_size);
462 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
464 if (list_res_size == -1 && errno == ERANGE)
466 list_size = list_size * 2;
467 list = g_realloc (list, list_size);
471 if (list_res_size == -1)
475 while (list_res_size > 0)
477 if ((user && g_str_has_prefix (attr, "user.")) ||
478 (!user && !g_str_has_prefix (attr, "user.")))
480 char *escaped_attr, *gio_attr;
481 gboolean free_escaped_attr;
485 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
486 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
490 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
491 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
494 if (free_escaped_attr)
495 g_free (escaped_attr);
497 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
500 len = strlen (attr) + 1;
502 list_res_size -= len;
509 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
511 char *unescaped_attribute, *a;
512 gboolean free_unescaped_attribute;
514 attr2 = strchr (attr, ':');
517 attr2++; /* Skip ':' */
518 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
520 a = g_strconcat ("user.", unescaped_attribute, NULL);
522 a = unescaped_attribute;
524 get_one_xattr (path, info, attr, a, follow_symlinks);
529 if (free_unescaped_attribute)
530 g_free (unescaped_attribute);
534 #endif /* defined HAVE_XATTR */
539 get_one_xattr_from_fd (int fd,
541 const char *gio_attr,
548 len = g_fgetxattr (fd, xattr, value, sizeof (value)-1);
553 else if (len == -1 && errno == ERANGE)
555 len = g_fgetxattr (fd, xattr, NULL, 0);
560 value_p = g_malloc (len+1);
562 len = g_fgetxattr (fd, xattr, value_p, len);
576 escape_xattr (info, gio_attr, value_p, len);
578 if (value_p != value)
581 #endif /* defined HAVE_XATTR */
584 get_xattrs_from_fd (int fd,
587 GFileAttributeMatcher *matcher)
592 ssize_t list_res_size;
595 const char *attr, *attr2;
598 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
600 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
604 list_res_size = g_flistxattr (fd, NULL, 0);
606 if (list_res_size == -1 ||
610 list_size = list_res_size;
611 list = g_malloc (list_size);
615 list_res_size = g_flistxattr (fd, list, list_size);
617 if (list_res_size == -1 && errno == ERANGE)
619 list_size = list_size * 2;
620 list = g_realloc (list, list_size);
624 if (list_res_size == -1)
628 while (list_res_size > 0)
630 if ((user && g_str_has_prefix (attr, "user.")) ||
631 (!user && !g_str_has_prefix (attr, "user.")))
633 char *escaped_attr, *gio_attr;
634 gboolean free_escaped_attr;
638 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
639 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
643 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
644 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
647 if (free_escaped_attr)
648 g_free (escaped_attr);
650 get_one_xattr_from_fd (fd, info, gio_attr, attr);
653 len = strlen (attr) + 1;
655 list_res_size -= len;
662 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
664 char *unescaped_attribute, *a;
665 gboolean free_unescaped_attribute;
667 attr2 = strchr (attr, ':');
670 attr2++; /* Skip ':' */
671 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
673 a = g_strconcat ("user.", unescaped_attribute, NULL);
675 a = unescaped_attribute;
677 get_one_xattr_from_fd (fd, info, attr, a);
682 if (free_unescaped_attribute)
683 g_free (unescaped_attribute);
687 #endif /* defined HAVE_XATTR */
692 set_xattr (char *filename,
693 const char *escaped_attribute,
694 const GFileAttributeValue *attr_value,
697 char *attribute, *value;
698 gboolean free_attribute, free_value;
699 int val_len, res, errsv;
703 if (attr_value == NULL)
705 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
706 _("Attribute value must be non-NULL"));
710 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
712 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
713 _("Invalid attribute type (string expected)"));
717 if (!name_is_valid (escaped_attribute))
719 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
720 _("Invalid extended attribute name"));
724 if (g_str_has_prefix (escaped_attribute, "xattr::"))
726 escaped_attribute += 6;
731 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
732 escaped_attribute += 10;
736 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
737 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
740 a = g_strconcat ("user.", attribute, NULL);
744 res = g_setxattr (filename, a, value, val_len);
758 g_set_error (error, G_IO_ERROR,
759 g_io_error_from_errno (errsv),
760 _("Error setting extended attribute '%s': %s"),
761 escaped_attribute, g_strerror (errno));
772 _g_local_file_info_get_parent_info (const char *dir,
773 GFileAttributeMatcher *attribute_matcher,
774 GLocalParentFileInfo *parent_info)
779 parent_info->writable = FALSE;
780 parent_info->is_sticky = FALSE;
781 parent_info->device = 0;
783 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME) ||
784 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE) ||
785 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH) ||
786 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT))
788 parent_info->writable = (g_access (dir, W_OK) == 0);
790 res = g_stat (dir, &statbuf);
793 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
794 * renamed or deleted only by the owner of the file, by the owner of the directory, and
795 * by a privileged process.
800 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
802 parent_info->is_sticky = FALSE;
804 parent_info->owner = statbuf.st_uid;
805 parent_info->device = statbuf.st_dev;
811 get_access_rights (GFileAttributeMatcher *attribute_matcher,
814 struct stat *statbuf,
815 GLocalParentFileInfo *parent_info)
817 if (g_file_attribute_matcher_matches (attribute_matcher,
818 G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
819 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
820 g_access (path, R_OK) == 0);
822 if (g_file_attribute_matcher_matches (attribute_matcher,
823 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
824 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
825 g_access (path, W_OK) == 0);
827 if (g_file_attribute_matcher_matches (attribute_matcher,
828 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
829 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE,
830 g_access (path, X_OK) == 0);
838 if (parent_info->writable)
840 if (parent_info->is_sticky)
843 uid_t uid = geteuid ();
845 if (uid == statbuf->st_uid ||
846 uid == parent_info->owner ||
855 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME))
856 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME,
859 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE))
860 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE,
863 /* TODO: This means we can move it, but we should also look for a trash dir */
864 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
865 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH,
871 set_info_from_stat (GFileInfo *info,
872 struct stat *statbuf,
873 GFileAttributeMatcher *attribute_matcher)
877 file_type = G_FILE_TYPE_UNKNOWN;
879 if (S_ISREG (statbuf->st_mode))
880 file_type = G_FILE_TYPE_REGULAR;
881 else if (S_ISDIR (statbuf->st_mode))
882 file_type = G_FILE_TYPE_DIRECTORY;
884 else if (S_ISCHR (statbuf->st_mode) ||
885 S_ISBLK (statbuf->st_mode) ||
886 S_ISFIFO (statbuf->st_mode)
888 || S_ISSOCK (statbuf->st_mode)
891 file_type = G_FILE_TYPE_SPECIAL;
894 else if (S_ISLNK (statbuf->st_mode))
895 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
898 g_file_info_set_file_type (info, file_type);
899 g_file_info_set_size (info, statbuf->st_size);
901 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE, statbuf->st_dev);
902 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_INODE, statbuf->st_ino);
903 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, statbuf->st_mode);
904 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_NLINK, statbuf->st_nlink);
905 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_UID, statbuf->st_uid);
906 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, statbuf->st_gid);
907 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_RDEV, statbuf->st_rdev);
908 #if defined (HAVE_STRUCT_STAT_BLKSIZE)
909 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE, statbuf->st_blksize);
911 #if defined (HAVE_STRUCT_STAT_BLOCKS)
912 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_BLOCKS, statbuf->st_blocks);
915 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED, statbuf->st_mtime);
916 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
917 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
918 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
919 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
922 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS, statbuf->st_atime);
923 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
924 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
925 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
926 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
929 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_CHANGED, statbuf->st_ctime);
930 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
931 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
932 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
933 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
936 if (g_file_attribute_matcher_matches (attribute_matcher,
937 G_FILE_ATTRIBUTE_ETAG_VALUE))
939 char *etag = _g_local_file_info_create_etag (statbuf);
940 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ETAG_VALUE, etag);
944 if (g_file_attribute_matcher_matches (attribute_matcher,
945 G_FILE_ATTRIBUTE_ID_FILE))
947 char *id = _g_local_file_info_create_file_id (statbuf);
948 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE, id);
952 if (g_file_attribute_matcher_matches (attribute_matcher,
953 G_FILE_ATTRIBUTE_ID_FS))
955 char *id = _g_local_file_info_create_fs_id (statbuf);
956 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FS, id);
962 make_valid_utf8 (const char *name)
965 const gchar *remainder, *invalid;
966 gint remaining_bytes, valid_bytes;
970 remaining_bytes = strlen (name);
972 while (remaining_bytes != 0)
974 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
976 valid_bytes = invalid - remainder;
979 string = g_string_sized_new (remaining_bytes);
981 g_string_append_len (string, remainder, valid_bytes);
982 /* append U+FFFD REPLACEMENT CHARACTER */
983 g_string_append (string, "\357\277\275");
985 remaining_bytes -= valid_bytes + 1;
986 remainder = invalid + 1;
990 return g_strdup (name);
992 g_string_append (string, remainder);
994 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
996 return g_string_free (string, FALSE);
1000 convert_pwd_string_to_utf8 (char *pwd_str)
1004 if (!g_utf8_validate (pwd_str, -1, NULL))
1006 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1007 if (utf8_string == NULL)
1008 utf8_string = make_valid_utf8 (pwd_str);
1011 utf8_string = g_strdup (pwd_str);
1017 uid_data_free (UidData *data)
1019 g_free (data->user_name);
1020 g_free (data->real_name);
1024 /* called with lock held */
1026 lookup_uid_data (uid_t uid)
1030 struct passwd pwbuf;
1031 struct passwd *pwbufp;
1032 char *gecos, *comma;
1034 if (uid_cache == NULL)
1035 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1037 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1042 data = g_new0 (UidData, 1);
1044 #if defined(HAVE_POSIX_GETPWUID_R)
1045 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1046 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1047 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1049 pwbufp = getpwuid (uid);
1054 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1055 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1057 gecos = pwbufp->pw_gecos;
1061 comma = strchr (gecos, ',');
1064 data->real_name = convert_pwd_string_to_utf8 (gecos);
1068 /* Default fallbacks */
1069 if (data->real_name == NULL)
1071 if (data->user_name != NULL)
1072 data->real_name = g_strdup (data->user_name);
1074 data->real_name = g_strdup_printf("user #%d", (int)uid);
1077 if (data->user_name == NULL)
1078 data->user_name = g_strdup_printf("%d", (int)uid);
1080 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1086 get_username_from_uid (uid_t uid)
1092 data = lookup_uid_data (uid);
1093 res = g_strdup (data->user_name);
1094 G_UNLOCK (uid_cache);
1100 get_realname_from_uid (uid_t uid)
1106 data = lookup_uid_data (uid);
1107 res = g_strdup (data->real_name);
1108 G_UNLOCK (uid_cache);
1113 /* called with lock held */
1115 lookup_gid_name (gid_t gid)
1120 struct group *gbufp;
1122 if (gid_cache == NULL)
1123 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1125 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1130 #if defined (HAVE_POSIX_GETGRGID_R)
1131 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1132 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1133 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1135 gbufp = getgrgid (gid);
1138 if (gbufp != NULL &&
1139 gbufp->gr_name != NULL &&
1140 gbufp->gr_name[0] != 0)
1141 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1143 name = g_strdup_printf("%d", (int)gid);
1145 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1151 get_groupname_from_gid (gid_t gid)
1157 name = lookup_gid_name (gid);
1158 res = g_strdup (name);
1159 G_UNLOCK (gid_cache);
1162 #endif /* !G_OS_WIN32 */
1165 get_content_type (const char *basename,
1167 struct stat *statbuf,
1168 gboolean is_symlink,
1169 gboolean symlink_broken,
1170 GFileQueryInfoFlags flags,
1174 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1175 return g_strdup ("inode/symlink");
1176 else if (S_ISDIR(statbuf->st_mode))
1177 return g_strdup ("inode/directory");
1179 else if (S_ISCHR(statbuf->st_mode))
1180 return g_strdup ("inode/chardevice");
1181 else if (S_ISBLK(statbuf->st_mode))
1182 return g_strdup ("inode/blockdevice");
1183 else if (S_ISFIFO(statbuf->st_mode))
1184 return g_strdup ("inode/fifo");
1187 else if (S_ISSOCK(statbuf->st_mode))
1188 return g_strdup ("inode/socket");
1193 gboolean result_uncertain;
1195 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1198 if (!fast && result_uncertain && path != NULL)
1200 guchar sniff_buffer[4096];
1204 sniff_length = _g_unix_content_type_get_sniff_len ();
1205 if (sniff_length > 4096)
1206 sniff_length = 4096;
1208 fd = open (path, O_RDONLY);
1213 res = read (fd, sniff_buffer, sniff_length);
1217 g_free (content_type);
1218 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1224 return content_type;
1230 get_thumbnail_attributes (const char *path,
1233 GChecksum *checksum;
1238 uri = g_filename_to_uri (path, NULL, NULL);
1240 checksum = g_checksum_new (G_CHECKSUM_MD5);
1241 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1243 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1244 g_checksum_free (checksum);
1246 filename = g_build_filename (g_get_home_dir(),
1247 ".thumbnails", "normal", basename,
1250 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1251 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1255 filename = g_build_filename (g_get_home_dir(),
1256 ".thumbnails", "fail",
1257 "gnome-thumbnail-factory",
1261 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1262 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1270 win32_get_file_user_info (const gchar* filename,
1275 PSECURITY_DESCRIPTOR psd = NULL;
1276 DWORD sd_size = 0; /* first call calculates the size required */
1278 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1279 if ((GetFileSecurityW (wfilename,
1280 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1283 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1284 (psd = g_try_malloc (sd_size)) != NULL &&
1285 GetFileSecurityW (wfilename,
1286 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1293 SID_NAME_USE name_use = 0; /* don't care? */
1294 wchar_t *name = NULL;
1295 wchar_t *domain = NULL;
1297 DWORD domain_len = 0;
1298 /* get the user name */
1302 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1304 if (!LookupAccountSidW (NULL, /* local machine */
1307 domain, &domain_len, /* no domain info yet */
1308 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1310 name = g_try_malloc (name_len*sizeof(wchar_t));
1311 domain = g_try_malloc (domain_len*sizeof(wchar_t));
1312 if (name && domain &&
1313 LookupAccountSidW (NULL, /* local machine */
1316 domain, &domain_len, /* no domain info yet */
1319 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1325 /* get the group name */
1329 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1331 if (!LookupAccountSidW (NULL, /* local machine */
1334 domain, &domain_len, /* no domain info yet */
1335 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1337 name = g_try_malloc (name_len*sizeof(wchar_t));
1338 domain = g_try_malloc (domain_len*sizeof(wchar_t));
1339 if (name && domain &&
1340 LookupAccountSidW (NULL, /* local machine */
1343 domain, &domain_len, /* no domain info yet */
1346 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1352 /* TODO: get real name */
1358 #endif /* G_OS_WIN32 */
1361 _g_local_file_info_get (const char *basename,
1363 GFileAttributeMatcher *attribute_matcher,
1364 GFileQueryInfoFlags flags,
1365 GLocalParentFileInfo *parent_info,
1369 struct stat statbuf;
1370 struct stat statbuf2;
1372 gboolean is_symlink, symlink_broken;
1374 info = g_file_info_new ();
1376 /* Make sure we don't set any unwanted attributes */
1377 g_file_info_set_attribute_mask (info, attribute_matcher);
1379 g_file_info_set_name (info, basename);
1381 /* Avoid stat in trivial case */
1382 if (attribute_matcher == NULL)
1385 res = g_lstat (path, &statbuf);
1388 g_object_unref (info);
1389 g_set_error (error, G_IO_ERROR,
1390 g_io_error_from_errno (errno),
1391 _("Error stating file '%s': %s"),
1392 path, g_strerror (errno));
1397 is_symlink = S_ISLNK (statbuf.st_mode);
1401 symlink_broken = FALSE;
1405 g_file_info_set_is_symlink (info, TRUE);
1407 /* Unless NOFOLLOW was set we default to following symlinks */
1408 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1410 res = stat (path, &statbuf2);
1412 /* Report broken links as symlinks */
1416 symlink_broken = TRUE;
1420 set_info_from_stat (info, &statbuf, attribute_matcher);
1422 if (basename != NULL && basename[0] == '.')
1423 g_file_info_set_is_hidden (info, TRUE);
1425 if (basename != NULL && basename[strlen (basename) -1] == '~')
1426 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STD_IS_BACKUP, TRUE);
1429 g_file_attribute_matcher_matches (attribute_matcher,
1430 G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET))
1432 char *link = read_link (path);
1433 g_file_info_set_symlink_target (info, link);
1437 if (g_file_attribute_matcher_matches (attribute_matcher,
1438 G_FILE_ATTRIBUTE_STD_DISPLAY_NAME))
1440 char *display_name = g_filename_display_basename (path);
1442 if (strstr (display_name, "\357\277\275") != NULL)
1444 char *p = display_name;
1445 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1448 g_file_info_set_display_name (info, display_name);
1449 g_free (display_name);
1452 if (g_file_attribute_matcher_matches (attribute_matcher,
1453 G_FILE_ATTRIBUTE_STD_EDIT_NAME))
1455 char *edit_name = g_filename_display_basename (path);
1456 g_file_info_set_edit_name (info, edit_name);
1461 if (g_file_attribute_matcher_matches (attribute_matcher,
1462 G_FILE_ATTRIBUTE_STD_COPY_NAME))
1464 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1466 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STD_COPY_NAME, copy_name);
1470 if (g_file_attribute_matcher_matches (attribute_matcher,
1471 G_FILE_ATTRIBUTE_STD_CONTENT_TYPE) ||
1472 g_file_attribute_matcher_matches (attribute_matcher,
1473 G_FILE_ATTRIBUTE_STD_ICON))
1475 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1479 g_file_info_set_content_type (info, content_type);
1481 if (g_file_attribute_matcher_matches (attribute_matcher,
1482 G_FILE_ATTRIBUTE_STD_ICON))
1484 char *mimetype_icon, *generic_mimetype_icon, *type_icon, *p;
1485 char *icon_names[3];
1489 mimetype_icon = g_strdup (content_type);
1491 while ((p = strchr(mimetype_icon, '/')) != NULL)
1494 p = strchr (content_type, '/');
1496 p = content_type + strlen (content_type);
1498 generic_mimetype_icon = g_malloc (p - content_type + strlen ("-x-generic") + 1);
1499 memcpy (generic_mimetype_icon, content_type, p - content_type);
1500 memcpy (generic_mimetype_icon + (p - content_type), "-x-generic", strlen ("-x-generic"));
1501 generic_mimetype_icon[(p - content_type) + strlen ("-x-generic")] = 0;
1503 /* TODO: Special case desktop dir? That could be expensive with xdg dirs... */
1504 if (strcmp (path, g_get_home_dir ()) == 0)
1505 type_icon = "user-home";
1506 else if (S_ISDIR (statbuf.st_mode))
1507 type_icon = "folder";
1508 else if (statbuf.st_mode & S_IXUSR)
1509 type_icon = "application-x-executable";
1511 type_icon = "text-x-generic";
1514 icon_names[i++] = mimetype_icon;
1515 icon_names[i++] = generic_mimetype_icon;
1516 if (strcmp (generic_mimetype_icon, type_icon) != 0 &&
1517 strcmp (mimetype_icon, type_icon) != 0)
1518 icon_names[i++] = type_icon;
1520 icon = g_themed_icon_new_from_names (icon_names, i);
1521 g_file_info_set_icon (info, icon);
1523 g_object_unref (icon);
1524 g_free (mimetype_icon);
1525 g_free (generic_mimetype_icon);
1528 g_free (content_type);
1532 if (g_file_attribute_matcher_matches (attribute_matcher,
1533 G_FILE_ATTRIBUTE_STD_FAST_CONTENT_TYPE))
1535 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1539 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STD_FAST_CONTENT_TYPE, content_type);
1540 g_free (content_type);
1544 if (g_file_attribute_matcher_matches (attribute_matcher,
1545 G_FILE_ATTRIBUTE_OWNER_USER))
1550 win32_get_file_user_info (path, NULL, &name, NULL);
1552 name = get_username_from_uid (statbuf.st_uid);
1555 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1559 if (g_file_attribute_matcher_matches (attribute_matcher,
1560 G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1564 win32_get_file_user_info (path, NULL, NULL, &name);
1566 name = get_realname_from_uid (statbuf.st_uid);
1569 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1573 if (g_file_attribute_matcher_matches (attribute_matcher,
1574 G_FILE_ATTRIBUTE_OWNER_GROUP))
1578 win32_get_file_user_info (path, &name, NULL, NULL);
1580 name = get_groupname_from_gid (statbuf.st_gid);
1583 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1587 if (parent_info && parent_info->device != 0 &&
1588 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1589 statbuf.st_dev != parent_info->device)
1590 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1592 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1594 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1595 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1596 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1598 if (g_file_attribute_matcher_matches (attribute_matcher,
1599 G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1600 get_thumbnail_attributes (path, info);
1602 g_file_info_unset_attribute_mask (info);
1608 _g_local_file_info_get_from_fd (int fd,
1612 struct stat stat_buf;
1613 GFileAttributeMatcher *matcher;
1616 if (fstat (fd, &stat_buf) == -1)
1618 g_set_error (error, G_IO_ERROR,
1619 g_io_error_from_errno (errno),
1620 _("Error stating file descriptor: %s"),
1621 g_strerror (errno));
1625 info = g_file_info_new ();
1627 matcher = g_file_attribute_matcher_new (attributes);
1629 /* Make sure we don't set any unwanted attributes */
1630 g_file_info_set_attribute_mask (info, matcher);
1632 set_info_from_stat (info, &stat_buf, matcher);
1635 if (g_file_attribute_matcher_matches (matcher, "selinux:context") &&
1636 is_selinux_enabled ())
1639 if (fgetfilecon_raw (fd, &context) >= 0)
1641 g_file_info_set_attribute_string (info, "selinux:context", context);
1647 get_xattrs_from_fd (fd, TRUE, info, matcher);
1648 get_xattrs_from_fd (fd, FALSE, info, matcher);
1650 g_file_attribute_matcher_unref (matcher);
1652 g_file_info_unset_attribute_mask (info);
1658 get_uint32 (const GFileAttributeValue *value,
1662 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1664 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1665 _("Invalid attribute type (uint32 expected)"));
1669 *val_out = value->u.uint32;
1675 get_uint64 (const GFileAttributeValue *value,
1679 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1681 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1682 _("Invalid attribute type (uint64 expected)"));
1686 *val_out = value->u.uint64;
1691 #if defined(HAVE_SYMLINK)
1693 get_byte_string (const GFileAttributeValue *value,
1694 const char **val_out,
1697 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1699 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1700 _("Invalid attribute type (byte string expected)"));
1704 *val_out = value->u.string;
1711 set_unix_mode (char *filename,
1712 const GFileAttributeValue *value,
1717 if (!get_uint32 (value, &val, error))
1720 if (g_chmod (filename, val) == -1)
1722 g_set_error (error, G_IO_ERROR,
1723 g_io_error_from_errno (errno),
1724 _("Error setting permissions: %s"),
1725 g_strerror (errno));
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))
1762 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1763 res = lchown (filename, uid, gid);
1765 res = chown (filename, uid, gid);
1769 g_set_error (error, G_IO_ERROR,
1770 g_io_error_from_errno (errno),
1771 _("Error setting owner: %s"),
1772 g_strerror (errno));
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))
1800 g_set_error (error, G_IO_ERROR,
1801 g_io_error_from_errno (errno),
1802 _("Error setting symlink: %s"),
1803 g_strerror (errno));
1807 if (!S_ISLNK (statbuf.st_mode))
1809 g_set_error (error, G_IO_ERROR,
1810 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1811 _("Error setting symlink: file is not a symlink"));
1815 if (g_unlink (filename))
1817 g_set_error (error, G_IO_ERROR,
1818 g_io_error_from_errno (errno),
1819 _("Error setting symlink: %s"),
1820 g_strerror (errno));
1824 if (symlink (filename, val) != 0)
1826 g_set_error (error, G_IO_ERROR,
1827 g_io_error_from_errno (errno),
1828 _("Error setting symlink: %s"),
1829 g_strerror (errno));
1838 lazy_stat (char *filename,
1839 struct stat *statbuf,
1840 gboolean *called_stat)
1847 res = g_stat (filename, statbuf);
1850 *called_stat = TRUE;
1858 set_mtime_atime (char *filename,
1859 const GFileAttributeValue *mtime_value,
1860 const GFileAttributeValue *mtime_usec_value,
1861 const GFileAttributeValue *atime_value,
1862 const GFileAttributeValue *atime_usec_value,
1868 struct stat statbuf;
1869 gboolean got_stat = FALSE;
1870 struct timeval times[2] = { {0, 0}, {0, 0} };
1875 if (!get_uint64 (atime_value, &val, error))
1877 times[0].tv_sec = val;
1881 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1883 times[0].tv_sec = statbuf.st_mtime;
1884 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1885 times[0].tv_usec = statbuf.st_atimensec / 1000;
1886 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1887 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
1892 if (atime_usec_value)
1894 if (!get_uint32 (atime_usec_value, &val_usec, error))
1896 times[0].tv_usec = val_usec;
1902 if (!get_uint64 (mtime_value, &val, error))
1904 times[1].tv_sec = val;
1908 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1910 times[1].tv_sec = statbuf.st_mtime;
1911 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
1912 times[1].tv_usec = statbuf.st_mtimensec / 1000;
1913 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
1914 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
1919 if (mtime_usec_value)
1921 if (!get_uint32 (mtime_usec_value, &val_usec, error))
1923 times[1].tv_usec = val_usec;
1926 res = utimes(filename, times);
1929 g_set_error (error, G_IO_ERROR,
1930 g_io_error_from_errno (errno),
1931 _("Error setting owner: %s"),
1932 g_strerror (errno));
1940 _g_local_file_info_set_attribute (char *filename,
1941 const char *attribute,
1942 const GFileAttributeValue *value,
1943 GFileQueryInfoFlags flags,
1944 GCancellable *cancellable,
1947 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
1948 return set_unix_mode (filename, value, error);
1951 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
1952 return set_unix_uid_gid (filename, value, NULL, flags, error);
1953 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
1954 return set_unix_uid_gid (filename, NULL, value, flags, error);
1958 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET) == 0)
1959 return set_symlink (filename, value, error);
1963 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
1964 return set_mtime_atime (filename, value, NULL, NULL, NULL, error);
1965 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
1966 return set_mtime_atime (filename, NULL, value, NULL, NULL, error);
1967 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
1968 return set_mtime_atime (filename, NULL, NULL, value, NULL, error);
1969 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
1970 return set_mtime_atime (filename, NULL, NULL, NULL, value, error);
1974 else if (g_str_has_prefix (attribute, "xattr::"))
1975 return set_xattr (filename, attribute, value, error);
1976 else if (g_str_has_prefix (attribute, "xattr-sys::"))
1977 return set_xattr (filename, attribute, value, error);
1980 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1981 _("Setting attribute %s not supported"), attribute);
1986 _g_local_file_info_set_attributes (char *filename,
1988 GFileQueryInfoFlags flags,
1989 GCancellable *cancellable,
1992 GFileAttributeValue *value, *uid, *gid;
1993 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
1994 GFileAttributeStatus status;
1997 /* Handles setting multiple specified data in a single set, and takes care
1998 of ordering restrictions when setting attributes */
2002 /* Set symlink first, since this recreates the file */
2004 value = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET);
2007 if (!set_symlink (filename, value, error))
2009 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2011 /* Don't set error multiple times */
2015 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2021 /* Group uid and gid setting into one call
2022 * Change ownership before permissions, since ownership changes can
2023 change permissions (e.g. setuid)
2025 uid = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_UID);
2026 gid = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_GID);
2030 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2032 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2034 /* Don't set error multiple times */
2038 status = G_FILE_ATTRIBUTE_STATUS_SET;
2040 uid->status = status;
2042 gid->status = status;
2046 value = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2049 if (!set_unix_mode (filename, value, error))
2051 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2053 /* Don't set error multiple times */
2057 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2062 /* Group all time settings into one call
2063 * Change times as the last thing to avoid it changing due to metadata changes
2066 mtime = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2067 mtime_usec = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2068 atime = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2069 atime_usec = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2071 if (mtime || mtime_usec || atime || atime_usec)
2073 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2075 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2077 /* Don't set error multiple times */
2081 status = G_FILE_ATTRIBUTE_STATUS_SET;
2084 mtime->status = status;
2086 mtime_usec->status = status;
2088 atime->status = status;
2090 atime_usec->status = status;
2094 /* xattrs are handled by default callback */