1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
27 #ifdef HAVE_SYS_TIME_H
30 #include <sys/types.h>
45 #include <selinux/selinux.h>
50 #if defined HAVE_SYS_XATTR_H
51 #include <sys/xattr.h>
52 #elif defined HAVE_ATTR_XATTR_H
53 #include <attr/xattr.h>
55 #error "Neither <sys/xattr.h> nor <attr/xattr.h> is present but extended attribute support is enabled."
56 #endif /* defined HAVE_SYS_XATTR_H || HAVE_ATTR_XATTR_H */
58 #endif /* HAVE_XATTR */
60 #include <glib/gstdio.h>
61 #include <glib/gchecksum.h>
62 #include <gfileattribute-priv.h>
76 #define X_OK 0 /* not really */
79 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
82 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
85 #define S_IXUSR _S_IEXEC
89 #include "glocalfileinfo.h"
91 #include "gthemedicon.h"
92 #include "gcontenttype.h"
93 #include "gcontenttypeprivate.h"
97 struct ThumbMD5Context {
100 unsigned char in[64];
110 G_LOCK_DEFINE_STATIC (uid_cache);
111 static GHashTable *uid_cache = NULL;
113 G_LOCK_DEFINE_STATIC (gid_cache);
114 static GHashTable *gid_cache = NULL;
116 #endif /* !G_OS_WIN32 */
119 _g_local_file_info_create_etag (GLocalFileStat *statbuf)
123 tv.tv_sec = statbuf->st_mtime;
124 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
125 tv.tv_usec = statbuf->st_mtimensec / 1000;
126 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
127 tv.tv_usec = statbuf->st_mtim.tv_nsec / 1000;
132 return g_strdup_printf ("%lu:%lu", tv.tv_sec, tv.tv_usec);
136 _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
138 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
139 (guint64) statbuf->st_dev,
140 (guint64) statbuf->st_ino);
144 _g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
146 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
147 (guint64) statbuf->st_dev);
154 read_link (const gchar *full_name)
161 buffer = g_malloc (size);
167 read_size = readlink (full_name, buffer, size);
173 if (read_size < size)
175 buffer[read_size] = 0;
179 buffer = g_realloc (buffer, size);
188 /* Get the SELinux security context */
190 get_selinux_context (const char *path,
192 GFileAttributeMatcher *attribute_matcher,
193 gboolean follow_symlinks)
198 if (!g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT))
201 if (is_selinux_enabled ())
205 if (lgetfilecon_raw (path, &context) < 0)
210 if (getfilecon_raw (path, &context) < 0)
216 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
225 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
226 * (Mac) getxattr(..., XATTR_NOFOLLOW)
228 #ifdef HAVE_XATTR_NOFOLLOW
229 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
230 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
231 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
233 #define g_fgetxattr fgetxattr
234 #define g_flistxattr flistxattr
235 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
239 g_getxattr (const char *path, const char *name, void *value, size_t size,
240 gboolean follow_symlinks)
242 #ifdef HAVE_XATTR_NOFOLLOW
243 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
246 return getxattr (path, name, value, size);
248 return lgetxattr (path, name, value, size);
253 g_listxattr(const char *path, char *namebuf, size_t size,
254 gboolean follow_symlinks)
256 #ifdef HAVE_XATTR_NOFOLLOW
257 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
260 return listxattr (path, namebuf, size);
262 return llistxattr (path, namebuf, size);
269 return c >= 32 && c <= 126 && c != '\\';
273 name_is_valid (const char *str)
277 if (!valid_char (*str++))
284 hex_escape_string (const char *str,
285 gboolean *free_return)
288 char *escaped_str, *p;
290 static char *hex_digits = "0123456789abcdef";
296 for (i = 0; i < len; i++)
298 if (!valid_char (str[i]))
302 if (num_invalid == 0)
304 *free_return = FALSE;
308 escaped_str = g_malloc (len + num_invalid*3 + 1);
311 for (i = 0; i < len; i++)
313 if (valid_char (str[i]))
320 *p++ = hex_digits[(c >> 4) & 0xf];
321 *p++ = hex_digits[c & 0xf];
331 hex_unescape_string (const char *str,
333 gboolean *free_return)
336 char *unescaped_str, *p;
342 if (strchr (str, '\\') == NULL)
346 *free_return = FALSE;
350 unescaped_str = g_malloc (len + 1);
353 for (i = 0; i < len; i++)
355 if (str[i] == '\\' &&
360 (g_ascii_xdigit_value (str[i+2]) << 4) |
361 g_ascii_xdigit_value (str[i+3]);
371 *out_len = p - unescaped_str;
373 return unescaped_str;
377 escape_xattr (GFileInfo *info,
378 const char *gio_attr, /* gio attribute name */
379 const char *value, /* Is zero terminated */
380 size_t len /* not including zero termination */)
383 gboolean free_escaped_val;
385 escaped_val = hex_escape_string (value, &free_escaped_val);
387 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
389 if (free_escaped_val)
390 g_free (escaped_val);
394 get_one_xattr (const char *path,
396 const char *gio_attr,
398 gboolean follow_symlinks)
404 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
409 else if (len == -1 && errno == ERANGE)
411 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
416 value_p = g_malloc (len+1);
418 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
432 escape_xattr (info, gio_attr, value_p, len);
434 if (value_p != value)
438 #endif /* defined HAVE_XATTR */
441 get_xattrs (const char *path,
444 GFileAttributeMatcher *matcher,
445 gboolean follow_symlinks)
450 ssize_t list_res_size;
453 const char *attr, *attr2;
456 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
458 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
462 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
464 if (list_res_size == -1 ||
468 list_size = list_res_size;
469 list = g_malloc (list_size);
473 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
475 if (list_res_size == -1 && errno == ERANGE)
477 list_size = list_size * 2;
478 list = g_realloc (list, list_size);
482 if (list_res_size == -1)
486 while (list_res_size > 0)
488 if ((user && g_str_has_prefix (attr, "user.")) ||
489 (!user && !g_str_has_prefix (attr, "user.")))
491 char *escaped_attr, *gio_attr;
492 gboolean free_escaped_attr;
496 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
497 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
501 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
502 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
505 if (free_escaped_attr)
506 g_free (escaped_attr);
508 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
511 len = strlen (attr) + 1;
513 list_res_size -= len;
520 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
522 char *unescaped_attribute, *a;
523 gboolean free_unescaped_attribute;
525 attr2 = strchr (attr, ':');
528 attr2++; /* Skip ':' */
529 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
531 a = g_strconcat ("user.", unescaped_attribute, NULL);
533 a = unescaped_attribute;
535 get_one_xattr (path, info, attr, a, follow_symlinks);
540 if (free_unescaped_attribute)
541 g_free (unescaped_attribute);
545 #endif /* defined HAVE_XATTR */
550 get_one_xattr_from_fd (int fd,
552 const char *gio_attr,
559 len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
564 else if (len == -1 && errno == ERANGE)
566 len = g_fgetxattr (fd, xattr, NULL, 0);
571 value_p = g_malloc (len + 1);
573 len = g_fgetxattr (fd, xattr, value_p, len);
587 escape_xattr (info, gio_attr, value_p, len);
589 if (value_p != value)
592 #endif /* defined HAVE_XATTR */
595 get_xattrs_from_fd (int fd,
598 GFileAttributeMatcher *matcher)
603 ssize_t list_res_size;
606 const char *attr, *attr2;
609 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
611 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
615 list_res_size = g_flistxattr (fd, NULL, 0);
617 if (list_res_size == -1 ||
621 list_size = list_res_size;
622 list = g_malloc (list_size);
626 list_res_size = g_flistxattr (fd, list, list_size);
628 if (list_res_size == -1 && errno == ERANGE)
630 list_size = list_size * 2;
631 list = g_realloc (list, list_size);
635 if (list_res_size == -1)
639 while (list_res_size > 0)
641 if ((user && g_str_has_prefix (attr, "user.")) ||
642 (!user && !g_str_has_prefix (attr, "user.")))
644 char *escaped_attr, *gio_attr;
645 gboolean free_escaped_attr;
649 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
650 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
654 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
655 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
658 if (free_escaped_attr)
659 g_free (escaped_attr);
661 get_one_xattr_from_fd (fd, info, gio_attr, attr);
664 len = strlen (attr) + 1;
666 list_res_size -= len;
673 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
675 char *unescaped_attribute, *a;
676 gboolean free_unescaped_attribute;
678 attr2 = strchr (attr, ':');
681 attr2++; /* Skip ':' */
682 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
684 a = g_strconcat ("user.", unescaped_attribute, NULL);
686 a = unescaped_attribute;
688 get_one_xattr_from_fd (fd, info, attr, a);
693 if (free_unescaped_attribute)
694 g_free (unescaped_attribute);
698 #endif /* defined HAVE_XATTR */
703 set_xattr (char *filename,
704 const char *escaped_attribute,
705 const GFileAttributeValue *attr_value,
708 char *attribute, *value;
709 gboolean free_attribute, free_value;
710 int val_len, res, errsv;
714 if (attr_value == NULL)
716 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
717 _("Attribute value must be non-NULL"));
721 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
723 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
724 _("Invalid attribute type (string expected)"));
728 if (!name_is_valid (escaped_attribute))
730 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
731 _("Invalid extended attribute name"));
735 if (g_str_has_prefix (escaped_attribute, "xattr::"))
737 escaped_attribute += 6;
742 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
743 escaped_attribute += 10;
747 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
748 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
751 a = g_strconcat ("user.", attribute, NULL);
755 res = g_setxattr (filename, a, value, val_len);
769 g_set_error (error, G_IO_ERROR,
770 g_io_error_from_errno (errsv),
771 _("Error setting extended attribute '%s': %s"),
772 escaped_attribute, g_strerror (errsv));
783 _g_local_file_info_get_parent_info (const char *dir,
784 GFileAttributeMatcher *attribute_matcher,
785 GLocalParentFileInfo *parent_info)
787 /* Use plain struct stat for now as long as we only look at the
788 * S_ISVTX bit which doesn't exist on Win32 anyway.
793 parent_info->writable = FALSE;
794 parent_info->is_sticky = FALSE;
795 parent_info->has_trash_dir = FALSE;
796 parent_info->device = 0;
798 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME) ||
799 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE) ||
800 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH) ||
801 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT))
803 /* FIXME: Windows: The underlying _waccess() call in the C
804 * library is mostly pointless as it only looks at the READONLY
805 * FAT-style attribute of the file, it doesn't check the ACL at
808 parent_info->writable = (g_access (dir, W_OK) == 0);
810 res = g_stat (dir, &statbuf);
813 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
814 * renamed or deleted only by the owner of the file, by the owner of the directory, and
815 * by a privileged process.
820 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
822 parent_info->is_sticky = FALSE;
824 parent_info->owner = statbuf.st_uid;
825 parent_info->device = statbuf.st_dev;
826 /* No need to find trash dir if its not writable anyway */
827 if (parent_info->writable &&
828 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
829 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
835 get_access_rights (GFileAttributeMatcher *attribute_matcher,
838 GLocalFileStat *statbuf,
839 GLocalParentFileInfo *parent_info)
841 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
842 if (g_file_attribute_matcher_matches (attribute_matcher,
843 G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
844 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
845 g_access (path, R_OK) == 0);
847 if (g_file_attribute_matcher_matches (attribute_matcher,
848 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
849 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
850 g_access (path, W_OK) == 0);
852 if (g_file_attribute_matcher_matches (attribute_matcher,
853 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
854 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE,
855 g_access (path, X_OK) == 0);
863 if (parent_info->writable)
865 if (parent_info->is_sticky)
868 uid_t uid = geteuid ();
870 if (uid == statbuf->st_uid ||
871 uid == parent_info->owner ||
880 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME))
881 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME,
884 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE))
885 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE,
888 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
889 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH,
890 writable && parent_info->has_trash_dir);
895 set_info_from_stat (GFileInfo *info,
896 GLocalFileStat *statbuf,
897 GFileAttributeMatcher *attribute_matcher)
901 file_type = G_FILE_TYPE_UNKNOWN;
903 if (S_ISREG (statbuf->st_mode))
904 file_type = G_FILE_TYPE_REGULAR;
905 else if (S_ISDIR (statbuf->st_mode))
906 file_type = G_FILE_TYPE_DIRECTORY;
908 else if (S_ISCHR (statbuf->st_mode) ||
909 S_ISBLK (statbuf->st_mode) ||
910 S_ISFIFO (statbuf->st_mode)
912 || S_ISSOCK (statbuf->st_mode)
915 file_type = G_FILE_TYPE_SPECIAL;
918 else if (S_ISLNK (statbuf->st_mode))
919 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
922 g_file_info_set_file_type (info, file_type);
923 g_file_info_set_size (info, statbuf->st_size);
925 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE, statbuf->st_dev);
927 /* Pointless setting these on Windows even if they exist in the struct */
928 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_INODE, statbuf->st_ino);
929 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_NLINK, statbuf->st_nlink);
930 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_UID, statbuf->st_uid);
931 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, statbuf->st_gid);
932 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_RDEV, statbuf->st_rdev);
934 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
935 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, statbuf->st_mode);
936 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
937 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE, statbuf->st_blksize);
939 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
940 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_BLOCKS, statbuf->st_blocks);
943 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED, statbuf->st_mtime);
944 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
945 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
946 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
947 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
950 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS, statbuf->st_atime);
951 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
952 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
953 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
954 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
957 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_CHANGED, statbuf->st_ctime);
958 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
959 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
960 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
961 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
964 if (g_file_attribute_matcher_matches (attribute_matcher,
965 G_FILE_ATTRIBUTE_ETAG_VALUE))
967 char *etag = _g_local_file_info_create_etag (statbuf);
968 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ETAG_VALUE, etag);
972 if (g_file_attribute_matcher_matches (attribute_matcher,
973 G_FILE_ATTRIBUTE_ID_FILE))
975 char *id = _g_local_file_info_create_file_id (statbuf);
976 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE, id);
980 if (g_file_attribute_matcher_matches (attribute_matcher,
981 G_FILE_ATTRIBUTE_ID_FILESYSTEM))
983 char *id = _g_local_file_info_create_fs_id (statbuf);
984 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILESYSTEM, id);
992 make_valid_utf8 (const char *name)
995 const gchar *remainder, *invalid;
996 gint remaining_bytes, valid_bytes;
1000 remaining_bytes = strlen (name);
1002 while (remaining_bytes != 0)
1004 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1006 valid_bytes = invalid - remainder;
1009 string = g_string_sized_new (remaining_bytes);
1011 g_string_append_len (string, remainder, valid_bytes);
1012 /* append U+FFFD REPLACEMENT CHARACTER */
1013 g_string_append (string, "\357\277\275");
1015 remaining_bytes -= valid_bytes + 1;
1016 remainder = invalid + 1;
1020 return g_strdup (name);
1022 g_string_append (string, remainder);
1024 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1026 return g_string_free (string, FALSE);
1030 convert_pwd_string_to_utf8 (char *pwd_str)
1034 if (!g_utf8_validate (pwd_str, -1, NULL))
1036 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1037 if (utf8_string == NULL)
1038 utf8_string = make_valid_utf8 (pwd_str);
1041 utf8_string = g_strdup (pwd_str);
1047 uid_data_free (UidData *data)
1049 g_free (data->user_name);
1050 g_free (data->real_name);
1054 /* called with lock held */
1056 lookup_uid_data (uid_t uid)
1060 struct passwd pwbuf;
1061 struct passwd *pwbufp;
1062 char *gecos, *comma;
1064 if (uid_cache == NULL)
1065 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1067 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1072 data = g_new0 (UidData, 1);
1074 #if defined(HAVE_POSIX_GETPWUID_R)
1075 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1076 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1077 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1079 pwbufp = getpwuid (uid);
1084 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1085 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1087 gecos = pwbufp->pw_gecos;
1091 comma = strchr (gecos, ',');
1094 data->real_name = convert_pwd_string_to_utf8 (gecos);
1098 /* Default fallbacks */
1099 if (data->real_name == NULL)
1101 if (data->user_name != NULL)
1102 data->real_name = g_strdup (data->user_name);
1104 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1107 if (data->user_name == NULL)
1108 data->user_name = g_strdup_printf ("%d", (int)uid);
1110 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1116 get_username_from_uid (uid_t uid)
1122 data = lookup_uid_data (uid);
1123 res = g_strdup (data->user_name);
1124 G_UNLOCK (uid_cache);
1130 get_realname_from_uid (uid_t uid)
1136 data = lookup_uid_data (uid);
1137 res = g_strdup (data->real_name);
1138 G_UNLOCK (uid_cache);
1143 /* called with lock held */
1145 lookup_gid_name (gid_t gid)
1150 struct group *gbufp;
1152 if (gid_cache == NULL)
1153 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1155 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1160 #if defined (HAVE_POSIX_GETGRGID_R)
1161 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1162 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1163 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1165 gbufp = getgrgid (gid);
1168 if (gbufp != NULL &&
1169 gbufp->gr_name != NULL &&
1170 gbufp->gr_name[0] != 0)
1171 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1173 name = g_strdup_printf("%d", (int)gid);
1175 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1181 get_groupname_from_gid (gid_t gid)
1187 name = lookup_gid_name (gid);
1188 res = g_strdup (name);
1189 G_UNLOCK (gid_cache);
1193 #endif /* !G_OS_WIN32 */
1196 get_content_type (const char *basename,
1198 GLocalFileStat *statbuf,
1199 gboolean is_symlink,
1200 gboolean symlink_broken,
1201 GFileQueryInfoFlags flags,
1205 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1206 return g_strdup ("inode/symlink");
1207 else if (S_ISDIR(statbuf->st_mode))
1208 return g_strdup ("inode/directory");
1210 else if (S_ISCHR(statbuf->st_mode))
1211 return g_strdup ("inode/chardevice");
1212 else if (S_ISBLK(statbuf->st_mode))
1213 return g_strdup ("inode/blockdevice");
1214 else if (S_ISFIFO(statbuf->st_mode))
1215 return g_strdup ("inode/fifo");
1218 else if (S_ISSOCK(statbuf->st_mode))
1219 return g_strdup ("inode/socket");
1224 gboolean result_uncertain;
1226 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1229 if (!fast && result_uncertain && path != NULL)
1231 guchar sniff_buffer[4096];
1235 sniff_length = _g_unix_content_type_get_sniff_len ();
1236 if (sniff_length > 4096)
1237 sniff_length = 4096;
1239 fd = open (path, O_RDONLY);
1244 res = read (fd, sniff_buffer, sniff_length);
1248 g_free (content_type);
1249 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1255 return content_type;
1261 get_thumbnail_attributes (const char *path,
1264 GChecksum *checksum;
1269 uri = g_filename_to_uri (path, NULL, NULL);
1271 checksum = g_checksum_new (G_CHECKSUM_MD5);
1272 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1276 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1277 g_checksum_free (checksum);
1279 filename = g_build_filename (g_get_home_dir (),
1280 ".thumbnails", "normal", basename,
1283 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1284 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1288 filename = g_build_filename (g_get_home_dir (),
1289 ".thumbnails", "fail",
1290 "gnome-thumbnail-factory",
1294 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1295 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1303 win32_get_file_user_info (const gchar *filename,
1308 PSECURITY_DESCRIPTOR psd = NULL;
1309 DWORD sd_size = 0; /* first call calculates the size required */
1311 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1312 if ((GetFileSecurityW (wfilename,
1313 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1316 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1317 (psd = g_try_malloc (sd_size)) != NULL &&
1318 GetFileSecurityW (wfilename,
1319 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1326 SID_NAME_USE name_use = 0; /* don't care? */
1327 wchar_t *name = NULL;
1328 wchar_t *domain = NULL;
1330 DWORD domain_len = 0;
1331 /* get the user name */
1335 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1337 if (!LookupAccountSidW (NULL, /* local machine */
1340 domain, &domain_len, /* no domain info yet */
1341 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1343 name = g_try_malloc (name_len * sizeof (wchar_t));
1344 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1345 if (name && domain &&
1346 LookupAccountSidW (NULL, /* local machine */
1349 domain, &domain_len, /* no domain info yet */
1352 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1358 /* get the group name */
1362 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1364 if (!LookupAccountSidW (NULL, /* local machine */
1367 domain, &domain_len, /* no domain info yet */
1368 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1370 name = g_try_malloc (name_len * sizeof (wchar_t));
1371 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1372 if (name && domain &&
1373 LookupAccountSidW (NULL, /* local machine */
1376 domain, &domain_len, /* no domain info yet */
1379 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1385 /* TODO: get real name */
1391 #endif /* G_OS_WIN32 */
1394 _g_local_file_info_get (const char *basename,
1396 GFileAttributeMatcher *attribute_matcher,
1397 GFileQueryInfoFlags flags,
1398 GLocalParentFileInfo *parent_info,
1402 GLocalFileStat statbuf;
1404 struct stat statbuf2;
1407 gboolean is_symlink, symlink_broken;
1409 DWORD dos_attributes;
1412 info = g_file_info_new ();
1414 /* Make sure we don't set any unwanted attributes */
1415 g_file_info_set_attribute_mask (info, attribute_matcher);
1417 g_file_info_set_name (info, basename);
1419 /* Avoid stat in trivial case */
1420 if (attribute_matcher == NULL)
1424 res = g_lstat (path, &statbuf);
1427 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1432 g_object_unref (info);
1436 len = wcslen (wpath);
1437 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1440 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1443 res = _wstati64 (wpath, &statbuf);
1444 dos_attributes = GetFileAttributesW (wpath);
1453 char *display_name = g_filename_display_name (path);
1454 g_object_unref (info);
1455 g_set_error (error, G_IO_ERROR,
1456 g_io_error_from_errno (errsv),
1457 _("Error stating file '%s': %s"),
1458 display_name, g_strerror (errsv));
1459 g_free (display_name);
1464 is_symlink = S_ISLNK (statbuf.st_mode);
1468 symlink_broken = FALSE;
1473 g_file_info_set_is_symlink (info, TRUE);
1475 /* Unless NOFOLLOW was set we default to following symlinks */
1476 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1478 res = stat (path, &statbuf2);
1480 /* Report broken links as symlinks */
1484 symlink_broken = TRUE;
1489 set_info_from_stat (info, &statbuf, attribute_matcher);
1492 if (basename != NULL && basename[0] == '.')
1493 g_file_info_set_is_hidden (info, TRUE);
1495 if (basename != NULL && basename[strlen (basename) -1] == '~')
1496 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP, TRUE);
1498 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1499 g_file_info_set_is_hidden (info, TRUE);
1501 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1502 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_DOS_IS_ARCHIVE, TRUE);
1504 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1505 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_DOS_IS_SYSTEM, TRUE);
1510 g_file_attribute_matcher_matches (attribute_matcher,
1511 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET))
1513 char *link = read_link (path);
1514 g_file_info_set_symlink_target (info, link);
1519 if (g_file_attribute_matcher_matches (attribute_matcher,
1520 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME))
1522 char *display_name = g_filename_display_basename (path);
1524 /* look for U+FFFD REPLACEMENT CHARACTER */
1525 if (strstr (display_name, "\357\277\275") != NULL)
1527 char *p = display_name;
1528 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1531 g_file_info_set_display_name (info, display_name);
1532 g_free (display_name);
1535 if (g_file_attribute_matcher_matches (attribute_matcher,
1536 G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME))
1538 char *edit_name = g_filename_display_basename (path);
1539 g_file_info_set_edit_name (info, edit_name);
1544 if (g_file_attribute_matcher_matches (attribute_matcher,
1545 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME))
1547 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1549 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME, copy_name);
1553 if (g_file_attribute_matcher_matches (attribute_matcher,
1554 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE) ||
1555 g_file_attribute_matcher_matches (attribute_matcher,
1556 G_FILE_ATTRIBUTE_STANDARD_ICON))
1558 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1562 g_file_info_set_content_type (info, content_type);
1564 if (g_file_attribute_matcher_matches (attribute_matcher,
1565 G_FILE_ATTRIBUTE_STANDARD_ICON))
1569 icon = g_content_type_get_icon (content_type);
1572 if (G_IS_THEMED_ICON (icon))
1574 const char *type_icon;
1576 /* TODO: Special case desktop dir? That could be expensive with xdg dirs... */
1577 if (strcmp (path, g_get_home_dir ()) == 0)
1578 type_icon = "user-home";
1579 else if (S_ISDIR (statbuf.st_mode))
1580 type_icon = "folder";
1581 else if (statbuf.st_mode & S_IXUSR)
1582 type_icon = "application-x-executable";
1584 type_icon = "text-x-generic";
1586 g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
1589 g_file_info_set_icon (info, icon);
1590 g_object_unref (icon);
1594 g_free (content_type);
1598 if (g_file_attribute_matcher_matches (attribute_matcher,
1599 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE))
1601 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1605 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE, content_type);
1606 g_free (content_type);
1610 if (g_file_attribute_matcher_matches (attribute_matcher,
1611 G_FILE_ATTRIBUTE_OWNER_USER))
1616 win32_get_file_user_info (path, NULL, &name, NULL);
1618 name = get_username_from_uid (statbuf.st_uid);
1621 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1625 if (g_file_attribute_matcher_matches (attribute_matcher,
1626 G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1630 win32_get_file_user_info (path, NULL, NULL, &name);
1632 name = get_realname_from_uid (statbuf.st_uid);
1635 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1639 if (g_file_attribute_matcher_matches (attribute_matcher,
1640 G_FILE_ATTRIBUTE_OWNER_GROUP))
1644 win32_get_file_user_info (path, &name, NULL, NULL);
1646 name = get_groupname_from_gid (statbuf.st_gid);
1649 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1653 if (parent_info && parent_info->device != 0 &&
1654 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1655 statbuf.st_dev != parent_info->device)
1656 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1658 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1660 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1661 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1662 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1664 if (g_file_attribute_matcher_matches (attribute_matcher,
1665 G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1666 get_thumbnail_attributes (path, info);
1668 g_file_info_unset_attribute_mask (info);
1674 _g_local_file_info_get_from_fd (int fd,
1678 GLocalFileStat stat_buf;
1679 GFileAttributeMatcher *matcher;
1683 #define FSTAT _fstati64
1688 if (FSTAT (fd, &stat_buf) == -1)
1692 g_set_error (error, G_IO_ERROR,
1693 g_io_error_from_errno (errsv),
1694 _("Error stating file descriptor: %s"),
1695 g_strerror (errsv));
1699 info = g_file_info_new ();
1701 matcher = g_file_attribute_matcher_new (attributes);
1703 /* Make sure we don't set any unwanted attributes */
1704 g_file_info_set_attribute_mask (info, matcher);
1706 set_info_from_stat (info, &stat_buf, matcher);
1709 if (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) &&
1710 is_selinux_enabled ())
1713 if (fgetfilecon_raw (fd, &context) >= 0)
1715 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
1721 get_xattrs_from_fd (fd, TRUE, info, matcher);
1722 get_xattrs_from_fd (fd, FALSE, info, matcher);
1724 g_file_attribute_matcher_unref (matcher);
1726 g_file_info_unset_attribute_mask (info);
1732 get_uint32 (const GFileAttributeValue *value,
1736 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1738 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1739 _("Invalid attribute type (uint32 expected)"));
1743 *val_out = value->u.uint32;
1750 get_uint64 (const GFileAttributeValue *value,
1754 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1756 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1757 _("Invalid attribute type (uint64 expected)"));
1761 *val_out = value->u.uint64;
1767 #if defined(HAVE_SYMLINK)
1769 get_byte_string (const GFileAttributeValue *value,
1770 const char **val_out,
1773 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1775 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1776 _("Invalid attribute type (byte string expected)"));
1780 *val_out = value->u.string;
1787 set_unix_mode (char *filename,
1788 const GFileAttributeValue *value,
1793 if (!get_uint32 (value, &val, error))
1796 if (g_chmod (filename, val) == -1)
1800 g_set_error (error, G_IO_ERROR,
1801 g_io_error_from_errno (errsv),
1802 _("Error setting permissions: %s"),
1803 g_strerror (errsv));
1811 set_unix_uid_gid (char *filename,
1812 const GFileAttributeValue *uid_value,
1813 const GFileAttributeValue *gid_value,
1814 GFileQueryInfoFlags flags,
1824 if (!get_uint32 (uid_value, &val, error))
1833 if (!get_uint32 (gid_value, &val, error))
1841 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1842 res = lchown (filename, uid, gid);
1845 res = chown (filename, uid, gid);
1851 g_set_error (error, G_IO_ERROR,
1852 g_io_error_from_errno (errsv),
1853 _("Error setting owner: %s"),
1854 g_strerror (errsv));
1863 set_symlink (char *filename,
1864 const GFileAttributeValue *value,
1868 struct stat statbuf;
1870 if (!get_byte_string (value, &val, error))
1875 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1876 _("symlink must be non-NULL"));
1880 if (g_lstat (filename, &statbuf))
1884 g_set_error (error, G_IO_ERROR,
1885 g_io_error_from_errno (errsv),
1886 _("Error setting symlink: %s"),
1887 g_strerror (errsv));
1891 if (!S_ISLNK (statbuf.st_mode))
1893 g_set_error (error, G_IO_ERROR,
1894 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1895 _("Error setting symlink: file is not a symlink"));
1899 if (g_unlink (filename))
1903 g_set_error (error, G_IO_ERROR,
1904 g_io_error_from_errno (errsv),
1905 _("Error setting symlink: %s"),
1906 g_strerror (errsv));
1910 if (symlink (filename, val) != 0)
1914 g_set_error (error, G_IO_ERROR,
1915 g_io_error_from_errno (errsv),
1916 _("Error setting symlink: %s"),
1917 g_strerror (errsv));
1927 lazy_stat (char *filename,
1928 struct stat *statbuf,
1929 gboolean *called_stat)
1936 res = g_stat (filename, statbuf);
1939 *called_stat = TRUE;
1946 set_mtime_atime (char *filename,
1947 const GFileAttributeValue *mtime_value,
1948 const GFileAttributeValue *mtime_usec_value,
1949 const GFileAttributeValue *atime_value,
1950 const GFileAttributeValue *atime_usec_value,
1956 struct stat statbuf;
1957 gboolean got_stat = FALSE;
1958 struct timeval times[2] = { {0, 0}, {0, 0} };
1963 if (!get_uint64 (atime_value, &val, error))
1965 times[0].tv_sec = val;
1969 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1971 times[0].tv_sec = statbuf.st_mtime;
1972 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1973 times[0].tv_usec = statbuf.st_atimensec / 1000;
1974 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1975 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
1980 if (atime_usec_value)
1982 if (!get_uint32 (atime_usec_value, &val_usec, error))
1984 times[0].tv_usec = val_usec;
1990 if (!get_uint64 (mtime_value, &val, error))
1992 times[1].tv_sec = val;
1996 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1998 times[1].tv_sec = statbuf.st_mtime;
1999 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2000 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2001 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2002 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2007 if (mtime_usec_value)
2009 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2011 times[1].tv_usec = val_usec;
2014 res = utimes (filename, times);
2019 g_set_error (error, G_IO_ERROR,
2020 g_io_error_from_errno (errsv),
2021 _("Error setting owner: %s"),
2022 g_strerror (errsv));
2030 _g_local_file_info_set_attribute (char *filename,
2031 const char *attribute,
2032 GFileAttributeType type,
2034 GFileQueryInfoFlags flags,
2035 GCancellable *cancellable,
2038 GFileAttributeValue value = { 0 };
2040 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2042 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2043 return set_unix_mode (filename, &value, error);
2046 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2047 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2048 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2049 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2053 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2054 return set_symlink (filename, &value, error);
2058 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2059 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2060 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2061 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2062 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2063 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2064 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2065 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2069 else if (g_str_has_prefix (attribute, "xattr::"))
2070 return set_xattr (filename, attribute, &value, error);
2071 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2072 return set_xattr (filename, attribute, &value, error);
2075 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2076 _("Setting attribute %s not supported"), attribute);
2081 _g_local_file_info_set_attributes (char *filename,
2083 GFileQueryInfoFlags flags,
2084 GCancellable *cancellable,
2087 GFileAttributeValue *value;
2089 GFileAttributeValue *uid, *gid;
2092 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2094 #if defined (HAVE_CHOWN) && defined (HAVE_UTIMES)
2095 GFileAttributeStatus status;
2099 /* Handles setting multiple specified data in a single set, and takes care
2100 of ordering restrictions when setting attributes */
2104 /* Set symlink first, since this recreates the file */
2106 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2109 if (!set_symlink (filename, value, error))
2111 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2113 /* Don't set error multiple times */
2117 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2123 /* Group uid and gid setting into one call
2124 * Change ownership before permissions, since ownership changes can
2125 change permissions (e.g. setuid)
2127 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2128 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2132 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2134 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2136 /* Don't set error multiple times */
2140 status = G_FILE_ATTRIBUTE_STATUS_SET;
2142 uid->status = status;
2144 gid->status = status;
2148 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2151 if (!set_unix_mode (filename, value, error))
2153 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2155 /* Don't set error multiple times */
2159 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2164 /* Group all time settings into one call
2165 * Change times as the last thing to avoid it changing due to metadata changes
2168 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2169 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2170 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2171 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2173 if (mtime || mtime_usec || atime || atime_usec)
2175 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2177 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2179 /* Don't set error multiple times */
2183 status = G_FILE_ATTRIBUTE_STATUS_SET;
2186 mtime->status = status;
2188 mtime_usec->status = status;
2190 atime->status = status;
2192 atime_usec->status = status;
2196 /* xattrs are handled by default callback */