archive_read_disk_posix.c 70 KB

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