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-unix.h"
67 #include "glib-private.h"
71 #include "thumbnail-verify.h"
83 #define X_OK 0 /* not really */
86 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
89 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
92 #define S_IXUSR _S_IEXEC
96 #include "glocalfileinfo.h"
98 #include "gthemedicon.h"
99 #include "gcontenttypeprivate.h"
102 struct ThumbMD5Context {
105 unsigned char in[64];
115 G_LOCK_DEFINE_STATIC (uid_cache);
116 static GHashTable *uid_cache = NULL;
118 G_LOCK_DEFINE_STATIC (gid_cache);
119 static GHashTable *gid_cache = NULL;
121 #endif /* !G_OS_WIN32 */
124 _g_local_file_info_create_etag (GLocalFileStat *statbuf)
128 sec = statbuf->st_mtime;
129 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
130 usec = statbuf->st_mtimensec / 1000;
131 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
132 usec = statbuf->st_mtim.tv_nsec / 1000;
137 return g_strdup_printf ("%lu:%lu", sec, usec);
141 _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
143 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
144 (guint64) statbuf->st_dev,
145 (guint64) statbuf->st_ino);
149 _g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
151 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
152 (guint64) statbuf->st_dev);
159 read_link (const gchar *full_name)
166 buffer = g_malloc (size);
172 read_size = readlink (full_name, buffer, size);
178 if (read_size < size)
180 buffer[read_size] = 0;
184 buffer = g_realloc (buffer, size);
194 /* Get the SELinux security context */
196 get_selinux_context (const char *path,
198 GFileAttributeMatcher *attribute_matcher,
199 gboolean follow_symlinks)
203 if (!_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT))
206 if (is_selinux_enabled ())
210 if (lgetfilecon_raw (path, &context) < 0)
215 if (getfilecon_raw (path, &context) < 0)
221 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
230 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
231 * (Mac) getxattr(..., XATTR_NOFOLLOW)
233 #ifdef HAVE_XATTR_NOFOLLOW
234 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
235 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
236 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
238 #define g_fgetxattr fgetxattr
239 #define g_flistxattr flistxattr
240 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
244 g_getxattr (const char *path, const char *name, void *value, size_t size,
245 gboolean follow_symlinks)
247 #ifdef HAVE_XATTR_NOFOLLOW
248 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
251 return getxattr (path, name, value, size);
253 return lgetxattr (path, name, value, size);
258 g_listxattr(const char *path, char *namebuf, size_t size,
259 gboolean follow_symlinks)
261 #ifdef HAVE_XATTR_NOFOLLOW
262 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
265 return listxattr (path, namebuf, size);
267 return llistxattr (path, namebuf, size);
274 return c >= 32 && c <= 126 && c != '\\';
278 name_is_valid (const char *str)
282 if (!valid_char (*str++))
289 hex_escape_string (const char *str,
290 gboolean *free_return)
293 char *escaped_str, *p;
295 static char *hex_digits = "0123456789abcdef";
301 for (i = 0; i < len; i++)
303 if (!valid_char (str[i]))
307 if (num_invalid == 0)
309 *free_return = FALSE;
313 escaped_str = g_malloc (len + num_invalid*3 + 1);
316 for (i = 0; i < len; i++)
318 if (valid_char (str[i]))
325 *p++ = hex_digits[(c >> 4) & 0xf];
326 *p++ = hex_digits[c & 0xf];
336 hex_unescape_string (const char *str,
338 gboolean *free_return)
341 char *unescaped_str, *p;
347 if (strchr (str, '\\') == NULL)
351 *free_return = FALSE;
355 unescaped_str = g_malloc (len + 1);
358 for (i = 0; i < len; i++)
360 if (str[i] == '\\' &&
365 (g_ascii_xdigit_value (str[i+2]) << 4) |
366 g_ascii_xdigit_value (str[i+3]);
376 *out_len = p - unescaped_str;
378 return unescaped_str;
382 escape_xattr (GFileInfo *info,
383 const char *gio_attr, /* gio attribute name */
384 const char *value, /* Is zero terminated */
385 size_t len /* not including zero termination */)
388 gboolean free_escaped_val;
390 escaped_val = hex_escape_string (value, &free_escaped_val);
392 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
394 if (free_escaped_val)
395 g_free (escaped_val);
399 get_one_xattr (const char *path,
401 const char *gio_attr,
403 gboolean follow_symlinks)
409 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
414 else if (len == -1 && errno == ERANGE)
416 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
421 value_p = g_malloc (len+1);
423 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
437 escape_xattr (info, gio_attr, value_p, len);
439 if (value_p != value)
443 #endif /* defined HAVE_XATTR */
446 get_xattrs (const char *path,
449 GFileAttributeMatcher *matcher,
450 gboolean follow_symlinks)
455 ssize_t list_res_size;
458 const char *attr, *attr2;
461 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
463 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
467 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
469 if (list_res_size == -1 ||
473 list_size = list_res_size;
474 list = g_malloc (list_size);
478 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
480 if (list_res_size == -1 && errno == ERANGE)
482 list_size = list_size * 2;
483 list = g_realloc (list, list_size);
487 if (list_res_size == -1)
491 while (list_res_size > 0)
493 if ((user && g_str_has_prefix (attr, "user.")) ||
494 (!user && !g_str_has_prefix (attr, "user.")))
496 char *escaped_attr, *gio_attr;
497 gboolean free_escaped_attr;
501 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
502 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
506 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
507 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
510 if (free_escaped_attr)
511 g_free (escaped_attr);
513 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
518 len = strlen (attr) + 1;
520 list_res_size -= len;
527 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
529 char *unescaped_attribute, *a;
530 gboolean free_unescaped_attribute;
532 attr2 = strchr (attr, ':');
535 attr2 += 2; /* Skip '::' */
536 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
538 a = g_strconcat ("user.", unescaped_attribute, NULL);
540 a = unescaped_attribute;
542 get_one_xattr (path, info, attr, a, follow_symlinks);
547 if (free_unescaped_attribute)
548 g_free (unescaped_attribute);
552 #endif /* defined HAVE_XATTR */
557 get_one_xattr_from_fd (int fd,
559 const char *gio_attr,
566 len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
571 else if (len == -1 && errno == ERANGE)
573 len = g_fgetxattr (fd, xattr, NULL, 0);
578 value_p = g_malloc (len + 1);
580 len = g_fgetxattr (fd, xattr, value_p, len);
594 escape_xattr (info, gio_attr, value_p, len);
596 if (value_p != value)
599 #endif /* defined HAVE_XATTR */
602 get_xattrs_from_fd (int fd,
605 GFileAttributeMatcher *matcher)
610 ssize_t list_res_size;
613 const char *attr, *attr2;
616 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
618 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
622 list_res_size = g_flistxattr (fd, NULL, 0);
624 if (list_res_size == -1 ||
628 list_size = list_res_size;
629 list = g_malloc (list_size);
633 list_res_size = g_flistxattr (fd, list, list_size);
635 if (list_res_size == -1 && errno == ERANGE)
637 list_size = list_size * 2;
638 list = g_realloc (list, list_size);
642 if (list_res_size == -1)
646 while (list_res_size > 0)
648 if ((user && g_str_has_prefix (attr, "user.")) ||
649 (!user && !g_str_has_prefix (attr, "user.")))
651 char *escaped_attr, *gio_attr;
652 gboolean free_escaped_attr;
656 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
657 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
661 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
662 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
665 if (free_escaped_attr)
666 g_free (escaped_attr);
668 get_one_xattr_from_fd (fd, info, gio_attr, attr);
671 len = strlen (attr) + 1;
673 list_res_size -= len;
680 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
682 char *unescaped_attribute, *a;
683 gboolean free_unescaped_attribute;
685 attr2 = strchr (attr, ':');
688 attr2++; /* Skip ':' */
689 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
691 a = g_strconcat ("user.", unescaped_attribute, NULL);
693 a = unescaped_attribute;
695 get_one_xattr_from_fd (fd, info, attr, a);
700 if (free_unescaped_attribute)
701 g_free (unescaped_attribute);
705 #endif /* defined HAVE_XATTR */
710 set_xattr (char *filename,
711 const char *escaped_attribute,
712 const GFileAttributeValue *attr_value,
715 char *attribute, *value;
716 gboolean free_attribute, free_value;
717 int val_len, res, errsv;
721 if (attr_value == NULL)
723 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
724 _("Attribute value must be non-NULL"));
728 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
730 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
731 _("Invalid attribute type (string expected)"));
735 if (!name_is_valid (escaped_attribute))
737 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
738 _("Invalid extended attribute name"));
742 if (g_str_has_prefix (escaped_attribute, "xattr::"))
744 escaped_attribute += strlen ("xattr::");
749 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
750 escaped_attribute += strlen ("xattr-sys::");
754 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
755 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
758 a = g_strconcat ("user.", attribute, NULL);
762 res = g_setxattr (filename, a, value, val_len);
776 g_set_error (error, G_IO_ERROR,
777 g_io_error_from_errno (errsv),
778 _("Error setting extended attribute '%s': %s"),
779 escaped_attribute, g_strerror (errsv));
790 _g_local_file_info_get_parent_info (const char *dir,
791 GFileAttributeMatcher *attribute_matcher,
792 GLocalParentFileInfo *parent_info)
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);
1104 gecos = pwbufp->pw_gecos;
1108 comma = strchr (gecos, ',');
1111 data->real_name = convert_pwd_string_to_utf8 (gecos);
1116 /* Default fallbacks */
1117 if (data->real_name == NULL)
1119 if (data->user_name != NULL)
1120 data->real_name = g_strdup (data->user_name);
1122 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1125 if (data->user_name == NULL)
1126 data->user_name = g_strdup_printf ("%d", (int)uid);
1128 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1134 get_username_from_uid (uid_t uid)
1140 data = lookup_uid_data (uid);
1141 res = g_strdup (data->user_name);
1142 G_UNLOCK (uid_cache);
1148 get_realname_from_uid (uid_t uid)
1154 data = lookup_uid_data (uid);
1155 res = g_strdup (data->real_name);
1156 G_UNLOCK (uid_cache);
1161 /* called with lock held */
1163 lookup_gid_name (gid_t gid)
1168 struct group *gbufp;
1170 if (gid_cache == NULL)
1171 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1173 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1178 #if defined (HAVE_POSIX_GETGRGID_R)
1179 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1180 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1181 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1183 gbufp = getgrgid (gid);
1186 if (gbufp != NULL &&
1187 gbufp->gr_name != NULL &&
1188 gbufp->gr_name[0] != 0)
1189 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1191 name = g_strdup_printf("%d", (int)gid);
1193 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1199 get_groupname_from_gid (gid_t gid)
1205 name = lookup_gid_name (gid);
1206 res = g_strdup (name);
1207 G_UNLOCK (gid_cache);
1211 #endif /* !G_OS_WIN32 */
1214 get_content_type (const char *basename,
1216 GLocalFileStat *statbuf,
1217 gboolean is_symlink,
1218 gboolean symlink_broken,
1219 GFileQueryInfoFlags flags,
1223 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1224 return g_strdup ("inode/symlink");
1225 else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
1226 return g_strdup ("inode/directory");
1228 else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
1229 return g_strdup ("inode/chardevice");
1230 else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
1231 return g_strdup ("inode/blockdevice");
1232 else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
1233 return g_strdup ("inode/fifo");
1236 else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
1237 return g_strdup ("inode/socket");
1242 gboolean result_uncertain;
1244 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1247 if (!fast && result_uncertain && path != NULL)
1249 guchar sniff_buffer[4096];
1253 sniff_length = _g_unix_content_type_get_sniff_len ();
1254 if (sniff_length > 4096)
1255 sniff_length = 4096;
1258 fd = g_open (path, O_RDONLY | O_NOATIME, 0);
1259 if (fd < 0 && errno == EPERM)
1261 fd = g_open (path, O_RDONLY, 0);
1267 res = read (fd, sniff_buffer, sniff_length);
1268 (void) g_close (fd, NULL);
1271 g_free (content_type);
1272 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1278 return content_type;
1283 /* @stat_buf is the pre-calculated result of stat(path), or %NULL if that failed. */
1285 get_thumbnail_attributes (const char *path,
1287 const GStatBuf *stat_buf)
1289 GChecksum *checksum;
1294 uri = g_filename_to_uri (path, NULL, NULL);
1296 checksum = g_checksum_new (G_CHECKSUM_MD5);
1297 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1299 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1300 g_checksum_free (checksum);
1302 filename = g_build_filename (g_get_user_cache_dir (),
1303 "thumbnails", "large", basename,
1306 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1308 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1309 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1310 thumbnail_verify (filename, uri, stat_buf));
1315 filename = g_build_filename (g_get_user_cache_dir (),
1316 "thumbnails", "normal", basename,
1319 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1321 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1322 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1323 thumbnail_verify (filename, uri, stat_buf));
1328 filename = g_build_filename (g_get_user_cache_dir (),
1329 "thumbnails", "fail",
1330 "gnome-thumbnail-factory",
1334 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1336 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1337 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1338 thumbnail_verify (filename, uri, stat_buf));
1349 win32_get_file_user_info (const gchar *filename,
1354 PSECURITY_DESCRIPTOR psd = NULL;
1355 DWORD sd_size = 0; /* first call calculates the size required */
1357 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1358 if ((GetFileSecurityW (wfilename,
1359 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1362 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1363 (psd = g_try_malloc (sd_size)) != NULL &&
1364 GetFileSecurityW (wfilename,
1365 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1372 SID_NAME_USE name_use = 0; /* don't care? */
1373 wchar_t *name = NULL;
1374 wchar_t *domain = NULL;
1376 DWORD domain_len = 0;
1377 /* get the user name */
1381 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1383 if (!LookupAccountSidW (NULL, /* local machine */
1386 domain, &domain_len, /* no domain info yet */
1387 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1389 name = g_try_malloc (name_len * sizeof (wchar_t));
1390 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1391 if (name && domain &&
1392 LookupAccountSidW (NULL, /* local machine */
1395 domain, &domain_len, /* no domain info yet */
1398 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1404 /* get the group name */
1408 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1410 if (!LookupAccountSidW (NULL, /* local machine */
1413 domain, &domain_len, /* no domain info yet */
1414 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1416 name = g_try_malloc (name_len * sizeof (wchar_t));
1417 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1418 if (name && domain &&
1419 LookupAccountSidW (NULL, /* local machine */
1422 domain, &domain_len, /* no domain info yet */
1425 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1431 /* TODO: get real name */
1437 #endif /* G_OS_WIN32 */
1440 /* support for '.hidden' files */
1441 G_LOCK_DEFINE_STATIC (hidden_cache);
1442 static GHashTable *hidden_cache;
1445 remove_from_hidden_cache (gpointer user_data)
1447 G_LOCK (hidden_cache);
1448 g_hash_table_remove (hidden_cache, user_data);
1449 G_UNLOCK (hidden_cache);
1455 read_hidden_file (const gchar *dirname)
1457 gchar *contents = NULL;
1460 filename = g_build_path ("/", dirname, ".hidden", NULL);
1461 g_file_get_contents (filename, &contents, NULL, NULL);
1464 if (contents != NULL)
1470 table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1472 lines = g_strsplit (contents, "\n", 0);
1475 for (i = 0; lines[i]; i++)
1476 /* hash table takes the individual strings... */
1477 g_hash_table_add (table, lines[i]);
1479 /* ... so we only free the container. */
1489 maybe_unref_hash_table (gpointer data)
1492 g_hash_table_unref (data);
1496 file_is_hidden (const gchar *path,
1497 const gchar *basename)
1503 dirname = g_path_get_dirname (path);
1505 G_LOCK (hidden_cache);
1507 if G_UNLIKELY (hidden_cache == NULL)
1508 hidden_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1509 g_free, maybe_unref_hash_table);
1511 if (!g_hash_table_lookup_extended (hidden_cache, dirname,
1515 GSource *remove_from_cache_source;
1517 g_hash_table_insert (hidden_cache,
1518 mydirname = g_strdup (dirname),
1519 table = read_hidden_file (dirname));
1521 remove_from_cache_source = g_timeout_source_new_seconds (5);
1522 g_source_set_priority (remove_from_cache_source, G_PRIORITY_DEFAULT);
1523 g_source_set_callback (remove_from_cache_source,
1524 remove_from_hidden_cache,
1527 g_source_attach (remove_from_cache_source,
1528 GLIB_PRIVATE_CALL (g_get_worker_context) ());
1529 g_source_unref (remove_from_cache_source);
1532 result = table != NULL && g_hash_table_contains (table, basename);
1534 G_UNLOCK (hidden_cache);
1540 #endif /* !G_OS_WIN32 */
1543 _g_local_file_info_get_nostat (GFileInfo *info,
1544 const char *basename,
1546 GFileAttributeMatcher *attribute_matcher)
1548 g_file_info_set_name (info, basename);
1550 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1551 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1553 char *display_name = g_filename_display_basename (path);
1555 /* look for U+FFFD REPLACEMENT CHARACTER */
1556 if (strstr (display_name, "\357\277\275") != NULL)
1558 char *p = display_name;
1559 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1562 g_file_info_set_display_name (info, display_name);
1563 g_free (display_name);
1566 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1567 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1569 char *edit_name = g_filename_display_basename (path);
1570 g_file_info_set_edit_name (info, edit_name);
1575 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1576 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1578 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1580 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1586 get_icon_name (const char *path,
1587 gboolean use_symbolic,
1588 gboolean *with_fallbacks_out)
1590 const char *name = NULL;
1591 gboolean with_fallbacks = TRUE;
1593 if (strcmp (path, g_get_home_dir ()) == 0)
1595 name = use_symbolic ? "user-home-symbolic" : "user-home";
1596 with_fallbacks = FALSE;
1598 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1600 name = use_symbolic ? "user-desktop-symbolic" : "user-desktop";
1601 with_fallbacks = FALSE;
1603 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1605 name = use_symbolic ? "folder-documents-symbolic" : "folder-documents";
1607 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1609 name = use_symbolic ? "folder-download-symbolic" : "folder-download";
1611 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1613 name = use_symbolic ? "folder-music-symbolic" : "folder-music";
1615 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1617 name = use_symbolic ? "folder-pictures-symbolic" : "folder-pictures";
1619 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1621 name = use_symbolic ? "folder-publicshare-symbolic" : "folder-publicshare";
1623 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1625 name = use_symbolic ? "folder-templates-symbolic" : "folder-templates";
1627 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1629 name = use_symbolic ? "folder-videos-symbolic" : "folder-videos";
1636 if (with_fallbacks_out != NULL)
1637 *with_fallbacks_out = with_fallbacks;
1643 get_icon (const char *path,
1644 const char *content_type,
1646 gboolean use_symbolic)
1649 const char *icon_name;
1650 gboolean with_fallbacks;
1652 icon_name = get_icon_name (path, use_symbolic, &with_fallbacks);
1653 if (icon_name != NULL)
1656 icon = g_themed_icon_new_with_default_fallbacks (icon_name);
1658 icon = g_themed_icon_new (icon_name);
1663 icon = g_content_type_get_symbolic_icon (content_type);
1665 icon = g_content_type_get_icon (content_type);
1667 if (G_IS_THEMED_ICON (icon) && is_folder)
1669 g_themed_icon_append_name (G_THEMED_ICON (icon), use_symbolic ? "folder-symbolic" : "folder");
1677 _g_local_file_info_get (const char *basename,
1679 GFileAttributeMatcher *attribute_matcher,
1680 GFileQueryInfoFlags flags,
1681 GLocalParentFileInfo *parent_info,
1685 GLocalFileStat statbuf;
1687 struct stat statbuf2;
1691 gboolean is_symlink, symlink_broken;
1693 DWORD dos_attributes;
1695 char *symlink_target;
1700 info = g_file_info_new ();
1702 /* Make sure we don't set any unwanted attributes */
1703 g_file_info_set_attribute_mask (info, attribute_matcher);
1705 _g_local_file_info_get_nostat (info, basename, path, attribute_matcher);
1707 if (attribute_matcher == NULL)
1709 g_file_info_unset_attribute_mask (info);
1714 res = g_lstat (path, &statbuf);
1717 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1722 g_object_unref (info);
1726 len = wcslen (wpath);
1727 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1730 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1733 res = _wstati64 (wpath, &statbuf);
1734 dos_attributes = GetFileAttributesW (wpath);
1744 /* Don't bail out if we get Permission denied (SELinux?) */
1745 if (errsv != EACCES)
1747 char *display_name = g_filename_display_name (path);
1748 g_object_unref (info);
1749 g_set_error (error, G_IO_ERROR,
1750 g_io_error_from_errno (errsv),
1751 _("Error when getting information for file '%s': %s"),
1752 display_name, g_strerror (errsv));
1753 g_free (display_name);
1758 /* Even if stat() fails, try to get as much as other attributes possible */
1759 stat_ok = res != -1;
1762 device = statbuf.st_dev;
1767 is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
1771 symlink_broken = FALSE;
1775 g_file_info_set_is_symlink (info, TRUE);
1777 /* Unless NOFOLLOW was set we default to following symlinks */
1778 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1780 res = stat (path, &statbuf2);
1782 /* Report broken links as symlinks */
1789 symlink_broken = TRUE;
1795 set_info_from_stat (info, &statbuf, attribute_matcher);
1798 if (stat_ok && _g_local_file_is_lost_found_dir (path, statbuf.st_dev))
1799 g_file_info_set_is_hidden (info, TRUE);
1803 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1804 G_FILE_ATTRIBUTE_ID_STANDARD_IS_HIDDEN))
1806 if (basename != NULL &&
1807 (basename[0] == '.' ||
1808 file_is_hidden (path, basename)))
1809 g_file_info_set_is_hidden (info, TRUE);
1812 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1813 (stat_ok && S_ISREG (statbuf.st_mode)))
1814 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1816 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1817 g_file_info_set_is_hidden (info, TRUE);
1819 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1820 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1822 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1823 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1826 symlink_target = NULL;
1830 symlink_target = read_link (path);
1831 if (symlink_target &&
1832 _g_file_attribute_matcher_matches_id (attribute_matcher,
1833 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1834 g_file_info_set_symlink_target (info, symlink_target);
1837 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1838 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1839 _g_file_attribute_matcher_matches_id (attribute_matcher,
1840 G_FILE_ATTRIBUTE_ID_STANDARD_ICON) ||
1841 _g_file_attribute_matcher_matches_id (attribute_matcher,
1842 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1844 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
1848 g_file_info_set_content_type (info, content_type);
1850 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1851 G_FILE_ATTRIBUTE_ID_STANDARD_ICON)
1852 || _g_file_attribute_matcher_matches_id (attribute_matcher,
1853 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1857 /* non symbolic icon */
1858 icon = get_icon (path, content_type, S_ISDIR (statbuf.st_mode), FALSE);
1861 g_file_info_set_icon (info, icon);
1862 g_object_unref (icon);
1866 icon = get_icon (path, content_type, S_ISDIR (statbuf.st_mode), TRUE);
1869 g_file_info_set_symbolic_icon (info, icon);
1870 g_object_unref (icon);
1875 g_free (content_type);
1879 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1880 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1882 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
1886 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1887 g_free (content_type);
1891 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1892 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1897 win32_get_file_user_info (path, NULL, &name, NULL);
1900 name = get_username_from_uid (statbuf.st_uid);
1903 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1907 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1908 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1912 win32_get_file_user_info (path, NULL, NULL, &name);
1915 name = get_realname_from_uid (statbuf.st_uid);
1918 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1922 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1923 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1927 win32_get_file_user_info (path, &name, NULL, NULL);
1930 name = get_groupname_from_gid (statbuf.st_gid);
1933 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1937 if (stat_ok && parent_info && parent_info->device != 0 &&
1938 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1939 statbuf.st_dev != parent_info->device)
1940 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1943 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1946 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1948 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1949 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1951 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1952 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH))
1955 get_thumbnail_attributes (path, info, &statbuf);
1957 get_thumbnail_attributes (path, info, NULL);
1960 vfs = g_vfs_get_default ();
1961 class = G_VFS_GET_CLASS (vfs);
1962 if (class->local_file_add_info)
1964 class->local_file_add_info (vfs,
1970 &parent_info->extra_data,
1971 &parent_info->free_extra_data);
1974 g_file_info_unset_attribute_mask (info);
1976 g_free (symlink_target);
1982 _g_local_file_info_get_from_fd (int fd,
1983 const char *attributes,
1986 GLocalFileStat stat_buf;
1987 GFileAttributeMatcher *matcher;
1991 #define FSTAT _fstati64
1996 if (FSTAT (fd, &stat_buf) == -1)
2000 g_set_error (error, G_IO_ERROR,
2001 g_io_error_from_errno (errsv),
2002 _("Error when getting information for file descriptor: %s"),
2003 g_strerror (errsv));
2007 info = g_file_info_new ();
2009 matcher = g_file_attribute_matcher_new (attributes);
2011 /* Make sure we don't set any unwanted attributes */
2012 g_file_info_set_attribute_mask (info, matcher);
2014 set_info_from_stat (info, &stat_buf, matcher);
2017 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
2018 is_selinux_enabled ())
2021 if (fgetfilecon_raw (fd, &context) >= 0)
2023 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
2029 get_xattrs_from_fd (fd, TRUE, info, matcher);
2030 get_xattrs_from_fd (fd, FALSE, info, matcher);
2032 g_file_attribute_matcher_unref (matcher);
2034 g_file_info_unset_attribute_mask (info);
2040 get_uint32 (const GFileAttributeValue *value,
2044 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
2046 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2047 _("Invalid attribute type (uint32 expected)"));
2051 *val_out = value->u.uint32;
2058 get_uint64 (const GFileAttributeValue *value,
2062 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
2064 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2065 _("Invalid attribute type (uint64 expected)"));
2069 *val_out = value->u.uint64;
2075 #if defined(HAVE_SYMLINK)
2077 get_byte_string (const GFileAttributeValue *value,
2078 const char **val_out,
2081 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
2083 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2084 _("Invalid attribute type (byte string expected)"));
2088 *val_out = value->u.string;
2096 get_string (const GFileAttributeValue *value,
2097 const char **val_out,
2100 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
2102 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2103 _("Invalid attribute type (byte string expected)"));
2107 *val_out = value->u.string;
2114 set_unix_mode (char *filename,
2115 GFileQueryInfoFlags flags,
2116 const GFileAttributeValue *value,
2122 if (!get_uint32 (value, &val, error))
2126 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
2128 res = lchmod (filename, val);
2130 struct stat statbuf;
2131 /* Calling chmod on a symlink changes permissions on the symlink.
2132 * We don't want to do this, so we need to check for a symlink */
2133 res = g_lstat (filename, &statbuf);
2134 if (res == 0 && S_ISLNK (statbuf.st_mode))
2136 g_set_error_literal (error, G_IO_ERROR,
2137 G_IO_ERROR_NOT_SUPPORTED,
2138 _("Cannot set permissions on symlinks"));
2142 res = g_chmod (filename, val);
2146 res = g_chmod (filename, val);
2152 g_set_error (error, G_IO_ERROR,
2153 g_io_error_from_errno (errsv),
2154 _("Error setting permissions: %s"),
2155 g_strerror (errsv));
2163 set_unix_uid_gid (char *filename,
2164 const GFileAttributeValue *uid_value,
2165 const GFileAttributeValue *gid_value,
2166 GFileQueryInfoFlags flags,
2176 if (!get_uint32 (uid_value, &val, error))
2185 if (!get_uint32 (gid_value, &val, error))
2193 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
2194 res = lchown (filename, uid, gid);
2197 res = chown (filename, uid, gid);
2203 g_set_error (error, G_IO_ERROR,
2204 g_io_error_from_errno (errsv),
2205 _("Error setting owner: %s"),
2206 g_strerror (errsv));
2215 set_symlink (char *filename,
2216 const GFileAttributeValue *value,
2220 struct stat statbuf;
2222 if (!get_byte_string (value, &val, error))
2227 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2228 _("symlink must be non-NULL"));
2232 if (g_lstat (filename, &statbuf))
2236 g_set_error (error, G_IO_ERROR,
2237 g_io_error_from_errno (errsv),
2238 _("Error setting symlink: %s"),
2239 g_strerror (errsv));
2243 if (!S_ISLNK (statbuf.st_mode))
2245 g_set_error_literal (error, G_IO_ERROR,
2246 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2247 _("Error setting symlink: file is not a symlink"));
2251 if (g_unlink (filename))
2255 g_set_error (error, G_IO_ERROR,
2256 g_io_error_from_errno (errsv),
2257 _("Error setting symlink: %s"),
2258 g_strerror (errsv));
2262 if (symlink (filename, val) != 0)
2266 g_set_error (error, G_IO_ERROR,
2267 g_io_error_from_errno (errsv),
2268 _("Error setting symlink: %s"),
2269 g_strerror (errsv));
2279 lazy_stat (char *filename,
2280 struct stat *statbuf,
2281 gboolean *called_stat)
2288 res = g_stat (filename, statbuf);
2291 *called_stat = TRUE;
2298 set_mtime_atime (char *filename,
2299 const GFileAttributeValue *mtime_value,
2300 const GFileAttributeValue *mtime_usec_value,
2301 const GFileAttributeValue *atime_value,
2302 const GFileAttributeValue *atime_usec_value,
2307 guint32 val_usec = 0;
2308 struct stat statbuf;
2309 gboolean got_stat = FALSE;
2310 struct timeval times[2] = { {0, 0}, {0, 0} };
2315 if (!get_uint64 (atime_value, &val, error))
2317 times[0].tv_sec = val;
2321 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2323 times[0].tv_sec = statbuf.st_mtime;
2324 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2325 times[0].tv_usec = statbuf.st_atimensec / 1000;
2326 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2327 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2332 if (atime_usec_value)
2334 if (!get_uint32 (atime_usec_value, &val_usec, error))
2336 times[0].tv_usec = val_usec;
2342 if (!get_uint64 (mtime_value, &val, error))
2344 times[1].tv_sec = val;
2348 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2350 times[1].tv_sec = statbuf.st_mtime;
2351 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2352 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2353 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2354 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2359 if (mtime_usec_value)
2361 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2363 times[1].tv_usec = val_usec;
2366 res = utimes (filename, times);
2371 g_set_error (error, G_IO_ERROR,
2372 g_io_error_from_errno (errsv),
2373 _("Error setting modification or access time: %s"),
2374 g_strerror (errsv));
2384 set_selinux_context (char *filename,
2385 const GFileAttributeValue *value,
2390 if (!get_string (value, &val, error))
2395 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2396 _("SELinux context must be non-NULL"));
2400 if (is_selinux_enabled ()) {
2401 security_context_t val_s;
2403 val_s = g_strdup (val);
2405 if (setfilecon_raw (filename, val_s) < 0)
2409 g_set_error (error, G_IO_ERROR,
2410 g_io_error_from_errno (errsv),
2411 _("Error setting SELinux context: %s"),
2412 g_strerror (errsv));
2417 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2418 _("SELinux is not enabled on this system"));
2428 _g_local_file_info_set_attribute (char *filename,
2429 const char *attribute,
2430 GFileAttributeType type,
2432 GFileQueryInfoFlags flags,
2433 GCancellable *cancellable,
2436 GFileAttributeValue value = { 0 };
2440 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2442 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2443 return set_unix_mode (filename, flags, &value, error);
2446 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2447 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2448 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2449 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2453 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2454 return set_symlink (filename, &value, error);
2458 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2459 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2460 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2461 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2462 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2463 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2464 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2465 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2469 else if (g_str_has_prefix (attribute, "xattr::"))
2470 return set_xattr (filename, attribute, &value, error);
2471 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2472 return set_xattr (filename, attribute, &value, error);
2476 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2477 return set_selinux_context (filename, &value, error);
2480 vfs = g_vfs_get_default ();
2481 class = G_VFS_GET_CLASS (vfs);
2482 if (class->local_file_set_attributes)
2486 info = g_file_info_new ();
2487 g_file_info_set_attribute (info,
2491 if (!class->local_file_set_attributes (vfs, filename,
2496 g_object_unref (info);
2500 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2502 g_object_unref (info);
2506 g_object_unref (info);
2509 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2510 _("Setting attribute %s not supported"), attribute);
2515 _g_local_file_info_set_attributes (char *filename,
2517 GFileQueryInfoFlags flags,
2518 GCancellable *cancellable,
2521 GFileAttributeValue *value;
2523 GFileAttributeValue *uid, *gid;
2526 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2528 #if defined (HAVE_CHOWN) || defined (HAVE_UTIMES)
2529 GFileAttributeStatus status;
2535 /* Handles setting multiple specified data in a single set, and takes care
2536 of ordering restrictions when setting attributes */
2540 /* Set symlink first, since this recreates the file */
2542 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2545 if (!set_symlink (filename, value, error))
2547 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2549 /* Don't set error multiple times */
2553 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2559 /* Group uid and gid setting into one call
2560 * Change ownership before permissions, since ownership changes can
2561 change permissions (e.g. setuid)
2563 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2564 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2568 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2570 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2572 /* Don't set error multiple times */
2576 status = G_FILE_ATTRIBUTE_STATUS_SET;
2578 uid->status = status;
2580 gid->status = status;
2584 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2587 if (!set_unix_mode (filename, flags, value, error))
2589 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2591 /* Don't set error multiple times */
2595 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2600 /* Group all time settings into one call
2601 * Change times as the last thing to avoid it changing due to metadata changes
2604 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2605 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2606 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2607 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2609 if (mtime || mtime_usec || atime || atime_usec)
2611 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2613 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2615 /* Don't set error multiple times */
2619 status = G_FILE_ATTRIBUTE_STATUS_SET;
2622 mtime->status = status;
2624 mtime_usec->status = status;
2626 atime->status = status;
2628 atime_usec->status = status;
2632 /* xattrs are handled by default callback */
2635 /* SELinux context */
2637 if (is_selinux_enabled ()) {
2638 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2641 if (!set_selinux_context (filename, value, error))
2643 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2645 /* Don't set error multiple times */
2649 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2654 vfs = g_vfs_get_default ();
2655 class = G_VFS_GET_CLASS (vfs);
2656 if (class->local_file_set_attributes)
2658 if (!class->local_file_set_attributes (vfs, filename,
2664 /* Don't set error multiple times */