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>
77 #define X_OK 0 /* not really */
80 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
83 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
86 #define S_IXUSR _S_IEXEC
90 #include "glocalfileinfo.h"
92 #include "gthemedicon.h"
93 #include "gcontenttype.h"
94 #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 (attribute_matcher, G_FILE_ATTRIBUTE_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 (info, G_FILE_ATTRIBUTE_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)
790 /* Use plain struct stat for now as long as we only look at the
791 * S_ISVTX bit which doesn't exist on Win32 anyway.
796 parent_info->extra_data = NULL;
797 parent_info->free_extra_data = NULL;
798 parent_info->writable = FALSE;
799 parent_info->is_sticky = FALSE;
800 parent_info->has_trash_dir = FALSE;
801 parent_info->device = 0;
803 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME) ||
804 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE) ||
805 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH) ||
806 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT))
808 /* FIXME: Windows: The underlying _waccess() call in the C
809 * library is mostly pointless as it only looks at the READONLY
810 * FAT-style attribute of the file, it doesn't check the ACL at
813 parent_info->writable = (g_access (dir, W_OK) == 0);
815 res = g_stat (dir, &statbuf);
818 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
819 * renamed or deleted only by the owner of the file, by the owner of the directory, and
820 * by a privileged process.
825 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
827 parent_info->is_sticky = FALSE;
829 parent_info->owner = statbuf.st_uid;
830 parent_info->device = statbuf.st_dev;
831 /* No need to find trash dir if it's not writable anyway */
832 if (parent_info->writable &&
833 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
834 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
840 _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info)
842 if (parent_info->extra_data &&
843 parent_info->free_extra_data)
844 parent_info->free_extra_data (parent_info->extra_data);
848 get_access_rights (GFileAttributeMatcher *attribute_matcher,
851 GLocalFileStat *statbuf,
852 GLocalParentFileInfo *parent_info)
854 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
855 if (g_file_attribute_matcher_matches (attribute_matcher,
856 G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
857 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
858 g_access (path, R_OK) == 0);
860 if (g_file_attribute_matcher_matches (attribute_matcher,
861 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
862 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
863 g_access (path, W_OK) == 0);
865 if (g_file_attribute_matcher_matches (attribute_matcher,
866 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
867 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE,
868 g_access (path, X_OK) == 0);
876 if (parent_info->writable)
878 if (parent_info->is_sticky)
881 uid_t uid = geteuid ();
883 if (uid == statbuf->st_uid ||
884 uid == parent_info->owner ||
893 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME))
894 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME,
897 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE))
898 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE,
901 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
902 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH,
903 writable && parent_info->has_trash_dir);
908 set_info_from_stat (GFileInfo *info,
909 GLocalFileStat *statbuf,
910 GFileAttributeMatcher *attribute_matcher)
914 file_type = G_FILE_TYPE_UNKNOWN;
916 if (S_ISREG (statbuf->st_mode))
917 file_type = G_FILE_TYPE_REGULAR;
918 else if (S_ISDIR (statbuf->st_mode))
919 file_type = G_FILE_TYPE_DIRECTORY;
921 else if (S_ISCHR (statbuf->st_mode) ||
922 S_ISBLK (statbuf->st_mode) ||
923 S_ISFIFO (statbuf->st_mode)
925 || S_ISSOCK (statbuf->st_mode)
928 file_type = G_FILE_TYPE_SPECIAL;
931 else if (S_ISLNK (statbuf->st_mode))
932 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
935 g_file_info_set_file_type (info, file_type);
936 g_file_info_set_size (info, statbuf->st_size);
938 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE, statbuf->st_dev);
940 /* Pointless setting these on Windows even if they exist in the struct */
941 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_INODE, statbuf->st_ino);
942 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_NLINK, statbuf->st_nlink);
943 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_UID, statbuf->st_uid);
944 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, statbuf->st_gid);
945 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_RDEV, statbuf->st_rdev);
947 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
948 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, statbuf->st_mode);
949 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
950 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE, statbuf->st_blksize);
952 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
953 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_BLOCKS, statbuf->st_blocks);
954 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_ALLOCATED_SIZE,
955 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
958 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED, statbuf->st_mtime);
959 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
960 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
961 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
962 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
965 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS, statbuf->st_atime);
966 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
967 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
968 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
969 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
972 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_CHANGED, statbuf->st_ctime);
973 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
974 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
975 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
976 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
979 if (g_file_attribute_matcher_matches (attribute_matcher,
980 G_FILE_ATTRIBUTE_ETAG_VALUE))
982 char *etag = _g_local_file_info_create_etag (statbuf);
983 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ETAG_VALUE, etag);
987 if (g_file_attribute_matcher_matches (attribute_matcher,
988 G_FILE_ATTRIBUTE_ID_FILE))
990 char *id = _g_local_file_info_create_file_id (statbuf);
991 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE, id);
995 if (g_file_attribute_matcher_matches (attribute_matcher,
996 G_FILE_ATTRIBUTE_ID_FILESYSTEM))
998 char *id = _g_local_file_info_create_fs_id (statbuf);
999 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILESYSTEM, id);
1007 make_valid_utf8 (const char *name)
1010 const gchar *remainder, *invalid;
1011 gint remaining_bytes, valid_bytes;
1015 remaining_bytes = strlen (name);
1017 while (remaining_bytes != 0)
1019 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1021 valid_bytes = invalid - remainder;
1024 string = g_string_sized_new (remaining_bytes);
1026 g_string_append_len (string, remainder, valid_bytes);
1027 /* append U+FFFD REPLACEMENT CHARACTER */
1028 g_string_append (string, "\357\277\275");
1030 remaining_bytes -= valid_bytes + 1;
1031 remainder = invalid + 1;
1035 return g_strdup (name);
1037 g_string_append (string, remainder);
1039 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1041 return g_string_free (string, FALSE);
1045 convert_pwd_string_to_utf8 (char *pwd_str)
1049 if (!g_utf8_validate (pwd_str, -1, NULL))
1051 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1052 if (utf8_string == NULL)
1053 utf8_string = make_valid_utf8 (pwd_str);
1056 utf8_string = g_strdup (pwd_str);
1062 uid_data_free (UidData *data)
1064 g_free (data->user_name);
1065 g_free (data->real_name);
1069 /* called with lock held */
1071 lookup_uid_data (uid_t uid)
1075 struct passwd pwbuf;
1076 struct passwd *pwbufp;
1077 char *gecos, *comma;
1079 if (uid_cache == NULL)
1080 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1082 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1087 data = g_new0 (UidData, 1);
1089 #if defined(HAVE_POSIX_GETPWUID_R)
1090 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1091 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1092 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1094 pwbufp = getpwuid (uid);
1099 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1100 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1102 gecos = pwbufp->pw_gecos;
1106 comma = strchr (gecos, ',');
1109 data->real_name = convert_pwd_string_to_utf8 (gecos);
1113 /* Default fallbacks */
1114 if (data->real_name == NULL)
1116 if (data->user_name != NULL)
1117 data->real_name = g_strdup (data->user_name);
1119 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1122 if (data->user_name == NULL)
1123 data->user_name = g_strdup_printf ("%d", (int)uid);
1125 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1131 get_username_from_uid (uid_t uid)
1137 data = lookup_uid_data (uid);
1138 res = g_strdup (data->user_name);
1139 G_UNLOCK (uid_cache);
1145 get_realname_from_uid (uid_t uid)
1151 data = lookup_uid_data (uid);
1152 res = g_strdup (data->real_name);
1153 G_UNLOCK (uid_cache);
1158 /* called with lock held */
1160 lookup_gid_name (gid_t gid)
1165 struct group *gbufp;
1167 if (gid_cache == NULL)
1168 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1170 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1175 #if defined (HAVE_POSIX_GETGRGID_R)
1176 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1177 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1178 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1180 gbufp = getgrgid (gid);
1183 if (gbufp != NULL &&
1184 gbufp->gr_name != NULL &&
1185 gbufp->gr_name[0] != 0)
1186 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1188 name = g_strdup_printf("%d", (int)gid);
1190 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1196 get_groupname_from_gid (gid_t gid)
1202 name = lookup_gid_name (gid);
1203 res = g_strdup (name);
1204 G_UNLOCK (gid_cache);
1208 #endif /* !G_OS_WIN32 */
1211 get_content_type (const char *basename,
1213 GLocalFileStat *statbuf,
1214 gboolean is_symlink,
1215 gboolean symlink_broken,
1216 GFileQueryInfoFlags flags,
1220 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1221 return g_strdup ("inode/symlink");
1222 else if (S_ISDIR(statbuf->st_mode))
1223 return g_strdup ("inode/directory");
1225 else if (S_ISCHR(statbuf->st_mode))
1226 return g_strdup ("inode/chardevice");
1227 else if (S_ISBLK(statbuf->st_mode))
1228 return g_strdup ("inode/blockdevice");
1229 else if (S_ISFIFO(statbuf->st_mode))
1230 return g_strdup ("inode/fifo");
1233 else if (S_ISSOCK(statbuf->st_mode))
1234 return g_strdup ("inode/socket");
1239 gboolean result_uncertain;
1241 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1244 if (!fast && result_uncertain && path != NULL)
1246 guchar sniff_buffer[4096];
1250 sniff_length = _g_unix_content_type_get_sniff_len ();
1251 if (sniff_length > 4096)
1252 sniff_length = 4096;
1255 fd = open (path, O_RDONLY | O_NOATIME);
1256 if (fd < 0 && errno == EPERM)
1258 fd = open (path, O_RDONLY);
1264 res = read (fd, sniff_buffer, sniff_length);
1268 g_free (content_type);
1269 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1275 return content_type;
1281 get_thumbnail_attributes (const char *path,
1284 GChecksum *checksum;
1289 uri = g_filename_to_uri (path, NULL, NULL);
1291 checksum = g_checksum_new (G_CHECKSUM_MD5);
1292 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1296 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1297 g_checksum_free (checksum);
1299 filename = g_build_filename (g_get_home_dir (),
1300 ".thumbnails", "normal", basename,
1303 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1304 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1308 filename = g_build_filename (g_get_home_dir (),
1309 ".thumbnails", "fail",
1310 "gnome-thumbnail-factory",
1314 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1315 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1323 win32_get_file_user_info (const gchar *filename,
1328 PSECURITY_DESCRIPTOR psd = NULL;
1329 DWORD sd_size = 0; /* first call calculates the size required */
1331 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1332 if ((GetFileSecurityW (wfilename,
1333 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1336 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1337 (psd = g_try_malloc (sd_size)) != NULL &&
1338 GetFileSecurityW (wfilename,
1339 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1346 SID_NAME_USE name_use = 0; /* don't care? */
1347 wchar_t *name = NULL;
1348 wchar_t *domain = NULL;
1350 DWORD domain_len = 0;
1351 /* get the user name */
1355 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1357 if (!LookupAccountSidW (NULL, /* local machine */
1360 domain, &domain_len, /* no domain info yet */
1361 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1363 name = g_try_malloc (name_len * sizeof (wchar_t));
1364 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1365 if (name && domain &&
1366 LookupAccountSidW (NULL, /* local machine */
1369 domain, &domain_len, /* no domain info yet */
1372 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1378 /* get the group name */
1382 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1384 if (!LookupAccountSidW (NULL, /* local machine */
1387 domain, &domain_len, /* no domain info yet */
1388 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1390 name = g_try_malloc (name_len * sizeof (wchar_t));
1391 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1392 if (name && domain &&
1393 LookupAccountSidW (NULL, /* local machine */
1396 domain, &domain_len, /* no domain info yet */
1399 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1405 /* TODO: get real name */
1411 #endif /* G_OS_WIN32 */
1414 _g_local_file_info_get (const char *basename,
1416 GFileAttributeMatcher *attribute_matcher,
1417 GFileQueryInfoFlags flags,
1418 GLocalParentFileInfo *parent_info,
1422 GLocalFileStat statbuf;
1424 struct stat statbuf2;
1427 gboolean is_symlink, symlink_broken;
1429 DWORD dos_attributes;
1431 char *symlink_target;
1435 info = g_file_info_new ();
1437 /* Make sure we don't set any unwanted attributes */
1438 g_file_info_set_attribute_mask (info, attribute_matcher);
1440 g_file_info_set_name (info, basename);
1442 /* Avoid stat in trivial case */
1443 if (attribute_matcher == NULL)
1447 res = g_lstat (path, &statbuf);
1450 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1455 g_object_unref (info);
1459 len = wcslen (wpath);
1460 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1463 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1466 res = _wstati64 (wpath, &statbuf);
1467 dos_attributes = GetFileAttributesW (wpath);
1476 char *display_name = g_filename_display_name (path);
1477 g_object_unref (info);
1478 g_set_error (error, G_IO_ERROR,
1479 g_io_error_from_errno (errsv),
1480 _("Error stating file '%s': %s"),
1481 display_name, g_strerror (errsv));
1482 g_free (display_name);
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 (info, G_FILE_ATTRIBUTE_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 (info, G_FILE_ATTRIBUTE_DOS_IS_ARCHIVE, TRUE);
1527 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1528 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_DOS_IS_SYSTEM, TRUE);
1531 symlink_target = NULL;
1534 symlink_target = read_link (path);
1535 if (symlink_target &&
1536 g_file_attribute_matcher_matches (attribute_matcher,
1537 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET))
1538 g_file_info_set_symlink_target (info, symlink_target);
1541 if (g_file_attribute_matcher_matches (attribute_matcher,
1542 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME))
1544 char *display_name = g_filename_display_basename (path);
1546 /* look for U+FFFD REPLACEMENT CHARACTER */
1547 if (strstr (display_name, "\357\277\275") != NULL)
1549 char *p = display_name;
1550 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1553 g_file_info_set_display_name (info, display_name);
1554 g_free (display_name);
1557 if (g_file_attribute_matcher_matches (attribute_matcher,
1558 G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME))
1560 char *edit_name = g_filename_display_basename (path);
1561 g_file_info_set_edit_name (info, edit_name);
1566 if (g_file_attribute_matcher_matches (attribute_matcher,
1567 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME))
1569 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1571 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME, copy_name);
1575 if (g_file_attribute_matcher_matches (attribute_matcher,
1576 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE) ||
1577 g_file_attribute_matcher_matches (attribute_matcher,
1578 G_FILE_ATTRIBUTE_STANDARD_ICON))
1580 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1584 g_file_info_set_content_type (info, content_type);
1586 if (g_file_attribute_matcher_matches (attribute_matcher,
1587 G_FILE_ATTRIBUTE_STANDARD_ICON))
1591 if (strcmp (path, g_get_home_dir ()) == 0)
1592 icon = g_themed_icon_new ("user-home");
1593 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1594 icon = g_themed_icon_new ("user-desktop");
1595 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1596 icon = g_themed_icon_new_with_default_fallbacks ("folder-documents");
1597 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1598 icon = g_themed_icon_new_with_default_fallbacks ("folder-download");
1599 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1600 icon = g_themed_icon_new_with_default_fallbacks ("folder-music");
1601 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1602 icon = g_themed_icon_new_with_default_fallbacks ("folder-pictures");
1603 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1604 icon = g_themed_icon_new_with_default_fallbacks ("folder-publicshare");
1605 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1606 icon = g_themed_icon_new_with_default_fallbacks ("folder-templates");
1607 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1608 icon = g_themed_icon_new_with_default_fallbacks ("folder-videos");
1611 icon = g_content_type_get_icon (content_type);
1612 if (G_IS_THEMED_ICON (icon))
1614 const char *type_icon = NULL;
1616 if (S_ISDIR (statbuf.st_mode))
1617 type_icon = "folder";
1619 g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
1625 g_file_info_set_icon (info, icon);
1626 g_object_unref (icon);
1630 g_free (content_type);
1634 if (g_file_attribute_matcher_matches (attribute_matcher,
1635 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE))
1637 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1641 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE, content_type);
1642 g_free (content_type);
1646 if (g_file_attribute_matcher_matches (attribute_matcher,
1647 G_FILE_ATTRIBUTE_OWNER_USER))
1652 win32_get_file_user_info (path, NULL, &name, NULL);
1654 name = get_username_from_uid (statbuf.st_uid);
1657 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1661 if (g_file_attribute_matcher_matches (attribute_matcher,
1662 G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1666 win32_get_file_user_info (path, NULL, NULL, &name);
1668 name = get_realname_from_uid (statbuf.st_uid);
1671 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1675 if (g_file_attribute_matcher_matches (attribute_matcher,
1676 G_FILE_ATTRIBUTE_OWNER_GROUP))
1680 win32_get_file_user_info (path, &name, NULL, NULL);
1682 name = get_groupname_from_gid (statbuf.st_gid);
1685 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1689 if (parent_info && parent_info->device != 0 &&
1690 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1691 statbuf.st_dev != parent_info->device)
1692 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1694 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1697 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1699 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1700 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1702 if (g_file_attribute_matcher_matches (attribute_matcher,
1703 G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1704 get_thumbnail_attributes (path, info);
1706 vfs = g_vfs_get_default ();
1707 class = G_VFS_GET_CLASS (vfs);
1708 if (class->local_file_add_info)
1710 const char *extra_target;
1712 extra_target = path;
1713 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) &&
1716 symlink_target != NULL)
1717 extra_target = symlink_target;
1719 class->local_file_add_info (vfs,
1725 &parent_info->extra_data,
1726 &parent_info->free_extra_data);
1729 g_file_info_unset_attribute_mask (info);
1731 g_free (symlink_target);
1737 _g_local_file_info_get_from_fd (int fd,
1738 const char *attributes,
1741 GLocalFileStat stat_buf;
1742 GFileAttributeMatcher *matcher;
1746 #define FSTAT _fstati64
1751 if (FSTAT (fd, &stat_buf) == -1)
1755 g_set_error (error, G_IO_ERROR,
1756 g_io_error_from_errno (errsv),
1757 _("Error stating file descriptor: %s"),
1758 g_strerror (errsv));
1762 info = g_file_info_new ();
1764 matcher = g_file_attribute_matcher_new (attributes);
1766 /* Make sure we don't set any unwanted attributes */
1767 g_file_info_set_attribute_mask (info, matcher);
1769 set_info_from_stat (info, &stat_buf, matcher);
1772 if (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) &&
1773 is_selinux_enabled ())
1776 if (fgetfilecon_raw (fd, &context) >= 0)
1778 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
1784 get_xattrs_from_fd (fd, TRUE, info, matcher);
1785 get_xattrs_from_fd (fd, FALSE, info, matcher);
1787 g_file_attribute_matcher_unref (matcher);
1789 g_file_info_unset_attribute_mask (info);
1795 get_uint32 (const GFileAttributeValue *value,
1799 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1801 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1802 _("Invalid attribute type (uint32 expected)"));
1806 *val_out = value->u.uint32;
1813 get_uint64 (const GFileAttributeValue *value,
1817 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1819 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1820 _("Invalid attribute type (uint64 expected)"));
1824 *val_out = value->u.uint64;
1830 #if defined(HAVE_SYMLINK)
1832 get_byte_string (const GFileAttributeValue *value,
1833 const char **val_out,
1836 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1838 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1839 _("Invalid attribute type (byte string expected)"));
1843 *val_out = value->u.string;
1851 get_string (const GFileAttributeValue *value,
1852 const char **val_out,
1855 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
1857 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1858 _("Invalid attribute type (byte string expected)"));
1862 *val_out = value->u.string;
1869 set_unix_mode (char *filename,
1870 const GFileAttributeValue *value,
1875 if (!get_uint32 (value, &val, error))
1878 if (g_chmod (filename, val) == -1)
1882 g_set_error (error, G_IO_ERROR,
1883 g_io_error_from_errno (errsv),
1884 _("Error setting permissions: %s"),
1885 g_strerror (errsv));
1893 set_unix_uid_gid (char *filename,
1894 const GFileAttributeValue *uid_value,
1895 const GFileAttributeValue *gid_value,
1896 GFileQueryInfoFlags flags,
1906 if (!get_uint32 (uid_value, &val, error))
1915 if (!get_uint32 (gid_value, &val, error))
1923 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1924 res = lchown (filename, uid, gid);
1927 res = chown (filename, uid, gid);
1933 g_set_error (error, G_IO_ERROR,
1934 g_io_error_from_errno (errsv),
1935 _("Error setting owner: %s"),
1936 g_strerror (errsv));
1945 set_symlink (char *filename,
1946 const GFileAttributeValue *value,
1950 struct stat statbuf;
1952 if (!get_byte_string (value, &val, error))
1957 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1958 _("symlink must be non-NULL"));
1962 if (g_lstat (filename, &statbuf))
1966 g_set_error (error, G_IO_ERROR,
1967 g_io_error_from_errno (errsv),
1968 _("Error setting symlink: %s"),
1969 g_strerror (errsv));
1973 if (!S_ISLNK (statbuf.st_mode))
1975 g_set_error_literal (error, G_IO_ERROR,
1976 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1977 _("Error setting symlink: file is not a symlink"));
1981 if (g_unlink (filename))
1985 g_set_error (error, G_IO_ERROR,
1986 g_io_error_from_errno (errsv),
1987 _("Error setting symlink: %s"),
1988 g_strerror (errsv));
1992 if (symlink (filename, val) != 0)
1996 g_set_error (error, G_IO_ERROR,
1997 g_io_error_from_errno (errsv),
1998 _("Error setting symlink: %s"),
1999 g_strerror (errsv));
2009 lazy_stat (char *filename,
2010 struct stat *statbuf,
2011 gboolean *called_stat)
2018 res = g_stat (filename, statbuf);
2021 *called_stat = TRUE;
2028 set_mtime_atime (char *filename,
2029 const GFileAttributeValue *mtime_value,
2030 const GFileAttributeValue *mtime_usec_value,
2031 const GFileAttributeValue *atime_value,
2032 const GFileAttributeValue *atime_usec_value,
2038 struct stat statbuf;
2039 gboolean got_stat = FALSE;
2040 struct timeval times[2] = { {0, 0}, {0, 0} };
2045 if (!get_uint64 (atime_value, &val, error))
2047 times[0].tv_sec = val;
2051 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2053 times[0].tv_sec = statbuf.st_mtime;
2054 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2055 times[0].tv_usec = statbuf.st_atimensec / 1000;
2056 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2057 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2062 if (atime_usec_value)
2064 if (!get_uint32 (atime_usec_value, &val_usec, error))
2066 times[0].tv_usec = val_usec;
2072 if (!get_uint64 (mtime_value, &val, error))
2074 times[1].tv_sec = val;
2078 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2080 times[1].tv_sec = statbuf.st_mtime;
2081 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2082 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2083 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2084 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2089 if (mtime_usec_value)
2091 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2093 times[1].tv_usec = val_usec;
2096 res = utimes (filename, times);
2101 g_set_error (error, G_IO_ERROR,
2102 g_io_error_from_errno (errsv),
2103 _("Error setting modification or access time: %s"),
2104 g_strerror (errsv));
2114 set_selinux_context (char *filename,
2115 const GFileAttributeValue *value,
2120 if (!get_string (value, &val, error))
2125 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2126 _("SELinux context must be non-NULL"));
2130 if (is_selinux_enabled ()) {
2131 security_context_t val_s;
2133 val_s = g_strdup (val);
2135 if (setfilecon_raw (filename, val_s) < 0)
2139 g_set_error (error, G_IO_ERROR,
2140 g_io_error_from_errno (errsv),
2141 _("Error setting SELinux context: %s"),
2142 g_strerror (errsv));
2147 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2148 _("SELinux is not enabled on this system"));
2158 _g_local_file_info_set_attribute (char *filename,
2159 const char *attribute,
2160 GFileAttributeType type,
2162 GFileQueryInfoFlags flags,
2163 GCancellable *cancellable,
2166 GFileAttributeValue value = { 0 };
2170 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2172 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2173 return set_unix_mode (filename, &value, error);
2176 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2177 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2178 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2179 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2183 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2184 return set_symlink (filename, &value, error);
2188 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2189 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2190 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2191 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2192 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2193 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2194 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2195 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2199 else if (g_str_has_prefix (attribute, "xattr::"))
2200 return set_xattr (filename, attribute, &value, error);
2201 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2202 return set_xattr (filename, attribute, &value, error);
2206 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2207 return set_selinux_context (filename, &value, error);
2210 vfs = g_vfs_get_default ();
2211 class = G_VFS_GET_CLASS (vfs);
2212 if (class->local_file_set_attributes)
2216 info = g_file_info_new ();
2217 g_file_info_set_attribute (info,
2221 if (!class->local_file_set_attributes (vfs, filename,
2226 g_object_unref (info);
2230 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2232 g_object_unref (info);
2236 g_object_unref (info);
2239 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2240 _("Setting attribute %s not supported"), attribute);
2245 _g_local_file_info_set_attributes (char *filename,
2247 GFileQueryInfoFlags flags,
2248 GCancellable *cancellable,
2251 GFileAttributeValue *value;
2253 GFileAttributeValue *uid, *gid;
2256 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2258 #if defined (HAVE_CHOWN) || defined (HAVE_UTIMES)
2259 GFileAttributeStatus status;
2265 /* Handles setting multiple specified data in a single set, and takes care
2266 of ordering restrictions when setting attributes */
2270 /* Set symlink first, since this recreates the file */
2272 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2275 if (!set_symlink (filename, value, error))
2277 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2279 /* Don't set error multiple times */
2283 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2289 /* Group uid and gid setting into one call
2290 * Change ownership before permissions, since ownership changes can
2291 change permissions (e.g. setuid)
2293 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2294 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2298 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2300 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2302 /* Don't set error multiple times */
2306 status = G_FILE_ATTRIBUTE_STATUS_SET;
2308 uid->status = status;
2310 gid->status = status;
2314 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2317 if (!set_unix_mode (filename, value, error))
2319 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2321 /* Don't set error multiple times */
2325 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2330 /* Group all time settings into one call
2331 * Change times as the last thing to avoid it changing due to metadata changes
2334 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2335 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2336 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2337 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2339 if (mtime || mtime_usec || atime || atime_usec)
2341 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2343 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2345 /* Don't set error multiple times */
2349 status = G_FILE_ATTRIBUTE_STATUS_SET;
2352 mtime->status = status;
2354 mtime_usec->status = status;
2356 atime->status = status;
2358 atime_usec->status = status;
2362 /* xattrs are handled by default callback */
2365 /* SELinux context */
2367 if (is_selinux_enabled ()) {
2368 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2371 if (!set_selinux_context (filename, value, error))
2373 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2375 /* Don't set error multiple times */
2379 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2384 vfs = g_vfs_get_default ();
2385 class = G_VFS_GET_CLASS (vfs);
2386 if (class->local_file_set_attributes)
2388 if (!class->local_file_set_attributes (vfs, filename,
2394 /* Don't set error multiple times */