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 int lnklen;
258
259 linkbuffer = malloc(linkbuffer_len + 1);
260 if (linkbuffer == NULL) {
261 archive_set_error(&a->archive, ENOMEM,
262 "Couldn't read link data");
263 return (ARCHIVE_FAILED);
264 }
265 if (a->tree != NULL) {
266 #ifdef HAVE_READLINKAT
267 lnklen = readlinkat(a->tree_current_dir_fd(a->tree),
268 path, linkbuffer, linkbuffer_len);
269 #else
270 if (a->tree_enter_working_dir(a->tree) != 0) {
271 archive_set_error(&a->archive, errno,
272 "Couldn't read link data");
273 free(linkbuffer);
274 return (ARCHIVE_FAILED);
275 }
276 lnklen = readlink(path, linkbuffer, linkbuffer_len);
277 #endif /* HAVE_READLINKAT */
278 } else
279 lnklen = readlink(path, linkbuffer, linkbuffer_len);
280 if (lnklen < 0) {
281 archive_set_error(&a->archive, errno,
282 "Couldn't read link data");
283 free(linkbuffer);
284 return (ARCHIVE_FAILED);
285 }
286 linkbuffer[lnklen] = '\0';
287 archive_entry_set_symlink(entry, linkbuffer);
288 free(linkbuffer);
289 }
290 #endif /* HAVE_READLINK || HAVE_READLINKAT */
291
292 r = 0;
293 if ((a->flags & ARCHIVE_READDISK_NO_ACL) == 0)
294 r = archive_read_disk_entry_setup_acls(a, entry, &fd);
295 if ((a->flags & ARCHIVE_READDISK_NO_XATTR) == 0) {
296 r1 = setup_xattrs(a, entry, &fd);
297 if (r1 < r)
298 r = r1;
299 }
300 if (a->flags & ARCHIVE_READDISK_MAC_COPYFILE) {
301 r1 = setup_mac_metadata(a, entry, &fd);
302 if (r1 < r)
303 r = r1;
304 }
305 if ((a->flags & ARCHIVE_READDISK_NO_SPARSE) == 0) {
306 r1 = setup_sparse(a, entry, &fd);
307 if (r1 < r)
308 r = r1;
309 }
310
311 /* If we opened the file earlier in this function, close it. */
312 if (initial_fd != fd)
313 close(fd);
314 return (r);
315 }
316
317 #if defined(__APPLE__) && defined(HAVE_COPYFILE_H)
318 /*
319 * The Mac OS "copyfile()" API copies the extended metadata for a
320 * file into a separate file in AppleDouble format (see RFC 1740).
321 *
322 * Mac OS tar and cpio implementations store this extended
323 * metadata as a separate entry just before the regular entry
324 * with a "._" prefix added to the filename.
325 *
326 * Note that this is currently done unconditionally; the tar program has
327 * an option to discard this information before the archive is written.
328 *
329 * TODO: If there's a failure, report it and return ARCHIVE_WARN.
330 */
331 static int
setup_mac_metadata(struct archive_read_disk * a,struct archive_entry * entry,int * fd)332 setup_mac_metadata(struct archive_read_disk *a,
333 struct archive_entry *entry, int *fd)
334 {
335 int tempfd = -1;
336 int copyfile_flags = COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR;
337 struct stat copyfile_stat;
338 int ret = ARCHIVE_OK;
339 void *buff = NULL;
340 int have_attrs;
341 const char *name, *tempdir;
342 struct archive_string tempfile;
343
344 (void)fd; /* UNUSED */
345
346 name = archive_read_disk_entry_setup_path(a, entry, NULL);
347 if (name == NULL)
348 return (ARCHIVE_WARN);
349
350 /* Short-circuit if there's nothing to do. */
351 have_attrs = copyfile(name, NULL, 0, copyfile_flags | COPYFILE_CHECK);
352 if (have_attrs == -1) {
353 archive_set_error(&a->archive, errno,
354 "Could not check extended attributes");
355 return (ARCHIVE_WARN);
356 }
357 if (have_attrs == 0)
358 return (ARCHIVE_OK);
359
360 tempdir = NULL;
361 if (issetugid() == 0)
362 tempdir = getenv("TMPDIR");
363 if (tempdir == NULL)
364 tempdir = _PATH_TMP;
365 archive_string_init(&tempfile);
366 archive_strcpy(&tempfile, tempdir);
367 archive_strcat(&tempfile, "tar.md.XXXXXX");
368 tempfd = mkstemp(tempfile.s);
369 if (tempfd < 0) {
370 archive_set_error(&a->archive, errno,
371 "Could not open extended attribute file");
372 ret = ARCHIVE_WARN;
373 goto cleanup;
374 }
375 __archive_ensure_cloexec_flag(tempfd);
376
377 /* XXX I wish copyfile() could pack directly to a memory
378 * buffer; that would avoid the temp file here. For that
379 * matter, it would be nice if fcopyfile() actually worked,
380 * that would reduce the many open/close races here. */
381 if (copyfile(name, tempfile.s, 0, copyfile_flags | COPYFILE_PACK)) {
382 archive_set_error(&a->archive, errno,
383 "Could not pack extended attributes");
384 ret = ARCHIVE_WARN;
385 goto cleanup;
386 }
387 if (fstat(tempfd, ©file_stat)) {
388 archive_set_error(&a->archive, errno,
389 "Could not check size of extended attributes");
390 ret = ARCHIVE_WARN;
391 goto cleanup;
392 }
393 buff = malloc(copyfile_stat.st_size);
394 if (buff == NULL) {
395 archive_set_error(&a->archive, errno,
396 "Could not allocate memory for extended attributes");
397 ret = ARCHIVE_WARN;
398 goto cleanup;
399 }
400 if (copyfile_stat.st_size != read(tempfd, buff, copyfile_stat.st_size)) {
401 archive_set_error(&a->archive, errno,
402 "Could not read extended attributes into memory");
403 ret = ARCHIVE_WARN;
404 goto cleanup;
405 }
406 archive_entry_copy_mac_metadata(entry, buff, copyfile_stat.st_size);
407
408 cleanup:
409 if (tempfd >= 0) {
410 close(tempfd);
411 unlink(tempfile.s);
412 }
413 archive_string_free(&tempfile);
414 free(buff);
415 return (ret);
416 }
417
418 #else
419
420 /*
421 * Stub implementation for non-Mac systems.
422 */
423 static int
setup_mac_metadata(struct archive_read_disk * a,struct archive_entry * entry,int * fd)424 setup_mac_metadata(struct archive_read_disk *a,
425 struct archive_entry *entry, int *fd)
426 {
427 (void)a; /* UNUSED */
428 (void)entry; /* UNUSED */
429 (void)fd; /* UNUSED */
430 return (ARCHIVE_OK);
431 }
432 #endif
433
434 #if ARCHIVE_XATTR_LINUX || ARCHIVE_XATTR_DARWIN || ARCHIVE_XATTR_AIX
435
436 /*
437 * Linux, Darwin and AIX extended attribute support.
438 *
439 * TODO: By using a stack-allocated buffer for the first
440 * call to getxattr(), we might be able to avoid the second
441 * call entirely. We only need the second call if the
442 * stack-allocated buffer is too small. But a modest buffer
443 * of 1024 bytes or so will often be big enough. Same applies
444 * to listxattr().
445 */
446
447
448 static int
setup_xattr(struct archive_read_disk * a,struct archive_entry * entry,const char * name,int fd,const char * accpath)449 setup_xattr(struct archive_read_disk *a,
450 struct archive_entry *entry, const char *name, int fd, const char *accpath)
451 {
452 ssize_t size;
453 void *value = NULL;
454
455
456 if (fd >= 0) {
457 #if ARCHIVE_XATTR_LINUX
458 size = fgetxattr(fd, name, NULL, 0);
459 #elif ARCHIVE_XATTR_DARWIN
460 size = fgetxattr(fd, name, NULL, 0, 0, 0);
461 #elif ARCHIVE_XATTR_AIX
462 size = fgetea(fd, name, NULL, 0);
463 #endif
464 } else if (!a->follow_symlinks) {
465 #if ARCHIVE_XATTR_LINUX
466 size = lgetxattr(accpath, name, NULL, 0);
467 #elif ARCHIVE_XATTR_DARWIN
468 size = getxattr(accpath, name, NULL, 0, 0, XATTR_NOFOLLOW);
469 #elif ARCHIVE_XATTR_AIX
470 size = lgetea(accpath, name, NULL, 0);
471 #endif
472 } else {
473 #if ARCHIVE_XATTR_LINUX
474 size = getxattr(accpath, name, NULL, 0);
475 #elif ARCHIVE_XATTR_DARWIN
476 size = getxattr(accpath, name, NULL, 0, 0, 0);
477 #elif ARCHIVE_XATTR_AIX
478 size = getea(accpath, name, NULL, 0);
479 #endif
480 }
481
482 if (size == -1) {
483 archive_set_error(&a->archive, errno,
484 "Couldn't query extended attribute");
485 return (ARCHIVE_WARN);
486 }
487
488 if (size > 0 && (value = malloc(size)) == NULL) {
489 archive_set_error(&a->archive, errno, "Out of memory");
490 return (ARCHIVE_FATAL);
491 }
492
493
494 if (fd >= 0) {
495 #if ARCHIVE_XATTR_LINUX
496 size = fgetxattr(fd, name, value, size);
497 #elif ARCHIVE_XATTR_DARWIN
498 size = fgetxattr(fd, name, value, size, 0, 0);
499 #elif ARCHIVE_XATTR_AIX
500 size = fgetea(fd, name, value, size);
501 #endif
502 } else if (!a->follow_symlinks) {
503 #if ARCHIVE_XATTR_LINUX
504 size = lgetxattr(accpath, name, value, size);
505 #elif ARCHIVE_XATTR_DARWIN
506 size = getxattr(accpath, name, value, size, 0, XATTR_NOFOLLOW);
507 #elif ARCHIVE_XATTR_AIX
508 size = lgetea(accpath, name, value, size);
509 #endif
510 } else {
511 #if ARCHIVE_XATTR_LINUX
512 size = getxattr(accpath, name, value, size);
513 #elif ARCHIVE_XATTR_DARWIN
514 size = getxattr(accpath, name, value, size, 0, 0);
515 #elif ARCHIVE_XATTR_AIX
516 size = getea(accpath, name, value, size);
517 #endif
518 }
519
520 if (size == -1) {
521 archive_set_error(&a->archive, errno,
522 "Couldn't read extended attribute");
523 return (ARCHIVE_WARN);
524 }
525
526 archive_entry_xattr_add_entry(entry, name, value, size);
527
528 free(value);
529 return (ARCHIVE_OK);
530 }
531
532 static int
setup_xattrs(struct archive_read_disk * a,struct archive_entry * entry,int * fd)533 setup_xattrs(struct archive_read_disk *a,
534 struct archive_entry *entry, int *fd)
535 {
536 char *list, *p;
537 const char *path;
538 ssize_t list_size;
539
540 path = NULL;
541
542 if (*fd < 0) {
543 path = archive_read_disk_entry_setup_path(a, entry, fd);
544 if (path == NULL)
545 return (ARCHIVE_WARN);
546 }
547
548 if (*fd >= 0) {
549 #if ARCHIVE_XATTR_LINUX
550 list_size = flistxattr(*fd, NULL, 0);
551 #elif ARCHIVE_XATTR_DARWIN
552 list_size = flistxattr(*fd, NULL, 0, 0);
553 #elif ARCHIVE_XATTR_AIX
554 list_size = flistea(*fd, NULL, 0);
555 #endif
556 } else if (!a->follow_symlinks) {
557 #if ARCHIVE_XATTR_LINUX
558 list_size = llistxattr(path, NULL, 0);
559 #elif ARCHIVE_XATTR_DARWIN
560 list_size = listxattr(path, NULL, 0, XATTR_NOFOLLOW);
561 #elif ARCHIVE_XATTR_AIX
562 list_size = llistea(path, NULL, 0);
563 #endif
564 } else {
565 #if ARCHIVE_XATTR_LINUX
566 list_size = listxattr(path, NULL, 0);
567 #elif ARCHIVE_XATTR_DARWIN
568 list_size = listxattr(path, NULL, 0, 0);
569 #elif ARCHIVE_XATTR_AIX
570 list_size = listea(path, NULL, 0);
571 #endif
572 }
573
574 if (list_size == -1) {
575 if (errno == ENOTSUP || errno == ENOSYS)
576 return (ARCHIVE_OK);
577 archive_set_error(&a->archive, errno,
578 "Couldn't list extended attributes");
579 return (ARCHIVE_WARN);
580 }
581
582 if (list_size == 0)
583 return (ARCHIVE_OK);
584
585 if ((list = malloc(list_size)) == NULL) {
586 archive_set_error(&a->archive, errno, "Out of memory");
587 return (ARCHIVE_FATAL);
588 }
589
590 if (*fd >= 0) {
591 #if ARCHIVE_XATTR_LINUX
592 list_size = flistxattr(*fd, list, list_size);
593 #elif ARCHIVE_XATTR_DARWIN
594 list_size = flistxattr(*fd, list, list_size, 0);
595 #elif ARCHIVE_XATTR_AIX
596 list_size = flistea(*fd, list, list_size);
597 #endif
598 } else if (!a->follow_symlinks) {
599 #if ARCHIVE_XATTR_LINUX
600 list_size = llistxattr(path, list, list_size);
601 #elif ARCHIVE_XATTR_DARWIN
602 list_size = listxattr(path, list, list_size, XATTR_NOFOLLOW);
603 #elif ARCHIVE_XATTR_AIX
604 list_size = llistea(path, list, list_size);
605 #endif
606 } else {
607 #if ARCHIVE_XATTR_LINUX
608 list_size = listxattr(path, list, list_size);
609 #elif ARCHIVE_XATTR_DARWIN
610 list_size = listxattr(path, list, list_size, 0);
611 #elif ARCHIVE_XATTR_AIX
612 list_size = listea(path, list, list_size);
613 #endif
614 }
615
616 if (list_size == -1) {
617 archive_set_error(&a->archive, errno,
618 "Couldn't retrieve extended attributes");
619 free(list);
620 return (ARCHIVE_WARN);
621 }
622
623 for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
624 #if ARCHIVE_XATTR_LINUX
625 /* Linux: skip POSIX.1e ACL extended attributes */
626 if (strncmp(p, "system.", 7) == 0 &&
627 (strcmp(p + 7, "posix_acl_access") == 0 ||
628 strcmp(p + 7, "posix_acl_default") == 0))
629 continue;
630 if (strncmp(p, "trusted.SGI_", 12) == 0 &&
631 (strcmp(p + 12, "ACL_DEFAULT") == 0 ||
632 strcmp(p + 12, "ACL_FILE") == 0))
633 continue;
634
635 /* Linux: xfsroot namespace is obsolete and unsupported */
636 if (strncmp(p, "xfsroot.", 8) == 0)
637 continue;
638 #endif
639 setup_xattr(a, entry, p, *fd, path);
640 }
641
642 free(list);
643 return (ARCHIVE_OK);
644 }
645
646 #elif ARCHIVE_XATTR_FREEBSD
647
648 /*
649 * FreeBSD extattr interface.
650 */
651
652 /* TODO: Implement this. Follow the Linux model above, but
653 * with FreeBSD-specific system calls, of course. Be careful
654 * to not include the system extattrs that hold ACLs; we handle
655 * those separately.
656 */
657 static int
658 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
659 int namespace, const char *name, const char *fullname, int fd,
660 const char *path);
661
662 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)663 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
664 int namespace, const char *name, const char *fullname, int fd,
665 const char *accpath)
666 {
667 ssize_t size;
668 void *value = NULL;
669
670 if (fd >= 0)
671 size = extattr_get_fd(fd, namespace, name, NULL, 0);
672 else if (!a->follow_symlinks)
673 size = extattr_get_link(accpath, namespace, name, NULL, 0);
674 else
675 size = extattr_get_file(accpath, namespace, name, NULL, 0);
676
677 if (size == -1) {
678 archive_set_error(&a->archive, errno,
679 "Couldn't query extended attribute");
680 return (ARCHIVE_WARN);
681 }
682
683 if (size > 0 && (value = malloc(size)) == NULL) {
684 archive_set_error(&a->archive, errno, "Out of memory");
685 return (ARCHIVE_FATAL);
686 }
687
688 if (fd >= 0)
689 size = extattr_get_fd(fd, namespace, name, value, size);
690 else if (!a->follow_symlinks)
691 size = extattr_get_link(accpath, namespace, name, value, size);
692 else
693 size = extattr_get_file(accpath, namespace, name, value, size);
694
695 if (size == -1) {
696 free(value);
697 archive_set_error(&a->archive, errno,
698 "Couldn't read extended attribute");
699 return (ARCHIVE_WARN);
700 }
701
702 archive_entry_xattr_add_entry(entry, fullname, value, size);
703
704 free(value);
705 return (ARCHIVE_OK);
706 }
707
708 static int
setup_xattrs_namespace(struct archive_read_disk * a,struct archive_entry * entry,int * fd,int namespace)709 setup_xattrs_namespace(struct archive_read_disk *a,
710 struct archive_entry *entry, int *fd, int namespace)
711 {
712 char buff[512];
713 char *list, *p;
714 ssize_t list_size;
715 const char *path;
716
717 path = NULL;
718
719 if (*fd < 0) {
720 path = archive_read_disk_entry_setup_path(a, entry, fd);
721 if (path == NULL)
722 return (ARCHIVE_WARN);
723 }
724
725 if (*fd >= 0)
726 list_size = extattr_list_fd(*fd, namespace, NULL, 0);
727 else if (!a->follow_symlinks)
728 list_size = extattr_list_link(path, namespace, NULL, 0);
729 else
730 list_size = extattr_list_file(path, namespace, NULL, 0);
731
732 if (list_size == -1 && errno == EOPNOTSUPP)
733 return (ARCHIVE_OK);
734 if (list_size == -1 && errno == EPERM)
735 return (ARCHIVE_OK);
736 if (list_size == -1) {
737 archive_set_error(&a->archive, errno,
738 "Couldn't list extended attributes");
739 return (ARCHIVE_WARN);
740 }
741
742 if (list_size == 0)
743 return (ARCHIVE_OK);
744
745 if ((list = malloc(list_size)) == NULL) {
746 archive_set_error(&a->archive, errno, "Out of memory");
747 return (ARCHIVE_FATAL);
748 }
749
750 if (*fd >= 0)
751 list_size = extattr_list_fd(*fd, namespace, list, list_size);
752 else if (!a->follow_symlinks)
753 list_size = extattr_list_link(path, namespace, list, list_size);
754 else
755 list_size = extattr_list_file(path, namespace, list, list_size);
756
757 if (list_size == -1) {
758 archive_set_error(&a->archive, errno,
759 "Couldn't retrieve extended attributes");
760 free(list);
761 return (ARCHIVE_WARN);
762 }
763
764 p = list;
765 while ((p - list) < list_size) {
766 size_t len = 255 & (int)*p;
767 char *name;
768
769 if (namespace == EXTATTR_NAMESPACE_SYSTEM) {
770 if (!strcmp(p + 1, "nfs4.acl") ||
771 !strcmp(p + 1, "posix1e.acl_access") ||
772 !strcmp(p + 1, "posix1e.acl_default")) {
773 p += 1 + len;
774 continue;
775 }
776 strcpy(buff, "system.");
777 } else {
778 strcpy(buff, "user.");
779 }
780 name = buff + strlen(buff);
781 memcpy(name, p + 1, len);
782 name[len] = '\0';
783 setup_xattr(a, entry, namespace, name, buff, *fd, path);
784 p += 1 + len;
785 }
786
787 free(list);
788 return (ARCHIVE_OK);
789 }
790
791 static int
setup_xattrs(struct archive_read_disk * a,struct archive_entry * entry,int * fd)792 setup_xattrs(struct archive_read_disk *a,
793 struct archive_entry *entry, int *fd)
794 {
795 int namespaces[2];
796 int i, res;
797
798 namespaces[0] = EXTATTR_NAMESPACE_USER;
799 namespaces[1] = EXTATTR_NAMESPACE_SYSTEM;
800
801 for (i = 0; i < 2; i++) {
802 res = setup_xattrs_namespace(a, entry, fd,
803 namespaces[i]);
804 switch (res) {
805 case (ARCHIVE_OK):
806 case (ARCHIVE_WARN):
807 break;
808 default:
809 return (res);
810 }
811 }
812
813 return (ARCHIVE_OK);
814 }
815
816 #else
817
818 /*
819 * Generic (stub) extended attribute support.
820 */
821 static int
setup_xattrs(struct archive_read_disk * a,struct archive_entry * entry,int * fd)822 setup_xattrs(struct archive_read_disk *a,
823 struct archive_entry *entry, int *fd)
824 {
825 (void)a; /* UNUSED */
826 (void)entry; /* UNUSED */
827 (void)fd; /* UNUSED */
828 return (ARCHIVE_OK);
829 }
830
831 #endif
832
833 #if defined(HAVE_LINUX_FIEMAP_H)
834
835 /*
836 * Linux FIEMAP sparse interface.
837 *
838 * The FIEMAP ioctl returns an "extent" for each physical allocation
839 * on disk. We need to process those to generate a more compact list
840 * of logical file blocks. We also need to be very careful to use
841 * FIEMAP_FLAG_SYNC here, since there are reports that Linux sometimes
842 * does not report allocations for newly-written data that hasn't
843 * been synced to disk.
844 *
845 * It's important to return a minimal sparse file list because we want
846 * to not trigger sparse file extensions if we don't have to, since
847 * not all readers support them.
848 */
849
850 static int
setup_sparse_fiemap(struct archive_read_disk * a,struct archive_entry * entry,int * fd)851 setup_sparse_fiemap(struct archive_read_disk *a,
852 struct archive_entry *entry, int *fd)
853 {
854 char buff[4096];
855 struct fiemap *fm;
856 struct fiemap_extent *fe;
857 int64_t size;
858 int count, do_fiemap, iters;
859 int exit_sts = ARCHIVE_OK;
860 const char *path;
861
862 if (archive_entry_filetype(entry) != AE_IFREG
863 || archive_entry_size(entry) <= 0
864 || archive_entry_hardlink(entry) != NULL)
865 return (ARCHIVE_OK);
866
867 if (*fd < 0) {
868 path = archive_read_disk_entry_setup_path(a, entry, NULL);
869 if (path == NULL)
870 return (ARCHIVE_FAILED);
871
872 if (a->tree != NULL)
873 *fd = a->open_on_current_dir(a->tree, path,
874 O_RDONLY | O_NONBLOCK | O_CLOEXEC);
875 else
876 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
877 if (*fd < 0) {
878 archive_set_error(&a->archive, errno,
879 "Can't open `%s'", path);
880 return (ARCHIVE_FAILED);
881 }
882 __archive_ensure_cloexec_flag(*fd);
883 }
884
885 /* Initialize buffer to avoid the error valgrind complains about. */
886 memset(buff, 0, sizeof(buff));
887 count = (sizeof(buff) - sizeof(*fm))/sizeof(*fe);
888 fm = (struct fiemap *)buff;
889 fm->fm_start = 0;
890 fm->fm_length = ~0ULL;;
891 fm->fm_flags = FIEMAP_FLAG_SYNC;
892 fm->fm_extent_count = count;
893 do_fiemap = 1;
894 size = archive_entry_size(entry);
895 for (iters = 0; ; ++iters) {
896 int i, r;
897
898 r = ioctl(*fd, FS_IOC_FIEMAP, fm);
899 if (r < 0) {
900 /* When something error happens, it is better we
901 * should return ARCHIVE_OK because an earlier
902 * version(<2.6.28) cannot perform FS_IOC_FIEMAP. */
903 goto exit_setup_sparse_fiemap;
904 }
905 if (fm->fm_mapped_extents == 0) {
906 if (iters == 0) {
907 /* Fully sparse file; insert a zero-length "data" entry */
908 archive_entry_sparse_add_entry(entry, 0, 0);
909 }
910 break;
911 }
912 fe = fm->fm_extents;
913 for (i = 0; i < (int)fm->fm_mapped_extents; i++, fe++) {
914 if (!(fe->fe_flags & FIEMAP_EXTENT_UNWRITTEN)) {
915 /* The fe_length of the last block does not
916 * adjust itself to its size files. */
917 int64_t length = fe->fe_length;
918 if (fe->fe_logical + length > (uint64_t)size)
919 length -= fe->fe_logical + length - size;
920 if (fe->fe_logical == 0 && length == size) {
921 /* This is not sparse. */
922 do_fiemap = 0;
923 break;
924 }
925 if (length > 0)
926 archive_entry_sparse_add_entry(entry,
927 fe->fe_logical, length);
928 }
929 if (fe->fe_flags & FIEMAP_EXTENT_LAST)
930 do_fiemap = 0;
931 }
932 if (do_fiemap) {
933 fe = fm->fm_extents + fm->fm_mapped_extents -1;
934 fm->fm_start = fe->fe_logical + fe->fe_length;
935 } else
936 break;
937 }
938 exit_setup_sparse_fiemap:
939 return (exit_sts);
940 }
941
942 #if !defined(SEEK_HOLE) || !defined(SEEK_DATA)
943 static int
setup_sparse(struct archive_read_disk * a,struct archive_entry * entry,int * fd)944 setup_sparse(struct archive_read_disk *a,
945 struct archive_entry *entry, int *fd)
946 {
947 return setup_sparse_fiemap(a, entry, fd);
948 }
949 #endif
950 #endif /* defined(HAVE_LINUX_FIEMAP_H) */
951
952 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
953
954 /*
955 * SEEK_HOLE sparse interface (FreeBSD, Linux, Solaris)
956 */
957
958 static int
setup_sparse(struct archive_read_disk * a,struct archive_entry * entry,int * fd)959 setup_sparse(struct archive_read_disk *a,
960 struct archive_entry *entry, int *fd)
961 {
962 int64_t size;
963 off_t initial_off;
964 off_t off_s, off_e;
965 int exit_sts = ARCHIVE_OK;
966 int check_fully_sparse = 0;
967 const char *path;
968
969 if (archive_entry_filetype(entry) != AE_IFREG
970 || archive_entry_size(entry) <= 0
971 || archive_entry_hardlink(entry) != NULL)
972 return (ARCHIVE_OK);
973
974 /* Does filesystem support the reporting of hole ? */
975 if (*fd < 0)
976 path = archive_read_disk_entry_setup_path(a, entry, fd);
977 else
978 path = NULL;
979
980 if (*fd >= 0) {
981 #ifdef _PC_MIN_HOLE_SIZE
982 if (fpathconf(*fd, _PC_MIN_HOLE_SIZE) <= 0)
983 return (ARCHIVE_OK);
984 #endif
985 initial_off = lseek(*fd, 0, SEEK_CUR);
986 if (initial_off != 0)
987 lseek(*fd, 0, SEEK_SET);
988 } else {
989 if (path == NULL)
990 return (ARCHIVE_FAILED);
991 #ifdef _PC_MIN_HOLE_SIZE
992 if (pathconf(path, _PC_MIN_HOLE_SIZE) <= 0)
993 return (ARCHIVE_OK);
994 #endif
995 *fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
996 if (*fd < 0) {
997 archive_set_error(&a->archive, errno,
998 "Can't open `%s'", path);
999 return (ARCHIVE_FAILED);
1000 }
1001 __archive_ensure_cloexec_flag(*fd);
1002 initial_off = 0;
1003 }
1004
1005 #ifndef _PC_MIN_HOLE_SIZE
1006 /* Check if the underlying filesystem supports seek hole */
1007 off_s = lseek(*fd, 0, SEEK_HOLE);
1008 if (off_s < 0)
1009 #if defined(HAVE_LINUX_FIEMAP_H)
1010 return setup_sparse_fiemap(a, entry, fd);
1011 #else
1012 goto exit_setup_sparse;
1013 #endif
1014 else if (off_s > 0)
1015 lseek(*fd, 0, SEEK_SET);
1016 #endif
1017
1018 off_s = 0;
1019 size = archive_entry_size(entry);
1020 while (off_s < size) {
1021 off_s = lseek(*fd, off_s, SEEK_DATA);
1022 if (off_s == (off_t)-1) {
1023 if (errno == ENXIO) {
1024 /* no more hole */
1025 if (archive_entry_sparse_count(entry) == 0) {
1026 /* Potentially a fully-sparse file. */
1027 check_fully_sparse = 1;
1028 }
1029 break;
1030 }
1031 archive_set_error(&a->archive, errno,
1032 "lseek(SEEK_HOLE) failed");
1033 exit_sts = ARCHIVE_FAILED;
1034 goto exit_setup_sparse;
1035 }
1036 off_e = lseek(*fd, off_s, SEEK_HOLE);
1037 if (off_e == (off_t)-1) {
1038 if (errno == ENXIO) {
1039 off_e = lseek(*fd, 0, SEEK_END);
1040 if (off_e != (off_t)-1)
1041 break;/* no more data */
1042 }
1043 archive_set_error(&a->archive, errno,
1044 "lseek(SEEK_DATA) failed");
1045 exit_sts = ARCHIVE_FAILED;
1046 goto exit_setup_sparse;
1047 }
1048 if (off_s == 0 && off_e == size)
1049 break;/* This is not sparse. */
1050 archive_entry_sparse_add_entry(entry, off_s,
1051 off_e - off_s);
1052 off_s = off_e;
1053 }
1054
1055 if (check_fully_sparse) {
1056 if (lseek(*fd, 0, SEEK_HOLE) == 0 &&
1057 lseek(*fd, 0, SEEK_END) == size) {
1058 /* Fully sparse file; insert a zero-length "data" entry */
1059 archive_entry_sparse_add_entry(entry, 0, 0);
1060 }
1061 }
1062 exit_setup_sparse:
1063 lseek(*fd, initial_off, SEEK_SET);
1064 return (exit_sts);
1065 }
1066
1067 #elif !defined(HAVE_LINUX_FIEMAP_H)
1068
1069 /*
1070 * Generic (stub) sparse support.
1071 */
1072 static int
setup_sparse(struct archive_read_disk * a,struct archive_entry * entry,int * fd)1073 setup_sparse(struct archive_read_disk *a,
1074 struct archive_entry *entry, int *fd)
1075 {
1076 (void)a; /* UNUSED */
1077 (void)entry; /* UNUSED */
1078 (void)fd; /* UNUSED */
1079 return (ARCHIVE_OK);
1080 }
1081
1082 #endif
1083
1084 #endif /* !defined(_WIN32) || defined(__CYGWIN__) */
1085
1086