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 
28 #ifdef HAVE_SYS_STAT_H
29 #include <sys/stat.h>
30 #endif
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
33 #endif
34 #ifdef HAVE_LIMITS_H
35 #include <limits.h>
36 #endif
37 #ifdef HAVE_LINUX_FS_H
38 #include <linux/fs.h>	/* for Linux file flags */
39 #endif
40 /*
41  * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
42  * As the include guards don't agree, the order of include is important.
43  */
44 #ifdef HAVE_LINUX_EXT2_FS_H
45 #include <linux/ext2_fs.h>	/* for Linux file flags */
46 #endif
47 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
48 #include <ext2fs/ext2_fs.h>	/* for Linux file flags */
49 #endif
50 #include <stddef.h>
51 #include <stdio.h>
52 #ifdef HAVE_STDLIB_H
53 #include <stdlib.h>
54 #endif
55 #ifdef HAVE_STRING_H
56 #include <string.h>
57 #endif
58 #ifdef HAVE_WCHAR_H
59 #include <wchar.h>
60 #endif
61 
62 #include "archive.h"
63 #include "archive_entry.h"
64 #include "archive_private.h"
65 #include "archive_entry_private.h"
66 
67 /*
68  * extended attribute handling
69  */
70 
71 void
archive_entry_xattr_clear(struct archive_entry * entry)72 archive_entry_xattr_clear(struct archive_entry *entry)
73 {
74 	struct ae_xattr	*xp;
75 
76 	while (entry->xattr_head != NULL) {
77 		xp = entry->xattr_head->next;
78 		free(entry->xattr_head->name);
79 		free(entry->xattr_head->value);
80 		free(entry->xattr_head);
81 		entry->xattr_head = xp;
82 	}
83 
84 	entry->xattr_head = NULL;
85 }
86 
87 void
archive_entry_xattr_add_entry(struct archive_entry * entry,const char * name,const void * value,size_t size)88 archive_entry_xattr_add_entry(struct archive_entry *entry,
89 	const char *name, const void *value, size_t size)
90 {
91 	struct ae_xattr	*xp;
92 
93 	if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
94 		__archive_errx(1, "Out of memory");
95 
96 	if ((xp->name = strdup(name)) == NULL)
97 		__archive_errx(1, "Out of memory");
98 
99 	if ((xp->value = malloc(size)) != NULL) {
100 		memcpy(xp->value, value, size);
101 		xp->size = size;
102 	} else
103 		xp->size = 0;
104 
105 	xp->next = entry->xattr_head;
106 	entry->xattr_head = xp;
107 }
108 
109 
110 /*
111  * returns number of the extended attribute entries
112  */
113 int
archive_entry_xattr_count(struct archive_entry * entry)114 archive_entry_xattr_count(struct archive_entry *entry)
115 {
116 	struct ae_xattr *xp;
117 	int count = 0;
118 
119 	for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
120 		count++;
121 
122 	return count;
123 }
124 
125 int
archive_entry_xattr_reset(struct archive_entry * entry)126 archive_entry_xattr_reset(struct archive_entry * entry)
127 {
128 	entry->xattr_p = entry->xattr_head;
129 
130 	return archive_entry_xattr_count(entry);
131 }
132 
133 int
archive_entry_xattr_next(struct archive_entry * entry,const char ** name,const void ** value,size_t * size)134 archive_entry_xattr_next(struct archive_entry * entry,
135 	const char **name, const void **value, size_t *size)
136 {
137 	if (entry->xattr_p) {
138 		*name = entry->xattr_p->name;
139 		*value = entry->xattr_p->value;
140 		*size = entry->xattr_p->size;
141 
142 		entry->xattr_p = entry->xattr_p->next;
143 
144 		return (ARCHIVE_OK);
145 	} else {
146 		*name = NULL;
147 		*value = NULL;
148 		*size = (size_t)0;
149 		return (ARCHIVE_WARN);
150 	}
151 }
152 
153 /*
154  * end of xattr handling
155  */
156