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