xref: /freebsd/contrib/libarchive/libarchive/archive_read_disk_entry_from_file.c (revision 185becb1e1bd2657c156f78aeb52edac05ba5fb5)
1 /*-
2  * Copyright (c) 2003-2009 Tim Kientzle
3  * Copyright (c) 2010-2012 Michihiro NAKAJIMA
4  * Copyright (c) 2016 Martin Matuska
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "archive_platform.h"
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 #include <sys/types.h>
35 #endif
36 #ifdef HAVE_SYS_EXTATTR_H
37 #include <sys/extattr.h>
38 #endif
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
41 #endif
42 #ifdef HAVE_SYS_PARAM_H
43 #include <sys/param.h>
44 #endif
45 #ifdef HAVE_SYS_STAT_H
46 #include <sys/stat.h>
47 #endif
48 #if defined(HAVE_SYS_XATTR_H)
49 #include <sys/xattr.h>
50 #elif defined(HAVE_ATTR_XATTR_H)
51 #include <attr/xattr.h>
52 #endif
53 #ifdef HAVE_SYS_EA_H
54 #include <sys/ea.h>
55 #endif
56 #ifdef HAVE_COPYFILE_H
57 #include <copyfile.h>
58 #endif
59 #ifdef HAVE_ERRNO_H
60 #include <errno.h>
61 #endif
62 #ifdef HAVE_FCNTL_H
63 #include <fcntl.h>
64 #endif
65 #ifdef HAVE_LIMITS_H
66 #include <limits.h>
67 #endif
68 #ifdef HAVE_LINUX_TYPES_H
69 #include <linux/types.h>
70 #endif
71 #ifdef HAVE_LINUX_FIEMAP_H
72 #include <linux/fiemap.h>
73 #endif
74 #ifdef HAVE_LINUX_FS_H
75 #include <linux/fs.h>
76 #endif
77 /*
78  * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
79  * As the include guards don't agree, the order of include is important.
80  */
81 #ifdef HAVE_LINUX_EXT2_FS_H
82 #include <linux/ext2_fs.h>      /* for Linux file flags */
83 #endif
84 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
85 #include <ext2fs/ext2_fs.h>     /* Linux file flags, broken on Cygwin */
86 #endif
87 #ifdef HAVE_PATHS_H
88 #include <paths.h>
89 #endif
90 #ifdef HAVE_UNISTD_H
91 #include <unistd.h>
92 #endif
93 
94 #include "archive.h"
95 #include "archive_entry.h"
96 #include "archive_private.h"
97 #include "archive_read_disk_private.h"
98 
99 #ifndef O_CLOEXEC
100 #define O_CLOEXEC	0
101 #endif
102 
103 static int setup_mac_metadata(struct archive_read_disk *,
104     struct archive_entry *, int *fd);
105 #ifdef ARCHIVE_XATTR_FREEBSD
106 static int setup_xattrs_namespace(struct archive_read_disk *,
107     struct archive_entry *, int *, int);
108 #endif
109 static int setup_xattrs(struct archive_read_disk *,
110     struct archive_entry *, int *fd);
111 static int setup_sparse(struct archive_read_disk *,
112     struct archive_entry *, int *fd);
113 #if defined(HAVE_LINUX_FIEMAP_H)
114 static int setup_sparse_fiemap(struct archive_read_disk *,
115     struct archive_entry *, int *fd);
116 #endif
117 
118 #if !ARCHIVE_ACL_SUPPORT
119 int
archive_read_disk_entry_setup_acls(struct archive_read_disk * a,struct archive_entry * entry,int * fd)120 archive_read_disk_entry_setup_acls(struct archive_read_disk *a,
121     struct archive_entry *entry, int *fd)
122 {
123 	(void)a;      /* UNUSED */
124 	(void)entry;  /* UNUSED */
125 	(void)fd;     /* UNUSED */
126 	return (ARCHIVE_OK);
127 }
128 #endif
129 
130 /*
131  * Enter working directory and return working pathname of archive_entry.
132  * If a pointer to an integer is provided and its value is below zero
133  * open a file descriptor on this pathname.
134  */
135 const char *
archive_read_disk_entry_setup_path(struct archive_read_disk * a,struct archive_entry * entry,int * fd)136 archive_read_disk_entry_setup_path(struct archive_read_disk *a,
137     struct archive_entry *entry, int *fd)
138 {
139 	const char *path;
140 
141 	path = archive_entry_sourcepath(entry);
142 
143 	if (path == NULL || (a->tree != NULL &&
144 	    a->tree_enter_working_dir(a->tree) != 0))
145 		path = archive_entry_pathname(entry);
146 	if (path == NULL) {
147 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
148 		   "Couldn't determine path");
149 	} else if (fd != NULL && *fd < 0 && a->tree != NULL &&
150 	    (a->follow_symlinks || archive_entry_filetype(entry) != AE_IFLNK)) {
151 		*fd = a->open_on_current_dir(a->tree, path,
152 		    O_RDONLY | O_NONBLOCK);
153 	}
154 	return (path);
155 }
156 
157 int
archive_read_disk_entry_from_file(struct archive * _a,struct archive_entry * entry,int fd,const struct stat * st)158 archive_read_disk_entry_from_file(struct archive *_a,
159     struct archive_entry *entry,
160     int fd,
161     const struct stat *st)
162 {
163 	struct archive_read_disk *a = (struct archive_read_disk *)_a;
164 	const char *path, *name;
165 	struct stat s;
166 	int initial_fd = fd;
167 	int r, r1;
168 
169 	archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY,
170 		"archive_read_disk_entry_from_file");
171 
172 	archive_clear_error(_a);
173 	path = archive_entry_sourcepath(entry);
174 	if (path == NULL)
175 		path = archive_entry_pathname(entry);
176 
177 	if (a->tree == NULL) {
178 		if (st == NULL) {
179 #if HAVE_FSTAT
180 			if (fd >= 0) {
181 				if (fstat(fd, &s) != 0) {
182 					archive_set_error(&a->archive, errno,
183 					    "Can't fstat");
184 					return (ARCHIVE_FAILED);
185 				}
186 			} else
187 #endif
188 #if HAVE_LSTAT
189 			if (!a->follow_symlinks) {
190 				if (lstat(path, &s) != 0) {
191 					archive_set_error(&a->archive, errno,
192 					    "Can't lstat %s", path);
193 					return (ARCHIVE_FAILED);
194 				}
195 			} else
196 #endif
197 			if (la_stat(path, &s) != 0) {
198 				archive_set_error(&a->archive, errno,
199 				    "Can't stat %s", path);
200 				return (ARCHIVE_FAILED);
201 			}
202 			st = &s;
203 		}
204 		archive_entry_copy_stat(entry, st);
205 	}
206 
207 	/* Lookup uname/gname */
208 	name = archive_read_disk_uname(_a, archive_entry_uid(entry));
209 	if (name != NULL)
210 		archive_entry_copy_uname(entry, name);
211 	name = archive_read_disk_gname(_a, archive_entry_gid(entry));
212 	if (name != NULL)
213 		archive_entry_copy_gname(entry, name);
214 
215 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
216 	/* On FreeBSD, we get flags for free with the stat. */
217 	/* TODO: Does this belong in copy_stat()? */
218 	if ((a->flags & ARCHIVE_READDISK_NO_FFLAGS) == 0 && st->st_flags != 0)
219 		archive_entry_set_fflags(entry, st->st_flags, 0);
220 #endif
221 
222 #if (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \
223     (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS))
224 	/* Linux requires an extra ioctl to pull the flags.  Although
225 	 * this is an extra step, it has a nice side-effect: We get an
226 	 * open file descriptor which we can use in the subsequent lookups. */
227 	if ((a->flags & ARCHIVE_READDISK_NO_FFLAGS) == 0 &&
228 	    (S_ISREG(st->st_mode) || S_ISDIR(st->st_mode))) {
229 		if (fd < 0) {
230 			if (a->tree != NULL)
231 				fd = a->open_on_current_dir(a->tree, path,
232 					O_RDONLY | O_NONBLOCK | O_CLOEXEC);
233 			else
234 				fd = open(path, O_RDONLY | O_NONBLOCK |
235 						O_CLOEXEC);
236 			__archive_ensure_cloexec_flag(fd);
237 		}
238 		if (fd >= 0) {
239 			int stflags;
240 			r = ioctl(fd,
241 #if defined(FS_IOC_GETFLAGS)
242 			    FS_IOC_GETFLAGS,
243 #else
244 			    EXT2_IOC_GETFLAGS,
245 #endif
246 			    &stflags);
247 			if (r == 0 && stflags != 0)
248 				archive_entry_set_fflags(entry, stflags, 0);
249 		}
250 	}
251 #endif
252 
253 #if defined(HAVE_READLINK) || defined(HAVE_READLINKAT)
254 	if (S_ISLNK(st->st_mode)) {
255 		size_t linkbuffer_len = st->st_size;
256 		char *linkbuffer;
257 		ssize_t lnklen;
258 
259 		if (st->st_size >= SSIZE_MAX) {
260 			archive_set_error(&a->archive, ENOMEM,
261 			    "Couldn't read link data");
262 			return (ARCHIVE_FAILED);
263 		}
264 		linkbuffer = malloc(linkbuffer_len + 1);
265 		if (linkbuffer == NULL) {
266 			archive_set_error(&a->archive, ENOMEM,
267 			    "Couldn't read link data");
268 			return (ARCHIVE_FAILED);
269 		}
270 		if (a->tree != NULL) {
271 #ifdef HAVE_READLINKAT
272 			lnklen = readlinkat(a->tree_current_dir_fd(a->tree),
273 			    path, linkbuffer, linkbuffer_len);
274 #else
275 			if (a->tree_enter_working_dir(a->tree) != 0) {
276 				archive_set_error(&a->archive, errno,
277 				    "Couldn't read link data");
278 				free(linkbuffer);
279 				return (ARCHIVE_FAILED);
280 			}
281 			lnklen = readlink(path, linkbuffer, linkbuffer_len);
282 #endif /* HAVE_READLINKAT */
283 		} else
284 			lnklen = readlink(path, linkbuffer, linkbuffer_len);
285 		if (lnklen < 0) {
286 			archive_set_error(&a->archive, errno,
287 			    "Couldn't read link data");
288 			free(linkbuffer);
289 			return (ARCHIVE_FAILED);
290 		}
291 		linkbuffer[lnklen] = '\0';
292 		archive_entry_set_symlink(entry, linkbuffer);
293 		free(linkbuffer);
294 	}
295 #endif /* HAVE_READLINK || HAVE_READLINKAT */
296 
297 	r = 0;
298 	if ((a->flags & ARCHIVE_READDISK_NO_ACL) == 0)
299 		r = archive_read_disk_entry_setup_acls(a, entry, &fd);
300 	if ((a->flags & ARCHIVE_READDISK_NO_XATTR) == 0) {
301 		r1 = setup_xattrs(a, entry, &fd);
302 		if (r1 < r)
303 			r = r1;
304 	}
305 	if (a->flags & ARCHIVE_READDISK_MAC_COPYFILE) {
306 		r1 = setup_mac_metadata(a, entry, &fd);
307 		if (r1 < r)
308 			r = r1;
309 	}
310 	if ((a->flags & ARCHIVE_READDISK_NO_SPARSE) == 0) {
311 		r1 = setup_sparse(a, entry, &fd);
312 		if (r1 < r)
313 			r = r1;
314 	}
315 
316 	/* If we opened the file earlier in this function, close it. */
317 	if (initial_fd != fd)
318 		close(fd);
319 	return (r);
320 }
321 
322 #if defined(__APPLE__) && defined(HAVE_COPYFILE_H)
323 /*
324  * The Mac OS "copyfile()" API copies the extended metadata for a
325  * file into a separate file in AppleDouble format (see RFC 1740).
326  *
327  * Mac OS tar and cpio implementations store this extended
328  * metadata as a separate entry just before the regular entry
329  * with a "._" prefix added to the filename.
330  *
331  * Note that this is currently done unconditionally; the tar program has
332  * an option to discard this information before the archive is written.
333  *
334  * TODO: If there's a failure, report it and return ARCHIVE_WARN.
335  */
336 static int
setup_mac_metadata(struct archive_read_disk * a,struct archive_entry * entry,int * fd)337 setup_mac_metadata(struct archive_read_disk *a,
338     struct archive_entry *entry, int *fd)
339 {
340 	int tempfd = -1;
341 	int copyfile_flags = COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR;
342 	struct stat copyfile_stat;
343 	int ret = ARCHIVE_OK;
344 	void *buff = NULL;
345 	int have_attrs;
346 	const char *name;
347 	struct archive_string tempfile;
348 
349 	(void)fd; /* UNUSED */
350 
351 	name = archive_read_disk_entry_setup_path(a, entry, NULL);
352 	if (name == NULL)
353 		return (ARCHIVE_WARN);
354 
355 	/* Short-circuit if there's nothing to do. */
356 	have_attrs = copyfile(name, NULL, 0, copyfile_flags | COPYFILE_CHECK);
357 	if (have_attrs == -1) {
358 		archive_set_error(&a->archive, errno,
359 			"Could not check extended attributes");
360 		return (ARCHIVE_WARN);
361 	}
362 	if (have_attrs == 0)
363 		return (ARCHIVE_OK);
364 
365 	archive_string_init(&tempfile);
366 	archive_strcpy(&tempfile, name);
367 	archive_string_dirname(&tempfile);
368 	archive_strcat(&tempfile, "/tar.XXXXXXXX");
369 	tempfd = __archive_mkstemp(tempfile.s);
370 	if (tempfd < 0) {
371 		archive_set_error(&a->archive, errno,
372 		    "Could not open extended attribute file");
373 		ret = ARCHIVE_WARN;
374 		goto cleanup;
375 	}
376 	__archive_ensure_cloexec_flag(tempfd);
377 
378 	/* XXX I wish copyfile() could pack directly to a memory
379 	 * buffer; that would avoid the temp file here.  For that
380 	 * matter, it would be nice if fcopyfile() actually worked,
381 	 * that would reduce the many open/close races here. */
382 	if (copyfile(name, tempfile.s, 0, copyfile_flags | COPYFILE_PACK)) {
383 		archive_set_error(&a->archive, errno,
384 		    "Could not pack extended attributes");
385 		ret = ARCHIVE_WARN;
386 		goto cleanup;
387 	}
388 	if (fstat(tempfd, &copyfile_stat)) {
389 		archive_set_error(&a->archive, errno,
390 		    "Could not check size of extended attributes");
391 		ret = ARCHIVE_WARN;
392 		goto cleanup;
393 	}
394 	buff = malloc(copyfile_stat.st_size);
395 	if (buff == NULL) {
396 		archive_set_error(&a->archive, errno,
397 		    "Could not allocate memory for extended attributes");
398 		ret = ARCHIVE_WARN;
399 		goto cleanup;
400 	}
401 	if (copyfile_stat.st_size != read(tempfd, buff, copyfile_stat.st_size)) {
402 		archive_set_error(&a->archive, errno,
403 		    "Could not read extended attributes into memory");
404 		ret = ARCHIVE_WARN;
405 		goto cleanup;
406 	}
407 	archive_entry_copy_mac_metadata(entry, buff, copyfile_stat.st_size);
408 
409 cleanup:
410 	if (tempfd >= 0) {
411 		close(tempfd);
412 		unlink(tempfile.s);
413 	}
414 	archive_string_free(&tempfile);
415 	free(buff);
416 	return (ret);
417 }
418 
419 #else
420 
421 /*
422  * Stub implementation for non-Mac systems.
423  */
424 static int
setup_mac_metadata(struct archive_read_disk * a,struct archive_entry * entry,int * fd)425 setup_mac_metadata(struct archive_read_disk *a,
426     struct archive_entry *entry, int *fd)
427 {
428 	(void)a; /* UNUSED */
429 	(void)entry; /* UNUSED */
430 	(void)fd; /* UNUSED */
431 	return (ARCHIVE_OK);
432 }
433 #endif
434 
435 #if ARCHIVE_XATTR_LINUX || ARCHIVE_XATTR_DARWIN || ARCHIVE_XATTR_AIX
436 
437 /*
438  * Linux, Darwin and AIX extended attribute support.
439  *
440  * TODO:  By using a stack-allocated buffer for the first
441  * call to getxattr(), we might be able to avoid the second
442  * call entirely.  We only need the second call if the
443  * stack-allocated buffer is too small.  But a modest buffer
444  * of 1024 bytes or so will often be big enough.  Same applies
445  * to listxattr().
446  */
447 
448 
449 static int
setup_xattr(struct archive_read_disk * a,struct archive_entry * entry,const char * name,int fd,const char * accpath)450 setup_xattr(struct archive_read_disk *a,
451     struct archive_entry *entry, const char *name, int fd, const char *accpath)
452 {
453 	ssize_t size;
454 	void *value = NULL;
455 
456 
457 	if (fd >= 0) {
458 #if ARCHIVE_XATTR_LINUX
459 		size = fgetxattr(fd, name, NULL, 0);
460 #elif ARCHIVE_XATTR_DARWIN
461 		size = fgetxattr(fd, name, NULL, 0, 0, 0);
462 #elif ARCHIVE_XATTR_AIX
463 		size = fgetea(fd, name, NULL, 0);
464 #endif
465 	} else if (!a->follow_symlinks) {
466 #if ARCHIVE_XATTR_LINUX
467 		size = lgetxattr(accpath, name, NULL, 0);
468 #elif ARCHIVE_XATTR_DARWIN
469 		size = getxattr(accpath, name, NULL, 0, 0, XATTR_NOFOLLOW);
470 #elif ARCHIVE_XATTR_AIX
471 		size = lgetea(accpath, name, NULL, 0);
472 #endif
473 	} else {
474 #if ARCHIVE_XATTR_LINUX
475 		size = getxattr(accpath, name, NULL, 0);
476 #elif ARCHIVE_XATTR_DARWIN
477 		size = getxattr(accpath, name, NULL, 0, 0, 0);
478 #elif ARCHIVE_XATTR_AIX
479 		size = getea(accpath, name, NULL, 0);
480 #endif
481 	}
482 
483 	if (size == -1) {
484 		archive_set_error(&a->archive, errno,
485 		    "Couldn't query extended attribute");
486 		return (ARCHIVE_WARN);
487 	}
488 
489 	if (size > 0 && (value = malloc(size)) == NULL) {
490 		archive_set_error(&a->archive, errno, "Out of memory");
491 		return (ARCHIVE_FATAL);
492 	}
493 
494 
495 	if (fd >= 0) {
496 #if ARCHIVE_XATTR_LINUX
497 		size = fgetxattr(fd, name, value, size);
498 #elif ARCHIVE_XATTR_DARWIN
499 		size = fgetxattr(fd, name, value, size, 0, 0);
500 #elif ARCHIVE_XATTR_AIX
501 		size = fgetea(fd, name, value, size);
502 #endif
503 	} else if (!a->follow_symlinks) {
504 #if ARCHIVE_XATTR_LINUX
505 		size = lgetxattr(accpath, name, value, size);
506 #elif ARCHIVE_XATTR_DARWIN
507 		size = getxattr(accpath, name, value, size, 0, XATTR_NOFOLLOW);
508 #elif ARCHIVE_XATTR_AIX
509 		size = lgetea(accpath, name, value, size);
510 #endif
511 	} else {
512 #if ARCHIVE_XATTR_LINUX
513 		size = getxattr(accpath, name, value, size);
514 #elif ARCHIVE_XATTR_DARWIN
515 		size = getxattr(accpath, name, value, size, 0, 0);
516 #elif ARCHIVE_XATTR_AIX
517 		size = getea(accpath, name, value, size);
518 #endif
519 	}
520 
521 	if (size == -1) {
522 		archive_set_error(&a->archive, errno,
523 		    "Couldn't read extended attribute");
524 		free(value);
525 		return (ARCHIVE_WARN);
526 	}
527 
528 	archive_entry_xattr_add_entry(entry, name, value, size);
529 
530 	free(value);
531 	return (ARCHIVE_OK);
532 }
533 
534 static int
setup_xattrs(struct archive_read_disk * a,struct archive_entry * entry,int * fd)535 setup_xattrs(struct archive_read_disk *a,
536     struct archive_entry *entry, int *fd)
537 {
538 	char *list, *p;
539 	const char *path;
540 	ssize_t list_size;
541 
542 	path = NULL;
543 
544 	if (*fd < 0) {
545 		path = archive_read_disk_entry_setup_path(a, entry, fd);
546 		if (path == NULL)
547 			return (ARCHIVE_WARN);
548 	}
549 
550 	if (*fd >= 0) {
551 #if ARCHIVE_XATTR_LINUX
552 		list_size = flistxattr(*fd, NULL, 0);
553 #elif ARCHIVE_XATTR_DARWIN
554 		list_size = flistxattr(*fd, NULL, 0, 0);
555 #elif ARCHIVE_XATTR_AIX
556 		list_size = flistea(*fd, NULL, 0);
557 #endif
558 	} else if (!a->follow_symlinks) {
559 #if ARCHIVE_XATTR_LINUX
560 		list_size = llistxattr(path, NULL, 0);
561 #elif ARCHIVE_XATTR_DARWIN
562 		list_size = listxattr(path, NULL, 0, XATTR_NOFOLLOW);
563 #elif ARCHIVE_XATTR_AIX
564 		list_size = llistea(path, NULL, 0);
565 #endif
566 	} else {
567 #if ARCHIVE_XATTR_LINUX
568 		list_size = listxattr(path, NULL, 0);
569 #elif ARCHIVE_XATTR_DARWIN
570 		list_size = listxattr(path, NULL, 0, 0);
571 #elif ARCHIVE_XATTR_AIX
572 		list_size = listea(path, NULL, 0);
573 #endif
574 	}
575 
576 	if (list_size == -1) {
577 		if (errno == ENOTSUP || errno == ENOSYS)
578 			return (ARCHIVE_OK);
579 		archive_set_error(&a->archive, errno,
580 			"Couldn't list extended attributes");
581 		return (ARCHIVE_WARN);
582 	}
583 
584 	if (list_size == 0)
585 		return (ARCHIVE_OK);
586 
587 	if ((list = malloc(list_size)) == NULL) {
588 		archive_set_error(&a->archive, errno, "Out of memory");
589 		return (ARCHIVE_FATAL);
590 	}
591 
592 	if (*fd >= 0) {
593 #if ARCHIVE_XATTR_LINUX
594 		list_size = flistxattr(*fd, list, list_size);
595 #elif ARCHIVE_XATTR_DARWIN
596 		list_size = flistxattr(*fd, list, list_size, 0);
597 #elif ARCHIVE_XATTR_AIX
598 		list_size = flistea(*fd, list, list_size);
599 #endif
600 	} else if (!a->follow_symlinks) {
601 #if ARCHIVE_XATTR_LINUX
602 		list_size = llistxattr(path, list, list_size);
603 #elif ARCHIVE_XATTR_DARWIN
604 		list_size = listxattr(path, list, list_size, XATTR_NOFOLLOW);
605 #elif ARCHIVE_XATTR_AIX
606 		list_size = llistea(path, list, list_size);
607 #endif
608 	} else {
609 #if ARCHIVE_XATTR_LINUX
610 		list_size = listxattr(path, list, list_size);
611 #elif ARCHIVE_XATTR_DARWIN
612 		list_size = listxattr(path, list, list_size, 0);
613 #elif ARCHIVE_XATTR_AIX
614 		list_size = listea(path, list, list_size);
615 #endif
616 	}
617 
618 	if (list_size == -1) {
619 		archive_set_error(&a->archive, errno,
620 			"Couldn't retrieve extended attributes");
621 		free(list);
622 		return (ARCHIVE_WARN);
623 	}
624 
625 	for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
626 #if ARCHIVE_XATTR_LINUX
627 		/* Linux: skip POSIX.1e ACL extended attributes */
628 		if (strncmp(p, "system.", 7) == 0 &&
629 		   (strcmp(p + 7, "posix_acl_access") == 0 ||
630 		    strcmp(p + 7, "posix_acl_default") == 0))
631 			continue;
632 		if (strncmp(p, "trusted.SGI_", 12) == 0 &&
633 		   (strcmp(p + 12, "ACL_DEFAULT") == 0 ||
634 		    strcmp(p + 12, "ACL_FILE") == 0))
635 			continue;
636 
637 		/* Linux: xfsroot namespace is obsolete and unsupported */
638 		if (strncmp(p, "xfsroot.", 8) == 0)
639 			continue;
640 #endif
641 		setup_xattr(a, entry, p, *fd, path);
642 	}
643 
644 	free(list);
645 	return (ARCHIVE_OK);
646 }
647 
648 #elif ARCHIVE_XATTR_FREEBSD
649 
650 /*
651  * FreeBSD extattr interface.
652  */
653 
654 /* TODO: Implement this.  Follow the Linux model above, but
655  * with FreeBSD-specific system calls, of course.  Be careful
656  * to not include the system extattrs that hold ACLs; we handle
657  * those separately.
658  */
659 static int
660 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
661     int namespace, const char *name, const char *fullname, int fd,
662     const char *path);
663 
664 static int
setup_xattr(struct archive_read_disk * a,struct archive_entry * entry,int namespace,const char * name,const char * fullname,int fd,const char * accpath)665 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
666     int namespace, const char *name, const char *fullname, int fd,
667     const char *accpath)
668 {
669 	ssize_t size;
670 	void *value = NULL;
671 
672 	if (fd >= 0)
673 		size = extattr_get_fd(fd, namespace, name, NULL, 0);
674 	else if (!a->follow_symlinks)
675 		size = extattr_get_link(accpath, namespace, name, NULL, 0);
676 	else
677 		size = extattr_get_file(accpath, namespace, name, NULL, 0);
678 
679 	if (size == -1) {
680 		archive_set_error(&a->archive, errno,
681 		    "Couldn't query extended attribute");
682 		return (ARCHIVE_WARN);
683 	}
684 
685 	if (size > 0 && (value = malloc(size)) == NULL) {
686 		archive_set_error(&a->archive, errno, "Out of memory");
687 		return (ARCHIVE_FATAL);
688 	}
689 
690 	if (fd >= 0)
691 		size = extattr_get_fd(fd, namespace, name, value, size);
692 	else if (!a->follow_symlinks)
693 		size = extattr_get_link(accpath, namespace, name, value, size);
694 	else
695 		size = extattr_get_file(accpath, namespace, name, value, size);
696 
697 	if (size == -1) {
698 		free(value);
699 		archive_set_error(&a->archive, errno,
700 		    "Couldn't read extended attribute");
701 		return (ARCHIVE_WARN);
702 	}
703 
704 	archive_entry_xattr_add_entry(entry, fullname, value, size);
705 
706 	free(value);
707 	return (ARCHIVE_OK);
708 }
709 
710 static int
setup_xattrs_namespace(struct archive_read_disk * a,struct archive_entry * entry,int * fd,int namespace)711 setup_xattrs_namespace(struct archive_read_disk *a,
712     struct archive_entry *entry, int *fd, int namespace)
713 {
714 	char buff[512];
715 	char *list, *p;
716 	ssize_t list_size;
717 	const char *path;
718 
719 	path = NULL;
720 
721 	if (*fd < 0) {
722 		path = archive_read_disk_entry_setup_path(a, entry, fd);
723 		if (path == NULL)
724 			return (ARCHIVE_WARN);
725 	}
726 
727 	if (*fd >= 0)
728 		list_size = extattr_list_fd(*fd, namespace, NULL, 0);
729 	else if (!a->follow_symlinks)
730 		list_size = extattr_list_link(path, namespace, NULL, 0);
731 	else
732 		list_size = extattr_list_file(path, namespace, NULL, 0);
733 
734 	if (list_size == -1 && errno == EOPNOTSUPP)
735 		return (ARCHIVE_OK);
736 	if (list_size == -1 && errno == EPERM)
737 		return (ARCHIVE_OK);
738 	if (list_size == -1) {
739 		archive_set_error(&a->archive, errno,
740 			"Couldn't list extended attributes");
741 		return (ARCHIVE_WARN);
742 	}
743 
744 	if (list_size == 0)
745 		return (ARCHIVE_OK);
746 
747 	if ((list = malloc(list_size)) == NULL) {
748 		archive_set_error(&a->archive, errno, "Out of memory");
749 		return (ARCHIVE_FATAL);
750 	}
751 
752 	if (*fd >= 0)
753 		list_size = extattr_list_fd(*fd, namespace, list, list_size);
754 	else if (!a->follow_symlinks)
755 		list_size = extattr_list_link(path, namespace, list, list_size);
756 	else
757 		list_size = extattr_list_file(path, namespace, list, list_size);
758 
759 	if (list_size == -1) {
760 		archive_set_error(&a->archive, errno,
761 			"Couldn't retrieve extended attributes");
762 		free(list);
763 		return (ARCHIVE_WARN);
764 	}
765 
766 	p = list;
767 	while ((p - list) < list_size) {
768 		size_t len = 255 & (int)*p;
769 		char *name;
770 
771 		if (namespace == EXTATTR_NAMESPACE_SYSTEM) {
772 			if (!strcmp(p + 1, "nfs4.acl") ||
773 			    !strcmp(p + 1, "posix1e.acl_access") ||
774 			    !strcmp(p + 1, "posix1e.acl_default")) {
775 				p += 1 + len;
776 				continue;
777 			}
778 			strcpy(buff, "system.");
779 		} else {
780 			strcpy(buff, "user.");
781 		}
782 		name = buff + strlen(buff);
783 		memcpy(name, p + 1, len);
784 		name[len] = '\0';
785 		setup_xattr(a, entry, namespace, name, buff, *fd, path);
786 		p += 1 + len;
787 	}
788 
789 	free(list);
790 	return (ARCHIVE_OK);
791 }
792 
793 static int
setup_xattrs(struct archive_read_disk * a,struct archive_entry * entry,int * fd)794 setup_xattrs(struct archive_read_disk *a,
795     struct archive_entry *entry, int *fd)
796 {
797 	int namespaces[2];
798 	int i, res;
799 
800 	namespaces[0] = EXTATTR_NAMESPACE_USER;
801 	namespaces[1] = EXTATTR_NAMESPACE_SYSTEM;
802 
803 	for (i = 0; i < 2; i++) {
804 		res = setup_xattrs_namespace(a, entry, fd,
805 		    namespaces[i]);
806 		switch (res) {
807 			case (ARCHIVE_OK):
808 			case (ARCHIVE_WARN):
809 				break;
810 			default:
811 				return (res);
812 		}
813 	}
814 
815 	return (ARCHIVE_OK);
816 }
817 
818 #else
819 
820 /*
821  * Generic (stub) extended attribute support.
822  */
823 static int
setup_xattrs(struct archive_read_disk * a,struct archive_entry * entry,int * fd)824 setup_xattrs(struct archive_read_disk *a,
825     struct archive_entry *entry, int *fd)
826 {
827 	(void)a;     /* UNUSED */
828 	(void)entry; /* UNUSED */
829 	(void)fd;    /* UNUSED */
830 	return (ARCHIVE_OK);
831 }
832 
833 #endif
834 
835 #if defined(HAVE_LINUX_FIEMAP_H)
836 
837 /*
838  * Linux FIEMAP sparse interface.
839  *
840  * The FIEMAP ioctl returns an "extent" for each physical allocation
841  * on disk.  We need to process those to generate a more compact list
842  * of logical file blocks.  We also need to be very careful to use
843  * FIEMAP_FLAG_SYNC here, since there are reports that Linux sometimes
844  * does not report allocations for newly-written data that hasn't
845  * been synced to disk.
846  *
847  * It's important to return a minimal sparse file list because we want
848  * to not trigger sparse file extensions if we don't have to, since
849  * not all readers support them.
850  */
851 
852 static int
setup_sparse_fiemap(struct archive_read_disk * a,struct archive_entry * entry,int * fd)853 setup_sparse_fiemap(struct archive_read_disk *a,
854     struct archive_entry *entry, int *fd)
855 {
856 	char buff[4096];
857 	struct fiemap *fm;
858 	struct fiemap_extent *fe;
859 	int64_t size;
860 	int count, do_fiemap, iters;
861 	int exit_sts = ARCHIVE_OK;
862 	const char *path;
863 
864 	if (archive_entry_filetype(entry) != AE_IFREG
865 	    || archive_entry_size(entry) <= 0
866 	    || archive_entry_hardlink(entry) != NULL)
867 		return (ARCHIVE_OK);
868 
869 	if (*fd < 0) {
870 		path = archive_read_disk_entry_setup_path(a, entry, NULL);
871 		if (path == NULL)
872 			return (ARCHIVE_FAILED);
873 
874 		if (a->tree != NULL)
875 			*fd = a->open_on_current_dir(a->tree, path,
876 				O_RDONLY | O_NONBLOCK | O_CLOEXEC);
877 		else
878 			*fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
879 		if (*fd < 0) {
880 			archive_set_error(&a->archive, errno,
881 			    "Can't open `%s'", path);
882 			return (ARCHIVE_FAILED);
883 		}
884 		__archive_ensure_cloexec_flag(*fd);
885 	}
886 
887 	/* Initialize buffer to avoid the error valgrind complains about. */
888 	memset(buff, 0, sizeof(buff));
889 	count = (sizeof(buff) - sizeof(*fm))/sizeof(*fe);
890 	fm = (struct fiemap *)buff;
891 	fm->fm_start = 0;
892 	fm->fm_length = ~0ULL;
893 	fm->fm_flags = FIEMAP_FLAG_SYNC;
894 	fm->fm_extent_count = count;
895 	do_fiemap = 1;
896 	size = archive_entry_size(entry);
897 	for (iters = 0; ; ++iters) {
898 		int i, r;
899 
900 		r = ioctl(*fd, FS_IOC_FIEMAP, fm);
901 		if (r < 0) {
902 			/* When some error happens, it is better we
903 			 * should return ARCHIVE_OK because an earlier
904 			 * version(<2.6.28) cannot perform FS_IOC_FIEMAP. */
905 			goto exit_setup_sparse_fiemap;
906 		}
907 		if (fm->fm_mapped_extents == 0) {
908 			if (iters == 0) {
909 				/* Fully sparse file; insert a zero-length "data" entry */
910 				archive_entry_sparse_add_entry(entry, 0, 0);
911 			}
912 			break;
913 		}
914 		fe = fm->fm_extents;
915 		for (i = 0; i < (int)fm->fm_mapped_extents; i++, fe++) {
916 			if (!(fe->fe_flags & FIEMAP_EXTENT_UNWRITTEN)) {
917 				/* The fe_length of the last block does not
918 				 * adjust itself to its size files. */
919 				int64_t length = fe->fe_length;
920 				if (fe->fe_logical + length > (uint64_t)size)
921 					length -= fe->fe_logical + length - size;
922 				if (fe->fe_logical == 0 && length == size) {
923 					/* This is not sparse. */
924 					do_fiemap = 0;
925 					break;
926 				}
927 				if (length > 0)
928 					archive_entry_sparse_add_entry(entry,
929 					    fe->fe_logical, length);
930 			}
931 			if (fe->fe_flags & FIEMAP_EXTENT_LAST)
932 				do_fiemap = 0;
933 		}
934 		if (do_fiemap) {
935 			fe = fm->fm_extents + fm->fm_mapped_extents -1;
936 			fm->fm_start = fe->fe_logical + fe->fe_length;
937 		} else
938 			break;
939 	}
940 exit_setup_sparse_fiemap:
941 	return (exit_sts);
942 }
943 
944 #if !defined(SEEK_HOLE) || !defined(SEEK_DATA)
945 static int
setup_sparse(struct archive_read_disk * a,struct archive_entry * entry,int * fd)946 setup_sparse(struct archive_read_disk *a,
947     struct archive_entry *entry, int *fd)
948 {
949 	return setup_sparse_fiemap(a, entry, fd);
950 }
951 #endif
952 #endif	/* defined(HAVE_LINUX_FIEMAP_H) */
953 
954 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
955 
956 /*
957  * SEEK_HOLE sparse interface (FreeBSD, Linux, Solaris)
958  */
959 
960 static int
setup_sparse(struct archive_read_disk * a,struct archive_entry * entry,int * fd)961 setup_sparse(struct archive_read_disk *a,
962     struct archive_entry *entry, int *fd)
963 {
964 	int64_t size;
965 	off_t initial_off;
966 	off_t off_s, off_e;
967 	int exit_sts = ARCHIVE_OK;
968 	int check_fully_sparse = 0;
969 	const char *path;
970 
971 	if (archive_entry_filetype(entry) != AE_IFREG
972 	    || archive_entry_size(entry) <= 0
973 	    || archive_entry_hardlink(entry) != NULL)
974 		return (ARCHIVE_OK);
975 
976 	/* Does filesystem support the reporting of hole ? */
977 	if (*fd < 0)
978 		path = archive_read_disk_entry_setup_path(a, entry, fd);
979 	else
980 		path = NULL;
981 
982 	if (*fd >= 0) {
983 #ifdef _PC_MIN_HOLE_SIZE
984 		if (fpathconf(*fd, _PC_MIN_HOLE_SIZE) <= 0)
985 			return (ARCHIVE_OK);
986 #endif
987 		initial_off = lseek(*fd, 0, SEEK_CUR);
988 		if (initial_off != 0)
989 			lseek(*fd, 0, SEEK_SET);
990 	} else {
991 		if (path == NULL)
992 			return (ARCHIVE_FAILED);
993 #ifdef _PC_MIN_HOLE_SIZE
994 		if (pathconf(path, _PC_MIN_HOLE_SIZE) <= 0)
995 			return (ARCHIVE_OK);
996 #endif
997 		*fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
998 		if (*fd < 0) {
999 			archive_set_error(&a->archive, errno,
1000 			    "Can't open `%s'", path);
1001 			return (ARCHIVE_FAILED);
1002 		}
1003 		__archive_ensure_cloexec_flag(*fd);
1004 		initial_off = 0;
1005 	}
1006 
1007 #ifndef _PC_MIN_HOLE_SIZE
1008 	/* Check if the underlying filesystem supports seek hole */
1009 	off_s = lseek(*fd, 0, SEEK_HOLE);
1010 	if (off_s < 0)
1011 #if defined(HAVE_LINUX_FIEMAP_H)
1012 		return setup_sparse_fiemap(a, entry, fd);
1013 #else
1014 		goto exit_setup_sparse;
1015 #endif
1016 	else if (off_s > 0)
1017 		lseek(*fd, 0, SEEK_SET);
1018 #endif
1019 
1020 	off_s = 0;
1021 	size = archive_entry_size(entry);
1022 	while (off_s < size) {
1023 		off_s = lseek(*fd, off_s, SEEK_DATA);
1024 		if (off_s == (off_t)-1) {
1025 			if (errno == ENXIO) {
1026 				/* no more hole */
1027 				if (archive_entry_sparse_count(entry) == 0) {
1028 					/* Potentially a fully-sparse file. */
1029 					check_fully_sparse = 1;
1030 				}
1031 				break;
1032 			}
1033 			archive_set_error(&a->archive, errno,
1034 			    "lseek(SEEK_HOLE) failed");
1035 			exit_sts = ARCHIVE_FAILED;
1036 			goto exit_setup_sparse;
1037 		}
1038 		off_e = lseek(*fd, off_s, SEEK_HOLE);
1039 		if (off_e == (off_t)-1) {
1040 			if (errno == ENXIO) {
1041 				off_e = lseek(*fd, 0, SEEK_END);
1042 				if (off_e != (off_t)-1)
1043 					break;/* no more data */
1044 			}
1045 			archive_set_error(&a->archive, errno,
1046 			    "lseek(SEEK_DATA) failed");
1047 			exit_sts = ARCHIVE_FAILED;
1048 			goto exit_setup_sparse;
1049 		}
1050 		if (off_s == 0 && off_e == size)
1051 			break;/* This is not sparse. */
1052 		archive_entry_sparse_add_entry(entry, off_s,
1053 			off_e - off_s);
1054 		off_s = off_e;
1055 	}
1056 
1057 	if (check_fully_sparse) {
1058 		if (lseek(*fd, 0, SEEK_HOLE) == 0 &&
1059 			lseek(*fd, 0, SEEK_END) == size) {
1060 			/* Fully sparse file; insert a zero-length "data" entry */
1061 			archive_entry_sparse_add_entry(entry, 0, 0);
1062 		}
1063 	}
1064 exit_setup_sparse:
1065 	lseek(*fd, initial_off, SEEK_SET);
1066 	return (exit_sts);
1067 }
1068 
1069 #elif !defined(HAVE_LINUX_FIEMAP_H)
1070 
1071 /*
1072  * Generic (stub) sparse support.
1073  */
1074 static int
setup_sparse(struct archive_read_disk * a,struct archive_entry * entry,int * fd)1075 setup_sparse(struct archive_read_disk *a,
1076     struct archive_entry *entry, int *fd)
1077 {
1078 	(void)a;     /* UNUSED */
1079 	(void)entry; /* UNUSED */
1080 	(void)fd;    /* UNUSED */
1081 	return (ARCHIVE_OK);
1082 }
1083 
1084 #endif
1085 
1086 #endif /* !defined(_WIN32) || defined(__CYGWIN__) */
1087