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"
99 struct ThumbMD5Context {
102 unsigned char in[64];
112 G_LOCK_DEFINE_STATIC (uid_cache);
113 static GHashTable *uid_cache = NULL;
115 G_LOCK_DEFINE_STATIC (gid_cache);
116 static GHashTable *gid_cache = NULL;
118 #endif /* !G_OS_WIN32 */
121 _g_local_file_info_create_etag (GLocalFileStat *statbuf)
125 tv.tv_sec = statbuf->st_mtime;
126 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
127 tv.tv_usec = statbuf->st_mtimensec / 1000;
128 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
129 tv.tv_usec = statbuf->st_mtim.tv_nsec / 1000;
134 return g_strdup_printf ("%lu:%lu", tv.tv_sec, tv.tv_usec);
138 _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
140 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
141 (guint64) statbuf->st_dev,
142 (guint64) statbuf->st_ino);
146 _g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
148 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
149 (guint64) statbuf->st_dev);
156 read_link (const gchar *full_name)
163 buffer = g_malloc (size);
169 read_size = readlink (full_name, buffer, size);
175 if (read_size < size)
177 buffer[read_size] = 0;
181 buffer = g_realloc (buffer, size);
191 /* Get the SELinux security context */
193 get_selinux_context (const char *path,
195 GFileAttributeMatcher *attribute_matcher,
196 gboolean follow_symlinks)
200 if (!_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT))
203 if (is_selinux_enabled ())
207 if (lgetfilecon_raw (path, &context) < 0)
212 if (getfilecon_raw (path, &context) < 0)
218 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
227 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
228 * (Mac) getxattr(..., XATTR_NOFOLLOW)
230 #ifdef HAVE_XATTR_NOFOLLOW
231 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
232 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
233 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
235 #define g_fgetxattr fgetxattr
236 #define g_flistxattr flistxattr
237 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
241 g_getxattr (const char *path, const char *name, void *value, size_t size,
242 gboolean follow_symlinks)
244 #ifdef HAVE_XATTR_NOFOLLOW
245 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
248 return getxattr (path, name, value, size);
250 return lgetxattr (path, name, value, size);
255 g_listxattr(const char *path, char *namebuf, size_t size,
256 gboolean follow_symlinks)
258 #ifdef HAVE_XATTR_NOFOLLOW
259 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
262 return listxattr (path, namebuf, size);
264 return llistxattr (path, namebuf, size);
271 return c >= 32 && c <= 126 && c != '\\';
275 name_is_valid (const char *str)
279 if (!valid_char (*str++))
286 hex_escape_string (const char *str,
287 gboolean *free_return)
290 char *escaped_str, *p;
292 static char *hex_digits = "0123456789abcdef";
298 for (i = 0; i < len; i++)
300 if (!valid_char (str[i]))
304 if (num_invalid == 0)
306 *free_return = FALSE;
310 escaped_str = g_malloc (len + num_invalid*3 + 1);
313 for (i = 0; i < len; i++)
315 if (valid_char (str[i]))
322 *p++ = hex_digits[(c >> 4) & 0xf];
323 *p++ = hex_digits[c & 0xf];
333 hex_unescape_string (const char *str,
335 gboolean *free_return)
338 char *unescaped_str, *p;
344 if (strchr (str, '\\') == NULL)
348 *free_return = FALSE;
352 unescaped_str = g_malloc (len + 1);
355 for (i = 0; i < len; i++)
357 if (str[i] == '\\' &&
362 (g_ascii_xdigit_value (str[i+2]) << 4) |
363 g_ascii_xdigit_value (str[i+3]);
373 *out_len = p - unescaped_str;
375 return unescaped_str;
379 escape_xattr (GFileInfo *info,
380 const char *gio_attr, /* gio attribute name */
381 const char *value, /* Is zero terminated */
382 size_t len /* not including zero termination */)
385 gboolean free_escaped_val;
387 escaped_val = hex_escape_string (value, &free_escaped_val);
389 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
391 if (free_escaped_val)
392 g_free (escaped_val);
396 get_one_xattr (const char *path,
398 const char *gio_attr,
400 gboolean follow_symlinks)
406 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
411 else if (len == -1 && errno == ERANGE)
413 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
418 value_p = g_malloc (len+1);
420 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
434 escape_xattr (info, gio_attr, value_p, len);
436 if (value_p != value)
440 #endif /* defined HAVE_XATTR */
443 get_xattrs (const char *path,
446 GFileAttributeMatcher *matcher,
447 gboolean follow_symlinks)
452 ssize_t list_res_size;
455 const char *attr, *attr2;
458 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
460 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
464 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
466 if (list_res_size == -1 ||
470 list_size = list_res_size;
471 list = g_malloc (list_size);
475 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
477 if (list_res_size == -1 && errno == ERANGE)
479 list_size = list_size * 2;
480 list = g_realloc (list, list_size);
484 if (list_res_size == -1)
488 while (list_res_size > 0)
490 if ((user && g_str_has_prefix (attr, "user.")) ||
491 (!user && !g_str_has_prefix (attr, "user.")))
493 char *escaped_attr, *gio_attr;
494 gboolean free_escaped_attr;
498 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
499 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
503 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
504 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
507 if (free_escaped_attr)
508 g_free (escaped_attr);
510 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
515 len = strlen (attr) + 1;
517 list_res_size -= len;
524 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
526 char *unescaped_attribute, *a;
527 gboolean free_unescaped_attribute;
529 attr2 = strchr (attr, ':');
532 attr2 += 2; /* Skip '::' */
533 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
535 a = g_strconcat ("user.", unescaped_attribute, NULL);
537 a = unescaped_attribute;
539 get_one_xattr (path, info, attr, a, follow_symlinks);
544 if (free_unescaped_attribute)
545 g_free (unescaped_attribute);
549 #endif /* defined HAVE_XATTR */
554 get_one_xattr_from_fd (int fd,
556 const char *gio_attr,
563 len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
568 else if (len == -1 && errno == ERANGE)
570 len = g_fgetxattr (fd, xattr, NULL, 0);
575 value_p = g_malloc (len + 1);
577 len = g_fgetxattr (fd, xattr, value_p, len);
591 escape_xattr (info, gio_attr, value_p, len);
593 if (value_p != value)
596 #endif /* defined HAVE_XATTR */
599 get_xattrs_from_fd (int fd,
602 GFileAttributeMatcher *matcher)
607 ssize_t list_res_size;
610 const char *attr, *attr2;
613 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
615 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
619 list_res_size = g_flistxattr (fd, NULL, 0);
621 if (list_res_size == -1 ||
625 list_size = list_res_size;
626 list = g_malloc (list_size);
630 list_res_size = g_flistxattr (fd, list, list_size);
632 if (list_res_size == -1 && errno == ERANGE)
634 list_size = list_size * 2;
635 list = g_realloc (list, list_size);
639 if (list_res_size == -1)
643 while (list_res_size > 0)
645 if ((user && g_str_has_prefix (attr, "user.")) ||
646 (!user && !g_str_has_prefix (attr, "user.")))
648 char *escaped_attr, *gio_attr;
649 gboolean free_escaped_attr;
653 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
654 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
658 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
659 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
662 if (free_escaped_attr)
663 g_free (escaped_attr);
665 get_one_xattr_from_fd (fd, info, gio_attr, attr);
668 len = strlen (attr) + 1;
670 list_res_size -= len;
677 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
679 char *unescaped_attribute, *a;
680 gboolean free_unescaped_attribute;
682 attr2 = strchr (attr, ':');
685 attr2++; /* Skip ':' */
686 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
688 a = g_strconcat ("user.", unescaped_attribute, NULL);
690 a = unescaped_attribute;
692 get_one_xattr_from_fd (fd, info, attr, a);
697 if (free_unescaped_attribute)
698 g_free (unescaped_attribute);
702 #endif /* defined HAVE_XATTR */
707 set_xattr (char *filename,
708 const char *escaped_attribute,
709 const GFileAttributeValue *attr_value,
712 char *attribute, *value;
713 gboolean free_attribute, free_value;
714 int val_len, res, errsv;
718 if (attr_value == NULL)
720 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
721 _("Attribute value must be non-NULL"));
725 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
727 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
728 _("Invalid attribute type (string expected)"));
732 if (!name_is_valid (escaped_attribute))
734 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
735 _("Invalid extended attribute name"));
739 if (g_str_has_prefix (escaped_attribute, "xattr::"))
741 escaped_attribute += strlen ("xattr::");
746 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
747 escaped_attribute += strlen ("xattr-sys::");
751 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
752 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
755 a = g_strconcat ("user.", attribute, NULL);
759 res = g_setxattr (filename, a, value, val_len);
773 g_set_error (error, G_IO_ERROR,
774 g_io_error_from_errno (errsv),
775 _("Error setting extended attribute '%s': %s"),
776 escaped_attribute, g_strerror (errsv));
787 _g_local_file_info_get_parent_info (const char *dir,
788 GFileAttributeMatcher *attribute_matcher,
789 GLocalParentFileInfo *parent_info)
791 /* Use plain struct stat for now as long as we only look at the
792 * S_ISVTX bit which doesn't exist on Win32 anyway.
797 parent_info->extra_data = NULL;
798 parent_info->free_extra_data = NULL;
799 parent_info->writable = FALSE;
800 parent_info->is_sticky = FALSE;
801 parent_info->has_trash_dir = FALSE;
802 parent_info->device = 0;
804 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME) ||
805 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE) ||
806 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH) ||
807 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT))
809 /* FIXME: Windows: The underlying _waccess() call in the C
810 * library is mostly pointless as it only looks at the READONLY
811 * FAT-style attribute of the file, it doesn't check the ACL at
814 parent_info->writable = (g_access (dir, W_OK) == 0);
816 res = g_stat (dir, &statbuf);
819 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
820 * renamed or deleted only by the owner of the file, by the owner of the directory, and
821 * by a privileged process.
826 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
828 parent_info->is_sticky = FALSE;
830 parent_info->owner = statbuf.st_uid;
831 parent_info->device = statbuf.st_dev;
832 /* No need to find trash dir if it's not writable anyway */
833 if (parent_info->writable &&
834 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
835 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
841 _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info)
843 if (parent_info->extra_data &&
844 parent_info->free_extra_data)
845 parent_info->free_extra_data (parent_info->extra_data);
849 get_access_rights (GFileAttributeMatcher *attribute_matcher,
852 GLocalFileStat *statbuf,
853 GLocalParentFileInfo *parent_info)
855 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
856 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
857 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ))
858 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ,
859 g_access (path, R_OK) == 0);
861 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
862 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE))
863 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE,
864 g_access (path, W_OK) == 0);
866 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
867 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE))
868 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE,
869 g_access (path, X_OK) == 0);
877 if (parent_info->writable)
879 if (parent_info->is_sticky)
882 uid_t uid = geteuid ();
884 if (uid == statbuf->st_uid ||
885 uid == parent_info->owner ||
894 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME))
895 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME,
898 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE))
899 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE,
902 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
903 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH,
904 writable && parent_info->has_trash_dir);
909 set_info_from_stat (GFileInfo *info,
910 GLocalFileStat *statbuf,
911 GFileAttributeMatcher *attribute_matcher)
915 file_type = G_FILE_TYPE_UNKNOWN;
917 if (S_ISREG (statbuf->st_mode))
918 file_type = G_FILE_TYPE_REGULAR;
919 else if (S_ISDIR (statbuf->st_mode))
920 file_type = G_FILE_TYPE_DIRECTORY;
922 else if (S_ISCHR (statbuf->st_mode) ||
923 S_ISBLK (statbuf->st_mode) ||
924 S_ISFIFO (statbuf->st_mode)
926 || S_ISSOCK (statbuf->st_mode)
929 file_type = G_FILE_TYPE_SPECIAL;
932 else if (S_ISLNK (statbuf->st_mode))
933 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
936 g_file_info_set_file_type (info, file_type);
937 g_file_info_set_size (info, statbuf->st_size);
939 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
941 /* Pointless setting these on Windows even if they exist in the struct */
942 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
943 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
944 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
945 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
946 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
948 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
949 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
950 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
951 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
953 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
954 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
955 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
956 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
959 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
960 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
961 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
962 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
963 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
966 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
967 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
968 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
969 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
970 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
973 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
974 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
975 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
976 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
977 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
980 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
981 G_FILE_ATTRIBUTE_ID_ETAG_VALUE))
983 char *etag = _g_local_file_info_create_etag (statbuf);
984 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ETAG_VALUE, etag);
988 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
989 G_FILE_ATTRIBUTE_ID_ID_FILE))
991 char *id = _g_local_file_info_create_file_id (statbuf);
992 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILE, id);
996 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
997 G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM))
999 char *id = _g_local_file_info_create_fs_id (statbuf);
1000 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM, id);
1008 make_valid_utf8 (const char *name)
1011 const gchar *remainder, *invalid;
1012 gint remaining_bytes, valid_bytes;
1016 remaining_bytes = strlen (name);
1018 while (remaining_bytes != 0)
1020 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1022 valid_bytes = invalid - remainder;
1025 string = g_string_sized_new (remaining_bytes);
1027 g_string_append_len (string, remainder, valid_bytes);
1028 /* append U+FFFD REPLACEMENT CHARACTER */
1029 g_string_append (string, "\357\277\275");
1031 remaining_bytes -= valid_bytes + 1;
1032 remainder = invalid + 1;
1036 return g_strdup (name);
1038 g_string_append (string, remainder);
1040 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1042 return g_string_free (string, FALSE);
1046 convert_pwd_string_to_utf8 (char *pwd_str)
1050 if (!g_utf8_validate (pwd_str, -1, NULL))
1052 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1053 if (utf8_string == NULL)
1054 utf8_string = make_valid_utf8 (pwd_str);
1057 utf8_string = g_strdup (pwd_str);
1063 uid_data_free (UidData *data)
1065 g_free (data->user_name);
1066 g_free (data->real_name);
1070 /* called with lock held */
1072 lookup_uid_data (uid_t uid)
1076 struct passwd pwbuf;
1077 struct passwd *pwbufp;
1078 char *gecos, *comma;
1080 if (uid_cache == NULL)
1081 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1083 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1088 data = g_new0 (UidData, 1);
1090 #if defined(HAVE_POSIX_GETPWUID_R)
1091 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1092 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1093 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1095 pwbufp = getpwuid (uid);
1100 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1101 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1103 gecos = pwbufp->pw_gecos;
1107 comma = strchr (gecos, ',');
1110 data->real_name = convert_pwd_string_to_utf8 (gecos);
1114 /* Default fallbacks */
1115 if (data->real_name == NULL)
1117 if (data->user_name != NULL)
1118 data->real_name = g_strdup (data->user_name);
1120 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1123 if (data->user_name == NULL)
1124 data->user_name = g_strdup_printf ("%d", (int)uid);
1126 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1132 get_username_from_uid (uid_t uid)
1138 data = lookup_uid_data (uid);
1139 res = g_strdup (data->user_name);
1140 G_UNLOCK (uid_cache);
1146 get_realname_from_uid (uid_t uid)
1152 data = lookup_uid_data (uid);
1153 res = g_strdup (data->real_name);
1154 G_UNLOCK (uid_cache);
1159 /* called with lock held */
1161 lookup_gid_name (gid_t gid)
1166 struct group *gbufp;
1168 if (gid_cache == NULL)
1169 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1171 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1176 #if defined (HAVE_POSIX_GETGRGID_R)
1177 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1178 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1179 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1181 gbufp = getgrgid (gid);
1184 if (gbufp != NULL &&
1185 gbufp->gr_name != NULL &&
1186 gbufp->gr_name[0] != 0)
1187 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1189 name = g_strdup_printf("%d", (int)gid);
1191 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1197 get_groupname_from_gid (gid_t gid)
1203 name = lookup_gid_name (gid);
1204 res = g_strdup (name);
1205 G_UNLOCK (gid_cache);
1209 #endif /* !G_OS_WIN32 */
1212 get_content_type (const char *basename,
1214 GLocalFileStat *statbuf,
1215 gboolean is_symlink,
1216 gboolean symlink_broken,
1217 GFileQueryInfoFlags flags,
1221 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1222 return g_strdup ("inode/symlink");
1223 else if (S_ISDIR(statbuf->st_mode))
1224 return g_strdup ("inode/directory");
1226 else if (S_ISCHR(statbuf->st_mode))
1227 return g_strdup ("inode/chardevice");
1228 else if (S_ISBLK(statbuf->st_mode))
1229 return g_strdup ("inode/blockdevice");
1230 else if (S_ISFIFO(statbuf->st_mode))
1231 return g_strdup ("inode/fifo");
1234 else if (S_ISSOCK(statbuf->st_mode))
1235 return g_strdup ("inode/socket");
1240 gboolean result_uncertain;
1242 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1245 if (!fast && result_uncertain && path != NULL)
1247 guchar sniff_buffer[4096];
1251 sniff_length = _g_unix_content_type_get_sniff_len ();
1252 if (sniff_length > 4096)
1253 sniff_length = 4096;
1256 fd = open (path, O_RDONLY | O_NOATIME);
1257 if (fd < 0 && errno == EPERM)
1259 fd = open (path, O_RDONLY);
1265 res = read (fd, sniff_buffer, sniff_length);
1269 g_free (content_type);
1270 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1276 return content_type;
1282 get_thumbnail_attributes (const char *path,
1285 GChecksum *checksum;
1290 uri = g_filename_to_uri (path, NULL, NULL);
1292 checksum = g_checksum_new (G_CHECKSUM_MD5);
1293 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1297 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1298 g_checksum_free (checksum);
1300 filename = g_build_filename (g_get_home_dir (),
1301 ".thumbnails", "normal", basename,
1304 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1305 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1309 filename = g_build_filename (g_get_home_dir (),
1310 ".thumbnails", "fail",
1311 "gnome-thumbnail-factory",
1315 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1316 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1324 win32_get_file_user_info (const gchar *filename,
1329 PSECURITY_DESCRIPTOR psd = NULL;
1330 DWORD sd_size = 0; /* first call calculates the size required */
1332 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1333 if ((GetFileSecurityW (wfilename,
1334 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1337 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1338 (psd = g_try_malloc (sd_size)) != NULL &&
1339 GetFileSecurityW (wfilename,
1340 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1347 SID_NAME_USE name_use = 0; /* don't care? */
1348 wchar_t *name = NULL;
1349 wchar_t *domain = NULL;
1351 DWORD domain_len = 0;
1352 /* get the user name */
1356 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1358 if (!LookupAccountSidW (NULL, /* local machine */
1361 domain, &domain_len, /* no domain info yet */
1362 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1364 name = g_try_malloc (name_len * sizeof (wchar_t));
1365 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1366 if (name && domain &&
1367 LookupAccountSidW (NULL, /* local machine */
1370 domain, &domain_len, /* no domain info yet */
1373 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1379 /* get the group name */
1383 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1385 if (!LookupAccountSidW (NULL, /* local machine */
1388 domain, &domain_len, /* no domain info yet */
1389 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1391 name = g_try_malloc (name_len * sizeof (wchar_t));
1392 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1393 if (name && domain &&
1394 LookupAccountSidW (NULL, /* local machine */
1397 domain, &domain_len, /* no domain info yet */
1400 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1406 /* TODO: get real name */
1412 #endif /* G_OS_WIN32 */
1415 _g_local_file_info_get (const char *basename,
1417 GFileAttributeMatcher *attribute_matcher,
1418 GFileQueryInfoFlags flags,
1419 GLocalParentFileInfo *parent_info,
1423 GLocalFileStat statbuf;
1425 struct stat statbuf2;
1428 gboolean is_symlink, symlink_broken;
1430 DWORD dos_attributes;
1432 char *symlink_target;
1436 info = g_file_info_new ();
1438 /* Make sure we don't set any unwanted attributes */
1439 g_file_info_set_attribute_mask (info, attribute_matcher);
1441 g_file_info_set_name (info, basename);
1443 /* Avoid stat in trivial case */
1444 if (attribute_matcher == NULL)
1448 res = g_lstat (path, &statbuf);
1451 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1456 g_object_unref (info);
1460 len = wcslen (wpath);
1461 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1464 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1467 res = _wstati64 (wpath, &statbuf);
1468 dos_attributes = GetFileAttributesW (wpath);
1477 char *display_name = g_filename_display_name (path);
1478 g_object_unref (info);
1479 g_set_error (error, G_IO_ERROR,
1480 g_io_error_from_errno (errsv),
1481 _("Error stating file '%s': %s"),
1482 display_name, g_strerror (errsv));
1483 g_free (display_name);
1488 is_symlink = S_ISLNK (statbuf.st_mode);
1492 symlink_broken = FALSE;
1496 g_file_info_set_is_symlink (info, TRUE);
1498 /* Unless NOFOLLOW was set we default to following symlinks */
1499 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1501 res = stat (path, &statbuf2);
1503 /* Report broken links as symlinks */
1507 symlink_broken = TRUE;
1512 set_info_from_stat (info, &statbuf, attribute_matcher);
1515 if (basename != NULL && basename[0] == '.')
1516 g_file_info_set_is_hidden (info, TRUE);
1518 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1519 S_ISREG (statbuf.st_mode))
1520 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1522 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1523 g_file_info_set_is_hidden (info, TRUE);
1525 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1526 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1528 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1529 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1532 symlink_target = NULL;
1536 symlink_target = read_link (path);
1537 if (symlink_target &&
1538 _g_file_attribute_matcher_matches_id (attribute_matcher,
1539 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1540 g_file_info_set_symlink_target (info, symlink_target);
1543 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1544 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1546 char *display_name = g_filename_display_basename (path);
1548 /* look for U+FFFD REPLACEMENT CHARACTER */
1549 if (strstr (display_name, "\357\277\275") != NULL)
1551 char *p = display_name;
1552 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1555 g_file_info_set_display_name (info, display_name);
1556 g_free (display_name);
1559 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1560 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1562 char *edit_name = g_filename_display_basename (path);
1563 g_file_info_set_edit_name (info, edit_name);
1568 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1569 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1571 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1573 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1577 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1578 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1579 _g_file_attribute_matcher_matches_id (attribute_matcher,
1580 G_FILE_ATTRIBUTE_ID_STANDARD_ICON))
1582 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1586 g_file_info_set_content_type (info, content_type);
1588 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1589 G_FILE_ATTRIBUTE_ID_STANDARD_ICON))
1593 if (strcmp (path, g_get_home_dir ()) == 0)
1594 icon = g_themed_icon_new ("user-home");
1595 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1596 icon = g_themed_icon_new ("user-desktop");
1597 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1598 icon = g_themed_icon_new_with_default_fallbacks ("folder-documents");
1599 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1600 icon = g_themed_icon_new_with_default_fallbacks ("folder-download");
1601 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1602 icon = g_themed_icon_new_with_default_fallbacks ("folder-music");
1603 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1604 icon = g_themed_icon_new_with_default_fallbacks ("folder-pictures");
1605 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1606 icon = g_themed_icon_new_with_default_fallbacks ("folder-publicshare");
1607 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1608 icon = g_themed_icon_new_with_default_fallbacks ("folder-templates");
1609 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1610 icon = g_themed_icon_new_with_default_fallbacks ("folder-videos");
1613 icon = g_content_type_get_icon (content_type);
1614 if (G_IS_THEMED_ICON (icon))
1616 const char *type_icon = NULL;
1618 if (S_ISDIR (statbuf.st_mode))
1619 type_icon = "folder";
1621 g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
1627 g_file_info_set_icon (info, icon);
1628 g_object_unref (icon);
1632 g_free (content_type);
1636 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1637 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1639 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1643 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1644 g_free (content_type);
1648 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1649 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1654 win32_get_file_user_info (path, NULL, &name, NULL);
1656 name = get_username_from_uid (statbuf.st_uid);
1659 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1663 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1664 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1668 win32_get_file_user_info (path, NULL, NULL, &name);
1670 name = get_realname_from_uid (statbuf.st_uid);
1673 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1677 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1678 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1682 win32_get_file_user_info (path, &name, NULL, NULL);
1684 name = get_groupname_from_gid (statbuf.st_gid);
1687 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1691 if (parent_info && parent_info->device != 0 &&
1692 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1693 statbuf.st_dev != parent_info->device)
1694 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1696 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1699 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1701 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1702 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1704 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1705 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH))
1706 get_thumbnail_attributes (path, info);
1708 vfs = g_vfs_get_default ();
1709 class = G_VFS_GET_CLASS (vfs);
1710 if (class->local_file_add_info)
1712 const char *extra_target;
1714 extra_target = path;
1715 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) &&
1718 symlink_target != NULL)
1719 extra_target = symlink_target;
1721 class->local_file_add_info (vfs,
1727 &parent_info->extra_data,
1728 &parent_info->free_extra_data);
1731 g_file_info_unset_attribute_mask (info);
1733 g_free (symlink_target);
1739 _g_local_file_info_get_from_fd (int fd,
1740 const char *attributes,
1743 GLocalFileStat stat_buf;
1744 GFileAttributeMatcher *matcher;
1748 #define FSTAT _fstati64
1753 if (FSTAT (fd, &stat_buf) == -1)
1757 g_set_error (error, G_IO_ERROR,
1758 g_io_error_from_errno (errsv),
1759 _("Error stating file descriptor: %s"),
1760 g_strerror (errsv));
1764 info = g_file_info_new ();
1766 matcher = g_file_attribute_matcher_new (attributes);
1768 /* Make sure we don't set any unwanted attributes */
1769 g_file_info_set_attribute_mask (info, matcher);
1771 set_info_from_stat (info, &stat_buf, matcher);
1774 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
1775 is_selinux_enabled ())
1778 if (fgetfilecon_raw (fd, &context) >= 0)
1780 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
1786 get_xattrs_from_fd (fd, TRUE, info, matcher);
1787 get_xattrs_from_fd (fd, FALSE, info, matcher);
1789 g_file_attribute_matcher_unref (matcher);
1791 g_file_info_unset_attribute_mask (info);
1797 get_uint32 (const GFileAttributeValue *value,
1801 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1803 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1804 _("Invalid attribute type (uint32 expected)"));
1808 *val_out = value->u.uint32;
1815 get_uint64 (const GFileAttributeValue *value,
1819 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1821 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1822 _("Invalid attribute type (uint64 expected)"));
1826 *val_out = value->u.uint64;
1832 #if defined(HAVE_SYMLINK)
1834 get_byte_string (const GFileAttributeValue *value,
1835 const char **val_out,
1838 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1840 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1841 _("Invalid attribute type (byte string expected)"));
1845 *val_out = value->u.string;
1853 get_string (const GFileAttributeValue *value,
1854 const char **val_out,
1857 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
1859 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1860 _("Invalid attribute type (byte string expected)"));
1864 *val_out = value->u.string;
1871 set_unix_mode (char *filename,
1872 GFileQueryInfoFlags flags,
1873 const GFileAttributeValue *value,
1879 if (!get_uint32 (value, &val, error))
1883 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
1885 res = lchmod (filename, val);
1887 struct stat statbuf;
1888 /* Calling chmod on a symlink changes permissions on the symlink.
1889 * We don't want to do this, so we need to check for a symlink */
1890 res = g_lstat (filename, &statbuf);
1891 if (res == 0 && S_ISLNK (statbuf.st_mode))
1893 g_set_error_literal (error, G_IO_ERROR,
1894 G_IO_ERROR_NOT_SUPPORTED,
1895 _("Cannot set permissions on symlinks"));
1899 res = g_chmod (filename, val);
1903 res = g_chmod (filename, val);
1909 g_set_error (error, G_IO_ERROR,
1910 g_io_error_from_errno (errsv),
1911 _("Error setting permissions: %s"),
1912 g_strerror (errsv));
1920 set_unix_uid_gid (char *filename,
1921 const GFileAttributeValue *uid_value,
1922 const GFileAttributeValue *gid_value,
1923 GFileQueryInfoFlags flags,
1933 if (!get_uint32 (uid_value, &val, error))
1942 if (!get_uint32 (gid_value, &val, error))
1950 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1951 res = lchown (filename, uid, gid);
1954 res = chown (filename, uid, gid);
1960 g_set_error (error, G_IO_ERROR,
1961 g_io_error_from_errno (errsv),
1962 _("Error setting owner: %s"),
1963 g_strerror (errsv));
1972 set_symlink (char *filename,
1973 const GFileAttributeValue *value,
1977 struct stat statbuf;
1979 if (!get_byte_string (value, &val, error))
1984 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1985 _("symlink must be non-NULL"));
1989 if (g_lstat (filename, &statbuf))
1993 g_set_error (error, G_IO_ERROR,
1994 g_io_error_from_errno (errsv),
1995 _("Error setting symlink: %s"),
1996 g_strerror (errsv));
2000 if (!S_ISLNK (statbuf.st_mode))
2002 g_set_error_literal (error, G_IO_ERROR,
2003 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2004 _("Error setting symlink: file is not a symlink"));
2008 if (g_unlink (filename))
2012 g_set_error (error, G_IO_ERROR,
2013 g_io_error_from_errno (errsv),
2014 _("Error setting symlink: %s"),
2015 g_strerror (errsv));
2019 if (symlink (filename, val) != 0)
2023 g_set_error (error, G_IO_ERROR,
2024 g_io_error_from_errno (errsv),
2025 _("Error setting symlink: %s"),
2026 g_strerror (errsv));
2036 lazy_stat (char *filename,
2037 struct stat *statbuf,
2038 gboolean *called_stat)
2045 res = g_stat (filename, statbuf);
2048 *called_stat = TRUE;
2055 set_mtime_atime (char *filename,
2056 const GFileAttributeValue *mtime_value,
2057 const GFileAttributeValue *mtime_usec_value,
2058 const GFileAttributeValue *atime_value,
2059 const GFileAttributeValue *atime_usec_value,
2065 struct stat statbuf;
2066 gboolean got_stat = FALSE;
2067 struct timeval times[2] = { {0, 0}, {0, 0} };
2072 if (!get_uint64 (atime_value, &val, error))
2074 times[0].tv_sec = val;
2078 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2080 times[0].tv_sec = statbuf.st_mtime;
2081 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2082 times[0].tv_usec = statbuf.st_atimensec / 1000;
2083 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2084 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2089 if (atime_usec_value)
2091 if (!get_uint32 (atime_usec_value, &val_usec, error))
2093 times[0].tv_usec = val_usec;
2099 if (!get_uint64 (mtime_value, &val, error))
2101 times[1].tv_sec = val;
2105 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2107 times[1].tv_sec = statbuf.st_mtime;
2108 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2109 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2110 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2111 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2116 if (mtime_usec_value)
2118 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2120 times[1].tv_usec = val_usec;
2123 res = utimes (filename, times);
2128 g_set_error (error, G_IO_ERROR,
2129 g_io_error_from_errno (errsv),
2130 _("Error setting modification or access time: %s"),
2131 g_strerror (errsv));
2141 set_selinux_context (char *filename,
2142 const GFileAttributeValue *value,
2147 if (!get_string (value, &val, error))
2152 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2153 _("SELinux context must be non-NULL"));
2157 if (is_selinux_enabled ()) {
2158 security_context_t val_s;
2160 val_s = g_strdup (val);
2162 if (setfilecon_raw (filename, val_s) < 0)
2166 g_set_error (error, G_IO_ERROR,
2167 g_io_error_from_errno (errsv),
2168 _("Error setting SELinux context: %s"),
2169 g_strerror (errsv));
2174 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2175 _("SELinux is not enabled on this system"));
2185 _g_local_file_info_set_attribute (char *filename,
2186 const char *attribute,
2187 GFileAttributeType type,
2189 GFileQueryInfoFlags flags,
2190 GCancellable *cancellable,
2193 GFileAttributeValue value = { 0 };
2197 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2199 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2200 return set_unix_mode (filename, flags, &value, error);
2203 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2204 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2205 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2206 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2210 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2211 return set_symlink (filename, &value, error);
2215 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2216 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2217 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2218 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2219 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2220 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2221 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2222 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2226 else if (g_str_has_prefix (attribute, "xattr::"))
2227 return set_xattr (filename, attribute, &value, error);
2228 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2229 return set_xattr (filename, attribute, &value, error);
2233 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2234 return set_selinux_context (filename, &value, error);
2237 vfs = g_vfs_get_default ();
2238 class = G_VFS_GET_CLASS (vfs);
2239 if (class->local_file_set_attributes)
2243 info = g_file_info_new ();
2244 g_file_info_set_attribute (info,
2248 if (!class->local_file_set_attributes (vfs, filename,
2253 g_object_unref (info);
2257 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2259 g_object_unref (info);
2263 g_object_unref (info);
2266 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2267 _("Setting attribute %s not supported"), attribute);
2272 _g_local_file_info_set_attributes (char *filename,
2274 GFileQueryInfoFlags flags,
2275 GCancellable *cancellable,
2278 GFileAttributeValue *value;
2280 GFileAttributeValue *uid, *gid;
2283 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2285 #if defined (HAVE_CHOWN) || defined (HAVE_UTIMES)
2286 GFileAttributeStatus status;
2292 /* Handles setting multiple specified data in a single set, and takes care
2293 of ordering restrictions when setting attributes */
2297 /* Set symlink first, since this recreates the file */
2299 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2302 if (!set_symlink (filename, value, error))
2304 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2306 /* Don't set error multiple times */
2310 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2316 /* Group uid and gid setting into one call
2317 * Change ownership before permissions, since ownership changes can
2318 change permissions (e.g. setuid)
2320 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2321 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2325 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2327 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2329 /* Don't set error multiple times */
2333 status = G_FILE_ATTRIBUTE_STATUS_SET;
2335 uid->status = status;
2337 gid->status = status;
2341 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2344 if (!set_unix_mode (filename, flags, value, error))
2346 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2348 /* Don't set error multiple times */
2352 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2357 /* Group all time settings into one call
2358 * Change times as the last thing to avoid it changing due to metadata changes
2361 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2362 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2363 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2364 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2366 if (mtime || mtime_usec || atime || atime_usec)
2368 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2370 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2372 /* Don't set error multiple times */
2376 status = G_FILE_ATTRIBUTE_STATUS_SET;
2379 mtime->status = status;
2381 mtime_usec->status = status;
2383 atime->status = status;
2385 atime_usec->status = status;
2389 /* xattrs are handled by default callback */
2392 /* SELinux context */
2394 if (is_selinux_enabled ()) {
2395 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2398 if (!set_selinux_context (filename, value, error))
2400 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2402 /* Don't set error multiple times */
2406 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2411 vfs = g_vfs_get_default ();
2412 class = G_VFS_GET_CLASS (vfs);
2413 if (class->local_file_set_attributes)
2415 if (!class->local_file_set_attributes (vfs, filename,
2421 /* Don't set error multiple times */