archive_read_disk_posix.c 70 KB

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