ci: collect test coverage and deploy a html report through gitlab pages
[glib.git] / gio / glocalfileinfo.c
blob801695ad09a82309dada8c9baa6fdfa5656a6011
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 <glib/gstdioprivate.h>
57 #include <gfileattribute-priv.h>
58 #include <gfileinfo-priv.h>
59 #include <gvfs.h>
61 #ifdef G_OS_UNIX
62 #include <unistd.h>
63 #include "glib-unix.h"
64 #endif
66 #include "glib-private.h"
68 #include "thumbnail-verify.h"
70 #ifdef G_OS_WIN32
71 #include <windows.h>
72 #include <io.h>
73 #ifndef W_OK
74 #define W_OK 2
75 #endif
76 #ifndef R_OK
77 #define R_OK 4
78 #endif
79 #ifndef X_OK
80 #define X_OK 0 /* not really */
81 #endif
82 #ifndef S_ISREG
83 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
84 #endif
85 #ifndef S_ISDIR
86 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
87 #endif
88 #ifndef S_IXUSR
89 #define S_IXUSR _S_IEXEC
90 #endif
91 #endif
93 #include "glocalfileinfo.h"
94 #include "gioerror.h"
95 #include "gthemedicon.h"
96 #include "gcontenttypeprivate.h"
97 #include "glibintl.h"
100 struct ThumbMD5Context {
101 guint32 buf[4];
102 guint32 bits[2];
103 unsigned char in[64];
106 #ifndef G_OS_WIN32
108 typedef struct {
109 char *user_name;
110 char *real_name;
111 } UidData;
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 */
121 char *
122 _g_local_file_info_create_etag (GLocalFileStat *statbuf)
124 glong sec, usec;
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;
131 #else
132 usec = 0;
133 #endif
135 return g_strdup_printf ("%lu:%lu", sec, usec);
138 static char *
139 _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
141 guint64 ino;
142 #ifdef G_OS_WIN32
143 ino = statbuf->file_index;
144 #else
145 ino = statbuf->st_ino;
146 #endif
147 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
148 (guint64) statbuf->st_dev,
149 ino);
152 static char *
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)
161 static gchar *
162 read_link (const gchar *full_name)
164 #if defined (HAVE_READLINK) || defined (G_OS_WIN32)
165 gchar *buffer;
166 guint size;
168 size = 256;
169 buffer = g_malloc (size);
171 while (1)
173 int read_size;
175 #ifndef G_OS_WIN32
176 read_size = readlink (full_name, buffer, size);
177 #else
178 read_size = GLIB_PRIVATE_CALL (g_win32_readlink_utf8) (full_name, buffer, size);
179 #endif
180 if (read_size < 0)
182 g_free (buffer);
183 return NULL;
185 if (read_size < size)
187 buffer[read_size] = 0;
188 return buffer;
190 size *= 2;
191 buffer = g_realloc (buffer, size);
193 #else
194 return NULL;
195 #endif
198 #endif /* S_ISLNK || G_OS_WIN32 */
200 #ifdef HAVE_SELINUX
201 /* Get the SELinux security context */
202 static void
203 get_selinux_context (const char *path,
204 GFileInfo *info,
205 GFileAttributeMatcher *attribute_matcher,
206 gboolean follow_symlinks)
208 char *context;
210 if (!_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT))
211 return;
213 if (is_selinux_enabled ())
215 if (follow_symlinks)
217 if (lgetfilecon_raw (path, &context) < 0)
218 return;
220 else
222 if (getfilecon_raw (path, &context) < 0)
223 return;
226 if (context)
228 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
229 freecon (context);
233 #endif
235 #ifdef HAVE_XATTR
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)
244 #else
245 #define g_fgetxattr fgetxattr
246 #define g_flistxattr flistxattr
247 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
248 #endif
250 static gssize
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);
256 #else
257 if (follow_symlinks)
258 return getxattr (path, name, value, size);
259 else
260 return lgetxattr (path, name, value, size);
261 #endif
264 static gssize
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);
270 #else
271 if (follow_symlinks)
272 return listxattr (path, namebuf, size);
273 else
274 return llistxattr (path, namebuf, size);
275 #endif
278 static gboolean
279 valid_char (char c)
281 return c >= 32 && c <= 126 && c != '\\';
284 static gboolean
285 name_is_valid (const char *str)
287 while (*str)
289 if (!valid_char (*str++))
290 return FALSE;
292 return TRUE;
295 static char *
296 hex_escape_string (const char *str,
297 gboolean *free_return)
299 int num_invalid, i;
300 char *escaped_str, *p;
301 unsigned char c;
302 static char *hex_digits = "0123456789abcdef";
303 int len;
305 len = strlen (str);
307 num_invalid = 0;
308 for (i = 0; i < len; i++)
310 if (!valid_char (str[i]))
311 num_invalid++;
314 if (num_invalid == 0)
316 *free_return = FALSE;
317 return (char *)str;
320 escaped_str = g_malloc (len + num_invalid*3 + 1);
322 p = escaped_str;
323 for (i = 0; i < len; i++)
325 if (valid_char (str[i]))
326 *p++ = str[i];
327 else
329 c = str[i];
330 *p++ = '\\';
331 *p++ = 'x';
332 *p++ = hex_digits[(c >> 4) & 0xf];
333 *p++ = hex_digits[c & 0xf];
336 *p = 0;
338 *free_return = TRUE;
339 return escaped_str;
342 static char *
343 hex_unescape_string (const char *str,
344 int *out_len,
345 gboolean *free_return)
347 int i;
348 char *unescaped_str, *p;
349 unsigned char c;
350 int len;
352 len = strlen (str);
354 if (strchr (str, '\\') == NULL)
356 if (out_len)
357 *out_len = len;
358 *free_return = FALSE;
359 return (char *)str;
362 unescaped_str = g_malloc (len + 1);
364 p = unescaped_str;
365 for (i = 0; i < len; i++)
367 if (str[i] == '\\' &&
368 str[i+1] == 'x' &&
369 len - i >= 4)
372 (g_ascii_xdigit_value (str[i+2]) << 4) |
373 g_ascii_xdigit_value (str[i+3]);
374 *p++ = c;
375 i += 3;
377 else
378 *p++ = str[i];
380 *p++ = 0;
382 if (out_len)
383 *out_len = p - unescaped_str;
384 *free_return = TRUE;
385 return unescaped_str;
388 static void
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 */)
394 char *escaped_val;
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);
405 static void
406 get_one_xattr (const char *path,
407 GFileInfo *info,
408 const char *gio_attr,
409 const char *xattr,
410 gboolean follow_symlinks)
412 char value[64];
413 char *value_p;
414 gssize len;
415 int errsv;
417 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
418 errsv = errno;
420 value_p = NULL;
421 if (len >= 0)
422 value_p = value;
423 else if (len == -1 && errsv == ERANGE)
425 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
427 if (len < 0)
428 return;
430 value_p = g_malloc (len+1);
432 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
434 if (len < 0)
436 g_free (value_p);
437 return;
440 else
441 return;
443 /* Null terminate */
444 value_p[len] = 0;
446 escape_xattr (info, gio_attr, value_p, len);
448 if (value_p != value)
449 g_free (value_p);
452 #endif /* defined HAVE_XATTR */
454 static void
455 get_xattrs (const char *path,
456 gboolean user,
457 GFileInfo *info,
458 GFileAttributeMatcher *matcher,
459 gboolean follow_symlinks)
461 #ifdef HAVE_XATTR
462 gboolean all;
463 gsize list_size;
464 gssize list_res_size;
465 size_t len;
466 char *list;
467 const char *attr, *attr2;
469 if (user)
470 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
471 else
472 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
474 if (all)
476 int errsv;
478 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
480 if (list_res_size == -1 ||
481 list_res_size == 0)
482 return;
484 list_size = list_res_size;
485 list = g_malloc (list_size);
487 retry:
489 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
490 errsv = errno;
492 if (list_res_size == -1 && errsv == ERANGE)
494 list_size = list_size * 2;
495 list = g_realloc (list, list_size);
496 goto retry;
499 if (list_res_size == -1)
500 return;
502 attr = list;
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;
511 if (user)
513 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
514 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
516 else
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);
527 g_free (gio_attr);
530 len = strlen (attr) + 1;
531 attr += len;
532 list_res_size -= len;
535 g_free (list);
537 else
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, ':');
545 if (attr2)
547 attr2 += 2; /* Skip '::' */
548 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
549 if (user)
550 a = g_strconcat ("user.", unescaped_attribute, NULL);
551 else
552 a = unescaped_attribute;
554 get_one_xattr (path, info, attr, a, follow_symlinks);
556 if (user)
557 g_free (a);
559 if (free_unescaped_attribute)
560 g_free (unescaped_attribute);
564 #endif /* defined HAVE_XATTR */
567 #ifdef HAVE_XATTR
568 static void
569 get_one_xattr_from_fd (int fd,
570 GFileInfo *info,
571 const char *gio_attr,
572 const char *xattr)
574 char value[64];
575 char *value_p;
576 gssize len;
577 int errsv;
579 len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
580 errsv = errno;
582 value_p = NULL;
583 if (len >= 0)
584 value_p = value;
585 else if (len == -1 && errsv == ERANGE)
587 len = g_fgetxattr (fd, xattr, NULL, 0);
589 if (len < 0)
590 return;
592 value_p = g_malloc (len + 1);
594 len = g_fgetxattr (fd, xattr, value_p, len);
596 if (len < 0)
598 g_free (value_p);
599 return;
602 else
603 return;
605 /* Null terminate */
606 value_p[len] = 0;
608 escape_xattr (info, gio_attr, value_p, len);
610 if (value_p != value)
611 g_free (value_p);
613 #endif /* defined HAVE_XATTR */
615 static void
616 get_xattrs_from_fd (int fd,
617 gboolean user,
618 GFileInfo *info,
619 GFileAttributeMatcher *matcher)
621 #ifdef HAVE_XATTR
622 gboolean all;
623 gsize list_size;
624 gssize list_res_size;
625 size_t len;
626 char *list;
627 const char *attr, *attr2;
629 if (user)
630 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
631 else
632 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
634 if (all)
636 int errsv;
638 list_res_size = g_flistxattr (fd, NULL, 0);
640 if (list_res_size == -1 ||
641 list_res_size == 0)
642 return;
644 list_size = list_res_size;
645 list = g_malloc (list_size);
647 retry:
649 list_res_size = g_flistxattr (fd, list, list_size);
650 errsv = errno;
652 if (list_res_size == -1 && errsv == ERANGE)
654 list_size = list_size * 2;
655 list = g_realloc (list, list_size);
656 goto retry;
659 if (list_res_size == -1)
661 g_free (list);
662 return;
665 attr = list;
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;
674 if (user)
676 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
677 gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
679 else
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);
689 g_free (gio_attr);
692 len = strlen (attr) + 1;
693 attr += len;
694 list_res_size -= len;
697 g_free (list);
699 else
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, ':');
707 if (attr2)
709 attr2++; /* Skip ':' */
710 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
711 if (user)
712 a = g_strconcat ("user.", unescaped_attribute, NULL);
713 else
714 a = unescaped_attribute;
716 get_one_xattr_from_fd (fd, info, attr, a);
718 if (user)
719 g_free (a);
721 if (free_unescaped_attribute)
722 g_free (unescaped_attribute);
726 #endif /* defined HAVE_XATTR */
729 #ifdef HAVE_XATTR
730 static gboolean
731 set_xattr (char *filename,
732 const char *escaped_attribute,
733 const GFileAttributeValue *attr_value,
734 GError **error)
736 char *attribute, *value;
737 gboolean free_attribute, free_value;
738 int val_len, res, errsv;
739 gboolean is_user;
740 char *a;
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"));
746 return FALSE;
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)"));
753 return FALSE;
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"));
760 return FALSE;
763 if (g_str_has_prefix (escaped_attribute, "xattr::"))
765 escaped_attribute += strlen ("xattr::");
766 is_user = TRUE;
768 else
770 g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
771 escaped_attribute += strlen ("xattr-sys::");
772 is_user = FALSE;
775 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
776 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
778 if (is_user)
779 a = g_strconcat ("user.", attribute, NULL);
780 else
781 a = attribute;
783 res = g_setxattr (filename, a, value, val_len);
784 errsv = errno;
786 if (is_user)
787 g_free (a);
789 if (free_attribute)
790 g_free (attribute);
792 if (free_value)
793 g_free (value);
795 if (res == -1)
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));
801 return FALSE;
804 return TRUE;
807 #endif
810 void
811 _g_local_file_info_get_parent_info (const char *dir,
812 GFileAttributeMatcher *attribute_matcher,
813 GLocalParentFileInfo *parent_info)
815 GStatBuf statbuf;
816 int res;
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;
825 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME) ||
826 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE) ||
827 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH) ||
828 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT))
830 /* FIXME: Windows: The underlying _waccess() call in the C
831 * library is mostly pointless as it only looks at the READONLY
832 * FAT-style attribute of the file, it doesn't check the ACL at
833 * all.
835 parent_info->writable = (g_access (dir, W_OK) == 0);
837 res = g_stat (dir, &statbuf);
840 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
841 * renamed or deleted only by the owner of the file, by the owner of the directory, and
842 * by a privileged process.
844 if (res == 0)
846 #ifdef S_ISVTX
847 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
848 #else
849 parent_info->is_sticky = FALSE;
850 #endif
851 parent_info->owner = statbuf.st_uid;
852 parent_info->device = statbuf.st_dev;
853 /* No need to find trash dir if it's not writable anyway */
854 if (parent_info->writable &&
855 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
856 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
861 void
862 _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info)
864 if (parent_info->extra_data &&
865 parent_info->free_extra_data)
866 parent_info->free_extra_data (parent_info->extra_data);
869 static void
870 get_access_rights (GFileAttributeMatcher *attribute_matcher,
871 GFileInfo *info,
872 const gchar *path,
873 GLocalFileStat *statbuf,
874 GLocalParentFileInfo *parent_info)
876 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
877 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
878 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ))
879 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ,
880 g_access (path, R_OK) == 0);
882 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
883 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE))
884 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE,
885 g_access (path, W_OK) == 0);
887 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
888 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE))
889 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE,
890 g_access (path, X_OK) == 0);
893 if (parent_info)
895 gboolean writable;
897 writable = FALSE;
898 if (parent_info->writable)
900 #ifdef G_OS_WIN32
901 writable = TRUE;
902 #else
903 if (parent_info->is_sticky)
905 uid_t uid = geteuid ();
907 if (uid == statbuf->st_uid ||
908 uid == parent_info->owner ||
909 uid == 0)
910 writable = TRUE;
912 else
913 writable = TRUE;
914 #endif
917 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME))
918 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME,
919 writable);
921 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE))
922 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE,
923 writable);
925 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
926 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH,
927 writable && parent_info->has_trash_dir);
931 static void
932 set_info_from_stat (GFileInfo *info,
933 GLocalFileStat *statbuf,
934 GFileAttributeMatcher *attribute_matcher)
936 GFileType file_type;
938 file_type = G_FILE_TYPE_UNKNOWN;
940 if (S_ISREG (statbuf->st_mode))
941 file_type = G_FILE_TYPE_REGULAR;
942 else if (S_ISDIR (statbuf->st_mode))
943 file_type = G_FILE_TYPE_DIRECTORY;
944 #ifndef G_OS_WIN32
945 else if (S_ISCHR (statbuf->st_mode) ||
946 S_ISBLK (statbuf->st_mode) ||
947 S_ISFIFO (statbuf->st_mode)
948 #ifdef S_ISSOCK
949 || S_ISSOCK (statbuf->st_mode)
950 #endif
952 file_type = G_FILE_TYPE_SPECIAL;
953 #endif
954 #ifdef S_ISLNK
955 else if (S_ISLNK (statbuf->st_mode))
956 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
957 #elif defined (G_OS_WIN32)
958 if (statbuf->reparse_tag == IO_REPARSE_TAG_SYMLINK)
959 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
960 #endif
962 g_file_info_set_file_type (info, file_type);
963 g_file_info_set_size (info, statbuf->st_size);
965 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
966 #ifndef G_OS_WIN32
967 /* Pointless setting these on Windows even if they exist in the struct */
968 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
969 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
970 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
971 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
972 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
973 #endif
974 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
975 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
976 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
977 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
978 #endif
979 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
980 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
981 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
982 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
983 #elif defined (G_OS_WIN32)
984 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
985 statbuf->allocated_size);
987 #endif
989 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
990 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
991 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
992 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
993 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
994 #endif
996 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
997 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
998 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
999 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1000 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
1001 #endif
1003 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
1004 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
1005 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
1006 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
1007 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
1008 #endif
1010 #if defined (HAVE_STRUCT_STAT_ST_BIRTHTIME) && defined (HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
1011 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtime);
1012 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED_USEC, statbuf->st_birthtimensec / 1000);
1013 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIM) && defined (HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
1014 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtim.tv_sec);
1015 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED_USEC, statbuf->st_birthtim.tv_nsec / 1000);
1016 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIME)
1017 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtime);
1018 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIM)
1019 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtim);
1020 #endif
1022 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1023 G_FILE_ATTRIBUTE_ID_ETAG_VALUE))
1025 char *etag = _g_local_file_info_create_etag (statbuf);
1026 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ETAG_VALUE, etag);
1027 g_free (etag);
1030 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1031 G_FILE_ATTRIBUTE_ID_ID_FILE))
1033 char *id = _g_local_file_info_create_file_id (statbuf);
1034 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILE, id);
1035 g_free (id);
1038 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1039 G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM))
1041 char *id = _g_local_file_info_create_fs_id (statbuf);
1042 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM, id);
1043 g_free (id);
1047 #ifndef G_OS_WIN32
1049 static char *
1050 make_valid_utf8 (const char *name)
1052 GString *string;
1053 const gchar *remainder, *invalid;
1054 gint remaining_bytes, valid_bytes;
1056 string = NULL;
1057 remainder = name;
1058 remaining_bytes = strlen (name);
1060 while (remaining_bytes != 0)
1062 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1063 break;
1064 valid_bytes = invalid - remainder;
1066 if (string == NULL)
1067 string = g_string_sized_new (remaining_bytes);
1069 g_string_append_len (string, remainder, valid_bytes);
1070 /* append U+FFFD REPLACEMENT CHARACTER */
1071 g_string_append (string, "\357\277\275");
1073 remaining_bytes -= valid_bytes + 1;
1074 remainder = invalid + 1;
1077 if (string == NULL)
1078 return g_strdup (name);
1080 g_string_append (string, remainder);
1082 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1084 return g_string_free (string, FALSE);
1087 static char *
1088 convert_pwd_string_to_utf8 (char *pwd_str)
1090 char *utf8_string;
1092 if (!g_utf8_validate (pwd_str, -1, NULL))
1094 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1095 if (utf8_string == NULL)
1096 utf8_string = make_valid_utf8 (pwd_str);
1098 else
1099 utf8_string = g_strdup (pwd_str);
1101 return utf8_string;
1104 static void
1105 uid_data_free (UidData *data)
1107 g_free (data->user_name);
1108 g_free (data->real_name);
1109 g_free (data);
1112 /* called with lock held */
1113 static UidData *
1114 lookup_uid_data (uid_t uid)
1116 UidData *data;
1117 char buffer[4096];
1118 struct passwd pwbuf;
1119 struct passwd *pwbufp;
1120 char *gecos, *comma;
1122 if (uid_cache == NULL)
1123 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1125 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1127 if (data)
1128 return data;
1130 data = g_new0 (UidData, 1);
1132 #if defined(HAVE_GETPWUID_R)
1133 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1134 #else
1135 pwbufp = getpwuid (uid);
1136 #endif
1138 if (pwbufp != NULL)
1140 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1141 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1143 #ifndef __BIONIC__
1144 gecos = pwbufp->pw_gecos;
1146 if (gecos)
1148 comma = strchr (gecos, ',');
1149 if (comma)
1150 *comma = 0;
1151 data->real_name = convert_pwd_string_to_utf8 (gecos);
1153 #endif
1156 /* Default fallbacks */
1157 if (data->real_name == NULL)
1159 if (data->user_name != NULL)
1160 data->real_name = g_strdup (data->user_name);
1161 else
1162 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1165 if (data->user_name == NULL)
1166 data->user_name = g_strdup_printf ("%d", (int)uid);
1168 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1170 return data;
1173 static char *
1174 get_username_from_uid (uid_t uid)
1176 char *res;
1177 UidData *data;
1179 G_LOCK (uid_cache);
1180 data = lookup_uid_data (uid);
1181 res = g_strdup (data->user_name);
1182 G_UNLOCK (uid_cache);
1184 return res;
1187 static char *
1188 get_realname_from_uid (uid_t uid)
1190 char *res;
1191 UidData *data;
1193 G_LOCK (uid_cache);
1194 data = lookup_uid_data (uid);
1195 res = g_strdup (data->real_name);
1196 G_UNLOCK (uid_cache);
1198 return res;
1201 /* called with lock held */
1202 static char *
1203 lookup_gid_name (gid_t gid)
1205 char *name;
1206 char buffer[4096];
1207 struct group gbuf;
1208 struct group *gbufp;
1210 if (gid_cache == NULL)
1211 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1213 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1215 if (name)
1216 return name;
1218 #if defined (HAVE_GETGRGID_R)
1219 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1220 #else
1221 gbufp = getgrgid (gid);
1222 #endif
1224 if (gbufp != NULL &&
1225 gbufp->gr_name != NULL &&
1226 gbufp->gr_name[0] != 0)
1227 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1228 else
1229 name = g_strdup_printf("%d", (int)gid);
1231 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1233 return name;
1236 static char *
1237 get_groupname_from_gid (gid_t gid)
1239 char *res;
1240 char *name;
1242 G_LOCK (gid_cache);
1243 name = lookup_gid_name (gid);
1244 res = g_strdup (name);
1245 G_UNLOCK (gid_cache);
1246 return res;
1249 #endif /* !G_OS_WIN32 */
1251 static char *
1252 get_content_type (const char *basename,
1253 const char *path,
1254 GLocalFileStat *statbuf,
1255 gboolean is_symlink,
1256 gboolean symlink_broken,
1257 GFileQueryInfoFlags flags,
1258 gboolean fast)
1260 if (is_symlink &&
1261 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1262 return g_content_type_from_mime_type ("inode/symlink");
1263 else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
1264 return g_content_type_from_mime_type ("inode/directory");
1265 #ifndef G_OS_WIN32
1266 else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
1267 return g_content_type_from_mime_type ("inode/chardevice");
1268 else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
1269 return g_content_type_from_mime_type ("inode/blockdevice");
1270 else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
1271 return g_content_type_from_mime_type ("inode/fifo");
1272 else if (statbuf != NULL && S_ISREG(statbuf->st_mode) && statbuf->st_size == 0)
1274 /* Don't sniff zero-length files in order to avoid reading files
1275 * that appear normal but are not (eg: files in /proc and /sys)
1277 * Note that we need to return text/plain here so that
1278 * newly-created text files are opened by the text editor.
1279 * See https://bugzilla.gnome.org/show_bug.cgi?id=755795
1281 return g_content_type_from_mime_type ("text/plain");
1283 #endif
1284 #ifdef S_ISSOCK
1285 else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
1286 return g_content_type_from_mime_type ("inode/socket");
1287 #endif
1288 else
1290 char *content_type;
1291 gboolean result_uncertain;
1293 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1295 #if !defined(G_OS_WIN32) && !defined(HAVE_COCOA)
1296 if (!fast && result_uncertain && path != NULL)
1298 guchar sniff_buffer[4096];
1299 gsize sniff_length;
1300 int fd, errsv;
1302 sniff_length = _g_unix_content_type_get_sniff_len ();
1303 if (sniff_length > 4096)
1304 sniff_length = 4096;
1306 #ifdef O_NOATIME
1307 fd = g_open (path, O_RDONLY | O_NOATIME, 0);
1308 errsv = errno;
1309 if (fd < 0 && errsv == EPERM)
1310 #endif
1311 fd = g_open (path, O_RDONLY, 0);
1313 if (fd != -1)
1315 gssize res;
1317 res = read (fd, sniff_buffer, sniff_length);
1318 (void) g_close (fd, NULL);
1319 if (res >= 0)
1321 g_free (content_type);
1322 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1326 #endif
1328 return content_type;
1333 /* @stat_buf is the pre-calculated result of stat(path), or %NULL if that failed. */
1334 static void
1335 get_thumbnail_attributes (const char *path,
1336 GFileInfo *info,
1337 const GLocalFileStat *stat_buf)
1339 GChecksum *checksum;
1340 char *uri;
1341 char *filename;
1342 char *basename;
1344 uri = g_filename_to_uri (path, NULL, NULL);
1346 checksum = g_checksum_new (G_CHECKSUM_MD5);
1347 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1349 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1350 g_checksum_free (checksum);
1352 filename = g_build_filename (g_get_user_cache_dir (),
1353 "thumbnails", "large", basename,
1354 NULL);
1356 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1358 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1359 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1360 thumbnail_verify (filename, uri, stat_buf));
1362 else
1364 g_free (filename);
1365 filename = g_build_filename (g_get_user_cache_dir (),
1366 "thumbnails", "normal", basename,
1367 NULL);
1369 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1371 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1372 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1373 thumbnail_verify (filename, uri, stat_buf));
1375 else
1377 g_free (filename);
1378 filename = g_build_filename (g_get_user_cache_dir (),
1379 "thumbnails", "fail",
1380 "gnome-thumbnail-factory",
1381 basename,
1382 NULL);
1384 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1386 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1387 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1388 thumbnail_verify (filename, uri, stat_buf));
1392 g_free (basename);
1393 g_free (filename);
1394 g_free (uri);
1397 #ifdef G_OS_WIN32
1398 static void
1399 win32_get_file_user_info (const gchar *filename,
1400 gchar **group_name,
1401 gchar **user_name,
1402 gchar **real_name)
1404 PSECURITY_DESCRIPTOR psd = NULL;
1405 DWORD sd_size = 0; /* first call calculates the size required */
1407 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1408 if ((GetFileSecurityW (wfilename,
1409 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1410 NULL,
1411 sd_size,
1412 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1413 (psd = g_try_malloc (sd_size)) != NULL &&
1414 GetFileSecurityW (wfilename,
1415 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1416 psd,
1417 sd_size,
1418 &sd_size))
1420 PSID psid = 0;
1421 BOOL defaulted;
1422 SID_NAME_USE name_use = 0; /* don't care? */
1423 wchar_t *name = NULL;
1424 wchar_t *domain = NULL;
1425 DWORD name_len = 0;
1426 DWORD domain_len = 0;
1427 /* get the user name */
1428 do {
1429 if (!user_name)
1430 break;
1431 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1432 break;
1433 if (!LookupAccountSidW (NULL, /* local machine */
1434 psid,
1435 name, &name_len,
1436 domain, &domain_len, /* no domain info yet */
1437 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1438 break;
1439 name = g_try_malloc (name_len * sizeof (wchar_t));
1440 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1441 if (name && domain &&
1442 LookupAccountSidW (NULL, /* local machine */
1443 psid,
1444 name, &name_len,
1445 domain, &domain_len, /* no domain info yet */
1446 &name_use))
1448 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1450 g_free (name);
1451 g_free (domain);
1452 } while (FALSE);
1454 /* get the group name */
1455 do {
1456 if (!group_name)
1457 break;
1458 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1459 break;
1460 if (!LookupAccountSidW (NULL, /* local machine */
1461 psid,
1462 name, &name_len,
1463 domain, &domain_len, /* no domain info yet */
1464 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1465 break;
1466 name = g_try_malloc (name_len * sizeof (wchar_t));
1467 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1468 if (name && domain &&
1469 LookupAccountSidW (NULL, /* local machine */
1470 psid,
1471 name, &name_len,
1472 domain, &domain_len, /* no domain info yet */
1473 &name_use))
1475 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1477 g_free (name);
1478 g_free (domain);
1479 } while (FALSE);
1481 /* TODO: get real name */
1483 g_free (psd);
1485 g_free (wfilename);
1487 #endif /* G_OS_WIN32 */
1489 #ifndef G_OS_WIN32
1490 /* support for '.hidden' files */
1491 G_LOCK_DEFINE_STATIC (hidden_cache);
1492 static GHashTable *hidden_cache;
1494 static gboolean
1495 remove_from_hidden_cache (gpointer user_data)
1497 G_LOCK (hidden_cache);
1498 g_hash_table_remove (hidden_cache, user_data);
1499 G_UNLOCK (hidden_cache);
1501 return FALSE;
1504 static GHashTable *
1505 read_hidden_file (const gchar *dirname)
1507 gchar *contents = NULL;
1508 gchar *filename;
1510 filename = g_build_path ("/", dirname, ".hidden", NULL);
1511 (void) g_file_get_contents (filename, &contents, NULL, NULL);
1512 g_free (filename);
1514 if (contents != NULL)
1516 GHashTable *table;
1517 gchar **lines;
1518 gint i;
1520 table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1522 lines = g_strsplit (contents, "\n", 0);
1523 g_free (contents);
1525 for (i = 0; lines[i]; i++)
1526 /* hash table takes the individual strings... */
1527 g_hash_table_add (table, lines[i]);
1529 /* ... so we only free the container. */
1530 g_free (lines);
1532 return table;
1534 else
1535 return NULL;
1538 static void
1539 maybe_unref_hash_table (gpointer data)
1541 if (data != NULL)
1542 g_hash_table_unref (data);
1545 static gboolean
1546 file_is_hidden (const gchar *path,
1547 const gchar *basename)
1549 gboolean result;
1550 gchar *dirname;
1551 gpointer table;
1553 dirname = g_path_get_dirname (path);
1555 G_LOCK (hidden_cache);
1557 if G_UNLIKELY (hidden_cache == NULL)
1558 hidden_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1559 g_free, maybe_unref_hash_table);
1561 if (!g_hash_table_lookup_extended (hidden_cache, dirname,
1562 NULL, &table))
1564 gchar *mydirname;
1565 GSource *remove_from_cache_source;
1567 g_hash_table_insert (hidden_cache,
1568 mydirname = g_strdup (dirname),
1569 table = read_hidden_file (dirname));
1571 remove_from_cache_source = g_timeout_source_new_seconds (5);
1572 g_source_set_priority (remove_from_cache_source, G_PRIORITY_DEFAULT);
1573 g_source_set_callback (remove_from_cache_source,
1574 remove_from_hidden_cache,
1575 mydirname,
1576 NULL);
1577 g_source_attach (remove_from_cache_source,
1578 GLIB_PRIVATE_CALL (g_get_worker_context) ());
1579 g_source_unref (remove_from_cache_source);
1582 result = table != NULL && g_hash_table_contains (table, basename);
1584 G_UNLOCK (hidden_cache);
1586 g_free (dirname);
1588 return result;
1590 #endif /* !G_OS_WIN32 */
1592 void
1593 _g_local_file_info_get_nostat (GFileInfo *info,
1594 const char *basename,
1595 const char *path,
1596 GFileAttributeMatcher *attribute_matcher)
1598 g_file_info_set_name (info, basename);
1600 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1601 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1603 char *display_name = g_filename_display_basename (path);
1605 /* look for U+FFFD REPLACEMENT CHARACTER */
1606 if (strstr (display_name, "\357\277\275") != NULL)
1608 char *p = display_name;
1609 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1610 g_free (p);
1612 g_file_info_set_display_name (info, display_name);
1613 g_free (display_name);
1616 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1617 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1619 char *edit_name = g_filename_display_basename (path);
1620 g_file_info_set_edit_name (info, edit_name);
1621 g_free (edit_name);
1625 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1626 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1628 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1629 if (copy_name)
1630 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1631 g_free (copy_name);
1635 static const char *
1636 get_icon_name (const char *path,
1637 const char *content_type,
1638 gboolean use_symbolic,
1639 gboolean *with_fallbacks_out)
1641 const char *name = NULL;
1642 gboolean with_fallbacks = TRUE;
1644 if (g_strcmp0 (path, g_get_home_dir ()) == 0)
1646 name = use_symbolic ? "user-home-symbolic" : "user-home";
1647 with_fallbacks = FALSE;
1649 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1651 name = use_symbolic ? "user-desktop-symbolic" : "user-desktop";
1652 with_fallbacks = FALSE;
1654 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1656 name = use_symbolic ? "folder-documents-symbolic" : "folder-documents";
1658 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1660 name = use_symbolic ? "folder-download-symbolic" : "folder-download";
1662 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1664 name = use_symbolic ? "folder-music-symbolic" : "folder-music";
1666 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1668 name = use_symbolic ? "folder-pictures-symbolic" : "folder-pictures";
1670 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1672 name = use_symbolic ? "folder-publicshare-symbolic" : "folder-publicshare";
1674 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1676 name = use_symbolic ? "folder-templates-symbolic" : "folder-templates";
1678 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1680 name = use_symbolic ? "folder-videos-symbolic" : "folder-videos";
1682 else if (g_content_type_is_mime_type (content_type,"inode/directory"))
1684 name = use_symbolic ? "folder-symbolic" : "folder";
1686 else
1688 name = NULL;
1691 if (with_fallbacks_out != NULL)
1692 *with_fallbacks_out = with_fallbacks;
1694 return name;
1697 static GIcon *
1698 get_icon (const char *path,
1699 const char *content_type,
1700 gboolean use_symbolic)
1702 GIcon *icon = NULL;
1703 const char *icon_name;
1704 gboolean with_fallbacks;
1706 icon_name = get_icon_name (path, content_type, use_symbolic, &with_fallbacks);
1707 if (icon_name != NULL)
1709 if (with_fallbacks)
1710 icon = g_themed_icon_new_with_default_fallbacks (icon_name);
1711 else
1712 icon = g_themed_icon_new (icon_name);
1714 else
1716 if (use_symbolic)
1717 icon = g_content_type_get_symbolic_icon (content_type);
1718 else
1719 icon = g_content_type_get_icon (content_type);
1722 return icon;
1725 GFileInfo *
1726 _g_local_file_info_get (const char *basename,
1727 const char *path,
1728 GFileAttributeMatcher *attribute_matcher,
1729 GFileQueryInfoFlags flags,
1730 GLocalParentFileInfo *parent_info,
1731 GError **error)
1733 GFileInfo *info;
1734 GLocalFileStat statbuf;
1735 #ifdef S_ISLNK
1736 struct stat statbuf2;
1737 #elif defined (G_OS_WIN32)
1738 GWin32PrivateStat statbuf2;
1739 #endif
1740 int res;
1741 gboolean stat_ok;
1742 gboolean is_symlink, symlink_broken;
1743 char *symlink_target;
1744 GVfs *vfs;
1745 GVfsClass *class;
1746 guint64 device;
1748 info = g_file_info_new ();
1750 /* Make sure we don't set any unwanted attributes */
1751 g_file_info_set_attribute_mask (info, attribute_matcher);
1753 _g_local_file_info_get_nostat (info, basename, path, attribute_matcher);
1755 if (attribute_matcher == NULL)
1757 g_file_info_unset_attribute_mask (info);
1758 return info;
1761 #ifndef G_OS_WIN32
1762 res = g_lstat (path, &statbuf);
1763 #else
1764 res = GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (path, &statbuf);
1765 #endif
1767 if (res == -1)
1769 int errsv = errno;
1771 /* Don't bail out if we get Permission denied (SELinux?) */
1772 if (errsv != EACCES)
1774 char *display_name = g_filename_display_name (path);
1775 g_object_unref (info);
1776 g_set_error (error, G_IO_ERROR,
1777 g_io_error_from_errno (errsv),
1778 _("Error when getting information for file “%s”: %s"),
1779 display_name, g_strerror (errsv));
1780 g_free (display_name);
1781 return NULL;
1785 /* Even if stat() fails, try to get as much as other attributes possible */
1786 stat_ok = res != -1;
1788 if (stat_ok)
1789 device = statbuf.st_dev;
1790 else
1791 device = 0;
1793 #ifdef S_ISLNK
1794 is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
1795 #elif defined (G_OS_WIN32)
1796 /* glib already checked the FILE_ATTRIBUTE_REPARSE_POINT for us */
1797 is_symlink = stat_ok && statbuf.reparse_tag == IO_REPARSE_TAG_SYMLINK;
1798 #else
1799 is_symlink = FALSE;
1800 #endif
1801 symlink_broken = FALSE;
1803 if (is_symlink)
1805 g_file_info_set_is_symlink (info, TRUE);
1807 /* Unless NOFOLLOW was set we default to following symlinks */
1808 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1810 #ifndef G_OS_WIN32
1811 res = stat (path, &statbuf2);
1812 #else
1813 res = GLIB_PRIVATE_CALL (g_win32_stat_utf8) (path, &statbuf2);
1814 #endif
1816 /* Report broken links as symlinks */
1817 if (res != -1)
1819 statbuf = statbuf2;
1820 stat_ok = TRUE;
1822 else
1823 symlink_broken = TRUE;
1827 if (stat_ok)
1828 set_info_from_stat (info, &statbuf, attribute_matcher);
1830 #ifdef G_OS_UNIX
1831 if (stat_ok && _g_local_file_is_lost_found_dir (path, statbuf.st_dev))
1832 g_file_info_set_is_hidden (info, TRUE);
1833 #endif
1835 #ifndef G_OS_WIN32
1836 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1837 G_FILE_ATTRIBUTE_ID_STANDARD_IS_HIDDEN))
1839 if (basename != NULL &&
1840 (basename[0] == '.' ||
1841 file_is_hidden (path, basename)))
1842 g_file_info_set_is_hidden (info, TRUE);
1845 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1846 (stat_ok && S_ISREG (statbuf.st_mode)))
1847 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1848 #else
1849 if (statbuf.attributes & FILE_ATTRIBUTE_HIDDEN)
1850 g_file_info_set_is_hidden (info, TRUE);
1852 if (statbuf.attributes & FILE_ATTRIBUTE_ARCHIVE)
1853 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1855 if (statbuf.attributes & FILE_ATTRIBUTE_SYSTEM)
1856 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1857 #endif
1859 symlink_target = NULL;
1860 if (is_symlink)
1862 #if defined (S_ISLNK) || defined (G_OS_WIN32)
1863 symlink_target = read_link (path);
1864 #endif
1865 if (symlink_target &&
1866 _g_file_attribute_matcher_matches_id (attribute_matcher,
1867 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1868 g_file_info_set_symlink_target (info, symlink_target);
1871 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1872 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1873 _g_file_attribute_matcher_matches_id (attribute_matcher,
1874 G_FILE_ATTRIBUTE_ID_STANDARD_ICON) ||
1875 _g_file_attribute_matcher_matches_id (attribute_matcher,
1876 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1878 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
1880 if (content_type)
1882 g_file_info_set_content_type (info, content_type);
1884 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1885 G_FILE_ATTRIBUTE_ID_STANDARD_ICON)
1886 || _g_file_attribute_matcher_matches_id (attribute_matcher,
1887 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1889 GIcon *icon;
1891 /* non symbolic icon */
1892 icon = get_icon (path, content_type, FALSE);
1893 if (icon != NULL)
1895 g_file_info_set_icon (info, icon);
1896 g_object_unref (icon);
1899 /* symbolic icon */
1900 icon = get_icon (path, content_type, TRUE);
1901 if (icon != NULL)
1903 g_file_info_set_symbolic_icon (info, icon);
1904 g_object_unref (icon);
1909 g_free (content_type);
1913 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1914 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1916 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
1918 if (content_type)
1920 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1921 g_free (content_type);
1925 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1926 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1928 char *name = NULL;
1930 #ifdef G_OS_WIN32
1931 win32_get_file_user_info (path, NULL, &name, NULL);
1932 #else
1933 if (stat_ok)
1934 name = get_username_from_uid (statbuf.st_uid);
1935 #endif
1936 if (name)
1937 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1938 g_free (name);
1941 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1942 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1944 char *name = NULL;
1945 #ifdef G_OS_WIN32
1946 win32_get_file_user_info (path, NULL, NULL, &name);
1947 #else
1948 if (stat_ok)
1949 name = get_realname_from_uid (statbuf.st_uid);
1950 #endif
1951 if (name)
1952 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1953 g_free (name);
1956 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1957 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1959 char *name = NULL;
1960 #ifdef G_OS_WIN32
1961 win32_get_file_user_info (path, &name, NULL, NULL);
1962 #else
1963 if (stat_ok)
1964 name = get_groupname_from_gid (statbuf.st_gid);
1965 #endif
1966 if (name)
1967 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1968 g_free (name);
1971 if (stat_ok && parent_info && parent_info->device != 0 &&
1972 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1973 statbuf.st_dev != parent_info->device)
1974 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1976 if (stat_ok)
1977 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1979 #ifdef HAVE_SELINUX
1980 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1981 #endif
1982 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1983 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1985 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1986 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH) ||
1987 _g_file_attribute_matcher_matches_id (attribute_matcher,
1988 G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID) ||
1989 _g_file_attribute_matcher_matches_id (attribute_matcher,
1990 G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED))
1992 if (stat_ok)
1993 get_thumbnail_attributes (path, info, &statbuf);
1994 else
1995 get_thumbnail_attributes (path, info, NULL);
1998 vfs = g_vfs_get_default ();
1999 class = G_VFS_GET_CLASS (vfs);
2000 if (class->local_file_add_info)
2002 class->local_file_add_info (vfs,
2003 path,
2004 device,
2005 attribute_matcher,
2006 info,
2007 NULL,
2008 &parent_info->extra_data,
2009 &parent_info->free_extra_data);
2012 g_file_info_unset_attribute_mask (info);
2014 g_free (symlink_target);
2016 return info;
2019 GFileInfo *
2020 _g_local_file_info_get_from_fd (int fd,
2021 const char *attributes,
2022 GError **error)
2024 GLocalFileStat stat_buf;
2025 GFileAttributeMatcher *matcher;
2026 GFileInfo *info;
2028 #ifdef G_OS_WIN32
2029 #define FSTAT GLIB_PRIVATE_CALL (g_win32_fstat)
2030 #else
2031 #define FSTAT fstat
2032 #endif
2034 if (FSTAT (fd, &stat_buf) == -1)
2036 int errsv = errno;
2038 g_set_error (error, G_IO_ERROR,
2039 g_io_error_from_errno (errsv),
2040 _("Error when getting information for file descriptor: %s"),
2041 g_strerror (errsv));
2042 return NULL;
2045 info = g_file_info_new ();
2047 matcher = g_file_attribute_matcher_new (attributes);
2049 /* Make sure we don't set any unwanted attributes */
2050 g_file_info_set_attribute_mask (info, matcher);
2052 set_info_from_stat (info, &stat_buf, matcher);
2054 #ifdef HAVE_SELINUX
2055 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
2056 is_selinux_enabled ())
2058 char *context;
2059 if (fgetfilecon_raw (fd, &context) >= 0)
2061 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
2062 freecon (context);
2065 #endif
2067 get_xattrs_from_fd (fd, TRUE, info, matcher);
2068 get_xattrs_from_fd (fd, FALSE, info, matcher);
2070 g_file_attribute_matcher_unref (matcher);
2072 g_file_info_unset_attribute_mask (info);
2074 return info;
2077 static gboolean
2078 get_uint32 (const GFileAttributeValue *value,
2079 guint32 *val_out,
2080 GError **error)
2082 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
2084 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2085 _("Invalid attribute type (uint32 expected)"));
2086 return FALSE;
2089 *val_out = value->u.uint32;
2091 return TRUE;
2094 #ifdef HAVE_UTIMES
2095 static gboolean
2096 get_uint64 (const GFileAttributeValue *value,
2097 guint64 *val_out,
2098 GError **error)
2100 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
2102 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2103 _("Invalid attribute type (uint64 expected)"));
2104 return FALSE;
2107 *val_out = value->u.uint64;
2109 return TRUE;
2111 #endif
2113 #if defined(HAVE_SYMLINK)
2114 static gboolean
2115 get_byte_string (const GFileAttributeValue *value,
2116 const char **val_out,
2117 GError **error)
2119 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
2121 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2122 _("Invalid attribute type (byte string expected)"));
2123 return FALSE;
2126 *val_out = value->u.string;
2128 return TRUE;
2130 #endif
2132 #ifdef HAVE_SELINUX
2133 static gboolean
2134 get_string (const GFileAttributeValue *value,
2135 const char **val_out,
2136 GError **error)
2138 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
2140 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2141 _("Invalid attribute type (byte string expected)"));
2142 return FALSE;
2145 *val_out = value->u.string;
2147 return TRUE;
2149 #endif
2151 static gboolean
2152 set_unix_mode (char *filename,
2153 GFileQueryInfoFlags flags,
2154 const GFileAttributeValue *value,
2155 GError **error)
2157 guint32 val = 0;
2158 int res = 0;
2160 if (!get_uint32 (value, &val, error))
2161 return FALSE;
2163 #if defined (HAVE_SYMLINK) || defined (G_OS_WIN32)
2164 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
2165 #ifdef HAVE_LCHMOD
2166 res = lchmod (filename, val);
2167 #else
2168 gboolean is_symlink;
2169 #ifndef G_OS_WIN32
2170 struct stat statbuf;
2171 /* Calling chmod on a symlink changes permissions on the symlink.
2172 * We don't want to do this, so we need to check for a symlink */
2173 res = g_lstat (filename, &statbuf);
2174 is_symlink = (res == 0 && S_ISLNK (statbuf.st_mode));
2175 #else
2176 /* FIXME: implement lchmod for W32, should be doable */
2177 GWin32PrivateStat statbuf;
2179 res = GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (filename, &statbuf);
2180 is_symlink = (res == 0 && statbuf.reparse_tag == IO_REPARSE_TAG_SYMLINK);
2181 #endif
2182 if (is_symlink)
2184 g_set_error_literal (error, G_IO_ERROR,
2185 G_IO_ERROR_NOT_SUPPORTED,
2186 _("Cannot set permissions on symlinks"));
2187 return FALSE;
2189 else if (res == 0)
2190 res = g_chmod (filename, val);
2191 #endif
2192 } else
2193 #endif
2194 res = g_chmod (filename, val);
2196 if (res == -1)
2198 int errsv = errno;
2200 g_set_error (error, G_IO_ERROR,
2201 g_io_error_from_errno (errsv),
2202 _("Error setting permissions: %s"),
2203 g_strerror (errsv));
2204 return FALSE;
2206 return TRUE;
2209 #ifdef G_OS_UNIX
2210 static gboolean
2211 set_unix_uid_gid (char *filename,
2212 const GFileAttributeValue *uid_value,
2213 const GFileAttributeValue *gid_value,
2214 GFileQueryInfoFlags flags,
2215 GError **error)
2217 int res;
2218 guint32 val = 0;
2219 uid_t uid;
2220 gid_t gid;
2222 if (uid_value)
2224 if (!get_uint32 (uid_value, &val, error))
2225 return FALSE;
2226 uid = val;
2228 else
2229 uid = -1;
2231 if (gid_value)
2233 if (!get_uint32 (gid_value, &val, error))
2234 return FALSE;
2235 gid = val;
2237 else
2238 gid = -1;
2240 #ifdef HAVE_LCHOWN
2241 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
2242 res = lchown (filename, uid, gid);
2243 else
2244 #endif
2245 res = chown (filename, uid, gid);
2247 if (res == -1)
2249 int errsv = errno;
2251 g_set_error (error, G_IO_ERROR,
2252 g_io_error_from_errno (errsv),
2253 _("Error setting owner: %s"),
2254 g_strerror (errsv));
2255 return FALSE;
2257 return TRUE;
2259 #endif
2261 #ifdef HAVE_SYMLINK
2262 static gboolean
2263 set_symlink (char *filename,
2264 const GFileAttributeValue *value,
2265 GError **error)
2267 const char *val;
2268 struct stat statbuf;
2270 if (!get_byte_string (value, &val, error))
2271 return FALSE;
2273 if (val == NULL)
2275 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2276 _("symlink must be non-NULL"));
2277 return FALSE;
2280 if (g_lstat (filename, &statbuf))
2282 int errsv = errno;
2284 g_set_error (error, G_IO_ERROR,
2285 g_io_error_from_errno (errsv),
2286 _("Error setting symlink: %s"),
2287 g_strerror (errsv));
2288 return FALSE;
2291 if (!S_ISLNK (statbuf.st_mode))
2293 g_set_error_literal (error, G_IO_ERROR,
2294 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2295 _("Error setting symlink: file is not a symlink"));
2296 return FALSE;
2299 if (g_unlink (filename))
2301 int errsv = errno;
2303 g_set_error (error, G_IO_ERROR,
2304 g_io_error_from_errno (errsv),
2305 _("Error setting symlink: %s"),
2306 g_strerror (errsv));
2307 return FALSE;
2310 if (symlink (filename, val) != 0)
2312 int errsv = errno;
2314 g_set_error (error, G_IO_ERROR,
2315 g_io_error_from_errno (errsv),
2316 _("Error setting symlink: %s"),
2317 g_strerror (errsv));
2318 return FALSE;
2321 return TRUE;
2323 #endif
2325 #ifdef HAVE_UTIMES
2326 static int
2327 lazy_stat (char *filename,
2328 struct stat *statbuf,
2329 gboolean *called_stat)
2331 int res;
2333 if (*called_stat)
2334 return 0;
2336 res = g_stat (filename, statbuf);
2338 if (res == 0)
2339 *called_stat = TRUE;
2341 return res;
2345 static gboolean
2346 set_mtime_atime (char *filename,
2347 const GFileAttributeValue *mtime_value,
2348 const GFileAttributeValue *mtime_usec_value,
2349 const GFileAttributeValue *atime_value,
2350 const GFileAttributeValue *atime_usec_value,
2351 GError **error)
2353 int res;
2354 guint64 val = 0;
2355 guint32 val_usec = 0;
2356 struct stat statbuf;
2357 gboolean got_stat = FALSE;
2358 struct timeval times[2] = { {0, 0}, {0, 0} };
2360 /* ATIME */
2361 if (atime_value)
2363 if (!get_uint64 (atime_value, &val, error))
2364 return FALSE;
2365 times[0].tv_sec = val;
2367 else
2369 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2371 times[0].tv_sec = statbuf.st_mtime;
2372 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2373 times[0].tv_usec = statbuf.st_atimensec / 1000;
2374 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2375 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2376 #endif
2380 if (atime_usec_value)
2382 if (!get_uint32 (atime_usec_value, &val_usec, error))
2383 return FALSE;
2384 times[0].tv_usec = val_usec;
2387 /* MTIME */
2388 if (mtime_value)
2390 if (!get_uint64 (mtime_value, &val, error))
2391 return FALSE;
2392 times[1].tv_sec = val;
2394 else
2396 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2398 times[1].tv_sec = statbuf.st_mtime;
2399 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2400 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2401 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2402 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2403 #endif
2407 if (mtime_usec_value)
2409 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2410 return FALSE;
2411 times[1].tv_usec = val_usec;
2414 res = utimes (filename, times);
2415 if (res == -1)
2417 int errsv = errno;
2419 g_set_error (error, G_IO_ERROR,
2420 g_io_error_from_errno (errsv),
2421 _("Error setting modification or access time: %s"),
2422 g_strerror (errsv));
2423 return FALSE;
2425 return TRUE;
2427 #endif
2430 #ifdef HAVE_SELINUX
2431 static gboolean
2432 set_selinux_context (char *filename,
2433 const GFileAttributeValue *value,
2434 GError **error)
2436 const char *val;
2438 if (!get_string (value, &val, error))
2439 return FALSE;
2441 if (val == NULL)
2443 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2444 _("SELinux context must be non-NULL"));
2445 return FALSE;
2448 if (is_selinux_enabled ()) {
2449 security_context_t val_s;
2451 val_s = g_strdup (val);
2453 if (setfilecon_raw (filename, val_s) < 0)
2455 int errsv = errno;
2457 g_set_error (error, G_IO_ERROR,
2458 g_io_error_from_errno (errsv),
2459 _("Error setting SELinux context: %s"),
2460 g_strerror (errsv));
2461 return FALSE;
2463 g_free (val_s);
2464 } else {
2465 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2466 _("SELinux is not enabled on this system"));
2467 return FALSE;
2470 return TRUE;
2472 #endif
2475 gboolean
2476 _g_local_file_info_set_attribute (char *filename,
2477 const char *attribute,
2478 GFileAttributeType type,
2479 gpointer value_p,
2480 GFileQueryInfoFlags flags,
2481 GCancellable *cancellable,
2482 GError **error)
2484 GFileAttributeValue value = { 0 };
2485 GVfsClass *class;
2486 GVfs *vfs;
2488 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2490 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2491 return set_unix_mode (filename, flags, &value, error);
2493 #ifdef G_OS_UNIX
2494 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2495 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2496 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2497 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2498 #endif
2500 #ifdef HAVE_SYMLINK
2501 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2502 return set_symlink (filename, &value, error);
2503 #endif
2505 #ifdef HAVE_UTIMES
2506 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2507 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2508 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2509 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2510 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2511 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2512 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2513 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2514 #endif
2516 #ifdef HAVE_XATTR
2517 else if (g_str_has_prefix (attribute, "xattr::"))
2518 return set_xattr (filename, attribute, &value, error);
2519 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2520 return set_xattr (filename, attribute, &value, error);
2521 #endif
2523 #ifdef HAVE_SELINUX
2524 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2525 return set_selinux_context (filename, &value, error);
2526 #endif
2528 vfs = g_vfs_get_default ();
2529 class = G_VFS_GET_CLASS (vfs);
2530 if (class->local_file_set_attributes)
2532 GFileInfo *info;
2534 info = g_file_info_new ();
2535 g_file_info_set_attribute (info,
2536 attribute,
2537 type,
2538 value_p);
2539 if (!class->local_file_set_attributes (vfs, filename,
2540 info,
2541 flags, cancellable,
2542 error))
2544 g_object_unref (info);
2545 return FALSE;
2548 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2550 g_object_unref (info);
2551 return TRUE;
2554 g_object_unref (info);
2557 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2558 _("Setting attribute %s not supported"), attribute);
2559 return FALSE;
2562 gboolean
2563 _g_local_file_info_set_attributes (char *filename,
2564 GFileInfo *info,
2565 GFileQueryInfoFlags flags,
2566 GCancellable *cancellable,
2567 GError **error)
2569 GFileAttributeValue *value;
2570 #ifdef G_OS_UNIX
2571 GFileAttributeValue *uid, *gid;
2572 #ifdef HAVE_UTIMES
2573 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2574 #endif
2575 GFileAttributeStatus status;
2576 #endif
2577 gboolean res;
2578 GVfsClass *class;
2579 GVfs *vfs;
2581 /* Handles setting multiple specified data in a single set, and takes care
2582 of ordering restrictions when setting attributes */
2584 res = TRUE;
2586 /* Set symlink first, since this recreates the file */
2587 #ifdef HAVE_SYMLINK
2588 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2589 if (value)
2591 if (!set_symlink (filename, value, error))
2593 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2594 res = FALSE;
2595 /* Don't set error multiple times */
2596 error = NULL;
2598 else
2599 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2602 #endif
2604 #ifdef G_OS_UNIX
2605 /* Group uid and gid setting into one call
2606 * Change ownership before permissions, since ownership changes can
2607 change permissions (e.g. setuid)
2609 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2610 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2612 if (uid || gid)
2614 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2616 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2617 res = FALSE;
2618 /* Don't set error multiple times */
2619 error = NULL;
2621 else
2622 status = G_FILE_ATTRIBUTE_STATUS_SET;
2623 if (uid)
2624 uid->status = status;
2625 if (gid)
2626 gid->status = status;
2628 #endif
2630 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2631 if (value)
2633 if (!set_unix_mode (filename, flags, value, error))
2635 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2636 res = FALSE;
2637 /* Don't set error multiple times */
2638 error = NULL;
2640 else
2641 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2645 #ifdef HAVE_UTIMES
2646 /* Group all time settings into one call
2647 * Change times as the last thing to avoid it changing due to metadata changes
2650 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2651 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2652 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2653 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2655 if (mtime || mtime_usec || atime || atime_usec)
2657 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2659 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2660 res = FALSE;
2661 /* Don't set error multiple times */
2662 error = NULL;
2664 else
2665 status = G_FILE_ATTRIBUTE_STATUS_SET;
2667 if (mtime)
2668 mtime->status = status;
2669 if (mtime_usec)
2670 mtime_usec->status = status;
2671 if (atime)
2672 atime->status = status;
2673 if (atime_usec)
2674 atime_usec->status = status;
2676 #endif
2678 /* xattrs are handled by default callback */
2681 /* SELinux context */
2682 #ifdef HAVE_SELINUX
2683 if (is_selinux_enabled ()) {
2684 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2685 if (value)
2687 if (!set_selinux_context (filename, value, error))
2689 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2690 res = FALSE;
2691 /* Don't set error multiple times */
2692 error = NULL;
2694 else
2695 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2698 #endif
2700 vfs = g_vfs_get_default ();
2701 class = G_VFS_GET_CLASS (vfs);
2702 if (class->local_file_set_attributes)
2704 if (!class->local_file_set_attributes (vfs, filename,
2705 info,
2706 flags, cancellable,
2707 error))
2709 res = FALSE;
2710 /* Don't set error multiple times */
2711 error = NULL;
2715 return res;