1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
27 #ifdef HAVE_SYS_TIME_H
30 #include <sys/types.h>
45 #include <selinux/selinux.h>
50 #if defined HAVE_SYS_XATTR_H
51 #include <sys/xattr.h>
52 #elif defined HAVE_ATTR_XATTR_H
53 #include <attr/xattr.h>
55 #error "Neither <sys/xattr.h> nor <attr/xattr.h> is present but extended attribute support is enabled."
56 #endif /* defined HAVE_SYS_XATTR_H || HAVE_ATTR_XATTR_H */
58 #endif /* HAVE_XATTR */
60 #include <glib/gstdio.h>
61 #include <gfileattribute-priv.h>
75 #define X_OK 0 /* not really */
78 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
81 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
84 #define S_IXUSR _S_IEXEC
88 #include "glocalfileinfo.h"
90 #include "gthemedicon.h"
91 #include "gcontenttype.h"
92 #include "gcontenttypeprivate.h"
96 struct ThumbMD5Context {
109 G_LOCK_DEFINE_STATIC (uid_cache);
110 static GHashTable *uid_cache = NULL;
112 G_LOCK_DEFINE_STATIC (gid_cache);
113 static GHashTable *gid_cache = NULL;
115 #endif /* !G_OS_WIN32 */
118 _g_local_file_info_create_etag (GLocalFileStat *statbuf)
122 tv.tv_sec = statbuf->st_mtime;
123 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
124 tv.tv_usec = statbuf->st_mtimensec / 1000;
125 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
126 tv.tv_usec = statbuf->st_mtim.tv_nsec / 1000;
131 return g_strdup_printf ("%lu:%lu", tv.tv_sec, tv.tv_usec);
135 _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
137 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
138 (guint64) statbuf->st_dev,
139 (guint64) statbuf->st_ino);
143 _g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
145 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
146 (guint64) statbuf->st_dev);
153 read_link (const gchar *full_name)
160 buffer = g_malloc (size);
166 read_size = readlink (full_name, buffer, size);
172 if (read_size < size)
174 buffer[read_size] = 0;
178 buffer = g_realloc (buffer, size);
187 /* Get the SELinux security context */
189 get_selinux_context (const char *path,
191 GFileAttributeMatcher *attribute_matcher,
192 gboolean follow_symlinks)
197 if (!g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT))
200 if (is_selinux_enabled ())
204 if (lgetfilecon_raw (path, &context) < 0)
209 if (getfilecon_raw (path, &context) < 0)
215 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
224 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
225 * (Mac) getxattr(..., XATTR_NOFOLLOW)
227 #ifdef HAVE_XATTR_NOFOLLOW
228 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
229 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
230 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
232 #define g_fgetxattr fgetxattr
233 #define g_flistxattr flistxattr
234 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
238 g_getxattr (const char *path, const char *name, void *value, size_t size,
239 gboolean follow_symlinks)
241 #ifdef HAVE_XATTR_NOFOLLOW
242 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
245 return getxattr (path, name, value, size);
247 return lgetxattr (path, name, value, size);
252 g_listxattr(const char *path, char *namebuf, size_t size,
253 gboolean follow_symlinks)
255 #ifdef HAVE_XATTR_NOFOLLOW
256 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
259 return listxattr (path, namebuf, size);
261 return llistxattr (path, namebuf, size);
268 return c >= 32 && c <= 126 && c != '\\';
272 name_is_valid (const char *str)
276 if (!valid_char (*str++))
283 hex_escape_string (const char *str,
284 gboolean *free_return)
287 char *escaped_str, *p;
289 static char *hex_digits = "0123456789abcdef";
295 for (i = 0; i < len; i++)
297 if (!valid_char (str[i]))
301 if (num_invalid == 0)
303 *free_return = FALSE;
307 escaped_str = g_malloc (len + num_invalid*3 + 1);
310 for (i = 0; i < len; i++)
312 if (valid_char (str[i]))
319 *p++ = hex_digits[(c >> 4) & 0xf];
320 *p++ = hex_digits[c & 0xf];
330 hex_unescape_string (const char *str,
332 gboolean *free_return)
335 char *unescaped_str, *p;
341 if (strchr (str, '\\') == NULL)
345 *free_return = FALSE;
349 unescaped_str = g_malloc (len + 1);
352 for (i = 0; i < len; i++)
354 if (str[i] == '\\' &&
359 (g_ascii_xdigit_value (str[i+2]) << 4) |
360 g_ascii_xdigit_value (str[i+3]);
370 *out_len = p - unescaped_str;
372 return unescaped_str;
376 escape_xattr (GFileInfo *info,
377 const char *gio_attr, /* gio attribute name */
378 const char *value, /* Is zero terminated */
379 size_t len /* not including zero termination */)
382 gboolean free_escaped_val;
384 escaped_val = hex_escape_string (value, &free_escaped_val);
386 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
388 if (free_escaped_val)
389 g_free (escaped_val);
393 get_one_xattr (const char *path,
395 const char *gio_attr,
397 gboolean follow_symlinks)
403 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
408 else if (len == -1 && errno == ERANGE)
410 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
415 value_p = g_malloc (len+1);
417 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
431 escape_xattr (info, gio_attr, value_p, len);
433 if (value_p != value)
437 #endif /* defined HAVE_XATTR */
440 get_xattrs (const char *path,
443 GFileAttributeMatcher *matcher,
444 gboolean follow_symlinks)
449 ssize_t list_res_size;
452 const char *attr, *attr2;
455 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
457 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
461 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
463 if (list_res_size == -1 ||
467 list_size = list_res_size;
468 list = g_malloc (list_size);
472 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
474 if (list_res_size == -1 && errno == ERANGE)
476 list_size = list_size * 2;
477 list = g_realloc (list, list_size);
481 if (list_res_size == -1)
485 while (list_res_size > 0)
487 if ((user && g_str_has_prefix (attr, "user.")) ||
488 (!user && !g_str_has_prefix (attr, "user.")))
490 char *escaped_attr, *gio_attr;
491 gboolean free_escaped_attr;
495 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
496 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
500 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
501 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
504 if (free_escaped_attr)
505 g_free (escaped_attr);
507 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
510 len = strlen (attr) + 1;
512 list_res_size -= len;
519 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
521 char *unescaped_attribute, *a;
522 gboolean free_unescaped_attribute;
524 attr2 = strchr (attr, ':');
527 attr2++; /* Skip ':' */
528 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
530 a = g_strconcat ("user.", unescaped_attribute, NULL);
532 a = unescaped_attribute;
534 get_one_xattr (path, info, attr, a, follow_symlinks);
539 if (free_unescaped_attribute)
540 g_free (unescaped_attribute);
544 #endif /* defined HAVE_XATTR */
549 get_one_xattr_from_fd (int fd,
551 const char *gio_attr,
558 len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
563 else if (len == -1 && errno == ERANGE)
565 len = g_fgetxattr (fd, xattr, NULL, 0);
570 value_p = g_malloc (len + 1);
572 len = g_fgetxattr (fd, xattr, value_p, len);
586 escape_xattr (info, gio_attr, value_p, len);
588 if (value_p != value)
591 #endif /* defined HAVE_XATTR */
594 get_xattrs_from_fd (int fd,
597 GFileAttributeMatcher *matcher)
602 ssize_t list_res_size;
605 const char *attr, *attr2;
608 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
610 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
614 list_res_size = g_flistxattr (fd, NULL, 0);
616 if (list_res_size == -1 ||
620 list_size = list_res_size;
621 list = g_malloc (list_size);
625 list_res_size = g_flistxattr (fd, list, list_size);
627 if (list_res_size == -1 && errno == ERANGE)
629 list_size = list_size * 2;
630 list = g_realloc (list, list_size);
634 if (list_res_size == -1)
638 while (list_res_size > 0)
640 if ((user && g_str_has_prefix (attr, "user.")) ||
641 (!user && !g_str_has_prefix (attr, "user.")))
643 char *escaped_attr, *gio_attr;
644 gboolean free_escaped_attr;
648 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
649 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
653 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
654 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
657 if (free_escaped_attr)
658 g_free (escaped_attr);
660 get_one_xattr_from_fd (fd, info, gio_attr, attr);
663 len = strlen (attr) + 1;
665 list_res_size -= len;
672 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
674 char *unescaped_attribute, *a;
675 gboolean free_unescaped_attribute;
677 attr2 = strchr (attr, ':');
680 attr2++; /* Skip ':' */
681 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
683 a = g_strconcat ("user.", unescaped_attribute, NULL);
685 a = unescaped_attribute;
687 get_one_xattr_from_fd (fd, info, attr, a);
692 if (free_unescaped_attribute)
693 g_free (unescaped_attribute);
697 #endif /* defined HAVE_XATTR */
702 set_xattr (char *filename,
703 const char *escaped_attribute,
704 const GFileAttributeValue *attr_value,
707 char *attribute, *value;
708 gboolean free_attribute, free_value;
709 int val_len, res, errsv;
713 if (attr_value == NULL)
715 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
716 _("Attribute value must be non-NULL"));
720 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
722 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
723 _("Invalid attribute type (string expected)"));
727 if (!name_is_valid (escaped_attribute))
729 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
730 _("Invalid extended attribute name"));
734 if (g_str_has_prefix (escaped_attribute, "xattr::"))
736 escaped_attribute += 6;
741 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
742 escaped_attribute += 10;
746 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
747 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
750 a = g_strconcat ("user.", attribute, NULL);
754 res = g_setxattr (filename, a, value, val_len);
768 g_set_error (error, G_IO_ERROR,
769 g_io_error_from_errno (errsv),
770 _("Error setting extended attribute '%s': %s"),
771 escaped_attribute, g_strerror (errsv));
782 _g_local_file_info_get_parent_info (const char *dir,
783 GFileAttributeMatcher *attribute_matcher,
784 GLocalParentFileInfo *parent_info)
786 /* Use plain struct stat for now as long as we only look at the
787 * S_ISVTX bit which doesn't exist on Win32 anyway.
792 parent_info->writable = FALSE;
793 parent_info->is_sticky = FALSE;
794 parent_info->has_trash_dir = FALSE;
795 parent_info->device = 0;
797 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME) ||
798 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE) ||
799 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH) ||
800 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT))
802 /* FIXME: Windows: The underlying _waccess() call in the C
803 * library is mostly pointless as it only looks at the READONLY
804 * FAT-style attribute of the file, it doesn't check the ACL at
807 parent_info->writable = (g_access (dir, W_OK) == 0);
809 res = g_stat (dir, &statbuf);
812 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
813 * renamed or deleted only by the owner of the file, by the owner of the directory, and
814 * by a privileged process.
819 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
821 parent_info->is_sticky = FALSE;
823 parent_info->owner = statbuf.st_uid;
824 parent_info->device = statbuf.st_dev;
825 /* No need to find trash dir if its not writable anyway */
826 if (parent_info->writable &&
827 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
828 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
834 get_access_rights (GFileAttributeMatcher *attribute_matcher,
837 GLocalFileStat *statbuf,
838 GLocalParentFileInfo *parent_info)
840 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
841 if (g_file_attribute_matcher_matches (attribute_matcher,
842 G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
843 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
844 g_access (path, R_OK) == 0);
846 if (g_file_attribute_matcher_matches (attribute_matcher,
847 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
848 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
849 g_access (path, W_OK) == 0);
851 if (g_file_attribute_matcher_matches (attribute_matcher,
852 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
853 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE,
854 g_access (path, X_OK) == 0);
862 if (parent_info->writable)
864 if (parent_info->is_sticky)
867 uid_t uid = geteuid ();
869 if (uid == statbuf->st_uid ||
870 uid == parent_info->owner ||
879 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME))
880 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME,
883 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE))
884 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE,
887 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
888 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH,
889 writable && parent_info->has_trash_dir);
894 set_info_from_stat (GFileInfo *info,
895 GLocalFileStat *statbuf,
896 GFileAttributeMatcher *attribute_matcher)
900 file_type = G_FILE_TYPE_UNKNOWN;
902 if (S_ISREG (statbuf->st_mode))
903 file_type = G_FILE_TYPE_REGULAR;
904 else if (S_ISDIR (statbuf->st_mode))
905 file_type = G_FILE_TYPE_DIRECTORY;
907 else if (S_ISCHR (statbuf->st_mode) ||
908 S_ISBLK (statbuf->st_mode) ||
909 S_ISFIFO (statbuf->st_mode)
911 || S_ISSOCK (statbuf->st_mode)
914 file_type = G_FILE_TYPE_SPECIAL;
917 else if (S_ISLNK (statbuf->st_mode))
918 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
921 g_file_info_set_file_type (info, file_type);
922 g_file_info_set_size (info, statbuf->st_size);
924 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE, statbuf->st_dev);
926 /* Pointless setting these on Windows even if they exist in the struct */
927 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_INODE, statbuf->st_ino);
928 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_NLINK, statbuf->st_nlink);
929 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_UID, statbuf->st_uid);
930 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, statbuf->st_gid);
931 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_RDEV, statbuf->st_rdev);
933 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
934 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, statbuf->st_mode);
935 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
936 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE, statbuf->st_blksize);
938 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
939 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_BLOCKS, statbuf->st_blocks);
942 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED, statbuf->st_mtime);
943 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
944 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
945 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
946 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
949 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS, statbuf->st_atime);
950 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
951 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
952 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
953 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
956 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_CHANGED, statbuf->st_ctime);
957 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
958 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
959 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
960 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
963 if (g_file_attribute_matcher_matches (attribute_matcher,
964 G_FILE_ATTRIBUTE_ETAG_VALUE))
966 char *etag = _g_local_file_info_create_etag (statbuf);
967 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ETAG_VALUE, etag);
971 if (g_file_attribute_matcher_matches (attribute_matcher,
972 G_FILE_ATTRIBUTE_ID_FILE))
974 char *id = _g_local_file_info_create_file_id (statbuf);
975 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE, id);
979 if (g_file_attribute_matcher_matches (attribute_matcher,
980 G_FILE_ATTRIBUTE_ID_FILESYSTEM))
982 char *id = _g_local_file_info_create_fs_id (statbuf);
983 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILESYSTEM, id);
991 make_valid_utf8 (const char *name)
994 const gchar *remainder, *invalid;
995 gint remaining_bytes, valid_bytes;
999 remaining_bytes = strlen (name);
1001 while (remaining_bytes != 0)
1003 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1005 valid_bytes = invalid - remainder;
1008 string = g_string_sized_new (remaining_bytes);
1010 g_string_append_len (string, remainder, valid_bytes);
1011 /* append U+FFFD REPLACEMENT CHARACTER */
1012 g_string_append (string, "\357\277\275");
1014 remaining_bytes -= valid_bytes + 1;
1015 remainder = invalid + 1;
1019 return g_strdup (name);
1021 g_string_append (string, remainder);
1023 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1025 return g_string_free (string, FALSE);
1029 convert_pwd_string_to_utf8 (char *pwd_str)
1033 if (!g_utf8_validate (pwd_str, -1, NULL))
1035 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1036 if (utf8_string == NULL)
1037 utf8_string = make_valid_utf8 (pwd_str);
1040 utf8_string = g_strdup (pwd_str);
1046 uid_data_free (UidData *data)
1048 g_free (data->user_name);
1049 g_free (data->real_name);
1053 /* called with lock held */
1055 lookup_uid_data (uid_t uid)
1059 struct passwd pwbuf;
1060 struct passwd *pwbufp;
1061 char *gecos, *comma;
1063 if (uid_cache == NULL)
1064 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1066 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1071 data = g_new0 (UidData, 1);
1073 #if defined(HAVE_POSIX_GETPWUID_R)
1074 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1075 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1076 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1078 pwbufp = getpwuid (uid);
1083 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1084 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1086 gecos = pwbufp->pw_gecos;
1090 comma = strchr (gecos, ',');
1093 data->real_name = convert_pwd_string_to_utf8 (gecos);
1097 /* Default fallbacks */
1098 if (data->real_name == NULL)
1100 if (data->user_name != NULL)
1101 data->real_name = g_strdup (data->user_name);
1103 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1106 if (data->user_name == NULL)
1107 data->user_name = g_strdup_printf ("%d", (int)uid);
1109 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1115 get_username_from_uid (uid_t uid)
1121 data = lookup_uid_data (uid);
1122 res = g_strdup (data->user_name);
1123 G_UNLOCK (uid_cache);
1129 get_realname_from_uid (uid_t uid)
1135 data = lookup_uid_data (uid);
1136 res = g_strdup (data->real_name);
1137 G_UNLOCK (uid_cache);
1142 /* called with lock held */
1144 lookup_gid_name (gid_t gid)
1149 struct group *gbufp;
1151 if (gid_cache == NULL)
1152 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1154 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1159 #if defined (HAVE_POSIX_GETGRGID_R)
1160 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1161 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1162 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1164 gbufp = getgrgid (gid);
1167 if (gbufp != NULL &&
1168 gbufp->gr_name != NULL &&
1169 gbufp->gr_name[0] != 0)
1170 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1172 name = g_strdup_printf("%d", (int)gid);
1174 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1180 get_groupname_from_gid (gid_t gid)
1186 name = lookup_gid_name (gid);
1187 res = g_strdup (name);
1188 G_UNLOCK (gid_cache);
1192 #endif /* !G_OS_WIN32 */
1195 get_content_type (const char *basename,
1197 GLocalFileStat *statbuf,
1198 gboolean is_symlink,
1199 gboolean symlink_broken,
1200 GFileQueryInfoFlags flags,
1204 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1205 return g_strdup ("inode/symlink");
1206 else if (S_ISDIR(statbuf->st_mode))
1207 return g_strdup ("inode/directory");
1209 else if (S_ISCHR(statbuf->st_mode))
1210 return g_strdup ("inode/chardevice");
1211 else if (S_ISBLK(statbuf->st_mode))
1212 return g_strdup ("inode/blockdevice");
1213 else if (S_ISFIFO(statbuf->st_mode))
1214 return g_strdup ("inode/fifo");
1217 else if (S_ISSOCK(statbuf->st_mode))
1218 return g_strdup ("inode/socket");
1223 gboolean result_uncertain;
1225 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1228 if (!fast && result_uncertain && path != NULL)
1230 guchar sniff_buffer[4096];
1234 sniff_length = _g_unix_content_type_get_sniff_len ();
1235 if (sniff_length > 4096)
1236 sniff_length = 4096;
1238 fd = open (path, O_RDONLY);
1243 res = read (fd, sniff_buffer, sniff_length);
1247 g_free (content_type);
1248 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1254 return content_type;
1260 get_thumbnail_attributes (const char *path,
1263 GChecksum *checksum;
1268 uri = g_filename_to_uri (path, NULL, NULL);
1270 checksum = g_checksum_new (G_CHECKSUM_MD5);
1271 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1275 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1276 g_checksum_free (checksum);
1278 filename = g_build_filename (g_get_home_dir (),
1279 ".thumbnails", "normal", basename,
1282 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1283 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1287 filename = g_build_filename (g_get_home_dir (),
1288 ".thumbnails", "fail",
1289 "gnome-thumbnail-factory",
1293 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1294 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1302 win32_get_file_user_info (const gchar *filename,
1307 PSECURITY_DESCRIPTOR psd = NULL;
1308 DWORD sd_size = 0; /* first call calculates the size required */
1310 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1311 if ((GetFileSecurityW (wfilename,
1312 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1315 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1316 (psd = g_try_malloc (sd_size)) != NULL &&
1317 GetFileSecurityW (wfilename,
1318 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1325 SID_NAME_USE name_use = 0; /* don't care? */
1326 wchar_t *name = NULL;
1327 wchar_t *domain = NULL;
1329 DWORD domain_len = 0;
1330 /* get the user name */
1334 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1336 if (!LookupAccountSidW (NULL, /* local machine */
1339 domain, &domain_len, /* no domain info yet */
1340 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1342 name = g_try_malloc (name_len * sizeof (wchar_t));
1343 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1344 if (name && domain &&
1345 LookupAccountSidW (NULL, /* local machine */
1348 domain, &domain_len, /* no domain info yet */
1351 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1357 /* get the group name */
1361 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1363 if (!LookupAccountSidW (NULL, /* local machine */
1366 domain, &domain_len, /* no domain info yet */
1367 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1369 name = g_try_malloc (name_len * sizeof (wchar_t));
1370 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1371 if (name && domain &&
1372 LookupAccountSidW (NULL, /* local machine */
1375 domain, &domain_len, /* no domain info yet */
1378 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1384 /* TODO: get real name */
1390 #endif /* G_OS_WIN32 */
1393 _g_local_file_info_get (const char *basename,
1395 GFileAttributeMatcher *attribute_matcher,
1396 GFileQueryInfoFlags flags,
1397 GLocalParentFileInfo *parent_info,
1401 GLocalFileStat statbuf;
1403 struct stat statbuf2;
1406 gboolean is_symlink, symlink_broken;
1408 DWORD dos_attributes;
1411 info = g_file_info_new ();
1413 /* Make sure we don't set any unwanted attributes */
1414 g_file_info_set_attribute_mask (info, attribute_matcher);
1416 g_file_info_set_name (info, basename);
1418 /* Avoid stat in trivial case */
1419 if (attribute_matcher == NULL)
1423 res = g_lstat (path, &statbuf);
1426 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1431 g_object_unref (info);
1435 len = wcslen (wpath);
1436 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1439 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1442 res = _wstati64 (wpath, &statbuf);
1443 dos_attributes = GetFileAttributesW (wpath);
1452 char *display_name = g_filename_display_name (path);
1453 g_object_unref (info);
1454 g_set_error (error, G_IO_ERROR,
1455 g_io_error_from_errno (errsv),
1456 _("Error stating file '%s': %s"),
1457 display_name, g_strerror (errsv));
1458 g_free (display_name);
1463 is_symlink = S_ISLNK (statbuf.st_mode);
1467 symlink_broken = FALSE;
1472 g_file_info_set_is_symlink (info, TRUE);
1474 /* Unless NOFOLLOW was set we default to following symlinks */
1475 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1477 res = stat (path, &statbuf2);
1479 /* Report broken links as symlinks */
1483 symlink_broken = TRUE;
1488 set_info_from_stat (info, &statbuf, attribute_matcher);
1491 if (basename != NULL && basename[0] == '.')
1492 g_file_info_set_is_hidden (info, TRUE);
1494 if (basename != NULL && basename[strlen (basename) -1] == '~')
1495 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP, TRUE);
1497 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1498 g_file_info_set_is_hidden (info, TRUE);
1500 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1501 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_DOS_IS_ARCHIVE, TRUE);
1503 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1504 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_DOS_IS_SYSTEM, TRUE);
1509 g_file_attribute_matcher_matches (attribute_matcher,
1510 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET))
1512 char *link = read_link (path);
1513 g_file_info_set_symlink_target (info, link);
1518 if (g_file_attribute_matcher_matches (attribute_matcher,
1519 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME))
1521 char *display_name = g_filename_display_basename (path);
1523 /* look for U+FFFD REPLACEMENT CHARACTER */
1524 if (strstr (display_name, "\357\277\275") != NULL)
1526 char *p = display_name;
1527 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1530 g_file_info_set_display_name (info, display_name);
1531 g_free (display_name);
1534 if (g_file_attribute_matcher_matches (attribute_matcher,
1535 G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME))
1537 char *edit_name = g_filename_display_basename (path);
1538 g_file_info_set_edit_name (info, edit_name);
1543 if (g_file_attribute_matcher_matches (attribute_matcher,
1544 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME))
1546 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1548 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME, copy_name);
1552 if (g_file_attribute_matcher_matches (attribute_matcher,
1553 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE) ||
1554 g_file_attribute_matcher_matches (attribute_matcher,
1555 G_FILE_ATTRIBUTE_STANDARD_ICON))
1557 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1561 g_file_info_set_content_type (info, content_type);
1563 if (g_file_attribute_matcher_matches (attribute_matcher,
1564 G_FILE_ATTRIBUTE_STANDARD_ICON))
1568 icon = g_content_type_get_icon (content_type);
1571 if (G_IS_THEMED_ICON (icon))
1573 const char *type_icon;
1575 /* TODO: Special case desktop dir? That could be expensive with xdg dirs... */
1576 if (strcmp (path, g_get_home_dir ()) == 0)
1577 type_icon = "user-home";
1578 else if (S_ISDIR (statbuf.st_mode))
1579 type_icon = "folder";
1580 else if (statbuf.st_mode & S_IXUSR)
1581 type_icon = "application-x-executable";
1583 type_icon = "text-x-generic";
1585 g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
1588 g_file_info_set_icon (info, icon);
1589 g_object_unref (icon);
1593 g_free (content_type);
1597 if (g_file_attribute_matcher_matches (attribute_matcher,
1598 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE))
1600 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1604 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE, content_type);
1605 g_free (content_type);
1609 if (g_file_attribute_matcher_matches (attribute_matcher,
1610 G_FILE_ATTRIBUTE_OWNER_USER))
1615 win32_get_file_user_info (path, NULL, &name, NULL);
1617 name = get_username_from_uid (statbuf.st_uid);
1620 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1624 if (g_file_attribute_matcher_matches (attribute_matcher,
1625 G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1629 win32_get_file_user_info (path, NULL, NULL, &name);
1631 name = get_realname_from_uid (statbuf.st_uid);
1634 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1638 if (g_file_attribute_matcher_matches (attribute_matcher,
1639 G_FILE_ATTRIBUTE_OWNER_GROUP))
1643 win32_get_file_user_info (path, &name, NULL, NULL);
1645 name = get_groupname_from_gid (statbuf.st_gid);
1648 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1652 if (parent_info && parent_info->device != 0 &&
1653 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1654 statbuf.st_dev != parent_info->device)
1655 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1657 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1659 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1660 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1661 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1663 if (g_file_attribute_matcher_matches (attribute_matcher,
1664 G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1665 get_thumbnail_attributes (path, info);
1667 g_file_info_unset_attribute_mask (info);
1673 _g_local_file_info_get_from_fd (int fd,
1677 GLocalFileStat stat_buf;
1678 GFileAttributeMatcher *matcher;
1682 #define FSTAT _fstati64
1687 if (FSTAT (fd, &stat_buf) == -1)
1691 g_set_error (error, G_IO_ERROR,
1692 g_io_error_from_errno (errsv),
1693 _("Error stating file descriptor: %s"),
1694 g_strerror (errsv));
1698 info = g_file_info_new ();
1700 matcher = g_file_attribute_matcher_new (attributes);
1702 /* Make sure we don't set any unwanted attributes */
1703 g_file_info_set_attribute_mask (info, matcher);
1705 set_info_from_stat (info, &stat_buf, matcher);
1708 if (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) &&
1709 is_selinux_enabled ())
1712 if (fgetfilecon_raw (fd, &context) >= 0)
1714 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
1720 get_xattrs_from_fd (fd, TRUE, info, matcher);
1721 get_xattrs_from_fd (fd, FALSE, info, matcher);
1723 g_file_attribute_matcher_unref (matcher);
1725 g_file_info_unset_attribute_mask (info);
1731 get_uint32 (const GFileAttributeValue *value,
1735 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1737 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1738 _("Invalid attribute type (uint32 expected)"));
1742 *val_out = value->u.uint32;
1749 get_uint64 (const GFileAttributeValue *value,
1753 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1755 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1756 _("Invalid attribute type (uint64 expected)"));
1760 *val_out = value->u.uint64;
1766 #if defined(HAVE_SYMLINK)
1768 get_byte_string (const GFileAttributeValue *value,
1769 const char **val_out,
1772 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1774 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1775 _("Invalid attribute type (byte string expected)"));
1779 *val_out = value->u.string;
1786 set_unix_mode (char *filename,
1787 const GFileAttributeValue *value,
1792 if (!get_uint32 (value, &val, error))
1795 if (g_chmod (filename, val) == -1)
1799 g_set_error (error, G_IO_ERROR,
1800 g_io_error_from_errno (errsv),
1801 _("Error setting permissions: %s"),
1802 g_strerror (errsv));
1810 set_unix_uid_gid (char *filename,
1811 const GFileAttributeValue *uid_value,
1812 const GFileAttributeValue *gid_value,
1813 GFileQueryInfoFlags flags,
1823 if (!get_uint32 (uid_value, &val, error))
1832 if (!get_uint32 (gid_value, &val, error))
1840 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1841 res = lchown (filename, uid, gid);
1844 res = chown (filename, uid, gid);
1850 g_set_error (error, G_IO_ERROR,
1851 g_io_error_from_errno (errsv),
1852 _("Error setting owner: %s"),
1853 g_strerror (errsv));
1862 set_symlink (char *filename,
1863 const GFileAttributeValue *value,
1867 struct stat statbuf;
1869 if (!get_byte_string (value, &val, error))
1874 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1875 _("symlink must be non-NULL"));
1879 if (g_lstat (filename, &statbuf))
1883 g_set_error (error, G_IO_ERROR,
1884 g_io_error_from_errno (errsv),
1885 _("Error setting symlink: %s"),
1886 g_strerror (errsv));
1890 if (!S_ISLNK (statbuf.st_mode))
1892 g_set_error (error, G_IO_ERROR,
1893 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1894 _("Error setting symlink: file is not a symlink"));
1898 if (g_unlink (filename))
1902 g_set_error (error, G_IO_ERROR,
1903 g_io_error_from_errno (errsv),
1904 _("Error setting symlink: %s"),
1905 g_strerror (errsv));
1909 if (symlink (filename, val) != 0)
1913 g_set_error (error, G_IO_ERROR,
1914 g_io_error_from_errno (errsv),
1915 _("Error setting symlink: %s"),
1916 g_strerror (errsv));
1926 lazy_stat (char *filename,
1927 struct stat *statbuf,
1928 gboolean *called_stat)
1935 res = g_stat (filename, statbuf);
1938 *called_stat = TRUE;
1945 set_mtime_atime (char *filename,
1946 const GFileAttributeValue *mtime_value,
1947 const GFileAttributeValue *mtime_usec_value,
1948 const GFileAttributeValue *atime_value,
1949 const GFileAttributeValue *atime_usec_value,
1955 struct stat statbuf;
1956 gboolean got_stat = FALSE;
1957 struct timeval times[2] = { {0, 0}, {0, 0} };
1962 if (!get_uint64 (atime_value, &val, error))
1964 times[0].tv_sec = val;
1968 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1970 times[0].tv_sec = statbuf.st_mtime;
1971 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1972 times[0].tv_usec = statbuf.st_atimensec / 1000;
1973 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1974 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
1979 if (atime_usec_value)
1981 if (!get_uint32 (atime_usec_value, &val_usec, error))
1983 times[0].tv_usec = val_usec;
1989 if (!get_uint64 (mtime_value, &val, error))
1991 times[1].tv_sec = val;
1995 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1997 times[1].tv_sec = statbuf.st_mtime;
1998 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
1999 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2000 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2001 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2006 if (mtime_usec_value)
2008 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2010 times[1].tv_usec = val_usec;
2013 res = utimes (filename, times);
2018 g_set_error (error, G_IO_ERROR,
2019 g_io_error_from_errno (errsv),
2020 _("Error setting owner: %s"),
2021 g_strerror (errsv));
2029 _g_local_file_info_set_attribute (char *filename,
2030 const char *attribute,
2031 GFileAttributeType type,
2033 GFileQueryInfoFlags flags,
2034 GCancellable *cancellable,
2037 GFileAttributeValue value = { 0 };
2039 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2041 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2042 return set_unix_mode (filename, &value, error);
2045 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2046 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2047 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2048 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2052 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2053 return set_symlink (filename, &value, error);
2057 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2058 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2059 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2060 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2061 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2062 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2063 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2064 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2068 else if (g_str_has_prefix (attribute, "xattr::"))
2069 return set_xattr (filename, attribute, &value, error);
2070 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2071 return set_xattr (filename, attribute, &value, error);
2074 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2075 _("Setting attribute %s not supported"), attribute);
2080 _g_local_file_info_set_attributes (char *filename,
2082 GFileQueryInfoFlags flags,
2083 GCancellable *cancellable,
2086 GFileAttributeValue *value;
2088 GFileAttributeValue *uid, *gid;
2091 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2093 #if defined (HAVE_CHOWN) && defined (HAVE_UTIMES)
2094 GFileAttributeStatus status;
2098 /* Handles setting multiple specified data in a single set, and takes care
2099 of ordering restrictions when setting attributes */
2103 /* Set symlink first, since this recreates the file */
2105 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2108 if (!set_symlink (filename, value, error))
2110 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2112 /* Don't set error multiple times */
2116 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2122 /* Group uid and gid setting into one call
2123 * Change ownership before permissions, since ownership changes can
2124 change permissions (e.g. setuid)
2126 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2127 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2131 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2133 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2135 /* Don't set error multiple times */
2139 status = G_FILE_ATTRIBUTE_STATUS_SET;
2141 uid->status = status;
2143 gid->status = status;
2147 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2150 if (!set_unix_mode (filename, value, error))
2152 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2154 /* Don't set error multiple times */
2158 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2163 /* Group all time settings into one call
2164 * Change times as the last thing to avoid it changing due to metadata changes
2167 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2168 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2169 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2170 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2172 if (mtime || mtime_usec || atime || atime_usec)
2174 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2176 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2178 /* Don't set error multiple times */
2182 status = G_FILE_ATTRIBUTE_STATUS_SET;
2185 mtime->status = status;
2187 mtime_usec->status = status;
2189 atime->status = status;
2191 atime_usec->status = status;
2195 /* xattrs are handled by default callback */