archive_read_disk_entry_from_file.c 28 KB

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