Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / gio / glocalfileinfo.c
blobb3e29374a305ee91743f71f51db61c48f80a8d47
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;
824 parent_info->inode = 0;
826 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME) ||
827 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE) ||
828 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH) ||
829 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT))
831 /* FIXME: Windows: The underlying _waccess() call in the C
832 * library is mostly pointless as it only looks at the READONLY
833 * FAT-style attribute of the file, it doesn't check the ACL at
834 * all.
836 parent_info->writable = (g_access (dir, W_OK) == 0);
838 res = g_stat (dir, &statbuf);
841 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
842 * renamed or deleted only by the owner of the file, by the owner of the directory, and
843 * by a privileged process.
845 if (res == 0)
847 #ifdef S_ISVTX
848 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
849 #else
850 parent_info->is_sticky = FALSE;
851 #endif
852 parent_info->owner = statbuf.st_uid;
853 parent_info->device = statbuf.st_dev;
854 parent_info->inode = statbuf.st_ino;
855 /* No need to find trash dir if it's not writable anyway */
856 if (parent_info->writable &&
857 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
858 parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
863 void
864 _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info)
866 if (parent_info->extra_data &&
867 parent_info->free_extra_data)
868 parent_info->free_extra_data (parent_info->extra_data);
871 static void
872 get_access_rights (GFileAttributeMatcher *attribute_matcher,
873 GFileInfo *info,
874 const gchar *path,
875 GLocalFileStat *statbuf,
876 GLocalParentFileInfo *parent_info)
878 /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
879 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
880 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ))
881 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ,
882 g_access (path, R_OK) == 0);
884 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
885 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE))
886 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE,
887 g_access (path, W_OK) == 0);
889 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
890 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE))
891 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE,
892 g_access (path, X_OK) == 0);
895 if (parent_info)
897 gboolean writable;
899 writable = FALSE;
900 if (parent_info->writable)
902 #ifdef G_OS_WIN32
903 writable = TRUE;
904 #else
905 if (parent_info->is_sticky)
907 uid_t uid = geteuid ();
909 if (uid == statbuf->st_uid ||
910 uid == parent_info->owner ||
911 uid == 0)
912 writable = TRUE;
914 else
915 writable = TRUE;
916 #endif
919 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME))
920 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME,
921 writable);
923 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE))
924 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE,
925 writable);
927 /* Trashing is supported only if the parent device is the same */
928 if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
929 _g_file_info_set_attribute_boolean_by_id (info,
930 G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH,
931 writable &&
932 parent_info->has_trash_dir &&
933 parent_info->device == statbuf->st_dev);
937 static void
938 set_info_from_stat (GFileInfo *info,
939 GLocalFileStat *statbuf,
940 GFileAttributeMatcher *attribute_matcher)
942 GFileType file_type;
944 file_type = G_FILE_TYPE_UNKNOWN;
946 if (S_ISREG (statbuf->st_mode))
947 file_type = G_FILE_TYPE_REGULAR;
948 else if (S_ISDIR (statbuf->st_mode))
949 file_type = G_FILE_TYPE_DIRECTORY;
950 #ifndef G_OS_WIN32
951 else if (S_ISCHR (statbuf->st_mode) ||
952 S_ISBLK (statbuf->st_mode) ||
953 S_ISFIFO (statbuf->st_mode)
954 #ifdef S_ISSOCK
955 || S_ISSOCK (statbuf->st_mode)
956 #endif
958 file_type = G_FILE_TYPE_SPECIAL;
959 #endif
960 #ifdef S_ISLNK
961 else if (S_ISLNK (statbuf->st_mode))
962 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
963 #elif defined (G_OS_WIN32)
964 if (statbuf->reparse_tag == IO_REPARSE_TAG_SYMLINK)
965 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
966 #endif
968 g_file_info_set_file_type (info, file_type);
969 g_file_info_set_size (info, statbuf->st_size);
971 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
972 #ifndef G_OS_WIN32
973 /* Pointless setting these on Windows even if they exist in the struct */
974 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
975 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
976 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
977 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
978 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
979 #endif
980 /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
981 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
982 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
983 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
984 #endif
985 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
986 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
987 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
988 statbuf->st_blocks * G_GUINT64_CONSTANT (512));
989 #elif defined (G_OS_WIN32)
990 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
991 statbuf->allocated_size);
993 #endif
995 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
996 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
997 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
998 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
999 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
1000 #endif
1002 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
1003 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1004 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
1005 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1006 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
1007 #endif
1009 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
1010 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
1011 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
1012 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
1013 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
1014 #endif
1016 #if defined (HAVE_STRUCT_STAT_ST_BIRTHTIME) && defined (HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
1017 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtime);
1018 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED_USEC, statbuf->st_birthtimensec / 1000);
1019 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIM) && defined (HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
1020 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtim.tv_sec);
1021 _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED_USEC, statbuf->st_birthtim.tv_nsec / 1000);
1022 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIME)
1023 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtime);
1024 #elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIM)
1025 _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtim);
1026 #endif
1028 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1029 G_FILE_ATTRIBUTE_ID_ETAG_VALUE))
1031 char *etag = _g_local_file_info_create_etag (statbuf);
1032 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ETAG_VALUE, etag);
1033 g_free (etag);
1036 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1037 G_FILE_ATTRIBUTE_ID_ID_FILE))
1039 char *id = _g_local_file_info_create_file_id (statbuf);
1040 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILE, id);
1041 g_free (id);
1044 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1045 G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM))
1047 char *id = _g_local_file_info_create_fs_id (statbuf);
1048 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM, id);
1049 g_free (id);
1053 #ifndef G_OS_WIN32
1055 static char *
1056 make_valid_utf8 (const char *name)
1058 GString *string;
1059 const gchar *remainder, *invalid;
1060 gint remaining_bytes, valid_bytes;
1062 string = NULL;
1063 remainder = name;
1064 remaining_bytes = strlen (name);
1066 while (remaining_bytes != 0)
1068 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1069 break;
1070 valid_bytes = invalid - remainder;
1072 if (string == NULL)
1073 string = g_string_sized_new (remaining_bytes);
1075 g_string_append_len (string, remainder, valid_bytes);
1076 /* append U+FFFD REPLACEMENT CHARACTER */
1077 g_string_append (string, "\357\277\275");
1079 remaining_bytes -= valid_bytes + 1;
1080 remainder = invalid + 1;
1083 if (string == NULL)
1084 return g_strdup (name);
1086 g_string_append (string, remainder);
1088 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
1090 return g_string_free (string, FALSE);
1093 static char *
1094 convert_pwd_string_to_utf8 (char *pwd_str)
1096 char *utf8_string;
1098 if (!g_utf8_validate (pwd_str, -1, NULL))
1100 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
1101 if (utf8_string == NULL)
1102 utf8_string = make_valid_utf8 (pwd_str);
1104 else
1105 utf8_string = g_strdup (pwd_str);
1107 return utf8_string;
1110 static void
1111 uid_data_free (UidData *data)
1113 g_free (data->user_name);
1114 g_free (data->real_name);
1115 g_free (data);
1118 /* called with lock held */
1119 static UidData *
1120 lookup_uid_data (uid_t uid)
1122 UidData *data;
1123 char buffer[4096];
1124 struct passwd pwbuf;
1125 struct passwd *pwbufp;
1126 #ifndef __BIONIC__
1127 char *gecos, *comma;
1128 #endif
1130 if (uid_cache == NULL)
1131 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1133 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1135 if (data)
1136 return data;
1138 data = g_new0 (UidData, 1);
1140 #if defined(HAVE_GETPWUID_R)
1141 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1142 #else
1143 pwbufp = getpwuid (uid);
1144 #endif
1146 if (pwbufp != NULL)
1148 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1149 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1151 #ifndef __BIONIC__
1152 gecos = pwbufp->pw_gecos;
1154 if (gecos)
1156 comma = strchr (gecos, ',');
1157 if (comma)
1158 *comma = 0;
1159 data->real_name = convert_pwd_string_to_utf8 (gecos);
1161 #endif
1164 /* Default fallbacks */
1165 if (data->real_name == NULL)
1167 if (data->user_name != NULL)
1168 data->real_name = g_strdup (data->user_name);
1169 else
1170 data->real_name = g_strdup_printf ("user #%d", (int)uid);
1173 if (data->user_name == NULL)
1174 data->user_name = g_strdup_printf ("%d", (int)uid);
1176 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1178 return data;
1181 static char *
1182 get_username_from_uid (uid_t uid)
1184 char *res;
1185 UidData *data;
1187 G_LOCK (uid_cache);
1188 data = lookup_uid_data (uid);
1189 res = g_strdup (data->user_name);
1190 G_UNLOCK (uid_cache);
1192 return res;
1195 static char *
1196 get_realname_from_uid (uid_t uid)
1198 char *res;
1199 UidData *data;
1201 G_LOCK (uid_cache);
1202 data = lookup_uid_data (uid);
1203 res = g_strdup (data->real_name);
1204 G_UNLOCK (uid_cache);
1206 return res;
1209 /* called with lock held */
1210 static char *
1211 lookup_gid_name (gid_t gid)
1213 char *name;
1214 #if defined (HAVE_GETGRGID_R)
1215 char buffer[4096];
1216 struct group gbuf;
1217 #endif
1218 struct group *gbufp;
1220 if (gid_cache == NULL)
1221 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1223 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1225 if (name)
1226 return name;
1228 #if defined (HAVE_GETGRGID_R)
1229 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1230 #else
1231 gbufp = getgrgid (gid);
1232 #endif
1234 if (gbufp != NULL &&
1235 gbufp->gr_name != NULL &&
1236 gbufp->gr_name[0] != 0)
1237 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1238 else
1239 name = g_strdup_printf("%d", (int)gid);
1241 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1243 return name;
1246 static char *
1247 get_groupname_from_gid (gid_t gid)
1249 char *res;
1250 char *name;
1252 G_LOCK (gid_cache);
1253 name = lookup_gid_name (gid);
1254 res = g_strdup (name);
1255 G_UNLOCK (gid_cache);
1256 return res;
1259 #endif /* !G_OS_WIN32 */
1261 static char *
1262 get_content_type (const char *basename,
1263 const char *path,
1264 GLocalFileStat *statbuf,
1265 gboolean is_symlink,
1266 gboolean symlink_broken,
1267 GFileQueryInfoFlags flags,
1268 gboolean fast)
1270 if (is_symlink &&
1271 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1272 return g_content_type_from_mime_type ("inode/symlink");
1273 else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
1274 return g_content_type_from_mime_type ("inode/directory");
1275 #ifndef G_OS_WIN32
1276 else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
1277 return g_content_type_from_mime_type ("inode/chardevice");
1278 else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
1279 return g_content_type_from_mime_type ("inode/blockdevice");
1280 else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
1281 return g_content_type_from_mime_type ("inode/fifo");
1282 else if (statbuf != NULL && S_ISREG(statbuf->st_mode) && statbuf->st_size == 0)
1284 /* Don't sniff zero-length files in order to avoid reading files
1285 * that appear normal but are not (eg: files in /proc and /sys)
1287 * Note that we need to return text/plain here so that
1288 * newly-created text files are opened by the text editor.
1289 * See https://bugzilla.gnome.org/show_bug.cgi?id=755795
1291 return g_content_type_from_mime_type ("text/plain");
1293 #endif
1294 #ifdef S_ISSOCK
1295 else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
1296 return g_content_type_from_mime_type ("inode/socket");
1297 #endif
1298 else
1300 char *content_type;
1301 gboolean result_uncertain;
1303 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1305 #if !defined(G_OS_WIN32) && !defined(HAVE_COCOA)
1306 if (!fast && result_uncertain && path != NULL)
1308 guchar sniff_buffer[4096];
1309 gsize sniff_length;
1310 int fd, errsv;
1312 sniff_length = _g_unix_content_type_get_sniff_len ();
1313 if (sniff_length > 4096)
1314 sniff_length = 4096;
1316 #ifdef O_NOATIME
1317 fd = g_open (path, O_RDONLY | O_NOATIME, 0);
1318 errsv = errno;
1319 if (fd < 0 && errsv == EPERM)
1320 #endif
1321 fd = g_open (path, O_RDONLY, 0);
1323 if (fd != -1)
1325 gssize res;
1327 res = read (fd, sniff_buffer, sniff_length);
1328 (void) g_close (fd, NULL);
1329 if (res >= 0)
1331 g_free (content_type);
1332 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1336 #endif
1338 return content_type;
1343 /* @stat_buf is the pre-calculated result of stat(path), or %NULL if that failed. */
1344 static void
1345 get_thumbnail_attributes (const char *path,
1346 GFileInfo *info,
1347 const GLocalFileStat *stat_buf)
1349 GChecksum *checksum;
1350 char *uri;
1351 char *filename;
1352 char *basename;
1354 uri = g_filename_to_uri (path, NULL, NULL);
1356 checksum = g_checksum_new (G_CHECKSUM_MD5);
1357 g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
1359 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1360 g_checksum_free (checksum);
1362 filename = g_build_filename (g_get_user_cache_dir (),
1363 "thumbnails", "large", basename,
1364 NULL);
1366 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1368 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1369 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1370 thumbnail_verify (filename, uri, stat_buf));
1372 else
1374 g_free (filename);
1375 filename = g_build_filename (g_get_user_cache_dir (),
1376 "thumbnails", "normal", basename,
1377 NULL);
1379 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1381 _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
1382 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1383 thumbnail_verify (filename, uri, stat_buf));
1385 else
1387 g_free (filename);
1388 filename = g_build_filename (g_get_user_cache_dir (),
1389 "thumbnails", "fail",
1390 "gnome-thumbnail-factory",
1391 basename,
1392 NULL);
1394 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1396 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
1397 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
1398 thumbnail_verify (filename, uri, stat_buf));
1402 g_free (basename);
1403 g_free (filename);
1404 g_free (uri);
1407 #ifdef G_OS_WIN32
1408 static void
1409 win32_get_file_user_info (const gchar *filename,
1410 gchar **group_name,
1411 gchar **user_name,
1412 gchar **real_name)
1414 PSECURITY_DESCRIPTOR psd = NULL;
1415 DWORD sd_size = 0; /* first call calculates the size required */
1417 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1418 if ((GetFileSecurityW (wfilename,
1419 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1420 NULL,
1421 sd_size,
1422 &sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
1423 (psd = g_try_malloc (sd_size)) != NULL &&
1424 GetFileSecurityW (wfilename,
1425 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
1426 psd,
1427 sd_size,
1428 &sd_size))
1430 PSID psid = 0;
1431 BOOL defaulted;
1432 SID_NAME_USE name_use = 0; /* don't care? */
1433 wchar_t *name = NULL;
1434 wchar_t *domain = NULL;
1435 DWORD name_len = 0;
1436 DWORD domain_len = 0;
1437 /* get the user name */
1438 do {
1439 if (!user_name)
1440 break;
1441 if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
1442 break;
1443 if (!LookupAccountSidW (NULL, /* local machine */
1444 psid,
1445 name, &name_len,
1446 domain, &domain_len, /* no domain info yet */
1447 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1448 break;
1449 name = g_try_malloc (name_len * sizeof (wchar_t));
1450 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1451 if (name && domain &&
1452 LookupAccountSidW (NULL, /* local machine */
1453 psid,
1454 name, &name_len,
1455 domain, &domain_len, /* no domain info yet */
1456 &name_use))
1458 *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1460 g_free (name);
1461 g_free (domain);
1462 } while (FALSE);
1464 /* get the group name */
1465 do {
1466 if (!group_name)
1467 break;
1468 if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
1469 break;
1470 if (!LookupAccountSidW (NULL, /* local machine */
1471 psid,
1472 name, &name_len,
1473 domain, &domain_len, /* no domain info yet */
1474 &name_use) && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
1475 break;
1476 name = g_try_malloc (name_len * sizeof (wchar_t));
1477 domain = g_try_malloc (domain_len * sizeof (wchar_t));
1478 if (name && domain &&
1479 LookupAccountSidW (NULL, /* local machine */
1480 psid,
1481 name, &name_len,
1482 domain, &domain_len, /* no domain info yet */
1483 &name_use))
1485 *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
1487 g_free (name);
1488 g_free (domain);
1489 } while (FALSE);
1491 /* TODO: get real name */
1493 g_free (psd);
1495 g_free (wfilename);
1497 #endif /* G_OS_WIN32 */
1499 #ifndef G_OS_WIN32
1500 /* support for '.hidden' files */
1501 G_LOCK_DEFINE_STATIC (hidden_cache);
1502 static GHashTable *hidden_cache;
1504 static gboolean
1505 remove_from_hidden_cache (gpointer user_data)
1507 G_LOCK (hidden_cache);
1508 g_hash_table_remove (hidden_cache, user_data);
1509 G_UNLOCK (hidden_cache);
1511 return FALSE;
1514 static GHashTable *
1515 read_hidden_file (const gchar *dirname)
1517 gchar *contents = NULL;
1518 gchar *filename;
1520 filename = g_build_path ("/", dirname, ".hidden", NULL);
1521 (void) g_file_get_contents (filename, &contents, NULL, NULL);
1522 g_free (filename);
1524 if (contents != NULL)
1526 GHashTable *table;
1527 gchar **lines;
1528 gint i;
1530 table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1532 lines = g_strsplit (contents, "\n", 0);
1533 g_free (contents);
1535 for (i = 0; lines[i]; i++)
1536 /* hash table takes the individual strings... */
1537 g_hash_table_add (table, lines[i]);
1539 /* ... so we only free the container. */
1540 g_free (lines);
1542 return table;
1544 else
1545 return NULL;
1548 static void
1549 maybe_unref_hash_table (gpointer data)
1551 if (data != NULL)
1552 g_hash_table_unref (data);
1555 static gboolean
1556 file_is_hidden (const gchar *path,
1557 const gchar *basename)
1559 gboolean result;
1560 gchar *dirname;
1561 gpointer table;
1563 dirname = g_path_get_dirname (path);
1565 G_LOCK (hidden_cache);
1567 if G_UNLIKELY (hidden_cache == NULL)
1568 hidden_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1569 g_free, maybe_unref_hash_table);
1571 if (!g_hash_table_lookup_extended (hidden_cache, dirname,
1572 NULL, &table))
1574 gchar *mydirname;
1575 GSource *remove_from_cache_source;
1577 g_hash_table_insert (hidden_cache,
1578 mydirname = g_strdup (dirname),
1579 table = read_hidden_file (dirname));
1581 remove_from_cache_source = g_timeout_source_new_seconds (5);
1582 g_source_set_priority (remove_from_cache_source, G_PRIORITY_DEFAULT);
1583 g_source_set_callback (remove_from_cache_source,
1584 remove_from_hidden_cache,
1585 mydirname,
1586 NULL);
1587 g_source_attach (remove_from_cache_source,
1588 GLIB_PRIVATE_CALL (g_get_worker_context) ());
1589 g_source_unref (remove_from_cache_source);
1592 result = table != NULL && g_hash_table_contains (table, basename);
1594 G_UNLOCK (hidden_cache);
1596 g_free (dirname);
1598 return result;
1600 #endif /* !G_OS_WIN32 */
1602 void
1603 _g_local_file_info_get_nostat (GFileInfo *info,
1604 const char *basename,
1605 const char *path,
1606 GFileAttributeMatcher *attribute_matcher)
1608 g_file_info_set_name (info, basename);
1610 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1611 G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
1613 char *display_name = g_filename_display_basename (path);
1615 /* look for U+FFFD REPLACEMENT CHARACTER */
1616 if (strstr (display_name, "\357\277\275") != NULL)
1618 char *p = display_name;
1619 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1620 g_free (p);
1622 g_file_info_set_display_name (info, display_name);
1623 g_free (display_name);
1626 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1627 G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
1629 char *edit_name = g_filename_display_basename (path);
1630 g_file_info_set_edit_name (info, edit_name);
1631 g_free (edit_name);
1635 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1636 G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
1638 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1639 if (copy_name)
1640 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
1641 g_free (copy_name);
1645 static const char *
1646 get_icon_name (const char *path,
1647 gboolean use_symbolic,
1648 gboolean *with_fallbacks_out)
1650 const char *name = NULL;
1651 gboolean with_fallbacks = TRUE;
1653 if (g_strcmp0 (path, g_get_home_dir ()) == 0)
1655 name = use_symbolic ? "user-home-symbolic" : "user-home";
1656 with_fallbacks = FALSE;
1658 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
1660 name = use_symbolic ? "user-desktop-symbolic" : "user-desktop";
1661 with_fallbacks = FALSE;
1663 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
1665 name = use_symbolic ? "folder-documents-symbolic" : "folder-documents";
1667 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
1669 name = use_symbolic ? "folder-download-symbolic" : "folder-download";
1671 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
1673 name = use_symbolic ? "folder-music-symbolic" : "folder-music";
1675 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
1677 name = use_symbolic ? "folder-pictures-symbolic" : "folder-pictures";
1679 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
1681 name = use_symbolic ? "folder-publicshare-symbolic" : "folder-publicshare";
1683 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
1685 name = use_symbolic ? "folder-templates-symbolic" : "folder-templates";
1687 else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
1689 name = use_symbolic ? "folder-videos-symbolic" : "folder-videos";
1691 else
1693 name = NULL;
1696 if (with_fallbacks_out != NULL)
1697 *with_fallbacks_out = with_fallbacks;
1699 return name;
1702 static GIcon *
1703 get_icon (const char *path,
1704 const char *content_type,
1705 gboolean use_symbolic)
1707 GIcon *icon = NULL;
1708 const char *icon_name;
1709 gboolean with_fallbacks;
1711 icon_name = get_icon_name (path, use_symbolic, &with_fallbacks);
1712 if (icon_name != NULL)
1714 if (with_fallbacks)
1715 icon = g_themed_icon_new_with_default_fallbacks (icon_name);
1716 else
1717 icon = g_themed_icon_new (icon_name);
1719 else
1721 if (use_symbolic)
1722 icon = g_content_type_get_symbolic_icon (content_type);
1723 else
1724 icon = g_content_type_get_icon (content_type);
1727 return icon;
1730 GFileInfo *
1731 _g_local_file_info_get (const char *basename,
1732 const char *path,
1733 GFileAttributeMatcher *attribute_matcher,
1734 GFileQueryInfoFlags flags,
1735 GLocalParentFileInfo *parent_info,
1736 GError **error)
1738 GFileInfo *info;
1739 GLocalFileStat statbuf;
1740 #ifdef S_ISLNK
1741 struct stat statbuf2;
1742 #elif defined (G_OS_WIN32)
1743 GWin32PrivateStat statbuf2;
1744 #endif
1745 int res;
1746 gboolean stat_ok;
1747 gboolean is_symlink, symlink_broken;
1748 char *symlink_target;
1749 GVfs *vfs;
1750 GVfsClass *class;
1751 guint64 device;
1753 info = g_file_info_new ();
1755 /* Make sure we don't set any unwanted attributes */
1756 g_file_info_set_attribute_mask (info, attribute_matcher);
1758 _g_local_file_info_get_nostat (info, basename, path, attribute_matcher);
1760 if (attribute_matcher == NULL)
1762 g_file_info_unset_attribute_mask (info);
1763 return info;
1766 #ifndef G_OS_WIN32
1767 res = g_lstat (path, &statbuf);
1768 #else
1769 res = GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (path, &statbuf);
1770 #endif
1772 if (res == -1)
1774 int errsv = errno;
1776 /* Don't bail out if we get Permission denied (SELinux?) */
1777 if (errsv != EACCES)
1779 char *display_name = g_filename_display_name (path);
1780 g_object_unref (info);
1781 g_set_error (error, G_IO_ERROR,
1782 g_io_error_from_errno (errsv),
1783 _("Error when getting information for file “%s”: %s"),
1784 display_name, g_strerror (errsv));
1785 g_free (display_name);
1786 return NULL;
1790 /* Even if stat() fails, try to get as much as other attributes possible */
1791 stat_ok = res != -1;
1793 if (stat_ok)
1794 device = statbuf.st_dev;
1795 else
1796 device = 0;
1798 #ifdef S_ISLNK
1799 is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
1800 #elif defined (G_OS_WIN32)
1801 /* glib already checked the FILE_ATTRIBUTE_REPARSE_POINT for us */
1802 is_symlink = stat_ok && statbuf.reparse_tag == IO_REPARSE_TAG_SYMLINK;
1803 #else
1804 is_symlink = FALSE;
1805 #endif
1806 symlink_broken = FALSE;
1808 if (is_symlink)
1810 g_file_info_set_is_symlink (info, TRUE);
1812 /* Unless NOFOLLOW was set we default to following symlinks */
1813 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1815 #ifndef G_OS_WIN32
1816 res = stat (path, &statbuf2);
1817 #else
1818 res = GLIB_PRIVATE_CALL (g_win32_stat_utf8) (path, &statbuf2);
1819 #endif
1821 /* Report broken links as symlinks */
1822 if (res != -1)
1824 statbuf = statbuf2;
1825 stat_ok = TRUE;
1827 else
1828 symlink_broken = TRUE;
1832 if (stat_ok)
1833 set_info_from_stat (info, &statbuf, attribute_matcher);
1835 #ifdef G_OS_UNIX
1836 if (stat_ok && _g_local_file_is_lost_found_dir (path, statbuf.st_dev))
1837 g_file_info_set_is_hidden (info, TRUE);
1838 #endif
1840 #ifndef G_OS_WIN32
1841 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1842 G_FILE_ATTRIBUTE_ID_STANDARD_IS_HIDDEN))
1844 if (basename != NULL &&
1845 (basename[0] == '.' ||
1846 file_is_hidden (path, basename)))
1847 g_file_info_set_is_hidden (info, TRUE);
1850 if (basename != NULL && basename[strlen (basename) -1] == '~' &&
1851 (stat_ok && S_ISREG (statbuf.st_mode)))
1852 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
1853 #else
1854 if (statbuf.attributes & FILE_ATTRIBUTE_HIDDEN)
1855 g_file_info_set_is_hidden (info, TRUE);
1857 if (statbuf.attributes & FILE_ATTRIBUTE_ARCHIVE)
1858 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
1860 if (statbuf.attributes & FILE_ATTRIBUTE_SYSTEM)
1861 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
1862 #endif
1864 symlink_target = NULL;
1865 if (is_symlink)
1867 #if defined (S_ISLNK) || defined (G_OS_WIN32)
1868 symlink_target = read_link (path);
1869 #endif
1870 if (symlink_target &&
1871 _g_file_attribute_matcher_matches_id (attribute_matcher,
1872 G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
1873 g_file_info_set_symlink_target (info, symlink_target);
1876 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1877 G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
1878 _g_file_attribute_matcher_matches_id (attribute_matcher,
1879 G_FILE_ATTRIBUTE_ID_STANDARD_ICON) ||
1880 _g_file_attribute_matcher_matches_id (attribute_matcher,
1881 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1883 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
1885 if (content_type)
1887 g_file_info_set_content_type (info, content_type);
1889 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1890 G_FILE_ATTRIBUTE_ID_STANDARD_ICON)
1891 || _g_file_attribute_matcher_matches_id (attribute_matcher,
1892 G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
1894 GIcon *icon;
1896 /* non symbolic icon */
1897 icon = get_icon (path, content_type, FALSE);
1898 if (icon != NULL)
1900 g_file_info_set_icon (info, icon);
1901 g_object_unref (icon);
1904 /* symbolic icon */
1905 icon = get_icon (path, content_type, TRUE);
1906 if (icon != NULL)
1908 g_file_info_set_symbolic_icon (info, icon);
1909 g_object_unref (icon);
1914 g_free (content_type);
1918 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1919 G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
1921 char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
1923 if (content_type)
1925 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
1926 g_free (content_type);
1930 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1931 G_FILE_ATTRIBUTE_ID_OWNER_USER))
1933 char *name = NULL;
1935 #ifdef G_OS_WIN32
1936 win32_get_file_user_info (path, NULL, &name, NULL);
1937 #else
1938 if (stat_ok)
1939 name = get_username_from_uid (statbuf.st_uid);
1940 #endif
1941 if (name)
1942 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
1943 g_free (name);
1946 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1947 G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
1949 char *name = NULL;
1950 #ifdef G_OS_WIN32
1951 win32_get_file_user_info (path, NULL, NULL, &name);
1952 #else
1953 if (stat_ok)
1954 name = get_realname_from_uid (statbuf.st_uid);
1955 #endif
1956 if (name)
1957 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
1958 g_free (name);
1961 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1962 G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
1964 char *name = NULL;
1965 #ifdef G_OS_WIN32
1966 win32_get_file_user_info (path, &name, NULL, NULL);
1967 #else
1968 if (stat_ok)
1969 name = get_groupname_from_gid (statbuf.st_gid);
1970 #endif
1971 if (name)
1972 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
1973 g_free (name);
1976 if (stat_ok && parent_info && parent_info->device != 0 &&
1977 _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
1978 (statbuf.st_dev != parent_info->device || statbuf.st_ino == parent_info->inode))
1979 _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
1981 if (stat_ok)
1982 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1984 #ifdef HAVE_SELINUX
1985 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1986 #endif
1987 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1988 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1990 if (_g_file_attribute_matcher_matches_id (attribute_matcher,
1991 G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH) ||
1992 _g_file_attribute_matcher_matches_id (attribute_matcher,
1993 G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID) ||
1994 _g_file_attribute_matcher_matches_id (attribute_matcher,
1995 G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED))
1997 if (stat_ok)
1998 get_thumbnail_attributes (path, info, &statbuf);
1999 else
2000 get_thumbnail_attributes (path, info, NULL);
2003 vfs = g_vfs_get_default ();
2004 class = G_VFS_GET_CLASS (vfs);
2005 if (class->local_file_add_info)
2007 class->local_file_add_info (vfs,
2008 path,
2009 device,
2010 attribute_matcher,
2011 info,
2012 NULL,
2013 &parent_info->extra_data,
2014 &parent_info->free_extra_data);
2017 g_file_info_unset_attribute_mask (info);
2019 g_free (symlink_target);
2021 return info;
2024 GFileInfo *
2025 _g_local_file_info_get_from_fd (int fd,
2026 const char *attributes,
2027 GError **error)
2029 GLocalFileStat stat_buf;
2030 GFileAttributeMatcher *matcher;
2031 GFileInfo *info;
2033 #ifdef G_OS_WIN32
2034 #define FSTAT GLIB_PRIVATE_CALL (g_win32_fstat)
2035 #else
2036 #define FSTAT fstat
2037 #endif
2039 if (FSTAT (fd, &stat_buf) == -1)
2041 int errsv = errno;
2043 g_set_error (error, G_IO_ERROR,
2044 g_io_error_from_errno (errsv),
2045 _("Error when getting information for file descriptor: %s"),
2046 g_strerror (errsv));
2047 return NULL;
2050 info = g_file_info_new ();
2052 matcher = g_file_attribute_matcher_new (attributes);
2054 /* Make sure we don't set any unwanted attributes */
2055 g_file_info_set_attribute_mask (info, matcher);
2057 set_info_from_stat (info, &stat_buf, matcher);
2059 #ifdef HAVE_SELINUX
2060 if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
2061 is_selinux_enabled ())
2063 char *context;
2064 if (fgetfilecon_raw (fd, &context) >= 0)
2066 _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
2067 freecon (context);
2070 #endif
2072 get_xattrs_from_fd (fd, TRUE, info, matcher);
2073 get_xattrs_from_fd (fd, FALSE, info, matcher);
2075 g_file_attribute_matcher_unref (matcher);
2077 g_file_info_unset_attribute_mask (info);
2079 return info;
2082 static gboolean
2083 get_uint32 (const GFileAttributeValue *value,
2084 guint32 *val_out,
2085 GError **error)
2087 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
2089 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2090 _("Invalid attribute type (uint32 expected)"));
2091 return FALSE;
2094 *val_out = value->u.uint32;
2096 return TRUE;
2099 #ifdef HAVE_UTIMES
2100 static gboolean
2101 get_uint64 (const GFileAttributeValue *value,
2102 guint64 *val_out,
2103 GError **error)
2105 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
2107 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2108 _("Invalid attribute type (uint64 expected)"));
2109 return FALSE;
2112 *val_out = value->u.uint64;
2114 return TRUE;
2116 #endif
2118 #if defined(HAVE_SYMLINK)
2119 static gboolean
2120 get_byte_string (const GFileAttributeValue *value,
2121 const char **val_out,
2122 GError **error)
2124 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
2126 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2127 _("Invalid attribute type (byte string expected)"));
2128 return FALSE;
2131 *val_out = value->u.string;
2133 return TRUE;
2135 #endif
2137 #ifdef HAVE_SELINUX
2138 static gboolean
2139 get_string (const GFileAttributeValue *value,
2140 const char **val_out,
2141 GError **error)
2143 if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
2145 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2146 _("Invalid attribute type (byte string expected)"));
2147 return FALSE;
2150 *val_out = value->u.string;
2152 return TRUE;
2154 #endif
2156 static gboolean
2157 set_unix_mode (char *filename,
2158 GFileQueryInfoFlags flags,
2159 const GFileAttributeValue *value,
2160 GError **error)
2162 guint32 val = 0;
2163 int res = 0;
2165 if (!get_uint32 (value, &val, error))
2166 return FALSE;
2168 #if defined (HAVE_SYMLINK) || defined (G_OS_WIN32)
2169 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
2170 #ifdef HAVE_LCHMOD
2171 res = lchmod (filename, val);
2172 #else
2173 gboolean is_symlink;
2174 #ifndef G_OS_WIN32
2175 struct stat statbuf;
2176 /* Calling chmod on a symlink changes permissions on the symlink.
2177 * We don't want to do this, so we need to check for a symlink */
2178 res = g_lstat (filename, &statbuf);
2179 is_symlink = (res == 0 && S_ISLNK (statbuf.st_mode));
2180 #else
2181 /* FIXME: implement lchmod for W32, should be doable */
2182 GWin32PrivateStat statbuf;
2184 res = GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (filename, &statbuf);
2185 is_symlink = (res == 0 && statbuf.reparse_tag == IO_REPARSE_TAG_SYMLINK);
2186 #endif
2187 if (is_symlink)
2189 g_set_error_literal (error, G_IO_ERROR,
2190 G_IO_ERROR_NOT_SUPPORTED,
2191 _("Cannot set permissions on symlinks"));
2192 return FALSE;
2194 else if (res == 0)
2195 res = g_chmod (filename, val);
2196 #endif
2197 } else
2198 #endif
2199 res = g_chmod (filename, val);
2201 if (res == -1)
2203 int errsv = errno;
2205 g_set_error (error, G_IO_ERROR,
2206 g_io_error_from_errno (errsv),
2207 _("Error setting permissions: %s"),
2208 g_strerror (errsv));
2209 return FALSE;
2211 return TRUE;
2214 #ifdef G_OS_UNIX
2215 static gboolean
2216 set_unix_uid_gid (char *filename,
2217 const GFileAttributeValue *uid_value,
2218 const GFileAttributeValue *gid_value,
2219 GFileQueryInfoFlags flags,
2220 GError **error)
2222 int res;
2223 guint32 val = 0;
2224 uid_t uid;
2225 gid_t gid;
2227 if (uid_value)
2229 if (!get_uint32 (uid_value, &val, error))
2230 return FALSE;
2231 uid = val;
2233 else
2234 uid = -1;
2236 if (gid_value)
2238 if (!get_uint32 (gid_value, &val, error))
2239 return FALSE;
2240 gid = val;
2242 else
2243 gid = -1;
2245 #ifdef HAVE_LCHOWN
2246 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
2247 res = lchown (filename, uid, gid);
2248 else
2249 #endif
2250 res = chown (filename, uid, gid);
2252 if (res == -1)
2254 int errsv = errno;
2256 g_set_error (error, G_IO_ERROR,
2257 g_io_error_from_errno (errsv),
2258 _("Error setting owner: %s"),
2259 g_strerror (errsv));
2260 return FALSE;
2262 return TRUE;
2264 #endif
2266 #ifdef HAVE_SYMLINK
2267 static gboolean
2268 set_symlink (char *filename,
2269 const GFileAttributeValue *value,
2270 GError **error)
2272 const char *val;
2273 struct stat statbuf;
2275 if (!get_byte_string (value, &val, error))
2276 return FALSE;
2278 if (val == NULL)
2280 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2281 _("symlink must be non-NULL"));
2282 return FALSE;
2285 if (g_lstat (filename, &statbuf))
2287 int errsv = errno;
2289 g_set_error (error, G_IO_ERROR,
2290 g_io_error_from_errno (errsv),
2291 _("Error setting symlink: %s"),
2292 g_strerror (errsv));
2293 return FALSE;
2296 if (!S_ISLNK (statbuf.st_mode))
2298 g_set_error_literal (error, G_IO_ERROR,
2299 G_IO_ERROR_NOT_SYMBOLIC_LINK,
2300 _("Error setting symlink: file is not a symlink"));
2301 return FALSE;
2304 if (g_unlink (filename))
2306 int errsv = errno;
2308 g_set_error (error, G_IO_ERROR,
2309 g_io_error_from_errno (errsv),
2310 _("Error setting symlink: %s"),
2311 g_strerror (errsv));
2312 return FALSE;
2315 if (symlink (filename, val) != 0)
2317 int errsv = errno;
2319 g_set_error (error, G_IO_ERROR,
2320 g_io_error_from_errno (errsv),
2321 _("Error setting symlink: %s"),
2322 g_strerror (errsv));
2323 return FALSE;
2326 return TRUE;
2328 #endif
2330 #ifdef HAVE_UTIMES
2331 static int
2332 lazy_stat (char *filename,
2333 struct stat *statbuf,
2334 gboolean *called_stat)
2336 int res;
2338 if (*called_stat)
2339 return 0;
2341 res = g_stat (filename, statbuf);
2343 if (res == 0)
2344 *called_stat = TRUE;
2346 return res;
2350 static gboolean
2351 set_mtime_atime (char *filename,
2352 const GFileAttributeValue *mtime_value,
2353 const GFileAttributeValue *mtime_usec_value,
2354 const GFileAttributeValue *atime_value,
2355 const GFileAttributeValue *atime_usec_value,
2356 GError **error)
2358 int res;
2359 guint64 val = 0;
2360 guint32 val_usec = 0;
2361 struct stat statbuf;
2362 gboolean got_stat = FALSE;
2363 struct timeval times[2] = { {0, 0}, {0, 0} };
2365 /* ATIME */
2366 if (atime_value)
2368 if (!get_uint64 (atime_value, &val, error))
2369 return FALSE;
2370 times[0].tv_sec = val;
2372 else
2374 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2376 times[0].tv_sec = statbuf.st_mtime;
2377 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
2378 times[0].tv_usec = statbuf.st_atimensec / 1000;
2379 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
2380 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
2381 #endif
2385 if (atime_usec_value)
2387 if (!get_uint32 (atime_usec_value, &val_usec, error))
2388 return FALSE;
2389 times[0].tv_usec = val_usec;
2392 /* MTIME */
2393 if (mtime_value)
2395 if (!get_uint64 (mtime_value, &val, error))
2396 return FALSE;
2397 times[1].tv_sec = val;
2399 else
2401 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
2403 times[1].tv_sec = statbuf.st_mtime;
2404 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
2405 times[1].tv_usec = statbuf.st_mtimensec / 1000;
2406 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
2407 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
2408 #endif
2412 if (mtime_usec_value)
2414 if (!get_uint32 (mtime_usec_value, &val_usec, error))
2415 return FALSE;
2416 times[1].tv_usec = val_usec;
2419 res = utimes (filename, times);
2420 if (res == -1)
2422 int errsv = errno;
2424 g_set_error (error, G_IO_ERROR,
2425 g_io_error_from_errno (errsv),
2426 _("Error setting modification or access time: %s"),
2427 g_strerror (errsv));
2428 return FALSE;
2430 return TRUE;
2432 #endif
2435 #ifdef HAVE_SELINUX
2436 static gboolean
2437 set_selinux_context (char *filename,
2438 const GFileAttributeValue *value,
2439 GError **error)
2441 const char *val;
2443 if (!get_string (value, &val, error))
2444 return FALSE;
2446 if (val == NULL)
2448 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2449 _("SELinux context must be non-NULL"));
2450 return FALSE;
2453 if (is_selinux_enabled ()) {
2454 security_context_t val_s;
2456 val_s = g_strdup (val);
2458 if (setfilecon_raw (filename, val_s) < 0)
2460 int errsv = errno;
2462 g_set_error (error, G_IO_ERROR,
2463 g_io_error_from_errno (errsv),
2464 _("Error setting SELinux context: %s"),
2465 g_strerror (errsv));
2466 return FALSE;
2468 g_free (val_s);
2469 } else {
2470 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
2471 _("SELinux is not enabled on this system"));
2472 return FALSE;
2475 return TRUE;
2477 #endif
2480 gboolean
2481 _g_local_file_info_set_attribute (char *filename,
2482 const char *attribute,
2483 GFileAttributeType type,
2484 gpointer value_p,
2485 GFileQueryInfoFlags flags,
2486 GCancellable *cancellable,
2487 GError **error)
2489 GFileAttributeValue value = { 0 };
2490 GVfsClass *class;
2491 GVfs *vfs;
2493 _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
2495 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
2496 return set_unix_mode (filename, flags, &value, error);
2498 #ifdef G_OS_UNIX
2499 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
2500 return set_unix_uid_gid (filename, &value, NULL, flags, error);
2501 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
2502 return set_unix_uid_gid (filename, NULL, &value, flags, error);
2503 #endif
2505 #ifdef HAVE_SYMLINK
2506 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
2507 return set_symlink (filename, &value, error);
2508 #endif
2510 #ifdef HAVE_UTIMES
2511 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
2512 return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
2513 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
2514 return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
2515 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
2516 return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
2517 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
2518 return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
2519 #endif
2521 #ifdef HAVE_XATTR
2522 else if (g_str_has_prefix (attribute, "xattr::"))
2523 return set_xattr (filename, attribute, &value, error);
2524 else if (g_str_has_prefix (attribute, "xattr-sys::"))
2525 return set_xattr (filename, attribute, &value, error);
2526 #endif
2528 #ifdef HAVE_SELINUX
2529 else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
2530 return set_selinux_context (filename, &value, error);
2531 #endif
2533 vfs = g_vfs_get_default ();
2534 class = G_VFS_GET_CLASS (vfs);
2535 if (class->local_file_set_attributes)
2537 GFileInfo *info;
2539 info = g_file_info_new ();
2540 g_file_info_set_attribute (info,
2541 attribute,
2542 type,
2543 value_p);
2544 if (!class->local_file_set_attributes (vfs, filename,
2545 info,
2546 flags, cancellable,
2547 error))
2549 g_object_unref (info);
2550 return FALSE;
2553 if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
2555 g_object_unref (info);
2556 return TRUE;
2559 g_object_unref (info);
2562 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2563 _("Setting attribute %s not supported"), attribute);
2564 return FALSE;
2567 gboolean
2568 _g_local_file_info_set_attributes (char *filename,
2569 GFileInfo *info,
2570 GFileQueryInfoFlags flags,
2571 GCancellable *cancellable,
2572 GError **error)
2574 GFileAttributeValue *value;
2575 #ifdef G_OS_UNIX
2576 GFileAttributeValue *uid, *gid;
2577 #ifdef HAVE_UTIMES
2578 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
2579 #endif
2580 GFileAttributeStatus status;
2581 #endif
2582 gboolean res;
2583 GVfsClass *class;
2584 GVfs *vfs;
2586 /* Handles setting multiple specified data in a single set, and takes care
2587 of ordering restrictions when setting attributes */
2589 res = TRUE;
2591 /* Set symlink first, since this recreates the file */
2592 #ifdef HAVE_SYMLINK
2593 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2594 if (value)
2596 if (!set_symlink (filename, value, error))
2598 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2599 res = FALSE;
2600 /* Don't set error multiple times */
2601 error = NULL;
2603 else
2604 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2607 #endif
2609 #ifdef G_OS_UNIX
2610 /* Group uid and gid setting into one call
2611 * Change ownership before permissions, since ownership changes can
2612 change permissions (e.g. setuid)
2614 uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
2615 gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
2617 if (uid || gid)
2619 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
2621 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2622 res = FALSE;
2623 /* Don't set error multiple times */
2624 error = NULL;
2626 else
2627 status = G_FILE_ATTRIBUTE_STATUS_SET;
2628 if (uid)
2629 uid->status = status;
2630 if (gid)
2631 gid->status = status;
2633 #endif
2635 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
2636 if (value)
2638 if (!set_unix_mode (filename, flags, value, error))
2640 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2641 res = FALSE;
2642 /* Don't set error multiple times */
2643 error = NULL;
2645 else
2646 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2650 #ifdef HAVE_UTIMES
2651 /* Group all time settings into one call
2652 * Change times as the last thing to avoid it changing due to metadata changes
2655 mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
2656 mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2657 atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
2658 atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
2660 if (mtime || mtime_usec || atime || atime_usec)
2662 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
2664 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2665 res = FALSE;
2666 /* Don't set error multiple times */
2667 error = NULL;
2669 else
2670 status = G_FILE_ATTRIBUTE_STATUS_SET;
2672 if (mtime)
2673 mtime->status = status;
2674 if (mtime_usec)
2675 mtime_usec->status = status;
2676 if (atime)
2677 atime->status = status;
2678 if (atime_usec)
2679 atime_usec->status = status;
2681 #endif
2683 /* xattrs are handled by default callback */
2686 /* SELinux context */
2687 #ifdef HAVE_SELINUX
2688 if (is_selinux_enabled ()) {
2689 value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
2690 if (value)
2692 if (!set_selinux_context (filename, value, error))
2694 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2695 res = FALSE;
2696 /* Don't set error multiple times */
2697 error = NULL;
2699 else
2700 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2703 #endif
2705 vfs = g_vfs_get_default ();
2706 class = G_VFS_GET_CLASS (vfs);
2707 if (class->local_file_set_attributes)
2709 if (!class->local_file_set_attributes (vfs, filename,
2710 info,
2711 flags, cancellable,
2712 error))
2714 res = FALSE;
2715 /* Don't set error multiple times */
2716 error = NULL;
2720 return res;