Initialize variable
[glib.git] / gio / glocalfileinfo.c
blob50d870fb724e84461bb11590bc1eb8feb3439a0b
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 if (parent_info->is_sticky)
902 #ifndef G_OS_WIN32
903 uid_t uid = geteuid ();
905 if (uid == statbuf->st_uid ||
906 uid == parent_info->owner ||
907 uid == 0)
908 #endif
909 writable = TRUE;
911 else
912 writable = TRUE;
915 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME))
916 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME,
917 writable);
919 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE))
920 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE,
921 writable);
923 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
924 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH,
925 writable && parent_info->has_trash_dir);
929 static void
930 set_info_from_stat (GFileInfo *info,
931 GLocalFileStat *statbuf,
932 GFileAttributeMatcher *attribute_matcher)
934 GFileType file_type;
936 file_type = G_FILE_TYPE_UNKNOWN;
938 if (S_ISREG (statbuf->st_mode))
939 file_type = G_FILE_TYPE_REGULAR;
940 else if (S_ISDIR (statbuf->st_mode))
941 file_type = G_FILE_TYPE_DIRECTORY;
942 #ifndef G_OS_WIN32
943 else if (S_ISCHR (statbuf->st_mode) ||
944 S_ISBLK (statbuf->st_mode) ||
945 S_ISFIFO (statbuf->st_mode)
946 #ifdef S_ISSOCK
947 || S_ISSOCK (statbuf->st_mode)
948 #endif
950 file_type = G_FILE_TYPE_SPECIAL;
951 #endif
952 #ifdef S_ISLNK
953 else if (S_ISLNK (statbuf->st_mode))
954 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
955 #elif defined (G_OS_WIN32)
956 if (statbuf->reparse_tag == IO_REPARSE_TAG_SYMLINK)
957 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
958 #endif
960 g_file_info_set_file_type (info, file_type);
961 g_file_info_set_size (info, statbuf->st_size);
963 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
964 #ifndef G_OS_WIN32
965 /* Pointless setting these on Windows even if they exist in the struct */
966 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
967 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
968 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
969 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
970 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
971 #endif
972 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
973 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
974 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
975 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
976 #endif
977 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
978 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
979 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
980 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
981 #elif defined (G_OS_WIN32)
982 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
983 statbuf->allocated_size);
985 #endif
987 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
988 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
989 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
990 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
991 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
992 #endif
994 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
995 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
996 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
997 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
998 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
999 #endif
1001 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
1002 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
1003 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
1004 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
1005 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
1006 #endif
1008 #if defined (HAVE_STRUCT_STAT_ST_BIRTHTIME) && defined (HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
1009 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtime);
1010 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED_USEC, statbuf->st_birthtimensec / 1000);
1011 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIM) && defined (HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
1012 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtim.tv_sec);
1013 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED_USEC, statbuf->st_birthtim.tv_nsec / 1000);
1014 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIME)
1015 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtime);
1016 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIM)
1017 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtim);
1018 #endif
1020 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1021 G_FILE_ATTRIBUTE_ID_ETAG_VALUE))
1023 char *etag = _g_local_file_info_create_etag (statbuf);
1024 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ETAG_VALUE, etag);
1025 g_free (etag);
1028 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1029 G_FILE_ATTRIBUTE_ID_ID_FILE))
1031 char *id = _g_local_file_info_create_file_id (statbuf);
1032 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILE, id);
1033 g_free (id);
1036 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1037 G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM))
1039 char *id = _g_local_file_info_create_fs_id (statbuf);
1040 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM, id);
1041 g_free (id);
1045 #ifndef G_OS_WIN32
1047 static char *
1048 make_valid_utf8 (const char *name)
1050 GString *string;
1051 const gchar *remainder, *invalid;
1052 gint remaining_bytes, valid_bytes;
1054 string = NULL;
1055 remainder = name;
1056 remaining_bytes = strlen (name);
1058 while (remaining_bytes != 0)
1060 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1061 break;
1062 valid_bytes = invalid - remainder;
1064 if (string == NULL)
1065 string = g_string_sized_new (remaining_bytes);
1067 g_string_append_len (string, remainder, valid_bytes);
1068 /* append U+FFFD REPLACEMENT CHARACTER */
1069 g_string_append (string, "\357\277\275");
1071 remaining_bytes -= valid_bytes + 1;
1072 remainder = invalid + 1;
1075 if (string == NULL)
1076 return g_strdup (name);
1078 g_string_append (string, remainder);
1080 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1082 return g_string_free (string, FALSE);
1085 static char *
1086 convert_pwd_string_to_utf8 (char *pwd_str)
1088 char *utf8_string;
1090 if (!g_utf8_validate (pwd_str, -1, NULL))
1092 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1093 if (utf8_string == NULL)
1094 utf8_string = make_valid_utf8 (pwd_str);
1096 else
1097 utf8_string = g_strdup (pwd_str);
1099 return utf8_string;
1102 static void
1103 uid_data_free (UidData *data)
1105 g_free (data->user_name);
1106 g_free (data->real_name);
1107 g_free (data);
1110 /* called with lock held */
1111 static UidData *
1112 lookup_uid_data (uid_t uid)
1114 UidData *data;
1115 char buffer[4096];
1116 struct passwd pwbuf;
1117 struct passwd *pwbufp;
1118 char *gecos, *comma;
1120 if (uid_cache == NULL)
1121 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1123 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1125 if (data)
1126 return data;
1128 data = g_new0 (UidData, 1);
1130 #if defined(HAVE_GETPWUID_R)
1131 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1132 #else
1133 pwbufp = getpwuid (uid);
1134 #endif
1136 if (pwbufp != NULL)
1138 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1139 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1141 #ifndef __BIONIC__
1142 gecos = pwbufp->pw_gecos;
1144 if (gecos)
1146 comma = strchr (gecos, ',');
1147 if (comma)
1148 *comma = 0;
1149 data->real_name = convert_pwd_string_to_utf8 (gecos);
1151 #endif
1154 /* Default fallbacks */
1155 if (data->real_name == NULL)
1157 if (data->user_name != NULL)
1158 data->real_name = g_strdup (data->user_name);
1159 else
1160 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1163 if (data->user_name == NULL)
1164 data->user_name = g_strdup_printf ("%d", (int)uid);
1166 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1168 return data;
1171 static char *
1172 get_username_from_uid (uid_t uid)
1174 char *res;
1175 UidData *data;
1177 G_LOCK (uid_cache);
1178 data = lookup_uid_data (uid);
1179 res = g_strdup (data->user_name);
1180 G_UNLOCK (uid_cache);
1182 return res;
1185 static char *
1186 get_realname_from_uid (uid_t uid)
1188 char *res;
1189 UidData *data;
1191 G_LOCK (uid_cache);
1192 data = lookup_uid_data (uid);
1193 res = g_strdup (data->real_name);
1194 G_UNLOCK (uid_cache);
1196 return res;
1199 /* called with lock held */
1200 static char *
1201 lookup_gid_name (gid_t gid)
1203 char *name;
1204 char buffer[4096];
1205 struct group gbuf;
1206 struct group *gbufp;
1208 if (gid_cache == NULL)
1209 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1211 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1213 if (name)
1214 return name;
1216 #if defined (HAVE_GETGRGID_R)
1217 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1218 #else
1219 gbufp = getgrgid (gid);
1220 #endif
1222 if (gbufp != NULL &&
1223 gbufp->gr_name != NULL &&
1224 gbufp->gr_name[0] != 0)
1225 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1226 else
1227 name = g_strdup_printf("%d", (int)gid);
1229 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1231 return name;
1234 static char *
1235 get_groupname_from_gid (gid_t gid)
1237 char *res;
1238 char *name;
1240 G_LOCK (gid_cache);
1241 name = lookup_gid_name (gid);
1242 res = g_strdup (name);
1243 G_UNLOCK (gid_cache);
1244 return res;
1247 #endif /* !G_OS_WIN32 */
1249 static char *
1250 get_content_type (const char *basename,
1251 const char *path,
1252 GLocalFileStat *statbuf,
1253 gboolean is_symlink,
1254 gboolean symlink_broken,
1255 GFileQueryInfoFlags flags,
1256 gboolean fast)
1258 if (is_symlink &&
1259 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1260 return g_content_type_from_mime_type ("inode/symlink");
1261 else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
1262 return g_content_type_from_mime_type ("inode/directory");
1263 #ifndef G_OS_WIN32
1264 else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
1265 return g_content_type_from_mime_type ("inode/chardevice");
1266 else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
1267 return g_content_type_from_mime_type ("inode/blockdevice");
1268 else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
1269 return g_content_type_from_mime_type ("inode/fifo");
1270 else if (statbuf != NULL && S_ISREG(statbuf->st_mode) && statbuf->st_size == 0)
1272 /* Don't sniff zero-length files in order to avoid reading files
1273 * that appear normal but are not (eg: files in /proc and /sys)
1275 * Note that we need to return text/plain here so that
1276 * newly-created text files are opened by the text editor.
1277 * See https://bugzilla.gnome.org/show_bug.cgi?id=755795
1279 return g_content_type_from_mime_type ("text/plain");
1281 #endif
1282 #ifdef S_ISSOCK
1283 else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
1284 return g_content_type_from_mime_type ("inode/socket");
1285 #endif
1286 else
1288 char *content_type;
1289 gboolean result_uncertain;
1291 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1293 #if !defined(G_OS_WIN32) && !defined(HAVE_COCOA)
1294 if (!fast && result_uncertain && path != NULL)
1296 guchar sniff_buffer[4096];
1297 gsize sniff_length;
1298 int fd, errsv;
1300 sniff_length = _g_unix_content_type_get_sniff_len ();
1301 if (sniff_length > 4096)
1302 sniff_length = 4096;
1304 #ifdef O_NOATIME
1305 fd = g_open (path, O_RDONLY | O_NOATIME, 0);
1306 errsv = errno;
1307 if (fd < 0 && errsv == EPERM)
1308 #endif
1309 fd = g_open (path, O_RDONLY, 0);
1311 if (fd != -1)
1313 gssize res;
1315 res = read (fd, sniff_buffer, sniff_length);
1316 (void) g_close (fd, NULL);
1317 if (res >= 0)
1319 g_free (content_type);
1320 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1324 #endif
1326 return content_type;
1331 /* @stat_buf is the pre-calculated result of stat(path), or %NULL if that failed. */
1332 static void
1333 get_thumbnail_attributes (const char *path,
1334 GFileInfo *info,
1335 const GLocalFileStat *stat_buf)
1337 GChecksum *checksum;
1338 char *uri;
1339 char *filename;
1340 char *basename;
1342 uri = g_filename_to_uri (path, NULL, NULL);
1344 checksum = g_checksum_new (G_CHECKSUM_MD5);
1345 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1347 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1348 g_checksum_free (checksum);
1350 filename = g_build_filename (g_get_user_cache_dir (),
1351 "thumbnails", "large", basename,
1352 NULL);
1354 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1356 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1357 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1358 thumbnail_verify (filename, uri, stat_buf));
1360 else
1362 g_free (filename);
1363 filename = g_build_filename (g_get_user_cache_dir (),
1364 "thumbnails", "normal", basename,
1365 NULL);
1367 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1369 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1370 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1371 thumbnail_verify (filename, uri, stat_buf));
1373 else
1375 g_free (filename);
1376 filename = g_build_filename (g_get_user_cache_dir (),
1377 "thumbnails", "fail",
1378 "gnome-thumbnail-factory",
1379 basename,
1380 NULL);
1382 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1384 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1385 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1386 thumbnail_verify (filename, uri, stat_buf));
1390 g_free (basename);
1391 g_free (filename);
1392 g_free (uri);
1395 #ifdef G_OS_WIN32
1396 static void
1397 win32_get_file_user_info (const gchar *filename,
1398 gchar **group_name,
1399 gchar **user_name,
1400 gchar **real_name)
1402 PSECURITY_DESCRIPTOR psd = NULL;
1403 DWORD sd_size = 0; /* first call calculates the size required */
1405 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1406 if ((GetFileSecurityW (wfilename,
1407 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1408 NULL,
1409 sd_size,
1410 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1411 (psd = g_try_malloc (sd_size)) != NULL &&
1412 GetFileSecurityW (wfilename,
1413 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1414 psd,
1415 sd_size,
1416 &sd_size))
1418 PSID psid = 0;
1419 BOOL defaulted;
1420 SID_NAME_USE name_use = 0; /* don't care? */
1421 wchar_t *name = NULL;
1422 wchar_t *domain = NULL;
1423 DWORD name_len = 0;
1424 DWORD domain_len = 0;
1425 /* get the user name */
1426 do {
1427 if (!user_name)
1428 break;
1429 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1430 break;
1431 if (!LookupAccountSidW (NULL, /* local machine */
1432 psid,
1433 name, &name_len,
1434 domain, &domain_len, /* no domain info yet */
1435 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1436 break;
1437 name = g_try_malloc (name_len * sizeof (wchar_t));
1438 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1439 if (name && domain &&
1440 LookupAccountSidW (NULL, /* local machine */
1441 psid,
1442 name, &name_len,
1443 domain, &domain_len, /* no domain info yet */
1444 &name_use))
1446 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1448 g_free (name);
1449 g_free (domain);
1450 } while (FALSE);
1452 /* get the group name */
1453 do {
1454 if (!group_name)
1455 break;
1456 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1457 break;
1458 if (!LookupAccountSidW (NULL, /* local machine */
1459 psid,
1460 name, &name_len,
1461 domain, &domain_len, /* no domain info yet */
1462 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1463 break;
1464 name = g_try_malloc (name_len * sizeof (wchar_t));
1465 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1466 if (name && domain &&
1467 LookupAccountSidW (NULL, /* local machine */
1468 psid,
1469 name, &name_len,
1470 domain, &domain_len, /* no domain info yet */
1471 &name_use))
1473 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1475 g_free (name);
1476 g_free (domain);
1477 } while (FALSE);
1479 /* TODO: get real name */
1481 g_free (psd);
1483 g_free (wfilename);
1485 #endif /* G_OS_WIN32 */
1487 #ifndef G_OS_WIN32
1488 /* support for '.hidden' files */
1489 G_LOCK_DEFINE_STATIC (hidden_cache);
1490 static GHashTable *hidden_cache;
1492 static gboolean
1493 remove_from_hidden_cache (gpointer user_data)
1495 G_LOCK (hidden_cache);
1496 g_hash_table_remove (hidden_cache, user_data);
1497 G_UNLOCK (hidden_cache);
1499 return FALSE;
1502 static GHashTable *
1503 read_hidden_file (const gchar *dirname)
1505 gchar *contents = NULL;
1506 gchar *filename;
1508 filename = g_build_path ("/", dirname, ".hidden", NULL);
1509 (void) g_file_get_contents (filename, &contents, NULL, NULL);
1510 g_free (filename);
1512 if (contents != NULL)
1514 GHashTable *table;
1515 gchar **lines;
1516 gint i;
1518 table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1520 lines = g_strsplit (contents, "\n", 0);
1521 g_free (contents);
1523 for (i = 0; lines[i]; i++)
1524 /* hash table takes the individual strings... */
1525 g_hash_table_add (table, lines[i]);
1527 /* ... so we only free the container. */
1528 g_free (lines);
1530 return table;
1532 else
1533 return NULL;
1536 static void
1537 maybe_unref_hash_table (gpointer data)
1539 if (data != NULL)
1540 g_hash_table_unref (data);
1543 static gboolean
1544 file_is_hidden (const gchar *path,
1545 const gchar *basename)
1547 gboolean result;
1548 gchar *dirname;
1549 gpointer table;
1551 dirname = g_path_get_dirname (path);
1553 G_LOCK (hidden_cache);
1555 if G_UNLIKELY (hidden_cache == NULL)
1556 hidden_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1557 g_free, maybe_unref_hash_table);
1559 if (!g_hash_table_lookup_extended (hidden_cache, dirname,
1560 NULL, &table))
1562 gchar *mydirname;
1563 GSource *remove_from_cache_source;
1565 g_hash_table_insert (hidden_cache,
1566 mydirname = g_strdup (dirname),
1567 table = read_hidden_file (dirname));
1569 remove_from_cache_source = g_timeout_source_new_seconds (5);
1570 g_source_set_priority (remove_from_cache_source, G_PRIORITY_DEFAULT);
1571 g_source_set_callback (remove_from_cache_source,
1572 remove_from_hidden_cache,
1573 mydirname,
1574 NULL);
1575 g_source_attach (remove_from_cache_source,
1576 GLIB_PRIVATE_CALL (g_get_worker_context) ());
1577 g_source_unref (remove_from_cache_source);
1580 result = table != NULL && g_hash_table_contains (table, basename);
1582 G_UNLOCK (hidden_cache);
1584 g_free (dirname);
1586 return result;
1588 #endif /* !G_OS_WIN32 */
1590 void
1591 _g_local_file_info_get_nostat (GFileInfo *info,
1592 const char *basename,
1593 const char *path,
1594 GFileAttributeMatcher *attribute_matcher)
1596 g_file_info_set_name (info, basename);
1598 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1599 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1601 char *display_name = g_filename_display_basename (path);
1603 /* look for U+FFFD REPLACEMENT CHARACTER */
1604 if (strstr (display_name, "\357\277\275") != NULL)
1606 char *p = display_name;
1607 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1608 g_free (p);
1610 g_file_info_set_display_name (info, display_name);
1611 g_free (display_name);
1614 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1615 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1617 char *edit_name = g_filename_display_basename (path);
1618 g_file_info_set_edit_name (info, edit_name);
1619 g_free (edit_name);
1623 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1624 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1626 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1627 if (copy_name)
1628 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1629 g_free (copy_name);
1633 static const char *
1634 get_icon_name (const char *path,
1635 const char *content_type,
1636 gboolean use_symbolic,
1637 gboolean *with_fallbacks_out)
1639 const char *name = NULL;
1640 gboolean with_fallbacks = TRUE;
1642 if (g_strcmp0 (path, g_get_home_dir ()) == 0)
1644 name = use_symbolic ? "user-home-symbolic" : "user-home";
1645 with_fallbacks = FALSE;
1647 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1649 name = use_symbolic ? "user-desktop-symbolic" : "user-desktop";
1650 with_fallbacks = FALSE;
1652 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1654 name = use_symbolic ? "folder-documents-symbolic" : "folder-documents";
1656 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1658 name = use_symbolic ? "folder-download-symbolic" : "folder-download";
1660 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1662 name = use_symbolic ? "folder-music-symbolic" : "folder-music";
1664 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1666 name = use_symbolic ? "folder-pictures-symbolic" : "folder-pictures";
1668 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1670 name = use_symbolic ? "folder-publicshare-symbolic" : "folder-publicshare";
1672 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1674 name = use_symbolic ? "folder-templates-symbolic" : "folder-templates";
1676 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1678 name = use_symbolic ? "folder-videos-symbolic" : "folder-videos";
1680 else if (g_content_type_is_mime_type (content_type,"inode/directory"))
1682 name = use_symbolic ? "folder-symbolic" : "folder";
1684 else
1686 name = NULL;
1689 if (with_fallbacks_out != NULL)
1690 *with_fallbacks_out = with_fallbacks;
1692 return name;
1695 static GIcon *
1696 get_icon (const char *path,
1697 const char *content_type,
1698 gboolean use_symbolic)
1700 GIcon *icon = NULL;
1701 const char *icon_name;
1702 gboolean with_fallbacks;
1704 icon_name = get_icon_name (path, content_type, use_symbolic, &with_fallbacks);
1705 if (icon_name != NULL)
1707 if (with_fallbacks)
1708 icon = g_themed_icon_new_with_default_fallbacks (icon_name);
1709 else
1710 icon = g_themed_icon_new (icon_name);
1712 else
1714 if (use_symbolic)
1715 icon = g_content_type_get_symbolic_icon (content_type);
1716 else
1717 icon = g_content_type_get_icon (content_type);
1720 return icon;
1723 GFileInfo *
1724 _g_local_file_info_get (const char *basename,
1725 const char *path,
1726 GFileAttributeMatcher *attribute_matcher,
1727 GFileQueryInfoFlags flags,
1728 GLocalParentFileInfo *parent_info,
1729 GError **error)
1731 GFileInfo *info;
1732 GLocalFileStat statbuf;
1733 #ifdef S_ISLNK
1734 struct stat statbuf2;
1735 #elif defined (G_OS_WIN32)
1736 GWin32PrivateStat statbuf2;
1737 #endif
1738 int res;
1739 gboolean stat_ok;
1740 gboolean is_symlink, symlink_broken;
1741 char *symlink_target;
1742 GVfs *vfs;
1743 GVfsClass *class;
1744 guint64 device;
1746 info = g_file_info_new ();
1748 /* Make sure we don't set any unwanted attributes */
1749 g_file_info_set_attribute_mask (info, attribute_matcher);
1751 _g_local_file_info_get_nostat (info, basename, path, attribute_matcher);
1753 if (attribute_matcher == NULL)
1755 g_file_info_unset_attribute_mask (info);
1756 return info;
1759 #ifndef G_OS_WIN32
1760 res = g_lstat (path, &statbuf);
1761 #else
1762 res = GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (path, &statbuf);
1763 #endif
1765 if (res == -1)
1767 int errsv = errno;
1769 /* Don't bail out if we get Permission denied (SELinux?) */
1770 if (errsv != EACCES)
1772 char *display_name = g_filename_display_name (path);
1773 g_object_unref (info);
1774 g_set_error (error, G_IO_ERROR,
1775 g_io_error_from_errno (errsv),
1776 _("Error when getting information for file “%s”: %s"),
1777 display_name, g_strerror (errsv));
1778 g_free (display_name);
1779 return NULL;
1783 /* Even if stat() fails, try to get as much as other attributes possible */
1784 stat_ok = res != -1;
1786 if (stat_ok)
1787 device = statbuf.st_dev;
1788 else
1789 device = 0;
1791 #ifdef S_ISLNK
1792 is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
1793 #elif defined (G_OS_WIN32)
1794 /* glib already checked the FILE_ATTRIBUTE_REPARSE_POINT for us */
1795 is_symlink = stat_ok && statbuf.reparse_tag == IO_REPARSE_TAG_SYMLINK;
1796 #else
1797 is_symlink = FALSE;
1798 #endif
1799 symlink_broken = FALSE;
1801 if (is_symlink)
1803 g_file_info_set_is_symlink (info, TRUE);
1805 /* Unless NOFOLLOW was set we default to following symlinks */
1806 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1808 #ifndef G_OS_WIN32
1809 res = stat (path, &statbuf2);
1810 #else
1811 res = GLIB_PRIVATE_CALL (g_win32_stat_utf8) (path, &statbuf2);
1812 #endif
1814 /* Report broken links as symlinks */
1815 if (res != -1)
1817 statbuf = statbuf2;
1818 stat_ok = TRUE;
1820 else
1821 symlink_broken = TRUE;
1825 if (stat_ok)
1826 set_info_from_stat (info, &statbuf, attribute_matcher);
1828 #ifdef G_OS_UNIX
1829 if (stat_ok && _g_local_file_is_lost_found_dir (path, statbuf.st_dev))
1830 g_file_info_set_is_hidden (info, TRUE);
1831 #endif
1833 #ifndef G_OS_WIN32
1834 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1835 G_FILE_ATTRIBUTE_ID_STANDARD_IS_HIDDEN))
1837 if (basename != NULL &&
1838 (basename[0] == '.' ||
1839 file_is_hidden (path, basename)))
1840 g_file_info_set_is_hidden (info, TRUE);
1843 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1844 (stat_ok && S_ISREG (statbuf.st_mode)))
1845 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1846 #else
1847 if (statbuf.attributes & FILE_ATTRIBUTE_HIDDEN)
1848 g_file_info_set_is_hidden (info, TRUE);
1850 if (statbuf.attributes & FILE_ATTRIBUTE_ARCHIVE)
1851 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1853 if (statbuf.attributes & FILE_ATTRIBUTE_SYSTEM)
1854 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1855 #endif
1857 symlink_target = NULL;
1858 if (is_symlink)
1860 #if defined (S_ISLNK) || defined (G_OS_WIN32)
1861 symlink_target = read_link (path);
1862 #endif
1863 if (symlink_target &&
1864 _g_file_attribute_matcher_matches_id (attribute_matcher,
1865 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1866 g_file_info_set_symlink_target (info, symlink_target);
1869 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1870 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1871 _g_file_attribute_matcher_matches_id (attribute_matcher,
1872 G_FILE_ATTRIBUTE_ID_STANDARD_ICON) ||
1873 _g_file_attribute_matcher_matches_id (attribute_matcher,
1874 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1876 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
1878 if (content_type)
1880 g_file_info_set_content_type (info, content_type);
1882 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1883 G_FILE_ATTRIBUTE_ID_STANDARD_ICON)
1884 || _g_file_attribute_matcher_matches_id (attribute_matcher,
1885 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1887 GIcon *icon;
1889 /* non symbolic icon */
1890 icon = get_icon (path, content_type, FALSE);
1891 if (icon != NULL)
1893 g_file_info_set_icon (info, icon);
1894 g_object_unref (icon);
1897 /* symbolic icon */
1898 icon = get_icon (path, content_type, TRUE);
1899 if (icon != NULL)
1901 g_file_info_set_symbolic_icon (info, icon);
1902 g_object_unref (icon);
1907 g_free (content_type);
1911 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1912 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1914 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
1916 if (content_type)
1918 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1919 g_free (content_type);
1923 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1924 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1926 char *name = NULL;
1928 #ifdef G_OS_WIN32
1929 win32_get_file_user_info (path, NULL, &name, NULL);
1930 #else
1931 if (stat_ok)
1932 name = get_username_from_uid (statbuf.st_uid);
1933 #endif
1934 if (name)
1935 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1936 g_free (name);
1939 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1940 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1942 char *name = NULL;
1943 #ifdef G_OS_WIN32
1944 win32_get_file_user_info (path, NULL, NULL, &name);
1945 #else
1946 if (stat_ok)
1947 name = get_realname_from_uid (statbuf.st_uid);
1948 #endif
1949 if (name)
1950 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1951 g_free (name);
1954 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1955 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1957 char *name = NULL;
1958 #ifdef G_OS_WIN32
1959 win32_get_file_user_info (path, &name, NULL, NULL);
1960 #else
1961 if (stat_ok)
1962 name = get_groupname_from_gid (statbuf.st_gid);
1963 #endif
1964 if (name)
1965 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1966 g_free (name);
1969 if (stat_ok && parent_info && parent_info->device != 0 &&
1970 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1971 statbuf.st_dev != parent_info->device)
1972 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1974 if (stat_ok)
1975 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1977 #ifdef HAVE_SELINUX
1978 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1979 #endif
1980 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1981 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1983 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1984 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH) ||
1985 _g_file_attribute_matcher_matches_id (attribute_matcher,
1986 G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID) ||
1987 _g_file_attribute_matcher_matches_id (attribute_matcher,
1988 G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED))
1990 if (stat_ok)
1991 get_thumbnail_attributes (path, info, &statbuf);
1992 else
1993 get_thumbnail_attributes (path, info, NULL);
1996 vfs = g_vfs_get_default ();
1997 class = G_VFS_GET_CLASS (vfs);
1998 if (class->local_file_add_info)
2000 class->local_file_add_info (vfs,
2001 path,
2002 device,
2003 attribute_matcher,
2004 info,
2005 NULL,
2006 &parent_info->extra_data,
2007 &parent_info->free_extra_data);
2010 g_file_info_unset_attribute_mask (info);
2012 g_free (symlink_target);
2014 return info;
2017 GFileInfo *
2018 _g_local_file_info_get_from_fd (int fd,
2019 const char *attributes,
2020 GError **error)
2022 GLocalFileStat stat_buf;
2023 GFileAttributeMatcher *matcher;
2024 GFileInfo *info;
2026 #ifdef G_OS_WIN32
2027 #define FSTAT GLIB_PRIVATE_CALL (g_win32_fstat)
2028 #else
2029 #define FSTAT fstat
2030 #endif
2032 if (FSTAT (fd, &stat_buf) == -1)
2034 int errsv = errno;
2036 g_set_error (error, G_IO_ERROR,
2037 g_io_error_from_errno (errsv),
2038 _("Error when getting information for file descriptor: %s"),
2039 g_strerror (errsv));
2040 return NULL;
2043 info = g_file_info_new ();
2045 matcher = g_file_attribute_matcher_new (attributes);
2047 /* Make sure we don't set any unwanted attributes */
2048 g_file_info_set_attribute_mask (info, matcher);
2050 set_info_from_stat (info, &stat_buf, matcher);
2052 #ifdef HAVE_SELINUX
2053 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
2054 is_selinux_enabled ())
2056 char *context;
2057 if (fgetfilecon_raw (fd, &context) >= 0)
2059 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
2060 freecon (context);
2063 #endif
2065 get_xattrs_from_fd (fd, TRUE, info, matcher);
2066 get_xattrs_from_fd (fd, FALSE, info, matcher);
2068 g_file_attribute_matcher_unref (matcher);
2070 g_file_info_unset_attribute_mask (info);
2072 return info;
2075 static gboolean
2076 get_uint32 (const GFileAttributeValue *value,
2077 guint32 *val_out,
2078 GError **error)
2080 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
2082 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2083 _("Invalid attribute type (uint32 expected)"));
2084 return FALSE;
2087 *val_out = value->u.uint32;
2089 return TRUE;
2092 #ifdef HAVE_UTIMES
2093 static gboolean
2094 get_uint64 (const GFileAttributeValue *value,
2095 guint64 *val_out,
2096 GError **error)
2098 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
2100 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2101 _("Invalid attribute type (uint64 expected)"));
2102 return FALSE;
2105 *val_out = value->u.uint64;
2107 return TRUE;
2109 #endif
2111 #if defined(HAVE_SYMLINK)
2112 static gboolean
2113 get_byte_string (const GFileAttributeValue *value,
2114 const char **val_out,
2115 GError **error)
2117 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
2119 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2120 _("Invalid attribute type (byte string expected)"));
2121 return FALSE;
2124 *val_out = value->u.string;
2126 return TRUE;
2128 #endif
2130 #ifdef HAVE_SELINUX
2131 static gboolean
2132 get_string (const GFileAttributeValue *value,
2133 const char **val_out,
2134 GError **error)
2136 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
2138 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2139 _("Invalid attribute type (byte string expected)"));
2140 return FALSE;
2143 *val_out = value->u.string;
2145 return TRUE;
2147 #endif
2149 static gboolean
2150 set_unix_mode (char *filename,
2151 GFileQueryInfoFlags flags,
2152 const GFileAttributeValue *value,
2153 GError **error)
2155 guint32 val = 0;
2156 int res = 0;
2158 if (!get_uint32 (value, &val, error))
2159 return FALSE;
2161 #if defined (HAVE_SYMLINK) || defined (G_OS_WIN32)
2162 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
2163 #ifdef HAVE_LCHMOD
2164 res = lchmod (filename, val);
2165 #else
2166 gboolean is_symlink;
2167 #ifndef G_OS_WIN32
2168 struct stat statbuf;
2169 /* Calling chmod on a symlink changes permissions on the symlink.
2170 * We don't want to do this, so we need to check for a symlink */
2171 res = g_lstat (filename, &statbuf);
2172 is_symlink = (res == 0 && S_ISLNK (statbuf.st_mode));
2173 #else
2174 /* FIXME: implement lchmod for W32, should be doable */
2175 GWin32PrivateStat statbuf;
2177 res = GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (filename, &statbuf);
2178 is_symlink = (res == 0 && statbuf.reparse_tag == IO_REPARSE_TAG_SYMLINK);
2179 #endif
2180 if (is_symlink)
2182 g_set_error_literal (error, G_IO_ERROR,
2183 G_IO_ERROR_NOT_SUPPORTED,
2184 _("Cannot set permissions on symlinks"));
2185 return FALSE;
2187 else if (res == 0)
2188 res = g_chmod (filename, val);
2189 #endif
2190 } else
2191 #endif
2192 res = g_chmod (filename, val);
2194 if (res == -1)
2196 int errsv = errno;
2198 g_set_error (error, G_IO_ERROR,
2199 g_io_error_from_errno (errsv),
2200 _("Error setting permissions: %s"),
2201 g_strerror (errsv));
2202 return FALSE;
2204 return TRUE;
2207 #ifdef G_OS_UNIX
2208 static gboolean
2209 set_unix_uid_gid (char *filename,
2210 const GFileAttributeValue *uid_value,
2211 const GFileAttributeValue *gid_value,
2212 GFileQueryInfoFlags flags,
2213 GError **error)
2215 int res;
2216 guint32 val = 0;
2217 uid_t uid;
2218 gid_t gid;
2220 if (uid_value)
2222 if (!get_uint32 (uid_value, &val, error))
2223 return FALSE;
2224 uid = val;
2226 else
2227 uid = -1;
2229 if (gid_value)
2231 if (!get_uint32 (gid_value, &val, error))
2232 return FALSE;
2233 gid = val;
2235 else
2236 gid = -1;
2238 #ifdef HAVE_LCHOWN
2239 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
2240 res = lchown (filename, uid, gid);
2241 else
2242 #endif
2243 res = chown (filename, uid, gid);
2245 if (res == -1)
2247 int errsv = errno;
2249 g_set_error (error, G_IO_ERROR,
2250 g_io_error_from_errno (errsv),
2251 _("Error setting owner: %s"),
2252 g_strerror (errsv));
2253 return FALSE;
2255 return TRUE;
2257 #endif
2259 #ifdef HAVE_SYMLINK
2260 static gboolean
2261 set_symlink (char *filename,
2262 const GFileAttributeValue *value,
2263 GError **error)
2265 const char *val;
2266 struct stat statbuf;
2268 if (!get_byte_string (value, &val, error))
2269 return FALSE;
2271 if (val == NULL)
2273 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2274 _("symlink must be non-NULL"));
2275 return FALSE;
2278 if (g_lstat (filename, &statbuf))
2280 int errsv = errno;
2282 g_set_error (error, G_IO_ERROR,
2283 g_io_error_from_errno (errsv),
2284 _("Error setting symlink: %s"),
2285 g_strerror (errsv));
2286 return FALSE;
2289 if (!S_ISLNK (statbuf.st_mode))
2291 g_set_error_literal (error, G_IO_ERROR,
2292 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2293 _("Error setting symlink: file is not a symlink"));
2294 return FALSE;
2297 if (g_unlink (filename))
2299 int errsv = errno;
2301 g_set_error (error, G_IO_ERROR,
2302 g_io_error_from_errno (errsv),
2303 _("Error setting symlink: %s"),
2304 g_strerror (errsv));
2305 return FALSE;
2308 if (symlink (filename, val) != 0)
2310 int errsv = errno;
2312 g_set_error (error, G_IO_ERROR,
2313 g_io_error_from_errno (errsv),
2314 _("Error setting symlink: %s"),
2315 g_strerror (errsv));
2316 return FALSE;
2319 return TRUE;
2321 #endif
2323 #ifdef HAVE_UTIMES
2324 static int
2325 lazy_stat (char *filename,
2326 struct stat *statbuf,
2327 gboolean *called_stat)
2329 int res;
2331 if (*called_stat)
2332 return 0;
2334 res = g_stat (filename, statbuf);
2336 if (res == 0)
2337 *called_stat = TRUE;
2339 return res;
2343 static gboolean
2344 set_mtime_atime (char *filename,
2345 const GFileAttributeValue *mtime_value,
2346 const GFileAttributeValue *mtime_usec_value,
2347 const GFileAttributeValue *atime_value,
2348 const GFileAttributeValue *atime_usec_value,
2349 GError **error)
2351 int res;
2352 guint64 val = 0;
2353 guint32 val_usec = 0;
2354 struct stat statbuf;
2355 gboolean got_stat = FALSE;
2356 struct timeval times[2] = { {0, 0}, {0, 0} };
2358 /* ATIME */
2359 if (atime_value)
2361 if (!get_uint64 (atime_value, &val, error))
2362 return FALSE;
2363 times[0].tv_sec = val;
2365 else
2367 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2369 times[0].tv_sec = statbuf.st_mtime;
2370 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2371 times[0].tv_usec = statbuf.st_atimensec / 1000;
2372 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2373 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2374 #endif
2378 if (atime_usec_value)
2380 if (!get_uint32 (atime_usec_value, &val_usec, error))
2381 return FALSE;
2382 times[0].tv_usec = val_usec;
2385 /* MTIME */
2386 if (mtime_value)
2388 if (!get_uint64 (mtime_value, &val, error))
2389 return FALSE;
2390 times[1].tv_sec = val;
2392 else
2394 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2396 times[1].tv_sec = statbuf.st_mtime;
2397 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2398 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2399 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2400 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2401 #endif
2405 if (mtime_usec_value)
2407 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2408 return FALSE;
2409 times[1].tv_usec = val_usec;
2412 res = utimes (filename, times);
2413 if (res == -1)
2415 int errsv = errno;
2417 g_set_error (error, G_IO_ERROR,
2418 g_io_error_from_errno (errsv),
2419 _("Error setting modification or access time: %s"),
2420 g_strerror (errsv));
2421 return FALSE;
2423 return TRUE;
2425 #endif
2428 #ifdef HAVE_SELINUX
2429 static gboolean
2430 set_selinux_context (char *filename,
2431 const GFileAttributeValue *value,
2432 GError **error)
2434 const char *val;
2436 if (!get_string (value, &val, error))
2437 return FALSE;
2439 if (val == NULL)
2441 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2442 _("SELinux context must be non-NULL"));
2443 return FALSE;
2446 if (is_selinux_enabled ()) {
2447 security_context_t val_s;
2449 val_s = g_strdup (val);
2451 if (setfilecon_raw (filename, val_s) < 0)
2453 int errsv = errno;
2455 g_set_error (error, G_IO_ERROR,
2456 g_io_error_from_errno (errsv),
2457 _("Error setting SELinux context: %s"),
2458 g_strerror (errsv));
2459 return FALSE;
2461 g_free (val_s);
2462 } else {
2463 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2464 _("SELinux is not enabled on this system"));
2465 return FALSE;
2468 return TRUE;
2470 #endif
2473 gboolean
2474 _g_local_file_info_set_attribute (char *filename,
2475 const char *attribute,
2476 GFileAttributeType type,
2477 gpointer value_p,
2478 GFileQueryInfoFlags flags,
2479 GCancellable *cancellable,
2480 GError **error)
2482 GFileAttributeValue value = { 0 };
2483 GVfsClass *class;
2484 GVfs *vfs;
2486 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2488 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2489 return set_unix_mode (filename, flags, &value, error);
2491 #ifdef G_OS_UNIX
2492 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2493 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2494 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2495 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2496 #endif
2498 #ifdef HAVE_SYMLINK
2499 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2500 return set_symlink (filename, &value, error);
2501 #endif
2503 #ifdef HAVE_UTIMES
2504 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2505 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2506 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2507 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2508 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2509 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2510 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2511 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2512 #endif
2514 #ifdef HAVE_XATTR
2515 else if (g_str_has_prefix (attribute, "xattr::"))
2516 return set_xattr (filename, attribute, &value, error);
2517 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2518 return set_xattr (filename, attribute, &value, error);
2519 #endif
2521 #ifdef HAVE_SELINUX
2522 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2523 return set_selinux_context (filename, &value, error);
2524 #endif
2526 vfs = g_vfs_get_default ();
2527 class = G_VFS_GET_CLASS (vfs);
2528 if (class->local_file_set_attributes)
2530 GFileInfo *info;
2532 info = g_file_info_new ();
2533 g_file_info_set_attribute (info,
2534 attribute,
2535 type,
2536 value_p);
2537 if (!class->local_file_set_attributes (vfs, filename,
2538 info,
2539 flags, cancellable,
2540 error))
2542 g_object_unref (info);
2543 return FALSE;
2546 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2548 g_object_unref (info);
2549 return TRUE;
2552 g_object_unref (info);
2555 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2556 _("Setting attribute %s not supported"), attribute);
2557 return FALSE;
2560 gboolean
2561 _g_local_file_info_set_attributes (char *filename,
2562 GFileInfo *info,
2563 GFileQueryInfoFlags flags,
2564 GCancellable *cancellable,
2565 GError **error)
2567 GFileAttributeValue *value;
2568 #ifdef G_OS_UNIX
2569 GFileAttributeValue *uid, *gid;
2570 #ifdef HAVE_UTIMES
2571 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2572 #endif
2573 GFileAttributeStatus status;
2574 #endif
2575 gboolean res;
2576 GVfsClass *class;
2577 GVfs *vfs;
2579 /* Handles setting multiple specified data in a single set, and takes care
2580 of ordering restrictions when setting attributes */
2582 res = TRUE;
2584 /* Set symlink first, since this recreates the file */
2585 #ifdef HAVE_SYMLINK
2586 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2587 if (value)
2589 if (!set_symlink (filename, value, error))
2591 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2592 res = FALSE;
2593 /* Don't set error multiple times */
2594 error = NULL;
2596 else
2597 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2600 #endif
2602 #ifdef G_OS_UNIX
2603 /* Group uid and gid setting into one call
2604 * Change ownership before permissions, since ownership changes can
2605 change permissions (e.g. setuid)
2607 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2608 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2610 if (uid || gid)
2612 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2614 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2615 res = FALSE;
2616 /* Don't set error multiple times */
2617 error = NULL;
2619 else
2620 status = G_FILE_ATTRIBUTE_STATUS_SET;
2621 if (uid)
2622 uid->status = status;
2623 if (gid)
2624 gid->status = status;
2626 #endif
2628 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2629 if (value)
2631 if (!set_unix_mode (filename, flags, value, error))
2633 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2634 res = FALSE;
2635 /* Don't set error multiple times */
2636 error = NULL;
2638 else
2639 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2643 #ifdef HAVE_UTIMES
2644 /* Group all time settings into one call
2645 * Change times as the last thing to avoid it changing due to metadata changes
2648 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2649 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2650 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2651 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2653 if (mtime || mtime_usec || atime || atime_usec)
2655 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2657 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2658 res = FALSE;
2659 /* Don't set error multiple times */
2660 error = NULL;
2662 else
2663 status = G_FILE_ATTRIBUTE_STATUS_SET;
2665 if (mtime)
2666 mtime->status = status;
2667 if (mtime_usec)
2668 mtime_usec->status = status;
2669 if (atime)
2670 atime->status = status;
2671 if (atime_usec)
2672 atime_usec->status = status;
2674 #endif
2676 /* xattrs are handled by default callback */
2679 /* SELinux context */
2680 #ifdef HAVE_SELINUX
2681 if (is_selinux_enabled ()) {
2682 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2683 if (value)
2685 if (!set_selinux_context (filename, value, error))
2687 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2688 res = FALSE;
2689 /* Don't set error multiple times */
2690 error = NULL;
2692 else
2693 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2696 #endif
2698 vfs = g_vfs_get_default ();
2699 class = G_VFS_GET_CLASS (vfs);
2700 if (class->local_file_set_attributes)
2702 if (!class->local_file_set_attributes (vfs, filename,
2703 info,
2704 flags, cancellable,
2705 error))
2707 res = FALSE;
2708 /* Don't set error multiple times */
2709 error = NULL;
2713 return res;