]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/archive_entry_xattr.c
MFC r299529,r299540,r299576,r299896:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / archive_entry_xattr.c
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$");
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
72 void
73 archive_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
88 void
89 archive_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         if ((xp->name = strdup(name)) == NULL)
102                 /* XXX Error XXX */
103                 return;
104
105         if ((xp->value = malloc(size)) != NULL) {
106                 memcpy(xp->value, value, size);
107                 xp->size = size;
108         } else
109                 xp->size = 0;
110
111         xp->next = entry->xattr_head;
112         entry->xattr_head = xp;
113 }
114
115
116 /*
117  * returns number of the extended attribute entries
118  */
119 int
120 archive_entry_xattr_count(struct archive_entry *entry)
121 {
122         struct ae_xattr *xp;
123         int count = 0;
124
125         for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
126                 count++;
127
128         return count;
129 }
130
131 int
132 archive_entry_xattr_reset(struct archive_entry * entry)
133 {
134         entry->xattr_p = entry->xattr_head;
135
136         return archive_entry_xattr_count(entry);
137 }
138
139 int
140 archive_entry_xattr_next(struct archive_entry * entry,
141         const char **name, const void **value, size_t *size)
142 {
143         if (entry->xattr_p) {
144                 *name = entry->xattr_p->name;
145                 *value = entry->xattr_p->value;
146                 *size = entry->xattr_p->size;
147
148                 entry->xattr_p = entry->xattr_p->next;
149
150                 return (ARCHIVE_OK);
151         } else {
152                 *name = NULL;
153                 *value = NULL;
154                 *size = (size_t)0;
155                 return (ARCHIVE_WARN);
156         }
157 }
158
159 /*
160  * end of xattr handling
161  */