1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <sys/types.h>
39 #include <selinux/selinux.h>
44 #if defined HAVE_SYS_XATTR_H
45 #include <sys/xattr.h>
46 #elif defined HAVE_ATTR_XATTR_H
47 #include <attr/xattr.h>
49 #error "Neither <sys/xattr.h> nor <attr/xattr.h> is present but extended attribute support is enabled."
50 #endif /* defined HAVE_SYS_XATTR_H || HAVE_ATTR_XATTR_H */
52 #endif /* HAVE_XATTR */
54 #include <glib/gstdio.h>
55 #include <glib/gchecksum.h>
59 #include "glocalfileinfo.h"
61 #include "gthemedicon.h"
62 #include "gcontenttype.h"
63 #include "gcontenttypeprivate.h"
67 struct ThumbMD5Context {
78 G_LOCK_DEFINE_STATIC (uid_cache);
79 static GHashTable *uid_cache = NULL;
81 G_LOCK_DEFINE_STATIC (gid_cache);
82 static GHashTable *gid_cache = NULL;
85 _g_local_file_info_create_etag (struct stat *statbuf)
89 tv.tv_sec = statbuf->st_mtime;
90 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
91 tv.tv_usec = statbuf->st_mtimensec / 1000;
92 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
93 tv.tv_usec = statbuf->st_mtim.tv_nsec / 1000;
98 return g_strdup_printf ("%lu:%lu", tv.tv_sec, tv.tv_usec);
102 _g_local_file_info_create_file_id (struct stat *statbuf)
104 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
105 (guint64) statbuf->st_dev,
106 (guint64) statbuf->st_ino);
110 _g_local_file_info_create_fs_id (struct stat *statbuf)
112 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
113 (guint64) statbuf->st_dev);
118 read_link (const gchar *full_name)
125 buffer = g_malloc (size);
131 read_size = readlink (full_name, buffer, size);
137 if (read_size < size)
139 buffer[read_size] = 0;
143 buffer = g_realloc (buffer, size);
150 /* Get the SELinux security context */
152 get_selinux_context (const char *path,
154 GFileAttributeMatcher *attribute_matcher,
155 gboolean follow_symlinks)
160 if (!g_file_attribute_matcher_matches (attribute_matcher, "selinux:context"))
163 if (is_selinux_enabled ())
167 if (lgetfilecon_raw (path, &context) < 0)
172 if (getfilecon_raw (path, &context) < 0)
178 g_file_info_set_attribute_string (info, "selinux:context", context);
187 /* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
188 * (Mac) getxattr(..., XATTR_NOFOLLOW)
190 #ifdef HAVE_XATTR_NOFOLLOW
191 #define g_fgetxattr(fd,name,value,size) fgetxattr(fd,name,value,size,0,0)
192 #define g_flistxattr(fd,name,size) flistxattr(fd,name,size,0)
193 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
195 #define g_fgetxattr fgetxattr
196 #define g_flistxattr flistxattr
197 #define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
201 g_getxattr (const char *path, const char *name, void *value, size_t size,
202 gboolean follow_symlinks)
204 #ifdef HAVE_XATTR_NOFOLLOW
205 return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
208 return getxattr (path, name, value, size);
210 return lgetxattr (path, name, value, size);
215 g_listxattr(const char *path, char *namebuf, size_t size,
216 gboolean follow_symlinks)
218 #ifdef HAVE_XATTR_NOFOLLOW
219 return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
222 return listxattr (path, namebuf, size);
224 return llistxattr (path, namebuf, size);
231 return c >= 32 && c <= 126 && c != '\\';
235 name_is_valid (const char *str)
239 if (!valid_char (*str++))
246 hex_escape_string (const char *str,
247 gboolean *free_return)
250 char *escaped_str, *p;
252 static char *hex_digits = "0123456789abcdef";
258 for (i = 0; i < len; i++)
260 if (!valid_char (str[i]))
264 if (num_invalid == 0)
266 *free_return = FALSE;
270 escaped_str = g_malloc (len + num_invalid*3 + 1);
273 for (i = 0; i < len; i++)
275 if (valid_char (str[i]))
282 *p++ = hex_digits[(c >> 4) & 0xf];
283 *p++ = hex_digits[c & 0xf];
293 hex_unescape_string (const char *str,
295 gboolean *free_return)
298 char *unescaped_str, *p;
304 if (strchr (str, '\\') == NULL)
308 *free_return = FALSE;
312 unescaped_str = g_malloc (len + 1);
315 for (i = 0; i < len; i++)
317 if (str[i] == '\\' &&
322 (g_ascii_xdigit_value (str[i+2]) << 4) |
323 g_ascii_xdigit_value (str[i+3]);
333 *out_len = p - unescaped_str;
335 return unescaped_str;
339 escape_xattr (GFileInfo *info,
340 const char *gio_attr, /* gio attribute name */
341 const char *value, /* Is zero terminated */
342 size_t len /* not including zero termination */)
345 gboolean free_escaped_val;
347 escaped_val = hex_escape_string (value, &free_escaped_val);
349 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
351 if (free_escaped_val)
352 g_free (escaped_val);
356 get_one_xattr (const char *path,
358 const char *gio_attr,
360 gboolean follow_symlinks)
366 len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
371 else if (len == -1 && errno == ERANGE)
373 len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
378 value_p = g_malloc (len+1);
380 len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
394 escape_xattr (info, gio_attr, value_p, len);
396 if (value_p != value)
400 #endif /* defined HAVE_XATTR */
403 get_xattrs (const char *path,
406 GFileAttributeMatcher *matcher,
407 gboolean follow_symlinks)
412 ssize_t list_res_size;
415 const char *attr, *attr2;
418 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
420 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr_sys");
424 list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
426 if (list_res_size == -1 ||
430 list_size = list_res_size;
431 list = g_malloc (list_size);
435 list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
437 if (list_res_size == -1 && errno == ERANGE)
439 list_size = list_size * 2;
440 list = g_realloc (list, list_size);
444 if (list_res_size == -1)
448 while (list_res_size > 0)
450 if ((user && g_str_has_prefix (attr, "user.")) ||
451 (!user && !g_str_has_prefix (attr, "user.")))
453 char *escaped_attr, *gio_attr;
454 gboolean free_escaped_attr;
458 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
459 gio_attr = g_strconcat ("xattr:", escaped_attr, NULL);
463 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
464 gio_attr = g_strconcat ("xattr_sys:", escaped_attr, NULL);
467 if (free_escaped_attr)
468 g_free (escaped_attr);
470 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
473 len = strlen (attr) + 1;
475 list_res_size -= len;
482 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
484 char *unescaped_attribute, *a;
485 gboolean free_unescaped_attribute;
487 attr2 = strchr (attr, ':');
490 attr2++; /* Skip ':' */
491 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
493 a = g_strconcat ("user.", unescaped_attribute, NULL);
495 a = unescaped_attribute;
497 get_one_xattr (path, info, attr, a, follow_symlinks);
502 if (free_unescaped_attribute)
503 g_free (unescaped_attribute);
507 #endif /* defined HAVE_XATTR */
512 get_one_xattr_from_fd (int fd,
514 const char *gio_attr,
521 len = g_fgetxattr (fd, xattr, value, sizeof (value)-1);
526 else if (len == -1 && errno == ERANGE)
528 len = g_fgetxattr (fd, xattr, NULL, 0);
533 value_p = g_malloc (len+1);
535 len = g_fgetxattr (fd, xattr, value_p, len);
549 escape_xattr (info, gio_attr, value_p, len);
551 if (value_p != value)
554 #endif /* defined HAVE_XATTR */
557 get_xattrs_from_fd (int fd,
560 GFileAttributeMatcher *matcher)
565 ssize_t list_res_size;
568 const char *attr, *attr2;
571 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
573 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr_sys");
577 list_res_size = g_flistxattr (fd, NULL, 0);
579 if (list_res_size == -1 ||
583 list_size = list_res_size;
584 list = g_malloc (list_size);
588 list_res_size = g_flistxattr (fd, list, list_size);
590 if (list_res_size == -1 && errno == ERANGE)
592 list_size = list_size * 2;
593 list = g_realloc (list, list_size);
597 if (list_res_size == -1)
601 while (list_res_size > 0)
603 if ((user && g_str_has_prefix (attr, "user.")) ||
604 (!user && !g_str_has_prefix (attr, "user.")))
606 char *escaped_attr, *gio_attr;
607 gboolean free_escaped_attr;
611 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
612 gio_attr = g_strconcat ("xattr:", escaped_attr, NULL);
616 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
617 gio_attr = g_strconcat ("xattr_sys:", escaped_attr, NULL);
620 if (free_escaped_attr)
621 g_free (escaped_attr);
623 get_one_xattr_from_fd (fd, info, gio_attr, attr);
626 len = strlen (attr) + 1;
628 list_res_size -= len;
635 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
637 char *unescaped_attribute, *a;
638 gboolean free_unescaped_attribute;
640 attr2 = strchr (attr, ':');
643 attr2++; /* Skip ':' */
644 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
646 a = g_strconcat ("user.", unescaped_attribute, NULL);
648 a = unescaped_attribute;
650 get_one_xattr_from_fd (fd, info, attr, a);
655 if (free_unescaped_attribute)
656 g_free (unescaped_attribute);
660 #endif /* defined HAVE_XATTR */
665 set_xattr (char *filename,
666 const char *escaped_attribute,
667 const GFileAttributeValue *attr_value,
670 char *attribute, *value;
671 gboolean free_attribute, free_value;
672 int val_len, res, errsv;
676 if (attr_value == NULL)
678 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
679 _("Attribute value must be non-NULL"));
683 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
685 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
686 _("Invalid attribute type (string expected)"));
690 if (!name_is_valid (escaped_attribute))
692 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
693 _("Invalid extended attribute name"));
697 if (g_str_has_prefix (escaped_attribute, "xattr:"))
699 escaped_attribute += 6;
704 g_assert (g_str_has_prefix (escaped_attribute, "xattr_sys:"));
705 escaped_attribute += 10;
709 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
710 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
713 a = g_strconcat ("user.", attribute, NULL);
717 res = g_setxattr (filename, a, value, val_len);
731 g_set_error (error, G_IO_ERROR,
732 g_io_error_from_errno (errsv),
733 _("Error setting extended attribute '%s': %s"),
734 escaped_attribute, g_strerror (errno));
745 _g_local_file_info_get_parent_info (const char *dir,
746 GFileAttributeMatcher *attribute_matcher,
747 GLocalParentFileInfo *parent_info)
752 parent_info->writable = FALSE;
753 parent_info->is_sticky = FALSE;
754 parent_info->device = 0;
756 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME) ||
757 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE) ||
758 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH) ||
759 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT))
761 parent_info->writable = (g_access (dir, W_OK) == 0);
763 res = g_stat (dir, &statbuf);
766 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
767 * renamed or deleted only by the owner of the file, by the owner of the directory, and
768 * by a privileged process.
772 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
773 parent_info->owner = statbuf.st_uid;
774 parent_info->device = statbuf.st_dev;
780 get_access_rights (GFileAttributeMatcher *attribute_matcher,
783 struct stat *statbuf,
784 GLocalParentFileInfo *parent_info)
786 if (g_file_attribute_matcher_matches (attribute_matcher,
787 G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
788 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
789 g_access (path, R_OK) == 0);
791 if (g_file_attribute_matcher_matches (attribute_matcher,
792 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
793 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
794 g_access (path, W_OK) == 0);
796 if (g_file_attribute_matcher_matches (attribute_matcher,
797 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
798 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE,
799 g_access (path, X_OK) == 0);
807 if (parent_info->writable)
809 if (parent_info->is_sticky)
811 uid_t uid = geteuid ();
813 if (uid == statbuf->st_uid ||
814 uid == parent_info->owner ||
822 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME))
823 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME,
826 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE))
827 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE,
830 /* TODO: This means we can move it, but we should also look for a trash dir */
831 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
832 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH,
838 set_info_from_stat (GFileInfo *info,
839 struct stat *statbuf,
840 GFileAttributeMatcher *attribute_matcher)
844 file_type = G_FILE_TYPE_UNKNOWN;
846 if (S_ISREG (statbuf->st_mode))
847 file_type = G_FILE_TYPE_REGULAR;
848 else if (S_ISDIR (statbuf->st_mode))
849 file_type = G_FILE_TYPE_DIRECTORY;
850 else if (S_ISCHR (statbuf->st_mode) ||
851 S_ISBLK (statbuf->st_mode) ||
852 S_ISFIFO (statbuf->st_mode)
854 || S_ISSOCK (statbuf->st_mode)
857 file_type = G_FILE_TYPE_SPECIAL;
859 else if (S_ISLNK (statbuf->st_mode))
860 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
863 g_file_info_set_file_type (info, file_type);
864 g_file_info_set_size (info, statbuf->st_size);
866 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE, statbuf->st_dev);
867 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_INODE, statbuf->st_ino);
868 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, statbuf->st_mode);
869 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_NLINK, statbuf->st_nlink);
870 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_UID, statbuf->st_uid);
871 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, statbuf->st_gid);
872 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_RDEV, statbuf->st_rdev);
873 #if defined (HAVE_STRUCT_STAT_BLKSIZE)
874 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE, statbuf->st_blksize);
876 #if defined (HAVE_STRUCT_STAT_BLOCKS)
877 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_BLOCKS, statbuf->st_blocks);
880 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED, statbuf->st_mtime);
881 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
882 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
883 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
884 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
887 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS, statbuf->st_atime);
888 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
889 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
890 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
891 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
894 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_CHANGED, statbuf->st_ctime);
895 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
896 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
897 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
898 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
901 if (g_file_attribute_matcher_matches (attribute_matcher,
902 G_FILE_ATTRIBUTE_ETAG_VALUE))
904 char *etag = _g_local_file_info_create_etag (statbuf);
905 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ETAG_VALUE, etag);
909 if (g_file_attribute_matcher_matches (attribute_matcher,
910 G_FILE_ATTRIBUTE_ID_FILE))
912 char *id = _g_local_file_info_create_file_id (statbuf);
913 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE, id);
917 if (g_file_attribute_matcher_matches (attribute_matcher,
918 G_FILE_ATTRIBUTE_ID_FS))
920 char *id = _g_local_file_info_create_fs_id (statbuf);
921 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FS, id);
927 make_valid_utf8 (const char *name)
930 const gchar *remainder, *invalid;
931 gint remaining_bytes, valid_bytes;
935 remaining_bytes = strlen (name);
937 while (remaining_bytes != 0)
939 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
941 valid_bytes = invalid - remainder;
944 string = g_string_sized_new (remaining_bytes);
946 g_string_append_len (string, remainder, valid_bytes);
947 /* append U+FFFD REPLACEMENT CHARACTER */
948 g_string_append (string, "\357\277\275");
950 remaining_bytes -= valid_bytes + 1;
951 remainder = invalid + 1;
955 return g_strdup (name);
957 g_string_append (string, remainder);
959 g_assert (g_utf8_validate (string->str, -1, NULL));
961 return g_string_free (string, FALSE);
965 convert_pwd_string_to_utf8 (char *pwd_str)
969 if (!g_utf8_validate (pwd_str, -1, NULL))
971 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
972 if (utf8_string == NULL)
973 utf8_string = make_valid_utf8 (pwd_str);
976 utf8_string = g_strdup (pwd_str);
982 uid_data_free (UidData *data)
984 g_free (data->user_name);
985 g_free (data->real_name);
989 /* called with lock held */
991 lookup_uid_data (uid_t uid)
996 struct passwd *pwbufp;
999 if (uid_cache == NULL)
1000 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
1002 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
1007 data = g_new0 (UidData, 1);
1009 #if defined(HAVE_POSIX_GETPWUID_R)
1010 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
1011 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
1012 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
1014 pwbufp = getpwuid (uid);
1019 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
1020 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
1022 gecos = pwbufp->pw_gecos;
1026 comma = strchr (gecos, ',');
1029 data->real_name = convert_pwd_string_to_utf8 (gecos);
1033 /* Default fallbacks */
1034 if (data->real_name == NULL)
1036 if (data->user_name != NULL)
1037 data->real_name = g_strdup (data->user_name);
1039 data->real_name = g_strdup_printf("user #%d", (int)uid);
1042 if (data->user_name == NULL)
1043 data->user_name = g_strdup_printf("%d", (int)uid);
1045 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1051 get_username_from_uid (uid_t uid)
1057 data = lookup_uid_data (uid);
1058 res = g_strdup (data->user_name);
1059 G_UNLOCK (uid_cache);
1065 get_realname_from_uid (uid_t uid)
1071 data = lookup_uid_data (uid);
1072 res = g_strdup (data->real_name);
1073 G_UNLOCK (uid_cache);
1079 /* called with lock held */
1081 lookup_gid_name (gid_t gid)
1086 struct group *gbufp;
1088 if (gid_cache == NULL)
1089 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1091 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1096 #if defined (HAVE_POSIX_GETGRGID_R)
1097 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1098 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1099 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1101 gbufp = getgrgid (gid);
1104 if (gbufp != NULL &&
1105 gbufp->gr_name != NULL &&
1106 gbufp->gr_name[0] != 0)
1107 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1109 name = g_strdup_printf("%d", (int)gid);
1111 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1117 get_groupname_from_gid (gid_t gid)
1123 name = lookup_gid_name (gid);
1124 res = g_strdup (name);
1125 G_UNLOCK (gid_cache);
1130 get_content_type (const char *basename,
1132 struct stat *statbuf,
1133 gboolean is_symlink,
1134 gboolean symlink_broken,
1135 GFileQueryInfoFlags flags,
1139 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1140 return g_strdup ("inode/symlink");
1141 else if (S_ISDIR(statbuf->st_mode))
1142 return g_strdup ("inode/directory");
1143 else if (S_ISCHR(statbuf->st_mode))
1144 return g_strdup ("inode/chardevice");
1145 else if (S_ISBLK(statbuf->st_mode))
1146 return g_strdup ("inode/blockdevice");
1147 else if (S_ISFIFO(statbuf->st_mode))
1148 return g_strdup ("inode/fifo");
1150 else if (S_ISSOCK(statbuf->st_mode))
1151 return g_strdup ("inode/socket");
1156 gboolean result_uncertain;
1158 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1161 if (!fast && result_uncertain && path != NULL)
1163 guchar sniff_buffer[4096];
1167 sniff_length = _g_unix_content_type_get_sniff_len ();
1168 if (sniff_length > 4096)
1169 sniff_length = 4096;
1171 fd = open (path, O_RDONLY);
1176 res = read (fd, sniff_buffer, sniff_length);
1180 g_free (content_type);
1181 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1187 return content_type;
1193 get_thumbnail_attributes (const char *path,
1196 GChecksum *checksum;
1201 uri = g_filename_to_uri (path, NULL, NULL);
1203 checksum = g_checksum_new (G_CHECKSUM_MD5);
1204 g_checksum_update (checksum, (const gchar *) uri, strlen (uri));
1206 basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
1207 g_checksum_free (checksum);
1209 filename = g_build_filename (g_get_home_dir(),
1210 ".thumbnails", "normal", basename,
1213 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1214 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1218 filename = g_build_filename (g_get_home_dir(),
1219 ".thumbnails", "fail",
1220 "gnome-thumbnail-factory",
1224 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1225 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1233 _g_local_file_info_get (const char *basename,
1235 GFileAttributeMatcher *attribute_matcher,
1236 GFileQueryInfoFlags flags,
1237 GLocalParentFileInfo *parent_info,
1241 struct stat statbuf;
1242 struct stat statbuf2;
1244 gboolean is_symlink, symlink_broken;
1246 info = g_file_info_new ();
1248 /* Make sure we don't set any unwanted attributes */
1249 g_file_info_set_attribute_mask (info, attribute_matcher);
1251 g_file_info_set_name (info, basename);
1253 /* Avoid stat in trivial case */
1254 if (attribute_matcher == NULL)
1257 res = g_lstat (path, &statbuf);
1260 g_object_unref (info);
1261 g_set_error (error, G_IO_ERROR,
1262 g_io_error_from_errno (errno),
1263 _("Error stating file '%s': %s"),
1264 path, g_strerror (errno));
1269 is_symlink = S_ISLNK (statbuf.st_mode);
1273 symlink_broken = FALSE;
1277 g_file_info_set_is_symlink (info, TRUE);
1279 /* Unless NOFOLLOW was set we default to following symlinks */
1280 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1282 res = stat (path, &statbuf2);
1284 /* Report broken links as symlinks */
1288 symlink_broken = TRUE;
1292 set_info_from_stat (info, &statbuf, attribute_matcher);
1294 if (basename != NULL && basename[0] == '.')
1295 g_file_info_set_is_hidden (info, TRUE);
1297 if (basename != NULL && basename[strlen (basename) -1] == '~')
1298 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STD_IS_BACKUP, TRUE);
1301 g_file_attribute_matcher_matches (attribute_matcher,
1302 G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET))
1304 char *link = read_link (path);
1305 g_file_info_set_symlink_target (info, link);
1309 if (g_file_attribute_matcher_matches (attribute_matcher,
1310 G_FILE_ATTRIBUTE_STD_DISPLAY_NAME))
1312 char *display_name = g_filename_display_basename (path);
1314 if (strstr (display_name, "\357\277\275") != NULL)
1316 char *p = display_name;
1317 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1320 g_file_info_set_display_name (info, display_name);
1321 g_free (display_name);
1324 if (g_file_attribute_matcher_matches (attribute_matcher,
1325 G_FILE_ATTRIBUTE_STD_EDIT_NAME))
1327 char *edit_name = g_filename_display_basename (path);
1328 g_file_info_set_edit_name (info, edit_name);
1333 if (g_file_attribute_matcher_matches (attribute_matcher,
1334 G_FILE_ATTRIBUTE_STD_COPY_NAME))
1336 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1338 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STD_COPY_NAME, copy_name);
1342 if (g_file_attribute_matcher_matches (attribute_matcher,
1343 G_FILE_ATTRIBUTE_STD_CONTENT_TYPE) ||
1344 g_file_attribute_matcher_matches (attribute_matcher,
1345 G_FILE_ATTRIBUTE_STD_ICON))
1347 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1351 g_file_info_set_content_type (info, content_type);
1353 if (g_file_attribute_matcher_matches (attribute_matcher,
1354 G_FILE_ATTRIBUTE_STD_ICON))
1356 char *mimetype_icon, *generic_mimetype_icon, *type_icon, *p;
1357 char *icon_names[3];
1361 mimetype_icon = g_strdup (content_type);
1363 while ((p = strchr(mimetype_icon, '/')) != NULL)
1366 p = strchr (content_type, '/');
1368 p = content_type + strlen (content_type);
1370 generic_mimetype_icon = g_malloc (p - content_type + strlen ("-x-generic") + 1);
1371 memcpy (generic_mimetype_icon, content_type, p - content_type);
1372 memcpy (generic_mimetype_icon + (p - content_type), "-x-generic", strlen ("-x-generic"));
1373 generic_mimetype_icon[(p - content_type) + strlen ("-x-generic")] = 0;
1375 /* TODO: Special case desktop dir? That could be expensive with xdg dirs... */
1376 if (strcmp (path, g_get_home_dir ()) == 0)
1377 type_icon = "user-home";
1378 else if (S_ISDIR (statbuf.st_mode))
1379 type_icon = "folder";
1380 else if (statbuf.st_mode & S_IXUSR)
1381 type_icon = "application-x-executable";
1383 type_icon = "text-x-generic";
1386 icon_names[i++] = mimetype_icon;
1387 icon_names[i++] = generic_mimetype_icon;
1388 if (strcmp (generic_mimetype_icon, type_icon) != 0 &&
1389 strcmp (mimetype_icon, type_icon) != 0)
1390 icon_names[i++] = type_icon;
1392 icon = g_themed_icon_new_from_names (icon_names, i);
1393 g_file_info_set_icon (info, icon);
1395 g_object_unref (icon);
1396 g_free (mimetype_icon);
1397 g_free (generic_mimetype_icon);
1400 g_free (content_type);
1404 if (g_file_attribute_matcher_matches (attribute_matcher,
1405 G_FILE_ATTRIBUTE_STD_FAST_CONTENT_TYPE))
1407 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1411 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STD_FAST_CONTENT_TYPE, content_type);
1412 g_free (content_type);
1416 if (g_file_attribute_matcher_matches (attribute_matcher,
1417 G_FILE_ATTRIBUTE_OWNER_USER))
1421 name = get_username_from_uid (statbuf.st_uid);
1423 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1427 if (g_file_attribute_matcher_matches (attribute_matcher,
1428 G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1432 name = get_realname_from_uid (statbuf.st_uid);
1434 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1438 if (g_file_attribute_matcher_matches (attribute_matcher,
1439 G_FILE_ATTRIBUTE_OWNER_GROUP))
1443 name = get_groupname_from_gid (statbuf.st_gid);
1445 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1449 if (parent_info && parent_info->device != 0 &&
1450 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1451 statbuf.st_dev != parent_info->device)
1452 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1454 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1456 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1457 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1458 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1460 if (g_file_attribute_matcher_matches (attribute_matcher,
1461 G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1462 get_thumbnail_attributes (path, info);
1464 g_file_info_unset_attribute_mask (info);
1470 _g_local_file_info_get_from_fd (int fd,
1474 struct stat stat_buf;
1475 GFileAttributeMatcher *matcher;
1478 if (fstat (fd, &stat_buf) == -1)
1480 g_set_error (error, G_IO_ERROR,
1481 g_io_error_from_errno (errno),
1482 _("Error stating file descriptor: %s"),
1483 g_strerror (errno));
1487 info = g_file_info_new ();
1489 matcher = g_file_attribute_matcher_new (attributes);
1491 /* Make sure we don't set any unwanted attributes */
1492 g_file_info_set_attribute_mask (info, matcher);
1494 set_info_from_stat (info, &stat_buf, matcher);
1497 if (g_file_attribute_matcher_matches (matcher, "selinux:context") &&
1498 is_selinux_enabled ())
1501 if (fgetfilecon_raw (fd, &context) >= 0)
1503 g_file_info_set_attribute_string (info, "selinux:context", context);
1509 get_xattrs_from_fd (fd, TRUE, info, matcher);
1510 get_xattrs_from_fd (fd, FALSE, info, matcher);
1512 g_file_attribute_matcher_unref (matcher);
1514 g_file_info_unset_attribute_mask (info);
1520 get_uint32 (const GFileAttributeValue *value,
1524 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1526 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1527 _("Invalid attribute type (uint32 expected)"));
1531 *val_out = value->u.uint32;
1537 get_uint64 (const GFileAttributeValue *value,
1541 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1543 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1544 _("Invalid attribute type (uint64 expected)"));
1548 *val_out = value->u.uint64;
1553 #if defined(HAVE_SYMLINK)
1555 get_byte_string (const GFileAttributeValue *value,
1556 const char **val_out,
1559 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1561 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1562 _("Invalid attribute type (byte string expected)"));
1566 *val_out = value->u.string;
1573 set_unix_mode (char *filename,
1574 const GFileAttributeValue *value,
1579 if (!get_uint32 (value, &val, error))
1582 if (g_chmod (filename, val) == -1)
1584 g_set_error (error, G_IO_ERROR,
1585 g_io_error_from_errno (errno),
1586 _("Error setting permissions: %s"),
1587 g_strerror (errno));
1595 set_unix_uid_gid (char *filename,
1596 const GFileAttributeValue *uid_value,
1597 const GFileAttributeValue *gid_value,
1598 GFileQueryInfoFlags flags,
1608 if (!get_uint32 (uid_value, &val, error))
1617 if (!get_uint32 (gid_value, &val, error))
1624 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1625 res = lchown (filename, uid, gid);
1627 res = chown (filename, uid, gid);
1631 g_set_error (error, G_IO_ERROR,
1632 g_io_error_from_errno (errno),
1633 _("Error setting owner: %s"),
1634 g_strerror (errno));
1643 set_symlink (char *filename,
1644 const GFileAttributeValue *value,
1648 struct stat statbuf;
1650 if (!get_byte_string (value, &val, error))
1655 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1656 _("symlink must be non-NULL"));
1660 if (g_lstat (filename, &statbuf))
1662 g_set_error (error, G_IO_ERROR,
1663 g_io_error_from_errno (errno),
1664 _("Error setting symlink: %s"),
1665 g_strerror (errno));
1669 if (!S_ISLNK (statbuf.st_mode))
1671 g_set_error (error, G_IO_ERROR,
1672 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1673 _("Error setting symlink: file is not a symlink"));
1677 if (g_unlink (filename))
1679 g_set_error (error, G_IO_ERROR,
1680 g_io_error_from_errno (errno),
1681 _("Error setting symlink: %s"),
1682 g_strerror (errno));
1686 if (symlink (filename, val) != 0)
1688 g_set_error (error, G_IO_ERROR,
1689 g_io_error_from_errno (errno),
1690 _("Error setting symlink: %s"),
1691 g_strerror (errno));
1700 lazy_stat (char *filename,
1701 struct stat *statbuf,
1702 gboolean *called_stat)
1709 res = g_stat (filename, statbuf);
1712 *called_stat = TRUE;
1720 set_mtime_atime (char *filename,
1721 const GFileAttributeValue *mtime_value,
1722 const GFileAttributeValue *mtime_usec_value,
1723 const GFileAttributeValue *atime_value,
1724 const GFileAttributeValue *atime_usec_value,
1730 struct stat statbuf;
1731 gboolean got_stat = FALSE;
1732 struct timeval times[2] = { {0, 0}, {0, 0} };
1737 if (!get_uint64 (atime_value, &val, error))
1739 times[0].tv_sec = val;
1743 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1745 times[0].tv_sec = statbuf.st_mtime;
1746 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1747 times[0].tv_usec = statbuf.st_atimensec / 1000;
1748 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1749 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
1754 if (atime_usec_value)
1756 if (!get_uint32 (atime_usec_value, &val_usec, error))
1758 times[0].tv_usec = val_usec;
1764 if (!get_uint64 (mtime_value, &val, error))
1766 times[1].tv_sec = val;
1770 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1772 times[1].tv_sec = statbuf.st_mtime;
1773 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
1774 times[1].tv_usec = statbuf.st_mtimensec / 1000;
1775 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
1776 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
1781 if (mtime_usec_value)
1783 if (!get_uint32 (mtime_usec_value, &val_usec, error))
1785 times[1].tv_usec = val_usec;
1788 res = utimes(filename, times);
1791 g_set_error (error, G_IO_ERROR,
1792 g_io_error_from_errno (errno),
1793 _("Error setting owner: %s"),
1794 g_strerror (errno));
1802 _g_local_file_info_set_attribute (char *filename,
1803 const char *attribute,
1804 const GFileAttributeValue *value,
1805 GFileQueryInfoFlags flags,
1806 GCancellable *cancellable,
1809 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
1810 return set_unix_mode (filename, value, error);
1813 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
1814 return set_unix_uid_gid (filename, value, NULL, flags, error);
1815 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
1816 return set_unix_uid_gid (filename, NULL, value, flags, error);
1820 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET) == 0)
1821 return set_symlink (filename, value, error);
1825 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
1826 return set_mtime_atime (filename, value, NULL, NULL, NULL, error);
1827 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
1828 return set_mtime_atime (filename, NULL, value, NULL, NULL, error);
1829 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
1830 return set_mtime_atime (filename, NULL, NULL, value, NULL, error);
1831 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
1832 return set_mtime_atime (filename, NULL, NULL, NULL, value, error);
1836 else if (g_str_has_prefix (attribute, "xattr:"))
1837 return set_xattr (filename, attribute, value, error);
1838 else if (g_str_has_prefix (attribute, "xattr_sys:"))
1839 return set_xattr (filename, attribute, value, error);
1842 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1843 _("Setting attribute %s not supported"), attribute);
1848 _g_local_file_info_set_attributes (char *filename,
1850 GFileQueryInfoFlags flags,
1851 GCancellable *cancellable,
1854 GFileAttributeValue *value, *uid, *gid;
1855 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
1856 GFileAttributeStatus status;
1859 /* Handles setting multiple specified data in a single set, and takes care
1860 of ordering restrictions when setting attributes */
1864 /* Set symlink first, since this recreates the file */
1866 value = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET);
1869 if (!set_symlink (filename, value, error))
1871 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1873 /* Don't set error multiple times */
1877 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
1883 /* Group uid and gid setting into one call
1884 * Change ownership before permissions, since ownership changes can
1885 change permissions (e.g. setuid)
1887 uid = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_UID);
1888 gid = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_GID);
1892 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
1894 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1896 /* Don't set error multiple times */
1900 status = G_FILE_ATTRIBUTE_STATUS_SET;
1902 uid->status = status;
1904 gid->status = status;
1908 value = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_MODE);
1911 if (!set_unix_mode (filename, value, error))
1913 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1915 /* Don't set error multiple times */
1919 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
1924 /* Group all time settings into one call
1925 * Change times as the last thing to avoid it changing due to metadata changes
1928 mtime = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
1929 mtime_usec = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1930 atime = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
1931 atime_usec = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
1933 if (mtime || mtime_usec || atime || atime_usec)
1935 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
1937 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1939 /* Don't set error multiple times */
1943 status = G_FILE_ATTRIBUTE_STATUS_SET;
1946 mtime->status = status;
1948 mtime_usec->status = status;
1950 atime->status = status;
1952 atime_usec->status = status;
1956 /* xattrs are handled by default callback */