1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
27 #ifdef HAVE_SYS_TIME_H
30 #include <sys/types.h>
45 #include <selinux/selinux.h>
50 #if defined HAVE_SYS_XATTR_H
51 #include <sys/xattr.h>
52 #elif defined HAVE_ATTR_XATTR_H
53 #include <attr/xattr.h>
55 #error "Neither <sys/xattr.h> nor <attr/xattr.h> is present but extended attribute support is enabled."
56 #endif /* defined HAVE_SYS_XATTR_H || HAVE_ATTR_XATTR_H */
58 #endif /* HAVE_XATTR */
60 #include <glib/gstdio.h>
61 #include <gfileattribute-priv.h>
62 #include <gfileinfo-priv.h>
66 #include "glib-private.h"
80 #define X_OK 0 /* not really */
83 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
86 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
89 #define S_IXUSR _S_IEXEC
93 #include "glocalfileinfo.h"
95 #include "gthemedicon.h"
96 #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 sec = statbuf->st_mtime;
126 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
127 usec = statbuf->st_mtimensec / 1000;
128 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
129 usec = statbuf->st_mtim.tv_nsec / 1000;
134 return g_strdup_printf ("%lu:%lu", sec, 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)
794 parent_info->extra_data = NULL;
795 parent_info->free_extra_data = NULL;
796 parent_info->writable = FALSE;
797 parent_info->is_sticky = FALSE;
798 parent_info->has_trash_dir = FALSE;
799 parent_info->device = 0;
801 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME) ||
802 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE) ||
803 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH) ||
804 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT))
806 /* FIXME: Windows: The underlying _waccess() call in the C
807 * library is mostly pointless as it only looks at the READONLY
808 * FAT-style attribute of the file, it doesn't check the ACL at
811 parent_info->writable = (g_access (dir, W_OK) == 0);
813 res = g_stat (dir, &statbuf);
816 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
817 * renamed or deleted only by the owner of the file, by the owner of the directory, and
818 * by a privileged process.
823 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
825 parent_info->is_sticky = FALSE;
827 parent_info->owner = statbuf.st_uid;
828 parent_info->device = statbuf.st_dev;
829 /* No need to find trash dir if it's not writable anyway */
830 if (parent_info->writable &&
831 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
832 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
838 _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info)
840 if (parent_info->extra_data &&
841 parent_info->free_extra_data)
842 parent_info->free_extra_data (parent_info->extra_data);
846 get_access_rights (GFileAttributeMatcher *attribute_matcher,
849 GLocalFileStat *statbuf,
850 GLocalParentFileInfo *parent_info)
852 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
853 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
854 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ))
855 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ,
856 g_access (path, R_OK) == 0);
858 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
859 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE))
860 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE,
861 g_access (path, W_OK) == 0);
863 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
864 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE))
865 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE,
866 g_access (path, X_OK) == 0);
874 if (parent_info->writable)
876 if (parent_info->is_sticky)
879 uid_t uid = geteuid ();
881 if (uid == statbuf->st_uid ||
882 uid == parent_info->owner ||
891 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME))
892 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME,
895 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE))
896 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE,
899 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
900 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH,
901 writable && parent_info->has_trash_dir);
906 set_info_from_stat (GFileInfo *info,
907 GLocalFileStat *statbuf,
908 GFileAttributeMatcher *attribute_matcher)
912 file_type = G_FILE_TYPE_UNKNOWN;
914 if (S_ISREG (statbuf->st_mode))
915 file_type = G_FILE_TYPE_REGULAR;
916 else if (S_ISDIR (statbuf->st_mode))
917 file_type = G_FILE_TYPE_DIRECTORY;
919 else if (S_ISCHR (statbuf->st_mode) ||
920 S_ISBLK (statbuf->st_mode) ||
921 S_ISFIFO (statbuf->st_mode)
923 || S_ISSOCK (statbuf->st_mode)
926 file_type = G_FILE_TYPE_SPECIAL;
929 else if (S_ISLNK (statbuf->st_mode))
930 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
933 g_file_info_set_file_type (info, file_type);
934 g_file_info_set_size (info, statbuf->st_size);
936 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
938 /* Pointless setting these on Windows even if they exist in the struct */
939 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
940 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
941 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
942 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
943 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
945 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
946 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
947 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
948 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
950 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
951 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
952 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
953 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
956 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
957 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
958 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
959 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
960 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
963 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
964 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
965 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
966 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
967 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
970 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
971 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
972 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
973 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
974 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
977 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
978 G_FILE_ATTRIBUTE_ID_ETAG_VALUE))
980 char *etag = _g_local_file_info_create_etag (statbuf);
981 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ETAG_VALUE, etag);
985 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
986 G_FILE_ATTRIBUTE_ID_ID_FILE))
988 char *id = _g_local_file_info_create_file_id (statbuf);
989 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILE, id);
993 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
994 G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM))
996 char *id = _g_local_file_info_create_fs_id (statbuf);
997 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM, id);
1005 make_valid_utf8 (const char *name)
1008 const gchar *remainder, *invalid;
1009 gint remaining_bytes, valid_bytes;
1013 remaining_bytes = strlen (name);
1015 while (remaining_bytes != 0)
1017 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1019 valid_bytes = invalid - remainder;
1022 string = g_string_sized_new (remaining_bytes);
1024 g_string_append_len (string, remainder, valid_bytes);
1025 /* append U+FFFD REPLACEMENT CHARACTER */
1026 g_string_append (string, "\357\277\275");
1028 remaining_bytes -= valid_bytes + 1;
1029 remainder = invalid + 1;
1033 return g_strdup (name);
1035 g_string_append (string, remainder);
1037 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1039 return g_string_free (string, FALSE);
1043 convert_pwd_string_to_utf8 (char *pwd_str)
1047 if (!g_utf8_validate (pwd_str, -1, NULL))
1049 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1050 if (utf8_string == NULL)
1051 utf8_string = make_valid_utf8 (pwd_str);
1054 utf8_string = g_strdup (pwd_str);
1060 uid_data_free (UidData *data)
1062 g_free (data->user_name);
1063 g_free (data->real_name);
1067 /* called with lock held */
1069 lookup_uid_data (uid_t uid)
1073 struct passwd pwbuf;
1074 struct passwd *pwbufp;
1075 char *gecos, *comma;
1077 if (uid_cache == NULL)
1078 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1080 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1085 data = g_new0 (UidData, 1);
1087 #if defined(HAVE_POSIX_GETPWUID_R)
1088 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1089 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1090 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1092 pwbufp = getpwuid (uid);
1097 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1098 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1100 gecos = pwbufp->pw_gecos;
1104 comma = strchr (gecos, ',');
1107 data->real_name = convert_pwd_string_to_utf8 (gecos);
1111 /* Default fallbacks */
1112 if (data->real_name == NULL)
1114 if (data->user_name != NULL)
1115 data->real_name = g_strdup (data->user_name);
1117 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1120 if (data->user_name == NULL)
1121 data->user_name = g_strdup_printf ("%d", (int)uid);
1123 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1129 get_username_from_uid (uid_t uid)
1135 data = lookup_uid_data (uid);
1136 res = g_strdup (data->user_name);
1137 G_UNLOCK (uid_cache);
1143 get_realname_from_uid (uid_t uid)
1149 data = lookup_uid_data (uid);
1150 res = g_strdup (data->real_name);
1151 G_UNLOCK (uid_cache);
1156 /* called with lock held */
1158 lookup_gid_name (gid_t gid)
1163 struct group *gbufp;
1165 if (gid_cache == NULL)
1166 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1168 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1173 #if defined (HAVE_POSIX_GETGRGID_R)
1174 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1175 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1176 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1178 gbufp = getgrgid (gid);
1181 if (gbufp != NULL &&
1182 gbufp->gr_name != NULL &&
1183 gbufp->gr_name[0] != 0)
1184 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1186 name = g_strdup_printf("%d", (int)gid);
1188 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1194 get_groupname_from_gid (gid_t gid)
1200 name = lookup_gid_name (gid);
1201 res = g_strdup (name);
1202 G_UNLOCK (gid_cache);
1206 #endif /* !G_OS_WIN32 */
1209 get_content_type (const char *basename,
1211 GLocalFileStat *statbuf,
1212 gboolean is_symlink,
1213 gboolean symlink_broken,
1214 GFileQueryInfoFlags flags,
1218 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1219 return g_strdup ("inode/symlink");
1220 else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
1221 return g_strdup ("inode/directory");
1223 else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
1224 return g_strdup ("inode/chardevice");
1225 else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
1226 return g_strdup ("inode/blockdevice");
1227 else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
1228 return g_strdup ("inode/fifo");
1231 else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
1232 return g_strdup ("inode/socket");
1237 gboolean result_uncertain;
1239 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1242 if (!fast && result_uncertain && path != NULL)
1244 guchar sniff_buffer[4096];
1248 sniff_length = _g_unix_content_type_get_sniff_len ();
1249 if (sniff_length > 4096)
1250 sniff_length = 4096;
1253 fd = g_open (path, O_RDONLY | O_NOATIME, 0);
1254 if (fd < 0 && errno == EPERM)
1256 fd = g_open (path, O_RDONLY, 0);
1262 res = read (fd, sniff_buffer, sniff_length);
1266 g_free (content_type);
1267 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1273 return content_type;
1279 get_thumbnail_attributes (const char *path,
1282 GChecksum *checksum;
1287 uri = g_filename_to_uri (path, NULL, NULL);
1289 checksum = g_checksum_new (G_CHECKSUM_MD5);
1290 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1294 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1295 g_checksum_free (checksum);
1297 filename = g_build_filename (g_get_user_cache_dir (),
1298 "thumbnails", "large", basename,
1301 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1302 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1306 filename = g_build_filename (g_get_user_cache_dir (),
1307 "thumbnails", "normal", basename,
1310 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1311 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1315 filename = g_build_filename (g_get_user_cache_dir (),
1316 "thumbnails", "fail",
1317 "gnome-thumbnail-factory",
1321 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1322 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1331 win32_get_file_user_info (const gchar *filename,
1336 PSECURITY_DESCRIPTOR psd = NULL;
1337 DWORD sd_size = 0; /* first call calculates the size required */
1339 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1340 if ((GetFileSecurityW (wfilename,
1341 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1344 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1345 (psd = g_try_malloc (sd_size)) != NULL &&
1346 GetFileSecurityW (wfilename,
1347 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1354 SID_NAME_USE name_use = 0; /* don't care? */
1355 wchar_t *name = NULL;
1356 wchar_t *domain = NULL;
1358 DWORD domain_len = 0;
1359 /* get the user name */
1363 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1365 if (!LookupAccountSidW (NULL, /* local machine */
1368 domain, &domain_len, /* no domain info yet */
1369 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1371 name = g_try_malloc (name_len * sizeof (wchar_t));
1372 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1373 if (name && domain &&
1374 LookupAccountSidW (NULL, /* local machine */
1377 domain, &domain_len, /* no domain info yet */
1380 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1386 /* get the group name */
1390 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1392 if (!LookupAccountSidW (NULL, /* local machine */
1395 domain, &domain_len, /* no domain info yet */
1396 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1398 name = g_try_malloc (name_len * sizeof (wchar_t));
1399 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1400 if (name && domain &&
1401 LookupAccountSidW (NULL, /* local machine */
1404 domain, &domain_len, /* no domain info yet */
1407 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1413 /* TODO: get real name */
1419 #endif /* G_OS_WIN32 */
1422 /* support for '.hidden' files */
1423 G_LOCK_DEFINE_STATIC (hidden_cache);
1424 static GHashTable *hidden_cache;
1427 remove_from_hidden_cache (gpointer user_data)
1429 G_LOCK (hidden_cache);
1430 g_hash_table_remove (hidden_cache, user_data);
1431 G_UNLOCK (hidden_cache);
1437 read_hidden_file (const gchar *dirname)
1442 filename = g_build_path ("/", dirname, ".hidden", NULL);
1443 hidden = fopen (filename, "r");
1448 gchar buffer[PATH_MAX + 2]; /* \n\0 */
1451 table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1453 while (fgets (buffer, sizeof buffer, hidden))
1457 if ((newline = strchr (buffer, '\n')) != NULL)
1461 g_hash_table_insert (table,
1462 g_memdup (buffer, newline - buffer),
1463 GINT_TO_POINTER (TRUE));
1476 maybe_unref_hash_table (gpointer data)
1479 g_hash_table_unref (data);
1483 file_is_hidden (const gchar *path,
1484 const gchar *basename)
1490 dirname = g_path_get_dirname (path);
1492 G_LOCK (hidden_cache);
1494 if G_UNLIKELY (hidden_cache == NULL)
1495 hidden_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1496 g_free, maybe_unref_hash_table);
1498 if (!g_hash_table_lookup_extended (hidden_cache, dirname,
1502 GSource *remove_from_cache_source;
1504 g_hash_table_insert (hidden_cache,
1505 mydirname = g_strdup (dirname),
1506 table = read_hidden_file (dirname));
1508 remove_from_cache_source = g_timeout_source_new_seconds (5);
1509 g_source_set_priority (remove_from_cache_source, G_PRIORITY_DEFAULT);
1510 g_source_set_callback (remove_from_cache_source,
1511 remove_from_hidden_cache,
1514 g_source_attach (remove_from_cache_source,
1515 GLIB_PRIVATE_CALL (g_get_worker_context) ());
1516 g_source_unref (remove_from_cache_source);
1519 result = table != NULL &&
1520 GPOINTER_TO_INT (g_hash_table_lookup (table, basename));
1522 G_UNLOCK (hidden_cache);
1528 #endif /* !G_OS_WIN32 */
1531 _g_local_file_info_get_nostat (GFileInfo *info,
1532 const char *basename,
1534 GFileAttributeMatcher *attribute_matcher)
1536 g_file_info_set_name (info, basename);
1538 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1539 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1541 char *display_name = g_filename_display_basename (path);
1543 /* look for U+FFFD REPLACEMENT CHARACTER */
1544 if (strstr (display_name, "\357\277\275") != NULL)
1546 char *p = display_name;
1547 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1550 g_file_info_set_display_name (info, display_name);
1551 g_free (display_name);
1554 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1555 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1557 char *edit_name = g_filename_display_basename (path);
1558 g_file_info_set_edit_name (info, edit_name);
1563 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1564 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1566 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1568 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1574 get_icon_name (const char *path,
1575 gboolean use_symbolic,
1576 gboolean *with_fallbacks_out)
1578 const char *name = NULL;
1579 gboolean with_fallbacks = TRUE;
1581 if (strcmp (path, g_get_home_dir ()) == 0)
1583 name = use_symbolic ? "user-home-symbolic" : "user-home";
1584 with_fallbacks = FALSE;
1586 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1588 name = use_symbolic ? "user-desktop-symbolic" : "user-desktop";
1589 with_fallbacks = FALSE;
1591 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1593 name = use_symbolic ? "folder-documents-symbolic" : "folder-documents";
1595 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1597 name = use_symbolic ? "folder-download-symbolic" : "folder-download";
1599 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1601 name = use_symbolic ? "folder-music-symbolic" : "folder-music";
1603 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1605 name = use_symbolic ? "folder-pictures-symbolic" : "folder-pictures";
1607 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1609 name = use_symbolic ? "folder-publicshare-symbolic" : "folder-publicshare";
1611 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1613 name = use_symbolic ? "folder-templates-symbolic" : "folder-templates";
1615 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1617 name = use_symbolic ? "folder-videos-symbolic" : "folder-videos";
1624 if (with_fallbacks_out != NULL)
1625 *with_fallbacks_out = with_fallbacks;
1631 get_icon (const char *path,
1632 const char *content_type,
1634 gboolean use_symbolic)
1637 const char *icon_name;
1638 gboolean with_fallbacks;
1640 icon_name = get_icon_name (path, use_symbolic, &with_fallbacks);
1641 if (icon_name != NULL)
1644 icon = g_themed_icon_new_with_default_fallbacks (icon_name);
1646 icon = g_themed_icon_new (icon_name);
1651 icon = g_content_type_get_symbolic_icon (content_type);
1653 icon = g_content_type_get_icon (content_type);
1655 if (G_IS_THEMED_ICON (icon) && is_folder)
1657 g_themed_icon_append_name (G_THEMED_ICON (icon), use_symbolic ? "folder-symbolic" : "folder");
1665 _g_local_file_info_get (const char *basename,
1667 GFileAttributeMatcher *attribute_matcher,
1668 GFileQueryInfoFlags flags,
1669 GLocalParentFileInfo *parent_info,
1673 GLocalFileStat statbuf;
1675 struct stat statbuf2;
1679 gboolean is_symlink, symlink_broken;
1681 DWORD dos_attributes;
1683 char *symlink_target;
1688 info = g_file_info_new ();
1690 /* Make sure we don't set any unwanted attributes */
1691 g_file_info_set_attribute_mask (info, attribute_matcher);
1693 _g_local_file_info_get_nostat (info, basename, path, attribute_matcher);
1695 if (attribute_matcher == NULL)
1697 g_file_info_unset_attribute_mask (info);
1702 res = g_lstat (path, &statbuf);
1705 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1710 g_object_unref (info);
1714 len = wcslen (wpath);
1715 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1718 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1721 res = _wstati64 (wpath, &statbuf);
1722 dos_attributes = GetFileAttributesW (wpath);
1732 /* Don't bail out if we get Permission denied (SELinux?) */
1733 if (errsv != EACCES)
1735 char *display_name = g_filename_display_name (path);
1736 g_object_unref (info);
1737 g_set_error (error, G_IO_ERROR,
1738 g_io_error_from_errno (errsv),
1739 _("Error when getting information for file '%s': %s"),
1740 display_name, g_strerror (errsv));
1741 g_free (display_name);
1746 /* Even if stat() fails, try to get as much as other attributes possible */
1747 stat_ok = res != -1;
1750 device = statbuf.st_dev;
1755 is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
1759 symlink_broken = FALSE;
1763 g_file_info_set_is_symlink (info, TRUE);
1765 /* Unless NOFOLLOW was set we default to following symlinks */
1766 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1768 res = stat (path, &statbuf2);
1770 /* Report broken links as symlinks */
1777 symlink_broken = TRUE;
1783 set_info_from_stat (info, &statbuf, attribute_matcher);
1786 if (stat_ok && _g_local_file_is_lost_found_dir (path, statbuf.st_dev))
1787 g_file_info_set_is_hidden (info, TRUE);
1791 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1792 G_FILE_ATTRIBUTE_ID_STANDARD_IS_HIDDEN))
1794 if (basename != NULL &&
1795 (basename[0] == '.' ||
1796 file_is_hidden (path, basename)))
1797 g_file_info_set_is_hidden (info, TRUE);
1800 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1801 (stat_ok && S_ISREG (statbuf.st_mode)))
1802 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1804 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1805 g_file_info_set_is_hidden (info, TRUE);
1807 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1808 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1810 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1811 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1814 symlink_target = NULL;
1818 symlink_target = read_link (path);
1819 if (symlink_target &&
1820 _g_file_attribute_matcher_matches_id (attribute_matcher,
1821 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1822 g_file_info_set_symlink_target (info, symlink_target);
1825 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1826 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1827 _g_file_attribute_matcher_matches_id (attribute_matcher,
1828 G_FILE_ATTRIBUTE_ID_STANDARD_ICON) ||
1829 _g_file_attribute_matcher_matches_id (attribute_matcher,
1830 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1832 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
1836 g_file_info_set_content_type (info, content_type);
1838 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1839 G_FILE_ATTRIBUTE_ID_STANDARD_ICON)
1840 || _g_file_attribute_matcher_matches_id (attribute_matcher,
1841 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1845 /* non symbolic icon */
1846 icon = get_icon (path, content_type, S_ISDIR (statbuf.st_mode), FALSE);
1849 g_file_info_set_icon (info, icon);
1850 g_object_unref (icon);
1854 icon = get_icon (path, content_type, S_ISDIR (statbuf.st_mode), TRUE);
1857 g_file_info_set_symbolic_icon (info, icon);
1858 g_object_unref (icon);
1863 g_free (content_type);
1867 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1868 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1870 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
1874 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1875 g_free (content_type);
1879 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1880 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1885 win32_get_file_user_info (path, NULL, &name, NULL);
1888 name = get_username_from_uid (statbuf.st_uid);
1891 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1895 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1896 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1900 win32_get_file_user_info (path, NULL, NULL, &name);
1903 name = get_realname_from_uid (statbuf.st_uid);
1906 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1910 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1911 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1915 win32_get_file_user_info (path, &name, NULL, NULL);
1918 name = get_groupname_from_gid (statbuf.st_gid);
1921 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1925 if (stat_ok && parent_info && parent_info->device != 0 &&
1926 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1927 statbuf.st_dev != parent_info->device)
1928 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1931 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1934 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1936 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1937 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1939 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1940 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH))
1941 get_thumbnail_attributes (path, info);
1943 vfs = g_vfs_get_default ();
1944 class = G_VFS_GET_CLASS (vfs);
1945 if (class->local_file_add_info)
1947 class->local_file_add_info (vfs,
1953 &parent_info->extra_data,
1954 &parent_info->free_extra_data);
1957 g_file_info_unset_attribute_mask (info);
1959 g_free (symlink_target);
1965 _g_local_file_info_get_from_fd (int fd,
1966 const char *attributes,
1969 GLocalFileStat stat_buf;
1970 GFileAttributeMatcher *matcher;
1974 #define FSTAT _fstati64
1979 if (FSTAT (fd, &stat_buf) == -1)
1983 g_set_error (error, G_IO_ERROR,
1984 g_io_error_from_errno (errsv),
1985 _("Error when getting information for file descriptor: %s"),
1986 g_strerror (errsv));
1990 info = g_file_info_new ();
1992 matcher = g_file_attribute_matcher_new (attributes);
1994 /* Make sure we don't set any unwanted attributes */
1995 g_file_info_set_attribute_mask (info, matcher);
1997 set_info_from_stat (info, &stat_buf, matcher);
2000 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
2001 is_selinux_enabled ())
2004 if (fgetfilecon_raw (fd, &context) >= 0)
2006 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
2012 get_xattrs_from_fd (fd, TRUE, info, matcher);
2013 get_xattrs_from_fd (fd, FALSE, info, matcher);
2015 g_file_attribute_matcher_unref (matcher);
2017 g_file_info_unset_attribute_mask (info);
2023 get_uint32 (const GFileAttributeValue *value,
2027 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
2029 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2030 _("Invalid attribute type (uint32 expected)"));
2034 *val_out = value->u.uint32;
2041 get_uint64 (const GFileAttributeValue *value,
2045 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
2047 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2048 _("Invalid attribute type (uint64 expected)"));
2052 *val_out = value->u.uint64;
2058 #if defined(HAVE_SYMLINK)
2060 get_byte_string (const GFileAttributeValue *value,
2061 const char **val_out,
2064 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
2066 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2067 _("Invalid attribute type (byte string expected)"));
2071 *val_out = value->u.string;
2079 get_string (const GFileAttributeValue *value,
2080 const char **val_out,
2083 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
2085 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2086 _("Invalid attribute type (byte string expected)"));
2090 *val_out = value->u.string;
2097 set_unix_mode (char *filename,
2098 GFileQueryInfoFlags flags,
2099 const GFileAttributeValue *value,
2105 if (!get_uint32 (value, &val, error))
2109 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
2111 res = lchmod (filename, val);
2113 struct stat statbuf;
2114 /* Calling chmod on a symlink changes permissions on the symlink.
2115 * We don't want to do this, so we need to check for a symlink */
2116 res = g_lstat (filename, &statbuf);
2117 if (res == 0 && S_ISLNK (statbuf.st_mode))
2119 g_set_error_literal (error, G_IO_ERROR,
2120 G_IO_ERROR_NOT_SUPPORTED,
2121 _("Cannot set permissions on symlinks"));
2125 res = g_chmod (filename, val);
2129 res = g_chmod (filename, val);
2135 g_set_error (error, G_IO_ERROR,
2136 g_io_error_from_errno (errsv),
2137 _("Error setting permissions: %s"),
2138 g_strerror (errsv));
2146 set_unix_uid_gid (char *filename,
2147 const GFileAttributeValue *uid_value,
2148 const GFileAttributeValue *gid_value,
2149 GFileQueryInfoFlags flags,
2159 if (!get_uint32 (uid_value, &val, error))
2168 if (!get_uint32 (gid_value, &val, error))
2176 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
2177 res = lchown (filename, uid, gid);
2180 res = chown (filename, uid, gid);
2186 g_set_error (error, G_IO_ERROR,
2187 g_io_error_from_errno (errsv),
2188 _("Error setting owner: %s"),
2189 g_strerror (errsv));
2198 set_symlink (char *filename,
2199 const GFileAttributeValue *value,
2203 struct stat statbuf;
2205 if (!get_byte_string (value, &val, error))
2210 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2211 _("symlink must be non-NULL"));
2215 if (g_lstat (filename, &statbuf))
2219 g_set_error (error, G_IO_ERROR,
2220 g_io_error_from_errno (errsv),
2221 _("Error setting symlink: %s"),
2222 g_strerror (errsv));
2226 if (!S_ISLNK (statbuf.st_mode))
2228 g_set_error_literal (error, G_IO_ERROR,
2229 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2230 _("Error setting symlink: file is not a symlink"));
2234 if (g_unlink (filename))
2238 g_set_error (error, G_IO_ERROR,
2239 g_io_error_from_errno (errsv),
2240 _("Error setting symlink: %s"),
2241 g_strerror (errsv));
2245 if (symlink (filename, val) != 0)
2249 g_set_error (error, G_IO_ERROR,
2250 g_io_error_from_errno (errsv),
2251 _("Error setting symlink: %s"),
2252 g_strerror (errsv));
2262 lazy_stat (char *filename,
2263 struct stat *statbuf,
2264 gboolean *called_stat)
2271 res = g_stat (filename, statbuf);
2274 *called_stat = TRUE;
2281 set_mtime_atime (char *filename,
2282 const GFileAttributeValue *mtime_value,
2283 const GFileAttributeValue *mtime_usec_value,
2284 const GFileAttributeValue *atime_value,
2285 const GFileAttributeValue *atime_usec_value,
2290 guint32 val_usec = 0;
2291 struct stat statbuf;
2292 gboolean got_stat = FALSE;
2293 struct timeval times[2] = { {0, 0}, {0, 0} };
2298 if (!get_uint64 (atime_value, &val, error))
2300 times[0].tv_sec = val;
2304 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2306 times[0].tv_sec = statbuf.st_mtime;
2307 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2308 times[0].tv_usec = statbuf.st_atimensec / 1000;
2309 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2310 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2315 if (atime_usec_value)
2317 if (!get_uint32 (atime_usec_value, &val_usec, error))
2319 times[0].tv_usec = val_usec;
2325 if (!get_uint64 (mtime_value, &val, error))
2327 times[1].tv_sec = val;
2331 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2333 times[1].tv_sec = statbuf.st_mtime;
2334 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2335 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2336 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2337 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2342 if (mtime_usec_value)
2344 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2346 times[1].tv_usec = val_usec;
2349 res = utimes (filename, times);
2354 g_set_error (error, G_IO_ERROR,
2355 g_io_error_from_errno (errsv),
2356 _("Error setting modification or access time: %s"),
2357 g_strerror (errsv));
2367 set_selinux_context (char *filename,
2368 const GFileAttributeValue *value,
2373 if (!get_string (value, &val, error))
2378 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2379 _("SELinux context must be non-NULL"));
2383 if (is_selinux_enabled ()) {
2384 security_context_t val_s;
2386 val_s = g_strdup (val);
2388 if (setfilecon_raw (filename, val_s) < 0)
2392 g_set_error (error, G_IO_ERROR,
2393 g_io_error_from_errno (errsv),
2394 _("Error setting SELinux context: %s"),
2395 g_strerror (errsv));
2400 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2401 _("SELinux is not enabled on this system"));
2411 _g_local_file_info_set_attribute (char *filename,
2412 const char *attribute,
2413 GFileAttributeType type,
2415 GFileQueryInfoFlags flags,
2416 GCancellable *cancellable,
2419 GFileAttributeValue value = { 0 };
2423 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2425 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2426 return set_unix_mode (filename, flags, &value, error);
2429 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2430 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2431 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2432 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2436 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2437 return set_symlink (filename, &value, error);
2441 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2442 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2443 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2444 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2445 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2446 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2447 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2448 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2452 else if (g_str_has_prefix (attribute, "xattr::"))
2453 return set_xattr (filename, attribute, &value, error);
2454 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2455 return set_xattr (filename, attribute, &value, error);
2459 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2460 return set_selinux_context (filename, &value, error);
2463 vfs = g_vfs_get_default ();
2464 class = G_VFS_GET_CLASS (vfs);
2465 if (class->local_file_set_attributes)
2469 info = g_file_info_new ();
2470 g_file_info_set_attribute (info,
2474 if (!class->local_file_set_attributes (vfs, filename,
2479 g_object_unref (info);
2483 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2485 g_object_unref (info);
2489 g_object_unref (info);
2492 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2493 _("Setting attribute %s not supported"), attribute);
2498 _g_local_file_info_set_attributes (char *filename,
2500 GFileQueryInfoFlags flags,
2501 GCancellable *cancellable,
2504 GFileAttributeValue *value;
2506 GFileAttributeValue *uid, *gid;
2509 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2511 #if defined (HAVE_CHOWN) || defined (HAVE_UTIMES)
2512 GFileAttributeStatus status;
2518 /* Handles setting multiple specified data in a single set, and takes care
2519 of ordering restrictions when setting attributes */
2523 /* Set symlink first, since this recreates the file */
2525 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2528 if (!set_symlink (filename, value, error))
2530 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2532 /* Don't set error multiple times */
2536 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2542 /* Group uid and gid setting into one call
2543 * Change ownership before permissions, since ownership changes can
2544 change permissions (e.g. setuid)
2546 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2547 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2551 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2553 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2555 /* Don't set error multiple times */
2559 status = G_FILE_ATTRIBUTE_STATUS_SET;
2561 uid->status = status;
2563 gid->status = status;
2567 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2570 if (!set_unix_mode (filename, flags, value, error))
2572 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2574 /* Don't set error multiple times */
2578 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2583 /* Group all time settings into one call
2584 * Change times as the last thing to avoid it changing due to metadata changes
2587 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2588 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2589 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2590 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2592 if (mtime || mtime_usec || atime || atime_usec)
2594 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2596 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2598 /* Don't set error multiple times */
2602 status = G_FILE_ATTRIBUTE_STATUS_SET;
2605 mtime->status = status;
2607 mtime_usec->status = status;
2609 atime->status = status;
2611 atime_usec->status = status;
2615 /* xattrs are handled by default callback */
2618 /* SELinux context */
2620 if (is_selinux_enabled ()) {
2621 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2624 if (!set_selinux_context (filename, value, error))
2626 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2628 /* Don't set error multiple times */
2632 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2637 vfs = g_vfs_get_default ();
2638 class = G_VFS_GET_CLASS (vfs);
2639 if (class->local_file_set_attributes)
2641 if (!class->local_file_set_attributes (vfs, filename,
2647 /* Don't set error multiple times */