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>
57 #include "glocalfileinfo.h"
59 #include "gthemedicon.h"
60 #include "gcontenttype.h"
61 #include "gcontenttypeprivate.h"
65 struct ThumbMD5Context {
76 G_LOCK_DEFINE_STATIC (uid_cache);
77 static GHashTable *uid_cache = NULL;
79 G_LOCK_DEFINE_STATIC (gid_cache);
80 static GHashTable *gid_cache = NULL;
82 static void thumb_md5 (const char *string, unsigned char digest[16]);
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 thumb_digest_to_ascii (unsigned char digest[16],
1196 static const char hex_digits[] = "0123456789abcdef";
1200 res = g_malloc (33 + strlen (suffix));
1202 for (i = 0; i < 16; i++)
1204 res[2*i] = hex_digits[digest[i] >> 4];
1205 res[2*i+1] = hex_digits[digest[i] & 0xf];
1210 strcat (res, suffix);
1217 get_thumbnail_attributes (const char *path,
1221 unsigned char digest[16];
1225 uri = g_filename_to_uri (path, NULL, NULL);
1226 thumb_md5 (uri, digest);
1229 basename = thumb_digest_to_ascii (digest, ".png");
1231 filename = g_build_filename (g_get_home_dir(), ".thumbnails", "normal", basename, NULL);
1233 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1234 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1238 filename = g_build_filename (g_get_home_dir(), ".thumbnails", "fail", "gnome-thumbnail-factory", basename, NULL);
1240 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1241 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1249 _g_local_file_info_get (const char *basename,
1251 GFileAttributeMatcher *attribute_matcher,
1252 GFileQueryInfoFlags flags,
1253 GLocalParentFileInfo *parent_info,
1257 struct stat statbuf;
1258 struct stat statbuf2;
1260 gboolean is_symlink, symlink_broken;
1262 info = g_file_info_new ();
1264 /* Make sure we don't set any unwanted attributes */
1265 g_file_info_set_attribute_mask (info, attribute_matcher);
1267 g_file_info_set_name (info, basename);
1269 /* Avoid stat in trivial case */
1270 if (attribute_matcher == NULL)
1273 res = g_lstat (path, &statbuf);
1276 g_object_unref (info);
1277 g_set_error (error, G_IO_ERROR,
1278 g_io_error_from_errno (errno),
1279 _("Error stating file '%s': %s"),
1280 path, g_strerror (errno));
1285 is_symlink = S_ISLNK (statbuf.st_mode);
1289 symlink_broken = FALSE;
1293 g_file_info_set_is_symlink (info, TRUE);
1295 /* Unless NOFOLLOW was set we default to following symlinks */
1296 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1298 res = stat (path, &statbuf2);
1300 /* Report broken links as symlinks */
1304 symlink_broken = TRUE;
1308 set_info_from_stat (info, &statbuf, attribute_matcher);
1310 if (basename != NULL && basename[0] == '.')
1311 g_file_info_set_is_hidden (info, TRUE);
1313 if (basename != NULL && basename[strlen (basename) -1] == '~')
1314 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STD_IS_BACKUP, TRUE);
1317 g_file_attribute_matcher_matches (attribute_matcher,
1318 G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET))
1320 char *link = read_link (path);
1321 g_file_info_set_symlink_target (info, link);
1325 if (g_file_attribute_matcher_matches (attribute_matcher,
1326 G_FILE_ATTRIBUTE_STD_DISPLAY_NAME))
1328 char *display_name = g_filename_display_basename (path);
1330 if (strstr (display_name, "\357\277\275") != NULL)
1332 char *p = display_name;
1333 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1336 g_file_info_set_display_name (info, display_name);
1337 g_free (display_name);
1340 if (g_file_attribute_matcher_matches (attribute_matcher,
1341 G_FILE_ATTRIBUTE_STD_EDIT_NAME))
1343 char *edit_name = g_filename_display_basename (path);
1344 g_file_info_set_edit_name (info, edit_name);
1349 if (g_file_attribute_matcher_matches (attribute_matcher,
1350 G_FILE_ATTRIBUTE_STD_COPY_NAME))
1352 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1354 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STD_COPY_NAME, copy_name);
1358 if (g_file_attribute_matcher_matches (attribute_matcher,
1359 G_FILE_ATTRIBUTE_STD_CONTENT_TYPE) ||
1360 g_file_attribute_matcher_matches (attribute_matcher,
1361 G_FILE_ATTRIBUTE_STD_ICON))
1363 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1367 g_file_info_set_content_type (info, content_type);
1369 if (g_file_attribute_matcher_matches (attribute_matcher,
1370 G_FILE_ATTRIBUTE_STD_ICON))
1372 char *mimetype_icon, *generic_mimetype_icon, *type_icon, *p;
1373 char *icon_names[3];
1377 mimetype_icon = g_strdup (content_type);
1379 while ((p = strchr(mimetype_icon, '/')) != NULL)
1382 p = strchr (content_type, '/');
1384 p = content_type + strlen (content_type);
1386 generic_mimetype_icon = g_malloc (p - content_type + strlen ("-x-generic") + 1);
1387 memcpy (generic_mimetype_icon, content_type, p - content_type);
1388 memcpy (generic_mimetype_icon + (p - content_type), "-x-generic", strlen ("-x-generic"));
1389 generic_mimetype_icon[(p - content_type) + strlen ("-x-generic")] = 0;
1391 /* TODO: Special case desktop dir? That could be expensive with xdg dirs... */
1392 if (strcmp (path, g_get_home_dir ()) == 0)
1393 type_icon = "user-home";
1394 else if (S_ISDIR (statbuf.st_mode))
1395 type_icon = "folder";
1396 else if (statbuf.st_mode & S_IXUSR)
1397 type_icon = "application-x-executable";
1399 type_icon = "text-x-generic";
1402 icon_names[i++] = mimetype_icon;
1403 icon_names[i++] = generic_mimetype_icon;
1404 if (strcmp (generic_mimetype_icon, type_icon) != 0 &&
1405 strcmp (mimetype_icon, type_icon) != 0)
1406 icon_names[i++] = type_icon;
1408 icon = g_themed_icon_new_from_names (icon_names, i);
1409 g_file_info_set_icon (info, icon);
1411 g_object_unref (icon);
1412 g_free (mimetype_icon);
1413 g_free (generic_mimetype_icon);
1416 g_free (content_type);
1420 if (g_file_attribute_matcher_matches (attribute_matcher,
1421 G_FILE_ATTRIBUTE_STD_FAST_CONTENT_TYPE))
1423 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1427 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STD_FAST_CONTENT_TYPE, content_type);
1428 g_free (content_type);
1432 if (g_file_attribute_matcher_matches (attribute_matcher,
1433 G_FILE_ATTRIBUTE_OWNER_USER))
1437 name = get_username_from_uid (statbuf.st_uid);
1439 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1443 if (g_file_attribute_matcher_matches (attribute_matcher,
1444 G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1448 name = get_realname_from_uid (statbuf.st_uid);
1450 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1454 if (g_file_attribute_matcher_matches (attribute_matcher,
1455 G_FILE_ATTRIBUTE_OWNER_GROUP))
1459 name = get_groupname_from_gid (statbuf.st_gid);
1461 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1465 if (parent_info && parent_info->device != 0 &&
1466 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1467 statbuf.st_dev != parent_info->device)
1468 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1470 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1472 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1473 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1474 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1476 if (g_file_attribute_matcher_matches (attribute_matcher,
1477 G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1478 get_thumbnail_attributes (path, info);
1480 g_file_info_unset_attribute_mask (info);
1486 _g_local_file_info_get_from_fd (int fd,
1490 struct stat stat_buf;
1491 GFileAttributeMatcher *matcher;
1494 if (fstat (fd, &stat_buf) == -1)
1496 g_set_error (error, G_IO_ERROR,
1497 g_io_error_from_errno (errno),
1498 _("Error stating file descriptor: %s"),
1499 g_strerror (errno));
1503 info = g_file_info_new ();
1505 matcher = g_file_attribute_matcher_new (attributes);
1507 /* Make sure we don't set any unwanted attributes */
1508 g_file_info_set_attribute_mask (info, matcher);
1510 set_info_from_stat (info, &stat_buf, matcher);
1513 if (g_file_attribute_matcher_matches (matcher, "selinux:context") &&
1514 is_selinux_enabled ())
1517 if (fgetfilecon_raw (fd, &context) >= 0)
1519 g_file_info_set_attribute_string (info, "selinux:context", context);
1525 get_xattrs_from_fd (fd, TRUE, info, matcher);
1526 get_xattrs_from_fd (fd, FALSE, info, matcher);
1528 g_file_attribute_matcher_unref (matcher);
1530 g_file_info_unset_attribute_mask (info);
1536 get_uint32 (const GFileAttributeValue *value,
1540 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1542 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1543 _("Invalid attribute type (uint32 expected)"));
1547 *val_out = value->u.uint32;
1553 get_uint64 (const GFileAttributeValue *value,
1557 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1559 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1560 _("Invalid attribute type (uint64 expected)"));
1564 *val_out = value->u.uint64;
1569 #if defined(HAVE_SYMLINK)
1571 get_byte_string (const GFileAttributeValue *value,
1572 const char **val_out,
1575 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1577 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1578 _("Invalid attribute type (byte string expected)"));
1582 *val_out = value->u.string;
1589 set_unix_mode (char *filename,
1590 const GFileAttributeValue *value,
1595 if (!get_uint32 (value, &val, error))
1598 if (g_chmod (filename, val) == -1)
1600 g_set_error (error, G_IO_ERROR,
1601 g_io_error_from_errno (errno),
1602 _("Error setting permissions: %s"),
1603 g_strerror (errno));
1611 set_unix_uid_gid (char *filename,
1612 const GFileAttributeValue *uid_value,
1613 const GFileAttributeValue *gid_value,
1614 GFileQueryInfoFlags flags,
1624 if (!get_uint32 (uid_value, &val, error))
1633 if (!get_uint32 (gid_value, &val, error))
1640 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1641 res = lchown (filename, uid, gid);
1643 res = chown (filename, uid, gid);
1647 g_set_error (error, G_IO_ERROR,
1648 g_io_error_from_errno (errno),
1649 _("Error setting owner: %s"),
1650 g_strerror (errno));
1659 set_symlink (char *filename,
1660 const GFileAttributeValue *value,
1664 struct stat statbuf;
1666 if (!get_byte_string (value, &val, error))
1671 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1672 _("symlink must be non-NULL"));
1676 if (g_lstat (filename, &statbuf))
1678 g_set_error (error, G_IO_ERROR,
1679 g_io_error_from_errno (errno),
1680 _("Error setting symlink: %s"),
1681 g_strerror (errno));
1685 if (!S_ISLNK (statbuf.st_mode))
1687 g_set_error (error, G_IO_ERROR,
1688 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1689 _("Error setting symlink: file is not a symlink"));
1693 if (g_unlink (filename))
1695 g_set_error (error, G_IO_ERROR,
1696 g_io_error_from_errno (errno),
1697 _("Error setting symlink: %s"),
1698 g_strerror (errno));
1702 if (symlink (filename, val) != 0)
1704 g_set_error (error, G_IO_ERROR,
1705 g_io_error_from_errno (errno),
1706 _("Error setting symlink: %s"),
1707 g_strerror (errno));
1716 lazy_stat (char *filename,
1717 struct stat *statbuf,
1718 gboolean *called_stat)
1725 res = g_stat (filename, statbuf);
1728 *called_stat = TRUE;
1736 set_mtime_atime (char *filename,
1737 const GFileAttributeValue *mtime_value,
1738 const GFileAttributeValue *mtime_usec_value,
1739 const GFileAttributeValue *atime_value,
1740 const GFileAttributeValue *atime_usec_value,
1746 struct stat statbuf;
1747 gboolean got_stat = FALSE;
1748 struct timeval times[2] = { {0, 0}, {0, 0} };
1753 if (!get_uint64 (atime_value, &val, error))
1755 times[0].tv_sec = val;
1759 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1761 times[0].tv_sec = statbuf.st_mtime;
1762 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1763 times[0].tv_usec = statbuf.st_atimensec / 1000;
1764 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1765 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
1770 if (atime_usec_value)
1772 if (!get_uint32 (atime_usec_value, &val_usec, error))
1774 times[0].tv_usec = val_usec;
1780 if (!get_uint64 (mtime_value, &val, error))
1782 times[1].tv_sec = val;
1786 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1788 times[1].tv_sec = statbuf.st_mtime;
1789 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
1790 times[1].tv_usec = statbuf.st_mtimensec / 1000;
1791 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
1792 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
1797 if (mtime_usec_value)
1799 if (!get_uint32 (mtime_usec_value, &val_usec, error))
1801 times[1].tv_usec = val_usec;
1804 res = utimes(filename, times);
1807 g_set_error (error, G_IO_ERROR,
1808 g_io_error_from_errno (errno),
1809 _("Error setting owner: %s"),
1810 g_strerror (errno));
1818 _g_local_file_info_set_attribute (char *filename,
1819 const char *attribute,
1820 const GFileAttributeValue *value,
1821 GFileQueryInfoFlags flags,
1822 GCancellable *cancellable,
1825 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
1826 return set_unix_mode (filename, value, error);
1829 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
1830 return set_unix_uid_gid (filename, value, NULL, flags, error);
1831 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
1832 return set_unix_uid_gid (filename, NULL, value, flags, error);
1836 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET) == 0)
1837 return set_symlink (filename, value, error);
1841 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
1842 return set_mtime_atime (filename, value, NULL, NULL, NULL, error);
1843 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
1844 return set_mtime_atime (filename, NULL, value, NULL, NULL, error);
1845 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
1846 return set_mtime_atime (filename, NULL, NULL, value, NULL, error);
1847 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
1848 return set_mtime_atime (filename, NULL, NULL, NULL, value, error);
1852 else if (g_str_has_prefix (attribute, "xattr:"))
1853 return set_xattr (filename, attribute, value, error);
1854 else if (g_str_has_prefix (attribute, "xattr_sys:"))
1855 return set_xattr (filename, attribute, value, error);
1858 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1859 _("Setting attribute %s not supported"), attribute);
1864 _g_local_file_info_set_attributes (char *filename,
1866 GFileQueryInfoFlags flags,
1867 GCancellable *cancellable,
1870 GFileAttributeValue *value, *uid, *gid;
1871 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
1872 GFileAttributeStatus status;
1875 /* Handles setting multiple specified data in a single set, and takes care
1876 of ordering restrictions when setting attributes */
1880 /* Set symlink first, since this recreates the file */
1882 value = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET);
1885 if (!set_symlink (filename, value, error))
1887 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1889 /* Don't set error multiple times */
1893 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
1899 /* Group uid and gid setting into one call
1900 * Change ownership before permissions, since ownership changes can
1901 change permissions (e.g. setuid)
1903 uid = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_UID);
1904 gid = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_GID);
1908 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
1910 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1912 /* Don't set error multiple times */
1916 status = G_FILE_ATTRIBUTE_STATUS_SET;
1918 uid->status = status;
1920 gid->status = status;
1924 value = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_MODE);
1927 if (!set_unix_mode (filename, value, error))
1929 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1931 /* Don't set error multiple times */
1935 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
1940 /* Group all time settings into one call
1941 * Change times as the last thing to avoid it changing due to metadata changes
1944 mtime = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
1945 mtime_usec = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1946 atime = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
1947 atime_usec = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
1949 if (mtime || mtime_usec || atime || atime_usec)
1951 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
1953 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1955 /* Don't set error multiple times */
1959 status = G_FILE_ATTRIBUTE_STATUS_SET;
1962 mtime->status = status;
1964 mtime_usec->status = status;
1966 atime->status = status;
1968 atime_usec->status = status;
1972 /* xattrs are handled by default callback */
1979 * This code implements the MD5 message-digest algorithm.
1980 * The algorithm is due to Ron Rivest. This code was
1981 * written by Colin Plumb in 1993, no copyright is claimed.
1982 * This code is in the public domain; do with it what you wish.
1984 * Equivalent code is available from RSA Data Security, Inc.
1985 * This code has been tested against that, and is equivalent,
1986 * except that you don't need to include two pages of legalese
1989 * To compute the message digest of a chunk of bytes, declare an
1990 * ThumbMD5Context structure, pass it to thumb_md5_init, call
1991 * thumb_md5_update as needed on buffers full of bytes, and then call
1992 * thumb_md5_final, which will fill a supplied 32-byte array with the
1993 * digest in ascii form.
1997 static void thumb_md5_init (struct ThumbMD5Context *context);
1998 static void thumb_md5_update (struct ThumbMD5Context *context,
1999 unsigned char const *buf,
2001 static void thumb_md5_final (unsigned char digest[16],
2002 struct ThumbMD5Context *context);
2003 static void thumb_md5_transform (guint32 buf[4],
2004 guint32 const in[16]);
2008 thumb_md5 (const char *string, unsigned char digest[16])
2010 struct ThumbMD5Context md5_context;
2012 thumb_md5_init (&md5_context);
2013 thumb_md5_update (&md5_context, (unsigned char *)string, strlen (string));
2014 thumb_md5_final (digest, &md5_context);
2017 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
2018 #define byteReverse(buf, len) /* Nothing */
2022 * Note: this code is harmless on little-endian machines.
2025 byteReverse (unsigned char *buf,
2030 t = (guint32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
2031 ((unsigned) buf[1] << 8 | buf[0]);
2032 *(guint32 *) buf = t;
2040 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
2041 * initialization constants.
2044 thumb_md5_init (struct ThumbMD5Context *ctx)
2046 ctx->buf[0] = 0x67452301;
2047 ctx->buf[1] = 0xefcdab89;
2048 ctx->buf[2] = 0x98badcfe;
2049 ctx->buf[3] = 0x10325476;
2056 * Update context to reflect the concatenation of another buffer full
2060 thumb_md5_update (struct ThumbMD5Context *ctx,
2061 unsigned char const *buf,
2066 /* Update bitcount */
2069 if ((ctx->bits[0] = t + ((guint32) len << 3)) < t)
2070 ctx->bits[1]++; /* Carry from low to high */
2071 ctx->bits[1] += len >> 29;
2073 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
2075 /* Handle any leading odd-sized chunks */
2078 unsigned char *p = (unsigned char *) ctx->in + t;
2082 memcpy (p, buf, len);
2086 byteReverse (ctx->in, 16);
2087 thumb_md5_transform (ctx->buf, (guint32 *) ctx->in);
2092 /* Process data in 64-byte chunks */
2095 memcpy (ctx->in, buf, 64);
2096 byteReverse (ctx->in, 16);
2097 thumb_md5_transform (ctx->buf, (guint32 *) ctx->in);
2102 /* Handle any remaining bytes of data. */
2104 memcpy(ctx->in, buf, len);
2108 * Final wrapup - pad to 64-byte boundary with the bit pattern
2109 * 1 0* (64-bit count of bits processed, MSB-first)
2112 thumb_md5_final (unsigned char digest[16],
2113 struct ThumbMD5Context *ctx)
2118 /* Compute number of bytes mod 64 */
2119 count = (ctx->bits[0] >> 3) & 0x3F;
2121 /* Set the first char of padding to 0x80. This is safe since there is
2122 always at least one byte free */
2123 p = ctx->in + count;
2126 /* Bytes of padding needed to make 64 bytes */
2127 count = 64 - 1 - count;
2129 /* Pad out to 56 mod 64 */
2131 /* Two lots of padding: Pad the first block to 64 bytes */
2132 memset (p, 0, count);
2133 byteReverse (ctx->in, 16);
2134 thumb_md5_transform (ctx->buf, (guint32 *) ctx->in);
2136 /* Now fill the next block with 56 bytes */
2137 memset(ctx->in, 0, 56);
2139 /* Pad block to 56 bytes */
2140 memset(p, 0, count - 8);
2142 byteReverse(ctx->in, 14);
2144 /* Append length in bits and transform */
2145 ((guint32 *) ctx->in)[14] = ctx->bits[0];
2146 ((guint32 *) ctx->in)[15] = ctx->bits[1];
2148 thumb_md5_transform (ctx->buf, (guint32 *) ctx->in);
2149 byteReverse ((unsigned char *) ctx->buf, 4);
2150 memcpy (digest, ctx->buf, 16);
2151 memset (ctx, 0, sizeof(ctx)); /* In case it's sensitive */
2155 /* The four core functions - F1 is optimized somewhat */
2157 #define F1(x, y, z) (z ^ (x & (y ^ z)))
2158 #define F2(x, y, z) F1 (z, x, y)
2159 #define F3(x, y, z) (x ^ y ^ z)
2160 #define F4(x, y, z) (y ^ (x | ~z))
2162 /* This is the central step in the MD5 algorithm. */
2163 #define thumb_md5_step(f, w, x, y, z, data, s) \
2164 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
2167 * The core of the MD5 algorithm, this alters an existing MD5 hash to
2168 * reflect the addition of 16 longwords of new data. ThumbMD5Update blocks
2169 * the data and converts bytes into longwords for this routine.
2172 thumb_md5_transform (guint32 buf[4],
2173 guint32 const in[16])
2175 register guint32 a, b, c, d;
2182 thumb_md5_step(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
2183 thumb_md5_step(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
2184 thumb_md5_step(F1, c, d, a, b, in[2] + 0x242070db, 17);
2185 thumb_md5_step(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
2186 thumb_md5_step(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
2187 thumb_md5_step(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
2188 thumb_md5_step(F1, c, d, a, b, in[6] + 0xa8304613, 17);
2189 thumb_md5_step(F1, b, c, d, a, in[7] + 0xfd469501, 22);
2190 thumb_md5_step(F1, a, b, c, d, in[8] + 0x698098d8, 7);
2191 thumb_md5_step(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
2192 thumb_md5_step(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
2193 thumb_md5_step(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
2194 thumb_md5_step(F1, a, b, c, d, in[12] + 0x6b901122, 7);
2195 thumb_md5_step(F1, d, a, b, c, in[13] + 0xfd987193, 12);
2196 thumb_md5_step(F1, c, d, a, b, in[14] + 0xa679438e, 17);
2197 thumb_md5_step(F1, b, c, d, a, in[15] + 0x49b40821, 22);
2199 thumb_md5_step(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
2200 thumb_md5_step(F2, d, a, b, c, in[6] + 0xc040b340, 9);
2201 thumb_md5_step(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
2202 thumb_md5_step(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
2203 thumb_md5_step(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
2204 thumb_md5_step(F2, d, a, b, c, in[10] + 0x02441453, 9);
2205 thumb_md5_step(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
2206 thumb_md5_step(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
2207 thumb_md5_step(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
2208 thumb_md5_step(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
2209 thumb_md5_step(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
2210 thumb_md5_step(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
2211 thumb_md5_step(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
2212 thumb_md5_step(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
2213 thumb_md5_step(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
2214 thumb_md5_step(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
2216 thumb_md5_step(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
2217 thumb_md5_step(F3, d, a, b, c, in[8] + 0x8771f681, 11);
2218 thumb_md5_step(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
2219 thumb_md5_step(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
2220 thumb_md5_step(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
2221 thumb_md5_step(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
2222 thumb_md5_step(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
2223 thumb_md5_step(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
2224 thumb_md5_step(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
2225 thumb_md5_step(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
2226 thumb_md5_step(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
2227 thumb_md5_step(F3, b, c, d, a, in[6] + 0x04881d05, 23);
2228 thumb_md5_step(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
2229 thumb_md5_step(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
2230 thumb_md5_step(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
2231 thumb_md5_step(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
2233 thumb_md5_step(F4, a, b, c, d, in[0] + 0xf4292244, 6);
2234 thumb_md5_step(F4, d, a, b, c, in[7] + 0x432aff97, 10);
2235 thumb_md5_step(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
2236 thumb_md5_step(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
2237 thumb_md5_step(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
2238 thumb_md5_step(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
2239 thumb_md5_step(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
2240 thumb_md5_step(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
2241 thumb_md5_step(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
2242 thumb_md5_step(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
2243 thumb_md5_step(F4, c, d, a, b, in[6] + 0xa3014314, 15);
2244 thumb_md5_step(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
2245 thumb_md5_step(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
2246 thumb_md5_step(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
2247 thumb_md5_step(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
2248 thumb_md5_step(F4, b, c, d, a, in[9] + 0xeb86d391, 21);