archive_read_disk_posix.c 73 KB

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