1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "archive_platform.h"
27__FBSDID("$FreeBSD: head/lib/libarchive/archive_entry_xattr.c 201096 2009-12-28 02:41:27Z kientzle $");
28
29#ifdef HAVE_SYS_STAT_H
30#include <sys/stat.h>
31#endif
32#ifdef HAVE_SYS_TYPES_H
33#include <sys/types.h>
34#endif
35#ifdef HAVE_LIMITS_H
36#include <limits.h>
37#endif
38#ifdef HAVE_LINUX_FS_H
39#include <linux/fs.h>	/* for Linux file flags */
40#endif
41/*
42 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
43 * As the include guards don't agree, the order of include is important.
44 */
45#ifdef HAVE_LINUX_EXT2_FS_H
46#include <linux/ext2_fs.h>	/* for Linux file flags */
47#endif
48#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
49#include <ext2fs/ext2_fs.h>	/* for Linux file flags */
50#endif
51#include <stddef.h>
52#include <stdio.h>
53#ifdef HAVE_STDLIB_H
54#include <stdlib.h>
55#endif
56#ifdef HAVE_STRING_H
57#include <string.h>
58#endif
59#ifdef HAVE_WCHAR_H
60#include <wchar.h>
61#endif
62
63#include "archive.h"
64#include "archive_entry.h"
65#include "archive_private.h"
66#include "archive_entry_private.h"
67
68/*
69 * extended attribute handling
70 */
71
72void
73archive_entry_xattr_clear(struct archive_entry *entry)
74{
75	struct ae_xattr	*xp;
76
77	while (entry->xattr_head != NULL) {
78		xp = entry->xattr_head->next;
79		free(entry->xattr_head->name);
80		free(entry->xattr_head->value);
81		free(entry->xattr_head);
82		entry->xattr_head = xp;
83	}
84
85	entry->xattr_head = NULL;
86}
87
88void
89archive_entry_xattr_add_entry(struct archive_entry *entry,
90	const char *name, const void *value, size_t size)
91{
92	struct ae_xattr	*xp;
93
94	for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
95		;
96
97	if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
98		/* XXX Error XXX */
99		return;
100
101	xp->name = strdup(name);
102	if ((xp->value = malloc(size)) != NULL) {
103		memcpy(xp->value, value, size);
104		xp->size = size;
105	} else
106		xp->size = 0;
107
108	xp->next = entry->xattr_head;
109	entry->xattr_head = xp;
110}
111
112
113/*
114 * returns number of the extended attribute entries
115 */
116int
117archive_entry_xattr_count(struct archive_entry *entry)
118{
119	struct ae_xattr *xp;
120	int count = 0;
121
122	for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
123		count++;
124
125	return count;
126}
127
128int
129archive_entry_xattr_reset(struct archive_entry * entry)
130{
131	entry->xattr_p = entry->xattr_head;
132
133	return archive_entry_xattr_count(entry);
134}
135
136int
137archive_entry_xattr_next(struct archive_entry * entry,
138	const char **name, const void **value, size_t *size)
139{
140	if (entry->xattr_p) {
141		*name = entry->xattr_p->name;
142		*value = entry->xattr_p->value;
143		*size = entry->xattr_p->size;
144
145		entry->xattr_p = entry->xattr_p->next;
146
147		return (ARCHIVE_OK);
148	} else {
149		*name = NULL;
150		*value = NULL;
151		*size = (size_t)0;
152		return (ARCHIVE_WARN);
153	}
154}
155
156/*
157 * end of xattr handling
158 */
159