2 * Copyright (c) 2003-2009 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
29 #ifdef HAVE_SYS_TYPES_H
30 /* Mac OSX requires sys/types.h before sys/acl.h. */
31 #include <sys/types.h>
36 #ifdef HAVE_SYS_EXTATTR_H
37 #include <sys/extattr.h>
39 #ifdef HAVE_SYS_PARAM_H
40 #include <sys/param.h>
42 #ifdef HAVE_SYS_STAT_H
45 #ifdef HAVE_ACL_LIBACL_H
46 #include <acl/libacl.h>
48 #ifdef HAVE_ATTR_XATTR_H
49 #include <attr/xattr.h>
62 #include "archive_entry.h"
63 #include "archive_private.h"
64 #include "archive_read_disk_private.h"
67 * Linux and FreeBSD plug this obvious hole in POSIX.1e in
71 #define ACL_GET_PERM acl_get_perm
72 #elif HAVE_ACL_GET_PERM_NP
73 #define ACL_GET_PERM acl_get_perm_np
76 static int setup_acls_posix1e(struct archive_read_disk *,
77 struct archive_entry *, int fd);
78 static int setup_xattrs(struct archive_read_disk *,
79 struct archive_entry *, int fd);
82 archive_read_disk_entry_from_file(struct archive *_a,
83 struct archive_entry *entry,
84 int fd, const struct stat *st)
86 struct archive_read_disk *a = (struct archive_read_disk *)_a;
92 path = archive_entry_sourcepath(entry);
94 path = archive_entry_pathname(entry);
96 #ifdef EXT2_IOC_GETFLAGS
97 /* Linux requires an extra ioctl to pull the flags. Although
98 * this is an extra step, it has a nice side-effect: We get an
99 * open file descriptor which we can use in the subsequent lookups. */
100 if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode))) {
102 fd = open(pathname, O_RDONLY | O_NONBLOCK);
104 unsigned long stflags;
105 int r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags);
106 if (r == 0 && stflags != 0)
107 archive_entry_set_fflags(entry, stflags, 0);
115 if (fstat(fd, &s) != 0)
116 return (ARCHIVE_FATAL);
120 if (!a->follow_symlinks) {
121 if (lstat(path, &s) != 0)
122 return (ARCHIVE_FATAL);
125 if (stat(path, &s) != 0)
126 return (ARCHIVE_FATAL);
129 archive_entry_copy_stat(entry, st);
131 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
132 /* On FreeBSD, we get flags for free with the stat. */
133 /* TODO: Does this belong in copy_stat()? */
134 if (st->st_flags != 0)
135 archive_entry_set_fflags(entry, st->st_flags, 0);
139 if (S_ISLNK(st->st_mode)) {
140 char linkbuffer[PATH_MAX + 1];
141 int lnklen = readlink(path, linkbuffer, PATH_MAX);
143 archive_set_error(&a->archive, errno,
144 "Couldn't read link data");
145 return (ARCHIVE_WARN);
147 linkbuffer[lnklen] = 0;
148 archive_entry_set_symlink(entry, linkbuffer);
152 r = setup_acls_posix1e(a, entry, fd);
153 r1 = setup_xattrs(a, entry, fd);
156 /* If we opened the file earlier in this function, close it. */
157 if (initial_fd != fd)
162 #ifdef HAVE_POSIX_ACL
163 static void setup_acl_posix1e(struct archive_read_disk *a,
164 struct archive_entry *entry, acl_t acl, int archive_entry_acl_type);
167 setup_acls_posix1e(struct archive_read_disk *a,
168 struct archive_entry *entry, int fd)
173 accpath = archive_entry_sourcepath(entry);
175 accpath = archive_entry_pathname(entry);
177 archive_entry_acl_clear(entry);
179 /* Retrieve access ACL from file. */
181 acl = acl_get_fd(fd);
182 #if HAVE_ACL_GET_LINK_NP
183 else if (!a->follow_symlinks)
184 acl = acl_get_link_np(accpath, ACL_TYPE_ACCESS);
187 acl = acl_get_file(accpath, ACL_TYPE_ACCESS);
189 setup_acl_posix1e(a, entry, acl,
190 ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
194 /* Only directories can have default ACLs. */
195 if (S_ISDIR(archive_entry_mode(entry))) {
196 acl = acl_get_file(accpath, ACL_TYPE_DEFAULT);
198 setup_acl_posix1e(a, entry, acl,
199 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
207 * Translate POSIX.1e ACL into libarchive internal structure.
210 setup_acl_posix1e(struct archive_read_disk *a,
211 struct archive_entry *entry, acl_t acl, int archive_entry_acl_type)
214 acl_entry_t acl_entry;
215 acl_permset_t acl_permset;
216 int s, ae_id, ae_tag, ae_perm;
219 s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
224 acl_get_tag_type(acl_entry, &acl_tag);
225 if (acl_tag == ACL_USER) {
226 ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
227 ae_name = archive_read_disk_uname(&a->archive, ae_id);
228 ae_tag = ARCHIVE_ENTRY_ACL_USER;
229 } else if (acl_tag == ACL_GROUP) {
230 ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
231 ae_name = archive_read_disk_gname(&a->archive, ae_id);
232 ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
233 } else if (acl_tag == ACL_MASK) {
234 ae_tag = ARCHIVE_ENTRY_ACL_MASK;
235 } else if (acl_tag == ACL_USER_OBJ) {
236 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
237 } else if (acl_tag == ACL_GROUP_OBJ) {
238 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
239 } else if (acl_tag == ACL_OTHER) {
240 ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
242 /* Skip types that libarchive can't support. */
246 acl_get_permset(acl_entry, &acl_permset);
249 * acl_get_perm() is spelled differently on different
250 * platforms; see above.
252 if (ACL_GET_PERM(acl_permset, ACL_EXECUTE))
253 ae_perm |= ARCHIVE_ENTRY_ACL_EXECUTE;
254 if (ACL_GET_PERM(acl_permset, ACL_READ))
255 ae_perm |= ARCHIVE_ENTRY_ACL_READ;
256 if (ACL_GET_PERM(acl_permset, ACL_WRITE))
257 ae_perm |= ARCHIVE_ENTRY_ACL_WRITE;
259 archive_entry_acl_add_entry(entry,
260 archive_entry_acl_type, ae_perm, ae_tag,
263 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
268 setup_acls_posix1e(struct archive_read_disk *a,
269 struct archive_entry *entry, int fd)
271 (void)a; /* UNUSED */
272 (void)entry; /* UNUSED */
273 (void)fd; /* UNUSED */
278 #if HAVE_LISTXATTR && HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR
281 * Linux extended attribute support.
283 * TODO: By using a stack-allocated buffer for the first
284 * call to getxattr(), we might be able to avoid the second
285 * call entirely. We only need the second call if the
286 * stack-allocated buffer is too small. But a modest buffer
287 * of 1024 bytes or so will often be big enough. Same applies
293 setup_xattr(struct archive_read_disk *a,
294 struct archive_entry *entry, const char *name, int fd)
300 (void)fd; /* UNUSED */
302 accpath = archive_entry_sourcepath(entry);
304 accpath = archive_entry_pathname(entry);
306 if (!a->follow_symlinks)
307 size = lgetxattr(accpath, name, NULL, 0);
309 size = getxattr(accpath, name, NULL, 0);
312 archive_set_error(&a->archive, errno,
313 "Couldn't query extended attribute");
314 return (ARCHIVE_WARN);
317 if (size > 0 && (value = malloc(size)) == NULL) {
318 archive_set_error(&a->archive, errno, "Out of memory");
319 return (ARCHIVE_FATAL);
322 if (!a->follow_symlinks)
323 size = lgetxattr(accpath, name, value, size);
325 size = getxattr(accpath, name, value, size);
328 archive_set_error(&a->archive, errno,
329 "Couldn't read extended attribute");
330 return (ARCHIVE_WARN);
333 archive_entry_xattr_add_entry(entry, name, value, size);
340 setup_xattrs(struct archive_read_disk *a,
341 struct archive_entry *entry, int fd)
348 path = archive_entry_sourcepath(entry);
350 path = archive_entry_pathname(entry);
352 if (!a->follow_symlinks)
353 list_size = llistxattr(path, NULL, 0);
355 list_size = listxattr(path, NULL, 0);
357 if (list_size == -1) {
358 if (errno == ENOTSUP)
360 archive_set_error(&a->archive, errno,
361 "Couldn't list extended attributes");
362 return (ARCHIVE_WARN);
368 if ((list = malloc(list_size)) == NULL) {
369 archive_set_error(&a->archive, errno, "Out of memory");
370 return (ARCHIVE_FATAL);
373 if (!a->follow_symlinks)
374 list_size = llistxattr(path, list, list_size);
376 list_size = listxattr(path, list, list_size);
378 if (list_size == -1) {
379 archive_set_error(&a->archive, errno,
380 "Couldn't retrieve extended attributes");
382 return (ARCHIVE_WARN);
385 for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
386 if (strncmp(p, "system.", 7) == 0 ||
387 strncmp(p, "xfsroot.", 8) == 0)
389 setup_xattr(a, entry, p, fd);
396 #elif HAVE_EXTATTR_GET_FILE && HAVE_EXTATTR_LIST_FILE
399 * FreeBSD extattr interface.
402 /* TODO: Implement this. Follow the Linux model above, but
403 * with FreeBSD-specific system calls, of course. Be careful
404 * to not include the system extattrs that hold ACLs; we handle
408 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
409 int namespace, const char *name, const char *fullname, int fd);
412 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
413 int namespace, const char *name, const char *fullname, int fd)
419 (void)fd; /* UNUSED */
421 accpath = archive_entry_sourcepath(entry);
423 accpath = archive_entry_pathname(entry);
425 if (!a->follow_symlinks)
426 size = extattr_get_link(accpath, namespace, name, NULL, 0);
428 size = extattr_get_file(accpath, namespace, name, NULL, 0);
431 archive_set_error(&a->archive, errno,
432 "Couldn't query extended attribute");
433 return (ARCHIVE_WARN);
436 if (size > 0 && (value = malloc(size)) == NULL) {
437 archive_set_error(&a->archive, errno, "Out of memory");
438 return (ARCHIVE_FATAL);
441 if (!a->follow_symlinks)
442 size = extattr_get_link(accpath, namespace, name, value, size);
444 size = extattr_get_file(accpath, namespace, name, value, size);
447 archive_set_error(&a->archive, errno,
448 "Couldn't read extended attribute");
449 return (ARCHIVE_WARN);
452 archive_entry_xattr_add_entry(entry, fullname, value, size);
459 setup_xattrs(struct archive_read_disk *a,
460 struct archive_entry *entry, int fd)
466 int namespace = EXTATTR_NAMESPACE_USER;
468 path = archive_entry_sourcepath(entry);
470 path = archive_entry_pathname(entry);
472 if (!a->follow_symlinks)
473 list_size = extattr_list_link(path, namespace, NULL, 0);
475 list_size = extattr_list_file(path, namespace, NULL, 0);
477 if (list_size == -1 && errno == EOPNOTSUPP)
479 if (list_size == -1) {
480 archive_set_error(&a->archive, errno,
481 "Couldn't list extended attributes");
482 return (ARCHIVE_WARN);
488 if ((list = malloc(list_size)) == NULL) {
489 archive_set_error(&a->archive, errno, "Out of memory");
490 return (ARCHIVE_FATAL);
493 if (!a->follow_symlinks)
494 list_size = extattr_list_link(path, namespace, list, list_size);
496 list_size = extattr_list_file(path, namespace, list, list_size);
498 if (list_size == -1) {
499 archive_set_error(&a->archive, errno,
500 "Couldn't retrieve extended attributes");
502 return (ARCHIVE_WARN);
506 while ((p - list) < list_size) {
507 size_t len = 255 & (int)*p;
510 strcpy(buff, "user.");
511 name = buff + strlen(buff);
512 memcpy(name, p + 1, len);
514 setup_xattr(a, entry, namespace, name, buff, fd);
525 * Generic (stub) extended attribute support.
528 setup_xattrs(struct archive_read_disk *a,
529 struct archive_entry *entry, int fd)
531 (void)a; /* UNUSED */
532 (void)entry; /* UNUSED */
533 (void)fd; /* UNUSED */