archive_read_disk_entry_from_file.c 27 KB

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