gio: Annotate GDBusObjectManagerClient signal appropriately
[glib.git] / gio / glocalfileinfo.c
blob1c97eef59d36556b4f29ff42886d497effc81c6c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
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>
23 #include "config.h"
25 #include <glib.h>
27 #ifdef HAVE_SYS_TIME_H
28 #include <sys/time.h>
29 #endif
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #ifdef G_OS_UNIX
36 #include <grp.h>
37 #include <pwd.h>
38 #endif
39 #ifdef HAVE_SELINUX
40 #include <selinux/selinux.h>
41 #endif
43 #ifdef HAVE_XATTR
45 #if defined HAVE_SYS_XATTR_H
46 #include <sys/xattr.h>
47 #elif defined HAVE_ATTR_XATTR_H
48 #include <attr/xattr.h>
49 #else
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 <gfileattribute-priv.h>
57 #include <gfileinfo-priv.h>
58 #include <gvfs.h>
60 #ifdef G_OS_UNIX
61 #include <unistd.h>
62 #include "glib-unix.h"
63 #include "glib-private.h"
64 #endif
66 #include "thumbnail-verify.h"
68 #ifdef G_OS_WIN32
69 #include <windows.h>
70 #include <io.h>
71 #ifndef W_OK
72 #define W_OK 2
73 #endif
74 #ifndef R_OK
75 #define R_OK 4
76 #endif
77 #ifndef X_OK
78 #define X_OK 0 /* not really */
79 #endif
80 #ifndef S_ISREG
81 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
82 #endif
83 #ifndef S_ISDIR
84 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
85 #endif
86 #ifndef S_IXUSR
87 #define S_IXUSR _S_IEXEC
88 #endif
89 #endif
91 #include "glocalfileinfo.h"
92 #include "gioerror.h"
93 #include "gthemedicon.h"
94 #include "gcontenttypeprivate.h"
95 #include "glibintl.h"
98 struct ThumbMD5Context {
99 guint32 buf[4];
100 guint32 bits[2];
101 unsigned char in[64];
104 #ifndef G_OS_WIN32
106 typedef struct {
107 char *user_name;
108 char *real_name;
109 } UidData;
111 G_LOCK_DEFINE_STATIC (uid_cache);
112 static GHashTable *uid_cache = NULL;
114 G_LOCK_DEFINE_STATIC (gid_cache);
115 static GHashTable *gid_cache = NULL;
117 #endif /* !G_OS_WIN32 */
119 char *
120 _g_local_file_info_create_etag (GLocalFileStat *statbuf)
122 glong sec, usec;
124 sec = statbuf->st_mtime;
125 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
126 usec = statbuf->st_mtimensec / 1000;
127 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
128 usec = statbuf->st_mtim.tv_nsec / 1000;
129 #else
130 usec = 0;
131 #endif
133 return g_strdup_printf ("%lu:%lu", sec, usec);
136 static char *
137 _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
139 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
140 (guint64) statbuf->st_dev,
141 (guint64) statbuf->st_ino);
144 static char *
145 _g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
147 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
148 (guint64) statbuf->st_dev);
152 #ifdef S_ISLNK
154 static gchar *
155 read_link (const gchar *full_name)
157 #ifdef HAVE_READLINK
158 gchar *buffer;
159 guint size;
161 size = 256;
162 buffer = g_malloc (size);
164 while (1)
166 int read_size;
168 read_size = readlink (full_name, buffer, size);
169 if (read_size < 0)
171 g_free (buffer);
172 return NULL;
174 if (read_size < size)
176 buffer[read_size] = 0;
177 return buffer;
179 size *= 2;
180 buffer = g_realloc (buffer, size);
182 #else
183 return NULL;
184 #endif
187 #endif /* S_ISLNK */
189 #ifdef HAVE_SELINUX
190 /* Get the SELinux security context */
191 static void
192 get_selinux_context (const char *path,
193 GFileInfo *info,
194 GFileAttributeMatcher *attribute_matcher,
195 gboolean follow_symlinks)
197 char *context;
199 if (!_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT))
200 return;
202 if (is_selinux_enabled ())
204 if (follow_symlinks)
206 if (lgetfilecon_raw (path, &context) < 0)
207 return;
209 else
211 if (getfilecon_raw (path, &context) < 0)
212 return;
215 if (context)
217 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
218 freecon (context);
222 #endif
224 #ifdef HAVE_XATTR
226 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
227 * (Mac) getxattr(..., XATTR_NOFOLLOW)
229 #ifdef HAVE_XATTR_NOFOLLOW
230 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
231 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
232 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
233 #else
234 #define g_fgetxattr fgetxattr
235 #define g_flistxattr flistxattr
236 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
237 #endif
239 static ssize_t
240 g_getxattr (const char *path, const char *name, void *value, size_t size,
241 gboolean follow_symlinks)
243 #ifdef HAVE_XATTR_NOFOLLOW
244 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
245 #else
246 if (follow_symlinks)
247 return getxattr (path, name, value, size);
248 else
249 return lgetxattr (path, name, value, size);
250 #endif
253 static ssize_t
254 g_listxattr(const char *path, char *namebuf, size_t size,
255 gboolean follow_symlinks)
257 #ifdef HAVE_XATTR_NOFOLLOW
258 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
259 #else
260 if (follow_symlinks)
261 return listxattr (path, namebuf, size);
262 else
263 return llistxattr (path, namebuf, size);
264 #endif
267 static gboolean
268 valid_char (char c)
270 return c >= 32 && c <= 126 && c != '\\';
273 static gboolean
274 name_is_valid (const char *str)
276 while (*str)
278 if (!valid_char (*str++))
279 return FALSE;
281 return TRUE;
284 static char *
285 hex_escape_string (const char *str,
286 gboolean *free_return)
288 int num_invalid, i;
289 char *escaped_str, *p;
290 unsigned char c;
291 static char *hex_digits = "0123456789abcdef";
292 int len;
294 len = strlen (str);
296 num_invalid = 0;
297 for (i = 0; i < len; i++)
299 if (!valid_char (str[i]))
300 num_invalid++;
303 if (num_invalid == 0)
305 *free_return = FALSE;
306 return (char *)str;
309 escaped_str = g_malloc (len + num_invalid*3 + 1);
311 p = escaped_str;
312 for (i = 0; i < len; i++)
314 if (valid_char (str[i]))
315 *p++ = str[i];
316 else
318 c = str[i];
319 *p++ = '\\';
320 *p++ = 'x';
321 *p++ = hex_digits[(c >> 4) & 0xf];
322 *p++ = hex_digits[c & 0xf];
325 *p = 0;
327 *free_return = TRUE;
328 return escaped_str;
331 static char *
332 hex_unescape_string (const char *str,
333 int *out_len,
334 gboolean *free_return)
336 int i;
337 char *unescaped_str, *p;
338 unsigned char c;
339 int len;
341 len = strlen (str);
343 if (strchr (str, '\\') == NULL)
345 if (out_len)
346 *out_len = len;
347 *free_return = FALSE;
348 return (char *)str;
351 unescaped_str = g_malloc (len + 1);
353 p = unescaped_str;
354 for (i = 0; i < len; i++)
356 if (str[i] == '\\' &&
357 str[i+1] == 'x' &&
358 len - i >= 4)
361 (g_ascii_xdigit_value (str[i+2]) << 4) |
362 g_ascii_xdigit_value (str[i+3]);
363 *p++ = c;
364 i += 3;
366 else
367 *p++ = str[i];
369 *p++ = 0;
371 if (out_len)
372 *out_len = p - unescaped_str;
373 *free_return = TRUE;
374 return unescaped_str;
377 static void
378 escape_xattr (GFileInfo *info,
379 const char *gio_attr, /* gio attribute name */
380 const char *value, /* Is zero terminated */
381 size_t len /* not including zero termination */)
383 char *escaped_val;
384 gboolean free_escaped_val;
386 escaped_val = hex_escape_string (value, &free_escaped_val);
388 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
390 if (free_escaped_val)
391 g_free (escaped_val);
394 static void
395 get_one_xattr (const char *path,
396 GFileInfo *info,
397 const char *gio_attr,
398 const char *xattr,
399 gboolean follow_symlinks)
401 char value[64];
402 char *value_p;
403 ssize_t len;
404 int errsv;
406 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
407 errsv = errno;
409 value_p = NULL;
410 if (len >= 0)
411 value_p = value;
412 else if (len == -1 && errsv == ERANGE)
414 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
416 if (len < 0)
417 return;
419 value_p = g_malloc (len+1);
421 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
423 if (len < 0)
425 g_free (value_p);
426 return;
429 else
430 return;
432 /* Null terminate */
433 value_p[len] = 0;
435 escape_xattr (info, gio_attr, value_p, len);
437 if (value_p != value)
438 g_free (value_p);
441 #endif /* defined HAVE_XATTR */
443 static void
444 get_xattrs (const char *path,
445 gboolean user,
446 GFileInfo *info,
447 GFileAttributeMatcher *matcher,
448 gboolean follow_symlinks)
450 #ifdef HAVE_XATTR
451 gboolean all;
452 gsize list_size;
453 ssize_t list_res_size;
454 size_t len;
455 char *list;
456 const char *attr, *attr2;
458 if (user)
459 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
460 else
461 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
463 if (all)
465 int errsv;
467 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
469 if (list_res_size == -1 ||
470 list_res_size == 0)
471 return;
473 list_size = list_res_size;
474 list = g_malloc (list_size);
476 retry:
478 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
479 errsv = errno;
481 if (list_res_size == -1 && errsv == ERANGE)
483 list_size = list_size * 2;
484 list = g_realloc (list, list_size);
485 goto retry;
488 if (list_res_size == -1)
489 return;
491 attr = list;
492 while (list_res_size > 0)
494 if ((user && g_str_has_prefix (attr, "user.")) ||
495 (!user && !g_str_has_prefix (attr, "user.")))
497 char *escaped_attr, *gio_attr;
498 gboolean free_escaped_attr;
500 if (user)
502 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
503 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
505 else
507 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
508 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
511 if (free_escaped_attr)
512 g_free (escaped_attr);
514 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
516 g_free (gio_attr);
519 len = strlen (attr) + 1;
520 attr += len;
521 list_res_size -= len;
524 g_free (list);
526 else
528 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
530 char *unescaped_attribute, *a;
531 gboolean free_unescaped_attribute;
533 attr2 = strchr (attr, ':');
534 if (attr2)
536 attr2 += 2; /* Skip '::' */
537 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
538 if (user)
539 a = g_strconcat ("user.", unescaped_attribute, NULL);
540 else
541 a = unescaped_attribute;
543 get_one_xattr (path, info, attr, a, follow_symlinks);
545 if (user)
546 g_free (a);
548 if (free_unescaped_attribute)
549 g_free (unescaped_attribute);
553 #endif /* defined HAVE_XATTR */
556 #ifdef HAVE_XATTR
557 static void
558 get_one_xattr_from_fd (int fd,
559 GFileInfo *info,
560 const char *gio_attr,
561 const char *xattr)
563 char value[64];
564 char *value_p;
565 ssize_t len;
566 int errsv;
568 len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
569 errsv = errno;
571 value_p = NULL;
572 if (len >= 0)
573 value_p = value;
574 else if (len == -1 && errsv == ERANGE)
576 len = g_fgetxattr (fd, xattr, NULL, 0);
578 if (len < 0)
579 return;
581 value_p = g_malloc (len + 1);
583 len = g_fgetxattr (fd, xattr, value_p, len);
585 if (len < 0)
587 g_free (value_p);
588 return;
591 else
592 return;
594 /* Null terminate */
595 value_p[len] = 0;
597 escape_xattr (info, gio_attr, value_p, len);
599 if (value_p != value)
600 g_free (value_p);
602 #endif /* defined HAVE_XATTR */
604 static void
605 get_xattrs_from_fd (int fd,
606 gboolean user,
607 GFileInfo *info,
608 GFileAttributeMatcher *matcher)
610 #ifdef HAVE_XATTR
611 gboolean all;
612 gsize list_size;
613 ssize_t list_res_size;
614 size_t len;
615 char *list;
616 const char *attr, *attr2;
618 if (user)
619 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
620 else
621 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
623 if (all)
625 int errsv;
627 list_res_size = g_flistxattr (fd, NULL, 0);
629 if (list_res_size == -1 ||
630 list_res_size == 0)
631 return;
633 list_size = list_res_size;
634 list = g_malloc (list_size);
636 retry:
638 list_res_size = g_flistxattr (fd, list, list_size);
639 errsv = errno;
641 if (list_res_size == -1 && errsv == ERANGE)
643 list_size = list_size * 2;
644 list = g_realloc (list, list_size);
645 goto retry;
648 if (list_res_size == -1)
649 return;
651 attr = list;
652 while (list_res_size > 0)
654 if ((user && g_str_has_prefix (attr, "user.")) ||
655 (!user && !g_str_has_prefix (attr, "user.")))
657 char *escaped_attr, *gio_attr;
658 gboolean free_escaped_attr;
660 if (user)
662 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
663 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
665 else
667 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
668 gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
671 if (free_escaped_attr)
672 g_free (escaped_attr);
674 get_one_xattr_from_fd (fd, info, gio_attr, attr);
675 g_free (gio_attr);
678 len = strlen (attr) + 1;
679 attr += len;
680 list_res_size -= len;
683 g_free (list);
685 else
687 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
689 char *unescaped_attribute, *a;
690 gboolean free_unescaped_attribute;
692 attr2 = strchr (attr, ':');
693 if (attr2)
695 attr2++; /* Skip ':' */
696 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
697 if (user)
698 a = g_strconcat ("user.", unescaped_attribute, NULL);
699 else
700 a = unescaped_attribute;
702 get_one_xattr_from_fd (fd, info, attr, a);
704 if (user)
705 g_free (a);
707 if (free_unescaped_attribute)
708 g_free (unescaped_attribute);
712 #endif /* defined HAVE_XATTR */
715 #ifdef HAVE_XATTR
716 static gboolean
717 set_xattr (char *filename,
718 const char *escaped_attribute,
719 const GFileAttributeValue *attr_value,
720 GError **error)
722 char *attribute, *value;
723 gboolean free_attribute, free_value;
724 int val_len, res, errsv;
725 gboolean is_user;
726 char *a;
728 if (attr_value == NULL)
730 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
731 _("Attribute value must be non-NULL"));
732 return FALSE;
735 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
737 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
738 _("Invalid attribute type (string expected)"));
739 return FALSE;
742 if (!name_is_valid (escaped_attribute))
744 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
745 _("Invalid extended attribute name"));
746 return FALSE;
749 if (g_str_has_prefix (escaped_attribute, "xattr::"))
751 escaped_attribute += strlen ("xattr::");
752 is_user = TRUE;
754 else
756 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
757 escaped_attribute += strlen ("xattr-sys::");
758 is_user = FALSE;
761 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
762 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
764 if (is_user)
765 a = g_strconcat ("user.", attribute, NULL);
766 else
767 a = attribute;
769 res = g_setxattr (filename, a, value, val_len);
770 errsv = errno;
772 if (is_user)
773 g_free (a);
775 if (free_attribute)
776 g_free (attribute);
778 if (free_value)
779 g_free (value);
781 if (res == -1)
783 g_set_error (error, G_IO_ERROR,
784 g_io_error_from_errno (errsv),
785 _("Error setting extended attribute “%s”: %s"),
786 escaped_attribute, g_strerror (errsv));
787 return FALSE;
790 return TRUE;
793 #endif
796 void
797 _g_local_file_info_get_parent_info (const char *dir,
798 GFileAttributeMatcher *attribute_matcher,
799 GLocalParentFileInfo *parent_info)
801 GStatBuf statbuf;
802 int res;
804 parent_info->extra_data = NULL;
805 parent_info->free_extra_data = NULL;
806 parent_info->writable = FALSE;
807 parent_info->is_sticky = FALSE;
808 parent_info->has_trash_dir = FALSE;
809 parent_info->device = 0;
811 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME) ||
812 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE) ||
813 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH) ||
814 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT))
816 /* FIXME: Windows: The underlying _waccess() call in the C
817 * library is mostly pointless as it only looks at the READONLY
818 * FAT-style attribute of the file, it doesn't check the ACL at
819 * all.
821 parent_info->writable = (g_access (dir, W_OK) == 0);
823 res = g_stat (dir, &statbuf);
826 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
827 * renamed or deleted only by the owner of the file, by the owner of the directory, and
828 * by a privileged process.
830 if (res == 0)
832 #ifdef S_ISVTX
833 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
834 #else
835 parent_info->is_sticky = FALSE;
836 #endif
837 parent_info->owner = statbuf.st_uid;
838 parent_info->device = statbuf.st_dev;
839 /* No need to find trash dir if it's not writable anyway */
840 if (parent_info->writable &&
841 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
842 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
847 void
848 _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info)
850 if (parent_info->extra_data &&
851 parent_info->free_extra_data)
852 parent_info->free_extra_data (parent_info->extra_data);
855 static void
856 get_access_rights (GFileAttributeMatcher *attribute_matcher,
857 GFileInfo *info,
858 const gchar *path,
859 GLocalFileStat *statbuf,
860 GLocalParentFileInfo *parent_info)
862 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
863 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
864 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ))
865 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ,
866 g_access (path, R_OK) == 0);
868 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
869 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE))
870 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE,
871 g_access (path, W_OK) == 0);
873 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
874 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE))
875 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE,
876 g_access (path, X_OK) == 0);
879 if (parent_info)
881 gboolean writable;
883 writable = FALSE;
884 if (parent_info->writable)
886 if (parent_info->is_sticky)
888 #ifndef G_OS_WIN32
889 uid_t uid = geteuid ();
891 if (uid == statbuf->st_uid ||
892 uid == parent_info->owner ||
893 uid == 0)
894 #endif
895 writable = TRUE;
897 else
898 writable = TRUE;
901 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME))
902 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME,
903 writable);
905 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE))
906 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE,
907 writable);
909 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
910 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH,
911 writable && parent_info->has_trash_dir);
915 static void
916 set_info_from_stat (GFileInfo *info,
917 GLocalFileStat *statbuf,
918 GFileAttributeMatcher *attribute_matcher)
920 GFileType file_type;
922 file_type = G_FILE_TYPE_UNKNOWN;
924 if (S_ISREG (statbuf->st_mode))
925 file_type = G_FILE_TYPE_REGULAR;
926 else if (S_ISDIR (statbuf->st_mode))
927 file_type = G_FILE_TYPE_DIRECTORY;
928 #ifndef G_OS_WIN32
929 else if (S_ISCHR (statbuf->st_mode) ||
930 S_ISBLK (statbuf->st_mode) ||
931 S_ISFIFO (statbuf->st_mode)
932 #ifdef S_ISSOCK
933 || S_ISSOCK (statbuf->st_mode)
934 #endif
936 file_type = G_FILE_TYPE_SPECIAL;
937 #endif
938 #ifdef S_ISLNK
939 else if (S_ISLNK (statbuf->st_mode))
940 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
941 #endif
943 g_file_info_set_file_type (info, file_type);
944 g_file_info_set_size (info, statbuf->st_size);
946 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
947 #ifndef G_OS_WIN32
948 /* Pointless setting these on Windows even if they exist in the struct */
949 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
950 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
951 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
952 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
953 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
954 #endif
955 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
956 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
957 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
958 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
959 #endif
960 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
961 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
962 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
963 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
964 #endif
966 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
967 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
968 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
969 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
970 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
971 #endif
973 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
974 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
975 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
976 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
977 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
978 #endif
980 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
981 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
982 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
983 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
984 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
985 #endif
987 #if defined (HAVE_STRUCT_STAT_ST_BIRTHTIME) && defined (HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
988 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtime);
989 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED_USEC, statbuf->st_birthtimensec / 1000);
990 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIM) && defined (HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
991 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtim.tv_sec);
992 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED_USEC, statbuf->st_birthtim.tv_nsec / 1000);
993 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIME)
994 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtime);
995 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIM)
996 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtim);
997 #endif
999 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1000 G_FILE_ATTRIBUTE_ID_ETAG_VALUE))
1002 char *etag = _g_local_file_info_create_etag (statbuf);
1003 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ETAG_VALUE, etag);
1004 g_free (etag);
1007 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1008 G_FILE_ATTRIBUTE_ID_ID_FILE))
1010 char *id = _g_local_file_info_create_file_id (statbuf);
1011 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILE, id);
1012 g_free (id);
1015 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1016 G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM))
1018 char *id = _g_local_file_info_create_fs_id (statbuf);
1019 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM, id);
1020 g_free (id);
1024 #ifndef G_OS_WIN32
1026 static char *
1027 make_valid_utf8 (const char *name)
1029 GString *string;
1030 const gchar *remainder, *invalid;
1031 gint remaining_bytes, valid_bytes;
1033 string = NULL;
1034 remainder = name;
1035 remaining_bytes = strlen (name);
1037 while (remaining_bytes != 0)
1039 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1040 break;
1041 valid_bytes = invalid - remainder;
1043 if (string == NULL)
1044 string = g_string_sized_new (remaining_bytes);
1046 g_string_append_len (string, remainder, valid_bytes);
1047 /* append U+FFFD REPLACEMENT CHARACTER */
1048 g_string_append (string, "\357\277\275");
1050 remaining_bytes -= valid_bytes + 1;
1051 remainder = invalid + 1;
1054 if (string == NULL)
1055 return g_strdup (name);
1057 g_string_append (string, remainder);
1059 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1061 return g_string_free (string, FALSE);
1064 static char *
1065 convert_pwd_string_to_utf8 (char *pwd_str)
1067 char *utf8_string;
1069 if (!g_utf8_validate (pwd_str, -1, NULL))
1071 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1072 if (utf8_string == NULL)
1073 utf8_string = make_valid_utf8 (pwd_str);
1075 else
1076 utf8_string = g_strdup (pwd_str);
1078 return utf8_string;
1081 static void
1082 uid_data_free (UidData *data)
1084 g_free (data->user_name);
1085 g_free (data->real_name);
1086 g_free (data);
1089 /* called with lock held */
1090 static UidData *
1091 lookup_uid_data (uid_t uid)
1093 UidData *data;
1094 char buffer[4096];
1095 struct passwd pwbuf;
1096 struct passwd *pwbufp;
1097 char *gecos, *comma;
1099 if (uid_cache == NULL)
1100 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1102 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1104 if (data)
1105 return data;
1107 data = g_new0 (UidData, 1);
1109 #if defined(HAVE_GETPWUID_R)
1110 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1111 #else
1112 pwbufp = getpwuid (uid);
1113 #endif
1115 if (pwbufp != NULL)
1117 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1118 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1120 #ifndef __BIONIC__
1121 gecos = pwbufp->pw_gecos;
1123 if (gecos)
1125 comma = strchr (gecos, ',');
1126 if (comma)
1127 *comma = 0;
1128 data->real_name = convert_pwd_string_to_utf8 (gecos);
1130 #endif
1133 /* Default fallbacks */
1134 if (data->real_name == NULL)
1136 if (data->user_name != NULL)
1137 data->real_name = g_strdup (data->user_name);
1138 else
1139 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1142 if (data->user_name == NULL)
1143 data->user_name = g_strdup_printf ("%d", (int)uid);
1145 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1147 return data;
1150 static char *
1151 get_username_from_uid (uid_t uid)
1153 char *res;
1154 UidData *data;
1156 G_LOCK (uid_cache);
1157 data = lookup_uid_data (uid);
1158 res = g_strdup (data->user_name);
1159 G_UNLOCK (uid_cache);
1161 return res;
1164 static char *
1165 get_realname_from_uid (uid_t uid)
1167 char *res;
1168 UidData *data;
1170 G_LOCK (uid_cache);
1171 data = lookup_uid_data (uid);
1172 res = g_strdup (data->real_name);
1173 G_UNLOCK (uid_cache);
1175 return res;
1178 /* called with lock held */
1179 static char *
1180 lookup_gid_name (gid_t gid)
1182 char *name;
1183 char buffer[4096];
1184 struct group gbuf;
1185 struct group *gbufp;
1187 if (gid_cache == NULL)
1188 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1190 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1192 if (name)
1193 return name;
1195 #if defined (HAVE_GETGRGID_R)
1196 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1197 #else
1198 gbufp = getgrgid (gid);
1199 #endif
1201 if (gbufp != NULL &&
1202 gbufp->gr_name != NULL &&
1203 gbufp->gr_name[0] != 0)
1204 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1205 else
1206 name = g_strdup_printf("%d", (int)gid);
1208 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1210 return name;
1213 static char *
1214 get_groupname_from_gid (gid_t gid)
1216 char *res;
1217 char *name;
1219 G_LOCK (gid_cache);
1220 name = lookup_gid_name (gid);
1221 res = g_strdup (name);
1222 G_UNLOCK (gid_cache);
1223 return res;
1226 #endif /* !G_OS_WIN32 */
1228 static char *
1229 get_content_type (const char *basename,
1230 const char *path,
1231 GLocalFileStat *statbuf,
1232 gboolean is_symlink,
1233 gboolean symlink_broken,
1234 GFileQueryInfoFlags flags,
1235 gboolean fast)
1237 if (is_symlink &&
1238 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1239 return g_content_type_from_mime_type ("inode/symlink");
1240 else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
1241 return g_content_type_from_mime_type ("inode/directory");
1242 #ifndef G_OS_WIN32
1243 else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
1244 return g_content_type_from_mime_type ("inode/chardevice");
1245 else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
1246 return g_content_type_from_mime_type ("inode/blockdevice");
1247 else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
1248 return g_content_type_from_mime_type ("inode/fifo");
1249 else if (statbuf != NULL && S_ISREG(statbuf->st_mode) && statbuf->st_size == 0)
1251 /* Don't sniff zero-length files in order to avoid reading files
1252 * that appear normal but are not (eg: files in /proc and /sys)
1254 * Note that we need to return text/plain here so that
1255 * newly-created text files are opened by the text editor.
1256 * See https://bugzilla.gnome.org/show_bug.cgi?id=755795
1258 return g_content_type_from_mime_type ("text/plain");
1260 #endif
1261 #ifdef S_ISSOCK
1262 else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
1263 return g_content_type_from_mime_type ("inode/socket");
1264 #endif
1265 else
1267 char *content_type;
1268 gboolean result_uncertain;
1270 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1272 #if !defined(G_OS_WIN32) && !defined(HAVE_COCOA)
1273 if (!fast && result_uncertain && path != NULL)
1275 guchar sniff_buffer[4096];
1276 gsize sniff_length;
1277 int fd, errsv;
1279 sniff_length = _g_unix_content_type_get_sniff_len ();
1280 if (sniff_length > 4096)
1281 sniff_length = 4096;
1283 #ifdef O_NOATIME
1284 fd = g_open (path, O_RDONLY | O_NOATIME, 0);
1285 errsv = errno;
1286 if (fd < 0 && errsv == EPERM)
1287 #endif
1288 fd = g_open (path, O_RDONLY, 0);
1290 if (fd != -1)
1292 ssize_t res;
1294 res = read (fd, sniff_buffer, sniff_length);
1295 (void) g_close (fd, NULL);
1296 if (res >= 0)
1298 g_free (content_type);
1299 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1303 #endif
1305 return content_type;
1310 /* @stat_buf is the pre-calculated result of stat(path), or %NULL if that failed. */
1311 static void
1312 get_thumbnail_attributes (const char *path,
1313 GFileInfo *info,
1314 const GLocalFileStat *stat_buf)
1316 GChecksum *checksum;
1317 char *uri;
1318 char *filename;
1319 char *basename;
1321 uri = g_filename_to_uri (path, NULL, NULL);
1323 checksum = g_checksum_new (G_CHECKSUM_MD5);
1324 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1326 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1327 g_checksum_free (checksum);
1329 filename = g_build_filename (g_get_user_cache_dir (),
1330 "thumbnails", "large", basename,
1331 NULL);
1333 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1335 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1336 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1337 thumbnail_verify (filename, uri, stat_buf));
1339 else
1341 g_free (filename);
1342 filename = g_build_filename (g_get_user_cache_dir (),
1343 "thumbnails", "normal", basename,
1344 NULL);
1346 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1348 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1349 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1350 thumbnail_verify (filename, uri, stat_buf));
1352 else
1354 g_free (filename);
1355 filename = g_build_filename (g_get_user_cache_dir (),
1356 "thumbnails", "fail",
1357 "gnome-thumbnail-factory",
1358 basename,
1359 NULL);
1361 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1363 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1364 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1365 thumbnail_verify (filename, uri, stat_buf));
1369 g_free (basename);
1370 g_free (filename);
1371 g_free (uri);
1374 #ifdef G_OS_WIN32
1375 static void
1376 win32_get_file_user_info (const gchar *filename,
1377 gchar **group_name,
1378 gchar **user_name,
1379 gchar **real_name)
1381 PSECURITY_DESCRIPTOR psd = NULL;
1382 DWORD sd_size = 0; /* first call calculates the size required */
1384 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1385 if ((GetFileSecurityW (wfilename,
1386 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1387 NULL,
1388 sd_size,
1389 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1390 (psd = g_try_malloc (sd_size)) != NULL &&
1391 GetFileSecurityW (wfilename,
1392 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1393 psd,
1394 sd_size,
1395 &sd_size))
1397 PSID psid = 0;
1398 BOOL defaulted;
1399 SID_NAME_USE name_use = 0; /* don't care? */
1400 wchar_t *name = NULL;
1401 wchar_t *domain = NULL;
1402 DWORD name_len = 0;
1403 DWORD domain_len = 0;
1404 /* get the user name */
1405 do {
1406 if (!user_name)
1407 break;
1408 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1409 break;
1410 if (!LookupAccountSidW (NULL, /* local machine */
1411 psid,
1412 name, &name_len,
1413 domain, &domain_len, /* no domain info yet */
1414 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1415 break;
1416 name = g_try_malloc (name_len * sizeof (wchar_t));
1417 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1418 if (name && domain &&
1419 LookupAccountSidW (NULL, /* local machine */
1420 psid,
1421 name, &name_len,
1422 domain, &domain_len, /* no domain info yet */
1423 &name_use))
1425 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1427 g_free (name);
1428 g_free (domain);
1429 } while (FALSE);
1431 /* get the group name */
1432 do {
1433 if (!group_name)
1434 break;
1435 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1436 break;
1437 if (!LookupAccountSidW (NULL, /* local machine */
1438 psid,
1439 name, &name_len,
1440 domain, &domain_len, /* no domain info yet */
1441 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1442 break;
1443 name = g_try_malloc (name_len * sizeof (wchar_t));
1444 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1445 if (name && domain &&
1446 LookupAccountSidW (NULL, /* local machine */
1447 psid,
1448 name, &name_len,
1449 domain, &domain_len, /* no domain info yet */
1450 &name_use))
1452 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1454 g_free (name);
1455 g_free (domain);
1456 } while (FALSE);
1458 /* TODO: get real name */
1460 g_free (psd);
1462 g_free (wfilename);
1464 #endif /* G_OS_WIN32 */
1466 #ifndef G_OS_WIN32
1467 /* support for '.hidden' files */
1468 G_LOCK_DEFINE_STATIC (hidden_cache);
1469 static GHashTable *hidden_cache;
1471 static gboolean
1472 remove_from_hidden_cache (gpointer user_data)
1474 G_LOCK (hidden_cache);
1475 g_hash_table_remove (hidden_cache, user_data);
1476 G_UNLOCK (hidden_cache);
1478 return FALSE;
1481 static GHashTable *
1482 read_hidden_file (const gchar *dirname)
1484 gchar *contents = NULL;
1485 gchar *filename;
1487 filename = g_build_path ("/", dirname, ".hidden", NULL);
1488 (void) g_file_get_contents (filename, &contents, NULL, NULL);
1489 g_free (filename);
1491 if (contents != NULL)
1493 GHashTable *table;
1494 gchar **lines;
1495 gint i;
1497 table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1499 lines = g_strsplit (contents, "\n", 0);
1500 g_free (contents);
1502 for (i = 0; lines[i]; i++)
1503 /* hash table takes the individual strings... */
1504 g_hash_table_add (table, lines[i]);
1506 /* ... so we only free the container. */
1507 g_free (lines);
1509 return table;
1511 else
1512 return NULL;
1515 static void
1516 maybe_unref_hash_table (gpointer data)
1518 if (data != NULL)
1519 g_hash_table_unref (data);
1522 static gboolean
1523 file_is_hidden (const gchar *path,
1524 const gchar *basename)
1526 gboolean result;
1527 gchar *dirname;
1528 gpointer table;
1530 dirname = g_path_get_dirname (path);
1532 G_LOCK (hidden_cache);
1534 if G_UNLIKELY (hidden_cache == NULL)
1535 hidden_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1536 g_free, maybe_unref_hash_table);
1538 if (!g_hash_table_lookup_extended (hidden_cache, dirname,
1539 NULL, &table))
1541 gchar *mydirname;
1542 GSource *remove_from_cache_source;
1544 g_hash_table_insert (hidden_cache,
1545 mydirname = g_strdup (dirname),
1546 table = read_hidden_file (dirname));
1548 remove_from_cache_source = g_timeout_source_new_seconds (5);
1549 g_source_set_priority (remove_from_cache_source, G_PRIORITY_DEFAULT);
1550 g_source_set_callback (remove_from_cache_source,
1551 remove_from_hidden_cache,
1552 mydirname,
1553 NULL);
1554 g_source_attach (remove_from_cache_source,
1555 GLIB_PRIVATE_CALL (g_get_worker_context) ());
1556 g_source_unref (remove_from_cache_source);
1559 result = table != NULL && g_hash_table_contains (table, basename);
1561 G_UNLOCK (hidden_cache);
1563 g_free (dirname);
1565 return result;
1567 #endif /* !G_OS_WIN32 */
1569 void
1570 _g_local_file_info_get_nostat (GFileInfo *info,
1571 const char *basename,
1572 const char *path,
1573 GFileAttributeMatcher *attribute_matcher)
1575 g_file_info_set_name (info, basename);
1577 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1578 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1580 char *display_name = g_filename_display_basename (path);
1582 /* look for U+FFFD REPLACEMENT CHARACTER */
1583 if (strstr (display_name, "\357\277\275") != NULL)
1585 char *p = display_name;
1586 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1587 g_free (p);
1589 g_file_info_set_display_name (info, display_name);
1590 g_free (display_name);
1593 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1594 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1596 char *edit_name = g_filename_display_basename (path);
1597 g_file_info_set_edit_name (info, edit_name);
1598 g_free (edit_name);
1602 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1603 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1605 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1606 if (copy_name)
1607 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1608 g_free (copy_name);
1612 static const char *
1613 get_icon_name (const char *path,
1614 const char *content_type,
1615 gboolean use_symbolic,
1616 gboolean *with_fallbacks_out)
1618 const char *name = NULL;
1619 gboolean with_fallbacks = TRUE;
1621 if (strcmp (path, g_get_home_dir ()) == 0)
1623 name = use_symbolic ? "user-home-symbolic" : "user-home";
1624 with_fallbacks = FALSE;
1626 else if (strcmp (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1628 name = use_symbolic ? "user-desktop-symbolic" : "user-desktop";
1629 with_fallbacks = FALSE;
1631 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1633 name = use_symbolic ? "folder-documents-symbolic" : "folder-documents";
1635 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1637 name = use_symbolic ? "folder-download-symbolic" : "folder-download";
1639 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1641 name = use_symbolic ? "folder-music-symbolic" : "folder-music";
1643 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1645 name = use_symbolic ? "folder-pictures-symbolic" : "folder-pictures";
1647 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1649 name = use_symbolic ? "folder-publicshare-symbolic" : "folder-publicshare";
1651 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1653 name = use_symbolic ? "folder-templates-symbolic" : "folder-templates";
1655 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1657 name = use_symbolic ? "folder-videos-symbolic" : "folder-videos";
1659 else if (g_strcmp0 (content_type, "inode/directory") == 0)
1661 name = use_symbolic ? "folder-symbolic" : "folder";
1663 else
1665 name = NULL;
1668 if (with_fallbacks_out != NULL)
1669 *with_fallbacks_out = with_fallbacks;
1671 return name;
1674 static GIcon *
1675 get_icon (const char *path,
1676 const char *content_type,
1677 gboolean use_symbolic)
1679 GIcon *icon = NULL;
1680 const char *icon_name;
1681 gboolean with_fallbacks;
1683 icon_name = get_icon_name (path, content_type, use_symbolic, &with_fallbacks);
1684 if (icon_name != NULL)
1686 if (with_fallbacks)
1687 icon = g_themed_icon_new_with_default_fallbacks (icon_name);
1688 else
1689 icon = g_themed_icon_new (icon_name);
1691 else
1693 if (use_symbolic)
1694 icon = g_content_type_get_symbolic_icon (content_type);
1695 else
1696 icon = g_content_type_get_icon (content_type);
1699 return icon;
1702 GFileInfo *
1703 _g_local_file_info_get (const char *basename,
1704 const char *path,
1705 GFileAttributeMatcher *attribute_matcher,
1706 GFileQueryInfoFlags flags,
1707 GLocalParentFileInfo *parent_info,
1708 GError **error)
1710 GFileInfo *info;
1711 GLocalFileStat statbuf;
1712 #ifdef S_ISLNK
1713 struct stat statbuf2;
1714 #endif
1715 int res;
1716 gboolean stat_ok;
1717 gboolean is_symlink, symlink_broken;
1718 #ifdef G_OS_WIN32
1719 DWORD dos_attributes;
1720 #endif
1721 char *symlink_target;
1722 GVfs *vfs;
1723 GVfsClass *class;
1724 guint64 device;
1726 info = g_file_info_new ();
1728 /* Make sure we don't set any unwanted attributes */
1729 g_file_info_set_attribute_mask (info, attribute_matcher);
1731 _g_local_file_info_get_nostat (info, basename, path, attribute_matcher);
1733 if (attribute_matcher == NULL)
1735 g_file_info_unset_attribute_mask (info);
1736 return info;
1739 #ifndef G_OS_WIN32
1740 res = g_lstat (path, &statbuf);
1741 #else
1743 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
1744 int len;
1746 if (wpath == NULL)
1748 g_object_unref (info);
1749 return NULL;
1752 len = wcslen (wpath);
1753 while (len > 0 && G_IS_DIR_SEPARATOR (wpath[len-1]))
1754 len--;
1755 if (len > 0 &&
1756 (!g_path_is_absolute (path) || len > g_path_skip_root (path) - path))
1757 wpath[len] = '\0';
1759 res = _wstati64 (wpath, &statbuf);
1760 dos_attributes = GetFileAttributesW (wpath);
1762 g_free (wpath);
1764 #endif
1766 if (res == -1)
1768 int errsv = errno;
1770 /* Don't bail out if we get Permission denied (SELinux?) */
1771 if (errsv != EACCES)
1773 char *display_name = g_filename_display_name (path);
1774 g_object_unref (info);
1775 g_set_error (error, G_IO_ERROR,
1776 g_io_error_from_errno (errsv),
1777 _("Error when getting information for file “%s”: %s"),
1778 display_name, g_strerror (errsv));
1779 g_free (display_name);
1780 return NULL;
1784 /* Even if stat() fails, try to get as much as other attributes possible */
1785 stat_ok = res != -1;
1787 if (stat_ok)
1788 device = statbuf.st_dev;
1789 else
1790 device = 0;
1792 #ifdef S_ISLNK
1793 is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
1794 #else
1795 is_symlink = FALSE;
1796 #endif
1797 symlink_broken = FALSE;
1798 #ifdef S_ISLNK
1799 if (is_symlink)
1801 g_file_info_set_is_symlink (info, TRUE);
1803 /* Unless NOFOLLOW was set we default to following symlinks */
1804 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1806 res = stat (path, &statbuf2);
1808 /* Report broken links as symlinks */
1809 if (res != -1)
1811 statbuf = statbuf2;
1812 stat_ok = TRUE;
1814 else
1815 symlink_broken = TRUE;
1818 #endif
1820 if (stat_ok)
1821 set_info_from_stat (info, &statbuf, attribute_matcher);
1823 #ifdef G_OS_UNIX
1824 if (stat_ok && _g_local_file_is_lost_found_dir (path, statbuf.st_dev))
1825 g_file_info_set_is_hidden (info, TRUE);
1826 #endif
1828 #ifndef G_OS_WIN32
1829 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1830 G_FILE_ATTRIBUTE_ID_STANDARD_IS_HIDDEN))
1832 if (basename != NULL &&
1833 (basename[0] == '.' ||
1834 file_is_hidden (path, basename)))
1835 g_file_info_set_is_hidden (info, TRUE);
1838 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1839 (stat_ok && S_ISREG (statbuf.st_mode)))
1840 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1841 #else
1842 if (dos_attributes & FILE_ATTRIBUTE_HIDDEN)
1843 g_file_info_set_is_hidden (info, TRUE);
1845 if (dos_attributes & FILE_ATTRIBUTE_ARCHIVE)
1846 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1848 if (dos_attributes & FILE_ATTRIBUTE_SYSTEM)
1849 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1850 #endif
1852 symlink_target = NULL;
1853 #ifdef S_ISLNK
1854 if (is_symlink)
1856 symlink_target = read_link (path);
1857 if (symlink_target &&
1858 _g_file_attribute_matcher_matches_id (attribute_matcher,
1859 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1860 g_file_info_set_symlink_target (info, symlink_target);
1862 #endif
1863 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1864 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1865 _g_file_attribute_matcher_matches_id (attribute_matcher,
1866 G_FILE_ATTRIBUTE_ID_STANDARD_ICON) ||
1867 _g_file_attribute_matcher_matches_id (attribute_matcher,
1868 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1870 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
1872 if (content_type)
1874 g_file_info_set_content_type (info, content_type);
1876 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1877 G_FILE_ATTRIBUTE_ID_STANDARD_ICON)
1878 || _g_file_attribute_matcher_matches_id (attribute_matcher,
1879 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1881 GIcon *icon;
1883 /* non symbolic icon */
1884 icon = get_icon (path, content_type, FALSE);
1885 if (icon != NULL)
1887 g_file_info_set_icon (info, icon);
1888 g_object_unref (icon);
1891 /* symbolic icon */
1892 icon = get_icon (path, content_type, TRUE);
1893 if (icon != NULL)
1895 g_file_info_set_symbolic_icon (info, icon);
1896 g_object_unref (icon);
1901 g_free (content_type);
1905 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1906 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1908 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
1910 if (content_type)
1912 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1913 g_free (content_type);
1917 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1918 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1920 char *name = NULL;
1922 #ifdef G_OS_WIN32
1923 win32_get_file_user_info (path, NULL, &name, NULL);
1924 #else
1925 if (stat_ok)
1926 name = get_username_from_uid (statbuf.st_uid);
1927 #endif
1928 if (name)
1929 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1930 g_free (name);
1933 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1934 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1936 char *name = NULL;
1937 #ifdef G_OS_WIN32
1938 win32_get_file_user_info (path, NULL, NULL, &name);
1939 #else
1940 if (stat_ok)
1941 name = get_realname_from_uid (statbuf.st_uid);
1942 #endif
1943 if (name)
1944 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1945 g_free (name);
1948 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1949 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1951 char *name = NULL;
1952 #ifdef G_OS_WIN32
1953 win32_get_file_user_info (path, &name, NULL, NULL);
1954 #else
1955 if (stat_ok)
1956 name = get_groupname_from_gid (statbuf.st_gid);
1957 #endif
1958 if (name)
1959 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1960 g_free (name);
1963 if (stat_ok && parent_info && parent_info->device != 0 &&
1964 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1965 statbuf.st_dev != parent_info->device)
1966 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1968 if (stat_ok)
1969 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1971 #ifdef HAVE_SELINUX
1972 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1973 #endif
1974 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1975 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1977 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1978 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH))
1980 if (stat_ok)
1981 get_thumbnail_attributes (path, info, &statbuf);
1982 else
1983 get_thumbnail_attributes (path, info, NULL);
1986 vfs = g_vfs_get_default ();
1987 class = G_VFS_GET_CLASS (vfs);
1988 if (class->local_file_add_info)
1990 class->local_file_add_info (vfs,
1991 path,
1992 device,
1993 attribute_matcher,
1994 info,
1995 NULL,
1996 &parent_info->extra_data,
1997 &parent_info->free_extra_data);
2000 g_file_info_unset_attribute_mask (info);
2002 g_free (symlink_target);
2004 return info;
2007 GFileInfo *
2008 _g_local_file_info_get_from_fd (int fd,
2009 const char *attributes,
2010 GError **error)
2012 GLocalFileStat stat_buf;
2013 GFileAttributeMatcher *matcher;
2014 GFileInfo *info;
2016 #ifdef G_OS_WIN32
2017 #define FSTAT _fstati64
2018 #else
2019 #define FSTAT fstat
2020 #endif
2022 if (FSTAT (fd, &stat_buf) == -1)
2024 int errsv = errno;
2026 g_set_error (error, G_IO_ERROR,
2027 g_io_error_from_errno (errsv),
2028 _("Error when getting information for file descriptor: %s"),
2029 g_strerror (errsv));
2030 return NULL;
2033 info = g_file_info_new ();
2035 matcher = g_file_attribute_matcher_new (attributes);
2037 /* Make sure we don't set any unwanted attributes */
2038 g_file_info_set_attribute_mask (info, matcher);
2040 set_info_from_stat (info, &stat_buf, matcher);
2042 #ifdef HAVE_SELINUX
2043 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
2044 is_selinux_enabled ())
2046 char *context;
2047 if (fgetfilecon_raw (fd, &context) >= 0)
2049 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
2050 freecon (context);
2053 #endif
2055 get_xattrs_from_fd (fd, TRUE, info, matcher);
2056 get_xattrs_from_fd (fd, FALSE, info, matcher);
2058 g_file_attribute_matcher_unref (matcher);
2060 g_file_info_unset_attribute_mask (info);
2062 return info;
2065 static gboolean
2066 get_uint32 (const GFileAttributeValue *value,
2067 guint32 *val_out,
2068 GError **error)
2070 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
2072 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2073 _("Invalid attribute type (uint32 expected)"));
2074 return FALSE;
2077 *val_out = value->u.uint32;
2079 return TRUE;
2082 #ifdef HAVE_UTIMES
2083 static gboolean
2084 get_uint64 (const GFileAttributeValue *value,
2085 guint64 *val_out,
2086 GError **error)
2088 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
2090 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2091 _("Invalid attribute type (uint64 expected)"));
2092 return FALSE;
2095 *val_out = value->u.uint64;
2097 return TRUE;
2099 #endif
2101 #if defined(HAVE_SYMLINK)
2102 static gboolean
2103 get_byte_string (const GFileAttributeValue *value,
2104 const char **val_out,
2105 GError **error)
2107 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
2109 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2110 _("Invalid attribute type (byte string expected)"));
2111 return FALSE;
2114 *val_out = value->u.string;
2116 return TRUE;
2118 #endif
2120 #ifdef HAVE_SELINUX
2121 static gboolean
2122 get_string (const GFileAttributeValue *value,
2123 const char **val_out,
2124 GError **error)
2126 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
2128 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2129 _("Invalid attribute type (byte string expected)"));
2130 return FALSE;
2133 *val_out = value->u.string;
2135 return TRUE;
2137 #endif
2139 static gboolean
2140 set_unix_mode (char *filename,
2141 GFileQueryInfoFlags flags,
2142 const GFileAttributeValue *value,
2143 GError **error)
2145 guint32 val = 0;
2146 int res = 0;
2148 if (!get_uint32 (value, &val, error))
2149 return FALSE;
2151 #ifdef HAVE_SYMLINK
2152 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
2153 #ifdef HAVE_LCHMOD
2154 res = lchmod (filename, val);
2155 #else
2156 struct stat statbuf;
2157 /* Calling chmod on a symlink changes permissions on the symlink.
2158 * We don't want to do this, so we need to check for a symlink */
2159 res = g_lstat (filename, &statbuf);
2160 if (res == 0 && S_ISLNK (statbuf.st_mode))
2162 g_set_error_literal (error, G_IO_ERROR,
2163 G_IO_ERROR_NOT_SUPPORTED,
2164 _("Cannot set permissions on symlinks"));
2165 return FALSE;
2167 else if (res == 0)
2168 res = g_chmod (filename, val);
2169 #endif
2170 } else
2171 #endif
2172 res = g_chmod (filename, val);
2174 if (res == -1)
2176 int errsv = errno;
2178 g_set_error (error, G_IO_ERROR,
2179 g_io_error_from_errno (errsv),
2180 _("Error setting permissions: %s"),
2181 g_strerror (errsv));
2182 return FALSE;
2184 return TRUE;
2187 #ifdef G_OS_UNIX
2188 static gboolean
2189 set_unix_uid_gid (char *filename,
2190 const GFileAttributeValue *uid_value,
2191 const GFileAttributeValue *gid_value,
2192 GFileQueryInfoFlags flags,
2193 GError **error)
2195 int res;
2196 guint32 val = 0;
2197 uid_t uid;
2198 gid_t gid;
2200 if (uid_value)
2202 if (!get_uint32 (uid_value, &val, error))
2203 return FALSE;
2204 uid = val;
2206 else
2207 uid = -1;
2209 if (gid_value)
2211 if (!get_uint32 (gid_value, &val, error))
2212 return FALSE;
2213 gid = val;
2215 else
2216 gid = -1;
2218 #ifdef HAVE_LCHOWN
2219 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
2220 res = lchown (filename, uid, gid);
2221 else
2222 #endif
2223 res = chown (filename, uid, gid);
2225 if (res == -1)
2227 int errsv = errno;
2229 g_set_error (error, G_IO_ERROR,
2230 g_io_error_from_errno (errsv),
2231 _("Error setting owner: %s"),
2232 g_strerror (errsv));
2233 return FALSE;
2235 return TRUE;
2237 #endif
2239 #ifdef HAVE_SYMLINK
2240 static gboolean
2241 set_symlink (char *filename,
2242 const GFileAttributeValue *value,
2243 GError **error)
2245 const char *val;
2246 struct stat statbuf;
2248 if (!get_byte_string (value, &val, error))
2249 return FALSE;
2251 if (val == NULL)
2253 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2254 _("symlink must be non-NULL"));
2255 return FALSE;
2258 if (g_lstat (filename, &statbuf))
2260 int errsv = errno;
2262 g_set_error (error, G_IO_ERROR,
2263 g_io_error_from_errno (errsv),
2264 _("Error setting symlink: %s"),
2265 g_strerror (errsv));
2266 return FALSE;
2269 if (!S_ISLNK (statbuf.st_mode))
2271 g_set_error_literal (error, G_IO_ERROR,
2272 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2273 _("Error setting symlink: file is not a symlink"));
2274 return FALSE;
2277 if (g_unlink (filename))
2279 int errsv = errno;
2281 g_set_error (error, G_IO_ERROR,
2282 g_io_error_from_errno (errsv),
2283 _("Error setting symlink: %s"),
2284 g_strerror (errsv));
2285 return FALSE;
2288 if (symlink (filename, val) != 0)
2290 int errsv = errno;
2292 g_set_error (error, G_IO_ERROR,
2293 g_io_error_from_errno (errsv),
2294 _("Error setting symlink: %s"),
2295 g_strerror (errsv));
2296 return FALSE;
2299 return TRUE;
2301 #endif
2303 #ifdef HAVE_UTIMES
2304 static int
2305 lazy_stat (char *filename,
2306 struct stat *statbuf,
2307 gboolean *called_stat)
2309 int res;
2311 if (*called_stat)
2312 return 0;
2314 res = g_stat (filename, statbuf);
2316 if (res == 0)
2317 *called_stat = TRUE;
2319 return res;
2323 static gboolean
2324 set_mtime_atime (char *filename,
2325 const GFileAttributeValue *mtime_value,
2326 const GFileAttributeValue *mtime_usec_value,
2327 const GFileAttributeValue *atime_value,
2328 const GFileAttributeValue *atime_usec_value,
2329 GError **error)
2331 int res;
2332 guint64 val = 0;
2333 guint32 val_usec = 0;
2334 struct stat statbuf;
2335 gboolean got_stat = FALSE;
2336 struct timeval times[2] = { {0, 0}, {0, 0} };
2338 /* ATIME */
2339 if (atime_value)
2341 if (!get_uint64 (atime_value, &val, error))
2342 return FALSE;
2343 times[0].tv_sec = val;
2345 else
2347 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2349 times[0].tv_sec = statbuf.st_mtime;
2350 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2351 times[0].tv_usec = statbuf.st_atimensec / 1000;
2352 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2353 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2354 #endif
2358 if (atime_usec_value)
2360 if (!get_uint32 (atime_usec_value, &val_usec, error))
2361 return FALSE;
2362 times[0].tv_usec = val_usec;
2365 /* MTIME */
2366 if (mtime_value)
2368 if (!get_uint64 (mtime_value, &val, error))
2369 return FALSE;
2370 times[1].tv_sec = val;
2372 else
2374 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2376 times[1].tv_sec = statbuf.st_mtime;
2377 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2378 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2379 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2380 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2381 #endif
2385 if (mtime_usec_value)
2387 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2388 return FALSE;
2389 times[1].tv_usec = val_usec;
2392 res = utimes (filename, times);
2393 if (res == -1)
2395 int errsv = errno;
2397 g_set_error (error, G_IO_ERROR,
2398 g_io_error_from_errno (errsv),
2399 _("Error setting modification or access time: %s"),
2400 g_strerror (errsv));
2401 return FALSE;
2403 return TRUE;
2405 #endif
2408 #ifdef HAVE_SELINUX
2409 static gboolean
2410 set_selinux_context (char *filename,
2411 const GFileAttributeValue *value,
2412 GError **error)
2414 const char *val;
2416 if (!get_string (value, &val, error))
2417 return FALSE;
2419 if (val == NULL)
2421 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2422 _("SELinux context must be non-NULL"));
2423 return FALSE;
2426 if (is_selinux_enabled ()) {
2427 security_context_t val_s;
2429 val_s = g_strdup (val);
2431 if (setfilecon_raw (filename, val_s) < 0)
2433 int errsv = errno;
2435 g_set_error (error, G_IO_ERROR,
2436 g_io_error_from_errno (errsv),
2437 _("Error setting SELinux context: %s"),
2438 g_strerror (errsv));
2439 return FALSE;
2441 g_free (val_s);
2442 } else {
2443 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2444 _("SELinux is not enabled on this system"));
2445 return FALSE;
2448 return TRUE;
2450 #endif
2453 gboolean
2454 _g_local_file_info_set_attribute (char *filename,
2455 const char *attribute,
2456 GFileAttributeType type,
2457 gpointer value_p,
2458 GFileQueryInfoFlags flags,
2459 GCancellable *cancellable,
2460 GError **error)
2462 GFileAttributeValue value = { 0 };
2463 GVfsClass *class;
2464 GVfs *vfs;
2466 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2468 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2469 return set_unix_mode (filename, flags, &value, error);
2471 #ifdef G_OS_UNIX
2472 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2473 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2474 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2475 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2476 #endif
2478 #ifdef HAVE_SYMLINK
2479 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2480 return set_symlink (filename, &value, error);
2481 #endif
2483 #ifdef HAVE_UTIMES
2484 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2485 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2486 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2487 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2488 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2489 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2490 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2491 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2492 #endif
2494 #ifdef HAVE_XATTR
2495 else if (g_str_has_prefix (attribute, "xattr::"))
2496 return set_xattr (filename, attribute, &value, error);
2497 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2498 return set_xattr (filename, attribute, &value, error);
2499 #endif
2501 #ifdef HAVE_SELINUX
2502 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2503 return set_selinux_context (filename, &value, error);
2504 #endif
2506 vfs = g_vfs_get_default ();
2507 class = G_VFS_GET_CLASS (vfs);
2508 if (class->local_file_set_attributes)
2510 GFileInfo *info;
2512 info = g_file_info_new ();
2513 g_file_info_set_attribute (info,
2514 attribute,
2515 type,
2516 value_p);
2517 if (!class->local_file_set_attributes (vfs, filename,
2518 info,
2519 flags, cancellable,
2520 error))
2522 g_object_unref (info);
2523 return FALSE;
2526 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2528 g_object_unref (info);
2529 return TRUE;
2532 g_object_unref (info);
2535 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2536 _("Setting attribute %s not supported"), attribute);
2537 return FALSE;
2540 gboolean
2541 _g_local_file_info_set_attributes (char *filename,
2542 GFileInfo *info,
2543 GFileQueryInfoFlags flags,
2544 GCancellable *cancellable,
2545 GError **error)
2547 GFileAttributeValue *value;
2548 #ifdef G_OS_UNIX
2549 GFileAttributeValue *uid, *gid;
2550 #ifdef HAVE_UTIMES
2551 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2552 #endif
2553 GFileAttributeStatus status;
2554 #endif
2555 gboolean res;
2556 GVfsClass *class;
2557 GVfs *vfs;
2559 /* Handles setting multiple specified data in a single set, and takes care
2560 of ordering restrictions when setting attributes */
2562 res = TRUE;
2564 /* Set symlink first, since this recreates the file */
2565 #ifdef HAVE_SYMLINK
2566 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2567 if (value)
2569 if (!set_symlink (filename, value, error))
2571 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2572 res = FALSE;
2573 /* Don't set error multiple times */
2574 error = NULL;
2576 else
2577 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2580 #endif
2582 #ifdef G_OS_UNIX
2583 /* Group uid and gid setting into one call
2584 * Change ownership before permissions, since ownership changes can
2585 change permissions (e.g. setuid)
2587 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2588 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2590 if (uid || gid)
2592 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2594 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2595 res = FALSE;
2596 /* Don't set error multiple times */
2597 error = NULL;
2599 else
2600 status = G_FILE_ATTRIBUTE_STATUS_SET;
2601 if (uid)
2602 uid->status = status;
2603 if (gid)
2604 gid->status = status;
2606 #endif
2608 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2609 if (value)
2611 if (!set_unix_mode (filename, flags, value, error))
2613 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2614 res = FALSE;
2615 /* Don't set error multiple times */
2616 error = NULL;
2618 else
2619 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2623 #ifdef HAVE_UTIMES
2624 /* Group all time settings into one call
2625 * Change times as the last thing to avoid it changing due to metadata changes
2628 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2629 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2630 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2631 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2633 if (mtime || mtime_usec || atime || atime_usec)
2635 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2637 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2638 res = FALSE;
2639 /* Don't set error multiple times */
2640 error = NULL;
2642 else
2643 status = G_FILE_ATTRIBUTE_STATUS_SET;
2645 if (mtime)
2646 mtime->status = status;
2647 if (mtime_usec)
2648 mtime_usec->status = status;
2649 if (atime)
2650 atime->status = status;
2651 if (atime_usec)
2652 atime_usec->status = status;
2654 #endif
2656 /* xattrs are handled by default callback */
2659 /* SELinux context */
2660 #ifdef HAVE_SELINUX
2661 if (is_selinux_enabled ()) {
2662 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2663 if (value)
2665 if (!set_selinux_context (filename, value, error))
2667 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2668 res = FALSE;
2669 /* Don't set error multiple times */
2670 error = NULL;
2672 else
2673 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2676 #endif
2678 vfs = g_vfs_get_default ();
2679 class = G_VFS_GET_CLASS (vfs);
2680 if (class->local_file_set_attributes)
2682 if (!class->local_file_set_attributes (vfs, filename,
2683 info,
2684 flags, cancellable,
2685 error))
2687 res = FALSE;
2688 /* Don't set error multiple times */
2689 error = NULL;
2693 return res;