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>
46 #include <selinux/selinux.h>
51 #if defined HAVE_SYS_XATTR_H
52 #include <sys/xattr.h>
53 #elif defined HAVE_ATTR_XATTR_H
54 #include <attr/xattr.h>
56 #error "Neither <sys/xattr.h> nor <attr/xattr.h> is present but extended attribute support is enabled."
57 #endif /* defined HAVE_SYS_XATTR_H || HAVE_ATTR_XATTR_H */
59 #endif /* HAVE_XATTR */
61 #include <glib/gstdio.h>
62 #include <gfileattribute-priv.h>
63 #include <gfileinfo-priv.h>
78 #define X_OK 0 /* not really */
81 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
84 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
87 #define S_IXUSR _S_IEXEC
91 #include "glocalfileinfo.h"
93 #include "gthemedicon.h"
94 #include "gcontenttype.h"
95 #include "gcontenttypeprivate.h"
98 struct ThumbMD5Context {
101 unsigned char in[64];
111 G_LOCK_DEFINE_STATIC (uid_cache);
112 static GHashTable *uid_cache = NULL;
114 G_LOCK_DEFINE_STATIC (gid_cache);
115 static GHashTable *gid_cache = NULL;
117 #endif /* !G_OS_WIN32 */
120 _g_local_file_info_create_etag (GLocalFileStat *statbuf)
124 sec = statbuf->st_mtime;
125 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
126 usec = statbuf->st_mtimensec / 1000;
127 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
128 usec = statbuf->st_mtim.tv_nsec / 1000;
133 return g_strdup_printf ("%lu:%lu", sec, usec);
137 _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
139 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
140 (guint64) statbuf->st_dev,
141 (guint64) statbuf->st_ino);
145 _g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
147 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
148 (guint64) statbuf->st_dev);
155 read_link (const gchar *full_name)
162 buffer = g_malloc (size);
168 read_size = readlink (full_name, buffer, size);
174 if (read_size < size)
176 buffer[read_size] = 0;
180 buffer = g_realloc (buffer, size);
190 /* Get the SELinux security context */
192 get_selinux_context (const char *path,
194 GFileAttributeMatcher *attribute_matcher,
195 gboolean follow_symlinks)
199 if (!_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT))
202 if (is_selinux_enabled ())
206 if (lgetfilecon_raw (path, &context) < 0)
211 if (getfilecon_raw (path, &context) < 0)
217 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
226 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
227 * (Mac) getxattr(..., XATTR_NOFOLLOW)
229 #ifdef HAVE_XATTR_NOFOLLOW
230 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
231 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
232 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
234 #define g_fgetxattr fgetxattr
235 #define g_flistxattr flistxattr
236 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
240 g_getxattr (const char *path, const char *name, void *value, size_t size,
241 gboolean follow_symlinks)
243 #ifdef HAVE_XATTR_NOFOLLOW
244 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
247 return getxattr (path, name, value, size);
249 return lgetxattr (path, name, value, size);
254 g_listxattr(const char *path, char *namebuf, size_t size,
255 gboolean follow_symlinks)
257 #ifdef HAVE_XATTR_NOFOLLOW
258 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
261 return listxattr (path, namebuf, size);
263 return llistxattr (path, namebuf, size);
270 return c >= 32 && c <= 126 && c != '\\';
274 name_is_valid (const char *str)
278 if (!valid_char (*str++))
285 hex_escape_string (const char *str,
286 gboolean *free_return)
289 char *escaped_str, *p;
291 static char *hex_digits = "0123456789abcdef";
297 for (i = 0; i < len; i++)
299 if (!valid_char (str[i]))
303 if (num_invalid == 0)
305 *free_return = FALSE;
309 escaped_str = g_malloc (len + num_invalid*3 + 1);
312 for (i = 0; i < len; i++)
314 if (valid_char (str[i]))
321 *p++ = hex_digits[(c >> 4) & 0xf];
322 *p++ = hex_digits[c & 0xf];
332 hex_unescape_string (const char *str,
334 gboolean *free_return)
337 char *unescaped_str, *p;
343 if (strchr (str, '\\') == NULL)
347 *free_return = FALSE;
351 unescaped_str = g_malloc (len + 1);
354 for (i = 0; i < len; i++)
356 if (str[i] == '\\' &&
361 (g_ascii_xdigit_value (str[i+2]) << 4) |
362 g_ascii_xdigit_value (str[i+3]);
372 *out_len = p - unescaped_str;
374 return unescaped_str;
378 escape_xattr (GFileInfo *info,
379 const char *gio_attr, /* gio attribute name */
380 const char *value, /* Is zero terminated */
381 size_t len /* not including zero termination */)
384 gboolean free_escaped_val;
386 escaped_val = hex_escape_string (value, &free_escaped_val);
388 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
390 if (free_escaped_val)
391 g_free (escaped_val);
395 get_one_xattr (const char *path,
397 const char *gio_attr,
399 gboolean follow_symlinks)
405 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
410 else if (len == -1 && errno == ERANGE)
412 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
417 value_p = g_malloc (len+1);
419 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
433 escape_xattr (info, gio_attr, value_p, len);
435 if (value_p != value)
439 #endif /* defined HAVE_XATTR */
442 get_xattrs (const char *path,
445 GFileAttributeMatcher *matcher,
446 gboolean follow_symlinks)
451 ssize_t list_res_size;
454 const char *attr, *attr2;
457 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
459 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
463 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
465 if (list_res_size == -1 ||
469 list_size = list_res_size;
470 list = g_malloc (list_size);
474 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
476 if (list_res_size == -1 && errno == ERANGE)
478 list_size = list_size * 2;
479 list = g_realloc (list, list_size);
483 if (list_res_size == -1)
487 while (list_res_size > 0)
489 if ((user && g_str_has_prefix (attr, "user.")) ||
490 (!user && !g_str_has_prefix (attr, "user.")))
492 char *escaped_attr, *gio_attr;
493 gboolean free_escaped_attr;
497 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
498 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
502 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
503 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
506 if (free_escaped_attr)
507 g_free (escaped_attr);
509 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
514 len = strlen (attr) + 1;
516 list_res_size -= len;
523 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
525 char *unescaped_attribute, *a;
526 gboolean free_unescaped_attribute;
528 attr2 = strchr (attr, ':');
531 attr2 += 2; /* Skip '::' */
532 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
534 a = g_strconcat ("user.", unescaped_attribute, NULL);
536 a = unescaped_attribute;
538 get_one_xattr (path, info, attr, a, follow_symlinks);
543 if (free_unescaped_attribute)
544 g_free (unescaped_attribute);
548 #endif /* defined HAVE_XATTR */
553 get_one_xattr_from_fd (int fd,
555 const char *gio_attr,
562 len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
567 else if (len == -1 && errno == ERANGE)
569 len = g_fgetxattr (fd, xattr, NULL, 0);
574 value_p = g_malloc (len + 1);
576 len = g_fgetxattr (fd, xattr, value_p, len);
590 escape_xattr (info, gio_attr, value_p, len);
592 if (value_p != value)
595 #endif /* defined HAVE_XATTR */
598 get_xattrs_from_fd (int fd,
601 GFileAttributeMatcher *matcher)
606 ssize_t list_res_size;
609 const char *attr, *attr2;
612 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
614 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
618 list_res_size = g_flistxattr (fd, NULL, 0);
620 if (list_res_size == -1 ||
624 list_size = list_res_size;
625 list = g_malloc (list_size);
629 list_res_size = g_flistxattr (fd, list, list_size);
631 if (list_res_size == -1 && errno == ERANGE)
633 list_size = list_size * 2;
634 list = g_realloc (list, list_size);
638 if (list_res_size == -1)
642 while (list_res_size > 0)
644 if ((user && g_str_has_prefix (attr, "user.")) ||
645 (!user && !g_str_has_prefix (attr, "user.")))
647 char *escaped_attr, *gio_attr;
648 gboolean free_escaped_attr;
652 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
653 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
657 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
658 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
661 if (free_escaped_attr)
662 g_free (escaped_attr);
664 get_one_xattr_from_fd (fd, info, gio_attr, attr);
667 len = strlen (attr) + 1;
669 list_res_size -= len;
676 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
678 char *unescaped_attribute, *a;
679 gboolean free_unescaped_attribute;
681 attr2 = strchr (attr, ':');
684 attr2++; /* Skip ':' */
685 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
687 a = g_strconcat ("user.", unescaped_attribute, NULL);
689 a = unescaped_attribute;
691 get_one_xattr_from_fd (fd, info, attr, a);
696 if (free_unescaped_attribute)
697 g_free (unescaped_attribute);
701 #endif /* defined HAVE_XATTR */
706 set_xattr (char *filename,
707 const char *escaped_attribute,
708 const GFileAttributeValue *attr_value,
711 char *attribute, *value;
712 gboolean free_attribute, free_value;
713 int val_len, res, errsv;
717 if (attr_value == NULL)
719 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
720 _("Attribute value must be non-NULL"));
724 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
726 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
727 _("Invalid attribute type (string expected)"));
731 if (!name_is_valid (escaped_attribute))
733 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
734 _("Invalid extended attribute name"));
738 if (g_str_has_prefix (escaped_attribute, "xattr::"))
740 escaped_attribute += strlen ("xattr::");
745 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
746 escaped_attribute += strlen ("xattr-sys::");
750 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
751 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
754 a = g_strconcat ("user.", attribute, NULL);
758 res = g_setxattr (filename, a, value, val_len);
772 g_set_error (error, G_IO_ERROR,
773 g_io_error_from_errno (errsv),
774 _("Error setting extended attribute '%s': %s"),
775 escaped_attribute, g_strerror (errsv));
786 _g_local_file_info_get_parent_info (const char *dir,
787 GFileAttributeMatcher *attribute_matcher,
788 GLocalParentFileInfo *parent_info)
793 parent_info->extra_data = NULL;
794 parent_info->free_extra_data = NULL;
795 parent_info->writable = FALSE;
796 parent_info->is_sticky = FALSE;
797 parent_info->has_trash_dir = FALSE;
798 parent_info->device = 0;
800 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME) ||
801 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE) ||
802 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH) ||
803 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT))
805 /* FIXME: Windows: The underlying _waccess() call in the C
806 * library is mostly pointless as it only looks at the READONLY
807 * FAT-style attribute of the file, it doesn't check the ACL at
810 parent_info->writable = (g_access (dir, W_OK) == 0);
812 res = g_stat (dir, &statbuf);
815 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
816 * renamed or deleted only by the owner of the file, by the owner of the directory, and
817 * by a privileged process.
822 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
824 parent_info->is_sticky = FALSE;
826 parent_info->owner = statbuf.st_uid;
827 parent_info->device = statbuf.st_dev;
828 /* No need to find trash dir if it's not writable anyway */
829 if (parent_info->writable &&
830 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
831 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
837 _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info)
839 if (parent_info->extra_data &&
840 parent_info->free_extra_data)
841 parent_info->free_extra_data (parent_info->extra_data);
845 get_access_rights (GFileAttributeMatcher *attribute_matcher,
848 GLocalFileStat *statbuf,
849 GLocalParentFileInfo *parent_info)
851 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
852 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
853 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ))
854 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ,
855 g_access (path, R_OK) == 0);
857 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
858 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE))
859 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE,
860 g_access (path, W_OK) == 0);
862 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
863 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE))
864 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE,
865 g_access (path, X_OK) == 0);
873 if (parent_info->writable)
875 if (parent_info->is_sticky)
878 uid_t uid = geteuid ();
880 if (uid == statbuf->st_uid ||
881 uid == parent_info->owner ||
890 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME))
891 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME,
894 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE))
895 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE,
898 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
899 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH,
900 writable && parent_info->has_trash_dir);
905 set_info_from_stat (GFileInfo *info,
906 GLocalFileStat *statbuf,
907 GFileAttributeMatcher *attribute_matcher)
911 file_type = G_FILE_TYPE_UNKNOWN;
913 if (S_ISREG (statbuf->st_mode))
914 file_type = G_FILE_TYPE_REGULAR;
915 else if (S_ISDIR (statbuf->st_mode))
916 file_type = G_FILE_TYPE_DIRECTORY;
918 else if (S_ISCHR (statbuf->st_mode) ||
919 S_ISBLK (statbuf->st_mode) ||
920 S_ISFIFO (statbuf->st_mode)
922 || S_ISSOCK (statbuf->st_mode)
925 file_type = G_FILE_TYPE_SPECIAL;
928 else if (S_ISLNK (statbuf->st_mode))
929 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
932 g_file_info_set_file_type (info, file_type);
933 g_file_info_set_size (info, statbuf->st_size);
935 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
937 /* Pointless setting these on Windows even if they exist in the struct */
938 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
939 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
940 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
941 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
942 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
944 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
945 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
946 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
947 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
949 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
950 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
951 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
952 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
955 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
956 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
957 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
958 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
959 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
962 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
963 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
964 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
965 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
966 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
969 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
970 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
971 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
972 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
973 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
976 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
977 G_FILE_ATTRIBUTE_ID_ETAG_VALUE))
979 char *etag = _g_local_file_info_create_etag (statbuf);
980 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ETAG_VALUE, etag);
984 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
985 G_FILE_ATTRIBUTE_ID_ID_FILE))
987 char *id = _g_local_file_info_create_file_id (statbuf);
988 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILE, id);
992 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
993 G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM))
995 char *id = _g_local_file_info_create_fs_id (statbuf);
996 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM, id);
1004 make_valid_utf8 (const char *name)
1007 const gchar *remainder, *invalid;
1008 gint remaining_bytes, valid_bytes;
1012 remaining_bytes = strlen (name);
1014 while (remaining_bytes != 0)
1016 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1018 valid_bytes = invalid - remainder;
1021 string = g_string_sized_new (remaining_bytes);
1023 g_string_append_len (string, remainder, valid_bytes);
1024 /* append U+FFFD REPLACEMENT CHARACTER */
1025 g_string_append (string, "\357\277\275");
1027 remaining_bytes -= valid_bytes + 1;
1028 remainder = invalid + 1;
1032 return g_strdup (name);
1034 g_string_append (string, remainder);
1036 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1038 return g_string_free (string, FALSE);
1042 convert_pwd_string_to_utf8 (char *pwd_str)
1046 if (!g_utf8_validate (pwd_str, -1, NULL))
1048 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1049 if (utf8_string == NULL)
1050 utf8_string = make_valid_utf8 (pwd_str);
1053 utf8_string = g_strdup (pwd_str);
1059 uid_data_free (UidData *data)
1061 g_free (data->user_name);
1062 g_free (data->real_name);
1066 /* called with lock held */
1068 lookup_uid_data (uid_t uid)
1072 struct passwd pwbuf;
1073 struct passwd *pwbufp;
1074 char *gecos, *comma;
1076 if (uid_cache == NULL)
1077 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1079 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1084 data = g_new0 (UidData, 1);
1086 #if defined(HAVE_POSIX_GETPWUID_R)
1087 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1088 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1089 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1091 pwbufp = getpwuid (uid);
1096 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1097 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1099 gecos = pwbufp->pw_gecos;
1103 comma = strchr (gecos, ',');
1106 data->real_name = convert_pwd_string_to_utf8 (gecos);
1110 /* Default fallbacks */
1111 if (data->real_name == NULL)
1113 if (data->user_name != NULL)
1114 data->real_name = g_strdup (data->user_name);
1116 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1119 if (data->user_name == NULL)
1120 data->user_name = g_strdup_printf ("%d", (int)uid);
1122 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1128 get_username_from_uid (uid_t uid)
1134 data = lookup_uid_data (uid);
1135 res = g_strdup (data->user_name);
1136 G_UNLOCK (uid_cache);
1142 get_realname_from_uid (uid_t uid)
1148 data = lookup_uid_data (uid);
1149 res = g_strdup (data->real_name);
1150 G_UNLOCK (uid_cache);
1155 /* called with lock held */
1157 lookup_gid_name (gid_t gid)
1162 struct group *gbufp;
1164 if (gid_cache == NULL)
1165 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1167 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1172 #if defined (HAVE_POSIX_GETGRGID_R)
1173 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1174 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1175 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1177 gbufp = getgrgid (gid);
1180 if (gbufp != NULL &&
1181 gbufp->gr_name != NULL &&
1182 gbufp->gr_name[0] != 0)
1183 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1185 name = g_strdup_printf("%d", (int)gid);
1187 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1193 get_groupname_from_gid (gid_t gid)
1199 name = lookup_gid_name (gid);
1200 res = g_strdup (name);
1201 G_UNLOCK (gid_cache);
1205 #endif /* !G_OS_WIN32 */
1208 get_content_type (const char *basename,
1210 GLocalFileStat *statbuf,
1211 gboolean is_symlink,
1212 gboolean symlink_broken,
1213 GFileQueryInfoFlags flags,
1217 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1218 return g_strdup ("inode/symlink");
1219 else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
1220 return g_strdup ("inode/directory");
1222 else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
1223 return g_strdup ("inode/chardevice");
1224 else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
1225 return g_strdup ("inode/blockdevice");
1226 else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
1227 return g_strdup ("inode/fifo");
1230 else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
1231 return g_strdup ("inode/socket");
1236 gboolean result_uncertain;
1238 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1241 if (!fast && result_uncertain && path != NULL)
1243 guchar sniff_buffer[4096];
1247 sniff_length = _g_unix_content_type_get_sniff_len ();
1248 if (sniff_length > 4096)
1249 sniff_length = 4096;
1252 fd = open (path, O_RDONLY | O_NOATIME);
1253 if (fd < 0 && errno == EPERM)
1255 fd = open (path, O_RDONLY);
1261 res = read (fd, sniff_buffer, sniff_length);
1265 g_free (content_type);
1266 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1272 return content_type;
1278 get_thumbnail_attributes (const char *path,
1281 GChecksum *checksum;
1286 uri = g_filename_to_uri (path, NULL, NULL);
1288 checksum = g_checksum_new (G_CHECKSUM_MD5);
1289 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1293 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1294 g_checksum_free (checksum);
1296 filename = g_build_filename (g_get_home_dir (),
1297 ".thumbnails", "normal", basename,
1300 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1301 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1305 filename = g_build_filename (g_get_home_dir (),
1306 ".thumbnails", "fail",
1307 "gnome-thumbnail-factory",
1311 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1312 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1320 win32_get_file_user_info (const gchar *filename,
1325 PSECURITY_DESCRIPTOR psd = NULL;
1326 DWORD sd_size = 0; /* first call calculates the size required */
1328 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1329 if ((GetFileSecurityW (wfilename,
1330 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1333 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1334 (psd = g_try_malloc (sd_size)) != NULL &&
1335 GetFileSecurityW (wfilename,
1336 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1343 SID_NAME_USE name_use = 0; /* don't care? */
1344 wchar_t *name = NULL;
1345 wchar_t *domain = NULL;
1347 DWORD domain_len = 0;
1348 /* get the user name */
1352 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1354 if (!LookupAccountSidW (NULL, /* local machine */
1357 domain, &domain_len, /* no domain info yet */
1358 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1360 name = g_try_malloc (name_len * sizeof (wchar_t));
1361 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1362 if (name && domain &&
1363 LookupAccountSidW (NULL, /* local machine */
1366 domain, &domain_len, /* no domain info yet */
1369 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1375 /* get the group name */
1379 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1381 if (!LookupAccountSidW (NULL, /* local machine */
1384 domain, &domain_len, /* no domain info yet */
1385 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1387 name = g_try_malloc (name_len * sizeof (wchar_t));
1388 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1389 if (name && domain &&
1390 LookupAccountSidW (NULL, /* local machine */
1393 domain, &domain_len, /* no domain info yet */
1396 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1402 /* TODO: get real name */
1408 #endif /* G_OS_WIN32 */
1411 _g_local_file_info_get (const char *basename,
1413 GFileAttributeMatcher *attribute_matcher,
1414 GFileQueryInfoFlags flags,
1415 GLocalParentFileInfo *parent_info,
1419 GLocalFileStat statbuf;
1421 struct stat statbuf2;
1425 gboolean is_symlink, symlink_broken;
1427 DWORD dos_attributes;
1429 char *symlink_target;
1434 info = g_file_info_new ();
1436 /* Make sure we don't set any unwanted attributes */
1437 g_file_info_set_attribute_mask (info, attribute_matcher);
1439 g_file_info_set_name (info, basename);
1441 /* Avoid stat in trivial case */
1442 if (attribute_matcher == NULL)
1446 res = g_lstat (path, &statbuf);
1449 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1454 g_object_unref (info);
1458 len = wcslen (wpath);
1459 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1462 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1465 res = _wstati64 (wpath, &statbuf);
1466 dos_attributes = GetFileAttributesW (wpath);
1476 /* Don't bail out if we get Permission denied (SELinux?) */
1477 if (errsv != EACCES)
1479 char *display_name = g_filename_display_name (path);
1480 g_object_unref (info);
1481 g_set_error (error, G_IO_ERROR,
1482 g_io_error_from_errno (errsv),
1483 _("Error stating file '%s': %s"),
1484 display_name, g_strerror (errsv));
1485 g_free (display_name);
1490 /* Even if stat() fails, try to get as much as other attributes possible */
1491 stat_ok = res != -1;
1494 device = statbuf.st_dev;
1499 is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
1503 symlink_broken = FALSE;
1507 g_file_info_set_is_symlink (info, TRUE);
1509 /* Unless NOFOLLOW was set we default to following symlinks */
1510 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1512 res = stat (path, &statbuf2);
1514 /* Report broken links as symlinks */
1521 symlink_broken = TRUE;
1527 set_info_from_stat (info, &statbuf, attribute_matcher);
1530 if (basename != NULL && basename[0] == '.')
1531 g_file_info_set_is_hidden (info, TRUE);
1533 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1534 (stat_ok && S_ISREG (statbuf.st_mode)))
1535 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1537 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1538 g_file_info_set_is_hidden (info, TRUE);
1540 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1541 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1543 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1544 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1547 symlink_target = NULL;
1551 symlink_target = read_link (path);
1552 if (symlink_target &&
1553 _g_file_attribute_matcher_matches_id (attribute_matcher,
1554 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1555 g_file_info_set_symlink_target (info, symlink_target);
1558 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1559 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1561 char *display_name = g_filename_display_basename (path);
1563 /* look for U+FFFD REPLACEMENT CHARACTER */
1564 if (strstr (display_name, "\357\277\275") != NULL)
1566 char *p = display_name;
1567 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1570 g_file_info_set_display_name (info, display_name);
1571 g_free (display_name);
1574 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1575 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1577 char *edit_name = g_filename_display_basename (path);
1578 g_file_info_set_edit_name (info, edit_name);
1583 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1584 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1586 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1588 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1592 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1593 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1594 _g_file_attribute_matcher_matches_id (attribute_matcher,
1595 G_FILE_ATTRIBUTE_ID_STANDARD_ICON))
1597 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
1601 g_file_info_set_content_type (info, content_type);
1603 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1604 G_FILE_ATTRIBUTE_ID_STANDARD_ICON))
1608 if (strcmp (path, g_get_home_dir ()) == 0)
1609 icon = g_themed_icon_new ("user-home");
1610 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1611 icon = g_themed_icon_new ("user-desktop");
1612 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1613 icon = g_themed_icon_new_with_default_fallbacks ("folder-documents");
1614 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1615 icon = g_themed_icon_new_with_default_fallbacks ("folder-download");
1616 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1617 icon = g_themed_icon_new_with_default_fallbacks ("folder-music");
1618 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1619 icon = g_themed_icon_new_with_default_fallbacks ("folder-pictures");
1620 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1621 icon = g_themed_icon_new_with_default_fallbacks ("folder-publicshare");
1622 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1623 icon = g_themed_icon_new_with_default_fallbacks ("folder-templates");
1624 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1625 icon = g_themed_icon_new_with_default_fallbacks ("folder-videos");
1628 icon = g_content_type_get_icon (content_type);
1629 if (G_IS_THEMED_ICON (icon))
1631 const char *type_icon = NULL;
1633 if (S_ISDIR (statbuf.st_mode))
1634 type_icon = "folder";
1636 g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
1642 g_file_info_set_icon (info, icon);
1643 g_object_unref (icon);
1647 g_free (content_type);
1651 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1652 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1654 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
1658 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1659 g_free (content_type);
1663 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1664 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1669 win32_get_file_user_info (path, NULL, &name, NULL);
1672 name = get_username_from_uid (statbuf.st_uid);
1675 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1679 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1680 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1684 win32_get_file_user_info (path, NULL, NULL, &name);
1687 name = get_realname_from_uid (statbuf.st_uid);
1690 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1694 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1695 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1699 win32_get_file_user_info (path, &name, NULL, NULL);
1702 name = get_groupname_from_gid (statbuf.st_gid);
1705 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1709 if (stat_ok && parent_info && parent_info->device != 0 &&
1710 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1711 statbuf.st_dev != parent_info->device)
1712 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1715 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1718 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1720 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1721 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1723 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1724 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH))
1725 get_thumbnail_attributes (path, info);
1727 vfs = g_vfs_get_default ();
1728 class = G_VFS_GET_CLASS (vfs);
1729 if (class->local_file_add_info)
1731 class->local_file_add_info (vfs,
1737 &parent_info->extra_data,
1738 &parent_info->free_extra_data);
1741 g_file_info_unset_attribute_mask (info);
1743 g_free (symlink_target);
1749 _g_local_file_info_get_from_fd (int fd,
1750 const char *attributes,
1753 GLocalFileStat stat_buf;
1754 GFileAttributeMatcher *matcher;
1758 #define FSTAT _fstati64
1763 if (FSTAT (fd, &stat_buf) == -1)
1767 g_set_error (error, G_IO_ERROR,
1768 g_io_error_from_errno (errsv),
1769 _("Error stating file descriptor: %s"),
1770 g_strerror (errsv));
1774 info = g_file_info_new ();
1776 matcher = g_file_attribute_matcher_new (attributes);
1778 /* Make sure we don't set any unwanted attributes */
1779 g_file_info_set_attribute_mask (info, matcher);
1781 set_info_from_stat (info, &stat_buf, matcher);
1784 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
1785 is_selinux_enabled ())
1788 if (fgetfilecon_raw (fd, &context) >= 0)
1790 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
1796 get_xattrs_from_fd (fd, TRUE, info, matcher);
1797 get_xattrs_from_fd (fd, FALSE, info, matcher);
1799 g_file_attribute_matcher_unref (matcher);
1801 g_file_info_unset_attribute_mask (info);
1807 get_uint32 (const GFileAttributeValue *value,
1811 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1813 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1814 _("Invalid attribute type (uint32 expected)"));
1818 *val_out = value->u.uint32;
1825 get_uint64 (const GFileAttributeValue *value,
1829 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1831 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1832 _("Invalid attribute type (uint64 expected)"));
1836 *val_out = value->u.uint64;
1842 #if defined(HAVE_SYMLINK)
1844 get_byte_string (const GFileAttributeValue *value,
1845 const char **val_out,
1848 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1850 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1851 _("Invalid attribute type (byte string expected)"));
1855 *val_out = value->u.string;
1863 get_string (const GFileAttributeValue *value,
1864 const char **val_out,
1867 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
1869 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1870 _("Invalid attribute type (byte string expected)"));
1874 *val_out = value->u.string;
1881 set_unix_mode (char *filename,
1882 GFileQueryInfoFlags flags,
1883 const GFileAttributeValue *value,
1889 if (!get_uint32 (value, &val, error))
1893 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
1895 res = lchmod (filename, val);
1897 struct stat statbuf;
1898 /* Calling chmod on a symlink changes permissions on the symlink.
1899 * We don't want to do this, so we need to check for a symlink */
1900 res = g_lstat (filename, &statbuf);
1901 if (res == 0 && S_ISLNK (statbuf.st_mode))
1903 g_set_error_literal (error, G_IO_ERROR,
1904 G_IO_ERROR_NOT_SUPPORTED,
1905 _("Cannot set permissions on symlinks"));
1909 res = g_chmod (filename, val);
1913 res = g_chmod (filename, val);
1919 g_set_error (error, G_IO_ERROR,
1920 g_io_error_from_errno (errsv),
1921 _("Error setting permissions: %s"),
1922 g_strerror (errsv));
1930 set_unix_uid_gid (char *filename,
1931 const GFileAttributeValue *uid_value,
1932 const GFileAttributeValue *gid_value,
1933 GFileQueryInfoFlags flags,
1943 if (!get_uint32 (uid_value, &val, error))
1952 if (!get_uint32 (gid_value, &val, error))
1960 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1961 res = lchown (filename, uid, gid);
1964 res = chown (filename, uid, gid);
1970 g_set_error (error, G_IO_ERROR,
1971 g_io_error_from_errno (errsv),
1972 _("Error setting owner: %s"),
1973 g_strerror (errsv));
1982 set_symlink (char *filename,
1983 const GFileAttributeValue *value,
1987 struct stat statbuf;
1989 if (!get_byte_string (value, &val, error))
1994 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1995 _("symlink must be non-NULL"));
1999 if (g_lstat (filename, &statbuf))
2003 g_set_error (error, G_IO_ERROR,
2004 g_io_error_from_errno (errsv),
2005 _("Error setting symlink: %s"),
2006 g_strerror (errsv));
2010 if (!S_ISLNK (statbuf.st_mode))
2012 g_set_error_literal (error, G_IO_ERROR,
2013 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2014 _("Error setting symlink: file is not a symlink"));
2018 if (g_unlink (filename))
2022 g_set_error (error, G_IO_ERROR,
2023 g_io_error_from_errno (errsv),
2024 _("Error setting symlink: %s"),
2025 g_strerror (errsv));
2029 if (symlink (filename, val) != 0)
2033 g_set_error (error, G_IO_ERROR,
2034 g_io_error_from_errno (errsv),
2035 _("Error setting symlink: %s"),
2036 g_strerror (errsv));
2046 lazy_stat (char *filename,
2047 struct stat *statbuf,
2048 gboolean *called_stat)
2055 res = g_stat (filename, statbuf);
2058 *called_stat = TRUE;
2065 set_mtime_atime (char *filename,
2066 const GFileAttributeValue *mtime_value,
2067 const GFileAttributeValue *mtime_usec_value,
2068 const GFileAttributeValue *atime_value,
2069 const GFileAttributeValue *atime_usec_value,
2075 struct stat statbuf;
2076 gboolean got_stat = FALSE;
2077 struct timeval times[2] = { {0, 0}, {0, 0} };
2082 if (!get_uint64 (atime_value, &val, error))
2084 times[0].tv_sec = val;
2088 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2090 times[0].tv_sec = statbuf.st_mtime;
2091 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2092 times[0].tv_usec = statbuf.st_atimensec / 1000;
2093 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2094 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2099 if (atime_usec_value)
2101 if (!get_uint32 (atime_usec_value, &val_usec, error))
2103 times[0].tv_usec = val_usec;
2109 if (!get_uint64 (mtime_value, &val, error))
2111 times[1].tv_sec = val;
2115 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2117 times[1].tv_sec = statbuf.st_mtime;
2118 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2119 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2120 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2121 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2126 if (mtime_usec_value)
2128 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2130 times[1].tv_usec = val_usec;
2133 res = utimes (filename, times);
2138 g_set_error (error, G_IO_ERROR,
2139 g_io_error_from_errno (errsv),
2140 _("Error setting modification or access time: %s"),
2141 g_strerror (errsv));
2151 set_selinux_context (char *filename,
2152 const GFileAttributeValue *value,
2157 if (!get_string (value, &val, error))
2162 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2163 _("SELinux context must be non-NULL"));
2167 if (is_selinux_enabled ()) {
2168 security_context_t val_s;
2170 val_s = g_strdup (val);
2172 if (setfilecon_raw (filename, val_s) < 0)
2176 g_set_error (error, G_IO_ERROR,
2177 g_io_error_from_errno (errsv),
2178 _("Error setting SELinux context: %s"),
2179 g_strerror (errsv));
2184 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2185 _("SELinux is not enabled on this system"));
2195 _g_local_file_info_set_attribute (char *filename,
2196 const char *attribute,
2197 GFileAttributeType type,
2199 GFileQueryInfoFlags flags,
2200 GCancellable *cancellable,
2203 GFileAttributeValue value = { 0 };
2207 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2209 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2210 return set_unix_mode (filename, flags, &value, error);
2213 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2214 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2215 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2216 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2220 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2221 return set_symlink (filename, &value, error);
2225 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2226 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2227 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2228 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2229 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2230 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2231 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2232 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2236 else if (g_str_has_prefix (attribute, "xattr::"))
2237 return set_xattr (filename, attribute, &value, error);
2238 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2239 return set_xattr (filename, attribute, &value, error);
2243 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2244 return set_selinux_context (filename, &value, error);
2247 vfs = g_vfs_get_default ();
2248 class = G_VFS_GET_CLASS (vfs);
2249 if (class->local_file_set_attributes)
2253 info = g_file_info_new ();
2254 g_file_info_set_attribute (info,
2258 if (!class->local_file_set_attributes (vfs, filename,
2263 g_object_unref (info);
2267 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2269 g_object_unref (info);
2273 g_object_unref (info);
2276 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2277 _("Setting attribute %s not supported"), attribute);
2282 _g_local_file_info_set_attributes (char *filename,
2284 GFileQueryInfoFlags flags,
2285 GCancellable *cancellable,
2288 GFileAttributeValue *value;
2290 GFileAttributeValue *uid, *gid;
2293 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2295 #if defined (HAVE_CHOWN) || defined (HAVE_UTIMES)
2296 GFileAttributeStatus status;
2302 /* Handles setting multiple specified data in a single set, and takes care
2303 of ordering restrictions when setting attributes */
2307 /* Set symlink first, since this recreates the file */
2309 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2312 if (!set_symlink (filename, value, error))
2314 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2316 /* Don't set error multiple times */
2320 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2326 /* Group uid and gid setting into one call
2327 * Change ownership before permissions, since ownership changes can
2328 change permissions (e.g. setuid)
2330 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2331 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2335 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2337 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2339 /* Don't set error multiple times */
2343 status = G_FILE_ATTRIBUTE_STATUS_SET;
2345 uid->status = status;
2347 gid->status = status;
2351 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2354 if (!set_unix_mode (filename, flags, value, error))
2356 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2358 /* Don't set error multiple times */
2362 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2367 /* Group all time settings into one call
2368 * Change times as the last thing to avoid it changing due to metadata changes
2371 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2372 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2373 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2374 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2376 if (mtime || mtime_usec || atime || atime_usec)
2378 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2380 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2382 /* Don't set error multiple times */
2386 status = G_FILE_ATTRIBUTE_STATUS_SET;
2389 mtime->status = status;
2391 mtime_usec->status = status;
2393 atime->status = status;
2395 atime_usec->status = status;
2399 /* xattrs are handled by default callback */
2402 /* SELinux context */
2404 if (is_selinux_enabled ()) {
2405 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2408 if (!set_selinux_context (filename, value, error))
2410 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2412 /* Don't set error multiple times */
2416 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2421 vfs = g_vfs_get_default ();
2422 class = G_VFS_GET_CLASS (vfs);
2423 if (class->local_file_set_attributes)
2425 if (!class->local_file_set_attributes (vfs, filename,
2431 /* Don't set error multiple times */