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 tv.tv_sec = statbuf->st_mtime;
125 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
126 tv.tv_usec = statbuf->st_mtimensec / 1000;
127 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
128 tv.tv_usec = statbuf->st_mtim.tv_nsec / 1000;
133 return g_strdup_printf ("%lu:%lu", tv.tv_sec, tv.tv_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 (S_ISDIR(statbuf->st_mode))
1220 return g_strdup ("inode/directory");
1222 else if (S_ISCHR(statbuf->st_mode))
1223 return g_strdup ("inode/chardevice");
1224 else if (S_ISBLK(statbuf->st_mode))
1225 return g_strdup ("inode/blockdevice");
1226 else if (S_ISFIFO(statbuf->st_mode))
1227 return g_strdup ("inode/fifo");
1230 else if (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;
1424 gboolean is_symlink, symlink_broken;
1426 DWORD dos_attributes;
1428 char *symlink_target;
1433 info = g_file_info_new ();
1435 /* Make sure we don't set any unwanted attributes */
1436 g_file_info_set_attribute_mask (info, attribute_matcher);
1438 g_file_info_set_name (info, basename);
1440 /* Avoid stat in trivial case */
1441 if (attribute_matcher == NULL)
1445 res = g_lstat (path, &statbuf);
1448 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1453 g_object_unref (info);
1457 len = wcslen (wpath);
1458 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1461 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1464 res = _wstati64 (wpath, &statbuf);
1465 dos_attributes = GetFileAttributesW (wpath);
1474 char *display_name = g_filename_display_name (path);
1475 g_object_unref (info);
1476 g_set_error (error, G_IO_ERROR,
1477 g_io_error_from_errno (errsv),
1478 _("Error stating file '%s': %s"),
1479 display_name, g_strerror (errsv));
1480 g_free (display_name);
1484 device = statbuf.st_dev;
1487 is_symlink = S_ISLNK (statbuf.st_mode);
1491 symlink_broken = FALSE;
1495 g_file_info_set_is_symlink (info, TRUE);
1497 /* Unless NOFOLLOW was set we default to following symlinks */
1498 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1500 res = stat (path, &statbuf2);
1502 /* Report broken links as symlinks */
1506 symlink_broken = TRUE;
1511 set_info_from_stat (info, &statbuf, attribute_matcher);
1514 if (basename != NULL && basename[0] == '.')
1515 g_file_info_set_is_hidden (info, TRUE);
1517 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1518 S_ISREG (statbuf.st_mode))
1519 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1521 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1522 g_file_info_set_is_hidden (info, TRUE);
1524 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1525 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1527 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1528 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1531 symlink_target = NULL;
1535 symlink_target = read_link (path);
1536 if (symlink_target &&
1537 _g_file_attribute_matcher_matches_id (attribute_matcher,
1538 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1539 g_file_info_set_symlink_target (info, symlink_target);
1542 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1543 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1545 char *display_name = g_filename_display_basename (path);
1547 /* look for U+FFFD REPLACEMENT CHARACTER */
1548 if (strstr (display_name, "\357\277\275") != NULL)
1550 char *p = display_name;
1551 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1554 g_file_info_set_display_name (info, display_name);
1555 g_free (display_name);
1558 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1559 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1561 char *edit_name = g_filename_display_basename (path);
1562 g_file_info_set_edit_name (info, edit_name);
1567 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1568 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1570 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1572 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1576 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1577 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1578 _g_file_attribute_matcher_matches_id (attribute_matcher,
1579 G_FILE_ATTRIBUTE_ID_STANDARD_ICON))
1581 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1585 g_file_info_set_content_type (info, content_type);
1587 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1588 G_FILE_ATTRIBUTE_ID_STANDARD_ICON))
1592 if (strcmp (path, g_get_home_dir ()) == 0)
1593 icon = g_themed_icon_new ("user-home");
1594 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1595 icon = g_themed_icon_new ("user-desktop");
1596 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1597 icon = g_themed_icon_new_with_default_fallbacks ("folder-documents");
1598 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1599 icon = g_themed_icon_new_with_default_fallbacks ("folder-download");
1600 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1601 icon = g_themed_icon_new_with_default_fallbacks ("folder-music");
1602 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1603 icon = g_themed_icon_new_with_default_fallbacks ("folder-pictures");
1604 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1605 icon = g_themed_icon_new_with_default_fallbacks ("folder-publicshare");
1606 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1607 icon = g_themed_icon_new_with_default_fallbacks ("folder-templates");
1608 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1609 icon = g_themed_icon_new_with_default_fallbacks ("folder-videos");
1612 icon = g_content_type_get_icon (content_type);
1613 if (G_IS_THEMED_ICON (icon))
1615 const char *type_icon = NULL;
1617 if (S_ISDIR (statbuf.st_mode))
1618 type_icon = "folder";
1620 g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
1626 g_file_info_set_icon (info, icon);
1627 g_object_unref (icon);
1631 g_free (content_type);
1635 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1636 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1638 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1642 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1643 g_free (content_type);
1647 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1648 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1653 win32_get_file_user_info (path, NULL, &name, NULL);
1655 name = get_username_from_uid (statbuf.st_uid);
1658 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1662 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1663 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1667 win32_get_file_user_info (path, NULL, NULL, &name);
1669 name = get_realname_from_uid (statbuf.st_uid);
1672 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1676 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1677 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1681 win32_get_file_user_info (path, &name, NULL, NULL);
1683 name = get_groupname_from_gid (statbuf.st_gid);
1686 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1690 if (parent_info && parent_info->device != 0 &&
1691 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1692 statbuf.st_dev != parent_info->device)
1693 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1695 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1698 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1700 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1701 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1703 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1704 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH))
1705 get_thumbnail_attributes (path, info);
1707 vfs = g_vfs_get_default ();
1708 class = G_VFS_GET_CLASS (vfs);
1709 if (class->local_file_add_info)
1711 class->local_file_add_info (vfs,
1717 &parent_info->extra_data,
1718 &parent_info->free_extra_data);
1721 g_file_info_unset_attribute_mask (info);
1723 g_free (symlink_target);
1729 _g_local_file_info_get_from_fd (int fd,
1730 const char *attributes,
1733 GLocalFileStat stat_buf;
1734 GFileAttributeMatcher *matcher;
1738 #define FSTAT _fstati64
1743 if (FSTAT (fd, &stat_buf) == -1)
1747 g_set_error (error, G_IO_ERROR,
1748 g_io_error_from_errno (errsv),
1749 _("Error stating file descriptor: %s"),
1750 g_strerror (errsv));
1754 info = g_file_info_new ();
1756 matcher = g_file_attribute_matcher_new (attributes);
1758 /* Make sure we don't set any unwanted attributes */
1759 g_file_info_set_attribute_mask (info, matcher);
1761 set_info_from_stat (info, &stat_buf, matcher);
1764 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
1765 is_selinux_enabled ())
1768 if (fgetfilecon_raw (fd, &context) >= 0)
1770 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
1776 get_xattrs_from_fd (fd, TRUE, info, matcher);
1777 get_xattrs_from_fd (fd, FALSE, info, matcher);
1779 g_file_attribute_matcher_unref (matcher);
1781 g_file_info_unset_attribute_mask (info);
1787 get_uint32 (const GFileAttributeValue *value,
1791 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1793 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1794 _("Invalid attribute type (uint32 expected)"));
1798 *val_out = value->u.uint32;
1805 get_uint64 (const GFileAttributeValue *value,
1809 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1811 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1812 _("Invalid attribute type (uint64 expected)"));
1816 *val_out = value->u.uint64;
1822 #if defined(HAVE_SYMLINK)
1824 get_byte_string (const GFileAttributeValue *value,
1825 const char **val_out,
1828 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1830 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1831 _("Invalid attribute type (byte string expected)"));
1835 *val_out = value->u.string;
1843 get_string (const GFileAttributeValue *value,
1844 const char **val_out,
1847 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
1849 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1850 _("Invalid attribute type (byte string expected)"));
1854 *val_out = value->u.string;
1861 set_unix_mode (char *filename,
1862 GFileQueryInfoFlags flags,
1863 const GFileAttributeValue *value,
1869 if (!get_uint32 (value, &val, error))
1873 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
1875 res = lchmod (filename, val);
1877 struct stat statbuf;
1878 /* Calling chmod on a symlink changes permissions on the symlink.
1879 * We don't want to do this, so we need to check for a symlink */
1880 res = g_lstat (filename, &statbuf);
1881 if (res == 0 && S_ISLNK (statbuf.st_mode))
1883 g_set_error_literal (error, G_IO_ERROR,
1884 G_IO_ERROR_NOT_SUPPORTED,
1885 _("Cannot set permissions on symlinks"));
1889 res = g_chmod (filename, val);
1893 res = g_chmod (filename, val);
1899 g_set_error (error, G_IO_ERROR,
1900 g_io_error_from_errno (errsv),
1901 _("Error setting permissions: %s"),
1902 g_strerror (errsv));
1910 set_unix_uid_gid (char *filename,
1911 const GFileAttributeValue *uid_value,
1912 const GFileAttributeValue *gid_value,
1913 GFileQueryInfoFlags flags,
1923 if (!get_uint32 (uid_value, &val, error))
1932 if (!get_uint32 (gid_value, &val, error))
1940 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1941 res = lchown (filename, uid, gid);
1944 res = chown (filename, uid, gid);
1950 g_set_error (error, G_IO_ERROR,
1951 g_io_error_from_errno (errsv),
1952 _("Error setting owner: %s"),
1953 g_strerror (errsv));
1962 set_symlink (char *filename,
1963 const GFileAttributeValue *value,
1967 struct stat statbuf;
1969 if (!get_byte_string (value, &val, error))
1974 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1975 _("symlink must be non-NULL"));
1979 if (g_lstat (filename, &statbuf))
1983 g_set_error (error, G_IO_ERROR,
1984 g_io_error_from_errno (errsv),
1985 _("Error setting symlink: %s"),
1986 g_strerror (errsv));
1990 if (!S_ISLNK (statbuf.st_mode))
1992 g_set_error_literal (error, G_IO_ERROR,
1993 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1994 _("Error setting symlink: file is not a symlink"));
1998 if (g_unlink (filename))
2002 g_set_error (error, G_IO_ERROR,
2003 g_io_error_from_errno (errsv),
2004 _("Error setting symlink: %s"),
2005 g_strerror (errsv));
2009 if (symlink (filename, val) != 0)
2013 g_set_error (error, G_IO_ERROR,
2014 g_io_error_from_errno (errsv),
2015 _("Error setting symlink: %s"),
2016 g_strerror (errsv));
2026 lazy_stat (char *filename,
2027 struct stat *statbuf,
2028 gboolean *called_stat)
2035 res = g_stat (filename, statbuf);
2038 *called_stat = TRUE;
2045 set_mtime_atime (char *filename,
2046 const GFileAttributeValue *mtime_value,
2047 const GFileAttributeValue *mtime_usec_value,
2048 const GFileAttributeValue *atime_value,
2049 const GFileAttributeValue *atime_usec_value,
2055 struct stat statbuf;
2056 gboolean got_stat = FALSE;
2057 struct timeval times[2] = { {0, 0}, {0, 0} };
2062 if (!get_uint64 (atime_value, &val, error))
2064 times[0].tv_sec = val;
2068 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2070 times[0].tv_sec = statbuf.st_mtime;
2071 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2072 times[0].tv_usec = statbuf.st_atimensec / 1000;
2073 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2074 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2079 if (atime_usec_value)
2081 if (!get_uint32 (atime_usec_value, &val_usec, error))
2083 times[0].tv_usec = val_usec;
2089 if (!get_uint64 (mtime_value, &val, error))
2091 times[1].tv_sec = val;
2095 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2097 times[1].tv_sec = statbuf.st_mtime;
2098 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2099 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2100 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2101 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2106 if (mtime_usec_value)
2108 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2110 times[1].tv_usec = val_usec;
2113 res = utimes (filename, times);
2118 g_set_error (error, G_IO_ERROR,
2119 g_io_error_from_errno (errsv),
2120 _("Error setting modification or access time: %s"),
2121 g_strerror (errsv));
2131 set_selinux_context (char *filename,
2132 const GFileAttributeValue *value,
2137 if (!get_string (value, &val, error))
2142 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2143 _("SELinux context must be non-NULL"));
2147 if (is_selinux_enabled ()) {
2148 security_context_t val_s;
2150 val_s = g_strdup (val);
2152 if (setfilecon_raw (filename, val_s) < 0)
2156 g_set_error (error, G_IO_ERROR,
2157 g_io_error_from_errno (errsv),
2158 _("Error setting SELinux context: %s"),
2159 g_strerror (errsv));
2164 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2165 _("SELinux is not enabled on this system"));
2175 _g_local_file_info_set_attribute (char *filename,
2176 const char *attribute,
2177 GFileAttributeType type,
2179 GFileQueryInfoFlags flags,
2180 GCancellable *cancellable,
2183 GFileAttributeValue value = { 0 };
2187 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2189 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2190 return set_unix_mode (filename, flags, &value, error);
2193 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2194 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2195 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2196 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2200 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2201 return set_symlink (filename, &value, error);
2205 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2206 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2207 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2208 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2209 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2210 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2211 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2212 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2216 else if (g_str_has_prefix (attribute, "xattr::"))
2217 return set_xattr (filename, attribute, &value, error);
2218 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2219 return set_xattr (filename, attribute, &value, error);
2223 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2224 return set_selinux_context (filename, &value, error);
2227 vfs = g_vfs_get_default ();
2228 class = G_VFS_GET_CLASS (vfs);
2229 if (class->local_file_set_attributes)
2233 info = g_file_info_new ();
2234 g_file_info_set_attribute (info,
2238 if (!class->local_file_set_attributes (vfs, filename,
2243 g_object_unref (info);
2247 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2249 g_object_unref (info);
2253 g_object_unref (info);
2256 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2257 _("Setting attribute %s not supported"), attribute);
2262 _g_local_file_info_set_attributes (char *filename,
2264 GFileQueryInfoFlags flags,
2265 GCancellable *cancellable,
2268 GFileAttributeValue *value;
2270 GFileAttributeValue *uid, *gid;
2273 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2275 #if defined (HAVE_CHOWN) || defined (HAVE_UTIMES)
2276 GFileAttributeStatus status;
2282 /* Handles setting multiple specified data in a single set, and takes care
2283 of ordering restrictions when setting attributes */
2287 /* Set symlink first, since this recreates the file */
2289 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2292 if (!set_symlink (filename, value, error))
2294 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2296 /* Don't set error multiple times */
2300 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2306 /* Group uid and gid setting into one call
2307 * Change ownership before permissions, since ownership changes can
2308 change permissions (e.g. setuid)
2310 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2311 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2315 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2317 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2319 /* Don't set error multiple times */
2323 status = G_FILE_ATTRIBUTE_STATUS_SET;
2325 uid->status = status;
2327 gid->status = status;
2331 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2334 if (!set_unix_mode (filename, flags, value, error))
2336 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2338 /* Don't set error multiple times */
2342 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2347 /* Group all time settings into one call
2348 * Change times as the last thing to avoid it changing due to metadata changes
2351 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2352 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2353 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2354 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2356 if (mtime || mtime_usec || atime || atime_usec)
2358 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2360 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2362 /* Don't set error multiple times */
2366 status = G_FILE_ATTRIBUTE_STATUS_SET;
2369 mtime->status = status;
2371 mtime_usec->status = status;
2373 atime->status = status;
2375 atime_usec->status = status;
2379 /* xattrs are handled by default callback */
2382 /* SELinux context */
2384 if (is_selinux_enabled ()) {
2385 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2388 if (!set_selinux_context (filename, value, error))
2390 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2392 /* Don't set error multiple times */
2396 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2401 vfs = g_vfs_get_default ();
2402 class = G_VFS_GET_CLASS (vfs);
2403 if (class->local_file_set_attributes)
2405 if (!class->local_file_set_attributes (vfs, filename,
2411 /* Don't set error multiple times */