1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm
26228753Smm#include "archive_platform.h"
27228763Smm__FBSDID("$FreeBSD$");
28228753Smm
29228753Smm#ifdef HAVE_SYS_STAT_H
30228753Smm#include <sys/stat.h>
31228753Smm#endif
32228753Smm#ifdef HAVE_SYS_TYPES_H
33228753Smm#include <sys/types.h>
34228753Smm#endif
35228753Smm#ifdef HAVE_LIMITS_H
36228753Smm#include <limits.h>
37228753Smm#endif
38228753Smm#ifdef HAVE_LINUX_FS_H
39228753Smm#include <linux/fs.h>	/* for Linux file flags */
40228753Smm#endif
41228753Smm/*
42228753Smm * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
43228753Smm * As the include guards don't agree, the order of include is important.
44228753Smm */
45228753Smm#ifdef HAVE_LINUX_EXT2_FS_H
46228753Smm#include <linux/ext2_fs.h>	/* for Linux file flags */
47228753Smm#endif
48228753Smm#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
49228753Smm#include <ext2fs/ext2_fs.h>	/* for Linux file flags */
50228753Smm#endif
51228753Smm#include <stddef.h>
52228753Smm#include <stdio.h>
53228753Smm#ifdef HAVE_STDLIB_H
54228753Smm#include <stdlib.h>
55228753Smm#endif
56228753Smm#ifdef HAVE_STRING_H
57228753Smm#include <string.h>
58228753Smm#endif
59228753Smm#ifdef HAVE_WCHAR_H
60228753Smm#include <wchar.h>
61228753Smm#endif
62228753Smm
63228753Smm#include "archive.h"
64228753Smm#include "archive_entry.h"
65228753Smm#include "archive_private.h"
66228753Smm#include "archive_entry_private.h"
67228753Smm
68228753Smm/*
69228753Smm * extended attribute handling
70228753Smm */
71228753Smm
72228753Smmvoid
73228753Smmarchive_entry_xattr_clear(struct archive_entry *entry)
74228753Smm{
75228753Smm	struct ae_xattr	*xp;
76228753Smm
77228753Smm	while (entry->xattr_head != NULL) {
78228753Smm		xp = entry->xattr_head->next;
79228753Smm		free(entry->xattr_head->name);
80228753Smm		free(entry->xattr_head->value);
81228753Smm		free(entry->xattr_head);
82228753Smm		entry->xattr_head = xp;
83228753Smm	}
84228753Smm
85228753Smm	entry->xattr_head = NULL;
86228753Smm}
87228753Smm
88228753Smmvoid
89228753Smmarchive_entry_xattr_add_entry(struct archive_entry *entry,
90228753Smm	const char *name, const void *value, size_t size)
91228753Smm{
92228753Smm	struct ae_xattr	*xp;
93228753Smm
94228753Smm	if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
95302295Smm		__archive_errx(1, "Out of memory");
96228753Smm
97302001Smm	if ((xp->name = strdup(name)) == NULL)
98302295Smm		__archive_errx(1, "Out of memory");
99302001Smm
100228753Smm	if ((xp->value = malloc(size)) != NULL) {
101228753Smm		memcpy(xp->value, value, size);
102228753Smm		xp->size = size;
103228753Smm	} else
104228753Smm		xp->size = 0;
105228753Smm
106228753Smm	xp->next = entry->xattr_head;
107228753Smm	entry->xattr_head = xp;
108228753Smm}
109228753Smm
110228753Smm
111228753Smm/*
112228753Smm * returns number of the extended attribute entries
113228753Smm */
114228753Smmint
115228753Smmarchive_entry_xattr_count(struct archive_entry *entry)
116228753Smm{
117228753Smm	struct ae_xattr *xp;
118228753Smm	int count = 0;
119228753Smm
120228753Smm	for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
121228753Smm		count++;
122228753Smm
123228753Smm	return count;
124228753Smm}
125228753Smm
126228753Smmint
127228753Smmarchive_entry_xattr_reset(struct archive_entry * entry)
128228753Smm{
129228753Smm	entry->xattr_p = entry->xattr_head;
130228753Smm
131228753Smm	return archive_entry_xattr_count(entry);
132228753Smm}
133228753Smm
134228753Smmint
135228753Smmarchive_entry_xattr_next(struct archive_entry * entry,
136228753Smm	const char **name, const void **value, size_t *size)
137228753Smm{
138228753Smm	if (entry->xattr_p) {
139228753Smm		*name = entry->xattr_p->name;
140228753Smm		*value = entry->xattr_p->value;
141228753Smm		*size = entry->xattr_p->size;
142228753Smm
143228753Smm		entry->xattr_p = entry->xattr_p->next;
144228753Smm
145228753Smm		return (ARCHIVE_OK);
146228753Smm	} else {
147228753Smm		*name = NULL;
148228753Smm		*value = NULL;
149228753Smm		*size = (size_t)0;
150228753Smm		return (ARCHIVE_WARN);
151228753Smm	}
152228753Smm}
153228753Smm
154228753Smm/*
155228753Smm * end of xattr handling
156228753Smm */
157