archive_read_disk_posix.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606
  1. /*-
  2. * Copyright (c) 2003-2009 Tim Kientzle
  3. * Copyright (c) 2010-2012 Michihiro NAKAJIMA
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer
  11. * in this position and unchanged.
  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. /* This is the tree-walking code for POSIX systems. */
  28. #if !defined(_WIN32) || defined(__CYGWIN__)
  29. #include "archive_platform.h"
  30. __FBSDID("$FreeBSD$");
  31. #ifdef HAVE_SYS_PARAM_H
  32. #include <sys/param.h>
  33. #endif
  34. #ifdef HAVE_SYS_MOUNT_H
  35. #include <sys/mount.h>
  36. #endif
  37. #ifdef HAVE_SYS_STAT_H
  38. #include <sys/stat.h>
  39. #endif
  40. #ifdef HAVE_SYS_STATFS_H
  41. #include <sys/statfs.h>
  42. #endif
  43. #ifdef HAVE_SYS_STATVFS_H
  44. #include <sys/statvfs.h>
  45. #endif
  46. #ifdef HAVE_SYS_TIME_H
  47. #include <sys/time.h>
  48. #endif
  49. #ifdef HAVE_LINUX_MAGIC_H
  50. #include <linux/magic.h>
  51. #endif
  52. #ifdef HAVE_LINUX_FS_H
  53. #include <linux/fs.h>
  54. #endif
  55. /*
  56. * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
  57. * As the include guards don't agree, the order of include is important.
  58. */
  59. #ifdef HAVE_LINUX_EXT2_FS_H
  60. #include <linux/ext2_fs.h> /* for Linux file flags */
  61. #endif
  62. #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
  63. #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
  64. #endif
  65. #ifdef HAVE_DIRECT_H
  66. #include <direct.h>
  67. #endif
  68. #ifdef HAVE_DIRENT_H
  69. #include <dirent.h>
  70. #endif
  71. #ifdef HAVE_ERRNO_H
  72. #include <errno.h>
  73. #endif
  74. #ifdef HAVE_FCNTL_H
  75. #include <fcntl.h>
  76. #endif
  77. #ifdef HAVE_LIMITS_H
  78. #include <limits.h>
  79. #endif
  80. #ifdef HAVE_STDLIB_H
  81. #include <stdlib.h>
  82. #endif
  83. #ifdef HAVE_STRING_H
  84. #include <string.h>
  85. #endif
  86. #ifdef HAVE_UNISTD_H
  87. #include <unistd.h>
  88. #endif
  89. #ifdef HAVE_SYS_IOCTL_H
  90. #include <sys/ioctl.h>
  91. #endif
  92. #include "archive.h"
  93. #include "archive_string.h"
  94. #include "archive_entry.h"
  95. #include "archive_private.h"
  96. #include "archive_read_disk_private.h"
  97. #ifndef HAVE_FCHDIR
  98. #error fchdir function required.
  99. #endif
  100. #ifndef O_BINARY
  101. #define O_BINARY 0
  102. #endif
  103. #ifndef O_CLOEXEC
  104. #define O_CLOEXEC 0
  105. #endif
  106. /*-
  107. * This is a new directory-walking system that addresses a number
  108. * of problems I've had with fts(3). In particular, it has no
  109. * pathname-length limits (other than the size of 'int'), handles
  110. * deep logical traversals, uses considerably less memory, and has
  111. * an opaque interface (easier to modify in the future).
  112. *
  113. * Internally, it keeps a single list of "tree_entry" items that
  114. * represent filesystem objects that require further attention.
  115. * Non-directories are not kept in memory: they are pulled from
  116. * readdir(), returned to the client, then freed as soon as possible.
  117. * Any directory entry to be traversed gets pushed onto the stack.
  118. *
  119. * There is surprisingly little information that needs to be kept for
  120. * each item on the stack. Just the name, depth (represented here as the
  121. * string length of the parent directory's pathname), and some markers
  122. * indicating how to get back to the parent (via chdir("..") for a
  123. * regular dir or via fchdir(2) for a symlink).
  124. */
  125. /*
  126. * TODO:
  127. * 1) Loop checking.
  128. * 3) Arbitrary logical traversals by closing/reopening intermediate fds.
  129. */
  130. struct restore_time {
  131. const char *name;
  132. time_t mtime;
  133. long mtime_nsec;
  134. time_t atime;
  135. long atime_nsec;
  136. mode_t filetype;
  137. int noatime;
  138. };
  139. struct tree_entry {
  140. int depth;
  141. struct tree_entry *next;
  142. struct tree_entry *parent;
  143. struct archive_string name;
  144. size_t dirname_length;
  145. int64_t dev;
  146. int64_t ino;
  147. int flags;
  148. int filesystem_id;
  149. /* How to return back to the parent of a symlink. */
  150. int symlink_parent_fd;
  151. /* How to restore time of a directory. */
  152. struct restore_time restore_time;
  153. };
  154. struct filesystem {
  155. int64_t dev;
  156. int synthetic;
  157. int remote;
  158. int noatime;
  159. #if defined(HAVE_READDIR_R)
  160. size_t name_max;
  161. #endif
  162. long incr_xfer_size;
  163. long max_xfer_size;
  164. long min_xfer_size;
  165. long xfer_align;
  166. /*
  167. * Buffer used for reading file contents.
  168. */
  169. /* Exactly allocated memory pointer. */
  170. unsigned char *allocation_ptr;
  171. /* Pointer adjusted to the filesystem alignment . */
  172. unsigned char *buff;
  173. size_t buff_size;
  174. };
  175. /* Definitions for tree_entry.flags bitmap. */
  176. #define isDir 1 /* This entry is a regular directory. */
  177. #define isDirLink 2 /* This entry is a symbolic link to a directory. */
  178. #define needsFirstVisit 4 /* This is an initial entry. */
  179. #define needsDescent 8 /* This entry needs to be previsited. */
  180. #define needsOpen 16 /* This is a directory that needs to be opened. */
  181. #define needsAscent 32 /* This entry needs to be postvisited. */
  182. /*
  183. * Local data for this package.
  184. */
  185. struct tree {
  186. struct tree_entry *stack;
  187. struct tree_entry *current;
  188. DIR *d;
  189. #define INVALID_DIR_HANDLE NULL
  190. struct dirent *de;
  191. #if defined(HAVE_READDIR_R)
  192. struct dirent *dirent;
  193. size_t dirent_allocated;
  194. #endif
  195. int flags;
  196. int visit_type;
  197. /* Error code from last failed operation. */
  198. int tree_errno;
  199. /* Dynamically-sized buffer for holding path */
  200. struct archive_string path;
  201. /* Last path element */
  202. const char *basename;
  203. /* Leading dir length */
  204. size_t dirname_length;
  205. int depth;
  206. int openCount;
  207. int maxOpenCount;
  208. int initial_dir_fd;
  209. int working_dir_fd;
  210. struct stat lst;
  211. struct stat st;
  212. int descend;
  213. int nlink;
  214. /* How to restore time of a file. */
  215. struct restore_time restore_time;
  216. struct entry_sparse {
  217. int64_t length;
  218. int64_t offset;
  219. } *sparse_list, *current_sparse;
  220. int sparse_count;
  221. int sparse_list_size;
  222. char initial_symlink_mode;
  223. char symlink_mode;
  224. struct filesystem *current_filesystem;
  225. struct filesystem *filesystem_table;
  226. int initial_filesystem_id;
  227. int current_filesystem_id;
  228. int max_filesystem_id;
  229. int allocated_filesytem;
  230. int entry_fd;
  231. int entry_eof;
  232. int64_t entry_remaining_bytes;
  233. int64_t entry_total;
  234. unsigned char *entry_buff;
  235. size_t entry_buff_size;
  236. };
  237. /* Definitions for tree.flags bitmap. */
  238. #define hasStat 16 /* The st entry is valid. */
  239. #define hasLstat 32 /* The lst entry is valid. */
  240. #define onWorkingDir 64 /* We are on the working dir where we are
  241. * reading directory entry at this time. */
  242. #define needsRestoreTimes 128
  243. #define onInitialDir 256 /* We are on the initial dir. */
  244. static int
  245. tree_dir_next_posix(struct tree *t);
  246. #ifdef HAVE_DIRENT_D_NAMLEN
  247. /* BSD extension; avoids need for a strlen() call. */
  248. #define D_NAMELEN(dp) (dp)->d_namlen
  249. #else
  250. #define D_NAMELEN(dp) (strlen((dp)->d_name))
  251. #endif
  252. /* Initiate/terminate a tree traversal. */
  253. static struct tree *tree_open(const char *, int, int);
  254. static struct tree *tree_reopen(struct tree *, const char *, int);
  255. static void tree_close(struct tree *);
  256. static void tree_free(struct tree *);
  257. static void tree_push(struct tree *, const char *, int, int64_t, int64_t,
  258. struct restore_time *);
  259. static int tree_enter_initial_dir(struct tree *);
  260. static int tree_enter_working_dir(struct tree *);
  261. static int tree_current_dir_fd(struct tree *);
  262. /*
  263. * tree_next() returns Zero if there is no next entry, non-zero if
  264. * there is. Note that directories are visited three times.
  265. * Directories are always visited first as part of enumerating their
  266. * parent; that is a "regular" visit. If tree_descend() is invoked at
  267. * that time, the directory is added to a work list and will
  268. * subsequently be visited two more times: once just after descending
  269. * into the directory ("postdescent") and again just after ascending
  270. * back to the parent ("postascent").
  271. *
  272. * TREE_ERROR_DIR is returned if the descent failed (because the
  273. * directory couldn't be opened, for instance). This is returned
  274. * instead of TREE_POSTDESCENT/TREE_POSTASCENT. TREE_ERROR_DIR is not a
  275. * fatal error, but it does imply that the relevant subtree won't be
  276. * visited. TREE_ERROR_FATAL is returned for an error that left the
  277. * traversal completely hosed. Right now, this is only returned for
  278. * chdir() failures during ascent.
  279. */
  280. #define TREE_REGULAR 1
  281. #define TREE_POSTDESCENT 2
  282. #define TREE_POSTASCENT 3
  283. #define TREE_ERROR_DIR -1
  284. #define TREE_ERROR_FATAL -2
  285. static int tree_next(struct tree *);
  286. /*
  287. * Return information about the current entry.
  288. */
  289. /*
  290. * The current full pathname, length of the full pathname, and a name
  291. * that can be used to access the file. Because tree does use chdir
  292. * extensively, the access path is almost never the same as the full
  293. * current path.
  294. *
  295. * TODO: On platforms that support it, use openat()-style operations
  296. * to eliminate the chdir() operations entirely while still supporting
  297. * arbitrarily deep traversals. This makes access_path troublesome to
  298. * support, of course, which means we'll need a rich enough interface
  299. * that clients can function without it. (In particular, we'll need
  300. * tree_current_open() that returns an open file descriptor.)
  301. *
  302. */
  303. static const char *tree_current_path(struct tree *);
  304. static const char *tree_current_access_path(struct tree *);
  305. /*
  306. * Request the lstat() or stat() data for the current path. Since the
  307. * tree package needs to do some of this anyway, and caches the
  308. * results, you should take advantage of it here if you need it rather
  309. * than make a redundant stat() or lstat() call of your own.
  310. */
  311. static const struct stat *tree_current_stat(struct tree *);
  312. static const struct stat *tree_current_lstat(struct tree *);
  313. static int tree_current_is_symblic_link_target(struct tree *);
  314. /* The following functions use tricks to avoid a certain number of
  315. * stat()/lstat() calls. */
  316. /* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
  317. static int tree_current_is_physical_dir(struct tree *);
  318. /* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
  319. static int tree_current_is_dir(struct tree *);
  320. static int update_current_filesystem(struct archive_read_disk *a,
  321. int64_t dev);
  322. static int setup_current_filesystem(struct archive_read_disk *);
  323. static int tree_target_is_same_as_parent(struct tree *, const struct stat *);
  324. static int _archive_read_disk_open(struct archive *, const char *);
  325. static int _archive_read_free(struct archive *);
  326. static int _archive_read_close(struct archive *);
  327. static int _archive_read_data_block(struct archive *,
  328. const void **, size_t *, int64_t *);
  329. static int _archive_read_next_header2(struct archive *,
  330. struct archive_entry *);
  331. static const char *trivial_lookup_gname(void *, int64_t gid);
  332. static const char *trivial_lookup_uname(void *, int64_t uid);
  333. static int setup_sparse(struct archive_read_disk *, struct archive_entry *);
  334. static int close_and_restore_time(int fd, struct tree *,
  335. struct restore_time *);
  336. static int open_on_current_dir(struct tree *, const char *, int);
  337. static int tree_dup(int);
  338. static struct archive_vtable *
  339. archive_read_disk_vtable(void)
  340. {
  341. static struct archive_vtable av;
  342. static int inited = 0;
  343. if (!inited) {
  344. av.archive_free = _archive_read_free;
  345. av.archive_close = _archive_read_close;
  346. av.archive_read_data_block = _archive_read_data_block;
  347. av.archive_read_next_header2 = _archive_read_next_header2;
  348. inited = 1;
  349. }
  350. return (&av);
  351. }
  352. const char *
  353. archive_read_disk_gname(struct archive *_a, int64_t gid)
  354. {
  355. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  356. if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  357. ARCHIVE_STATE_ANY, "archive_read_disk_gname"))
  358. return (NULL);
  359. if (a->lookup_gname == NULL)
  360. return (NULL);
  361. return ((*a->lookup_gname)(a->lookup_gname_data, gid));
  362. }
  363. const char *
  364. archive_read_disk_uname(struct archive *_a, int64_t uid)
  365. {
  366. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  367. if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  368. ARCHIVE_STATE_ANY, "archive_read_disk_uname"))
  369. return (NULL);
  370. if (a->lookup_uname == NULL)
  371. return (NULL);
  372. return ((*a->lookup_uname)(a->lookup_uname_data, uid));
  373. }
  374. int
  375. archive_read_disk_set_gname_lookup(struct archive *_a,
  376. void *private_data,
  377. const char * (*lookup_gname)(void *private, int64_t gid),
  378. void (*cleanup_gname)(void *private))
  379. {
  380. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  381. archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
  382. ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
  383. if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
  384. (a->cleanup_gname)(a->lookup_gname_data);
  385. a->lookup_gname = lookup_gname;
  386. a->cleanup_gname = cleanup_gname;
  387. a->lookup_gname_data = private_data;
  388. return (ARCHIVE_OK);
  389. }
  390. int
  391. archive_read_disk_set_uname_lookup(struct archive *_a,
  392. void *private_data,
  393. const char * (*lookup_uname)(void *private, int64_t uid),
  394. void (*cleanup_uname)(void *private))
  395. {
  396. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  397. archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
  398. ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
  399. if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
  400. (a->cleanup_uname)(a->lookup_uname_data);
  401. a->lookup_uname = lookup_uname;
  402. a->cleanup_uname = cleanup_uname;
  403. a->lookup_uname_data = private_data;
  404. return (ARCHIVE_OK);
  405. }
  406. /*
  407. * Create a new archive_read_disk object and initialize it with global state.
  408. */
  409. struct archive *
  410. archive_read_disk_new(void)
  411. {
  412. struct archive_read_disk *a;
  413. a = (struct archive_read_disk *)calloc(1, sizeof(*a));
  414. if (a == NULL)
  415. return (NULL);
  416. a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
  417. a->archive.state = ARCHIVE_STATE_NEW;
  418. a->archive.vtable = archive_read_disk_vtable();
  419. a->lookup_uname = trivial_lookup_uname;
  420. a->lookup_gname = trivial_lookup_gname;
  421. a->enable_copyfile = 1;
  422. a->traverse_mount_points = 1;
  423. a->open_on_current_dir = open_on_current_dir;
  424. a->tree_current_dir_fd = tree_current_dir_fd;
  425. a->tree_enter_working_dir = tree_enter_working_dir;
  426. return (&a->archive);
  427. }
  428. static int
  429. _archive_read_free(struct archive *_a)
  430. {
  431. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  432. int r;
  433. if (_a == NULL)
  434. return (ARCHIVE_OK);
  435. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  436. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
  437. if (a->archive.state != ARCHIVE_STATE_CLOSED)
  438. r = _archive_read_close(&a->archive);
  439. else
  440. r = ARCHIVE_OK;
  441. tree_free(a->tree);
  442. if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
  443. (a->cleanup_gname)(a->lookup_gname_data);
  444. if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
  445. (a->cleanup_uname)(a->lookup_uname_data);
  446. archive_string_free(&a->archive.error_string);
  447. a->archive.magic = 0;
  448. __archive_clean(&a->archive);
  449. free(a);
  450. return (r);
  451. }
  452. static int
  453. _archive_read_close(struct archive *_a)
  454. {
  455. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  456. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  457. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
  458. if (a->archive.state != ARCHIVE_STATE_FATAL)
  459. a->archive.state = ARCHIVE_STATE_CLOSED;
  460. tree_close(a->tree);
  461. return (ARCHIVE_OK);
  462. }
  463. static void
  464. setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
  465. int follow_symlinks)
  466. {
  467. a->symlink_mode = symlink_mode;
  468. a->follow_symlinks = follow_symlinks;
  469. if (a->tree != NULL) {
  470. a->tree->initial_symlink_mode = a->symlink_mode;
  471. a->tree->symlink_mode = a->symlink_mode;
  472. }
  473. }
  474. int
  475. archive_read_disk_set_symlink_logical(struct archive *_a)
  476. {
  477. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  478. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  479. ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
  480. setup_symlink_mode(a, 'L', 1);
  481. return (ARCHIVE_OK);
  482. }
  483. int
  484. archive_read_disk_set_symlink_physical(struct archive *_a)
  485. {
  486. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  487. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  488. ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
  489. setup_symlink_mode(a, 'P', 0);
  490. return (ARCHIVE_OK);
  491. }
  492. int
  493. archive_read_disk_set_symlink_hybrid(struct archive *_a)
  494. {
  495. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  496. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  497. ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
  498. setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
  499. return (ARCHIVE_OK);
  500. }
  501. int
  502. archive_read_disk_set_atime_restored(struct archive *_a)
  503. {
  504. #ifndef HAVE_UTIMES
  505. static int warning_done = 0;
  506. #endif
  507. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  508. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  509. ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
  510. #ifdef HAVE_UTIMES
  511. a->restore_time = 1;
  512. if (a->tree != NULL)
  513. a->tree->flags |= needsRestoreTimes;
  514. return (ARCHIVE_OK);
  515. #else
  516. if (warning_done)
  517. /* Warning was already emitted; suppress further warnings. */
  518. return (ARCHIVE_OK);
  519. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  520. "Cannot restore access time on this system");
  521. warning_done = 1;
  522. return (ARCHIVE_WARN);
  523. #endif
  524. }
  525. int
  526. archive_read_disk_set_behavior(struct archive *_a, int flags)
  527. {
  528. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  529. int r = ARCHIVE_OK;
  530. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  531. ARCHIVE_STATE_ANY, "archive_read_disk_honor_nodump");
  532. if (flags & ARCHIVE_READDISK_RESTORE_ATIME)
  533. r = archive_read_disk_set_atime_restored(_a);
  534. else {
  535. a->restore_time = 0;
  536. if (a->tree != NULL)
  537. a->tree->flags &= ~needsRestoreTimes;
  538. }
  539. if (flags & ARCHIVE_READDISK_HONOR_NODUMP)
  540. a->honor_nodump = 1;
  541. else
  542. a->honor_nodump = 0;
  543. if (flags & ARCHIVE_READDISK_MAC_COPYFILE)
  544. a->enable_copyfile = 1;
  545. else
  546. a->enable_copyfile = 0;
  547. if (flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS)
  548. a->traverse_mount_points = 0;
  549. else
  550. a->traverse_mount_points = 1;
  551. return (r);
  552. }
  553. /*
  554. * Trivial implementations of gname/uname lookup functions.
  555. * These are normally overridden by the client, but these stub
  556. * versions ensure that we always have something that works.
  557. */
  558. static const char *
  559. trivial_lookup_gname(void *private_data, int64_t gid)
  560. {
  561. (void)private_data; /* UNUSED */
  562. (void)gid; /* UNUSED */
  563. return (NULL);
  564. }
  565. static const char *
  566. trivial_lookup_uname(void *private_data, int64_t uid)
  567. {
  568. (void)private_data; /* UNUSED */
  569. (void)uid; /* UNUSED */
  570. return (NULL);
  571. }
  572. /*
  573. * Allocate memory for the reading buffer adjusted to the filesystem
  574. * alignment.
  575. */
  576. static int
  577. setup_suitable_read_buffer(struct archive_read_disk *a)
  578. {
  579. struct tree *t = a->tree;
  580. struct filesystem *cf = t->current_filesystem;
  581. size_t asize;
  582. size_t s;
  583. if (cf->allocation_ptr == NULL) {
  584. /* If we couldn't get a filesystem alignment,
  585. * we use 4096 as default value but we won't use
  586. * O_DIRECT to open() and openat() operations. */
  587. long xfer_align = (cf->xfer_align == -1)?4096:cf->xfer_align;
  588. if (cf->max_xfer_size != -1)
  589. asize = cf->max_xfer_size + xfer_align;
  590. else {
  591. long incr = cf->incr_xfer_size;
  592. /* Some platform does not set a proper value to
  593. * incr_xfer_size.*/
  594. if (incr < 0)
  595. incr = cf->min_xfer_size;
  596. if (cf->min_xfer_size < 0) {
  597. incr = xfer_align;
  598. asize = xfer_align;
  599. } else
  600. asize = cf->min_xfer_size;
  601. /* Increase a buffer size up to 64K bytes in
  602. * a proper incremant size. */
  603. while (asize < 1024*64)
  604. asize += incr;
  605. /* Take a margin to adjust to the filesystem
  606. * alignment. */
  607. asize += xfer_align;
  608. }
  609. cf->allocation_ptr = malloc(asize);
  610. if (cf->allocation_ptr == NULL) {
  611. archive_set_error(&a->archive, ENOMEM,
  612. "Couldn't allocate memory");
  613. a->archive.state = ARCHIVE_STATE_FATAL;
  614. return (ARCHIVE_FATAL);
  615. }
  616. /*
  617. * Calculate proper address for the filesystem.
  618. */
  619. s = (uintptr_t)cf->allocation_ptr;
  620. s %= xfer_align;
  621. if (s > 0)
  622. s = xfer_align - s;
  623. /*
  624. * Set a read buffer pointer in the proper alignment of
  625. * the current filesystem.
  626. */
  627. cf->buff = cf->allocation_ptr + s;
  628. cf->buff_size = asize - xfer_align;
  629. }
  630. return (ARCHIVE_OK);
  631. }
  632. static int
  633. _archive_read_data_block(struct archive *_a, const void **buff,
  634. size_t *size, int64_t *offset)
  635. {
  636. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  637. struct tree *t = a->tree;
  638. int r;
  639. ssize_t bytes;
  640. size_t buffbytes;
  641. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
  642. "archive_read_data_block");
  643. if (t->entry_eof || t->entry_remaining_bytes <= 0) {
  644. r = ARCHIVE_EOF;
  645. goto abort_read_data;
  646. }
  647. /*
  648. * Open the current file.
  649. */
  650. if (t->entry_fd < 0) {
  651. int flags = O_RDONLY | O_BINARY | O_CLOEXEC;
  652. /*
  653. * Eliminate or reduce cache effects if we can.
  654. *
  655. * Carefully consider this to be enabled.
  656. */
  657. #if defined(O_DIRECT) && 0/* Disabled for now */
  658. if (t->current_filesystem->xfer_align != -1 &&
  659. t->nlink == 1)
  660. flags |= O_DIRECT;
  661. #endif
  662. #if defined(O_NOATIME)
  663. /*
  664. * Linux has O_NOATIME flag; use it if we need.
  665. */
  666. if ((t->flags & needsRestoreTimes) != 0 &&
  667. t->restore_time.noatime == 0)
  668. flags |= O_NOATIME;
  669. do {
  670. #endif
  671. t->entry_fd = open_on_current_dir(t,
  672. tree_current_access_path(t), flags);
  673. __archive_ensure_cloexec_flag(t->entry_fd);
  674. #if defined(O_NOATIME)
  675. /*
  676. * When we did open the file with O_NOATIME flag,
  677. * if successful, set 1 to t->restore_time.noatime
  678. * not to restore an atime of the file later.
  679. * if failed by EPERM, retry it without O_NOATIME flag.
  680. */
  681. if (flags & O_NOATIME) {
  682. if (t->entry_fd >= 0)
  683. t->restore_time.noatime = 1;
  684. else if (errno == EPERM) {
  685. flags &= ~O_NOATIME;
  686. continue;
  687. }
  688. }
  689. } while (0);
  690. #endif
  691. if (t->entry_fd < 0) {
  692. archive_set_error(&a->archive, errno,
  693. "Couldn't open %s", tree_current_path(t));
  694. r = ARCHIVE_FAILED;
  695. tree_enter_initial_dir(t);
  696. goto abort_read_data;
  697. }
  698. tree_enter_initial_dir(t);
  699. }
  700. /*
  701. * Allocate read buffer if not allocated.
  702. */
  703. if (t->current_filesystem->allocation_ptr == NULL) {
  704. r = setup_suitable_read_buffer(a);
  705. if (r != ARCHIVE_OK) {
  706. a->archive.state = ARCHIVE_STATE_FATAL;
  707. goto abort_read_data;
  708. }
  709. }
  710. t->entry_buff = t->current_filesystem->buff;
  711. t->entry_buff_size = t->current_filesystem->buff_size;
  712. buffbytes = t->entry_buff_size;
  713. if ((int64_t)buffbytes > t->current_sparse->length)
  714. buffbytes = t->current_sparse->length;
  715. /*
  716. * Skip hole.
  717. * TODO: Should we consider t->current_filesystem->xfer_align?
  718. */
  719. if (t->current_sparse->offset > t->entry_total) {
  720. if (lseek(t->entry_fd,
  721. (off_t)t->current_sparse->offset, SEEK_SET) < 0) {
  722. archive_set_error(&a->archive, errno, "Seek error");
  723. r = ARCHIVE_FATAL;
  724. a->archive.state = ARCHIVE_STATE_FATAL;
  725. goto abort_read_data;
  726. }
  727. bytes = t->current_sparse->offset - t->entry_total;
  728. t->entry_remaining_bytes -= bytes;
  729. t->entry_total += bytes;
  730. }
  731. /*
  732. * Read file contents.
  733. */
  734. if (buffbytes > 0) {
  735. bytes = read(t->entry_fd, t->entry_buff, buffbytes);
  736. if (bytes < 0) {
  737. archive_set_error(&a->archive, errno, "Read error");
  738. r = ARCHIVE_FATAL;
  739. a->archive.state = ARCHIVE_STATE_FATAL;
  740. goto abort_read_data;
  741. }
  742. } else
  743. bytes = 0;
  744. if (bytes == 0) {
  745. /* Get EOF */
  746. t->entry_eof = 1;
  747. r = ARCHIVE_EOF;
  748. goto abort_read_data;
  749. }
  750. *buff = t->entry_buff;
  751. *size = bytes;
  752. *offset = t->entry_total;
  753. t->entry_total += bytes;
  754. t->entry_remaining_bytes -= bytes;
  755. if (t->entry_remaining_bytes == 0) {
  756. /* Close the current file descriptor */
  757. close_and_restore_time(t->entry_fd, t, &t->restore_time);
  758. t->entry_fd = -1;
  759. t->entry_eof = 1;
  760. }
  761. t->current_sparse->offset += bytes;
  762. t->current_sparse->length -= bytes;
  763. if (t->current_sparse->length == 0 && !t->entry_eof)
  764. t->current_sparse++;
  765. return (ARCHIVE_OK);
  766. abort_read_data:
  767. *buff = NULL;
  768. *size = 0;
  769. *offset = t->entry_total;
  770. if (t->entry_fd >= 0) {
  771. /* Close the current file descriptor */
  772. close_and_restore_time(t->entry_fd, t, &t->restore_time);
  773. t->entry_fd = -1;
  774. }
  775. return (r);
  776. }
  777. static int
  778. next_entry(struct archive_read_disk *a, struct tree *t,
  779. struct archive_entry *entry)
  780. {
  781. const struct stat *st; /* info to use for this entry */
  782. const struct stat *lst;/* lstat() information */
  783. const char *name;
  784. int descend, r;
  785. st = NULL;
  786. lst = NULL;
  787. t->descend = 0;
  788. do {
  789. switch (tree_next(t)) {
  790. case TREE_ERROR_FATAL:
  791. archive_set_error(&a->archive, t->tree_errno,
  792. "%s: Unable to continue traversing directory tree",
  793. tree_current_path(t));
  794. a->archive.state = ARCHIVE_STATE_FATAL;
  795. tree_enter_initial_dir(t);
  796. return (ARCHIVE_FATAL);
  797. case TREE_ERROR_DIR:
  798. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  799. "%s: Couldn't visit directory",
  800. tree_current_path(t));
  801. tree_enter_initial_dir(t);
  802. return (ARCHIVE_FAILED);
  803. case 0:
  804. tree_enter_initial_dir(t);
  805. return (ARCHIVE_EOF);
  806. case TREE_POSTDESCENT:
  807. case TREE_POSTASCENT:
  808. break;
  809. case TREE_REGULAR:
  810. lst = tree_current_lstat(t);
  811. if (lst == NULL) {
  812. archive_set_error(&a->archive, errno,
  813. "%s: Cannot stat",
  814. tree_current_path(t));
  815. tree_enter_initial_dir(t);
  816. return (ARCHIVE_FAILED);
  817. }
  818. break;
  819. }
  820. } while (lst == NULL);
  821. #ifdef __APPLE__
  822. if (a->enable_copyfile) {
  823. /* If we're using copyfile(), ignore "._XXX" files. */
  824. const char *bname = strrchr(tree_current_path(t), '/');
  825. if (bname == NULL)
  826. bname = tree_current_path(t);
  827. else
  828. ++bname;
  829. if (bname[0] == '.' && bname[1] == '_')
  830. return (ARCHIVE_RETRY);
  831. }
  832. #endif
  833. archive_entry_copy_pathname(entry, tree_current_path(t));
  834. /*
  835. * Perform path matching.
  836. */
  837. if (a->matching) {
  838. r = archive_match_path_excluded(a->matching, entry);
  839. if (r < 0) {
  840. archive_set_error(&(a->archive), errno,
  841. "Faild : %s", archive_error_string(a->matching));
  842. return (r);
  843. }
  844. if (r) {
  845. if (a->excluded_cb_func)
  846. a->excluded_cb_func(&(a->archive),
  847. a->excluded_cb_data, entry);
  848. return (ARCHIVE_RETRY);
  849. }
  850. }
  851. /*
  852. * Distinguish 'L'/'P'/'H' symlink following.
  853. */
  854. switch(t->symlink_mode) {
  855. case 'H':
  856. /* 'H': After the first item, rest like 'P'. */
  857. t->symlink_mode = 'P';
  858. /* 'H': First item (from command line) like 'L'. */
  859. /* FALLTHROUGH */
  860. case 'L':
  861. /* 'L': Do descend through a symlink to dir. */
  862. descend = tree_current_is_dir(t);
  863. /* 'L': Follow symlinks to files. */
  864. a->symlink_mode = 'L';
  865. a->follow_symlinks = 1;
  866. /* 'L': Archive symlinks as targets, if we can. */
  867. st = tree_current_stat(t);
  868. if (st != NULL && !tree_target_is_same_as_parent(t, st))
  869. break;
  870. /* If stat fails, we have a broken symlink;
  871. * in that case, don't follow the link. */
  872. /* FALLTHROUGH */
  873. default:
  874. /* 'P': Don't descend through a symlink to dir. */
  875. descend = tree_current_is_physical_dir(t);
  876. /* 'P': Don't follow symlinks to files. */
  877. a->symlink_mode = 'P';
  878. a->follow_symlinks = 0;
  879. /* 'P': Archive symlinks as symlinks. */
  880. st = lst;
  881. break;
  882. }
  883. if (update_current_filesystem(a, st->st_dev) != ARCHIVE_OK) {
  884. a->archive.state = ARCHIVE_STATE_FATAL;
  885. tree_enter_initial_dir(t);
  886. return (ARCHIVE_FATAL);
  887. }
  888. if (t->initial_filesystem_id == -1)
  889. t->initial_filesystem_id = t->current_filesystem_id;
  890. if (!a->traverse_mount_points) {
  891. if (t->initial_filesystem_id != t->current_filesystem_id)
  892. return (ARCHIVE_RETRY);
  893. }
  894. t->descend = descend;
  895. /*
  896. * Honor nodump flag.
  897. * If the file is marked with nodump flag, do not return this entry.
  898. */
  899. if (a->honor_nodump) {
  900. #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
  901. if (st->st_flags & UF_NODUMP)
  902. return (ARCHIVE_RETRY);
  903. #elif defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL) &&\
  904. defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)
  905. if (S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) {
  906. int stflags;
  907. t->entry_fd = open_on_current_dir(t,
  908. tree_current_access_path(t),
  909. O_RDONLY | O_NONBLOCK | O_CLOEXEC);
  910. __archive_ensure_cloexec_flag(t->entry_fd);
  911. if (t->entry_fd >= 0) {
  912. r = ioctl(t->entry_fd, EXT2_IOC_GETFLAGS,
  913. &stflags);
  914. if (r == 0 && (stflags & EXT2_NODUMP_FL) != 0)
  915. return (ARCHIVE_RETRY);
  916. }
  917. }
  918. #endif
  919. }
  920. archive_entry_copy_stat(entry, st);
  921. /* Save the times to be restored. This must be in before
  922. * calling archive_read_disk_descend() or any chance of it,
  923. * especially, invokng a callback. */
  924. t->restore_time.mtime = archive_entry_mtime(entry);
  925. t->restore_time.mtime_nsec = archive_entry_mtime_nsec(entry);
  926. t->restore_time.atime = archive_entry_atime(entry);
  927. t->restore_time.atime_nsec = archive_entry_atime_nsec(entry);
  928. t->restore_time.filetype = archive_entry_filetype(entry);
  929. t->restore_time.noatime = t->current_filesystem->noatime;
  930. /*
  931. * Perform time matching.
  932. */
  933. if (a->matching) {
  934. r = archive_match_time_excluded(a->matching, entry);
  935. if (r < 0) {
  936. archive_set_error(&(a->archive), errno,
  937. "Faild : %s", archive_error_string(a->matching));
  938. return (r);
  939. }
  940. if (r) {
  941. if (a->excluded_cb_func)
  942. a->excluded_cb_func(&(a->archive),
  943. a->excluded_cb_data, entry);
  944. return (ARCHIVE_RETRY);
  945. }
  946. }
  947. /* Lookup uname/gname */
  948. name = archive_read_disk_uname(&(a->archive), archive_entry_uid(entry));
  949. if (name != NULL)
  950. archive_entry_copy_uname(entry, name);
  951. name = archive_read_disk_gname(&(a->archive), archive_entry_gid(entry));
  952. if (name != NULL)
  953. archive_entry_copy_gname(entry, name);
  954. /*
  955. * Perform owner matching.
  956. */
  957. if (a->matching) {
  958. r = archive_match_owner_excluded(a->matching, entry);
  959. if (r < 0) {
  960. archive_set_error(&(a->archive), errno,
  961. "Faild : %s", archive_error_string(a->matching));
  962. return (r);
  963. }
  964. if (r) {
  965. if (a->excluded_cb_func)
  966. a->excluded_cb_func(&(a->archive),
  967. a->excluded_cb_data, entry);
  968. return (ARCHIVE_RETRY);
  969. }
  970. }
  971. /*
  972. * Invoke a meta data filter callback.
  973. */
  974. if (a->metadata_filter_func) {
  975. if (!a->metadata_filter_func(&(a->archive),
  976. a->metadata_filter_data, entry))
  977. return (ARCHIVE_RETRY);
  978. }
  979. /*
  980. * Populate the archive_entry with metadata from the disk.
  981. */
  982. archive_entry_copy_sourcepath(entry, tree_current_access_path(t));
  983. r = archive_read_disk_entry_from_file(&(a->archive), entry,
  984. t->entry_fd, st);
  985. return (r);
  986. }
  987. static int
  988. _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
  989. {
  990. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  991. struct tree *t;
  992. int r;
  993. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  994. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  995. "archive_read_next_header2");
  996. t = a->tree;
  997. if (t->entry_fd >= 0) {
  998. close_and_restore_time(t->entry_fd, t, &t->restore_time);
  999. t->entry_fd = -1;
  1000. }
  1001. for (;;) {
  1002. r = next_entry(a, t, entry);
  1003. if (t->entry_fd >= 0) {
  1004. close(t->entry_fd);
  1005. t->entry_fd = -1;
  1006. }
  1007. if (r == ARCHIVE_RETRY) {
  1008. archive_entry_clear(entry);
  1009. continue;
  1010. }
  1011. break;
  1012. }
  1013. /* Return to the initial directory. */
  1014. tree_enter_initial_dir(t);
  1015. /*
  1016. * EOF and FATAL are persistent at this layer. By
  1017. * modifying the state, we guarantee that future calls to
  1018. * read a header or read data will fail.
  1019. */
  1020. switch (r) {
  1021. case ARCHIVE_EOF:
  1022. a->archive.state = ARCHIVE_STATE_EOF;
  1023. break;
  1024. case ARCHIVE_OK:
  1025. case ARCHIVE_WARN:
  1026. /* Overwrite the sourcepath based on the initial directory. */
  1027. archive_entry_copy_sourcepath(entry, tree_current_path(t));
  1028. t->entry_total = 0;
  1029. if (archive_entry_filetype(entry) == AE_IFREG) {
  1030. t->nlink = archive_entry_nlink(entry);
  1031. t->entry_remaining_bytes = archive_entry_size(entry);
  1032. t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
  1033. if (!t->entry_eof &&
  1034. setup_sparse(a, entry) != ARCHIVE_OK)
  1035. return (ARCHIVE_FATAL);
  1036. } else {
  1037. t->entry_remaining_bytes = 0;
  1038. t->entry_eof = 1;
  1039. }
  1040. a->archive.state = ARCHIVE_STATE_DATA;
  1041. break;
  1042. case ARCHIVE_RETRY:
  1043. break;
  1044. case ARCHIVE_FATAL:
  1045. a->archive.state = ARCHIVE_STATE_FATAL;
  1046. break;
  1047. }
  1048. return (r);
  1049. }
  1050. static int
  1051. setup_sparse(struct archive_read_disk *a, struct archive_entry *entry)
  1052. {
  1053. struct tree *t = a->tree;
  1054. int64_t length, offset;
  1055. int i;
  1056. t->sparse_count = archive_entry_sparse_reset(entry);
  1057. if (t->sparse_count+1 > t->sparse_list_size) {
  1058. free(t->sparse_list);
  1059. t->sparse_list_size = t->sparse_count + 1;
  1060. t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
  1061. t->sparse_list_size);
  1062. if (t->sparse_list == NULL) {
  1063. t->sparse_list_size = 0;
  1064. archive_set_error(&a->archive, ENOMEM,
  1065. "Can't allocate data");
  1066. a->archive.state = ARCHIVE_STATE_FATAL;
  1067. return (ARCHIVE_FATAL);
  1068. }
  1069. }
  1070. for (i = 0; i < t->sparse_count; i++) {
  1071. archive_entry_sparse_next(entry, &offset, &length);
  1072. t->sparse_list[i].offset = offset;
  1073. t->sparse_list[i].length = length;
  1074. }
  1075. if (i == 0) {
  1076. t->sparse_list[i].offset = 0;
  1077. t->sparse_list[i].length = archive_entry_size(entry);
  1078. } else {
  1079. t->sparse_list[i].offset = archive_entry_size(entry);
  1080. t->sparse_list[i].length = 0;
  1081. }
  1082. t->current_sparse = t->sparse_list;
  1083. return (ARCHIVE_OK);
  1084. }
  1085. int
  1086. archive_read_disk_set_matching(struct archive *_a, struct archive *_ma,
  1087. void (*_excluded_func)(struct archive *, void *, struct archive_entry *),
  1088. void *_client_data)
  1089. {
  1090. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1091. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1092. ARCHIVE_STATE_ANY, "archive_read_disk_set_matching");
  1093. a->matching = _ma;
  1094. a->excluded_cb_func = _excluded_func;
  1095. a->excluded_cb_data = _client_data;
  1096. return (ARCHIVE_OK);
  1097. }
  1098. int
  1099. archive_read_disk_set_metadata_filter_callback(struct archive *_a,
  1100. int (*_metadata_filter_func)(struct archive *, void *,
  1101. struct archive_entry *), void *_client_data)
  1102. {
  1103. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1104. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY,
  1105. "archive_read_disk_set_metadata_filter_callback");
  1106. a->metadata_filter_func = _metadata_filter_func;
  1107. a->metadata_filter_data = _client_data;
  1108. return (ARCHIVE_OK);
  1109. }
  1110. int
  1111. archive_read_disk_can_descend(struct archive *_a)
  1112. {
  1113. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1114. struct tree *t = a->tree;
  1115. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1116. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1117. "archive_read_disk_can_descend");
  1118. return (t->visit_type == TREE_REGULAR && t->descend);
  1119. }
  1120. /*
  1121. * Called by the client to mark the directory just returned from
  1122. * tree_next() as needing to be visited.
  1123. */
  1124. int
  1125. archive_read_disk_descend(struct archive *_a)
  1126. {
  1127. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1128. struct tree *t = a->tree;
  1129. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1130. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1131. "archive_read_disk_descend");
  1132. if (t->visit_type != TREE_REGULAR || !t->descend)
  1133. return (ARCHIVE_OK);
  1134. if (tree_current_is_physical_dir(t)) {
  1135. tree_push(t, t->basename, t->current_filesystem_id,
  1136. t->lst.st_dev, t->lst.st_ino, &t->restore_time);
  1137. t->stack->flags |= isDir;
  1138. } else if (tree_current_is_dir(t)) {
  1139. tree_push(t, t->basename, t->current_filesystem_id,
  1140. t->st.st_dev, t->st.st_ino, &t->restore_time);
  1141. t->stack->flags |= isDirLink;
  1142. }
  1143. t->descend = 0;
  1144. return (ARCHIVE_OK);
  1145. }
  1146. int
  1147. archive_read_disk_open(struct archive *_a, const char *pathname)
  1148. {
  1149. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1150. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1151. ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
  1152. "archive_read_disk_open");
  1153. archive_clear_error(&a->archive);
  1154. return (_archive_read_disk_open(_a, pathname));
  1155. }
  1156. int
  1157. archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
  1158. {
  1159. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1160. struct archive_string path;
  1161. int ret;
  1162. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1163. ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
  1164. "archive_read_disk_open_w");
  1165. archive_clear_error(&a->archive);
  1166. /* Make a char string from a wchar_t string. */
  1167. archive_string_init(&path);
  1168. if (archive_string_append_from_wcs(&path, pathname,
  1169. wcslen(pathname)) != 0) {
  1170. if (errno == ENOMEM)
  1171. archive_set_error(&a->archive, ENOMEM,
  1172. "Can't allocate memory");
  1173. else
  1174. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1175. "Can't convert a path to a char string");
  1176. a->archive.state = ARCHIVE_STATE_FATAL;
  1177. ret = ARCHIVE_FATAL;
  1178. } else
  1179. ret = _archive_read_disk_open(_a, path.s);
  1180. archive_string_free(&path);
  1181. return (ret);
  1182. }
  1183. static int
  1184. _archive_read_disk_open(struct archive *_a, const char *pathname)
  1185. {
  1186. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1187. if (a->tree != NULL)
  1188. a->tree = tree_reopen(a->tree, pathname, a->restore_time);
  1189. else
  1190. a->tree = tree_open(pathname, a->symlink_mode,
  1191. a->restore_time);
  1192. if (a->tree == NULL) {
  1193. archive_set_error(&a->archive, ENOMEM,
  1194. "Can't allocate tar data");
  1195. a->archive.state = ARCHIVE_STATE_FATAL;
  1196. return (ARCHIVE_FATAL);
  1197. }
  1198. a->archive.state = ARCHIVE_STATE_HEADER;
  1199. return (ARCHIVE_OK);
  1200. }
  1201. /*
  1202. * Return a current filesystem ID which is index of the filesystem entry
  1203. * you've visited through archive_read_disk.
  1204. */
  1205. int
  1206. archive_read_disk_current_filesystem(struct archive *_a)
  1207. {
  1208. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1209. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
  1210. "archive_read_disk_current_filesystem");
  1211. return (a->tree->current_filesystem_id);
  1212. }
  1213. static int
  1214. update_current_filesystem(struct archive_read_disk *a, int64_t dev)
  1215. {
  1216. struct tree *t = a->tree;
  1217. int i, fid;
  1218. if (t->current_filesystem != NULL &&
  1219. t->current_filesystem->dev == dev)
  1220. return (ARCHIVE_OK);
  1221. for (i = 0; i < t->max_filesystem_id; i++) {
  1222. if (t->filesystem_table[i].dev == dev) {
  1223. /* There is the filesytem ID we've already generated. */
  1224. t->current_filesystem_id = i;
  1225. t->current_filesystem = &(t->filesystem_table[i]);
  1226. return (ARCHIVE_OK);
  1227. }
  1228. }
  1229. /*
  1230. * This is the new filesytem which we have to generate a new ID for.
  1231. */
  1232. fid = t->max_filesystem_id++;
  1233. if (t->max_filesystem_id > t->allocated_filesytem) {
  1234. size_t s;
  1235. void *p;
  1236. s = t->max_filesystem_id * 2;
  1237. p = realloc(t->filesystem_table,
  1238. s * sizeof(*t->filesystem_table));
  1239. if (p == NULL) {
  1240. archive_set_error(&a->archive, ENOMEM,
  1241. "Can't allocate tar data");
  1242. return (ARCHIVE_FATAL);
  1243. }
  1244. t->filesystem_table = (struct filesystem *)p;
  1245. t->allocated_filesytem = s;
  1246. }
  1247. t->current_filesystem_id = fid;
  1248. t->current_filesystem = &(t->filesystem_table[fid]);
  1249. t->current_filesystem->dev = dev;
  1250. t->current_filesystem->allocation_ptr = NULL;
  1251. t->current_filesystem->buff = NULL;
  1252. /* Setup the current filesystem properties which depend on
  1253. * platform specific. */
  1254. return (setup_current_filesystem(a));
  1255. }
  1256. /*
  1257. * Returns 1 if current filesystem is generated filesystem, 0 if it is not
  1258. * or -1 if it is unknown.
  1259. */
  1260. int
  1261. archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
  1262. {
  1263. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1264. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
  1265. "archive_read_disk_current_filesystem");
  1266. return (a->tree->current_filesystem->synthetic);
  1267. }
  1268. /*
  1269. * Returns 1 if current filesystem is remote filesystem, 0 if it is not
  1270. * or -1 if it is unknown.
  1271. */
  1272. int
  1273. archive_read_disk_current_filesystem_is_remote(struct archive *_a)
  1274. {
  1275. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1276. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
  1277. "archive_read_disk_current_filesystem");
  1278. return (a->tree->current_filesystem->remote);
  1279. }
  1280. #if defined(_PC_REC_INCR_XFER_SIZE) && defined(_PC_REC_MAX_XFER_SIZE) &&\
  1281. defined(_PC_REC_MIN_XFER_SIZE) && defined(_PC_REC_XFER_ALIGN)
  1282. static int
  1283. get_xfer_size(struct tree *t, int fd, const char *path)
  1284. {
  1285. t->current_filesystem->xfer_align = -1;
  1286. errno = 0;
  1287. if (fd >= 0) {
  1288. t->current_filesystem->incr_xfer_size =
  1289. fpathconf(fd, _PC_REC_INCR_XFER_SIZE);
  1290. t->current_filesystem->max_xfer_size =
  1291. fpathconf(fd, _PC_REC_MAX_XFER_SIZE);
  1292. t->current_filesystem->min_xfer_size =
  1293. fpathconf(fd, _PC_REC_MIN_XFER_SIZE);
  1294. t->current_filesystem->xfer_align =
  1295. fpathconf(fd, _PC_REC_XFER_ALIGN);
  1296. } else if (path != NULL) {
  1297. t->current_filesystem->incr_xfer_size =
  1298. pathconf(path, _PC_REC_INCR_XFER_SIZE);
  1299. t->current_filesystem->max_xfer_size =
  1300. pathconf(path, _PC_REC_MAX_XFER_SIZE);
  1301. t->current_filesystem->min_xfer_size =
  1302. pathconf(path, _PC_REC_MIN_XFER_SIZE);
  1303. t->current_filesystem->xfer_align =
  1304. pathconf(path, _PC_REC_XFER_ALIGN);
  1305. }
  1306. /* At least we need an alignment size. */
  1307. if (t->current_filesystem->xfer_align == -1)
  1308. return ((errno == EINVAL)?1:-1);
  1309. else
  1310. return (0);
  1311. }
  1312. #else
  1313. static int
  1314. get_xfer_size(struct tree *t, int fd, const char *path)
  1315. {
  1316. (void)t; /* UNUSED */
  1317. (void)fd; /* UNUSED */
  1318. (void)path; /* UNUSED */
  1319. return (1);/* Not supported */
  1320. }
  1321. #endif
  1322. #if defined(HAVE_STATFS) && defined(HAVE_FSTATFS) && defined(MNT_LOCAL) \
  1323. && !defined(ST_LOCAL)
  1324. /*
  1325. * Gather current filesystem properties on FreeBSD, OpenBSD and Mac OS X.
  1326. */
  1327. static int
  1328. setup_current_filesystem(struct archive_read_disk *a)
  1329. {
  1330. struct tree *t = a->tree;
  1331. struct statfs sfs;
  1332. #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
  1333. struct xvfsconf vfc;
  1334. #endif
  1335. int r, xr = 0;
  1336. #if !defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
  1337. long nm;
  1338. #endif
  1339. t->current_filesystem->synthetic = -1;
  1340. t->current_filesystem->remote = -1;
  1341. if (tree_current_is_symblic_link_target(t)) {
  1342. #if defined(HAVE_OPENAT)
  1343. /*
  1344. * Get file system statistics on any directory
  1345. * where current is.
  1346. */
  1347. int fd = openat(tree_current_dir_fd(t),
  1348. tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
  1349. __archive_ensure_cloexec_flag(fd);
  1350. if (fd < 0) {
  1351. archive_set_error(&a->archive, errno,
  1352. "openat failed");
  1353. return (ARCHIVE_FAILED);
  1354. }
  1355. r = fstatfs(fd, &sfs);
  1356. if (r == 0)
  1357. xr = get_xfer_size(t, fd, NULL);
  1358. close(fd);
  1359. #else
  1360. if (tree_enter_working_dir(t) != 0) {
  1361. archive_set_error(&a->archive, errno, "fchdir failed");
  1362. return (ARCHIVE_FAILED);
  1363. }
  1364. r = statfs(tree_current_access_path(t), &sfs);
  1365. if (r == 0)
  1366. xr = get_xfer_size(t, -1, tree_current_access_path(t));
  1367. #endif
  1368. } else {
  1369. r = fstatfs(tree_current_dir_fd(t), &sfs);
  1370. if (r == 0)
  1371. xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
  1372. }
  1373. if (r == -1 || xr == -1) {
  1374. archive_set_error(&a->archive, errno, "statfs failed");
  1375. return (ARCHIVE_FAILED);
  1376. } else if (xr == 1) {
  1377. /* pathconf(_PC_REX_*) operations are not supported. */
  1378. t->current_filesystem->xfer_align = sfs.f_bsize;
  1379. t->current_filesystem->max_xfer_size = -1;
  1380. t->current_filesystem->min_xfer_size = sfs.f_iosize;
  1381. t->current_filesystem->incr_xfer_size = sfs.f_iosize;
  1382. }
  1383. if (sfs.f_flags & MNT_LOCAL)
  1384. t->current_filesystem->remote = 0;
  1385. else
  1386. t->current_filesystem->remote = 1;
  1387. #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
  1388. r = getvfsbyname(sfs.f_fstypename, &vfc);
  1389. if (r == -1) {
  1390. archive_set_error(&a->archive, errno, "getvfsbyname failed");
  1391. return (ARCHIVE_FAILED);
  1392. }
  1393. if (vfc.vfc_flags & VFCF_SYNTHETIC)
  1394. t->current_filesystem->synthetic = 1;
  1395. else
  1396. t->current_filesystem->synthetic = 0;
  1397. #endif
  1398. #if defined(MNT_NOATIME)
  1399. if (sfs.f_flags & MNT_NOATIME)
  1400. t->current_filesystem->noatime = 1;
  1401. else
  1402. #endif
  1403. t->current_filesystem->noatime = 0;
  1404. #if defined(HAVE_READDIR_R)
  1405. /* Set maximum filename length. */
  1406. #if defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
  1407. t->current_filesystem->name_max = sfs.f_namemax;
  1408. #else
  1409. /* Mac OS X does not have f_namemax in struct statfs. */
  1410. if (tree_current_is_symblic_link_target(t)) {
  1411. if (tree_enter_working_dir(t) != 0) {
  1412. archive_set_error(&a->archive, errno, "fchdir failed");
  1413. return (ARCHIVE_FAILED);
  1414. }
  1415. nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
  1416. } else
  1417. nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
  1418. if (nm == -1)
  1419. t->current_filesystem->name_max = NAME_MAX;
  1420. else
  1421. t->current_filesystem->name_max = nm;
  1422. #endif
  1423. #endif /* HAVE_READDIR_R */
  1424. return (ARCHIVE_OK);
  1425. }
  1426. #elif (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS)) && defined(ST_LOCAL)
  1427. /*
  1428. * Gather current filesystem properties on NetBSD
  1429. */
  1430. static int
  1431. setup_current_filesystem(struct archive_read_disk *a)
  1432. {
  1433. struct tree *t = a->tree;
  1434. struct statvfs sfs;
  1435. int r, xr = 0;
  1436. t->current_filesystem->synthetic = -1;
  1437. if (tree_enter_working_dir(t) != 0) {
  1438. archive_set_error(&a->archive, errno, "fchdir failed");
  1439. return (ARCHIVE_FAILED);
  1440. }
  1441. if (tree_current_is_symblic_link_target(t)) {
  1442. r = statvfs(tree_current_access_path(t), &sfs);
  1443. if (r == 0)
  1444. xr = get_xfer_size(t, -1, tree_current_access_path(t));
  1445. } else {
  1446. #ifdef HAVE_FSTATVFS
  1447. r = fstatvfs(tree_current_dir_fd(t), &sfs);
  1448. if (r == 0)
  1449. xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
  1450. #else
  1451. r = statvfs(".", &sfs);
  1452. if (r == 0)
  1453. xr = get_xfer_size(t, -1, ".");
  1454. #endif
  1455. }
  1456. if (r == -1 || xr == -1) {
  1457. t->current_filesystem->remote = -1;
  1458. archive_set_error(&a->archive, errno, "statvfs failed");
  1459. return (ARCHIVE_FAILED);
  1460. } else if (xr == 1) {
  1461. /* Usuall come here unless NetBSD supports _PC_REC_XFER_ALIGN
  1462. * for pathconf() function. */
  1463. t->current_filesystem->xfer_align = sfs.f_frsize;
  1464. t->current_filesystem->max_xfer_size = -1;
  1465. #if defined(HAVE_STRUCT_STATVFS_F_IOSIZE)
  1466. t->current_filesystem->min_xfer_size = sfs.f_iosize;
  1467. t->current_filesystem->incr_xfer_size = sfs.f_iosize;
  1468. #else
  1469. t->current_filesystem->min_xfer_size = sfs.f_bsize;
  1470. t->current_filesystem->incr_xfer_size = sfs.f_bsize;
  1471. #endif
  1472. }
  1473. if (sfs.f_flag & ST_LOCAL)
  1474. t->current_filesystem->remote = 0;
  1475. else
  1476. t->current_filesystem->remote = 1;
  1477. #if defined(ST_NOATIME)
  1478. if (sfs.f_flag & ST_NOATIME)
  1479. t->current_filesystem->noatime = 1;
  1480. else
  1481. #endif
  1482. t->current_filesystem->noatime = 0;
  1483. /* Set maximum filename length. */
  1484. t->current_filesystem->name_max = sfs.f_namemax;
  1485. return (ARCHIVE_OK);
  1486. }
  1487. #elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_LINUX_MAGIC_H) &&\
  1488. defined(HAVE_STATFS) && defined(HAVE_FSTATFS)
  1489. /*
  1490. * Note: statfs is deprecated since LSB 3.2
  1491. */
  1492. #ifndef CIFS_SUPER_MAGIC
  1493. #define CIFS_SUPER_MAGIC 0xFF534D42
  1494. #endif
  1495. #ifndef DEVFS_SUPER_MAGIC
  1496. #define DEVFS_SUPER_MAGIC 0x1373
  1497. #endif
  1498. /*
  1499. * Gather current filesystem properties on Linux
  1500. */
  1501. static int
  1502. setup_current_filesystem(struct archive_read_disk *a)
  1503. {
  1504. struct tree *t = a->tree;
  1505. struct statfs sfs;
  1506. struct statvfs svfs;
  1507. int r, vr = 0, xr = 0;
  1508. if (tree_current_is_symblic_link_target(t)) {
  1509. #if defined(HAVE_OPENAT)
  1510. /*
  1511. * Get file system statistics on any directory
  1512. * where current is.
  1513. */
  1514. int fd = openat(tree_current_dir_fd(t),
  1515. tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
  1516. __archive_ensure_cloexec_flag(fd);
  1517. if (fd < 0) {
  1518. archive_set_error(&a->archive, errno,
  1519. "openat failed");
  1520. return (ARCHIVE_FAILED);
  1521. }
  1522. vr = fstatvfs(fd, &svfs);/* for f_flag, mount flags */
  1523. r = fstatfs(fd, &sfs);
  1524. if (r == 0)
  1525. xr = get_xfer_size(t, fd, NULL);
  1526. close(fd);
  1527. #else
  1528. if (tree_enter_working_dir(t) != 0) {
  1529. archive_set_error(&a->archive, errno, "fchdir failed");
  1530. return (ARCHIVE_FAILED);
  1531. }
  1532. vr = statvfs(tree_current_access_path(t), &svfs);
  1533. r = statfs(tree_current_access_path(t), &sfs);
  1534. if (r == 0)
  1535. xr = get_xfer_size(t, -1, tree_current_access_path(t));
  1536. #endif
  1537. } else {
  1538. #ifdef HAVE_FSTATFS
  1539. vr = fstatvfs(tree_current_dir_fd(t), &svfs);
  1540. r = fstatfs(tree_current_dir_fd(t), &sfs);
  1541. if (r == 0)
  1542. xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
  1543. #else
  1544. if (tree_enter_working_dir(t) != 0) {
  1545. archive_set_error(&a->archive, errno, "fchdir failed");
  1546. return (ARCHIVE_FAILED);
  1547. }
  1548. vr = statvfs(".", &svfs);
  1549. r = statfs(".", &sfs);
  1550. if (r == 0)
  1551. xr = get_xfer_size(t, -1, ".");
  1552. #endif
  1553. }
  1554. if (r == -1 || xr == -1 || vr == -1) {
  1555. t->current_filesystem->synthetic = -1;
  1556. t->current_filesystem->remote = -1;
  1557. archive_set_error(&a->archive, errno, "statfs failed");
  1558. return (ARCHIVE_FAILED);
  1559. } else if (xr == 1) {
  1560. /* pathconf(_PC_REX_*) operations are not supported. */
  1561. t->current_filesystem->xfer_align = svfs.f_frsize;
  1562. t->current_filesystem->max_xfer_size = -1;
  1563. t->current_filesystem->min_xfer_size = svfs.f_bsize;
  1564. t->current_filesystem->incr_xfer_size = svfs.f_bsize;
  1565. }
  1566. switch (sfs.f_type) {
  1567. case AFS_SUPER_MAGIC:
  1568. case CIFS_SUPER_MAGIC:
  1569. case CODA_SUPER_MAGIC:
  1570. case NCP_SUPER_MAGIC:/* NetWare */
  1571. case NFS_SUPER_MAGIC:
  1572. case SMB_SUPER_MAGIC:
  1573. t->current_filesystem->remote = 1;
  1574. t->current_filesystem->synthetic = 0;
  1575. break;
  1576. case DEVFS_SUPER_MAGIC:
  1577. case PROC_SUPER_MAGIC:
  1578. case USBDEVICE_SUPER_MAGIC:
  1579. t->current_filesystem->remote = 0;
  1580. t->current_filesystem->synthetic = 1;
  1581. break;
  1582. default:
  1583. t->current_filesystem->remote = 0;
  1584. t->current_filesystem->synthetic = 0;
  1585. break;
  1586. }
  1587. #if defined(ST_NOATIME)
  1588. if (svfs.f_flag & ST_NOATIME)
  1589. t->current_filesystem->noatime = 1;
  1590. else
  1591. #endif
  1592. t->current_filesystem->noatime = 0;
  1593. #if defined(HAVE_READDIR_R)
  1594. /* Set maximum filename length. */
  1595. t->current_filesystem->name_max = sfs.f_namelen;
  1596. #endif
  1597. return (ARCHIVE_OK);
  1598. }
  1599. #elif defined(HAVE_SYS_STATVFS_H) &&\
  1600. (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS))
  1601. /*
  1602. * Gather current filesystem properties on other posix platform.
  1603. */
  1604. static int
  1605. setup_current_filesystem(struct archive_read_disk *a)
  1606. {
  1607. struct tree *t = a->tree;
  1608. struct statvfs sfs;
  1609. int r, xr = 0;
  1610. t->current_filesystem->synthetic = -1;/* Not supported */
  1611. t->current_filesystem->remote = -1;/* Not supported */
  1612. if (tree_current_is_symblic_link_target(t)) {
  1613. #if defined(HAVE_OPENAT)
  1614. /*
  1615. * Get file system statistics on any directory
  1616. * where current is.
  1617. */
  1618. int fd = openat(tree_current_dir_fd(t),
  1619. tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
  1620. __archive_ensure_cloexec_flag(fd);
  1621. if (fd < 0) {
  1622. archive_set_error(&a->archive, errno,
  1623. "openat failed");
  1624. return (ARCHIVE_FAILED);
  1625. }
  1626. r = fstatvfs(fd, &sfs);
  1627. if (r == 0)
  1628. xr = get_xfer_size(t, fd, NULL);
  1629. close(fd);
  1630. #else
  1631. if (tree_enter_working_dir(t) != 0) {
  1632. archive_set_error(&a->archive, errno, "fchdir failed");
  1633. return (ARCHIVE_FAILED);
  1634. }
  1635. r = statvfs(tree_current_access_path(t), &sfs);
  1636. if (r == 0)
  1637. xr = get_xfer_size(t, -1, tree_current_access_path(t));
  1638. #endif
  1639. } else {
  1640. #ifdef HAVE_FSTATVFS
  1641. r = fstatvfs(tree_current_dir_fd(t), &sfs);
  1642. if (r == 0)
  1643. xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
  1644. #else
  1645. if (tree_enter_working_dir(t) != 0) {
  1646. archive_set_error(&a->archive, errno, "fchdir failed");
  1647. return (ARCHIVE_FAILED);
  1648. }
  1649. r = statvfs(".", &sfs);
  1650. if (r == 0)
  1651. xr = get_xfer_size(t, -1, ".");
  1652. #endif
  1653. }
  1654. if (r == -1 || xr == -1) {
  1655. t->current_filesystem->synthetic = -1;
  1656. t->current_filesystem->remote = -1;
  1657. archive_set_error(&a->archive, errno, "statvfs failed");
  1658. return (ARCHIVE_FAILED);
  1659. } else if (xr == 1) {
  1660. /* pathconf(_PC_REX_*) operations are not supported. */
  1661. t->current_filesystem->xfer_align = sfs.f_frsize;
  1662. t->current_filesystem->max_xfer_size = -1;
  1663. t->current_filesystem->min_xfer_size = sfs.f_bsize;
  1664. t->current_filesystem->incr_xfer_size = sfs.f_bsize;
  1665. }
  1666. #if defined(ST_NOATIME)
  1667. if (sfs.f_flag & ST_NOATIME)
  1668. t->current_filesystem->noatime = 1;
  1669. else
  1670. #endif
  1671. t->current_filesystem->noatime = 0;
  1672. #if defined(HAVE_READDIR_R)
  1673. /* Set maximum filename length. */
  1674. t->current_filesystem->name_max = sfs.f_namemax;
  1675. #endif
  1676. return (ARCHIVE_OK);
  1677. }
  1678. #else
  1679. /*
  1680. * Generic: Gather current filesystem properties.
  1681. * TODO: Is this generic function really needed?
  1682. */
  1683. static int
  1684. setup_current_filesystem(struct archive_read_disk *a)
  1685. {
  1686. struct tree *t = a->tree;
  1687. #if defined(_PC_NAME_MAX) && defined(HAVE_READDIR_R)
  1688. long nm;
  1689. #endif
  1690. t->current_filesystem->synthetic = -1;/* Not supported */
  1691. t->current_filesystem->remote = -1;/* Not supported */
  1692. t->current_filesystem->noatime = 0;
  1693. (void)get_xfer_size(t, -1, ".");/* Dummy call to avoid build error. */
  1694. t->current_filesystem->xfer_align = -1;/* Unknown */
  1695. t->current_filesystem->max_xfer_size = -1;
  1696. t->current_filesystem->min_xfer_size = -1;
  1697. t->current_filesystem->incr_xfer_size = -1;
  1698. #if defined(HAVE_READDIR_R)
  1699. /* Set maximum filename length. */
  1700. # if defined(_PC_NAME_MAX)
  1701. if (tree_current_is_symblic_link_target(t)) {
  1702. if (tree_enter_working_dir(t) != 0) {
  1703. archive_set_error(&a->archive, errno, "fchdir failed");
  1704. return (ARCHIVE_FAILED);
  1705. }
  1706. nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
  1707. } else
  1708. nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
  1709. if (nm == -1)
  1710. # endif /* _PC_NAME_MAX */
  1711. /*
  1712. * Some sysmtes (HP-UX or others?) incorrectly defined
  1713. * NAME_MAX macro to be a smaller value.
  1714. */
  1715. # if defined(NAME_MAX) && NAME_MAX >= 255
  1716. t->current_filesystem->name_max = NAME_MAX;
  1717. # else
  1718. /* No way to get a trusted value of maximum filename
  1719. * length. */
  1720. t->current_filesystem->name_max = PATH_MAX;
  1721. # endif /* NAME_MAX */
  1722. # if defined(_PC_NAME_MAX)
  1723. else
  1724. t->current_filesystem->name_max = nm;
  1725. # endif /* _PC_NAME_MAX */
  1726. #endif /* HAVE_READDIR_R */
  1727. return (ARCHIVE_OK);
  1728. }
  1729. #endif
  1730. static int
  1731. close_and_restore_time(int fd, struct tree *t, struct restore_time *rt)
  1732. {
  1733. #ifndef HAVE_UTIMES
  1734. (void)t; /* UNUSED */
  1735. (void)rt; /* UNUSED */
  1736. return (close(fd));
  1737. #else
  1738. #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
  1739. struct timespec timespecs[2];
  1740. #endif
  1741. struct timeval times[2];
  1742. if ((t->flags & needsRestoreTimes) == 0 || rt->noatime) {
  1743. if (fd >= 0)
  1744. return (close(fd));
  1745. else
  1746. return (0);
  1747. }
  1748. #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
  1749. timespecs[1].tv_sec = rt->mtime;
  1750. timespecs[1].tv_nsec = rt->mtime_nsec;
  1751. timespecs[0].tv_sec = rt->atime;
  1752. timespecs[0].tv_nsec = rt->atime_nsec;
  1753. /* futimens() is defined in POSIX.1-2008. */
  1754. if (futimens(fd, timespecs) == 0)
  1755. return (close(fd));
  1756. #endif
  1757. times[1].tv_sec = rt->mtime;
  1758. times[1].tv_usec = rt->mtime_nsec / 1000;
  1759. times[0].tv_sec = rt->atime;
  1760. times[0].tv_usec = rt->atime_nsec / 1000;
  1761. #if !defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
  1762. if (futimes(fd, times) == 0)
  1763. return (close(fd));
  1764. #endif
  1765. close(fd);
  1766. #if defined(HAVE_FUTIMESAT)
  1767. if (futimesat(tree_current_dir_fd(t), rt->name, times) == 0)
  1768. return (0);
  1769. #endif
  1770. #ifdef HAVE_LUTIMES
  1771. if (lutimes(rt->name, times) != 0)
  1772. #else
  1773. if (AE_IFLNK != rt->filetype && utimes(rt->name, times) != 0)
  1774. #endif
  1775. return (-1);
  1776. #endif
  1777. return (0);
  1778. }
  1779. static int
  1780. open_on_current_dir(struct tree *t, const char *path, int flags)
  1781. {
  1782. #ifdef HAVE_OPENAT
  1783. return (openat(tree_current_dir_fd(t), path, flags));
  1784. #else
  1785. if (tree_enter_working_dir(t) != 0)
  1786. return (-1);
  1787. return (open(path, flags));
  1788. #endif
  1789. }
  1790. static int
  1791. tree_dup(int fd)
  1792. {
  1793. int new_fd;
  1794. #ifdef F_DUPFD_CLOEXEC
  1795. static volatile int can_dupfd_cloexec = 1;
  1796. if (can_dupfd_cloexec) {
  1797. new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
  1798. if (new_fd != -1)
  1799. return (new_fd);
  1800. /* Linux 2.6.18 - 2.6.23 declare F_DUPFD_CLOEXEC,
  1801. * but it cannot be used. So we have to try dup(). */
  1802. /* We won't try F_DUPFD_CLOEXEC. */
  1803. can_dupfd_cloexec = 0;
  1804. }
  1805. #endif /* F_DUPFD_CLOEXEC */
  1806. new_fd = dup(fd);
  1807. __archive_ensure_cloexec_flag(new_fd);
  1808. return (new_fd);
  1809. }
  1810. /*
  1811. * Add a directory path to the current stack.
  1812. */
  1813. static void
  1814. tree_push(struct tree *t, const char *path, int filesystem_id,
  1815. int64_t dev, int64_t ino, struct restore_time *rt)
  1816. {
  1817. struct tree_entry *te;
  1818. te = malloc(sizeof(*te));
  1819. memset(te, 0, sizeof(*te));
  1820. te->next = t->stack;
  1821. te->parent = t->current;
  1822. if (te->parent)
  1823. te->depth = te->parent->depth + 1;
  1824. t->stack = te;
  1825. archive_string_init(&te->name);
  1826. te->symlink_parent_fd = -1;
  1827. archive_strcpy(&te->name, path);
  1828. te->flags = needsDescent | needsOpen | needsAscent;
  1829. te->filesystem_id = filesystem_id;
  1830. te->dev = dev;
  1831. te->ino = ino;
  1832. te->dirname_length = t->dirname_length;
  1833. te->restore_time.name = te->name.s;
  1834. if (rt != NULL) {
  1835. te->restore_time.mtime = rt->mtime;
  1836. te->restore_time.mtime_nsec = rt->mtime_nsec;
  1837. te->restore_time.atime = rt->atime;
  1838. te->restore_time.atime_nsec = rt->atime_nsec;
  1839. te->restore_time.filetype = rt->filetype;
  1840. te->restore_time.noatime = rt->noatime;
  1841. }
  1842. }
  1843. /*
  1844. * Append a name to the current dir path.
  1845. */
  1846. static void
  1847. tree_append(struct tree *t, const char *name, size_t name_length)
  1848. {
  1849. size_t size_needed;
  1850. t->path.s[t->dirname_length] = '\0';
  1851. t->path.length = t->dirname_length;
  1852. /* Strip trailing '/' from name, unless entire name is "/". */
  1853. while (name_length > 1 && name[name_length - 1] == '/')
  1854. name_length--;
  1855. /* Resize pathname buffer as needed. */
  1856. size_needed = name_length + t->dirname_length + 2;
  1857. archive_string_ensure(&t->path, size_needed);
  1858. /* Add a separating '/' if it's needed. */
  1859. if (t->dirname_length > 0 && t->path.s[archive_strlen(&t->path)-1] != '/')
  1860. archive_strappend_char(&t->path, '/');
  1861. t->basename = t->path.s + archive_strlen(&t->path);
  1862. archive_strncat(&t->path, name, name_length);
  1863. t->restore_time.name = t->basename;
  1864. }
  1865. /*
  1866. * Open a directory tree for traversal.
  1867. */
  1868. static struct tree *
  1869. tree_open(const char *path, int symlink_mode, int restore_time)
  1870. {
  1871. struct tree *t;
  1872. if ((t = malloc(sizeof(*t))) == NULL)
  1873. return (NULL);
  1874. memset(t, 0, sizeof(*t));
  1875. archive_string_init(&t->path);
  1876. archive_string_ensure(&t->path, 31);
  1877. t->initial_symlink_mode = symlink_mode;
  1878. return (tree_reopen(t, path, restore_time));
  1879. }
  1880. static struct tree *
  1881. tree_reopen(struct tree *t, const char *path, int restore_time)
  1882. {
  1883. t->flags = (restore_time)?needsRestoreTimes:0;
  1884. t->flags |= onInitialDir;
  1885. t->visit_type = 0;
  1886. t->tree_errno = 0;
  1887. t->dirname_length = 0;
  1888. t->depth = 0;
  1889. t->descend = 0;
  1890. t->current = NULL;
  1891. t->d = INVALID_DIR_HANDLE;
  1892. t->symlink_mode = t->initial_symlink_mode;
  1893. archive_string_empty(&t->path);
  1894. t->entry_fd = -1;
  1895. t->entry_eof = 0;
  1896. t->entry_remaining_bytes = 0;
  1897. t->initial_filesystem_id = -1;
  1898. /* First item is set up a lot like a symlink traversal. */
  1899. tree_push(t, path, 0, 0, 0, NULL);
  1900. t->stack->flags = needsFirstVisit;
  1901. t->maxOpenCount = t->openCount = 1;
  1902. t->initial_dir_fd = open(".", O_RDONLY | O_CLOEXEC);
  1903. __archive_ensure_cloexec_flag(t->initial_dir_fd);
  1904. t->working_dir_fd = tree_dup(t->initial_dir_fd);
  1905. return (t);
  1906. }
  1907. static int
  1908. tree_descent(struct tree *t)
  1909. {
  1910. int flag, new_fd, r = 0;
  1911. t->dirname_length = archive_strlen(&t->path);
  1912. flag = O_RDONLY | O_CLOEXEC;
  1913. #if defined(O_DIRECTORY)
  1914. flag |= O_DIRECTORY;
  1915. #endif
  1916. new_fd = open_on_current_dir(t, t->stack->name.s, flag);
  1917. __archive_ensure_cloexec_flag(new_fd);
  1918. if (new_fd < 0) {
  1919. t->tree_errno = errno;
  1920. r = TREE_ERROR_DIR;
  1921. } else {
  1922. t->depth++;
  1923. /* If it is a link, set up fd for the ascent. */
  1924. if (t->stack->flags & isDirLink) {
  1925. t->stack->symlink_parent_fd = t->working_dir_fd;
  1926. t->openCount++;
  1927. if (t->openCount > t->maxOpenCount)
  1928. t->maxOpenCount = t->openCount;
  1929. } else
  1930. close(t->working_dir_fd);
  1931. /* Renew the current working directory. */
  1932. t->working_dir_fd = new_fd;
  1933. t->flags &= ~onWorkingDir;
  1934. }
  1935. return (r);
  1936. }
  1937. /*
  1938. * We've finished a directory; ascend back to the parent.
  1939. */
  1940. static int
  1941. tree_ascend(struct tree *t)
  1942. {
  1943. struct tree_entry *te;
  1944. int new_fd, r = 0, prev_dir_fd;
  1945. te = t->stack;
  1946. prev_dir_fd = t->working_dir_fd;
  1947. if (te->flags & isDirLink)
  1948. new_fd = te->symlink_parent_fd;
  1949. else {
  1950. new_fd = open_on_current_dir(t, "..", O_RDONLY | O_CLOEXEC);
  1951. __archive_ensure_cloexec_flag(new_fd);
  1952. }
  1953. if (new_fd < 0) {
  1954. t->tree_errno = errno;
  1955. r = TREE_ERROR_FATAL;
  1956. } else {
  1957. /* Renew the current working directory. */
  1958. t->working_dir_fd = new_fd;
  1959. t->flags &= ~onWorkingDir;
  1960. /* Current directory has been changed, we should
  1961. * close an fd of previous working directory. */
  1962. close_and_restore_time(prev_dir_fd, t, &te->restore_time);
  1963. if (te->flags & isDirLink) {
  1964. t->openCount--;
  1965. te->symlink_parent_fd = -1;
  1966. }
  1967. t->depth--;
  1968. }
  1969. return (r);
  1970. }
  1971. /*
  1972. * Return to the initial directory where tree_open() was performed.
  1973. */
  1974. static int
  1975. tree_enter_initial_dir(struct tree *t)
  1976. {
  1977. int r = 0;
  1978. if ((t->flags & onInitialDir) == 0) {
  1979. r = fchdir(t->initial_dir_fd);
  1980. if (r == 0) {
  1981. t->flags &= ~onWorkingDir;
  1982. t->flags |= onInitialDir;
  1983. }
  1984. }
  1985. return (r);
  1986. }
  1987. /*
  1988. * Restore working directory of directory traversals.
  1989. */
  1990. static int
  1991. tree_enter_working_dir(struct tree *t)
  1992. {
  1993. int r = 0;
  1994. /*
  1995. * Change the current directory if really needed.
  1996. * Sometimes this is unneeded when we did not do
  1997. * descent.
  1998. */
  1999. if (t->depth > 0 && (t->flags & onWorkingDir) == 0) {
  2000. r = fchdir(t->working_dir_fd);
  2001. if (r == 0) {
  2002. t->flags &= ~onInitialDir;
  2003. t->flags |= onWorkingDir;
  2004. }
  2005. }
  2006. return (r);
  2007. }
  2008. static int
  2009. tree_current_dir_fd(struct tree *t)
  2010. {
  2011. return (t->working_dir_fd);
  2012. }
  2013. /*
  2014. * Pop the working stack.
  2015. */
  2016. static void
  2017. tree_pop(struct tree *t)
  2018. {
  2019. struct tree_entry *te;
  2020. t->path.s[t->dirname_length] = '\0';
  2021. t->path.length = t->dirname_length;
  2022. if (t->stack == t->current && t->current != NULL)
  2023. t->current = t->current->parent;
  2024. te = t->stack;
  2025. t->stack = te->next;
  2026. t->dirname_length = te->dirname_length;
  2027. t->basename = t->path.s + t->dirname_length;
  2028. while (t->basename[0] == '/')
  2029. t->basename++;
  2030. archive_string_free(&te->name);
  2031. free(te);
  2032. }
  2033. /*
  2034. * Get the next item in the tree traversal.
  2035. */
  2036. static int
  2037. tree_next(struct tree *t)
  2038. {
  2039. int r;
  2040. while (t->stack != NULL) {
  2041. /* If there's an open dir, get the next entry from there. */
  2042. if (t->d != INVALID_DIR_HANDLE) {
  2043. r = tree_dir_next_posix(t);
  2044. if (r == 0)
  2045. continue;
  2046. return (r);
  2047. }
  2048. if (t->stack->flags & needsFirstVisit) {
  2049. /* Top stack item needs a regular visit. */
  2050. t->current = t->stack;
  2051. tree_append(t, t->stack->name.s,
  2052. archive_strlen(&(t->stack->name)));
  2053. /* t->dirname_length = t->path_length; */
  2054. /* tree_pop(t); */
  2055. t->stack->flags &= ~needsFirstVisit;
  2056. return (t->visit_type = TREE_REGULAR);
  2057. } else if (t->stack->flags & needsDescent) {
  2058. /* Top stack item is dir to descend into. */
  2059. t->current = t->stack;
  2060. tree_append(t, t->stack->name.s,
  2061. archive_strlen(&(t->stack->name)));
  2062. t->stack->flags &= ~needsDescent;
  2063. r = tree_descent(t);
  2064. if (r != 0) {
  2065. tree_pop(t);
  2066. t->visit_type = r;
  2067. } else
  2068. t->visit_type = TREE_POSTDESCENT;
  2069. return (t->visit_type);
  2070. } else if (t->stack->flags & needsOpen) {
  2071. t->stack->flags &= ~needsOpen;
  2072. r = tree_dir_next_posix(t);
  2073. if (r == 0)
  2074. continue;
  2075. return (r);
  2076. } else if (t->stack->flags & needsAscent) {
  2077. /* Top stack item is dir and we're done with it. */
  2078. r = tree_ascend(t);
  2079. tree_pop(t);
  2080. t->visit_type = r != 0 ? r : TREE_POSTASCENT;
  2081. return (t->visit_type);
  2082. } else {
  2083. /* Top item on stack is dead. */
  2084. tree_pop(t);
  2085. t->flags &= ~hasLstat;
  2086. t->flags &= ~hasStat;
  2087. }
  2088. }
  2089. return (t->visit_type = 0);
  2090. }
  2091. static int
  2092. tree_dir_next_posix(struct tree *t)
  2093. {
  2094. int r;
  2095. const char *name;
  2096. size_t namelen;
  2097. if (t->d == NULL) {
  2098. #if defined(HAVE_READDIR_R)
  2099. size_t dirent_size;
  2100. #endif
  2101. #if defined(HAVE_FDOPENDIR)
  2102. t->d = fdopendir(tree_dup(t->working_dir_fd));
  2103. #else /* HAVE_FDOPENDIR */
  2104. if (tree_enter_working_dir(t) == 0) {
  2105. t->d = opendir(".");
  2106. #if HAVE_DIRFD || defined(dirfd)
  2107. __archive_ensure_cloexec_flag(dirfd(t->d));
  2108. #endif
  2109. }
  2110. #endif /* HAVE_FDOPENDIR */
  2111. if (t->d == NULL) {
  2112. r = tree_ascend(t); /* Undo "chdir" */
  2113. tree_pop(t);
  2114. t->tree_errno = errno;
  2115. t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
  2116. return (t->visit_type);
  2117. }
  2118. #if defined(HAVE_READDIR_R)
  2119. dirent_size = offsetof(struct dirent, d_name) +
  2120. t->filesystem_table[t->current->filesystem_id].name_max + 1;
  2121. if (t->dirent == NULL || t->dirent_allocated < dirent_size) {
  2122. free(t->dirent);
  2123. t->dirent = malloc(dirent_size);
  2124. if (t->dirent == NULL) {
  2125. closedir(t->d);
  2126. t->d = INVALID_DIR_HANDLE;
  2127. (void)tree_ascend(t);
  2128. tree_pop(t);
  2129. t->tree_errno = ENOMEM;
  2130. t->visit_type = TREE_ERROR_DIR;
  2131. return (t->visit_type);
  2132. }
  2133. t->dirent_allocated = dirent_size;
  2134. }
  2135. #endif /* HAVE_READDIR_R */
  2136. }
  2137. for (;;) {
  2138. errno = 0;
  2139. #if defined(HAVE_READDIR_R)
  2140. r = readdir_r(t->d, t->dirent, &t->de);
  2141. #ifdef _AIX
  2142. /* Note: According to the man page, return value 9 indicates
  2143. * that the readdir_r was not successful and the error code
  2144. * is set to the global errno variable. And then if the end
  2145. * of directory entries was reached, the return value is 9
  2146. * and the third parameter is set to NULL and errno is
  2147. * unchanged. */
  2148. if (r == 9)
  2149. r = errno;
  2150. #endif /* _AIX */
  2151. if (r != 0 || t->de == NULL) {
  2152. #else
  2153. t->de = readdir(t->d);
  2154. if (t->de == NULL) {
  2155. r = errno;
  2156. #endif
  2157. closedir(t->d);
  2158. t->d = INVALID_DIR_HANDLE;
  2159. if (r != 0) {
  2160. t->tree_errno = r;
  2161. t->visit_type = TREE_ERROR_DIR;
  2162. return (t->visit_type);
  2163. } else
  2164. return (0);
  2165. }
  2166. name = t->de->d_name;
  2167. namelen = D_NAMELEN(t->de);
  2168. t->flags &= ~hasLstat;
  2169. t->flags &= ~hasStat;
  2170. if (name[0] == '.' && name[1] == '\0')
  2171. continue;
  2172. if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
  2173. continue;
  2174. tree_append(t, name, namelen);
  2175. return (t->visit_type = TREE_REGULAR);
  2176. }
  2177. }
  2178. /*
  2179. * Get the stat() data for the entry just returned from tree_next().
  2180. */
  2181. static const struct stat *
  2182. tree_current_stat(struct tree *t)
  2183. {
  2184. if (!(t->flags & hasStat)) {
  2185. #ifdef HAVE_FSTATAT
  2186. if (fstatat(tree_current_dir_fd(t),
  2187. tree_current_access_path(t), &t->st, 0) != 0)
  2188. #else
  2189. if (tree_enter_working_dir(t) != 0)
  2190. return NULL;
  2191. if (stat(tree_current_access_path(t), &t->st) != 0)
  2192. #endif
  2193. return NULL;
  2194. t->flags |= hasStat;
  2195. }
  2196. return (&t->st);
  2197. }
  2198. /*
  2199. * Get the lstat() data for the entry just returned from tree_next().
  2200. */
  2201. static const struct stat *
  2202. tree_current_lstat(struct tree *t)
  2203. {
  2204. if (!(t->flags & hasLstat)) {
  2205. #ifdef HAVE_FSTATAT
  2206. if (fstatat(tree_current_dir_fd(t),
  2207. tree_current_access_path(t), &t->lst,
  2208. AT_SYMLINK_NOFOLLOW) != 0)
  2209. #else
  2210. if (tree_enter_working_dir(t) != 0)
  2211. return NULL;
  2212. if (lstat(tree_current_access_path(t), &t->lst) != 0)
  2213. #endif
  2214. return NULL;
  2215. t->flags |= hasLstat;
  2216. }
  2217. return (&t->lst);
  2218. }
  2219. /*
  2220. * Test whether current entry is a dir or link to a dir.
  2221. */
  2222. static int
  2223. tree_current_is_dir(struct tree *t)
  2224. {
  2225. const struct stat *st;
  2226. /*
  2227. * If we already have lstat() info, then try some
  2228. * cheap tests to determine if this is a dir.
  2229. */
  2230. if (t->flags & hasLstat) {
  2231. /* If lstat() says it's a dir, it must be a dir. */
  2232. st = tree_current_lstat(t);
  2233. if (st == NULL)
  2234. return 0;
  2235. if (S_ISDIR(st->st_mode))
  2236. return 1;
  2237. /* Not a dir; might be a link to a dir. */
  2238. /* If it's not a link, then it's not a link to a dir. */
  2239. if (!S_ISLNK(st->st_mode))
  2240. return 0;
  2241. /*
  2242. * It's a link, but we don't know what it's a link to,
  2243. * so we'll have to use stat().
  2244. */
  2245. }
  2246. st = tree_current_stat(t);
  2247. /* If we can't stat it, it's not a dir. */
  2248. if (st == NULL)
  2249. return 0;
  2250. /* Use the definitive test. Hopefully this is cached. */
  2251. return (S_ISDIR(st->st_mode));
  2252. }
  2253. /*
  2254. * Test whether current entry is a physical directory. Usually, we
  2255. * already have at least one of stat() or lstat() in memory, so we
  2256. * use tricks to try to avoid an extra trip to the disk.
  2257. */
  2258. static int
  2259. tree_current_is_physical_dir(struct tree *t)
  2260. {
  2261. const struct stat *st;
  2262. /*
  2263. * If stat() says it isn't a dir, then it's not a dir.
  2264. * If stat() data is cached, this check is free, so do it first.
  2265. */
  2266. if (t->flags & hasStat) {
  2267. st = tree_current_stat(t);
  2268. if (st == NULL)
  2269. return (0);
  2270. if (!S_ISDIR(st->st_mode))
  2271. return (0);
  2272. }
  2273. /*
  2274. * Either stat() said it was a dir (in which case, we have
  2275. * to determine whether it's really a link to a dir) or
  2276. * stat() info wasn't available. So we use lstat(), which
  2277. * hopefully is already cached.
  2278. */
  2279. st = tree_current_lstat(t);
  2280. /* If we can't stat it, it's not a dir. */
  2281. if (st == NULL)
  2282. return 0;
  2283. /* Use the definitive test. Hopefully this is cached. */
  2284. return (S_ISDIR(st->st_mode));
  2285. }
  2286. /*
  2287. * Test whether the same file has been in the tree as its parent.
  2288. */
  2289. static int
  2290. tree_target_is_same_as_parent(struct tree *t, const struct stat *st)
  2291. {
  2292. struct tree_entry *te;
  2293. for (te = t->current->parent; te != NULL; te = te->parent) {
  2294. if (te->dev == (int64_t)st->st_dev &&
  2295. te->ino == (int64_t)st->st_ino)
  2296. return (1);
  2297. }
  2298. return (0);
  2299. }
  2300. /*
  2301. * Test whether the current file is symbolic link target and
  2302. * on the other filesystem.
  2303. */
  2304. static int
  2305. tree_current_is_symblic_link_target(struct tree *t)
  2306. {
  2307. static const struct stat *lst, *st;
  2308. lst = tree_current_lstat(t);
  2309. st = tree_current_stat(t);
  2310. return (st != NULL && lst != NULL &&
  2311. (int64_t)st->st_dev == t->current_filesystem->dev &&
  2312. st->st_dev != lst->st_dev);
  2313. }
  2314. /*
  2315. * Return the access path for the entry just returned from tree_next().
  2316. */
  2317. static const char *
  2318. tree_current_access_path(struct tree *t)
  2319. {
  2320. return (t->basename);
  2321. }
  2322. /*
  2323. * Return the full path for the entry just returned from tree_next().
  2324. */
  2325. static const char *
  2326. tree_current_path(struct tree *t)
  2327. {
  2328. return (t->path.s);
  2329. }
  2330. /*
  2331. * Terminate the traversal.
  2332. */
  2333. static void
  2334. tree_close(struct tree *t)
  2335. {
  2336. if (t == NULL)
  2337. return;
  2338. if (t->entry_fd >= 0) {
  2339. close_and_restore_time(t->entry_fd, t, &t->restore_time);
  2340. t->entry_fd = -1;
  2341. }
  2342. /* Close the handle of readdir(). */
  2343. if (t->d != INVALID_DIR_HANDLE) {
  2344. closedir(t->d);
  2345. t->d = INVALID_DIR_HANDLE;
  2346. }
  2347. /* Release anything remaining in the stack. */
  2348. while (t->stack != NULL) {
  2349. if (t->stack->flags & isDirLink)
  2350. close(t->stack->symlink_parent_fd);
  2351. tree_pop(t);
  2352. }
  2353. if (t->working_dir_fd >= 0) {
  2354. close(t->working_dir_fd);
  2355. t->working_dir_fd = -1;
  2356. }
  2357. if (t->initial_dir_fd >= 0) {
  2358. close(t->initial_dir_fd);
  2359. t->initial_dir_fd = -1;
  2360. }
  2361. }
  2362. /*
  2363. * Release any resources.
  2364. */
  2365. static void
  2366. tree_free(struct tree *t)
  2367. {
  2368. int i;
  2369. if (t == NULL)
  2370. return;
  2371. archive_string_free(&t->path);
  2372. #if defined(HAVE_READDIR_R)
  2373. free(t->dirent);
  2374. #endif
  2375. free(t->sparse_list);
  2376. for (i = 0; i < t->max_filesystem_id; i++)
  2377. free(t->filesystem_table[i].allocation_ptr);
  2378. free(t->filesystem_table);
  2379. free(t);
  2380. }
  2381. #endif