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_SYS_XATTR_H
46 #include <sys/xattr.h>
48 #ifdef HAVE_ACL_LIBACL_H
49 #include <acl/libacl.h>
51 #ifdef HAVE_ATTR_XATTR_H
52 #include <attr/xattr.h>
65 #include "archive_entry.h"
66 #include "archive_private.h"
67 #include "archive_read_disk_private.h"
70 * Linux and FreeBSD plug this obvious hole in POSIX.1e in
74 #define ACL_GET_PERM acl_get_perm
75 #elif HAVE_ACL_GET_PERM_NP
76 #define ACL_GET_PERM acl_get_perm_np
79 static int setup_acls_posix1e(struct archive_read_disk *,
80 struct archive_entry *, int fd);
81 static int setup_xattrs(struct archive_read_disk *,
82 struct archive_entry *, int fd);
85 archive_read_disk_entry_from_file(struct archive *_a,
86 struct archive_entry *entry,
87 int fd, const struct stat *st)
89 struct archive_read_disk *a = (struct archive_read_disk *)_a;
90 const char *path, *name;
95 archive_clear_error(_a);
96 path = archive_entry_sourcepath(entry);
98 path = archive_entry_pathname(entry);
100 #ifdef EXT2_IOC_GETFLAGS
101 /* Linux requires an extra ioctl to pull the flags. Although
102 * this is an extra step, it has a nice side-effect: We get an
103 * open file descriptor which we can use in the subsequent lookups. */
104 if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode))) {
106 fd = open(pathname, O_RDONLY | O_NONBLOCK | O_BINARY);
108 unsigned long stflags;
109 int r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags);
110 if (r == 0 && stflags != 0)
111 archive_entry_set_fflags(entry, stflags, 0);
117 /* TODO: On Windows, use GetFileInfoByHandle() here.
118 * Using Windows stat() call is badly broken, but
119 * even the stat() wrapper has problems because
120 * 'struct stat' is broken on Windows.
124 if (fstat(fd, &s) != 0) {
125 archive_set_error(&a->archive, errno,
127 return (ARCHIVE_FAILED);
132 if (!a->follow_symlinks) {
133 if (lstat(path, &s) != 0) {
134 archive_set_error(&a->archive, errno,
135 "Can't lstat %s", path);
136 return (ARCHIVE_FAILED);
140 if (stat(path, &s) != 0) {
141 archive_set_error(&a->archive, errno,
142 "Can't stat %s", path);
143 return (ARCHIVE_FAILED);
147 archive_entry_copy_stat(entry, st);
149 /* Lookup uname/gname */
150 name = archive_read_disk_uname(_a, archive_entry_uid(entry));
152 archive_entry_copy_uname(entry, name);
153 name = archive_read_disk_gname(_a, archive_entry_gid(entry));
155 archive_entry_copy_gname(entry, name);
157 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
158 /* On FreeBSD, we get flags for free with the stat. */
159 /* TODO: Does this belong in copy_stat()? */
160 if (st->st_flags != 0)
161 archive_entry_set_fflags(entry, st->st_flags, 0);
165 if (S_ISLNK(st->st_mode)) {
166 char linkbuffer[PATH_MAX + 1];
167 int lnklen = readlink(path, linkbuffer, PATH_MAX);
169 archive_set_error(&a->archive, errno,
170 "Couldn't read link data");
171 return (ARCHIVE_FAILED);
173 linkbuffer[lnklen] = 0;
174 archive_entry_set_symlink(entry, linkbuffer);
178 r = setup_acls_posix1e(a, entry, fd);
179 r1 = setup_xattrs(a, entry, fd);
182 /* If we opened the file earlier in this function, close it. */
183 if (initial_fd != fd)
188 #ifdef HAVE_POSIX_ACL
189 static void setup_acl_posix1e(struct archive_read_disk *a,
190 struct archive_entry *entry, acl_t acl, int archive_entry_acl_type);
193 setup_acls_posix1e(struct archive_read_disk *a,
194 struct archive_entry *entry, int fd)
199 accpath = archive_entry_sourcepath(entry);
201 accpath = archive_entry_pathname(entry);
203 archive_entry_acl_clear(entry);
205 /* Retrieve access ACL from file. */
207 acl = acl_get_fd(fd);
208 #if HAVE_ACL_GET_LINK_NP
209 else if (!a->follow_symlinks)
210 acl = acl_get_link_np(accpath, ACL_TYPE_ACCESS);
212 else if ((!a->follow_symlinks)
213 && (archive_entry_filetype(entry) == AE_IFLNK))
214 /* We can't get the ACL of a symlink, so we assume it can't
219 acl = acl_get_file(accpath, ACL_TYPE_ACCESS);
221 setup_acl_posix1e(a, entry, acl,
222 ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
226 /* Only directories can have default ACLs. */
227 if (S_ISDIR(archive_entry_mode(entry))) {
228 acl = acl_get_file(accpath, ACL_TYPE_DEFAULT);
230 setup_acl_posix1e(a, entry, acl,
231 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
239 * Translate POSIX.1e ACL into libarchive internal structure.
242 setup_acl_posix1e(struct archive_read_disk *a,
243 struct archive_entry *entry, acl_t acl, int archive_entry_acl_type)
246 acl_entry_t acl_entry;
247 acl_permset_t acl_permset;
248 int s, ae_id, ae_tag, ae_perm;
251 s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
256 acl_get_tag_type(acl_entry, &acl_tag);
257 if (acl_tag == ACL_USER) {
258 ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
259 ae_name = archive_read_disk_uname(&a->archive, ae_id);
260 ae_tag = ARCHIVE_ENTRY_ACL_USER;
261 } else if (acl_tag == ACL_GROUP) {
262 ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
263 ae_name = archive_read_disk_gname(&a->archive, ae_id);
264 ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
265 } else if (acl_tag == ACL_MASK) {
266 ae_tag = ARCHIVE_ENTRY_ACL_MASK;
267 } else if (acl_tag == ACL_USER_OBJ) {
268 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
269 } else if (acl_tag == ACL_GROUP_OBJ) {
270 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
271 } else if (acl_tag == ACL_OTHER) {
272 ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
274 /* Skip types that libarchive can't support. */
278 acl_get_permset(acl_entry, &acl_permset);
281 * acl_get_perm() is spelled differently on different
282 * platforms; see above.
284 if (ACL_GET_PERM(acl_permset, ACL_EXECUTE))
285 ae_perm |= ARCHIVE_ENTRY_ACL_EXECUTE;
286 if (ACL_GET_PERM(acl_permset, ACL_READ))
287 ae_perm |= ARCHIVE_ENTRY_ACL_READ;
288 if (ACL_GET_PERM(acl_permset, ACL_WRITE))
289 ae_perm |= ARCHIVE_ENTRY_ACL_WRITE;
291 archive_entry_acl_add_entry(entry,
292 archive_entry_acl_type, ae_perm, ae_tag,
295 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
300 setup_acls_posix1e(struct archive_read_disk *a,
301 struct archive_entry *entry, int fd)
303 (void)a; /* UNUSED */
304 (void)entry; /* UNUSED */
305 (void)fd; /* UNUSED */
310 #if HAVE_LISTXATTR && HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR
313 * Linux extended attribute support.
315 * TODO: By using a stack-allocated buffer for the first
316 * call to getxattr(), we might be able to avoid the second
317 * call entirely. We only need the second call if the
318 * stack-allocated buffer is too small. But a modest buffer
319 * of 1024 bytes or so will often be big enough. Same applies
325 setup_xattr(struct archive_read_disk *a,
326 struct archive_entry *entry, const char *name, int fd)
332 (void)fd; /* UNUSED */
334 accpath = archive_entry_sourcepath(entry);
336 accpath = archive_entry_pathname(entry);
338 if (!a->follow_symlinks)
339 size = lgetxattr(accpath, name, NULL, 0);
341 size = getxattr(accpath, name, NULL, 0);
344 archive_set_error(&a->archive, errno,
345 "Couldn't query extended attribute");
346 return (ARCHIVE_WARN);
349 if (size > 0 && (value = malloc(size)) == NULL) {
350 archive_set_error(&a->archive, errno, "Out of memory");
351 return (ARCHIVE_FATAL);
354 if (!a->follow_symlinks)
355 size = lgetxattr(accpath, name, value, size);
357 size = getxattr(accpath, name, value, size);
360 archive_set_error(&a->archive, errno,
361 "Couldn't read extended attribute");
362 return (ARCHIVE_WARN);
365 archive_entry_xattr_add_entry(entry, name, value, size);
372 setup_xattrs(struct archive_read_disk *a,
373 struct archive_entry *entry, int fd)
380 path = archive_entry_sourcepath(entry);
382 path = archive_entry_pathname(entry);
384 if (!a->follow_symlinks)
385 list_size = llistxattr(path, NULL, 0);
387 list_size = listxattr(path, NULL, 0);
389 if (list_size == -1) {
390 if (errno == ENOTSUP)
392 archive_set_error(&a->archive, errno,
393 "Couldn't list extended attributes");
394 return (ARCHIVE_WARN);
400 if ((list = malloc(list_size)) == NULL) {
401 archive_set_error(&a->archive, errno, "Out of memory");
402 return (ARCHIVE_FATAL);
405 if (!a->follow_symlinks)
406 list_size = llistxattr(path, list, list_size);
408 list_size = listxattr(path, list, list_size);
410 if (list_size == -1) {
411 archive_set_error(&a->archive, errno,
412 "Couldn't retrieve extended attributes");
414 return (ARCHIVE_WARN);
417 for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
418 if (strncmp(p, "system.", 7) == 0 ||
419 strncmp(p, "xfsroot.", 8) == 0)
421 setup_xattr(a, entry, p, fd);
428 #elif HAVE_EXTATTR_GET_FILE && HAVE_EXTATTR_LIST_FILE && \
429 HAVE_DECL_EXTATTR_NAMESPACE_USER
432 * FreeBSD extattr interface.
435 /* TODO: Implement this. Follow the Linux model above, but
436 * with FreeBSD-specific system calls, of course. Be careful
437 * to not include the system extattrs that hold ACLs; we handle
441 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
442 int namespace, const char *name, const char *fullname, int fd);
445 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
446 int namespace, const char *name, const char *fullname, int fd)
452 (void)fd; /* UNUSED */
454 accpath = archive_entry_sourcepath(entry);
456 accpath = archive_entry_pathname(entry);
458 if (!a->follow_symlinks)
459 size = extattr_get_link(accpath, namespace, name, NULL, 0);
461 size = extattr_get_file(accpath, namespace, name, NULL, 0);
464 archive_set_error(&a->archive, errno,
465 "Couldn't query extended attribute");
466 return (ARCHIVE_WARN);
469 if (size > 0 && (value = malloc(size)) == NULL) {
470 archive_set_error(&a->archive, errno, "Out of memory");
471 return (ARCHIVE_FATAL);
474 if (!a->follow_symlinks)
475 size = extattr_get_link(accpath, namespace, name, value, size);
477 size = extattr_get_file(accpath, namespace, name, value, size);
480 archive_set_error(&a->archive, errno,
481 "Couldn't read extended attribute");
482 return (ARCHIVE_WARN);
485 archive_entry_xattr_add_entry(entry, fullname, value, size);
492 setup_xattrs(struct archive_read_disk *a,
493 struct archive_entry *entry, int fd)
499 int namespace = EXTATTR_NAMESPACE_USER;
501 path = archive_entry_sourcepath(entry);
503 path = archive_entry_pathname(entry);
505 if (!a->follow_symlinks)
506 list_size = extattr_list_link(path, namespace, NULL, 0);
508 list_size = extattr_list_file(path, namespace, NULL, 0);
510 if (list_size == -1 && errno == EOPNOTSUPP)
512 if (list_size == -1) {
513 archive_set_error(&a->archive, errno,
514 "Couldn't list extended attributes");
515 return (ARCHIVE_WARN);
521 if ((list = malloc(list_size)) == NULL) {
522 archive_set_error(&a->archive, errno, "Out of memory");
523 return (ARCHIVE_FATAL);
526 if (!a->follow_symlinks)
527 list_size = extattr_list_link(path, namespace, list, list_size);
529 list_size = extattr_list_file(path, namespace, list, list_size);
531 if (list_size == -1) {
532 archive_set_error(&a->archive, errno,
533 "Couldn't retrieve extended attributes");
535 return (ARCHIVE_WARN);
539 while ((p - list) < list_size) {
540 size_t len = 255 & (int)*p;
543 strcpy(buff, "user.");
544 name = buff + strlen(buff);
545 memcpy(name, p + 1, len);
547 setup_xattr(a, entry, namespace, name, buff, fd);
558 * Generic (stub) extended attribute support.
561 setup_xattrs(struct archive_read_disk *a,
562 struct archive_entry *entry, int fd)
564 (void)a; /* UNUSED */
565 (void)entry; /* UNUSED */
566 (void)fd; /* UNUSED */