19c82a63eSPeter Avalos /*-
29c82a63eSPeter Avalos  * Copyright (c) 2003-2007 Tim Kientzle
39c82a63eSPeter Avalos  * All rights reserved.
49c82a63eSPeter Avalos  *
59c82a63eSPeter Avalos  * Redistribution and use in source and binary forms, with or without
69c82a63eSPeter Avalos  * modification, are permitted provided that the following conditions
79c82a63eSPeter Avalos  * are met:
89c82a63eSPeter Avalos  * 1. Redistributions of source code must retain the above copyright
99c82a63eSPeter Avalos  *    notice, this list of conditions and the following disclaimer.
109c82a63eSPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
119c82a63eSPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
129c82a63eSPeter Avalos  *    documentation and/or other materials provided with the distribution.
139c82a63eSPeter Avalos  *
149c82a63eSPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
159c82a63eSPeter Avalos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
169c82a63eSPeter Avalos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
179c82a63eSPeter Avalos  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
189c82a63eSPeter Avalos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
199c82a63eSPeter Avalos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
209c82a63eSPeter Avalos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
219c82a63eSPeter Avalos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
229c82a63eSPeter Avalos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
239c82a63eSPeter Avalos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
249c82a63eSPeter Avalos  */
259c82a63eSPeter Avalos 
269c82a63eSPeter Avalos #include "archive_platform.h"
279c82a63eSPeter Avalos __FBSDID("$FreeBSD: head/lib/libarchive/archive_entry_xattr.c 201096 2009-12-28 02:41:27Z kientzle $");
289c82a63eSPeter Avalos 
299c82a63eSPeter Avalos #ifdef HAVE_SYS_STAT_H
309c82a63eSPeter Avalos #include <sys/stat.h>
319c82a63eSPeter Avalos #endif
329c82a63eSPeter Avalos #ifdef HAVE_SYS_TYPES_H
339c82a63eSPeter Avalos #include <sys/types.h>
349c82a63eSPeter Avalos #endif
359c82a63eSPeter Avalos #ifdef HAVE_LIMITS_H
369c82a63eSPeter Avalos #include <limits.h>
379c82a63eSPeter Avalos #endif
389c82a63eSPeter Avalos #ifdef HAVE_LINUX_FS_H
399c82a63eSPeter Avalos #include <linux/fs.h>	/* for Linux file flags */
409c82a63eSPeter Avalos #endif
419c82a63eSPeter Avalos /*
429c82a63eSPeter Avalos  * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
439c82a63eSPeter Avalos  * As the include guards don't agree, the order of include is important.
449c82a63eSPeter Avalos  */
459c82a63eSPeter Avalos #ifdef HAVE_LINUX_EXT2_FS_H
469c82a63eSPeter Avalos #include <linux/ext2_fs.h>	/* for Linux file flags */
479c82a63eSPeter Avalos #endif
489c82a63eSPeter Avalos #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
499c82a63eSPeter Avalos #include <ext2fs/ext2_fs.h>	/* for Linux file flags */
509c82a63eSPeter Avalos #endif
519c82a63eSPeter Avalos #include <stddef.h>
529c82a63eSPeter Avalos #include <stdio.h>
539c82a63eSPeter Avalos #ifdef HAVE_STDLIB_H
549c82a63eSPeter Avalos #include <stdlib.h>
559c82a63eSPeter Avalos #endif
569c82a63eSPeter Avalos #ifdef HAVE_STRING_H
579c82a63eSPeter Avalos #include <string.h>
589c82a63eSPeter Avalos #endif
599c82a63eSPeter Avalos #ifdef HAVE_WCHAR_H
609c82a63eSPeter Avalos #include <wchar.h>
619c82a63eSPeter Avalos #endif
629c82a63eSPeter Avalos 
639c82a63eSPeter Avalos #include "archive.h"
649c82a63eSPeter Avalos #include "archive_entry.h"
659c82a63eSPeter Avalos #include "archive_private.h"
669c82a63eSPeter Avalos #include "archive_entry_private.h"
679c82a63eSPeter Avalos 
689c82a63eSPeter Avalos /*
699c82a63eSPeter Avalos  * extended attribute handling
709c82a63eSPeter Avalos  */
719c82a63eSPeter Avalos 
729c82a63eSPeter Avalos void
archive_entry_xattr_clear(struct archive_entry * entry)739c82a63eSPeter Avalos archive_entry_xattr_clear(struct archive_entry *entry)
749c82a63eSPeter Avalos {
759c82a63eSPeter Avalos 	struct ae_xattr	*xp;
769c82a63eSPeter Avalos 
779c82a63eSPeter Avalos 	while (entry->xattr_head != NULL) {
789c82a63eSPeter Avalos 		xp = entry->xattr_head->next;
799c82a63eSPeter Avalos 		free(entry->xattr_head->name);
809c82a63eSPeter Avalos 		free(entry->xattr_head->value);
819c82a63eSPeter Avalos 		free(entry->xattr_head);
829c82a63eSPeter Avalos 		entry->xattr_head = xp;
839c82a63eSPeter Avalos 	}
849c82a63eSPeter Avalos 
859c82a63eSPeter Avalos 	entry->xattr_head = NULL;
869c82a63eSPeter Avalos }
879c82a63eSPeter Avalos 
889c82a63eSPeter Avalos void
archive_entry_xattr_add_entry(struct archive_entry * entry,const char * name,const void * value,size_t size)899c82a63eSPeter Avalos archive_entry_xattr_add_entry(struct archive_entry *entry,
909c82a63eSPeter Avalos 	const char *name, const void *value, size_t size)
919c82a63eSPeter Avalos {
929c82a63eSPeter Avalos 	struct ae_xattr	*xp;
939c82a63eSPeter Avalos 
949c82a63eSPeter Avalos 	if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
95e95abc47Szrj 		__archive_errx(1, "Out of memory");
969c82a63eSPeter Avalos 
976b384f39SPeter Avalos 	if ((xp->name = strdup(name)) == NULL)
98e95abc47Szrj 		__archive_errx(1, "Out of memory");
996b384f39SPeter Avalos 
1009c82a63eSPeter Avalos 	if ((xp->value = malloc(size)) != NULL) {
1019c82a63eSPeter Avalos 		memcpy(xp->value, value, size);
1029c82a63eSPeter Avalos 		xp->size = size;
1039c82a63eSPeter Avalos 	} else
1049c82a63eSPeter Avalos 		xp->size = 0;
1059c82a63eSPeter Avalos 
1069c82a63eSPeter Avalos 	xp->next = entry->xattr_head;
1079c82a63eSPeter Avalos 	entry->xattr_head = xp;
1089c82a63eSPeter Avalos }
1099c82a63eSPeter Avalos 
1109c82a63eSPeter Avalos 
1119c82a63eSPeter Avalos /*
1129c82a63eSPeter Avalos  * returns number of the extended attribute entries
1139c82a63eSPeter Avalos  */
1149c82a63eSPeter Avalos int
archive_entry_xattr_count(struct archive_entry * entry)1159c82a63eSPeter Avalos archive_entry_xattr_count(struct archive_entry *entry)
1169c82a63eSPeter Avalos {
1179c82a63eSPeter Avalos 	struct ae_xattr *xp;
1189c82a63eSPeter Avalos 	int count = 0;
1199c82a63eSPeter Avalos 
1209c82a63eSPeter Avalos 	for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1219c82a63eSPeter Avalos 		count++;
1229c82a63eSPeter Avalos 
1239c82a63eSPeter Avalos 	return count;
1249c82a63eSPeter Avalos }
1259c82a63eSPeter Avalos 
1269c82a63eSPeter Avalos int
archive_entry_xattr_reset(struct archive_entry * entry)1279c82a63eSPeter Avalos archive_entry_xattr_reset(struct archive_entry * entry)
1289c82a63eSPeter Avalos {
1299c82a63eSPeter Avalos 	entry->xattr_p = entry->xattr_head;
1309c82a63eSPeter Avalos 
1319c82a63eSPeter Avalos 	return archive_entry_xattr_count(entry);
1329c82a63eSPeter Avalos }
1339c82a63eSPeter Avalos 
1349c82a63eSPeter Avalos int
archive_entry_xattr_next(struct archive_entry * entry,const char ** name,const void ** value,size_t * size)1359c82a63eSPeter Avalos archive_entry_xattr_next(struct archive_entry * entry,
1369c82a63eSPeter Avalos 	const char **name, const void **value, size_t *size)
1379c82a63eSPeter Avalos {
1389c82a63eSPeter Avalos 	if (entry->xattr_p) {
1399c82a63eSPeter Avalos 		*name = entry->xattr_p->name;
1409c82a63eSPeter Avalos 		*value = entry->xattr_p->value;
1419c82a63eSPeter Avalos 		*size = entry->xattr_p->size;
1429c82a63eSPeter Avalos 
1439c82a63eSPeter Avalos 		entry->xattr_p = entry->xattr_p->next;
1449c82a63eSPeter Avalos 
1459c82a63eSPeter Avalos 		return (ARCHIVE_OK);
1469c82a63eSPeter Avalos 	} else {
1479c82a63eSPeter Avalos 		*name = NULL;
1489c82a63eSPeter Avalos 		*value = NULL;
1499c82a63eSPeter Avalos 		*size = (size_t)0;
1509c82a63eSPeter Avalos 		return (ARCHIVE_WARN);
1519c82a63eSPeter Avalos 	}
1529c82a63eSPeter Avalos }
1539c82a63eSPeter Avalos 
1549c82a63eSPeter Avalos /*
1559c82a63eSPeter Avalos  * end of xattr handling
1569c82a63eSPeter Avalos  */
157