archive_read_disk_entry_from_file.c revision 305188
1/*-
2 * Copyright (c) 2003-2009 Tim Kientzle
3 * Copyright (c) 2010-2012 Michihiro NAKAJIMA
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "archive_platform.h"
28__FBSDID("$FreeBSD: stable/11/contrib/libarchive/libarchive/archive_read_disk_entry_from_file.c 305188 2016-09-01 07:53:59Z mm $");
29
30/* This is the tree-walking code for POSIX systems. */
31#if !defined(_WIN32) || defined(__CYGWIN__)
32
33#ifdef HAVE_SYS_TYPES_H
34/* Mac OSX requires sys/types.h before sys/acl.h. */
35#include <sys/types.h>
36#endif
37#ifdef HAVE_SYS_ACL_H
38#include <sys/acl.h>
39#endif
40#ifdef HAVE_SYS_EXTATTR_H
41#include <sys/extattr.h>
42#endif
43#ifdef HAVE_SYS_IOCTL_H
44#include <sys/ioctl.h>
45#endif
46#ifdef HAVE_SYS_PARAM_H
47#include <sys/param.h>
48#endif
49#ifdef HAVE_SYS_STAT_H
50#include <sys/stat.h>
51#endif
52#if defined(HAVE_SYS_XATTR_H)
53#include <sys/xattr.h>
54#elif defined(HAVE_ATTR_XATTR_H)
55#include <attr/xattr.h>
56#endif
57#ifdef HAVE_SYS_EA_H
58#include <sys/ea.h>
59#endif
60#ifdef HAVE_ACL_LIBACL_H
61#include <acl/libacl.h>
62#endif
63#ifdef HAVE_COPYFILE_H
64#include <copyfile.h>
65#endif
66#ifdef HAVE_ERRNO_H
67#include <errno.h>
68#endif
69#ifdef HAVE_FCNTL_H
70#include <fcntl.h>
71#endif
72#ifdef HAVE_LIMITS_H
73#include <limits.h>
74#endif
75#ifdef HAVE_LINUX_TYPES_H
76#include <linux/types.h>
77#endif
78#ifdef HAVE_LINUX_FIEMAP_H
79#include <linux/fiemap.h>
80#endif
81#ifdef HAVE_LINUX_FS_H
82#include <linux/fs.h>
83#endif
84/*
85 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
86 * As the include guards don't agree, the order of include is important.
87 */
88#ifdef HAVE_LINUX_EXT2_FS_H
89#include <linux/ext2_fs.h>      /* for Linux file flags */
90#endif
91#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
92#include <ext2fs/ext2_fs.h>     /* Linux file flags, broken on Cygwin */
93#endif
94#ifdef HAVE_PATHS_H
95#include <paths.h>
96#endif
97#ifdef HAVE_UNISTD_H
98#include <unistd.h>
99#endif
100
101#include "archive.h"
102#include "archive_entry.h"
103#include "archive_private.h"
104#include "archive_read_disk_private.h"
105
106#ifndef O_CLOEXEC
107#define O_CLOEXEC	0
108#endif
109
110/*
111 * Linux and FreeBSD plug this obvious hole in POSIX.1e in
112 * different ways.
113 */
114#if HAVE_ACL_GET_PERM
115#define	ACL_GET_PERM acl_get_perm
116#elif HAVE_ACL_GET_PERM_NP
117#define	ACL_GET_PERM acl_get_perm_np
118#endif
119
120static int setup_acls(struct archive_read_disk *,
121    struct archive_entry *, int *fd);
122static int setup_mac_metadata(struct archive_read_disk *,
123    struct archive_entry *, int *fd);
124static int setup_xattrs(struct archive_read_disk *,
125    struct archive_entry *, int *fd);
126static int setup_sparse(struct archive_read_disk *,
127    struct archive_entry *, int *fd);
128
129int
130archive_read_disk_entry_from_file(struct archive *_a,
131    struct archive_entry *entry,
132    int fd,
133    const struct stat *st)
134{
135	struct archive_read_disk *a = (struct archive_read_disk *)_a;
136	const char *path, *name;
137	struct stat s;
138	int initial_fd = fd;
139	int r, r1;
140
141	archive_clear_error(_a);
142	path = archive_entry_sourcepath(entry);
143	if (path == NULL)
144		path = archive_entry_pathname(entry);
145
146	if (a->tree == NULL) {
147		if (st == NULL) {
148#if HAVE_FSTAT
149			if (fd >= 0) {
150				if (fstat(fd, &s) != 0) {
151					archive_set_error(&a->archive, errno,
152					    "Can't fstat");
153					return (ARCHIVE_FAILED);
154				}
155			} else
156#endif
157#if HAVE_LSTAT
158			if (!a->follow_symlinks) {
159				if (lstat(path, &s) != 0) {
160					archive_set_error(&a->archive, errno,
161					    "Can't lstat %s", path);
162					return (ARCHIVE_FAILED);
163				}
164			} else
165#endif
166			if (stat(path, &s) != 0) {
167				archive_set_error(&a->archive, errno,
168				    "Can't stat %s", path);
169				return (ARCHIVE_FAILED);
170			}
171			st = &s;
172		}
173		archive_entry_copy_stat(entry, st);
174	}
175
176	/* Lookup uname/gname */
177	name = archive_read_disk_uname(_a, archive_entry_uid(entry));
178	if (name != NULL)
179		archive_entry_copy_uname(entry, name);
180	name = archive_read_disk_gname(_a, archive_entry_gid(entry));
181	if (name != NULL)
182		archive_entry_copy_gname(entry, name);
183
184#ifdef HAVE_STRUCT_STAT_ST_FLAGS
185	/* On FreeBSD, we get flags for free with the stat. */
186	/* TODO: Does this belong in copy_stat()? */
187	if (st->st_flags != 0)
188		archive_entry_set_fflags(entry, st->st_flags, 0);
189#endif
190
191#if defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)
192	/* Linux requires an extra ioctl to pull the flags.  Although
193	 * this is an extra step, it has a nice side-effect: We get an
194	 * open file descriptor which we can use in the subsequent lookups. */
195	if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode))) {
196		if (fd < 0) {
197			if (a->tree != NULL)
198				fd = a->open_on_current_dir(a->tree, path,
199					O_RDONLY | O_NONBLOCK | O_CLOEXEC);
200			else
201				fd = open(path, O_RDONLY | O_NONBLOCK |
202						O_CLOEXEC);
203			__archive_ensure_cloexec_flag(fd);
204		}
205		if (fd >= 0) {
206			int stflags;
207			r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags);
208			if (r == 0 && stflags != 0)
209				archive_entry_set_fflags(entry, stflags, 0);
210		}
211	}
212#endif
213
214#if defined(HAVE_READLINK) || defined(HAVE_READLINKAT)
215	if (S_ISLNK(st->st_mode)) {
216		size_t linkbuffer_len = st->st_size + 1;
217		char *linkbuffer;
218		int lnklen;
219
220		linkbuffer = malloc(linkbuffer_len);
221		if (linkbuffer == NULL) {
222			archive_set_error(&a->archive, ENOMEM,
223			    "Couldn't read link data");
224			return (ARCHIVE_FAILED);
225		}
226		if (a->tree != NULL) {
227#ifdef HAVE_READLINKAT
228			lnklen = readlinkat(a->tree_current_dir_fd(a->tree),
229			    path, linkbuffer, linkbuffer_len);
230#else
231			if (a->tree_enter_working_dir(a->tree) != 0) {
232				archive_set_error(&a->archive, errno,
233				    "Couldn't read link data");
234				free(linkbuffer);
235				return (ARCHIVE_FAILED);
236			}
237			lnklen = readlink(path, linkbuffer, linkbuffer_len);
238#endif /* HAVE_READLINKAT */
239		} else
240			lnklen = readlink(path, linkbuffer, linkbuffer_len);
241		if (lnklen < 0) {
242			archive_set_error(&a->archive, errno,
243			    "Couldn't read link data");
244			free(linkbuffer);
245			return (ARCHIVE_FAILED);
246		}
247		linkbuffer[lnklen] = 0;
248		archive_entry_set_symlink(entry, linkbuffer);
249		free(linkbuffer);
250	}
251#endif /* HAVE_READLINK || HAVE_READLINKAT */
252
253	r = setup_acls(a, entry, &fd);
254	if (!a->suppress_xattr) {
255		r1 = setup_xattrs(a, entry, &fd);
256		if (r1 < r)
257			r = r1;
258	}
259	if (a->enable_copyfile) {
260		r1 = setup_mac_metadata(a, entry, &fd);
261		if (r1 < r)
262			r = r1;
263	}
264	r1 = setup_sparse(a, entry, &fd);
265	if (r1 < r)
266		r = r1;
267
268	/* If we opened the file earlier in this function, close it. */
269	if (initial_fd != fd)
270		close(fd);
271	return (r);
272}
273
274#if defined(__APPLE__) && defined(HAVE_COPYFILE_H)
275/*
276 * The Mac OS "copyfile()" API copies the extended metadata for a
277 * file into a separate file in AppleDouble format (see RFC 1740).
278 *
279 * Mac OS tar and cpio implementations store this extended
280 * metadata as a separate entry just before the regular entry
281 * with a "._" prefix added to the filename.
282 *
283 * Note that this is currently done unconditionally; the tar program has
284 * an option to discard this information before the archive is written.
285 *
286 * TODO: If there's a failure, report it and return ARCHIVE_WARN.
287 */
288static int
289setup_mac_metadata(struct archive_read_disk *a,
290    struct archive_entry *entry, int *fd)
291{
292	int tempfd = -1;
293	int copyfile_flags = COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR;
294	struct stat copyfile_stat;
295	int ret = ARCHIVE_OK;
296	void *buff = NULL;
297	int have_attrs;
298	const char *name, *tempdir;
299	struct archive_string tempfile;
300
301	(void)fd; /* UNUSED */
302	name = archive_entry_sourcepath(entry);
303	if (name == NULL)
304		name = archive_entry_pathname(entry);
305	if (name == NULL) {
306		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
307		    "Can't open file to read extended attributes: No name");
308		return (ARCHIVE_WARN);
309	}
310
311	if (a->tree != NULL) {
312		if (a->tree_enter_working_dir(a->tree) != 0) {
313			archive_set_error(&a->archive, errno,
314				    "Couldn't change dir");
315				return (ARCHIVE_FAILED);
316		}
317	}
318
319	/* Short-circuit if there's nothing to do. */
320	have_attrs = copyfile(name, NULL, 0, copyfile_flags | COPYFILE_CHECK);
321	if (have_attrs == -1) {
322		archive_set_error(&a->archive, errno,
323			"Could not check extended attributes");
324		return (ARCHIVE_WARN);
325	}
326	if (have_attrs == 0)
327		return (ARCHIVE_OK);
328
329	tempdir = NULL;
330	if (issetugid() == 0)
331		tempdir = getenv("TMPDIR");
332	if (tempdir == NULL)
333		tempdir = _PATH_TMP;
334	archive_string_init(&tempfile);
335	archive_strcpy(&tempfile, tempdir);
336	archive_strcat(&tempfile, "tar.md.XXXXXX");
337	tempfd = mkstemp(tempfile.s);
338	if (tempfd < 0) {
339		archive_set_error(&a->archive, errno,
340		    "Could not open extended attribute file");
341		ret = ARCHIVE_WARN;
342		goto cleanup;
343	}
344	__archive_ensure_cloexec_flag(tempfd);
345
346	/* XXX I wish copyfile() could pack directly to a memory
347	 * buffer; that would avoid the temp file here.  For that
348	 * matter, it would be nice if fcopyfile() actually worked,
349	 * that would reduce the many open/close races here. */
350	if (copyfile(name, tempfile.s, 0, copyfile_flags | COPYFILE_PACK)) {
351		archive_set_error(&a->archive, errno,
352		    "Could not pack extended attributes");
353		ret = ARCHIVE_WARN;
354		goto cleanup;
355	}
356	if (fstat(tempfd, &copyfile_stat)) {
357		archive_set_error(&a->archive, errno,
358		    "Could not check size of extended attributes");
359		ret = ARCHIVE_WARN;
360		goto cleanup;
361	}
362	buff = malloc(copyfile_stat.st_size);
363	if (buff == NULL) {
364		archive_set_error(&a->archive, errno,
365		    "Could not allocate memory for extended attributes");
366		ret = ARCHIVE_WARN;
367		goto cleanup;
368	}
369	if (copyfile_stat.st_size != read(tempfd, buff, copyfile_stat.st_size)) {
370		archive_set_error(&a->archive, errno,
371		    "Could not read extended attributes into memory");
372		ret = ARCHIVE_WARN;
373		goto cleanup;
374	}
375	archive_entry_copy_mac_metadata(entry, buff, copyfile_stat.st_size);
376
377cleanup:
378	if (tempfd >= 0) {
379		close(tempfd);
380		unlink(tempfile.s);
381	}
382	archive_string_free(&tempfile);
383	free(buff);
384	return (ret);
385}
386
387#else
388
389/*
390 * Stub implementation for non-Mac systems.
391 */
392static int
393setup_mac_metadata(struct archive_read_disk *a,
394    struct archive_entry *entry, int *fd)
395{
396	(void)a; /* UNUSED */
397	(void)entry; /* UNUSED */
398	(void)fd; /* UNUSED */
399	return (ARCHIVE_OK);
400}
401#endif
402
403
404#ifdef HAVE_POSIX_ACL
405static int translate_acl(struct archive_read_disk *a,
406    struct archive_entry *entry, acl_t acl, int archive_entry_acl_type);
407
408static int
409setup_acls(struct archive_read_disk *a,
410    struct archive_entry *entry, int *fd)
411{
412	const char	*accpath;
413	acl_t		 acl;
414#if HAVE_ACL_IS_TRIVIAL_NP
415	int		r;
416#endif
417
418	accpath = archive_entry_sourcepath(entry);
419	if (accpath == NULL)
420		accpath = archive_entry_pathname(entry);
421
422	archive_entry_acl_clear(entry);
423
424#ifdef ACL_TYPE_NFS4
425	/* Try NFS4 ACL first. */
426	if (*fd >= 0)
427		acl = acl_get_fd(*fd);
428#if HAVE_ACL_GET_LINK_NP
429	else if (!a->follow_symlinks)
430		acl = acl_get_link_np(accpath, ACL_TYPE_NFS4);
431#else
432	else if ((!a->follow_symlinks)
433	    && (archive_entry_filetype(entry) == AE_IFLNK))
434		/* We can't get the ACL of a symlink, so we assume it can't
435		   have one. */
436		acl = NULL;
437#endif
438	else
439		acl = acl_get_file(accpath, ACL_TYPE_NFS4);
440#if HAVE_ACL_IS_TRIVIAL_NP
441	/* Ignore "trivial" ACLs that just mirror the file mode. */
442	acl_is_trivial_np(acl, &r);
443	if (r) {
444		acl_free(acl);
445		acl = NULL;
446	}
447#endif
448	if (acl != NULL) {
449		translate_acl(a, entry, acl, ARCHIVE_ENTRY_ACL_TYPE_NFS4);
450		acl_free(acl);
451		return (ARCHIVE_OK);
452	}
453#endif
454
455	/* Retrieve access ACL from file. */
456	if (*fd >= 0)
457		acl = acl_get_fd(*fd);
458#if HAVE_ACL_GET_LINK_NP
459	else if (!a->follow_symlinks)
460		acl = acl_get_link_np(accpath, ACL_TYPE_ACCESS);
461#else
462	else if ((!a->follow_symlinks)
463	    && (archive_entry_filetype(entry) == AE_IFLNK))
464		/* We can't get the ACL of a symlink, so we assume it can't
465		   have one. */
466		acl = NULL;
467#endif
468	else
469		acl = acl_get_file(accpath, ACL_TYPE_ACCESS);
470	if (acl != NULL) {
471		translate_acl(a, entry, acl,
472		    ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
473		acl_free(acl);
474	}
475
476	/* Only directories can have default ACLs. */
477	if (S_ISDIR(archive_entry_mode(entry))) {
478		acl = acl_get_file(accpath, ACL_TYPE_DEFAULT);
479		if (acl != NULL) {
480			translate_acl(a, entry, acl,
481			    ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
482			acl_free(acl);
483		}
484	}
485	return (ARCHIVE_OK);
486}
487
488/*
489 * Translate system ACL into libarchive internal structure.
490 */
491
492static struct {
493        int archive_perm;
494        int platform_perm;
495} acl_perm_map[] = {
496        {ARCHIVE_ENTRY_ACL_EXECUTE, ACL_EXECUTE},
497        {ARCHIVE_ENTRY_ACL_WRITE, ACL_WRITE},
498        {ARCHIVE_ENTRY_ACL_READ, ACL_READ},
499#ifdef ACL_TYPE_NFS4
500        {ARCHIVE_ENTRY_ACL_READ_DATA, ACL_READ_DATA},
501        {ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, ACL_LIST_DIRECTORY},
502        {ARCHIVE_ENTRY_ACL_WRITE_DATA, ACL_WRITE_DATA},
503        {ARCHIVE_ENTRY_ACL_ADD_FILE, ACL_ADD_FILE},
504        {ARCHIVE_ENTRY_ACL_APPEND_DATA, ACL_APPEND_DATA},
505        {ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY, ACL_ADD_SUBDIRECTORY},
506        {ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
507        {ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
508        {ARCHIVE_ENTRY_ACL_DELETE_CHILD, ACL_DELETE_CHILD},
509        {ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, ACL_READ_ATTRIBUTES},
510        {ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
511        {ARCHIVE_ENTRY_ACL_DELETE, ACL_DELETE},
512        {ARCHIVE_ENTRY_ACL_READ_ACL, ACL_READ_ACL},
513        {ARCHIVE_ENTRY_ACL_WRITE_ACL, ACL_WRITE_ACL},
514        {ARCHIVE_ENTRY_ACL_WRITE_OWNER, ACL_WRITE_OWNER},
515        {ARCHIVE_ENTRY_ACL_SYNCHRONIZE, ACL_SYNCHRONIZE}
516#endif
517};
518
519#ifdef ACL_TYPE_NFS4
520static struct {
521        int archive_inherit;
522        int platform_inherit;
523} acl_inherit_map[] = {
524        {ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_FILE_INHERIT},
525	{ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT},
526	{ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_NO_PROPAGATE_INHERIT},
527	{ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, ACL_ENTRY_INHERIT_ONLY}
528};
529#endif
530static int
531translate_acl(struct archive_read_disk *a,
532    struct archive_entry *entry, acl_t acl, int default_entry_acl_type)
533{
534	acl_tag_t	 acl_tag;
535#ifdef ACL_TYPE_NFS4
536	acl_entry_type_t acl_type;
537	acl_flagset_t	 acl_flagset;
538	int brand, r;
539#endif
540	acl_entry_t	 acl_entry;
541	acl_permset_t	 acl_permset;
542	int		 i, entry_acl_type;
543	int		 s, ae_id, ae_tag, ae_perm;
544	const char	*ae_name;
545
546#ifdef ACL_TYPE_NFS4
547	// FreeBSD "brands" ACLs as POSIX.1e or NFSv4
548	// Make sure the "brand" on this ACL is consistent
549	// with the default_entry_acl_type bits provided.
550	acl_get_brand_np(acl, &brand);
551	switch (brand) {
552	case ACL_BRAND_POSIX:
553		switch (default_entry_acl_type) {
554		case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
555		case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
556			break;
557		default:
558			// XXX set warning message?
559			return ARCHIVE_FAILED;
560		}
561		break;
562	case ACL_BRAND_NFS4:
563		if (default_entry_acl_type & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
564			// XXX set warning message?
565			return ARCHIVE_FAILED;
566		}
567		break;
568	default:
569		// XXX set warning message?
570		return ARCHIVE_FAILED;
571		break;
572	}
573#endif
574
575
576	s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
577	while (s == 1) {
578		ae_id = -1;
579		ae_name = NULL;
580		ae_perm = 0;
581
582		acl_get_tag_type(acl_entry, &acl_tag);
583		switch (acl_tag) {
584		case ACL_USER:
585			ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
586			ae_name = archive_read_disk_uname(&a->archive, ae_id);
587			ae_tag = ARCHIVE_ENTRY_ACL_USER;
588			break;
589		case ACL_GROUP:
590			ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
591			ae_name = archive_read_disk_gname(&a->archive, ae_id);
592			ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
593			break;
594		case ACL_MASK:
595			ae_tag = ARCHIVE_ENTRY_ACL_MASK;
596			break;
597		case ACL_USER_OBJ:
598			ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
599			break;
600		case ACL_GROUP_OBJ:
601			ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
602			break;
603		case ACL_OTHER:
604			ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
605			break;
606#ifdef ACL_TYPE_NFS4
607		case ACL_EVERYONE:
608			ae_tag = ARCHIVE_ENTRY_ACL_EVERYONE;
609			break;
610#endif
611		default:
612			/* Skip types that libarchive can't support. */
613			s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
614			continue;
615		}
616
617		// XXX acl type maps to allow/deny/audit/YYYY bits
618		// XXX acl_get_entry_type_np on FreeBSD returns EINVAL for
619		// non-NFSv4 ACLs
620		entry_acl_type = default_entry_acl_type;
621#ifdef ACL_TYPE_NFS4
622		r = acl_get_entry_type_np(acl_entry, &acl_type);
623		if (r == 0) {
624			switch (acl_type) {
625			case ACL_ENTRY_TYPE_ALLOW:
626				entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW;
627				break;
628			case ACL_ENTRY_TYPE_DENY:
629				entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_DENY;
630				break;
631			case ACL_ENTRY_TYPE_AUDIT:
632				entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_AUDIT;
633				break;
634			case ACL_ENTRY_TYPE_ALARM:
635				entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALARM;
636				break;
637			}
638		}
639
640		/*
641		 * Libarchive stores "flag" (NFSv4 inheritance bits)
642		 * in the ae_perm bitmap.
643		 */
644		// XXX acl_get_flagset_np on FreeBSD returns EINVAL for
645		// non-NFSv4 ACLs
646		r = acl_get_flagset_np(acl_entry, &acl_flagset);
647		if (r == 0) {
648	                for (i = 0; i < (int)(sizeof(acl_inherit_map) / sizeof(acl_inherit_map[0])); ++i) {
649				if (acl_get_flag_np(acl_flagset,
650						    acl_inherit_map[i].platform_inherit))
651					ae_perm |= acl_inherit_map[i].archive_inherit;
652                	}
653		}
654#endif
655
656		acl_get_permset(acl_entry, &acl_permset);
657		for (i = 0; i < (int)(sizeof(acl_perm_map) / sizeof(acl_perm_map[0])); ++i) {
658			/*
659			 * acl_get_perm() is spelled differently on different
660			 * platforms; see above.
661			 */
662			if (ACL_GET_PERM(acl_permset, acl_perm_map[i].platform_perm))
663				ae_perm |= acl_perm_map[i].archive_perm;
664		}
665
666		archive_entry_acl_add_entry(entry, entry_acl_type,
667					    ae_perm, ae_tag,
668					    ae_id, ae_name);
669
670		s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
671	}
672	return (ARCHIVE_OK);
673}
674#else
675static int
676setup_acls(struct archive_read_disk *a,
677    struct archive_entry *entry, int *fd)
678{
679	(void)a;      /* UNUSED */
680	(void)entry;  /* UNUSED */
681	(void)fd;     /* UNUSED */
682	return (ARCHIVE_OK);
683}
684#endif
685
686#if (HAVE_FGETXATTR && HAVE_FLISTXATTR && HAVE_LISTXATTR && \
687    HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR) || \
688    (HAVE_FGETEA && HAVE_FLISTEA && HAVE_LISTEA)
689
690/*
691 * Linux and AIX extended attribute support.
692 *
693 * TODO:  By using a stack-allocated buffer for the first
694 * call to getxattr(), we might be able to avoid the second
695 * call entirely.  We only need the second call if the
696 * stack-allocated buffer is too small.  But a modest buffer
697 * of 1024 bytes or so will often be big enough.  Same applies
698 * to listxattr().
699 */
700
701
702static int
703setup_xattr(struct archive_read_disk *a,
704    struct archive_entry *entry, const char *name, int fd)
705{
706	ssize_t size;
707	void *value = NULL;
708	const char *accpath;
709
710	accpath = archive_entry_sourcepath(entry);
711	if (accpath == NULL)
712		accpath = archive_entry_pathname(entry);
713
714#if HAVE_FGETXATTR
715	if (fd >= 0)
716		size = fgetxattr(fd, name, NULL, 0);
717	else if (!a->follow_symlinks)
718		size = lgetxattr(accpath, name, NULL, 0);
719	else
720		size = getxattr(accpath, name, NULL, 0);
721#elif HAVE_FGETEA
722	if (fd >= 0)
723		size = fgetea(fd, name, NULL, 0);
724	else if (!a->follow_symlinks)
725		size = lgetea(accpath, name, NULL, 0);
726	else
727		size = getea(accpath, name, NULL, 0);
728#endif
729
730	if (size == -1) {
731		archive_set_error(&a->archive, errno,
732		    "Couldn't query extended attribute");
733		return (ARCHIVE_WARN);
734	}
735
736	if (size > 0 && (value = malloc(size)) == NULL) {
737		archive_set_error(&a->archive, errno, "Out of memory");
738		return (ARCHIVE_FATAL);
739	}
740
741#if HAVE_FGETXATTR
742	if (fd >= 0)
743		size = fgetxattr(fd, name, value, size);
744	else if (!a->follow_symlinks)
745		size = lgetxattr(accpath, name, value, size);
746	else
747		size = getxattr(accpath, name, value, size);
748#elif HAVE_FGETEA
749	if (fd >= 0)
750		size = fgetea(fd, name, value, size);
751	else if (!a->follow_symlinks)
752		size = lgetea(accpath, name, value, size);
753	else
754		size = getea(accpath, name, value, size);
755#endif
756
757	if (size == -1) {
758		archive_set_error(&a->archive, errno,
759		    "Couldn't read extended attribute");
760		return (ARCHIVE_WARN);
761	}
762
763	archive_entry_xattr_add_entry(entry, name, value, size);
764
765	free(value);
766	return (ARCHIVE_OK);
767}
768
769static int
770setup_xattrs(struct archive_read_disk *a,
771    struct archive_entry *entry, int *fd)
772{
773	char *list, *p;
774	const char *path;
775	ssize_t list_size;
776
777	path = archive_entry_sourcepath(entry);
778	if (path == NULL)
779		path = archive_entry_pathname(entry);
780
781	if (*fd < 0 && a->tree != NULL) {
782		if (a->follow_symlinks ||
783		    archive_entry_filetype(entry) != AE_IFLNK)
784			*fd = a->open_on_current_dir(a->tree, path,
785				O_RDONLY | O_NONBLOCK);
786		if (*fd < 0) {
787			if (a->tree_enter_working_dir(a->tree) != 0) {
788				archive_set_error(&a->archive, errno,
789				    "Couldn't access %s", path);
790				return (ARCHIVE_FAILED);
791			}
792		}
793	}
794
795#if HAVE_FLISTXATTR
796	if (*fd >= 0)
797		list_size = flistxattr(*fd, NULL, 0);
798	else if (!a->follow_symlinks)
799		list_size = llistxattr(path, NULL, 0);
800	else
801		list_size = listxattr(path, NULL, 0);
802#elif HAVE_FLISTEA
803	if (*fd >= 0)
804		list_size = flistea(*fd, NULL, 0);
805	else if (!a->follow_symlinks)
806		list_size = llistea(path, NULL, 0);
807	else
808		list_size = listea(path, NULL, 0);
809#endif
810
811	if (list_size == -1) {
812		if (errno == ENOTSUP || errno == ENOSYS)
813			return (ARCHIVE_OK);
814		archive_set_error(&a->archive, errno,
815			"Couldn't list extended attributes");
816		return (ARCHIVE_WARN);
817	}
818
819	if (list_size == 0)
820		return (ARCHIVE_OK);
821
822	if ((list = malloc(list_size)) == NULL) {
823		archive_set_error(&a->archive, errno, "Out of memory");
824		return (ARCHIVE_FATAL);
825	}
826
827#if HAVE_FLISTXATTR
828	if (*fd >= 0)
829		list_size = flistxattr(*fd, list, list_size);
830	else if (!a->follow_symlinks)
831		list_size = llistxattr(path, list, list_size);
832	else
833		list_size = listxattr(path, list, list_size);
834#elif HAVE_FLISTEA
835	if (*fd >= 0)
836		list_size = flistea(*fd, list, list_size);
837	else if (!a->follow_symlinks)
838		list_size = llistea(path, list, list_size);
839	else
840		list_size = listea(path, list, list_size);
841#endif
842
843	if (list_size == -1) {
844		archive_set_error(&a->archive, errno,
845			"Couldn't retrieve extended attributes");
846		free(list);
847		return (ARCHIVE_WARN);
848	}
849
850	for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
851		if (strncmp(p, "system.", 7) == 0 ||
852				strncmp(p, "xfsroot.", 8) == 0)
853			continue;
854		setup_xattr(a, entry, p, *fd);
855	}
856
857	free(list);
858	return (ARCHIVE_OK);
859}
860
861#elif HAVE_EXTATTR_GET_FILE && HAVE_EXTATTR_LIST_FILE && \
862    HAVE_DECL_EXTATTR_NAMESPACE_USER
863
864/*
865 * FreeBSD extattr interface.
866 */
867
868/* TODO: Implement this.  Follow the Linux model above, but
869 * with FreeBSD-specific system calls, of course.  Be careful
870 * to not include the system extattrs that hold ACLs; we handle
871 * those separately.
872 */
873static int
874setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
875    int namespace, const char *name, const char *fullname, int fd);
876
877static int
878setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
879    int namespace, const char *name, const char *fullname, int fd)
880{
881	ssize_t size;
882	void *value = NULL;
883	const char *accpath;
884
885	accpath = archive_entry_sourcepath(entry);
886	if (accpath == NULL)
887		accpath = archive_entry_pathname(entry);
888
889	if (fd >= 0)
890		size = extattr_get_fd(fd, namespace, name, NULL, 0);
891	else if (!a->follow_symlinks)
892		size = extattr_get_link(accpath, namespace, name, NULL, 0);
893	else
894		size = extattr_get_file(accpath, namespace, name, NULL, 0);
895
896	if (size == -1) {
897		archive_set_error(&a->archive, errno,
898		    "Couldn't query extended attribute");
899		return (ARCHIVE_WARN);
900	}
901
902	if (size > 0 && (value = malloc(size)) == NULL) {
903		archive_set_error(&a->archive, errno, "Out of memory");
904		return (ARCHIVE_FATAL);
905	}
906
907	if (fd >= 0)
908		size = extattr_get_fd(fd, namespace, name, value, size);
909	else if (!a->follow_symlinks)
910		size = extattr_get_link(accpath, namespace, name, value, size);
911	else
912		size = extattr_get_file(accpath, namespace, name, value, size);
913
914	if (size == -1) {
915		free(value);
916		archive_set_error(&a->archive, errno,
917		    "Couldn't read extended attribute");
918		return (ARCHIVE_WARN);
919	}
920
921	archive_entry_xattr_add_entry(entry, fullname, value, size);
922
923	free(value);
924	return (ARCHIVE_OK);
925}
926
927static int
928setup_xattrs(struct archive_read_disk *a,
929    struct archive_entry *entry, int *fd)
930{
931	char buff[512];
932	char *list, *p;
933	ssize_t list_size;
934	const char *path;
935	int namespace = EXTATTR_NAMESPACE_USER;
936
937	path = archive_entry_sourcepath(entry);
938	if (path == NULL)
939		path = archive_entry_pathname(entry);
940
941	if (*fd < 0 && a->tree != NULL) {
942		if (a->follow_symlinks ||
943		    archive_entry_filetype(entry) != AE_IFLNK)
944			*fd = a->open_on_current_dir(a->tree, path,
945				O_RDONLY | O_NONBLOCK);
946		if (*fd < 0) {
947			if (a->tree_enter_working_dir(a->tree) != 0) {
948				archive_set_error(&a->archive, errno,
949				    "Couldn't access %s", path);
950				return (ARCHIVE_FAILED);
951			}
952		}
953	}
954
955	if (*fd >= 0)
956		list_size = extattr_list_fd(*fd, namespace, NULL, 0);
957	else if (!a->follow_symlinks)
958		list_size = extattr_list_link(path, namespace, NULL, 0);
959	else
960		list_size = extattr_list_file(path, namespace, NULL, 0);
961
962	if (list_size == -1 && errno == EOPNOTSUPP)
963		return (ARCHIVE_OK);
964	if (list_size == -1) {
965		archive_set_error(&a->archive, errno,
966			"Couldn't list extended attributes");
967		return (ARCHIVE_WARN);
968	}
969
970	if (list_size == 0)
971		return (ARCHIVE_OK);
972
973	if ((list = malloc(list_size)) == NULL) {
974		archive_set_error(&a->archive, errno, "Out of memory");
975		return (ARCHIVE_FATAL);
976	}
977
978	if (*fd >= 0)
979		list_size = extattr_list_fd(*fd, namespace, list, list_size);
980	else if (!a->follow_symlinks)
981		list_size = extattr_list_link(path, namespace, list, list_size);
982	else
983		list_size = extattr_list_file(path, namespace, list, list_size);
984
985	if (list_size == -1) {
986		archive_set_error(&a->archive, errno,
987			"Couldn't retrieve extended attributes");
988		free(list);
989		return (ARCHIVE_WARN);
990	}
991
992	p = list;
993	while ((p - list) < list_size) {
994		size_t len = 255 & (int)*p;
995		char *name;
996
997		strcpy(buff, "user.");
998		name = buff + strlen(buff);
999		memcpy(name, p + 1, len);
1000		name[len] = '\0';
1001		setup_xattr(a, entry, namespace, name, buff, *fd);
1002		p += 1 + len;
1003	}
1004
1005	free(list);
1006	return (ARCHIVE_OK);
1007}
1008
1009#else
1010
1011/*
1012 * Generic (stub) extended attribute support.
1013 */
1014static int
1015setup_xattrs(struct archive_read_disk *a,
1016    struct archive_entry *entry, int *fd)
1017{
1018	(void)a;     /* UNUSED */
1019	(void)entry; /* UNUSED */
1020	(void)fd;    /* UNUSED */
1021	return (ARCHIVE_OK);
1022}
1023
1024#endif
1025
1026#if defined(HAVE_LINUX_FIEMAP_H)
1027
1028/*
1029 * Linux sparse interface.
1030 *
1031 * The FIEMAP ioctl returns an "extent" for each physical allocation
1032 * on disk.  We need to process those to generate a more compact list
1033 * of logical file blocks.  We also need to be very careful to use
1034 * FIEMAP_FLAG_SYNC here, since there are reports that Linux sometimes
1035 * does not report allocations for newly-written data that hasn't
1036 * been synced to disk.
1037 *
1038 * It's important to return a minimal sparse file list because we want
1039 * to not trigger sparse file extensions if we don't have to, since
1040 * not all readers support them.
1041 */
1042
1043static int
1044setup_sparse(struct archive_read_disk *a,
1045    struct archive_entry *entry, int *fd)
1046{
1047	char buff[4096];
1048	struct fiemap *fm;
1049	struct fiemap_extent *fe;
1050	int64_t size;
1051	int count, do_fiemap, iters;
1052	int exit_sts = ARCHIVE_OK;
1053
1054	if (archive_entry_filetype(entry) != AE_IFREG
1055	    || archive_entry_size(entry) <= 0
1056	    || archive_entry_hardlink(entry) != NULL)
1057		return (ARCHIVE_OK);
1058
1059	if (*fd < 0) {
1060		const char *path;
1061
1062		path = archive_entry_sourcepath(entry);
1063		if (path == NULL)
1064			path = archive_entry_pathname(entry);
1065		if (a->tree != NULL)
1066			*fd = a->open_on_current_dir(a->tree, path,
1067				O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1068		else
1069			*fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1070		if (*fd < 0) {
1071			archive_set_error(&a->archive, errno,
1072			    "Can't open `%s'", path);
1073			return (ARCHIVE_FAILED);
1074		}
1075		__archive_ensure_cloexec_flag(*fd);
1076	}
1077
1078	/* Initialize buffer to avoid the error valgrind complains about. */
1079	memset(buff, 0, sizeof(buff));
1080	count = (sizeof(buff) - sizeof(*fm))/sizeof(*fe);
1081	fm = (struct fiemap *)buff;
1082	fm->fm_start = 0;
1083	fm->fm_length = ~0ULL;;
1084	fm->fm_flags = FIEMAP_FLAG_SYNC;
1085	fm->fm_extent_count = count;
1086	do_fiemap = 1;
1087	size = archive_entry_size(entry);
1088	for (iters = 0; ; ++iters) {
1089		int i, r;
1090
1091		r = ioctl(*fd, FS_IOC_FIEMAP, fm);
1092		if (r < 0) {
1093			/* When something error happens, it is better we
1094			 * should return ARCHIVE_OK because an earlier
1095			 * version(<2.6.28) cannot perfom FS_IOC_FIEMAP. */
1096			goto exit_setup_sparse;
1097		}
1098		if (fm->fm_mapped_extents == 0) {
1099			if (iters == 0) {
1100				/* Fully sparse file; insert a zero-length "data" entry */
1101				archive_entry_sparse_add_entry(entry, 0, 0);
1102			}
1103			break;
1104		}
1105		fe = fm->fm_extents;
1106		for (i = 0; i < (int)fm->fm_mapped_extents; i++, fe++) {
1107			if (!(fe->fe_flags & FIEMAP_EXTENT_UNWRITTEN)) {
1108				/* The fe_length of the last block does not
1109				 * adjust itself to its size files. */
1110				int64_t length = fe->fe_length;
1111				if (fe->fe_logical + length > (uint64_t)size)
1112					length -= fe->fe_logical + length - size;
1113				if (fe->fe_logical == 0 && length == size) {
1114					/* This is not sparse. */
1115					do_fiemap = 0;
1116					break;
1117				}
1118				if (length > 0)
1119					archive_entry_sparse_add_entry(entry,
1120					    fe->fe_logical, length);
1121			}
1122			if (fe->fe_flags & FIEMAP_EXTENT_LAST)
1123				do_fiemap = 0;
1124		}
1125		if (do_fiemap) {
1126			fe = fm->fm_extents + fm->fm_mapped_extents -1;
1127			fm->fm_start = fe->fe_logical + fe->fe_length;
1128		} else
1129			break;
1130	}
1131exit_setup_sparse:
1132	return (exit_sts);
1133}
1134
1135#elif defined(SEEK_HOLE) && defined(SEEK_DATA) && defined(_PC_MIN_HOLE_SIZE)
1136
1137/*
1138 * FreeBSD and Solaris sparse interface.
1139 */
1140
1141static int
1142setup_sparse(struct archive_read_disk *a,
1143    struct archive_entry *entry, int *fd)
1144{
1145	int64_t size;
1146	off_t initial_off; /* FreeBSD/Solaris only, so off_t okay here */
1147	off_t off_s, off_e; /* FreeBSD/Solaris only, so off_t okay here */
1148	int exit_sts = ARCHIVE_OK;
1149	int check_fully_sparse = 0;
1150
1151	if (archive_entry_filetype(entry) != AE_IFREG
1152	    || archive_entry_size(entry) <= 0
1153	    || archive_entry_hardlink(entry) != NULL)
1154		return (ARCHIVE_OK);
1155
1156	/* Does filesystem support the reporting of hole ? */
1157	if (*fd < 0 && a->tree != NULL) {
1158		const char *path;
1159
1160		path = archive_entry_sourcepath(entry);
1161		if (path == NULL)
1162			path = archive_entry_pathname(entry);
1163		*fd = a->open_on_current_dir(a->tree, path,
1164				O_RDONLY | O_NONBLOCK);
1165		if (*fd < 0) {
1166			archive_set_error(&a->archive, errno,
1167			    "Can't open `%s'", path);
1168			return (ARCHIVE_FAILED);
1169		}
1170	}
1171
1172	if (*fd >= 0) {
1173		if (fpathconf(*fd, _PC_MIN_HOLE_SIZE) <= 0)
1174			return (ARCHIVE_OK);
1175		initial_off = lseek(*fd, 0, SEEK_CUR);
1176		if (initial_off != 0)
1177			lseek(*fd, 0, SEEK_SET);
1178	} else {
1179		const char *path;
1180
1181		path = archive_entry_sourcepath(entry);
1182		if (path == NULL)
1183			path = archive_entry_pathname(entry);
1184
1185		if (pathconf(path, _PC_MIN_HOLE_SIZE) <= 0)
1186			return (ARCHIVE_OK);
1187		*fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1188		if (*fd < 0) {
1189			archive_set_error(&a->archive, errno,
1190			    "Can't open `%s'", path);
1191			return (ARCHIVE_FAILED);
1192		}
1193		__archive_ensure_cloexec_flag(*fd);
1194		initial_off = 0;
1195	}
1196
1197	off_s = 0;
1198	size = archive_entry_size(entry);
1199	while (off_s < size) {
1200		off_s = lseek(*fd, off_s, SEEK_DATA);
1201		if (off_s == (off_t)-1) {
1202			if (errno == ENXIO) {
1203				/* no more hole */
1204				if (archive_entry_sparse_count(entry) == 0) {
1205					/* Potentially a fully-sparse file. */
1206					check_fully_sparse = 1;
1207				}
1208				break;
1209			}
1210			archive_set_error(&a->archive, errno,
1211			    "lseek(SEEK_HOLE) failed");
1212			exit_sts = ARCHIVE_FAILED;
1213			goto exit_setup_sparse;
1214		}
1215		off_e = lseek(*fd, off_s, SEEK_HOLE);
1216		if (off_e == (off_t)-1) {
1217			if (errno == ENXIO) {
1218				off_e = lseek(*fd, 0, SEEK_END);
1219				if (off_e != (off_t)-1)
1220					break;/* no more data */
1221			}
1222			archive_set_error(&a->archive, errno,
1223			    "lseek(SEEK_DATA) failed");
1224			exit_sts = ARCHIVE_FAILED;
1225			goto exit_setup_sparse;
1226		}
1227		if (off_s == 0 && off_e == size)
1228			break;/* This is not spase. */
1229		archive_entry_sparse_add_entry(entry, off_s,
1230			off_e - off_s);
1231		off_s = off_e;
1232	}
1233
1234	if (check_fully_sparse) {
1235		if (lseek(*fd, 0, SEEK_HOLE) == 0 &&
1236			lseek(*fd, 0, SEEK_END) == size) {
1237			/* Fully sparse file; insert a zero-length "data" entry */
1238			archive_entry_sparse_add_entry(entry, 0, 0);
1239		}
1240	}
1241exit_setup_sparse:
1242	lseek(*fd, initial_off, SEEK_SET);
1243	return (exit_sts);
1244}
1245
1246#else
1247
1248/*
1249 * Generic (stub) sparse support.
1250 */
1251static int
1252setup_sparse(struct archive_read_disk *a,
1253    struct archive_entry *entry, int *fd)
1254{
1255	(void)a;     /* UNUSED */
1256	(void)entry; /* UNUSED */
1257	(void)fd;    /* UNUSED */
1258	return (ARCHIVE_OK);
1259}
1260
1261#endif
1262
1263#endif /* !defined(_WIN32) || defined(__CYGWIN__) */
1264
1265