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, G_FILE_ATTRIBUTE_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, G_FILE_ATTRIBUTE_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 (errsv));
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_ST_BLKSIZE)
910 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE, statbuf->st_blksize);
912 #if defined (HAVE_STRUCT_STAT_ST_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));
1246 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1247 g_checksum_free (checksum);
1249 filename = g_build_filename (g_get_home_dir (),
1250 ".thumbnails", "normal", basename,
1253 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1254 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1258 filename = g_build_filename (g_get_home_dir (),
1259 ".thumbnails", "fail",
1260 "gnome-thumbnail-factory",
1264 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1265 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1273 win32_get_file_user_info (const gchar *filename,
1278 PSECURITY_DESCRIPTOR psd = NULL;
1279 DWORD sd_size = 0; /* first call calculates the size required */
1281 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1282 if ((GetFileSecurityW (wfilename,
1283 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1286 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1287 (psd = g_try_malloc (sd_size)) != NULL &&
1288 GetFileSecurityW (wfilename,
1289 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1296 SID_NAME_USE name_use = 0; /* don't care? */
1297 wchar_t *name = NULL;
1298 wchar_t *domain = NULL;
1300 DWORD domain_len = 0;
1301 /* get the user name */
1305 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1307 if (!LookupAccountSidW (NULL, /* local machine */
1310 domain, &domain_len, /* no domain info yet */
1311 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1313 name = g_try_malloc (name_len * sizeof (wchar_t));
1314 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1315 if (name && domain &&
1316 LookupAccountSidW (NULL, /* local machine */
1319 domain, &domain_len, /* no domain info yet */
1322 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1328 /* get the group name */
1332 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1334 if (!LookupAccountSidW (NULL, /* local machine */
1337 domain, &domain_len, /* no domain info yet */
1338 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1340 name = g_try_malloc (name_len * sizeof (wchar_t));
1341 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1342 if (name && domain &&
1343 LookupAccountSidW (NULL, /* local machine */
1346 domain, &domain_len, /* no domain info yet */
1349 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1355 /* TODO: get real name */
1361 #endif /* G_OS_WIN32 */
1364 _g_local_file_info_get (const char *basename,
1366 GFileAttributeMatcher *attribute_matcher,
1367 GFileQueryInfoFlags flags,
1368 GLocalParentFileInfo *parent_info,
1372 struct stat statbuf;
1373 struct stat statbuf2;
1375 gboolean is_symlink, symlink_broken;
1377 info = g_file_info_new ();
1379 /* Make sure we don't set any unwanted attributes */
1380 g_file_info_set_attribute_mask (info, attribute_matcher);
1382 g_file_info_set_name (info, basename);
1384 /* Avoid stat in trivial case */
1385 if (attribute_matcher == NULL)
1388 res = g_lstat (path, &statbuf);
1392 char *display_name = g_filename_display_name (path);
1393 g_object_unref (info);
1394 g_set_error (error, G_IO_ERROR,
1395 g_io_error_from_errno (errsv),
1396 _("Error stating file '%s': %s"),
1397 display_name, g_strerror (errsv));
1398 g_free (display_name);
1403 is_symlink = S_ISLNK (statbuf.st_mode);
1407 symlink_broken = FALSE;
1411 g_file_info_set_is_symlink (info, TRUE);
1413 /* Unless NOFOLLOW was set we default to following symlinks */
1414 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1416 res = stat (path, &statbuf2);
1418 /* Report broken links as symlinks */
1422 symlink_broken = TRUE;
1426 set_info_from_stat (info, &statbuf, attribute_matcher);
1428 if (basename != NULL && basename[0] == '.')
1429 g_file_info_set_is_hidden (info, TRUE);
1431 if (basename != NULL && basename[strlen (basename) -1] == '~')
1432 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP, TRUE);
1435 g_file_attribute_matcher_matches (attribute_matcher,
1436 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET))
1438 char *link = read_link (path);
1439 g_file_info_set_symlink_target (info, link);
1443 if (g_file_attribute_matcher_matches (attribute_matcher,
1444 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME))
1446 char *display_name = g_filename_display_basename (path);
1448 /* look for U+FFFD REPLACEMENT CHARACTER */
1449 if (strstr (display_name, "\357\277\275") != NULL)
1451 char *p = display_name;
1452 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1455 g_file_info_set_display_name (info, display_name);
1456 g_free (display_name);
1459 if (g_file_attribute_matcher_matches (attribute_matcher,
1460 G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME))
1462 char *edit_name = g_filename_display_basename (path);
1463 g_file_info_set_edit_name (info, edit_name);
1468 if (g_file_attribute_matcher_matches (attribute_matcher,
1469 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME))
1471 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1473 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME, copy_name);
1477 if (g_file_attribute_matcher_matches (attribute_matcher,
1478 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE) ||
1479 g_file_attribute_matcher_matches (attribute_matcher,
1480 G_FILE_ATTRIBUTE_STANDARD_ICON))
1482 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1486 g_file_info_set_content_type (info, content_type);
1488 if (g_file_attribute_matcher_matches (attribute_matcher,
1489 G_FILE_ATTRIBUTE_STANDARD_ICON))
1491 char *mimetype_icon, *generic_mimetype_icon, *type_icon, *p;
1492 char *icon_names[3];
1496 mimetype_icon = g_strdup (content_type);
1498 while ((p = strchr (mimetype_icon, '/')) != NULL)
1501 p = strchr (content_type, '/');
1503 p = content_type + strlen (content_type);
1505 generic_mimetype_icon = g_malloc (p - content_type + strlen ("-x-generic") + 1);
1506 memcpy (generic_mimetype_icon, content_type, p - content_type);
1507 memcpy (generic_mimetype_icon + (p - content_type), "-x-generic", strlen ("-x-generic"));
1508 generic_mimetype_icon[(p - content_type) + strlen ("-x-generic")] = 0;
1510 /* TODO: Special case desktop dir? That could be expensive with xdg dirs... */
1511 if (strcmp (path, g_get_home_dir ()) == 0)
1512 type_icon = "user-home";
1513 else if (S_ISDIR (statbuf.st_mode))
1514 type_icon = "folder";
1515 else if (statbuf.st_mode & S_IXUSR)
1516 type_icon = "application-x-executable";
1518 type_icon = "text-x-generic";
1521 icon_names[i++] = mimetype_icon;
1522 icon_names[i++] = generic_mimetype_icon;
1523 if (strcmp (generic_mimetype_icon, type_icon) != 0 &&
1524 strcmp (mimetype_icon, type_icon) != 0)
1525 icon_names[i++] = type_icon;
1527 icon = g_themed_icon_new_from_names (icon_names, i);
1528 g_file_info_set_icon (info, icon);
1530 g_object_unref (icon);
1531 g_free (mimetype_icon);
1532 g_free (generic_mimetype_icon);
1535 g_free (content_type);
1539 if (g_file_attribute_matcher_matches (attribute_matcher,
1540 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE))
1542 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1546 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE, content_type);
1547 g_free (content_type);
1551 if (g_file_attribute_matcher_matches (attribute_matcher,
1552 G_FILE_ATTRIBUTE_OWNER_USER))
1557 win32_get_file_user_info (path, NULL, &name, NULL);
1559 name = get_username_from_uid (statbuf.st_uid);
1562 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1566 if (g_file_attribute_matcher_matches (attribute_matcher,
1567 G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1571 win32_get_file_user_info (path, NULL, NULL, &name);
1573 name = get_realname_from_uid (statbuf.st_uid);
1576 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1580 if (g_file_attribute_matcher_matches (attribute_matcher,
1581 G_FILE_ATTRIBUTE_OWNER_GROUP))
1585 win32_get_file_user_info (path, &name, NULL, NULL);
1587 name = get_groupname_from_gid (statbuf.st_gid);
1590 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1594 if (parent_info && parent_info->device != 0 &&
1595 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1596 statbuf.st_dev != parent_info->device)
1597 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1599 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1601 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1602 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1603 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1605 if (g_file_attribute_matcher_matches (attribute_matcher,
1606 G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1607 get_thumbnail_attributes (path, info);
1609 g_file_info_unset_attribute_mask (info);
1615 _g_local_file_info_get_from_fd (int fd,
1619 struct stat stat_buf;
1620 GFileAttributeMatcher *matcher;
1623 if (fstat (fd, &stat_buf) == -1)
1627 g_set_error (error, G_IO_ERROR,
1628 g_io_error_from_errno (errsv),
1629 _("Error stating file descriptor: %s"),
1630 g_strerror (errsv));
1634 info = g_file_info_new ();
1636 matcher = g_file_attribute_matcher_new (attributes);
1638 /* Make sure we don't set any unwanted attributes */
1639 g_file_info_set_attribute_mask (info, matcher);
1641 set_info_from_stat (info, &stat_buf, matcher);
1644 if (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) &&
1645 is_selinux_enabled ())
1648 if (fgetfilecon_raw (fd, &context) >= 0)
1650 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
1656 get_xattrs_from_fd (fd, TRUE, info, matcher);
1657 get_xattrs_from_fd (fd, FALSE, info, matcher);
1659 g_file_attribute_matcher_unref (matcher);
1661 g_file_info_unset_attribute_mask (info);
1667 get_uint32 (const GFileAttributeValue *value,
1671 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1673 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1674 _("Invalid attribute type (uint32 expected)"));
1678 *val_out = value->u.uint32;
1684 get_uint64 (const GFileAttributeValue *value,
1688 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1690 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1691 _("Invalid attribute type (uint64 expected)"));
1695 *val_out = value->u.uint64;
1700 #if defined(HAVE_SYMLINK)
1702 get_byte_string (const GFileAttributeValue *value,
1703 const char **val_out,
1706 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1708 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1709 _("Invalid attribute type (byte string expected)"));
1713 *val_out = value->u.string;
1720 set_unix_mode (char *filename,
1721 const GFileAttributeValue *value,
1726 if (!get_uint32 (value, &val, error))
1729 if (g_chmod (filename, val) == -1)
1733 g_set_error (error, G_IO_ERROR,
1734 g_io_error_from_errno (errsv),
1735 _("Error setting permissions: %s"),
1736 g_strerror (errsv));
1744 set_unix_uid_gid (char *filename,
1745 const GFileAttributeValue *uid_value,
1746 const GFileAttributeValue *gid_value,
1747 GFileQueryInfoFlags flags,
1757 if (!get_uint32 (uid_value, &val, error))
1766 if (!get_uint32 (gid_value, &val, error))
1774 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1775 res = lchown (filename, uid, gid);
1778 res = chown (filename, uid, gid);
1784 g_set_error (error, G_IO_ERROR,
1785 g_io_error_from_errno (errsv),
1786 _("Error setting owner: %s"),
1787 g_strerror (errsv));
1796 set_symlink (char *filename,
1797 const GFileAttributeValue *value,
1801 struct stat statbuf;
1803 if (!get_byte_string (value, &val, error))
1808 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1809 _("symlink must be non-NULL"));
1813 if (g_lstat (filename, &statbuf))
1817 g_set_error (error, G_IO_ERROR,
1818 g_io_error_from_errno (errsv),
1819 _("Error setting symlink: %s"),
1820 g_strerror (errsv));
1824 if (!S_ISLNK (statbuf.st_mode))
1826 g_set_error (error, G_IO_ERROR,
1827 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1828 _("Error setting symlink: file is not a symlink"));
1832 if (g_unlink (filename))
1836 g_set_error (error, G_IO_ERROR,
1837 g_io_error_from_errno (errsv),
1838 _("Error setting symlink: %s"),
1839 g_strerror (errsv));
1843 if (symlink (filename, val) != 0)
1847 g_set_error (error, G_IO_ERROR,
1848 g_io_error_from_errno (errsv),
1849 _("Error setting symlink: %s"),
1850 g_strerror (errsv));
1859 lazy_stat (char *filename,
1860 struct stat *statbuf,
1861 gboolean *called_stat)
1868 res = g_stat (filename, statbuf);
1871 *called_stat = TRUE;
1879 set_mtime_atime (char *filename,
1880 const GFileAttributeValue *mtime_value,
1881 const GFileAttributeValue *mtime_usec_value,
1882 const GFileAttributeValue *atime_value,
1883 const GFileAttributeValue *atime_usec_value,
1889 struct stat statbuf;
1890 gboolean got_stat = FALSE;
1891 struct timeval times[2] = { {0, 0}, {0, 0} };
1896 if (!get_uint64 (atime_value, &val, error))
1898 times[0].tv_sec = val;
1902 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1904 times[0].tv_sec = statbuf.st_mtime;
1905 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1906 times[0].tv_usec = statbuf.st_atimensec / 1000;
1907 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1908 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
1913 if (atime_usec_value)
1915 if (!get_uint32 (atime_usec_value, &val_usec, error))
1917 times[0].tv_usec = val_usec;
1923 if (!get_uint64 (mtime_value, &val, error))
1925 times[1].tv_sec = val;
1929 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1931 times[1].tv_sec = statbuf.st_mtime;
1932 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
1933 times[1].tv_usec = statbuf.st_mtimensec / 1000;
1934 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
1935 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
1940 if (mtime_usec_value)
1942 if (!get_uint32 (mtime_usec_value, &val_usec, error))
1944 times[1].tv_usec = val_usec;
1947 res = utimes (filename, times);
1952 g_set_error (error, G_IO_ERROR,
1953 g_io_error_from_errno (errsv),
1954 _("Error setting owner: %s"),
1955 g_strerror (errsv));
1963 _g_local_file_info_set_attribute (char *filename,
1964 const char *attribute,
1965 GFileAttributeType type,
1967 GFileQueryInfoFlags flags,
1968 GCancellable *cancellable,
1971 GFileAttributeValue value = { 0 };
1973 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
1975 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
1976 return set_unix_mode (filename, &value, error);
1979 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
1980 return set_unix_uid_gid (filename, &value, NULL, flags, error);
1981 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
1982 return set_unix_uid_gid (filename, NULL, &value, flags, error);
1986 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
1987 return set_symlink (filename, &value, error);
1991 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
1992 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
1993 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
1994 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
1995 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
1996 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
1997 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
1998 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2002 else if (g_str_has_prefix (attribute, "xattr::"))
2003 return set_xattr (filename, attribute, &value, error);
2004 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2005 return set_xattr (filename, attribute, &value, error);
2008 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2009 _("Setting attribute %s not supported"), attribute);
2014 _g_local_file_info_set_attributes (char *filename,
2016 GFileQueryInfoFlags flags,
2017 GCancellable *cancellable,
2020 GFileAttributeValue *value, *uid, *gid;
2021 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2022 GFileAttributeStatus status;
2025 /* Handles setting multiple specified data in a single set, and takes care
2026 of ordering restrictions when setting attributes */
2030 /* Set symlink first, since this recreates the file */
2032 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2035 if (!set_symlink (filename, value, error))
2037 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2039 /* Don't set error multiple times */
2043 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2049 /* Group uid and gid setting into one call
2050 * Change ownership before permissions, since ownership changes can
2051 change permissions (e.g. setuid)
2053 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2054 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2058 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2060 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2062 /* Don't set error multiple times */
2066 status = G_FILE_ATTRIBUTE_STATUS_SET;
2068 uid->status = status;
2070 gid->status = status;
2074 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2077 if (!set_unix_mode (filename, value, error))
2079 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2081 /* Don't set error multiple times */
2085 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2090 /* Group all time settings into one call
2091 * Change times as the last thing to avoid it changing due to metadata changes
2094 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2095 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2096 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2097 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2099 if (mtime || mtime_usec || atime || atime_usec)
2101 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2103 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2105 /* Don't set error multiple times */
2109 status = G_FILE_ATTRIBUTE_STATUS_SET;
2112 mtime->status = status;
2114 mtime_usec->status = status;
2116 atime->status = status;
2118 atime_usec->status = status;
2122 /* xattrs are handled by default callback */