1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
27 #ifdef HAVE_SYS_TIME_H
30 #include <sys/types.h>
46 #include <selinux/selinux.h>
51 #if defined HAVE_SYS_XATTR_H
52 #include <sys/xattr.h>
53 #elif defined HAVE_ATTR_XATTR_H
54 #include <attr/xattr.h>
56 #error "Neither <sys/xattr.h> nor <attr/xattr.h> is present but extended attribute support is enabled."
57 #endif /* defined HAVE_SYS_XATTR_H || HAVE_ATTR_XATTR_H */
59 #endif /* HAVE_XATTR */
61 #include <glib/gstdio.h>
62 #include <gfileattribute-priv.h>
76 #define X_OK 0 /* not really */
79 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
82 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
85 #define S_IXUSR _S_IEXEC
89 #include "glocalfileinfo.h"
91 #include "gthemedicon.h"
92 #include "gcontenttype.h"
93 #include "gcontenttypeprivate.h"
97 struct ThumbMD5Context {
100 unsigned char in[64];
110 G_LOCK_DEFINE_STATIC (uid_cache);
111 static GHashTable *uid_cache = NULL;
113 G_LOCK_DEFINE_STATIC (gid_cache);
114 static GHashTable *gid_cache = NULL;
116 #endif /* !G_OS_WIN32 */
119 _g_local_file_info_create_etag (GLocalFileStat *statbuf)
123 tv.tv_sec = statbuf->st_mtime;
124 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
125 tv.tv_usec = statbuf->st_mtimensec / 1000;
126 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
127 tv.tv_usec = statbuf->st_mtim.tv_nsec / 1000;
132 return g_strdup_printf ("%lu:%lu", tv.tv_sec, tv.tv_usec);
136 _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
138 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
139 (guint64) statbuf->st_dev,
140 (guint64) statbuf->st_ino);
144 _g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
146 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
147 (guint64) statbuf->st_dev);
154 read_link (const gchar *full_name)
161 buffer = g_malloc (size);
167 read_size = readlink (full_name, buffer, size);
173 if (read_size < size)
175 buffer[read_size] = 0;
179 buffer = g_realloc (buffer, size);
189 /* Get the SELinux security context */
191 get_selinux_context (const char *path,
193 GFileAttributeMatcher *attribute_matcher,
194 gboolean follow_symlinks)
198 if (!g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT))
201 if (is_selinux_enabled ())
205 if (lgetfilecon_raw (path, &context) < 0)
210 if (getfilecon_raw (path, &context) < 0)
216 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
225 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
226 * (Mac) getxattr(..., XATTR_NOFOLLOW)
228 #ifdef HAVE_XATTR_NOFOLLOW
229 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
230 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
231 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
233 #define g_fgetxattr fgetxattr
234 #define g_flistxattr flistxattr
235 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
239 g_getxattr (const char *path, const char *name, void *value, size_t size,
240 gboolean follow_symlinks)
242 #ifdef HAVE_XATTR_NOFOLLOW
243 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
246 return getxattr (path, name, value, size);
248 return lgetxattr (path, name, value, size);
253 g_listxattr(const char *path, char *namebuf, size_t size,
254 gboolean follow_symlinks)
256 #ifdef HAVE_XATTR_NOFOLLOW
257 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
260 return listxattr (path, namebuf, size);
262 return llistxattr (path, namebuf, size);
269 return c >= 32 && c <= 126 && c != '\\';
273 name_is_valid (const char *str)
277 if (!valid_char (*str++))
284 hex_escape_string (const char *str,
285 gboolean *free_return)
288 char *escaped_str, *p;
290 static char *hex_digits = "0123456789abcdef";
296 for (i = 0; i < len; i++)
298 if (!valid_char (str[i]))
302 if (num_invalid == 0)
304 *free_return = FALSE;
308 escaped_str = g_malloc (len + num_invalid*3 + 1);
311 for (i = 0; i < len; i++)
313 if (valid_char (str[i]))
320 *p++ = hex_digits[(c >> 4) & 0xf];
321 *p++ = hex_digits[c & 0xf];
331 hex_unescape_string (const char *str,
333 gboolean *free_return)
336 char *unescaped_str, *p;
342 if (strchr (str, '\\') == NULL)
346 *free_return = FALSE;
350 unescaped_str = g_malloc (len + 1);
353 for (i = 0; i < len; i++)
355 if (str[i] == '\\' &&
360 (g_ascii_xdigit_value (str[i+2]) << 4) |
361 g_ascii_xdigit_value (str[i+3]);
371 *out_len = p - unescaped_str;
373 return unescaped_str;
377 escape_xattr (GFileInfo *info,
378 const char *gio_attr, /* gio attribute name */
379 const char *value, /* Is zero terminated */
380 size_t len /* not including zero termination */)
383 gboolean free_escaped_val;
385 escaped_val = hex_escape_string (value, &free_escaped_val);
387 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
389 if (free_escaped_val)
390 g_free (escaped_val);
394 get_one_xattr (const char *path,
396 const char *gio_attr,
398 gboolean follow_symlinks)
404 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
409 else if (len == -1 && errno == ERANGE)
411 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
416 value_p = g_malloc (len+1);
418 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
432 escape_xattr (info, gio_attr, value_p, len);
434 if (value_p != value)
438 #endif /* defined HAVE_XATTR */
441 get_xattrs (const char *path,
444 GFileAttributeMatcher *matcher,
445 gboolean follow_symlinks)
450 ssize_t list_res_size;
453 const char *attr, *attr2;
456 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
458 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
462 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
464 if (list_res_size == -1 ||
468 list_size = list_res_size;
469 list = g_malloc (list_size);
473 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
475 if (list_res_size == -1 && errno == ERANGE)
477 list_size = list_size * 2;
478 list = g_realloc (list, list_size);
482 if (list_res_size == -1)
486 while (list_res_size > 0)
488 if ((user && g_str_has_prefix (attr, "user.")) ||
489 (!user && !g_str_has_prefix (attr, "user.")))
491 char *escaped_attr, *gio_attr;
492 gboolean free_escaped_attr;
496 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
497 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
501 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
502 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
505 if (free_escaped_attr)
506 g_free (escaped_attr);
508 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
513 len = strlen (attr) + 1;
515 list_res_size -= len;
522 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
524 char *unescaped_attribute, *a;
525 gboolean free_unescaped_attribute;
527 attr2 = strchr (attr, ':');
530 attr2++; /* Skip ':' */
531 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
533 a = g_strconcat ("user.", unescaped_attribute, NULL);
535 a = unescaped_attribute;
537 get_one_xattr (path, info, attr, a, follow_symlinks);
542 if (free_unescaped_attribute)
543 g_free (unescaped_attribute);
547 #endif /* defined HAVE_XATTR */
552 get_one_xattr_from_fd (int fd,
554 const char *gio_attr,
561 len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
566 else if (len == -1 && errno == ERANGE)
568 len = g_fgetxattr (fd, xattr, NULL, 0);
573 value_p = g_malloc (len + 1);
575 len = g_fgetxattr (fd, xattr, value_p, len);
589 escape_xattr (info, gio_attr, value_p, len);
591 if (value_p != value)
594 #endif /* defined HAVE_XATTR */
597 get_xattrs_from_fd (int fd,
600 GFileAttributeMatcher *matcher)
605 ssize_t list_res_size;
608 const char *attr, *attr2;
611 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
613 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
617 list_res_size = g_flistxattr (fd, NULL, 0);
619 if (list_res_size == -1 ||
623 list_size = list_res_size;
624 list = g_malloc (list_size);
628 list_res_size = g_flistxattr (fd, list, list_size);
630 if (list_res_size == -1 && errno == ERANGE)
632 list_size = list_size * 2;
633 list = g_realloc (list, list_size);
637 if (list_res_size == -1)
641 while (list_res_size > 0)
643 if ((user && g_str_has_prefix (attr, "user.")) ||
644 (!user && !g_str_has_prefix (attr, "user.")))
646 char *escaped_attr, *gio_attr;
647 gboolean free_escaped_attr;
651 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
652 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
656 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
657 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
660 if (free_escaped_attr)
661 g_free (escaped_attr);
663 get_one_xattr_from_fd (fd, info, gio_attr, attr);
666 len = strlen (attr) + 1;
668 list_res_size -= len;
675 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
677 char *unescaped_attribute, *a;
678 gboolean free_unescaped_attribute;
680 attr2 = strchr (attr, ':');
683 attr2++; /* Skip ':' */
684 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
686 a = g_strconcat ("user.", unescaped_attribute, NULL);
688 a = unescaped_attribute;
690 get_one_xattr_from_fd (fd, info, attr, a);
695 if (free_unescaped_attribute)
696 g_free (unescaped_attribute);
700 #endif /* defined HAVE_XATTR */
705 set_xattr (char *filename,
706 const char *escaped_attribute,
707 const GFileAttributeValue *attr_value,
710 char *attribute, *value;
711 gboolean free_attribute, free_value;
712 int val_len, res, errsv;
716 if (attr_value == NULL)
718 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
719 _("Attribute value must be non-NULL"));
723 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
725 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
726 _("Invalid attribute type (string expected)"));
730 if (!name_is_valid (escaped_attribute))
732 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
733 _("Invalid extended attribute name"));
737 if (g_str_has_prefix (escaped_attribute, "xattr::"))
739 escaped_attribute += strlen ("xattr::");
744 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
745 escaped_attribute += strlen ("xattr-sys::");
749 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
750 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
753 a = g_strconcat ("user.", attribute, NULL);
757 res = g_setxattr (filename, a, value, val_len);
771 g_set_error (error, G_IO_ERROR,
772 g_io_error_from_errno (errsv),
773 _("Error setting extended attribute '%s': %s"),
774 escaped_attribute, g_strerror (errsv));
785 _g_local_file_info_get_parent_info (const char *dir,
786 GFileAttributeMatcher *attribute_matcher,
787 GLocalParentFileInfo *parent_info)
789 /* Use plain struct stat for now as long as we only look at the
790 * S_ISVTX bit which doesn't exist on Win32 anyway.
795 parent_info->writable = FALSE;
796 parent_info->is_sticky = FALSE;
797 parent_info->has_trash_dir = FALSE;
798 parent_info->device = 0;
800 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME) ||
801 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE) ||
802 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH) ||
803 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT))
805 /* FIXME: Windows: The underlying _waccess() call in the C
806 * library is mostly pointless as it only looks at the READONLY
807 * FAT-style attribute of the file, it doesn't check the ACL at
810 parent_info->writable = (g_access (dir, W_OK) == 0);
812 res = g_stat (dir, &statbuf);
815 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
816 * renamed or deleted only by the owner of the file, by the owner of the directory, and
817 * by a privileged process.
822 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
824 parent_info->is_sticky = FALSE;
826 parent_info->owner = statbuf.st_uid;
827 parent_info->device = statbuf.st_dev;
828 /* No need to find trash dir if it's not writable anyway */
829 if (parent_info->writable &&
830 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
831 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
837 get_access_rights (GFileAttributeMatcher *attribute_matcher,
840 GLocalFileStat *statbuf,
841 GLocalParentFileInfo *parent_info)
843 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
844 if (g_file_attribute_matcher_matches (attribute_matcher,
845 G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
846 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
847 g_access (path, R_OK) == 0);
849 if (g_file_attribute_matcher_matches (attribute_matcher,
850 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
851 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
852 g_access (path, W_OK) == 0);
854 if (g_file_attribute_matcher_matches (attribute_matcher,
855 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
856 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE,
857 g_access (path, X_OK) == 0);
865 if (parent_info->writable)
867 if (parent_info->is_sticky)
870 uid_t uid = geteuid ();
872 if (uid == statbuf->st_uid ||
873 uid == parent_info->owner ||
882 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME))
883 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME,
886 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE))
887 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE,
890 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
891 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH,
892 writable && parent_info->has_trash_dir);
897 set_info_from_stat (GFileInfo *info,
898 GLocalFileStat *statbuf,
899 GFileAttributeMatcher *attribute_matcher)
903 file_type = G_FILE_TYPE_UNKNOWN;
905 if (S_ISREG (statbuf->st_mode))
906 file_type = G_FILE_TYPE_REGULAR;
907 else if (S_ISDIR (statbuf->st_mode))
908 file_type = G_FILE_TYPE_DIRECTORY;
910 else if (S_ISCHR (statbuf->st_mode) ||
911 S_ISBLK (statbuf->st_mode) ||
912 S_ISFIFO (statbuf->st_mode)
914 || S_ISSOCK (statbuf->st_mode)
917 file_type = G_FILE_TYPE_SPECIAL;
920 else if (S_ISLNK (statbuf->st_mode))
921 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
924 g_file_info_set_file_type (info, file_type);
925 g_file_info_set_size (info, statbuf->st_size);
927 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE, statbuf->st_dev);
929 /* Pointless setting these on Windows even if they exist in the struct */
930 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_INODE, statbuf->st_ino);
931 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_NLINK, statbuf->st_nlink);
932 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_UID, statbuf->st_uid);
933 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, statbuf->st_gid);
934 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_RDEV, statbuf->st_rdev);
936 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
937 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, statbuf->st_mode);
938 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
939 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE, statbuf->st_blksize);
941 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
942 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_BLOCKS, statbuf->st_blocks);
943 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_ALLOCATED_SIZE,
944 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
947 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED, statbuf->st_mtime);
948 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
949 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
950 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
951 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
954 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS, statbuf->st_atime);
955 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
956 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
957 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
958 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
961 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_CHANGED, statbuf->st_ctime);
962 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
963 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
964 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
965 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
968 if (g_file_attribute_matcher_matches (attribute_matcher,
969 G_FILE_ATTRIBUTE_ETAG_VALUE))
971 char *etag = _g_local_file_info_create_etag (statbuf);
972 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ETAG_VALUE, etag);
976 if (g_file_attribute_matcher_matches (attribute_matcher,
977 G_FILE_ATTRIBUTE_ID_FILE))
979 char *id = _g_local_file_info_create_file_id (statbuf);
980 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE, id);
984 if (g_file_attribute_matcher_matches (attribute_matcher,
985 G_FILE_ATTRIBUTE_ID_FILESYSTEM))
987 char *id = _g_local_file_info_create_fs_id (statbuf);
988 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILESYSTEM, id);
996 make_valid_utf8 (const char *name)
999 const gchar *remainder, *invalid;
1000 gint remaining_bytes, valid_bytes;
1004 remaining_bytes = strlen (name);
1006 while (remaining_bytes != 0)
1008 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1010 valid_bytes = invalid - remainder;
1013 string = g_string_sized_new (remaining_bytes);
1015 g_string_append_len (string, remainder, valid_bytes);
1016 /* append U+FFFD REPLACEMENT CHARACTER */
1017 g_string_append (string, "\357\277\275");
1019 remaining_bytes -= valid_bytes + 1;
1020 remainder = invalid + 1;
1024 return g_strdup (name);
1026 g_string_append (string, remainder);
1028 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1030 return g_string_free (string, FALSE);
1034 convert_pwd_string_to_utf8 (char *pwd_str)
1038 if (!g_utf8_validate (pwd_str, -1, NULL))
1040 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1041 if (utf8_string == NULL)
1042 utf8_string = make_valid_utf8 (pwd_str);
1045 utf8_string = g_strdup (pwd_str);
1051 uid_data_free (UidData *data)
1053 g_free (data->user_name);
1054 g_free (data->real_name);
1058 /* called with lock held */
1060 lookup_uid_data (uid_t uid)
1064 struct passwd pwbuf;
1065 struct passwd *pwbufp;
1066 char *gecos, *comma;
1068 if (uid_cache == NULL)
1069 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1071 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1076 data = g_new0 (UidData, 1);
1078 #if defined(HAVE_POSIX_GETPWUID_R)
1079 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1080 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1081 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1083 pwbufp = getpwuid (uid);
1088 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1089 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1091 gecos = pwbufp->pw_gecos;
1095 comma = strchr (gecos, ',');
1098 data->real_name = convert_pwd_string_to_utf8 (gecos);
1102 /* Default fallbacks */
1103 if (data->real_name == NULL)
1105 if (data->user_name != NULL)
1106 data->real_name = g_strdup (data->user_name);
1108 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1111 if (data->user_name == NULL)
1112 data->user_name = g_strdup_printf ("%d", (int)uid);
1114 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1120 get_username_from_uid (uid_t uid)
1126 data = lookup_uid_data (uid);
1127 res = g_strdup (data->user_name);
1128 G_UNLOCK (uid_cache);
1134 get_realname_from_uid (uid_t uid)
1140 data = lookup_uid_data (uid);
1141 res = g_strdup (data->real_name);
1142 G_UNLOCK (uid_cache);
1147 /* called with lock held */
1149 lookup_gid_name (gid_t gid)
1154 struct group *gbufp;
1156 if (gid_cache == NULL)
1157 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1159 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1164 #if defined (HAVE_POSIX_GETGRGID_R)
1165 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1166 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1167 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1169 gbufp = getgrgid (gid);
1172 if (gbufp != NULL &&
1173 gbufp->gr_name != NULL &&
1174 gbufp->gr_name[0] != 0)
1175 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1177 name = g_strdup_printf("%d", (int)gid);
1179 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1185 get_groupname_from_gid (gid_t gid)
1191 name = lookup_gid_name (gid);
1192 res = g_strdup (name);
1193 G_UNLOCK (gid_cache);
1197 #endif /* !G_OS_WIN32 */
1200 get_content_type (const char *basename,
1202 GLocalFileStat *statbuf,
1203 gboolean is_symlink,
1204 gboolean symlink_broken,
1205 GFileQueryInfoFlags flags,
1209 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1210 return g_strdup ("inode/symlink");
1211 else if (S_ISDIR(statbuf->st_mode))
1212 return g_strdup ("inode/directory");
1214 else if (S_ISCHR(statbuf->st_mode))
1215 return g_strdup ("inode/chardevice");
1216 else if (S_ISBLK(statbuf->st_mode))
1217 return g_strdup ("inode/blockdevice");
1218 else if (S_ISFIFO(statbuf->st_mode))
1219 return g_strdup ("inode/fifo");
1222 else if (S_ISSOCK(statbuf->st_mode))
1223 return g_strdup ("inode/socket");
1228 gboolean result_uncertain;
1230 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1233 if (!fast && result_uncertain && path != NULL)
1235 guchar sniff_buffer[4096];
1239 sniff_length = _g_unix_content_type_get_sniff_len ();
1240 if (sniff_length > 4096)
1241 sniff_length = 4096;
1244 fd = open (path, O_RDONLY | O_NOATIME);
1245 if (fd < 0 && errno == EPERM)
1247 fd = open (path, O_RDONLY);
1253 res = read (fd, sniff_buffer, sniff_length);
1257 g_free (content_type);
1258 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1264 return content_type;
1270 get_thumbnail_attributes (const char *path,
1273 GChecksum *checksum;
1278 uri = g_filename_to_uri (path, NULL, NULL);
1280 checksum = g_checksum_new (G_CHECKSUM_MD5);
1281 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1285 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1286 g_checksum_free (checksum);
1288 filename = g_build_filename (g_get_home_dir (),
1289 ".thumbnails", "normal", basename,
1292 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1293 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1297 filename = g_build_filename (g_get_home_dir (),
1298 ".thumbnails", "fail",
1299 "gnome-thumbnail-factory",
1303 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1304 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1312 win32_get_file_user_info (const gchar *filename,
1317 PSECURITY_DESCRIPTOR psd = NULL;
1318 DWORD sd_size = 0; /* first call calculates the size required */
1320 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1321 if ((GetFileSecurityW (wfilename,
1322 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1325 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1326 (psd = g_try_malloc (sd_size)) != NULL &&
1327 GetFileSecurityW (wfilename,
1328 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1335 SID_NAME_USE name_use = 0; /* don't care? */
1336 wchar_t *name = NULL;
1337 wchar_t *domain = NULL;
1339 DWORD domain_len = 0;
1340 /* get the user name */
1344 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1346 if (!LookupAccountSidW (NULL, /* local machine */
1349 domain, &domain_len, /* no domain info yet */
1350 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1352 name = g_try_malloc (name_len * sizeof (wchar_t));
1353 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1354 if (name && domain &&
1355 LookupAccountSidW (NULL, /* local machine */
1358 domain, &domain_len, /* no domain info yet */
1361 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1367 /* get the group name */
1371 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1373 if (!LookupAccountSidW (NULL, /* local machine */
1376 domain, &domain_len, /* no domain info yet */
1377 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1379 name = g_try_malloc (name_len * sizeof (wchar_t));
1380 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1381 if (name && domain &&
1382 LookupAccountSidW (NULL, /* local machine */
1385 domain, &domain_len, /* no domain info yet */
1388 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1394 /* TODO: get real name */
1400 #endif /* G_OS_WIN32 */
1403 _g_local_file_info_get (const char *basename,
1405 GFileAttributeMatcher *attribute_matcher,
1406 GFileQueryInfoFlags flags,
1407 GLocalParentFileInfo *parent_info,
1411 GLocalFileStat statbuf;
1413 struct stat statbuf2;
1416 gboolean is_symlink, symlink_broken;
1418 DWORD dos_attributes;
1421 info = g_file_info_new ();
1423 /* Make sure we don't set any unwanted attributes */
1424 g_file_info_set_attribute_mask (info, attribute_matcher);
1426 g_file_info_set_name (info, basename);
1428 /* Avoid stat in trivial case */
1429 if (attribute_matcher == NULL)
1433 res = g_lstat (path, &statbuf);
1436 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1441 g_object_unref (info);
1445 len = wcslen (wpath);
1446 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1449 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1452 res = _wstati64 (wpath, &statbuf);
1453 dos_attributes = GetFileAttributesW (wpath);
1462 char *display_name = g_filename_display_name (path);
1463 g_object_unref (info);
1464 g_set_error (error, G_IO_ERROR,
1465 g_io_error_from_errno (errsv),
1466 _("Error stating file '%s': %s"),
1467 display_name, g_strerror (errsv));
1468 g_free (display_name);
1473 is_symlink = S_ISLNK (statbuf.st_mode);
1477 symlink_broken = FALSE;
1481 g_file_info_set_is_symlink (info, TRUE);
1483 /* Unless NOFOLLOW was set we default to following symlinks */
1484 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1486 res = stat (path, &statbuf2);
1488 /* Report broken links as symlinks */
1492 symlink_broken = TRUE;
1497 set_info_from_stat (info, &statbuf, attribute_matcher);
1500 if (basename != NULL && basename[0] == '.')
1501 g_file_info_set_is_hidden (info, TRUE);
1503 if (basename != NULL && basename[strlen (basename) -1] == '~')
1504 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP, TRUE);
1506 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1507 g_file_info_set_is_hidden (info, TRUE);
1509 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1510 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_DOS_IS_ARCHIVE, TRUE);
1512 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1513 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_DOS_IS_SYSTEM, TRUE);
1518 g_file_attribute_matcher_matches (attribute_matcher,
1519 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET))
1521 char *link = read_link (path);
1522 g_file_info_set_symlink_target (info, link);
1527 if (g_file_attribute_matcher_matches (attribute_matcher,
1528 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME))
1530 char *display_name = g_filename_display_basename (path);
1532 /* look for U+FFFD REPLACEMENT CHARACTER */
1533 if (strstr (display_name, "\357\277\275") != NULL)
1535 char *p = display_name;
1536 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1539 g_file_info_set_display_name (info, display_name);
1540 g_free (display_name);
1543 if (g_file_attribute_matcher_matches (attribute_matcher,
1544 G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME))
1546 char *edit_name = g_filename_display_basename (path);
1547 g_file_info_set_edit_name (info, edit_name);
1552 if (g_file_attribute_matcher_matches (attribute_matcher,
1553 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME))
1555 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1557 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME, copy_name);
1561 if (g_file_attribute_matcher_matches (attribute_matcher,
1562 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE) ||
1563 g_file_attribute_matcher_matches (attribute_matcher,
1564 G_FILE_ATTRIBUTE_STANDARD_ICON))
1566 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1570 g_file_info_set_content_type (info, content_type);
1572 if (g_file_attribute_matcher_matches (attribute_matcher,
1573 G_FILE_ATTRIBUTE_STANDARD_ICON))
1577 if (strcmp (path, g_get_home_dir ()) == 0)
1578 icon = g_themed_icon_new ("user-home");
1579 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1580 icon = g_themed_icon_new ("user-desktop");
1583 icon = g_content_type_get_icon (content_type);
1584 if (G_IS_THEMED_ICON (icon))
1586 const char *type_icon = NULL;
1588 if (S_ISDIR (statbuf.st_mode))
1589 type_icon = "folder";
1591 g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
1597 g_file_info_set_icon (info, icon);
1598 g_object_unref (icon);
1602 g_free (content_type);
1606 if (g_file_attribute_matcher_matches (attribute_matcher,
1607 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE))
1609 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1613 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE, content_type);
1614 g_free (content_type);
1618 if (g_file_attribute_matcher_matches (attribute_matcher,
1619 G_FILE_ATTRIBUTE_OWNER_USER))
1624 win32_get_file_user_info (path, NULL, &name, NULL);
1626 name = get_username_from_uid (statbuf.st_uid);
1629 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1633 if (g_file_attribute_matcher_matches (attribute_matcher,
1634 G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1638 win32_get_file_user_info (path, NULL, NULL, &name);
1640 name = get_realname_from_uid (statbuf.st_uid);
1643 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1647 if (g_file_attribute_matcher_matches (attribute_matcher,
1648 G_FILE_ATTRIBUTE_OWNER_GROUP))
1652 win32_get_file_user_info (path, &name, NULL, NULL);
1654 name = get_groupname_from_gid (statbuf.st_gid);
1657 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1661 if (parent_info && parent_info->device != 0 &&
1662 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1663 statbuf.st_dev != parent_info->device)
1664 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1666 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1669 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1671 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1672 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1674 if (g_file_attribute_matcher_matches (attribute_matcher,
1675 G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1676 get_thumbnail_attributes (path, info);
1678 g_file_info_unset_attribute_mask (info);
1684 _g_local_file_info_get_from_fd (int fd,
1688 GLocalFileStat stat_buf;
1689 GFileAttributeMatcher *matcher;
1693 #define FSTAT _fstati64
1698 if (FSTAT (fd, &stat_buf) == -1)
1702 g_set_error (error, G_IO_ERROR,
1703 g_io_error_from_errno (errsv),
1704 _("Error stating file descriptor: %s"),
1705 g_strerror (errsv));
1709 info = g_file_info_new ();
1711 matcher = g_file_attribute_matcher_new (attributes);
1713 /* Make sure we don't set any unwanted attributes */
1714 g_file_info_set_attribute_mask (info, matcher);
1716 set_info_from_stat (info, &stat_buf, matcher);
1719 if (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) &&
1720 is_selinux_enabled ())
1723 if (fgetfilecon_raw (fd, &context) >= 0)
1725 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT, context);
1731 get_xattrs_from_fd (fd, TRUE, info, matcher);
1732 get_xattrs_from_fd (fd, FALSE, info, matcher);
1734 g_file_attribute_matcher_unref (matcher);
1736 g_file_info_unset_attribute_mask (info);
1742 get_uint32 (const GFileAttributeValue *value,
1746 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1748 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1749 _("Invalid attribute type (uint32 expected)"));
1753 *val_out = value->u.uint32;
1760 get_uint64 (const GFileAttributeValue *value,
1764 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1766 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1767 _("Invalid attribute type (uint64 expected)"));
1771 *val_out = value->u.uint64;
1777 #if defined(HAVE_SYMLINK)
1779 get_byte_string (const GFileAttributeValue *value,
1780 const char **val_out,
1783 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1785 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1786 _("Invalid attribute type (byte string expected)"));
1790 *val_out = value->u.string;
1798 get_string (const GFileAttributeValue *value,
1799 const char **val_out,
1802 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
1804 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1805 _("Invalid attribute type (byte string expected)"));
1809 *val_out = value->u.string;
1816 set_unix_mode (char *filename,
1817 const GFileAttributeValue *value,
1822 if (!get_uint32 (value, &val, error))
1825 if (g_chmod (filename, val) == -1)
1829 g_set_error (error, G_IO_ERROR,
1830 g_io_error_from_errno (errsv),
1831 _("Error setting permissions: %s"),
1832 g_strerror (errsv));
1840 set_unix_uid_gid (char *filename,
1841 const GFileAttributeValue *uid_value,
1842 const GFileAttributeValue *gid_value,
1843 GFileQueryInfoFlags flags,
1853 if (!get_uint32 (uid_value, &val, error))
1862 if (!get_uint32 (gid_value, &val, error))
1870 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1871 res = lchown (filename, uid, gid);
1874 res = chown (filename, uid, gid);
1880 g_set_error (error, G_IO_ERROR,
1881 g_io_error_from_errno (errsv),
1882 _("Error setting owner: %s"),
1883 g_strerror (errsv));
1892 set_symlink (char *filename,
1893 const GFileAttributeValue *value,
1897 struct stat statbuf;
1899 if (!get_byte_string (value, &val, error))
1904 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1905 _("symlink must be non-NULL"));
1909 if (g_lstat (filename, &statbuf))
1913 g_set_error (error, G_IO_ERROR,
1914 g_io_error_from_errno (errsv),
1915 _("Error setting symlink: %s"),
1916 g_strerror (errsv));
1920 if (!S_ISLNK (statbuf.st_mode))
1922 g_set_error_literal (error, G_IO_ERROR,
1923 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1924 _("Error setting symlink: file is not a symlink"));
1928 if (g_unlink (filename))
1932 g_set_error (error, G_IO_ERROR,
1933 g_io_error_from_errno (errsv),
1934 _("Error setting symlink: %s"),
1935 g_strerror (errsv));
1939 if (symlink (filename, val) != 0)
1943 g_set_error (error, G_IO_ERROR,
1944 g_io_error_from_errno (errsv),
1945 _("Error setting symlink: %s"),
1946 g_strerror (errsv));
1956 lazy_stat (char *filename,
1957 struct stat *statbuf,
1958 gboolean *called_stat)
1965 res = g_stat (filename, statbuf);
1968 *called_stat = TRUE;
1975 set_mtime_atime (char *filename,
1976 const GFileAttributeValue *mtime_value,
1977 const GFileAttributeValue *mtime_usec_value,
1978 const GFileAttributeValue *atime_value,
1979 const GFileAttributeValue *atime_usec_value,
1985 struct stat statbuf;
1986 gboolean got_stat = FALSE;
1987 struct timeval times[2] = { {0, 0}, {0, 0} };
1992 if (!get_uint64 (atime_value, &val, error))
1994 times[0].tv_sec = val;
1998 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2000 times[0].tv_sec = statbuf.st_mtime;
2001 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2002 times[0].tv_usec = statbuf.st_atimensec / 1000;
2003 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2004 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2009 if (atime_usec_value)
2011 if (!get_uint32 (atime_usec_value, &val_usec, error))
2013 times[0].tv_usec = val_usec;
2019 if (!get_uint64 (mtime_value, &val, error))
2021 times[1].tv_sec = val;
2025 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2027 times[1].tv_sec = statbuf.st_mtime;
2028 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2029 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2030 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2031 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2036 if (mtime_usec_value)
2038 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2040 times[1].tv_usec = val_usec;
2043 res = utimes (filename, times);
2048 g_set_error (error, G_IO_ERROR,
2049 g_io_error_from_errno (errsv),
2050 _("Error setting owner: %s"),
2051 g_strerror (errsv));
2061 set_selinux_context (char *filename,
2062 const GFileAttributeValue *value,
2067 if (!get_string (value, &val, error))
2072 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2073 _("SELinux context must be non-NULL"));
2077 if (is_selinux_enabled ()) {
2078 security_context_t val_s;
2080 val_s = g_strdup (val);
2082 if (setfilecon_raw (filename, val_s) < 0)
2086 g_set_error (error, G_IO_ERROR,
2087 g_io_error_from_errno (errsv),
2088 _("Error setting SELinux context: %s"),
2089 g_strerror (errsv));
2094 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2095 _("SELinux is not enabled on this system"));
2105 _g_local_file_info_set_attribute (char *filename,
2106 const char *attribute,
2107 GFileAttributeType type,
2109 GFileQueryInfoFlags flags,
2110 GCancellable *cancellable,
2113 GFileAttributeValue value = { 0 };
2115 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2117 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2118 return set_unix_mode (filename, &value, error);
2121 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2122 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2123 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2124 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2128 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2129 return set_symlink (filename, &value, error);
2133 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2134 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2135 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2136 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2137 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2138 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2139 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2140 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2144 else if (g_str_has_prefix (attribute, "xattr::"))
2145 return set_xattr (filename, attribute, &value, error);
2146 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2147 return set_xattr (filename, attribute, &value, error);
2151 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2152 return set_selinux_context (filename, &value, error);
2155 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2156 _("Setting attribute %s not supported"), attribute);
2161 _g_local_file_info_set_attributes (char *filename,
2163 GFileQueryInfoFlags flags,
2164 GCancellable *cancellable,
2167 GFileAttributeValue *value;
2169 GFileAttributeValue *uid, *gid;
2172 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2174 #if defined (HAVE_CHOWN) && defined (HAVE_UTIMES)
2175 GFileAttributeStatus status;
2179 /* Handles setting multiple specified data in a single set, and takes care
2180 of ordering restrictions when setting attributes */
2184 /* Set symlink first, since this recreates the file */
2186 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2189 if (!set_symlink (filename, value, error))
2191 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2193 /* Don't set error multiple times */
2197 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2203 /* Group uid and gid setting into one call
2204 * Change ownership before permissions, since ownership changes can
2205 change permissions (e.g. setuid)
2207 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2208 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2212 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2214 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2216 /* Don't set error multiple times */
2220 status = G_FILE_ATTRIBUTE_STATUS_SET;
2222 uid->status = status;
2224 gid->status = status;
2228 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2231 if (!set_unix_mode (filename, value, error))
2233 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2235 /* Don't set error multiple times */
2239 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2244 /* Group all time settings into one call
2245 * Change times as the last thing to avoid it changing due to metadata changes
2248 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2249 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2250 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2251 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2253 if (mtime || mtime_usec || atime || atime_usec)
2255 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2257 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2259 /* Don't set error multiple times */
2263 status = G_FILE_ATTRIBUTE_STATUS_SET;
2266 mtime->status = status;
2268 mtime_usec->status = status;
2270 atime->status = status;
2272 atime_usec->status = status;
2276 /* xattrs are handled by default callback */
2279 /* SELinux context */
2281 if (is_selinux_enabled ()) {
2282 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2285 if (!set_selinux_context (filename, value, error))
2287 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2289 /* Don't set error multiple times */
2293 value->status = G_FILE_ATTRIBUTE_STATUS_SET;