archive_read_disk_entry_from_file.c 27 KB

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