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>
60 #include <gfileattribute-priv.h>
74 #define X_OK 0 /* not really */
77 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
80 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
83 #define S_IXUSR _S_IEXEC
87 #include "glocalfileinfo.h"
89 #include "gthemedicon.h"
90 #include "gcontenttype.h"
91 #include "gcontenttypeprivate.h"
95 struct ThumbMD5Context {
106 G_LOCK_DEFINE_STATIC (uid_cache);
107 static GHashTable *uid_cache = NULL;
109 G_LOCK_DEFINE_STATIC (gid_cache);
110 static GHashTable *gid_cache = NULL;
113 _g_local_file_info_create_etag (struct stat *statbuf)
117 tv.tv_sec = statbuf->st_mtime;
118 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
119 tv.tv_usec = statbuf->st_mtimensec / 1000;
120 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
121 tv.tv_usec = statbuf->st_mtim.tv_nsec / 1000;
126 return g_strdup_printf ("%lu:%lu", tv.tv_sec, tv.tv_usec);
130 _g_local_file_info_create_file_id (struct stat *statbuf)
132 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
133 (guint64) statbuf->st_dev,
134 (guint64) statbuf->st_ino);
138 _g_local_file_info_create_fs_id (struct stat *statbuf)
140 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
141 (guint64) statbuf->st_dev);
146 read_link (const gchar *full_name)
153 buffer = g_malloc (size);
159 read_size = readlink (full_name, buffer, size);
165 if (read_size < size)
167 buffer[read_size] = 0;
171 buffer = g_realloc (buffer, size);
178 /* Get the SELinux security context */
180 get_selinux_context (const char *path,
182 GFileAttributeMatcher *attribute_matcher,
183 gboolean follow_symlinks)
188 if (!g_file_attribute_matcher_matches (attribute_matcher, "selinux::context"))
191 if (is_selinux_enabled ())
195 if (lgetfilecon_raw (path, &context) < 0)
200 if (getfilecon_raw (path, &context) < 0)
206 g_file_info_set_attribute_string (info, "selinux::context", context);
215 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
216 * (Mac) getxattr(..., XATTR_NOFOLLOW)
218 #ifdef HAVE_XATTR_NOFOLLOW
219 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
220 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
221 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
223 #define g_fgetxattr fgetxattr
224 #define g_flistxattr flistxattr
225 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
229 g_getxattr (const char *path, const char *name, void *value, size_t size,
230 gboolean follow_symlinks)
232 #ifdef HAVE_XATTR_NOFOLLOW
233 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
236 return getxattr (path, name, value, size);
238 return lgetxattr (path, name, value, size);
243 g_listxattr(const char *path, char *namebuf, size_t size,
244 gboolean follow_symlinks)
246 #ifdef HAVE_XATTR_NOFOLLOW
247 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
250 return listxattr (path, namebuf, size);
252 return llistxattr (path, namebuf, size);
259 return c >= 32 && c <= 126 && c != '\\';
263 name_is_valid (const char *str)
267 if (!valid_char (*str++))
274 hex_escape_string (const char *str,
275 gboolean *free_return)
278 char *escaped_str, *p;
280 static char *hex_digits = "0123456789abcdef";
286 for (i = 0; i < len; i++)
288 if (!valid_char (str[i]))
292 if (num_invalid == 0)
294 *free_return = FALSE;
298 escaped_str = g_malloc (len + num_invalid*3 + 1);
301 for (i = 0; i < len; i++)
303 if (valid_char (str[i]))
310 *p++ = hex_digits[(c >> 4) & 0xf];
311 *p++ = hex_digits[c & 0xf];
321 hex_unescape_string (const char *str,
323 gboolean *free_return)
326 char *unescaped_str, *p;
332 if (strchr (str, '\\') == NULL)
336 *free_return = FALSE;
340 unescaped_str = g_malloc (len + 1);
343 for (i = 0; i < len; i++)
345 if (str[i] == '\\' &&
350 (g_ascii_xdigit_value (str[i+2]) << 4) |
351 g_ascii_xdigit_value (str[i+3]);
361 *out_len = p - unescaped_str;
363 return unescaped_str;
367 escape_xattr (GFileInfo *info,
368 const char *gio_attr, /* gio attribute name */
369 const char *value, /* Is zero terminated */
370 size_t len /* not including zero termination */)
373 gboolean free_escaped_val;
375 escaped_val = hex_escape_string (value, &free_escaped_val);
377 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
379 if (free_escaped_val)
380 g_free (escaped_val);
384 get_one_xattr (const char *path,
386 const char *gio_attr,
388 gboolean follow_symlinks)
394 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
399 else if (len == -1 && errno == ERANGE)
401 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
406 value_p = g_malloc (len+1);
408 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
422 escape_xattr (info, gio_attr, value_p, len);
424 if (value_p != value)
428 #endif /* defined HAVE_XATTR */
431 get_xattrs (const char *path,
434 GFileAttributeMatcher *matcher,
435 gboolean follow_symlinks)
440 ssize_t list_res_size;
443 const char *attr, *attr2;
446 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
448 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr_sys");
452 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
454 if (list_res_size == -1 ||
458 list_size = list_res_size;
459 list = g_malloc (list_size);
463 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
465 if (list_res_size == -1 && errno == ERANGE)
467 list_size = list_size * 2;
468 list = g_realloc (list, list_size);
472 if (list_res_size == -1)
476 while (list_res_size > 0)
478 if ((user && g_str_has_prefix (attr, "user.")) ||
479 (!user && !g_str_has_prefix (attr, "user.")))
481 char *escaped_attr, *gio_attr;
482 gboolean free_escaped_attr;
486 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
487 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
491 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
492 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
495 if (free_escaped_attr)
496 g_free (escaped_attr);
498 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
501 len = strlen (attr) + 1;
503 list_res_size -= len;
510 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
512 char *unescaped_attribute, *a;
513 gboolean free_unescaped_attribute;
515 attr2 = strchr (attr, ':');
518 attr2++; /* Skip ':' */
519 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
521 a = g_strconcat ("user.", unescaped_attribute, NULL);
523 a = unescaped_attribute;
525 get_one_xattr (path, info, attr, a, follow_symlinks);
530 if (free_unescaped_attribute)
531 g_free (unescaped_attribute);
535 #endif /* defined HAVE_XATTR */
540 get_one_xattr_from_fd (int fd,
542 const char *gio_attr,
549 len = g_fgetxattr (fd, xattr, value, sizeof (value)-1);
554 else if (len == -1 && errno == ERANGE)
556 len = g_fgetxattr (fd, xattr, NULL, 0);
561 value_p = g_malloc (len+1);
563 len = g_fgetxattr (fd, xattr, value_p, len);
577 escape_xattr (info, gio_attr, value_p, len);
579 if (value_p != value)
582 #endif /* defined HAVE_XATTR */
585 get_xattrs_from_fd (int fd,
588 GFileAttributeMatcher *matcher)
593 ssize_t list_res_size;
596 const char *attr, *attr2;
599 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
601 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
605 list_res_size = g_flistxattr (fd, NULL, 0);
607 if (list_res_size == -1 ||
611 list_size = list_res_size;
612 list = g_malloc (list_size);
616 list_res_size = g_flistxattr (fd, list, list_size);
618 if (list_res_size == -1 && errno == ERANGE)
620 list_size = list_size * 2;
621 list = g_realloc (list, list_size);
625 if (list_res_size == -1)
629 while (list_res_size > 0)
631 if ((user && g_str_has_prefix (attr, "user.")) ||
632 (!user && !g_str_has_prefix (attr, "user.")))
634 char *escaped_attr, *gio_attr;
635 gboolean free_escaped_attr;
639 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
640 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
644 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
645 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
648 if (free_escaped_attr)
649 g_free (escaped_attr);
651 get_one_xattr_from_fd (fd, info, gio_attr, attr);
654 len = strlen (attr) + 1;
656 list_res_size -= len;
663 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
665 char *unescaped_attribute, *a;
666 gboolean free_unescaped_attribute;
668 attr2 = strchr (attr, ':');
671 attr2++; /* Skip ':' */
672 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
674 a = g_strconcat ("user.", unescaped_attribute, NULL);
676 a = unescaped_attribute;
678 get_one_xattr_from_fd (fd, info, attr, a);
683 if (free_unescaped_attribute)
684 g_free (unescaped_attribute);
688 #endif /* defined HAVE_XATTR */
693 set_xattr (char *filename,
694 const char *escaped_attribute,
695 const GFileAttributeValue *attr_value,
698 char *attribute, *value;
699 gboolean free_attribute, free_value;
700 int val_len, res, errsv;
704 if (attr_value == NULL)
706 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
707 _("Attribute value must be non-NULL"));
711 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
713 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
714 _("Invalid attribute type (string expected)"));
718 if (!name_is_valid (escaped_attribute))
720 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
721 _("Invalid extended attribute name"));
725 if (g_str_has_prefix (escaped_attribute, "xattr::"))
727 escaped_attribute += 6;
732 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
733 escaped_attribute += 10;
737 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
738 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
741 a = g_strconcat ("user.", attribute, NULL);
745 res = g_setxattr (filename, a, value, val_len);
759 g_set_error (error, G_IO_ERROR,
760 g_io_error_from_errno (errsv),
761 _("Error setting extended attribute '%s': %s"),
762 escaped_attribute, g_strerror (errno));
773 _g_local_file_info_get_parent_info (const char *dir,
774 GFileAttributeMatcher *attribute_matcher,
775 GLocalParentFileInfo *parent_info)
780 parent_info->writable = FALSE;
781 parent_info->is_sticky = FALSE;
782 parent_info->device = 0;
784 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME) ||
785 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE) ||
786 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH) ||
787 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT))
789 parent_info->writable = (g_access (dir, W_OK) == 0);
791 res = g_stat (dir, &statbuf);
794 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
795 * renamed or deleted only by the owner of the file, by the owner of the directory, and
796 * by a privileged process.
801 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
803 parent_info->is_sticky = FALSE;
805 parent_info->owner = statbuf.st_uid;
806 parent_info->device = statbuf.st_dev;
812 get_access_rights (GFileAttributeMatcher *attribute_matcher,
815 struct stat *statbuf,
816 GLocalParentFileInfo *parent_info)
818 if (g_file_attribute_matcher_matches (attribute_matcher,
819 G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
820 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
821 g_access (path, R_OK) == 0);
823 if (g_file_attribute_matcher_matches (attribute_matcher,
824 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
825 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
826 g_access (path, W_OK) == 0);
828 if (g_file_attribute_matcher_matches (attribute_matcher,
829 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
830 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE,
831 g_access (path, X_OK) == 0);
839 if (parent_info->writable)
841 if (parent_info->is_sticky)
844 uid_t uid = geteuid ();
846 if (uid == statbuf->st_uid ||
847 uid == parent_info->owner ||
856 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME))
857 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME,
860 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE))
861 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE,
864 /* TODO: This means we can move it, but we should also look for a trash dir */
865 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
866 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH,
872 set_info_from_stat (GFileInfo *info,
873 struct stat *statbuf,
874 GFileAttributeMatcher *attribute_matcher)
878 file_type = G_FILE_TYPE_UNKNOWN;
880 if (S_ISREG (statbuf->st_mode))
881 file_type = G_FILE_TYPE_REGULAR;
882 else if (S_ISDIR (statbuf->st_mode))
883 file_type = G_FILE_TYPE_DIRECTORY;
885 else if (S_ISCHR (statbuf->st_mode) ||
886 S_ISBLK (statbuf->st_mode) ||
887 S_ISFIFO (statbuf->st_mode)
889 || S_ISSOCK (statbuf->st_mode)
892 file_type = G_FILE_TYPE_SPECIAL;
895 else if (S_ISLNK (statbuf->st_mode))
896 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
899 g_file_info_set_file_type (info, file_type);
900 g_file_info_set_size (info, statbuf->st_size);
902 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE, statbuf->st_dev);
903 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_INODE, statbuf->st_ino);
904 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, statbuf->st_mode);
905 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_NLINK, statbuf->st_nlink);
906 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_UID, statbuf->st_uid);
907 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, statbuf->st_gid);
908 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_RDEV, statbuf->st_rdev);
909 #if defined (HAVE_STRUCT_STAT_BLKSIZE)
910 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE, statbuf->st_blksize);
912 #if defined (HAVE_STRUCT_STAT_BLOCKS)
913 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_BLOCKS, statbuf->st_blocks);
916 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED, statbuf->st_mtime);
917 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
918 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
919 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
920 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
923 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS, statbuf->st_atime);
924 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
925 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
926 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
927 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
930 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_CHANGED, statbuf->st_ctime);
931 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
932 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
933 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
934 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
937 if (g_file_attribute_matcher_matches (attribute_matcher,
938 G_FILE_ATTRIBUTE_ETAG_VALUE))
940 char *etag = _g_local_file_info_create_etag (statbuf);
941 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ETAG_VALUE, etag);
945 if (g_file_attribute_matcher_matches (attribute_matcher,
946 G_FILE_ATTRIBUTE_ID_FILE))
948 char *id = _g_local_file_info_create_file_id (statbuf);
949 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE, id);
953 if (g_file_attribute_matcher_matches (attribute_matcher,
954 G_FILE_ATTRIBUTE_ID_FILESYSTEM))
956 char *id = _g_local_file_info_create_fs_id (statbuf);
957 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILESYSTEM, id);
963 make_valid_utf8 (const char *name)
966 const gchar *remainder, *invalid;
967 gint remaining_bytes, valid_bytes;
971 remaining_bytes = strlen (name);
973 while (remaining_bytes != 0)
975 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
977 valid_bytes = invalid - remainder;
980 string = g_string_sized_new (remaining_bytes);
982 g_string_append_len (string, remainder, valid_bytes);
983 /* append U+FFFD REPLACEMENT CHARACTER */
984 g_string_append (string, "\357\277\275");
986 remaining_bytes -= valid_bytes + 1;
987 remainder = invalid + 1;
991 return g_strdup (name);
993 g_string_append (string, remainder);
995 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
997 return g_string_free (string, FALSE);
1001 convert_pwd_string_to_utf8 (char *pwd_str)
1005 if (!g_utf8_validate (pwd_str, -1, NULL))
1007 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1008 if (utf8_string == NULL)
1009 utf8_string = make_valid_utf8 (pwd_str);
1012 utf8_string = g_strdup (pwd_str);
1018 uid_data_free (UidData *data)
1020 g_free (data->user_name);
1021 g_free (data->real_name);
1025 /* called with lock held */
1027 lookup_uid_data (uid_t uid)
1031 struct passwd pwbuf;
1032 struct passwd *pwbufp;
1033 char *gecos, *comma;
1035 if (uid_cache == NULL)
1036 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1038 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1043 data = g_new0 (UidData, 1);
1045 #if defined(HAVE_POSIX_GETPWUID_R)
1046 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1047 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1048 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1050 pwbufp = getpwuid (uid);
1055 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1056 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1058 gecos = pwbufp->pw_gecos;
1062 comma = strchr (gecos, ',');
1065 data->real_name = convert_pwd_string_to_utf8 (gecos);
1069 /* Default fallbacks */
1070 if (data->real_name == NULL)
1072 if (data->user_name != NULL)
1073 data->real_name = g_strdup (data->user_name);
1075 data->real_name = g_strdup_printf("user #%d", (int)uid);
1078 if (data->user_name == NULL)
1079 data->user_name = g_strdup_printf("%d", (int)uid);
1081 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1087 get_username_from_uid (uid_t uid)
1093 data = lookup_uid_data (uid);
1094 res = g_strdup (data->user_name);
1095 G_UNLOCK (uid_cache);
1101 get_realname_from_uid (uid_t uid)
1107 data = lookup_uid_data (uid);
1108 res = g_strdup (data->real_name);
1109 G_UNLOCK (uid_cache);
1114 /* called with lock held */
1116 lookup_gid_name (gid_t gid)
1121 struct group *gbufp;
1123 if (gid_cache == NULL)
1124 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1126 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1131 #if defined (HAVE_POSIX_GETGRGID_R)
1132 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1133 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1134 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1136 gbufp = getgrgid (gid);
1139 if (gbufp != NULL &&
1140 gbufp->gr_name != NULL &&
1141 gbufp->gr_name[0] != 0)
1142 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1144 name = g_strdup_printf("%d", (int)gid);
1146 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1152 get_groupname_from_gid (gid_t gid)
1158 name = lookup_gid_name (gid);
1159 res = g_strdup (name);
1160 G_UNLOCK (gid_cache);
1163 #endif /* !G_OS_WIN32 */
1166 get_content_type (const char *basename,
1168 struct stat *statbuf,
1169 gboolean is_symlink,
1170 gboolean symlink_broken,
1171 GFileQueryInfoFlags flags,
1175 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1176 return g_strdup ("inode/symlink");
1177 else if (S_ISDIR(statbuf->st_mode))
1178 return g_strdup ("inode/directory");
1180 else if (S_ISCHR(statbuf->st_mode))
1181 return g_strdup ("inode/chardevice");
1182 else if (S_ISBLK(statbuf->st_mode))
1183 return g_strdup ("inode/blockdevice");
1184 else if (S_ISFIFO(statbuf->st_mode))
1185 return g_strdup ("inode/fifo");
1188 else if (S_ISSOCK(statbuf->st_mode))
1189 return g_strdup ("inode/socket");
1194 gboolean result_uncertain;
1196 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1199 if (!fast && result_uncertain && path != NULL)
1201 guchar sniff_buffer[4096];
1205 sniff_length = _g_unix_content_type_get_sniff_len ();
1206 if (sniff_length > 4096)
1207 sniff_length = 4096;
1209 fd = open (path, O_RDONLY);
1214 res = read (fd, sniff_buffer, sniff_length);
1218 g_free (content_type);
1219 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1225 return content_type;
1231 get_thumbnail_attributes (const char *path,
1234 GChecksum *checksum;
1239 uri = g_filename_to_uri (path, NULL, NULL);
1241 checksum = g_checksum_new (G_CHECKSUM_MD5);
1242 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1244 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1245 g_checksum_free (checksum);
1247 filename = g_build_filename (g_get_home_dir(),
1248 ".thumbnails", "normal", basename,
1251 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1252 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1256 filename = g_build_filename (g_get_home_dir(),
1257 ".thumbnails", "fail",
1258 "gnome-thumbnail-factory",
1262 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1263 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1271 win32_get_file_user_info (const gchar* filename,
1276 PSECURITY_DESCRIPTOR psd = NULL;
1277 DWORD sd_size = 0; /* first call calculates the size required */
1279 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1280 if ((GetFileSecurityW (wfilename,
1281 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1284 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1285 (psd = g_try_malloc (sd_size)) != NULL &&
1286 GetFileSecurityW (wfilename,
1287 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1294 SID_NAME_USE name_use = 0; /* don't care? */
1295 wchar_t *name = NULL;
1296 wchar_t *domain = NULL;
1298 DWORD domain_len = 0;
1299 /* get the user name */
1303 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1305 if (!LookupAccountSidW (NULL, /* local machine */
1308 domain, &domain_len, /* no domain info yet */
1309 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1311 name = g_try_malloc (name_len*sizeof(wchar_t));
1312 domain = g_try_malloc (domain_len*sizeof(wchar_t));
1313 if (name && domain &&
1314 LookupAccountSidW (NULL, /* local machine */
1317 domain, &domain_len, /* no domain info yet */
1320 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1326 /* get the group name */
1330 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1332 if (!LookupAccountSidW (NULL, /* local machine */
1335 domain, &domain_len, /* no domain info yet */
1336 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1338 name = g_try_malloc (name_len*sizeof(wchar_t));
1339 domain = g_try_malloc (domain_len*sizeof(wchar_t));
1340 if (name && domain &&
1341 LookupAccountSidW (NULL, /* local machine */
1344 domain, &domain_len, /* no domain info yet */
1347 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1353 /* TODO: get real name */
1359 #endif /* G_OS_WIN32 */
1362 _g_local_file_info_get (const char *basename,
1364 GFileAttributeMatcher *attribute_matcher,
1365 GFileQueryInfoFlags flags,
1366 GLocalParentFileInfo *parent_info,
1370 struct stat statbuf;
1371 struct stat statbuf2;
1373 gboolean is_symlink, symlink_broken;
1375 info = g_file_info_new ();
1377 /* Make sure we don't set any unwanted attributes */
1378 g_file_info_set_attribute_mask (info, attribute_matcher);
1380 g_file_info_set_name (info, basename);
1382 /* Avoid stat in trivial case */
1383 if (attribute_matcher == NULL)
1386 res = g_lstat (path, &statbuf);
1389 g_object_unref (info);
1390 g_set_error (error, G_IO_ERROR,
1391 g_io_error_from_errno (errno),
1392 _("Error stating file '%s': %s"),
1393 path, g_strerror (errno));
1398 is_symlink = S_ISLNK (statbuf.st_mode);
1402 symlink_broken = FALSE;
1406 g_file_info_set_is_symlink (info, TRUE);
1408 /* Unless NOFOLLOW was set we default to following symlinks */
1409 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1411 res = stat (path, &statbuf2);
1413 /* Report broken links as symlinks */
1417 symlink_broken = TRUE;
1421 set_info_from_stat (info, &statbuf, attribute_matcher);
1423 if (basename != NULL && basename[0] == '.')
1424 g_file_info_set_is_hidden (info, TRUE);
1426 if (basename != NULL && basename[strlen (basename) -1] == '~')
1427 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP, TRUE);
1430 g_file_attribute_matcher_matches (attribute_matcher,
1431 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET))
1433 char *link = read_link (path);
1434 g_file_info_set_symlink_target (info, link);
1438 if (g_file_attribute_matcher_matches (attribute_matcher,
1439 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME))
1441 char *display_name = g_filename_display_basename (path);
1443 if (strstr (display_name, "\357\277\275") != NULL)
1445 char *p = display_name;
1446 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1449 g_file_info_set_display_name (info, display_name);
1450 g_free (display_name);
1453 if (g_file_attribute_matcher_matches (attribute_matcher,
1454 G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME))
1456 char *edit_name = g_filename_display_basename (path);
1457 g_file_info_set_edit_name (info, edit_name);
1462 if (g_file_attribute_matcher_matches (attribute_matcher,
1463 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME))
1465 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1467 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME, copy_name);
1471 if (g_file_attribute_matcher_matches (attribute_matcher,
1472 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE) ||
1473 g_file_attribute_matcher_matches (attribute_matcher,
1474 G_FILE_ATTRIBUTE_STANDARD_ICON))
1476 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1480 g_file_info_set_content_type (info, content_type);
1482 if (g_file_attribute_matcher_matches (attribute_matcher,
1483 G_FILE_ATTRIBUTE_STANDARD_ICON))
1485 char *mimetype_icon, *generic_mimetype_icon, *type_icon, *p;
1486 char *icon_names[3];
1490 mimetype_icon = g_strdup (content_type);
1492 while ((p = strchr(mimetype_icon, '/')) != NULL)
1495 p = strchr (content_type, '/');
1497 p = content_type + strlen (content_type);
1499 generic_mimetype_icon = g_malloc (p - content_type + strlen ("-x-generic") + 1);
1500 memcpy (generic_mimetype_icon, content_type, p - content_type);
1501 memcpy (generic_mimetype_icon + (p - content_type), "-x-generic", strlen ("-x-generic"));
1502 generic_mimetype_icon[(p - content_type) + strlen ("-x-generic")] = 0;
1504 /* TODO: Special case desktop dir? That could be expensive with xdg dirs... */
1505 if (strcmp (path, g_get_home_dir ()) == 0)
1506 type_icon = "user-home";
1507 else if (S_ISDIR (statbuf.st_mode))
1508 type_icon = "folder";
1509 else if (statbuf.st_mode & S_IXUSR)
1510 type_icon = "application-x-executable";
1512 type_icon = "text-x-generic";
1515 icon_names[i++] = mimetype_icon;
1516 icon_names[i++] = generic_mimetype_icon;
1517 if (strcmp (generic_mimetype_icon, type_icon) != 0 &&
1518 strcmp (mimetype_icon, type_icon) != 0)
1519 icon_names[i++] = type_icon;
1521 icon = g_themed_icon_new_from_names (icon_names, i);
1522 g_file_info_set_icon (info, icon);
1524 g_object_unref (icon);
1525 g_free (mimetype_icon);
1526 g_free (generic_mimetype_icon);
1529 g_free (content_type);
1533 if (g_file_attribute_matcher_matches (attribute_matcher,
1534 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE))
1536 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1540 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE, content_type);
1541 g_free (content_type);
1545 if (g_file_attribute_matcher_matches (attribute_matcher,
1546 G_FILE_ATTRIBUTE_OWNER_USER))
1551 win32_get_file_user_info (path, NULL, &name, NULL);
1553 name = get_username_from_uid (statbuf.st_uid);
1556 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1560 if (g_file_attribute_matcher_matches (attribute_matcher,
1561 G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1565 win32_get_file_user_info (path, NULL, NULL, &name);
1567 name = get_realname_from_uid (statbuf.st_uid);
1570 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1574 if (g_file_attribute_matcher_matches (attribute_matcher,
1575 G_FILE_ATTRIBUTE_OWNER_GROUP))
1579 win32_get_file_user_info (path, &name, NULL, NULL);
1581 name = get_groupname_from_gid (statbuf.st_gid);
1584 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1588 if (parent_info && parent_info->device != 0 &&
1589 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1590 statbuf.st_dev != parent_info->device)
1591 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1593 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1595 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1596 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1597 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1599 if (g_file_attribute_matcher_matches (attribute_matcher,
1600 G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1601 get_thumbnail_attributes (path, info);
1603 g_file_info_unset_attribute_mask (info);
1609 _g_local_file_info_get_from_fd (int fd,
1613 struct stat stat_buf;
1614 GFileAttributeMatcher *matcher;
1617 if (fstat (fd, &stat_buf) == -1)
1619 g_set_error (error, G_IO_ERROR,
1620 g_io_error_from_errno (errno),
1621 _("Error stating file descriptor: %s"),
1622 g_strerror (errno));
1626 info = g_file_info_new ();
1628 matcher = g_file_attribute_matcher_new (attributes);
1630 /* Make sure we don't set any unwanted attributes */
1631 g_file_info_set_attribute_mask (info, matcher);
1633 set_info_from_stat (info, &stat_buf, matcher);
1636 if (g_file_attribute_matcher_matches (matcher, "selinux:context") &&
1637 is_selinux_enabled ())
1640 if (fgetfilecon_raw (fd, &context) >= 0)
1642 g_file_info_set_attribute_string (info, "selinux:context", context);
1648 get_xattrs_from_fd (fd, TRUE, info, matcher);
1649 get_xattrs_from_fd (fd, FALSE, info, matcher);
1651 g_file_attribute_matcher_unref (matcher);
1653 g_file_info_unset_attribute_mask (info);
1659 get_uint32 (const GFileAttributeValue *value,
1663 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1665 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1666 _("Invalid attribute type (uint32 expected)"));
1670 *val_out = value->u.uint32;
1676 get_uint64 (const GFileAttributeValue *value,
1680 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1682 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1683 _("Invalid attribute type (uint64 expected)"));
1687 *val_out = value->u.uint64;
1692 #if defined(HAVE_SYMLINK)
1694 get_byte_string (const GFileAttributeValue *value,
1695 const char **val_out,
1698 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1700 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1701 _("Invalid attribute type (byte string expected)"));
1705 *val_out = value->u.string;
1712 set_unix_mode (char *filename,
1713 const GFileAttributeValue *value,
1718 if (!get_uint32 (value, &val, error))
1721 if (g_chmod (filename, val) == -1)
1723 g_set_error (error, G_IO_ERROR,
1724 g_io_error_from_errno (errno),
1725 _("Error setting permissions: %s"),
1726 g_strerror (errno));
1734 set_unix_uid_gid (char *filename,
1735 const GFileAttributeValue *uid_value,
1736 const GFileAttributeValue *gid_value,
1737 GFileQueryInfoFlags flags,
1747 if (!get_uint32 (uid_value, &val, error))
1756 if (!get_uint32 (gid_value, &val, error))
1763 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1764 res = lchown (filename, uid, gid);
1766 res = chown (filename, uid, gid);
1770 g_set_error (error, G_IO_ERROR,
1771 g_io_error_from_errno (errno),
1772 _("Error setting owner: %s"),
1773 g_strerror (errno));
1782 set_symlink (char *filename,
1783 const GFileAttributeValue *value,
1787 struct stat statbuf;
1789 if (!get_byte_string (value, &val, error))
1794 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1795 _("symlink must be non-NULL"));
1799 if (g_lstat (filename, &statbuf))
1801 g_set_error (error, G_IO_ERROR,
1802 g_io_error_from_errno (errno),
1803 _("Error setting symlink: %s"),
1804 g_strerror (errno));
1808 if (!S_ISLNK (statbuf.st_mode))
1810 g_set_error (error, G_IO_ERROR,
1811 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1812 _("Error setting symlink: file is not a symlink"));
1816 if (g_unlink (filename))
1818 g_set_error (error, G_IO_ERROR,
1819 g_io_error_from_errno (errno),
1820 _("Error setting symlink: %s"),
1821 g_strerror (errno));
1825 if (symlink (filename, val) != 0)
1827 g_set_error (error, G_IO_ERROR,
1828 g_io_error_from_errno (errno),
1829 _("Error setting symlink: %s"),
1830 g_strerror (errno));
1839 lazy_stat (char *filename,
1840 struct stat *statbuf,
1841 gboolean *called_stat)
1848 res = g_stat (filename, statbuf);
1851 *called_stat = TRUE;
1859 set_mtime_atime (char *filename,
1860 const GFileAttributeValue *mtime_value,
1861 const GFileAttributeValue *mtime_usec_value,
1862 const GFileAttributeValue *atime_value,
1863 const GFileAttributeValue *atime_usec_value,
1869 struct stat statbuf;
1870 gboolean got_stat = FALSE;
1871 struct timeval times[2] = { {0, 0}, {0, 0} };
1876 if (!get_uint64 (atime_value, &val, error))
1878 times[0].tv_sec = val;
1882 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1884 times[0].tv_sec = statbuf.st_mtime;
1885 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1886 times[0].tv_usec = statbuf.st_atimensec / 1000;
1887 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1888 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
1893 if (atime_usec_value)
1895 if (!get_uint32 (atime_usec_value, &val_usec, error))
1897 times[0].tv_usec = val_usec;
1903 if (!get_uint64 (mtime_value, &val, error))
1905 times[1].tv_sec = val;
1909 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1911 times[1].tv_sec = statbuf.st_mtime;
1912 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
1913 times[1].tv_usec = statbuf.st_mtimensec / 1000;
1914 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
1915 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
1920 if (mtime_usec_value)
1922 if (!get_uint32 (mtime_usec_value, &val_usec, error))
1924 times[1].tv_usec = val_usec;
1927 res = utimes(filename, times);
1930 g_set_error (error, G_IO_ERROR,
1931 g_io_error_from_errno (errno),
1932 _("Error setting owner: %s"),
1933 g_strerror (errno));
1941 _g_local_file_info_set_attribute (char *filename,
1942 const char *attribute,
1943 GFileAttributeType type,
1945 GFileQueryInfoFlags flags,
1946 GCancellable *cancellable,
1949 GFileAttributeValue value = { 0 };
1951 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
1953 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
1954 return set_unix_mode (filename, &value, error);
1957 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
1958 return set_unix_uid_gid (filename, &value, NULL, flags, error);
1959 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
1960 return set_unix_uid_gid (filename, NULL, &value, flags, error);
1964 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
1965 return set_symlink (filename, &value, error);
1969 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
1970 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
1971 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
1972 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
1973 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
1974 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
1975 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
1976 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
1980 else if (g_str_has_prefix (attribute, "xattr::"))
1981 return set_xattr (filename, attribute, &value, error);
1982 else if (g_str_has_prefix (attribute, "xattr-sys::"))
1983 return set_xattr (filename, attribute, &value, error);
1986 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1987 _("Setting attribute %s not supported"), attribute);
1992 _g_local_file_info_set_attributes (char *filename,
1994 GFileQueryInfoFlags flags,
1995 GCancellable *cancellable,
1998 GFileAttributeValue *value, *uid, *gid;
1999 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2000 GFileAttributeStatus status;
2003 /* Handles setting multiple specified data in a single set, and takes care
2004 of ordering restrictions when setting attributes */
2008 /* Set symlink first, since this recreates the file */
2010 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2013 if (!set_symlink (filename, value, error))
2015 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2017 /* Don't set error multiple times */
2021 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2027 /* Group uid and gid setting into one call
2028 * Change ownership before permissions, since ownership changes can
2029 change permissions (e.g. setuid)
2031 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2032 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2036 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2038 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2040 /* Don't set error multiple times */
2044 status = G_FILE_ATTRIBUTE_STATUS_SET;
2046 uid->status = status;
2048 gid->status = status;
2052 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2055 if (!set_unix_mode (filename, value, error))
2057 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2059 /* Don't set error multiple times */
2063 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2068 /* Group all time settings into one call
2069 * Change times as the last thing to avoid it changing due to metadata changes
2072 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2073 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2074 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2075 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2077 if (mtime || mtime_usec || atime || atime_usec)
2079 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2081 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2083 /* Don't set error multiple times */
2087 status = G_FILE_ATTRIBUTE_STATUS_SET;
2090 mtime->status = status;
2092 mtime_usec->status = status;
2094 atime->status = status;
2096 atime_usec->status = status;
2100 /* xattrs are handled by default callback */