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.1 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, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
27 #ifdef HAVE_SYS_TIME_H
30 #include <sys/types.h>
40 #include <selinux/selinux.h>
45 #if defined HAVE_SYS_XATTR_H
46 #include <sys/xattr.h>
47 #elif defined HAVE_ATTR_XATTR_H
48 #include <attr/xattr.h>
50 #error "Neither <sys/xattr.h> nor <attr/xattr.h> is present but extended attribute support is enabled."
51 #endif /* defined HAVE_SYS_XATTR_H || HAVE_ATTR_XATTR_H */
53 #endif /* HAVE_XATTR */
55 #include <glib/gstdio.h>
56 #include <glib/gstdioprivate.h>
57 #include <gfileattribute-priv.h>
58 #include <gfileinfo-priv.h>
63 #include "glib-unix.h"
66 #include "glib-private.h"
68 #include "thumbnail-verify.h"
80 #define X_OK 0 /* not really */
83 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
86 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
89 #define S_IXUSR _S_IEXEC
93 #include "glocalfileinfo.h"
95 #include "gthemedicon.h"
96 #include "gcontenttypeprivate.h"
100 struct ThumbMD5Context
{
103 unsigned char in
[64];
113 G_LOCK_DEFINE_STATIC (uid_cache
);
114 static GHashTable
*uid_cache
= NULL
;
116 G_LOCK_DEFINE_STATIC (gid_cache
);
117 static GHashTable
*gid_cache
= NULL
;
119 #endif /* !G_OS_WIN32 */
122 _g_local_file_info_create_etag (GLocalFileStat
*statbuf
)
126 sec
= statbuf
->st_mtime
;
127 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
128 usec
= statbuf
->st_mtimensec
/ 1000;
129 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
130 usec
= statbuf
->st_mtim
.tv_nsec
/ 1000;
135 return g_strdup_printf ("%lu:%lu", sec
, usec
);
139 _g_local_file_info_create_file_id (GLocalFileStat
*statbuf
)
143 ino
= statbuf
->file_index
;
145 ino
= statbuf
->st_ino
;
147 return g_strdup_printf ("l%" G_GUINT64_FORMAT
":%" G_GUINT64_FORMAT
,
148 (guint64
) statbuf
->st_dev
,
153 _g_local_file_info_create_fs_id (GLocalFileStat
*statbuf
)
155 return g_strdup_printf ("l%" G_GUINT64_FORMAT
,
156 (guint64
) statbuf
->st_dev
);
159 #if defined (S_ISLNK) || defined (G_OS_WIN32)
162 read_link (const gchar
*full_name
)
164 #if defined (HAVE_READLINK) || defined (G_OS_WIN32)
169 buffer
= g_malloc (size
);
176 read_size
= readlink (full_name
, buffer
, size
);
178 read_size
= GLIB_PRIVATE_CALL (g_win32_readlink_utf8
) (full_name
, buffer
, size
);
185 if (read_size
< size
)
187 buffer
[read_size
] = 0;
191 buffer
= g_realloc (buffer
, size
);
198 #endif /* S_ISLNK || G_OS_WIN32 */
201 /* Get the SELinux security context */
203 get_selinux_context (const char *path
,
205 GFileAttributeMatcher
*attribute_matcher
,
206 gboolean follow_symlinks
)
210 if (!_g_file_attribute_matcher_matches_id (attribute_matcher
, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT
))
213 if (is_selinux_enabled ())
217 if (lgetfilecon_raw (path
, &context
) < 0)
222 if (getfilecon_raw (path
, &context
) < 0)
228 _g_file_info_set_attribute_string_by_id (info
, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT
, context
);
237 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
238 * (Mac) getxattr(..., XATTR_NOFOLLOW)
240 #ifdef HAVE_XATTR_NOFOLLOW
241 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
242 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
243 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
245 #define g_fgetxattr fgetxattr
246 #define g_flistxattr flistxattr
247 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
251 g_getxattr (const char *path
, const char *name
, void *value
, size_t size
,
252 gboolean follow_symlinks
)
254 #ifdef HAVE_XATTR_NOFOLLOW
255 return getxattr (path
, name
, value
, size
, 0, follow_symlinks
? 0 : XATTR_NOFOLLOW
);
258 return getxattr (path
, name
, value
, size
);
260 return lgetxattr (path
, name
, value
, size
);
265 g_listxattr(const char *path
, char *namebuf
, size_t size
,
266 gboolean follow_symlinks
)
268 #ifdef HAVE_XATTR_NOFOLLOW
269 return listxattr (path
, namebuf
, size
, follow_symlinks
? 0 : XATTR_NOFOLLOW
);
272 return listxattr (path
, namebuf
, size
);
274 return llistxattr (path
, namebuf
, size
);
281 return c
>= 32 && c
<= 126 && c
!= '\\';
285 name_is_valid (const char *str
)
289 if (!valid_char (*str
++))
296 hex_escape_string (const char *str
,
297 gboolean
*free_return
)
300 char *escaped_str
, *p
;
302 static char *hex_digits
= "0123456789abcdef";
308 for (i
= 0; i
< len
; i
++)
310 if (!valid_char (str
[i
]))
314 if (num_invalid
== 0)
316 *free_return
= FALSE
;
320 escaped_str
= g_malloc (len
+ num_invalid
*3 + 1);
323 for (i
= 0; i
< len
; i
++)
325 if (valid_char (str
[i
]))
332 *p
++ = hex_digits
[(c
>> 4) & 0xf];
333 *p
++ = hex_digits
[c
& 0xf];
343 hex_unescape_string (const char *str
,
345 gboolean
*free_return
)
348 char *unescaped_str
, *p
;
354 if (strchr (str
, '\\') == NULL
)
358 *free_return
= FALSE
;
362 unescaped_str
= g_malloc (len
+ 1);
365 for (i
= 0; i
< len
; i
++)
367 if (str
[i
] == '\\' &&
372 (g_ascii_xdigit_value (str
[i
+2]) << 4) |
373 g_ascii_xdigit_value (str
[i
+3]);
383 *out_len
= p
- unescaped_str
;
385 return unescaped_str
;
389 escape_xattr (GFileInfo
*info
,
390 const char *gio_attr
, /* gio attribute name */
391 const char *value
, /* Is zero terminated */
392 size_t len
/* not including zero termination */)
395 gboolean free_escaped_val
;
397 escaped_val
= hex_escape_string (value
, &free_escaped_val
);
399 g_file_info_set_attribute_string (info
, gio_attr
, escaped_val
);
401 if (free_escaped_val
)
402 g_free (escaped_val
);
406 get_one_xattr (const char *path
,
408 const char *gio_attr
,
410 gboolean follow_symlinks
)
417 len
= g_getxattr (path
, xattr
, value
, sizeof (value
)-1, follow_symlinks
);
423 else if (len
== -1 && errsv
== ERANGE
)
425 len
= g_getxattr (path
, xattr
, NULL
, 0, follow_symlinks
);
430 value_p
= g_malloc (len
+1);
432 len
= g_getxattr (path
, xattr
, value_p
, len
, follow_symlinks
);
446 escape_xattr (info
, gio_attr
, value_p
, len
);
448 if (value_p
!= value
)
452 #endif /* defined HAVE_XATTR */
455 get_xattrs (const char *path
,
458 GFileAttributeMatcher
*matcher
,
459 gboolean follow_symlinks
)
464 gssize list_res_size
;
467 const char *attr
, *attr2
;
470 all
= g_file_attribute_matcher_enumerate_namespace (matcher
, "xattr");
472 all
= g_file_attribute_matcher_enumerate_namespace (matcher
, "xattr-sys");
478 list_res_size
= g_listxattr (path
, NULL
, 0, follow_symlinks
);
480 if (list_res_size
== -1 ||
484 list_size
= list_res_size
;
485 list
= g_malloc (list_size
);
489 list_res_size
= g_listxattr (path
, list
, list_size
, follow_symlinks
);
492 if (list_res_size
== -1 && errsv
== ERANGE
)
494 list_size
= list_size
* 2;
495 list
= g_realloc (list
, list_size
);
499 if (list_res_size
== -1)
503 while (list_res_size
> 0)
505 if ((user
&& g_str_has_prefix (attr
, "user.")) ||
506 (!user
&& !g_str_has_prefix (attr
, "user.")))
508 char *escaped_attr
, *gio_attr
;
509 gboolean free_escaped_attr
;
513 escaped_attr
= hex_escape_string (attr
+ 5, &free_escaped_attr
);
514 gio_attr
= g_strconcat ("xattr::", escaped_attr
, NULL
);
518 escaped_attr
= hex_escape_string (attr
, &free_escaped_attr
);
519 gio_attr
= g_strconcat ("xattr-sys::", escaped_attr
, NULL
);
522 if (free_escaped_attr
)
523 g_free (escaped_attr
);
525 get_one_xattr (path
, info
, gio_attr
, attr
, follow_symlinks
);
530 len
= strlen (attr
) + 1;
532 list_res_size
-= len
;
539 while ((attr
= g_file_attribute_matcher_enumerate_next (matcher
)) != NULL
)
541 char *unescaped_attribute
, *a
;
542 gboolean free_unescaped_attribute
;
544 attr2
= strchr (attr
, ':');
547 attr2
+= 2; /* Skip '::' */
548 unescaped_attribute
= hex_unescape_string (attr2
, NULL
, &free_unescaped_attribute
);
550 a
= g_strconcat ("user.", unescaped_attribute
, NULL
);
552 a
= unescaped_attribute
;
554 get_one_xattr (path
, info
, attr
, a
, follow_symlinks
);
559 if (free_unescaped_attribute
)
560 g_free (unescaped_attribute
);
564 #endif /* defined HAVE_XATTR */
569 get_one_xattr_from_fd (int fd
,
571 const char *gio_attr
,
579 len
= g_fgetxattr (fd
, xattr
, value
, sizeof (value
) - 1);
585 else if (len
== -1 && errsv
== ERANGE
)
587 len
= g_fgetxattr (fd
, xattr
, NULL
, 0);
592 value_p
= g_malloc (len
+ 1);
594 len
= g_fgetxattr (fd
, xattr
, value_p
, len
);
608 escape_xattr (info
, gio_attr
, value_p
, len
);
610 if (value_p
!= value
)
613 #endif /* defined HAVE_XATTR */
616 get_xattrs_from_fd (int fd
,
619 GFileAttributeMatcher
*matcher
)
624 gssize list_res_size
;
627 const char *attr
, *attr2
;
630 all
= g_file_attribute_matcher_enumerate_namespace (matcher
, "xattr");
632 all
= g_file_attribute_matcher_enumerate_namespace (matcher
, "xattr-sys");
638 list_res_size
= g_flistxattr (fd
, NULL
, 0);
640 if (list_res_size
== -1 ||
644 list_size
= list_res_size
;
645 list
= g_malloc (list_size
);
649 list_res_size
= g_flistxattr (fd
, list
, list_size
);
652 if (list_res_size
== -1 && errsv
== ERANGE
)
654 list_size
= list_size
* 2;
655 list
= g_realloc (list
, list_size
);
659 if (list_res_size
== -1)
666 while (list_res_size
> 0)
668 if ((user
&& g_str_has_prefix (attr
, "user.")) ||
669 (!user
&& !g_str_has_prefix (attr
, "user.")))
671 char *escaped_attr
, *gio_attr
;
672 gboolean free_escaped_attr
;
676 escaped_attr
= hex_escape_string (attr
+ 5, &free_escaped_attr
);
677 gio_attr
= g_strconcat ("xattr::", escaped_attr
, NULL
);
681 escaped_attr
= hex_escape_string (attr
, &free_escaped_attr
);
682 gio_attr
= g_strconcat ("xattr-sys::", escaped_attr
, NULL
);
685 if (free_escaped_attr
)
686 g_free (escaped_attr
);
688 get_one_xattr_from_fd (fd
, info
, gio_attr
, attr
);
692 len
= strlen (attr
) + 1;
694 list_res_size
-= len
;
701 while ((attr
= g_file_attribute_matcher_enumerate_next (matcher
)) != NULL
)
703 char *unescaped_attribute
, *a
;
704 gboolean free_unescaped_attribute
;
706 attr2
= strchr (attr
, ':');
709 attr2
++; /* Skip ':' */
710 unescaped_attribute
= hex_unescape_string (attr2
, NULL
, &free_unescaped_attribute
);
712 a
= g_strconcat ("user.", unescaped_attribute
, NULL
);
714 a
= unescaped_attribute
;
716 get_one_xattr_from_fd (fd
, info
, attr
, a
);
721 if (free_unescaped_attribute
)
722 g_free (unescaped_attribute
);
726 #endif /* defined HAVE_XATTR */
731 set_xattr (char *filename
,
732 const char *escaped_attribute
,
733 const GFileAttributeValue
*attr_value
,
736 char *attribute
, *value
;
737 gboolean free_attribute
, free_value
;
738 int val_len
, res
, errsv
;
742 if (attr_value
== NULL
)
744 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
745 _("Attribute value must be non-NULL"));
749 if (attr_value
->type
!= G_FILE_ATTRIBUTE_TYPE_STRING
)
751 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
752 _("Invalid attribute type (string expected)"));
756 if (!name_is_valid (escaped_attribute
))
758 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
759 _("Invalid extended attribute name"));
763 if (g_str_has_prefix (escaped_attribute
, "xattr::"))
765 escaped_attribute
+= strlen ("xattr::");
770 g_warn_if_fail (g_str_has_prefix (escaped_attribute
, "xattr-sys::"));
771 escaped_attribute
+= strlen ("xattr-sys::");
775 attribute
= hex_unescape_string (escaped_attribute
, NULL
, &free_attribute
);
776 value
= hex_unescape_string (attr_value
->u
.string
, &val_len
, &free_value
);
779 a
= g_strconcat ("user.", attribute
, NULL
);
783 res
= g_setxattr (filename
, a
, value
, val_len
);
797 g_set_error (error
, G_IO_ERROR
,
798 g_io_error_from_errno (errsv
),
799 _("Error setting extended attribute “%s”: %s"),
800 escaped_attribute
, g_strerror (errsv
));
811 _g_local_file_info_get_parent_info (const char *dir
,
812 GFileAttributeMatcher
*attribute_matcher
,
813 GLocalParentFileInfo
*parent_info
)
818 parent_info
->extra_data
= NULL
;
819 parent_info
->free_extra_data
= NULL
;
820 parent_info
->writable
= FALSE
;
821 parent_info
->is_sticky
= FALSE
;
822 parent_info
->has_trash_dir
= FALSE
;
823 parent_info
->device
= 0;
824 parent_info
->inode
= 0;
826 if (_g_file_attribute_matcher_matches_id (attribute_matcher
, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME
) ||
827 _g_file_attribute_matcher_matches_id (attribute_matcher
, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE
) ||
828 _g_file_attribute_matcher_matches_id (attribute_matcher
, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH
) ||
829 _g_file_attribute_matcher_matches_id (attribute_matcher
, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT
))
831 /* FIXME: Windows: The underlying _waccess() call in the C
832 * library is mostly pointless as it only looks at the READONLY
833 * FAT-style attribute of the file, it doesn't check the ACL at
836 parent_info
->writable
= (g_access (dir
, W_OK
) == 0);
838 res
= g_stat (dir
, &statbuf
);
841 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
842 * renamed or deleted only by the owner of the file, by the owner of the directory, and
843 * by a privileged process.
848 parent_info
->is_sticky
= (statbuf
.st_mode
& S_ISVTX
) != 0;
850 parent_info
->is_sticky
= FALSE
;
852 parent_info
->owner
= statbuf
.st_uid
;
853 parent_info
->device
= statbuf
.st_dev
;
854 parent_info
->inode
= statbuf
.st_ino
;
855 /* No need to find trash dir if it's not writable anyway */
856 if (parent_info
->writable
&&
857 _g_file_attribute_matcher_matches_id (attribute_matcher
, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH
))
858 parent_info
->has_trash_dir
= _g_local_file_has_trash_dir (dir
, statbuf
.st_dev
);
864 _g_local_file_info_free_parent_info (GLocalParentFileInfo
*parent_info
)
866 if (parent_info
->extra_data
&&
867 parent_info
->free_extra_data
)
868 parent_info
->free_extra_data (parent_info
->extra_data
);
872 get_access_rights (GFileAttributeMatcher
*attribute_matcher
,
875 GLocalFileStat
*statbuf
,
876 GLocalParentFileInfo
*parent_info
)
878 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
879 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
880 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ
))
881 _g_file_info_set_attribute_boolean_by_id (info
, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ
,
882 g_access (path
, R_OK
) == 0);
884 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
885 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE
))
886 _g_file_info_set_attribute_boolean_by_id (info
, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE
,
887 g_access (path
, W_OK
) == 0);
889 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
890 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE
))
891 _g_file_info_set_attribute_boolean_by_id (info
, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE
,
892 g_access (path
, X_OK
) == 0);
900 if (parent_info
->writable
)
905 if (parent_info
->is_sticky
)
907 uid_t uid
= geteuid ();
909 if (uid
== statbuf
->st_uid
||
910 uid
== parent_info
->owner
||
919 if (_g_file_attribute_matcher_matches_id (attribute_matcher
, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME
))
920 _g_file_info_set_attribute_boolean_by_id (info
, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME
,
923 if (_g_file_attribute_matcher_matches_id (attribute_matcher
, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE
))
924 _g_file_info_set_attribute_boolean_by_id (info
, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE
,
927 /* Trashing is supported only if the parent device is the same */
928 if (_g_file_attribute_matcher_matches_id (attribute_matcher
, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH
))
929 _g_file_info_set_attribute_boolean_by_id (info
,
930 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH
,
932 parent_info
->has_trash_dir
&&
933 parent_info
->device
== statbuf
->st_dev
);
938 set_info_from_stat (GFileInfo
*info
,
939 GLocalFileStat
*statbuf
,
940 GFileAttributeMatcher
*attribute_matcher
)
944 file_type
= G_FILE_TYPE_UNKNOWN
;
946 if (S_ISREG (statbuf
->st_mode
))
947 file_type
= G_FILE_TYPE_REGULAR
;
948 else if (S_ISDIR (statbuf
->st_mode
))
949 file_type
= G_FILE_TYPE_DIRECTORY
;
951 else if (S_ISCHR (statbuf
->st_mode
) ||
952 S_ISBLK (statbuf
->st_mode
) ||
953 S_ISFIFO (statbuf
->st_mode
)
955 || S_ISSOCK (statbuf
->st_mode
)
958 file_type
= G_FILE_TYPE_SPECIAL
;
961 else if (S_ISLNK (statbuf
->st_mode
))
962 file_type
= G_FILE_TYPE_SYMBOLIC_LINK
;
963 #elif defined (G_OS_WIN32)
964 if (statbuf
->reparse_tag
== IO_REPARSE_TAG_SYMLINK
)
965 file_type
= G_FILE_TYPE_SYMBOLIC_LINK
;
968 g_file_info_set_file_type (info
, file_type
);
969 g_file_info_set_size (info
, statbuf
->st_size
);
971 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE
, statbuf
->st_dev
);
973 /* Pointless setting these on Windows even if they exist in the struct */
974 _g_file_info_set_attribute_uint64_by_id (info
, G_FILE_ATTRIBUTE_ID_UNIX_INODE
, statbuf
->st_ino
);
975 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_UNIX_NLINK
, statbuf
->st_nlink
);
976 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_UNIX_UID
, statbuf
->st_uid
);
977 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_UNIX_GID
, statbuf
->st_gid
);
978 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_UNIX_RDEV
, statbuf
->st_rdev
);
980 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
981 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_UNIX_MODE
, statbuf
->st_mode
);
982 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
983 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE
, statbuf
->st_blksize
);
985 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
986 _g_file_info_set_attribute_uint64_by_id (info
, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS
, statbuf
->st_blocks
);
987 _g_file_info_set_attribute_uint64_by_id (info
, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE
,
988 statbuf
->st_blocks
* G_GUINT64_CONSTANT (512));
989 #elif defined (G_OS_WIN32)
990 _g_file_info_set_attribute_uint64_by_id (info
, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE
,
991 statbuf
->allocated_size
);
995 _g_file_info_set_attribute_uint64_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED
, statbuf
->st_mtime
);
996 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
997 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC
, statbuf
->st_mtimensec
/ 1000);
998 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
999 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC
, statbuf
->st_mtim
.tv_nsec
/ 1000);
1002 _g_file_info_set_attribute_uint64_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_ACCESS
, statbuf
->st_atime
);
1003 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1004 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC
, statbuf
->st_atimensec
/ 1000);
1005 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1006 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC
, statbuf
->st_atim
.tv_nsec
/ 1000);
1009 _g_file_info_set_attribute_uint64_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_CHANGED
, statbuf
->st_ctime
);
1010 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
1011 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC
, statbuf
->st_ctimensec
/ 1000);
1012 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
1013 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC
, statbuf
->st_ctim
.tv_nsec
/ 1000);
1016 #if defined (HAVE_STRUCT_STAT_ST_BIRTHTIME) && defined (HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
1017 _g_file_info_set_attribute_uint64_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_CREATED
, statbuf
->st_birthtime
);
1018 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_CREATED_USEC
, statbuf
->st_birthtimensec
/ 1000);
1019 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIM) && defined (HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
1020 _g_file_info_set_attribute_uint64_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_CREATED
, statbuf
->st_birthtim
.tv_sec
);
1021 _g_file_info_set_attribute_uint32_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_CREATED_USEC
, statbuf
->st_birthtim
.tv_nsec
/ 1000);
1022 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIME)
1023 _g_file_info_set_attribute_uint64_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_CREATED
, statbuf
->st_birthtime
);
1024 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIM)
1025 _g_file_info_set_attribute_uint64_by_id (info
, G_FILE_ATTRIBUTE_ID_TIME_CREATED
, statbuf
->st_birthtim
);
1028 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1029 G_FILE_ATTRIBUTE_ID_ETAG_VALUE
))
1031 char *etag
= _g_local_file_info_create_etag (statbuf
);
1032 _g_file_info_set_attribute_string_by_id (info
, G_FILE_ATTRIBUTE_ID_ETAG_VALUE
, etag
);
1036 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1037 G_FILE_ATTRIBUTE_ID_ID_FILE
))
1039 char *id
= _g_local_file_info_create_file_id (statbuf
);
1040 _g_file_info_set_attribute_string_by_id (info
, G_FILE_ATTRIBUTE_ID_ID_FILE
, id
);
1044 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1045 G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM
))
1047 char *id
= _g_local_file_info_create_fs_id (statbuf
);
1048 _g_file_info_set_attribute_string_by_id (info
, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM
, id
);
1056 make_valid_utf8 (const char *name
)
1059 const gchar
*remainder
, *invalid
;
1060 gint remaining_bytes
, valid_bytes
;
1064 remaining_bytes
= strlen (name
);
1066 while (remaining_bytes
!= 0)
1068 if (g_utf8_validate (remainder
, remaining_bytes
, &invalid
))
1070 valid_bytes
= invalid
- remainder
;
1073 string
= g_string_sized_new (remaining_bytes
);
1075 g_string_append_len (string
, remainder
, valid_bytes
);
1076 /* append U+FFFD REPLACEMENT CHARACTER */
1077 g_string_append (string
, "\357\277\275");
1079 remaining_bytes
-= valid_bytes
+ 1;
1080 remainder
= invalid
+ 1;
1084 return g_strdup (name
);
1086 g_string_append (string
, remainder
);
1088 g_warn_if_fail (g_utf8_validate (string
->str
, -1, NULL
));
1090 return g_string_free (string
, FALSE
);
1094 convert_pwd_string_to_utf8 (char *pwd_str
)
1098 if (!g_utf8_validate (pwd_str
, -1, NULL
))
1100 utf8_string
= g_locale_to_utf8 (pwd_str
, -1, NULL
, NULL
, NULL
);
1101 if (utf8_string
== NULL
)
1102 utf8_string
= make_valid_utf8 (pwd_str
);
1105 utf8_string
= g_strdup (pwd_str
);
1111 uid_data_free (UidData
*data
)
1113 g_free (data
->user_name
);
1114 g_free (data
->real_name
);
1118 /* called with lock held */
1120 lookup_uid_data (uid_t uid
)
1124 struct passwd pwbuf
;
1125 struct passwd
*pwbufp
;
1127 char *gecos
, *comma
;
1130 if (uid_cache
== NULL
)
1131 uid_cache
= g_hash_table_new_full (NULL
, NULL
, NULL
, (GDestroyNotify
)uid_data_free
);
1133 data
= g_hash_table_lookup (uid_cache
, GINT_TO_POINTER (uid
));
1138 data
= g_new0 (UidData
, 1);
1140 #if defined(HAVE_GETPWUID_R)
1141 getpwuid_r (uid
, &pwbuf
, buffer
, sizeof(buffer
), &pwbufp
);
1143 pwbufp
= getpwuid (uid
);
1148 if (pwbufp
->pw_name
!= NULL
&& pwbufp
->pw_name
[0] != 0)
1149 data
->user_name
= convert_pwd_string_to_utf8 (pwbufp
->pw_name
);
1152 gecos
= pwbufp
->pw_gecos
;
1156 comma
= strchr (gecos
, ',');
1159 data
->real_name
= convert_pwd_string_to_utf8 (gecos
);
1164 /* Default fallbacks */
1165 if (data
->real_name
== NULL
)
1167 if (data
->user_name
!= NULL
)
1168 data
->real_name
= g_strdup (data
->user_name
);
1170 data
->real_name
= g_strdup_printf ("user #%d", (int)uid
);
1173 if (data
->user_name
== NULL
)
1174 data
->user_name
= g_strdup_printf ("%d", (int)uid
);
1176 g_hash_table_replace (uid_cache
, GINT_TO_POINTER (uid
), data
);
1182 get_username_from_uid (uid_t uid
)
1188 data
= lookup_uid_data (uid
);
1189 res
= g_strdup (data
->user_name
);
1190 G_UNLOCK (uid_cache
);
1196 get_realname_from_uid (uid_t uid
)
1202 data
= lookup_uid_data (uid
);
1203 res
= g_strdup (data
->real_name
);
1204 G_UNLOCK (uid_cache
);
1209 /* called with lock held */
1211 lookup_gid_name (gid_t gid
)
1214 #if defined (HAVE_GETGRGID_R)
1218 struct group
*gbufp
;
1220 if (gid_cache
== NULL
)
1221 gid_cache
= g_hash_table_new_full (NULL
, NULL
, NULL
, (GDestroyNotify
)g_free
);
1223 name
= g_hash_table_lookup (gid_cache
, GINT_TO_POINTER (gid
));
1228 #if defined (HAVE_GETGRGID_R)
1229 getgrgid_r (gid
, &gbuf
, buffer
, sizeof(buffer
), &gbufp
);
1231 gbufp
= getgrgid (gid
);
1234 if (gbufp
!= NULL
&&
1235 gbufp
->gr_name
!= NULL
&&
1236 gbufp
->gr_name
[0] != 0)
1237 name
= convert_pwd_string_to_utf8 (gbufp
->gr_name
);
1239 name
= g_strdup_printf("%d", (int)gid
);
1241 g_hash_table_replace (gid_cache
, GINT_TO_POINTER (gid
), name
);
1247 get_groupname_from_gid (gid_t gid
)
1253 name
= lookup_gid_name (gid
);
1254 res
= g_strdup (name
);
1255 G_UNLOCK (gid_cache
);
1259 #endif /* !G_OS_WIN32 */
1262 get_content_type (const char *basename
,
1264 GLocalFileStat
*statbuf
,
1265 gboolean is_symlink
,
1266 gboolean symlink_broken
,
1267 GFileQueryInfoFlags flags
,
1271 (symlink_broken
|| (flags
& G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
)))
1272 return g_content_type_from_mime_type ("inode/symlink");
1273 else if (statbuf
!= NULL
&& S_ISDIR(statbuf
->st_mode
))
1274 return g_content_type_from_mime_type ("inode/directory");
1276 else if (statbuf
!= NULL
&& S_ISCHR(statbuf
->st_mode
))
1277 return g_content_type_from_mime_type ("inode/chardevice");
1278 else if (statbuf
!= NULL
&& S_ISBLK(statbuf
->st_mode
))
1279 return g_content_type_from_mime_type ("inode/blockdevice");
1280 else if (statbuf
!= NULL
&& S_ISFIFO(statbuf
->st_mode
))
1281 return g_content_type_from_mime_type ("inode/fifo");
1282 else if (statbuf
!= NULL
&& S_ISREG(statbuf
->st_mode
) && statbuf
->st_size
== 0)
1284 /* Don't sniff zero-length files in order to avoid reading files
1285 * that appear normal but are not (eg: files in /proc and /sys)
1287 * Note that we need to return text/plain here so that
1288 * newly-created text files are opened by the text editor.
1289 * See https://bugzilla.gnome.org/show_bug.cgi?id=755795
1291 return g_content_type_from_mime_type ("text/plain");
1295 else if (statbuf
!= NULL
&& S_ISSOCK(statbuf
->st_mode
))
1296 return g_content_type_from_mime_type ("inode/socket");
1301 gboolean result_uncertain
;
1303 content_type
= g_content_type_guess (basename
, NULL
, 0, &result_uncertain
);
1305 #if !defined(G_OS_WIN32) && !defined(HAVE_COCOA)
1306 if (!fast
&& result_uncertain
&& path
!= NULL
)
1308 guchar sniff_buffer
[4096];
1312 sniff_length
= _g_unix_content_type_get_sniff_len ();
1313 if (sniff_length
> 4096)
1314 sniff_length
= 4096;
1317 fd
= g_open (path
, O_RDONLY
| O_NOATIME
, 0);
1319 if (fd
< 0 && errsv
== EPERM
)
1321 fd
= g_open (path
, O_RDONLY
, 0);
1327 res
= read (fd
, sniff_buffer
, sniff_length
);
1328 (void) g_close (fd
, NULL
);
1331 g_free (content_type
);
1332 content_type
= g_content_type_guess (basename
, sniff_buffer
, res
, NULL
);
1338 return content_type
;
1343 /* @stat_buf is the pre-calculated result of stat(path), or %NULL if that failed. */
1345 get_thumbnail_attributes (const char *path
,
1347 const GLocalFileStat
*stat_buf
)
1349 GChecksum
*checksum
;
1354 uri
= g_filename_to_uri (path
, NULL
, NULL
);
1356 checksum
= g_checksum_new (G_CHECKSUM_MD5
);
1357 g_checksum_update (checksum
, (const guchar
*) uri
, strlen (uri
));
1359 basename
= g_strconcat (g_checksum_get_string (checksum
), ".png", NULL
);
1360 g_checksum_free (checksum
);
1362 filename
= g_build_filename (g_get_user_cache_dir (),
1363 "thumbnails", "large", basename
,
1366 if (g_file_test (filename
, G_FILE_TEST_IS_REGULAR
))
1368 _g_file_info_set_attribute_byte_string_by_id (info
, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH
, filename
);
1369 _g_file_info_set_attribute_boolean_by_id (info
, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID
,
1370 thumbnail_verify (filename
, uri
, stat_buf
));
1375 filename
= g_build_filename (g_get_user_cache_dir (),
1376 "thumbnails", "normal", basename
,
1379 if (g_file_test (filename
, G_FILE_TEST_IS_REGULAR
))
1381 _g_file_info_set_attribute_byte_string_by_id (info
, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH
, filename
);
1382 _g_file_info_set_attribute_boolean_by_id (info
, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID
,
1383 thumbnail_verify (filename
, uri
, stat_buf
));
1388 filename
= g_build_filename (g_get_user_cache_dir (),
1389 "thumbnails", "fail",
1390 "gnome-thumbnail-factory",
1394 if (g_file_test (filename
, G_FILE_TEST_IS_REGULAR
))
1396 _g_file_info_set_attribute_boolean_by_id (info
, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED
, TRUE
);
1397 _g_file_info_set_attribute_boolean_by_id (info
, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID
,
1398 thumbnail_verify (filename
, uri
, stat_buf
));
1409 win32_get_file_user_info (const gchar
*filename
,
1414 PSECURITY_DESCRIPTOR psd
= NULL
;
1415 DWORD sd_size
= 0; /* first call calculates the size required */
1417 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
1418 if ((GetFileSecurityW (wfilename
,
1419 GROUP_SECURITY_INFORMATION
| OWNER_SECURITY_INFORMATION
,
1422 &sd_size
) || (ERROR_INSUFFICIENT_BUFFER
== GetLastError())) &&
1423 (psd
= g_try_malloc (sd_size
)) != NULL
&&
1424 GetFileSecurityW (wfilename
,
1425 GROUP_SECURITY_INFORMATION
| OWNER_SECURITY_INFORMATION
,
1432 SID_NAME_USE name_use
= 0; /* don't care? */
1433 wchar_t *name
= NULL
;
1434 wchar_t *domain
= NULL
;
1436 DWORD domain_len
= 0;
1437 /* get the user name */
1441 if (!GetSecurityDescriptorOwner (psd
, &psid
, &defaulted
))
1443 if (!LookupAccountSidW (NULL
, /* local machine */
1446 domain
, &domain_len
, /* no domain info yet */
1447 &name_use
) && (ERROR_INSUFFICIENT_BUFFER
!= GetLastError()))
1449 name
= g_try_malloc (name_len
* sizeof (wchar_t));
1450 domain
= g_try_malloc (domain_len
* sizeof (wchar_t));
1451 if (name
&& domain
&&
1452 LookupAccountSidW (NULL
, /* local machine */
1455 domain
, &domain_len
, /* no domain info yet */
1458 *user_name
= g_utf16_to_utf8 (name
, -1, NULL
, NULL
, NULL
);
1464 /* get the group name */
1468 if (!GetSecurityDescriptorGroup (psd
, &psid
, &defaulted
))
1470 if (!LookupAccountSidW (NULL
, /* local machine */
1473 domain
, &domain_len
, /* no domain info yet */
1474 &name_use
) && (ERROR_INSUFFICIENT_BUFFER
!= GetLastError()))
1476 name
= g_try_malloc (name_len
* sizeof (wchar_t));
1477 domain
= g_try_malloc (domain_len
* sizeof (wchar_t));
1478 if (name
&& domain
&&
1479 LookupAccountSidW (NULL
, /* local machine */
1482 domain
, &domain_len
, /* no domain info yet */
1485 *group_name
= g_utf16_to_utf8 (name
, -1, NULL
, NULL
, NULL
);
1491 /* TODO: get real name */
1497 #endif /* G_OS_WIN32 */
1500 /* support for '.hidden' files */
1501 G_LOCK_DEFINE_STATIC (hidden_cache
);
1502 static GHashTable
*hidden_cache
;
1505 remove_from_hidden_cache (gpointer user_data
)
1507 G_LOCK (hidden_cache
);
1508 g_hash_table_remove (hidden_cache
, user_data
);
1509 G_UNLOCK (hidden_cache
);
1515 read_hidden_file (const gchar
*dirname
)
1517 gchar
*contents
= NULL
;
1520 filename
= g_build_path ("/", dirname
, ".hidden", NULL
);
1521 (void) g_file_get_contents (filename
, &contents
, NULL
, NULL
);
1524 if (contents
!= NULL
)
1530 table
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
1532 lines
= g_strsplit (contents
, "\n", 0);
1535 for (i
= 0; lines
[i
]; i
++)
1536 /* hash table takes the individual strings... */
1537 g_hash_table_add (table
, lines
[i
]);
1539 /* ... so we only free the container. */
1549 maybe_unref_hash_table (gpointer data
)
1552 g_hash_table_unref (data
);
1556 file_is_hidden (const gchar
*path
,
1557 const gchar
*basename
)
1563 dirname
= g_path_get_dirname (path
);
1565 G_LOCK (hidden_cache
);
1567 if G_UNLIKELY (hidden_cache
== NULL
)
1568 hidden_cache
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1569 g_free
, maybe_unref_hash_table
);
1571 if (!g_hash_table_lookup_extended (hidden_cache
, dirname
,
1575 GSource
*remove_from_cache_source
;
1577 g_hash_table_insert (hidden_cache
,
1578 mydirname
= g_strdup (dirname
),
1579 table
= read_hidden_file (dirname
));
1581 remove_from_cache_source
= g_timeout_source_new_seconds (5);
1582 g_source_set_priority (remove_from_cache_source
, G_PRIORITY_DEFAULT
);
1583 g_source_set_callback (remove_from_cache_source
,
1584 remove_from_hidden_cache
,
1587 g_source_attach (remove_from_cache_source
,
1588 GLIB_PRIVATE_CALL (g_get_worker_context
) ());
1589 g_source_unref (remove_from_cache_source
);
1592 result
= table
!= NULL
&& g_hash_table_contains (table
, basename
);
1594 G_UNLOCK (hidden_cache
);
1600 #endif /* !G_OS_WIN32 */
1603 _g_local_file_info_get_nostat (GFileInfo
*info
,
1604 const char *basename
,
1606 GFileAttributeMatcher
*attribute_matcher
)
1608 g_file_info_set_name (info
, basename
);
1610 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1611 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME
))
1613 char *display_name
= g_filename_display_basename (path
);
1615 /* look for U+FFFD REPLACEMENT CHARACTER */
1616 if (strstr (display_name
, "\357\277\275") != NULL
)
1618 char *p
= display_name
;
1619 display_name
= g_strconcat (display_name
, _(" (invalid encoding)"), NULL
);
1622 g_file_info_set_display_name (info
, display_name
);
1623 g_free (display_name
);
1626 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1627 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME
))
1629 char *edit_name
= g_filename_display_basename (path
);
1630 g_file_info_set_edit_name (info
, edit_name
);
1635 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1636 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME
))
1638 char *copy_name
= g_filename_to_utf8 (basename
, -1, NULL
, NULL
, NULL
);
1640 _g_file_info_set_attribute_string_by_id (info
, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME
, copy_name
);
1646 get_icon_name (const char *path
,
1647 gboolean use_symbolic
,
1648 gboolean
*with_fallbacks_out
)
1650 const char *name
= NULL
;
1651 gboolean with_fallbacks
= TRUE
;
1653 if (g_strcmp0 (path
, g_get_home_dir ()) == 0)
1655 name
= use_symbolic
? "user-home-symbolic" : "user-home";
1656 with_fallbacks
= FALSE
;
1658 else if (g_strcmp0 (path
, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP
)) == 0)
1660 name
= use_symbolic
? "user-desktop-symbolic" : "user-desktop";
1661 with_fallbacks
= FALSE
;
1663 else if (g_strcmp0 (path
, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS
)) == 0)
1665 name
= use_symbolic
? "folder-documents-symbolic" : "folder-documents";
1667 else if (g_strcmp0 (path
, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD
)) == 0)
1669 name
= use_symbolic
? "folder-download-symbolic" : "folder-download";
1671 else if (g_strcmp0 (path
, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC
)) == 0)
1673 name
= use_symbolic
? "folder-music-symbolic" : "folder-music";
1675 else if (g_strcmp0 (path
, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES
)) == 0)
1677 name
= use_symbolic
? "folder-pictures-symbolic" : "folder-pictures";
1679 else if (g_strcmp0 (path
, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE
)) == 0)
1681 name
= use_symbolic
? "folder-publicshare-symbolic" : "folder-publicshare";
1683 else if (g_strcmp0 (path
, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES
)) == 0)
1685 name
= use_symbolic
? "folder-templates-symbolic" : "folder-templates";
1687 else if (g_strcmp0 (path
, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS
)) == 0)
1689 name
= use_symbolic
? "folder-videos-symbolic" : "folder-videos";
1696 if (with_fallbacks_out
!= NULL
)
1697 *with_fallbacks_out
= with_fallbacks
;
1703 get_icon (const char *path
,
1704 const char *content_type
,
1705 gboolean use_symbolic
)
1708 const char *icon_name
;
1709 gboolean with_fallbacks
;
1711 icon_name
= get_icon_name (path
, use_symbolic
, &with_fallbacks
);
1712 if (icon_name
!= NULL
)
1715 icon
= g_themed_icon_new_with_default_fallbacks (icon_name
);
1717 icon
= g_themed_icon_new (icon_name
);
1722 icon
= g_content_type_get_symbolic_icon (content_type
);
1724 icon
= g_content_type_get_icon (content_type
);
1731 _g_local_file_info_get (const char *basename
,
1733 GFileAttributeMatcher
*attribute_matcher
,
1734 GFileQueryInfoFlags flags
,
1735 GLocalParentFileInfo
*parent_info
,
1739 GLocalFileStat statbuf
;
1741 struct stat statbuf2
;
1742 #elif defined (G_OS_WIN32)
1743 GWin32PrivateStat statbuf2
;
1747 gboolean is_symlink
, symlink_broken
;
1748 char *symlink_target
;
1753 info
= g_file_info_new ();
1755 /* Make sure we don't set any unwanted attributes */
1756 g_file_info_set_attribute_mask (info
, attribute_matcher
);
1758 _g_local_file_info_get_nostat (info
, basename
, path
, attribute_matcher
);
1760 if (attribute_matcher
== NULL
)
1762 g_file_info_unset_attribute_mask (info
);
1767 res
= g_lstat (path
, &statbuf
);
1769 res
= GLIB_PRIVATE_CALL (g_win32_lstat_utf8
) (path
, &statbuf
);
1776 /* Don't bail out if we get Permission denied (SELinux?) */
1777 if (errsv
!= EACCES
)
1779 char *display_name
= g_filename_display_name (path
);
1780 g_object_unref (info
);
1781 g_set_error (error
, G_IO_ERROR
,
1782 g_io_error_from_errno (errsv
),
1783 _("Error when getting information for file “%s”: %s"),
1784 display_name
, g_strerror (errsv
));
1785 g_free (display_name
);
1790 /* Even if stat() fails, try to get as much as other attributes possible */
1791 stat_ok
= res
!= -1;
1794 device
= statbuf
.st_dev
;
1799 is_symlink
= stat_ok
&& S_ISLNK (statbuf
.st_mode
);
1800 #elif defined (G_OS_WIN32)
1801 /* glib already checked the FILE_ATTRIBUTE_REPARSE_POINT for us */
1802 is_symlink
= stat_ok
&& statbuf
.reparse_tag
== IO_REPARSE_TAG_SYMLINK
;
1806 symlink_broken
= FALSE
;
1810 g_file_info_set_is_symlink (info
, TRUE
);
1812 /* Unless NOFOLLOW was set we default to following symlinks */
1813 if (!(flags
& G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
))
1816 res
= stat (path
, &statbuf2
);
1818 res
= GLIB_PRIVATE_CALL (g_win32_stat_utf8
) (path
, &statbuf2
);
1821 /* Report broken links as symlinks */
1828 symlink_broken
= TRUE
;
1833 set_info_from_stat (info
, &statbuf
, attribute_matcher
);
1836 if (stat_ok
&& _g_local_file_is_lost_found_dir (path
, statbuf
.st_dev
))
1837 g_file_info_set_is_hidden (info
, TRUE
);
1841 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1842 G_FILE_ATTRIBUTE_ID_STANDARD_IS_HIDDEN
))
1844 if (basename
!= NULL
&&
1845 (basename
[0] == '.' ||
1846 file_is_hidden (path
, basename
)))
1847 g_file_info_set_is_hidden (info
, TRUE
);
1850 if (basename
!= NULL
&& basename
[strlen (basename
) -1] == '~' &&
1851 (stat_ok
&& S_ISREG (statbuf
.st_mode
)))
1852 _g_file_info_set_attribute_boolean_by_id (info
, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP
, TRUE
);
1854 if (statbuf
.attributes
& FILE_ATTRIBUTE_HIDDEN
)
1855 g_file_info_set_is_hidden (info
, TRUE
);
1857 if (statbuf
.attributes
& FILE_ATTRIBUTE_ARCHIVE
)
1858 _g_file_info_set_attribute_boolean_by_id (info
, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE
, TRUE
);
1860 if (statbuf
.attributes
& FILE_ATTRIBUTE_SYSTEM
)
1861 _g_file_info_set_attribute_boolean_by_id (info
, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM
, TRUE
);
1864 symlink_target
= NULL
;
1867 #if defined (S_ISLNK) || defined (G_OS_WIN32)
1868 symlink_target
= read_link (path
);
1870 if (symlink_target
&&
1871 _g_file_attribute_matcher_matches_id (attribute_matcher
,
1872 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET
))
1873 g_file_info_set_symlink_target (info
, symlink_target
);
1876 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1877 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE
) ||
1878 _g_file_attribute_matcher_matches_id (attribute_matcher
,
1879 G_FILE_ATTRIBUTE_ID_STANDARD_ICON
) ||
1880 _g_file_attribute_matcher_matches_id (attribute_matcher
,
1881 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON
))
1883 char *content_type
= get_content_type (basename
, path
, stat_ok
? &statbuf
: NULL
, is_symlink
, symlink_broken
, flags
, FALSE
);
1887 g_file_info_set_content_type (info
, content_type
);
1889 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1890 G_FILE_ATTRIBUTE_ID_STANDARD_ICON
)
1891 || _g_file_attribute_matcher_matches_id (attribute_matcher
,
1892 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON
))
1896 /* non symbolic icon */
1897 icon
= get_icon (path
, content_type
, FALSE
);
1900 g_file_info_set_icon (info
, icon
);
1901 g_object_unref (icon
);
1905 icon
= get_icon (path
, content_type
, TRUE
);
1908 g_file_info_set_symbolic_icon (info
, icon
);
1909 g_object_unref (icon
);
1914 g_free (content_type
);
1918 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1919 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE
))
1921 char *content_type
= get_content_type (basename
, path
, stat_ok
? &statbuf
: NULL
, is_symlink
, symlink_broken
, flags
, TRUE
);
1925 _g_file_info_set_attribute_string_by_id (info
, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE
, content_type
);
1926 g_free (content_type
);
1930 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1931 G_FILE_ATTRIBUTE_ID_OWNER_USER
))
1936 win32_get_file_user_info (path
, NULL
, &name
, NULL
);
1939 name
= get_username_from_uid (statbuf
.st_uid
);
1942 _g_file_info_set_attribute_string_by_id (info
, G_FILE_ATTRIBUTE_ID_OWNER_USER
, name
);
1946 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1947 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL
))
1951 win32_get_file_user_info (path
, NULL
, NULL
, &name
);
1954 name
= get_realname_from_uid (statbuf
.st_uid
);
1957 _g_file_info_set_attribute_string_by_id (info
, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL
, name
);
1961 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1962 G_FILE_ATTRIBUTE_ID_OWNER_GROUP
))
1966 win32_get_file_user_info (path
, &name
, NULL
, NULL
);
1969 name
= get_groupname_from_gid (statbuf
.st_gid
);
1972 _g_file_info_set_attribute_string_by_id (info
, G_FILE_ATTRIBUTE_ID_OWNER_GROUP
, name
);
1976 if (stat_ok
&& parent_info
&& parent_info
->device
!= 0 &&
1977 _g_file_attribute_matcher_matches_id (attribute_matcher
, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT
) &&
1978 (statbuf
.st_dev
!= parent_info
->device
|| statbuf
.st_ino
== parent_info
->inode
))
1979 _g_file_info_set_attribute_boolean_by_id (info
, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT
, TRUE
);
1982 get_access_rights (attribute_matcher
, info
, path
, &statbuf
, parent_info
);
1985 get_selinux_context (path
, info
, attribute_matcher
, (flags
& G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
) == 0);
1987 get_xattrs (path
, TRUE
, info
, attribute_matcher
, (flags
& G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
) == 0);
1988 get_xattrs (path
, FALSE
, info
, attribute_matcher
, (flags
& G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
) == 0);
1990 if (_g_file_attribute_matcher_matches_id (attribute_matcher
,
1991 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH
) ||
1992 _g_file_attribute_matcher_matches_id (attribute_matcher
,
1993 G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID
) ||
1994 _g_file_attribute_matcher_matches_id (attribute_matcher
,
1995 G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED
))
1998 get_thumbnail_attributes (path
, info
, &statbuf
);
2000 get_thumbnail_attributes (path
, info
, NULL
);
2003 vfs
= g_vfs_get_default ();
2004 class = G_VFS_GET_CLASS (vfs
);
2005 if (class->local_file_add_info
)
2007 class->local_file_add_info (vfs
,
2013 &parent_info
->extra_data
,
2014 &parent_info
->free_extra_data
);
2017 g_file_info_unset_attribute_mask (info
);
2019 g_free (symlink_target
);
2025 _g_local_file_info_get_from_fd (int fd
,
2026 const char *attributes
,
2029 GLocalFileStat stat_buf
;
2030 GFileAttributeMatcher
*matcher
;
2034 #define FSTAT GLIB_PRIVATE_CALL (g_win32_fstat)
2039 if (FSTAT (fd
, &stat_buf
) == -1)
2043 g_set_error (error
, G_IO_ERROR
,
2044 g_io_error_from_errno (errsv
),
2045 _("Error when getting information for file descriptor: %s"),
2046 g_strerror (errsv
));
2050 info
= g_file_info_new ();
2052 matcher
= g_file_attribute_matcher_new (attributes
);
2054 /* Make sure we don't set any unwanted attributes */
2055 g_file_info_set_attribute_mask (info
, matcher
);
2057 set_info_from_stat (info
, &stat_buf
, matcher
);
2060 if (_g_file_attribute_matcher_matches_id (matcher
, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT
) &&
2061 is_selinux_enabled ())
2064 if (fgetfilecon_raw (fd
, &context
) >= 0)
2066 _g_file_info_set_attribute_string_by_id (info
, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT
, context
);
2072 get_xattrs_from_fd (fd
, TRUE
, info
, matcher
);
2073 get_xattrs_from_fd (fd
, FALSE
, info
, matcher
);
2075 g_file_attribute_matcher_unref (matcher
);
2077 g_file_info_unset_attribute_mask (info
);
2083 get_uint32 (const GFileAttributeValue
*value
,
2087 if (value
->type
!= G_FILE_ATTRIBUTE_TYPE_UINT32
)
2089 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
2090 _("Invalid attribute type (uint32 expected)"));
2094 *val_out
= value
->u
.uint32
;
2101 get_uint64 (const GFileAttributeValue
*value
,
2105 if (value
->type
!= G_FILE_ATTRIBUTE_TYPE_UINT64
)
2107 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
2108 _("Invalid attribute type (uint64 expected)"));
2112 *val_out
= value
->u
.uint64
;
2118 #if defined(HAVE_SYMLINK)
2120 get_byte_string (const GFileAttributeValue
*value
,
2121 const char **val_out
,
2124 if (value
->type
!= G_FILE_ATTRIBUTE_TYPE_BYTE_STRING
)
2126 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
2127 _("Invalid attribute type (byte string expected)"));
2131 *val_out
= value
->u
.string
;
2139 get_string (const GFileAttributeValue
*value
,
2140 const char **val_out
,
2143 if (value
->type
!= G_FILE_ATTRIBUTE_TYPE_STRING
)
2145 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
2146 _("Invalid attribute type (byte string expected)"));
2150 *val_out
= value
->u
.string
;
2157 set_unix_mode (char *filename
,
2158 GFileQueryInfoFlags flags
,
2159 const GFileAttributeValue
*value
,
2165 if (!get_uint32 (value
, &val
, error
))
2168 #if defined (HAVE_SYMLINK) || defined (G_OS_WIN32)
2169 if (flags
& G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
) {
2171 res
= lchmod (filename
, val
);
2173 gboolean is_symlink
;
2175 struct stat statbuf
;
2176 /* Calling chmod on a symlink changes permissions on the symlink.
2177 * We don't want to do this, so we need to check for a symlink */
2178 res
= g_lstat (filename
, &statbuf
);
2179 is_symlink
= (res
== 0 && S_ISLNK (statbuf
.st_mode
));
2181 /* FIXME: implement lchmod for W32, should be doable */
2182 GWin32PrivateStat statbuf
;
2184 res
= GLIB_PRIVATE_CALL (g_win32_lstat_utf8
) (filename
, &statbuf
);
2185 is_symlink
= (res
== 0 && statbuf
.reparse_tag
== IO_REPARSE_TAG_SYMLINK
);
2189 g_set_error_literal (error
, G_IO_ERROR
,
2190 G_IO_ERROR_NOT_SUPPORTED
,
2191 _("Cannot set permissions on symlinks"));
2195 res
= g_chmod (filename
, val
);
2199 res
= g_chmod (filename
, val
);
2205 g_set_error (error
, G_IO_ERROR
,
2206 g_io_error_from_errno (errsv
),
2207 _("Error setting permissions: %s"),
2208 g_strerror (errsv
));
2216 set_unix_uid_gid (char *filename
,
2217 const GFileAttributeValue
*uid_value
,
2218 const GFileAttributeValue
*gid_value
,
2219 GFileQueryInfoFlags flags
,
2229 if (!get_uint32 (uid_value
, &val
, error
))
2238 if (!get_uint32 (gid_value
, &val
, error
))
2246 if (flags
& G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
)
2247 res
= lchown (filename
, uid
, gid
);
2250 res
= chown (filename
, uid
, gid
);
2256 g_set_error (error
, G_IO_ERROR
,
2257 g_io_error_from_errno (errsv
),
2258 _("Error setting owner: %s"),
2259 g_strerror (errsv
));
2268 set_symlink (char *filename
,
2269 const GFileAttributeValue
*value
,
2273 struct stat statbuf
;
2275 if (!get_byte_string (value
, &val
, error
))
2280 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
2281 _("symlink must be non-NULL"));
2285 if (g_lstat (filename
, &statbuf
))
2289 g_set_error (error
, G_IO_ERROR
,
2290 g_io_error_from_errno (errsv
),
2291 _("Error setting symlink: %s"),
2292 g_strerror (errsv
));
2296 if (!S_ISLNK (statbuf
.st_mode
))
2298 g_set_error_literal (error
, G_IO_ERROR
,
2299 G_IO_ERROR_NOT_SYMBOLIC_LINK
,
2300 _("Error setting symlink: file is not a symlink"));
2304 if (g_unlink (filename
))
2308 g_set_error (error
, G_IO_ERROR
,
2309 g_io_error_from_errno (errsv
),
2310 _("Error setting symlink: %s"),
2311 g_strerror (errsv
));
2315 if (symlink (filename
, val
) != 0)
2319 g_set_error (error
, G_IO_ERROR
,
2320 g_io_error_from_errno (errsv
),
2321 _("Error setting symlink: %s"),
2322 g_strerror (errsv
));
2332 lazy_stat (char *filename
,
2333 struct stat
*statbuf
,
2334 gboolean
*called_stat
)
2341 res
= g_stat (filename
, statbuf
);
2344 *called_stat
= TRUE
;
2351 set_mtime_atime (char *filename
,
2352 const GFileAttributeValue
*mtime_value
,
2353 const GFileAttributeValue
*mtime_usec_value
,
2354 const GFileAttributeValue
*atime_value
,
2355 const GFileAttributeValue
*atime_usec_value
,
2360 guint32 val_usec
= 0;
2361 struct stat statbuf
;
2362 gboolean got_stat
= FALSE
;
2363 struct timeval times
[2] = { {0, 0}, {0, 0} };
2368 if (!get_uint64 (atime_value
, &val
, error
))
2370 times
[0].tv_sec
= val
;
2374 if (lazy_stat (filename
, &statbuf
, &got_stat
) == 0)
2376 times
[0].tv_sec
= statbuf
.st_mtime
;
2377 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2378 times
[0].tv_usec
= statbuf
.st_atimensec
/ 1000;
2379 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2380 times
[0].tv_usec
= statbuf
.st_atim
.tv_nsec
/ 1000;
2385 if (atime_usec_value
)
2387 if (!get_uint32 (atime_usec_value
, &val_usec
, error
))
2389 times
[0].tv_usec
= val_usec
;
2395 if (!get_uint64 (mtime_value
, &val
, error
))
2397 times
[1].tv_sec
= val
;
2401 if (lazy_stat (filename
, &statbuf
, &got_stat
) == 0)
2403 times
[1].tv_sec
= statbuf
.st_mtime
;
2404 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2405 times
[1].tv_usec
= statbuf
.st_mtimensec
/ 1000;
2406 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2407 times
[1].tv_usec
= statbuf
.st_mtim
.tv_nsec
/ 1000;
2412 if (mtime_usec_value
)
2414 if (!get_uint32 (mtime_usec_value
, &val_usec
, error
))
2416 times
[1].tv_usec
= val_usec
;
2419 res
= utimes (filename
, times
);
2424 g_set_error (error
, G_IO_ERROR
,
2425 g_io_error_from_errno (errsv
),
2426 _("Error setting modification or access time: %s"),
2427 g_strerror (errsv
));
2437 set_selinux_context (char *filename
,
2438 const GFileAttributeValue
*value
,
2443 if (!get_string (value
, &val
, error
))
2448 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
2449 _("SELinux context must be non-NULL"));
2453 if (is_selinux_enabled ()) {
2454 security_context_t val_s
;
2456 val_s
= g_strdup (val
);
2458 if (setfilecon_raw (filename
, val_s
) < 0)
2462 g_set_error (error
, G_IO_ERROR
,
2463 g_io_error_from_errno (errsv
),
2464 _("Error setting SELinux context: %s"),
2465 g_strerror (errsv
));
2470 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
2471 _("SELinux is not enabled on this system"));
2481 _g_local_file_info_set_attribute (char *filename
,
2482 const char *attribute
,
2483 GFileAttributeType type
,
2485 GFileQueryInfoFlags flags
,
2486 GCancellable
*cancellable
,
2489 GFileAttributeValue value
= { 0 };
2493 _g_file_attribute_value_set_from_pointer (&value
, type
, value_p
, FALSE
);
2495 if (strcmp (attribute
, G_FILE_ATTRIBUTE_UNIX_MODE
) == 0)
2496 return set_unix_mode (filename
, flags
, &value
, error
);
2499 else if (strcmp (attribute
, G_FILE_ATTRIBUTE_UNIX_UID
) == 0)
2500 return set_unix_uid_gid (filename
, &value
, NULL
, flags
, error
);
2501 else if (strcmp (attribute
, G_FILE_ATTRIBUTE_UNIX_GID
) == 0)
2502 return set_unix_uid_gid (filename
, NULL
, &value
, flags
, error
);
2506 else if (strcmp (attribute
, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET
) == 0)
2507 return set_symlink (filename
, &value
, error
);
2511 else if (strcmp (attribute
, G_FILE_ATTRIBUTE_TIME_MODIFIED
) == 0)
2512 return set_mtime_atime (filename
, &value
, NULL
, NULL
, NULL
, error
);
2513 else if (strcmp (attribute
, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC
) == 0)
2514 return set_mtime_atime (filename
, NULL
, &value
, NULL
, NULL
, error
);
2515 else if (strcmp (attribute
, G_FILE_ATTRIBUTE_TIME_ACCESS
) == 0)
2516 return set_mtime_atime (filename
, NULL
, NULL
, &value
, NULL
, error
);
2517 else if (strcmp (attribute
, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC
) == 0)
2518 return set_mtime_atime (filename
, NULL
, NULL
, NULL
, &value
, error
);
2522 else if (g_str_has_prefix (attribute
, "xattr::"))
2523 return set_xattr (filename
, attribute
, &value
, error
);
2524 else if (g_str_has_prefix (attribute
, "xattr-sys::"))
2525 return set_xattr (filename
, attribute
, &value
, error
);
2529 else if (strcmp (attribute
, G_FILE_ATTRIBUTE_SELINUX_CONTEXT
) == 0)
2530 return set_selinux_context (filename
, &value
, error
);
2533 vfs
= g_vfs_get_default ();
2534 class = G_VFS_GET_CLASS (vfs
);
2535 if (class->local_file_set_attributes
)
2539 info
= g_file_info_new ();
2540 g_file_info_set_attribute (info
,
2544 if (!class->local_file_set_attributes (vfs
, filename
,
2549 g_object_unref (info
);
2553 if (g_file_info_get_attribute_status (info
, attribute
) == G_FILE_ATTRIBUTE_STATUS_SET
)
2555 g_object_unref (info
);
2559 g_object_unref (info
);
2562 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
2563 _("Setting attribute %s not supported"), attribute
);
2568 _g_local_file_info_set_attributes (char *filename
,
2570 GFileQueryInfoFlags flags
,
2571 GCancellable
*cancellable
,
2574 GFileAttributeValue
*value
;
2576 GFileAttributeValue
*uid
, *gid
;
2578 GFileAttributeValue
*mtime
, *mtime_usec
, *atime
, *atime_usec
;
2580 GFileAttributeStatus status
;
2586 /* Handles setting multiple specified data in a single set, and takes care
2587 of ordering restrictions when setting attributes */
2591 /* Set symlink first, since this recreates the file */
2593 value
= _g_file_info_get_attribute_value (info
, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET
);
2596 if (!set_symlink (filename
, value
, error
))
2598 value
->status
= G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING
;
2600 /* Don't set error multiple times */
2604 value
->status
= G_FILE_ATTRIBUTE_STATUS_SET
;
2610 /* Group uid and gid setting into one call
2611 * Change ownership before permissions, since ownership changes can
2612 change permissions (e.g. setuid)
2614 uid
= _g_file_info_get_attribute_value (info
, G_FILE_ATTRIBUTE_UNIX_UID
);
2615 gid
= _g_file_info_get_attribute_value (info
, G_FILE_ATTRIBUTE_UNIX_GID
);
2619 if (!set_unix_uid_gid (filename
, uid
, gid
, flags
, error
))
2621 status
= G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING
;
2623 /* Don't set error multiple times */
2627 status
= G_FILE_ATTRIBUTE_STATUS_SET
;
2629 uid
->status
= status
;
2631 gid
->status
= status
;
2635 value
= _g_file_info_get_attribute_value (info
, G_FILE_ATTRIBUTE_UNIX_MODE
);
2638 if (!set_unix_mode (filename
, flags
, value
, error
))
2640 value
->status
= G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING
;
2642 /* Don't set error multiple times */
2646 value
->status
= G_FILE_ATTRIBUTE_STATUS_SET
;
2651 /* Group all time settings into one call
2652 * Change times as the last thing to avoid it changing due to metadata changes
2655 mtime
= _g_file_info_get_attribute_value (info
, G_FILE_ATTRIBUTE_TIME_MODIFIED
);
2656 mtime_usec
= _g_file_info_get_attribute_value (info
, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC
);
2657 atime
= _g_file_info_get_attribute_value (info
, G_FILE_ATTRIBUTE_TIME_ACCESS
);
2658 atime_usec
= _g_file_info_get_attribute_value (info
, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC
);
2660 if (mtime
|| mtime_usec
|| atime
|| atime_usec
)
2662 if (!set_mtime_atime (filename
, mtime
, mtime_usec
, atime
, atime_usec
, error
))
2664 status
= G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING
;
2666 /* Don't set error multiple times */
2670 status
= G_FILE_ATTRIBUTE_STATUS_SET
;
2673 mtime
->status
= status
;
2675 mtime_usec
->status
= status
;
2677 atime
->status
= status
;
2679 atime_usec
->status
= status
;
2683 /* xattrs are handled by default callback */
2686 /* SELinux context */
2688 if (is_selinux_enabled ()) {
2689 value
= _g_file_info_get_attribute_value (info
, G_FILE_ATTRIBUTE_SELINUX_CONTEXT
);
2692 if (!set_selinux_context (filename
, value
, error
))
2694 value
->status
= G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING
;
2696 /* Don't set error multiple times */
2700 value
->status
= G_FILE_ATTRIBUTE_STATUS_SET
;
2705 vfs
= g_vfs_get_default ();
2706 class = G_VFS_GET_CLASS (vfs
);
2707 if (class->local_file_set_attributes
)
2709 if (!class->local_file_set_attributes (vfs
, filename
,
2715 /* Don't set error multiple times */