archive_read_disk_posix.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770
  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 *, int, 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. int 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) < 0) {
  711. archive_set_error(&a->archive, errno, "Seek error");
  712. r = ARCHIVE_FATAL;
  713. a->archive.state = ARCHIVE_STATE_FATAL;
  714. goto abort_read_data;
  715. }
  716. sparse_bytes = t->current_sparse->offset - t->entry_total;
  717. t->entry_remaining_bytes -= sparse_bytes;
  718. t->entry_total += sparse_bytes;
  719. }
  720. /*
  721. * Read file contents.
  722. */
  723. if (buffbytes > 0) {
  724. bytes = read(t->entry_fd, t->entry_buff, buffbytes);
  725. if (bytes < 0) {
  726. archive_set_error(&a->archive, errno, "Read error");
  727. r = ARCHIVE_FATAL;
  728. a->archive.state = ARCHIVE_STATE_FATAL;
  729. goto abort_read_data;
  730. }
  731. } else
  732. bytes = 0;
  733. /*
  734. * Return an EOF unless we've read a leading empty sparse region, which
  735. * is used to represent fully-sparse files.
  736. */
  737. if (bytes == 0 && !empty_sparse_region) {
  738. /* Get EOF */
  739. t->entry_eof = 1;
  740. r = ARCHIVE_EOF;
  741. goto abort_read_data;
  742. }
  743. *buff = t->entry_buff;
  744. *size = bytes;
  745. *offset = t->entry_total;
  746. t->entry_total += bytes;
  747. t->entry_remaining_bytes -= bytes;
  748. if (t->entry_remaining_bytes == 0) {
  749. /* Close the current file descriptor */
  750. close_and_restore_time(t->entry_fd, t, &t->restore_time);
  751. t->entry_fd = -1;
  752. t->entry_eof = 1;
  753. }
  754. t->current_sparse->offset += bytes;
  755. t->current_sparse->length -= bytes;
  756. if (t->current_sparse->length == 0 && !t->entry_eof)
  757. t->current_sparse++;
  758. return (ARCHIVE_OK);
  759. abort_read_data:
  760. *buff = NULL;
  761. *size = 0;
  762. *offset = t->entry_total;
  763. if (t->entry_fd >= 0) {
  764. /* Close the current file descriptor */
  765. close_and_restore_time(t->entry_fd, t, &t->restore_time);
  766. t->entry_fd = -1;
  767. }
  768. return (r);
  769. }
  770. static int
  771. next_entry(struct archive_read_disk *a, struct tree *t,
  772. struct archive_entry *entry)
  773. {
  774. const struct stat *st; /* info to use for this entry */
  775. const struct stat *lst;/* lstat() information */
  776. const char *name;
  777. int delayed, delayed_errno, descend, r;
  778. struct archive_string delayed_str;
  779. delayed = ARCHIVE_OK;
  780. delayed_errno = 0;
  781. archive_string_init(&delayed_str);
  782. st = NULL;
  783. lst = NULL;
  784. t->descend = 0;
  785. do {
  786. switch (tree_next(t)) {
  787. case TREE_ERROR_FATAL:
  788. archive_set_error(&a->archive, t->tree_errno,
  789. "%s: Unable to continue traversing directory tree",
  790. tree_current_path(t));
  791. a->archive.state = ARCHIVE_STATE_FATAL;
  792. tree_enter_initial_dir(t);
  793. return (ARCHIVE_FATAL);
  794. case TREE_ERROR_DIR:
  795. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  796. "%s: Couldn't visit directory",
  797. tree_current_path(t));
  798. tree_enter_initial_dir(t);
  799. return (ARCHIVE_FAILED);
  800. case 0:
  801. tree_enter_initial_dir(t);
  802. return (ARCHIVE_EOF);
  803. case TREE_POSTDESCENT:
  804. case TREE_POSTASCENT:
  805. break;
  806. case TREE_REGULAR:
  807. lst = tree_current_lstat(t);
  808. if (lst == NULL) {
  809. if (errno == ENOENT && t->depth > 0) {
  810. delayed = ARCHIVE_WARN;
  811. delayed_errno = errno;
  812. if (delayed_str.length == 0) {
  813. archive_string_sprintf(&delayed_str,
  814. "%s", tree_current_path(t));
  815. } else {
  816. archive_string_sprintf(&delayed_str,
  817. " %s", tree_current_path(t));
  818. }
  819. } else {
  820. archive_set_error(&a->archive, errno,
  821. "%s: Cannot stat",
  822. tree_current_path(t));
  823. tree_enter_initial_dir(t);
  824. return (ARCHIVE_FAILED);
  825. }
  826. }
  827. break;
  828. }
  829. } while (lst == NULL);
  830. #ifdef __APPLE__
  831. if (a->flags & ARCHIVE_READDISK_MAC_COPYFILE) {
  832. /* If we're using copyfile(), ignore "._XXX" files. */
  833. const char *bname = strrchr(tree_current_path(t), '/');
  834. if (bname == NULL)
  835. bname = tree_current_path(t);
  836. else
  837. ++bname;
  838. if (bname[0] == '.' && bname[1] == '_')
  839. return (ARCHIVE_RETRY);
  840. }
  841. #endif
  842. archive_entry_copy_pathname(entry, tree_current_path(t));
  843. /*
  844. * Perform path matching.
  845. */
  846. if (a->matching) {
  847. r = archive_match_path_excluded(a->matching, entry);
  848. if (r < 0) {
  849. archive_set_error(&(a->archive), errno,
  850. "Failed : %s", archive_error_string(a->matching));
  851. return (r);
  852. }
  853. if (r) {
  854. if (a->excluded_cb_func)
  855. a->excluded_cb_func(&(a->archive),
  856. a->excluded_cb_data, entry);
  857. return (ARCHIVE_RETRY);
  858. }
  859. }
  860. /*
  861. * Distinguish 'L'/'P'/'H' symlink following.
  862. */
  863. switch(t->symlink_mode) {
  864. case 'H':
  865. /* 'H': After the first item, rest like 'P'. */
  866. t->symlink_mode = 'P';
  867. /* 'H': First item (from command line) like 'L'. */
  868. /* FALLTHROUGH */
  869. case 'L':
  870. /* 'L': Do descend through a symlink to dir. */
  871. descend = tree_current_is_dir(t);
  872. /* 'L': Follow symlinks to files. */
  873. a->symlink_mode = 'L';
  874. a->follow_symlinks = 1;
  875. /* 'L': Archive symlinks as targets, if we can. */
  876. st = tree_current_stat(t);
  877. if (st != NULL && !tree_target_is_same_as_parent(t, st))
  878. break;
  879. /* If stat fails, we have a broken symlink;
  880. * in that case, don't follow the link. */
  881. /* FALLTHROUGH */
  882. default:
  883. /* 'P': Don't descend through a symlink to dir. */
  884. descend = tree_current_is_physical_dir(t);
  885. /* 'P': Don't follow symlinks to files. */
  886. a->symlink_mode = 'P';
  887. a->follow_symlinks = 0;
  888. /* 'P': Archive symlinks as symlinks. */
  889. st = lst;
  890. break;
  891. }
  892. if (update_current_filesystem(a, st->st_dev) != ARCHIVE_OK) {
  893. a->archive.state = ARCHIVE_STATE_FATAL;
  894. tree_enter_initial_dir(t);
  895. return (ARCHIVE_FATAL);
  896. }
  897. if (t->initial_filesystem_id == -1)
  898. t->initial_filesystem_id = t->current_filesystem_id;
  899. if (a->flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS) {
  900. if (t->initial_filesystem_id != t->current_filesystem_id)
  901. descend = 0;
  902. }
  903. t->descend = descend;
  904. /*
  905. * Honor nodump flag.
  906. * If the file is marked with nodump flag, do not return this entry.
  907. */
  908. if (a->flags & ARCHIVE_READDISK_HONOR_NODUMP) {
  909. #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
  910. if (st->st_flags & UF_NODUMP)
  911. return (ARCHIVE_RETRY);
  912. #elif (defined(FS_IOC_GETFLAGS) && defined(FS_NODUMP_FL) && \
  913. defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \
  914. (defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL) && \
  915. defined(HAVE_WORKING_EXT2_IOC_GETFLAGS))
  916. if (S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) {
  917. int stflags;
  918. t->entry_fd = open_on_current_dir(t,
  919. tree_current_access_path(t),
  920. O_RDONLY | O_NONBLOCK | O_CLOEXEC);
  921. __archive_ensure_cloexec_flag(t->entry_fd);
  922. if (t->entry_fd >= 0) {
  923. r = ioctl(t->entry_fd,
  924. #ifdef FS_IOC_GETFLAGS
  925. FS_IOC_GETFLAGS,
  926. #else
  927. EXT2_IOC_GETFLAGS,
  928. #endif
  929. &stflags);
  930. #ifdef FS_NODUMP_FL
  931. if (r == 0 && (stflags & FS_NODUMP_FL) != 0)
  932. #else
  933. if (r == 0 && (stflags & EXT2_NODUMP_FL) != 0)
  934. #endif
  935. return (ARCHIVE_RETRY);
  936. }
  937. }
  938. #endif
  939. }
  940. archive_entry_copy_stat(entry, st);
  941. /* Save the times to be restored. This must be in before
  942. * calling archive_read_disk_descend() or any chance of it,
  943. * especially, invoking a callback. */
  944. t->restore_time.mtime = archive_entry_mtime(entry);
  945. t->restore_time.mtime_nsec = archive_entry_mtime_nsec(entry);
  946. t->restore_time.atime = archive_entry_atime(entry);
  947. t->restore_time.atime_nsec = archive_entry_atime_nsec(entry);
  948. t->restore_time.filetype = archive_entry_filetype(entry);
  949. t->restore_time.noatime = t->current_filesystem->noatime;
  950. /*
  951. * Perform time matching.
  952. */
  953. if (a->matching) {
  954. r = archive_match_time_excluded(a->matching, entry);
  955. if (r < 0) {
  956. archive_set_error(&(a->archive), errno,
  957. "Failed : %s", archive_error_string(a->matching));
  958. return (r);
  959. }
  960. if (r) {
  961. if (a->excluded_cb_func)
  962. a->excluded_cb_func(&(a->archive),
  963. a->excluded_cb_data, entry);
  964. return (ARCHIVE_RETRY);
  965. }
  966. }
  967. /* Lookup uname/gname */
  968. name = archive_read_disk_uname(&(a->archive), archive_entry_uid(entry));
  969. if (name != NULL)
  970. archive_entry_copy_uname(entry, name);
  971. name = archive_read_disk_gname(&(a->archive), archive_entry_gid(entry));
  972. if (name != NULL)
  973. archive_entry_copy_gname(entry, name);
  974. /*
  975. * Perform owner matching.
  976. */
  977. if (a->matching) {
  978. r = archive_match_owner_excluded(a->matching, entry);
  979. if (r < 0) {
  980. archive_set_error(&(a->archive), errno,
  981. "Failed : %s", archive_error_string(a->matching));
  982. return (r);
  983. }
  984. if (r) {
  985. if (a->excluded_cb_func)
  986. a->excluded_cb_func(&(a->archive),
  987. a->excluded_cb_data, entry);
  988. return (ARCHIVE_RETRY);
  989. }
  990. }
  991. /*
  992. * Invoke a meta data filter callback.
  993. */
  994. if (a->metadata_filter_func) {
  995. if (!a->metadata_filter_func(&(a->archive),
  996. a->metadata_filter_data, entry))
  997. return (ARCHIVE_RETRY);
  998. }
  999. /*
  1000. * Populate the archive_entry with metadata from the disk.
  1001. */
  1002. archive_entry_copy_sourcepath(entry, tree_current_access_path(t));
  1003. r = archive_read_disk_entry_from_file(&(a->archive), entry,
  1004. t->entry_fd, st);
  1005. if (r == ARCHIVE_OK) {
  1006. r = delayed;
  1007. if (r != ARCHIVE_OK) {
  1008. archive_string_sprintf(&delayed_str, ": %s",
  1009. "File removed before we read it");
  1010. archive_set_error(&(a->archive), delayed_errno,
  1011. "%s", delayed_str.s);
  1012. }
  1013. }
  1014. archive_string_free(&delayed_str);
  1015. return (r);
  1016. }
  1017. static int
  1018. _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
  1019. {
  1020. int ret;
  1021. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1022. *entryp = NULL;
  1023. ret = _archive_read_next_header2(_a, a->entry);
  1024. *entryp = a->entry;
  1025. return ret;
  1026. }
  1027. static int
  1028. _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
  1029. {
  1030. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1031. struct tree *t;
  1032. int r;
  1033. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1034. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1035. "archive_read_next_header2");
  1036. t = a->tree;
  1037. if (t->entry_fd >= 0) {
  1038. close_and_restore_time(t->entry_fd, t, &t->restore_time);
  1039. t->entry_fd = -1;
  1040. }
  1041. archive_entry_clear(entry);
  1042. for (;;) {
  1043. r = next_entry(a, t, entry);
  1044. if (t->entry_fd >= 0) {
  1045. close(t->entry_fd);
  1046. t->entry_fd = -1;
  1047. }
  1048. if (r == ARCHIVE_RETRY) {
  1049. archive_entry_clear(entry);
  1050. continue;
  1051. }
  1052. break;
  1053. }
  1054. /* Return to the initial directory. */
  1055. tree_enter_initial_dir(t);
  1056. /*
  1057. * EOF and FATAL are persistent at this layer. By
  1058. * modifying the state, we guarantee that future calls to
  1059. * read a header or read data will fail.
  1060. */
  1061. switch (r) {
  1062. case ARCHIVE_EOF:
  1063. a->archive.state = ARCHIVE_STATE_EOF;
  1064. break;
  1065. case ARCHIVE_OK:
  1066. case ARCHIVE_WARN:
  1067. /* Overwrite the sourcepath based on the initial directory. */
  1068. archive_entry_copy_sourcepath(entry, tree_current_path(t));
  1069. t->entry_total = 0;
  1070. if (archive_entry_filetype(entry) == AE_IFREG) {
  1071. t->nlink = archive_entry_nlink(entry);
  1072. t->entry_remaining_bytes = archive_entry_size(entry);
  1073. t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
  1074. if (!t->entry_eof &&
  1075. setup_sparse(a, entry) != ARCHIVE_OK)
  1076. return (ARCHIVE_FATAL);
  1077. } else {
  1078. t->entry_remaining_bytes = 0;
  1079. t->entry_eof = 1;
  1080. }
  1081. a->archive.state = ARCHIVE_STATE_DATA;
  1082. break;
  1083. case ARCHIVE_RETRY:
  1084. break;
  1085. case ARCHIVE_FATAL:
  1086. a->archive.state = ARCHIVE_STATE_FATAL;
  1087. break;
  1088. }
  1089. __archive_reset_read_data(&a->archive);
  1090. return (r);
  1091. }
  1092. static int
  1093. setup_sparse(struct archive_read_disk *a, struct archive_entry *entry)
  1094. {
  1095. struct tree *t = a->tree;
  1096. int64_t length, offset;
  1097. int i;
  1098. t->sparse_count = archive_entry_sparse_reset(entry);
  1099. if (t->sparse_count+1 > t->sparse_list_size) {
  1100. free(t->sparse_list);
  1101. t->sparse_list_size = t->sparse_count + 1;
  1102. t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
  1103. t->sparse_list_size);
  1104. if (t->sparse_list == NULL) {
  1105. t->sparse_list_size = 0;
  1106. archive_set_error(&a->archive, ENOMEM,
  1107. "Can't allocate data");
  1108. a->archive.state = ARCHIVE_STATE_FATAL;
  1109. return (ARCHIVE_FATAL);
  1110. }
  1111. }
  1112. for (i = 0; i < t->sparse_count; i++) {
  1113. archive_entry_sparse_next(entry, &offset, &length);
  1114. t->sparse_list[i].offset = offset;
  1115. t->sparse_list[i].length = length;
  1116. }
  1117. if (i == 0) {
  1118. t->sparse_list[i].offset = 0;
  1119. t->sparse_list[i].length = archive_entry_size(entry);
  1120. } else {
  1121. t->sparse_list[i].offset = archive_entry_size(entry);
  1122. t->sparse_list[i].length = 0;
  1123. }
  1124. t->current_sparse = t->sparse_list;
  1125. return (ARCHIVE_OK);
  1126. }
  1127. int
  1128. archive_read_disk_set_matching(struct archive *_a, struct archive *_ma,
  1129. void (*_excluded_func)(struct archive *, void *, struct archive_entry *),
  1130. void *_client_data)
  1131. {
  1132. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1133. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1134. ARCHIVE_STATE_ANY, "archive_read_disk_set_matching");
  1135. a->matching = _ma;
  1136. a->excluded_cb_func = _excluded_func;
  1137. a->excluded_cb_data = _client_data;
  1138. return (ARCHIVE_OK);
  1139. }
  1140. int
  1141. archive_read_disk_set_metadata_filter_callback(struct archive *_a,
  1142. int (*_metadata_filter_func)(struct archive *, void *,
  1143. struct archive_entry *), void *_client_data)
  1144. {
  1145. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1146. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY,
  1147. "archive_read_disk_set_metadata_filter_callback");
  1148. a->metadata_filter_func = _metadata_filter_func;
  1149. a->metadata_filter_data = _client_data;
  1150. return (ARCHIVE_OK);
  1151. }
  1152. int
  1153. archive_read_disk_can_descend(struct archive *_a)
  1154. {
  1155. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1156. struct tree *t = a->tree;
  1157. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1158. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1159. "archive_read_disk_can_descend");
  1160. return (t->visit_type == TREE_REGULAR && t->descend);
  1161. }
  1162. /*
  1163. * Called by the client to mark the directory just returned from
  1164. * tree_next() as needing to be visited.
  1165. */
  1166. int
  1167. archive_read_disk_descend(struct archive *_a)
  1168. {
  1169. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1170. struct tree *t = a->tree;
  1171. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1172. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1173. "archive_read_disk_descend");
  1174. if (!archive_read_disk_can_descend(_a))
  1175. return (ARCHIVE_OK);
  1176. /*
  1177. * We must not treat the initial specified path as a physical dir,
  1178. * because if we do then we will try and ascend out of it by opening
  1179. * ".." which is (a) wrong and (b) causes spurious permissions errors
  1180. * if ".." is not readable by us. Instead, treat it as if it were a
  1181. * symlink. (This uses an extra fd, but it can only happen once at the
  1182. * top level of a traverse.) But we can't necessarily assume t->st is
  1183. * valid here (though t->lst is), which complicates the logic a
  1184. * little.
  1185. */
  1186. if (tree_current_is_physical_dir(t)) {
  1187. tree_push(t, t->basename, t->current_filesystem_id,
  1188. t->lst.st_dev, t->lst.st_ino, &t->restore_time);
  1189. if (t->stack->parent->parent != NULL)
  1190. t->stack->flags |= isDir;
  1191. else
  1192. t->stack->flags |= isDirLink;
  1193. } else if (tree_current_is_dir(t)) {
  1194. tree_push(t, t->basename, t->current_filesystem_id,
  1195. t->st.st_dev, t->st.st_ino, &t->restore_time);
  1196. t->stack->flags |= isDirLink;
  1197. }
  1198. t->descend = 0;
  1199. return (ARCHIVE_OK);
  1200. }
  1201. int
  1202. archive_read_disk_open(struct archive *_a, const char *pathname)
  1203. {
  1204. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1205. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1206. ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
  1207. "archive_read_disk_open");
  1208. archive_clear_error(&a->archive);
  1209. return (_archive_read_disk_open(_a, pathname));
  1210. }
  1211. int
  1212. archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
  1213. {
  1214. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1215. struct archive_string path;
  1216. int ret;
  1217. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1218. ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
  1219. "archive_read_disk_open_w");
  1220. archive_clear_error(&a->archive);
  1221. /* Make a char string from a wchar_t string. */
  1222. archive_string_init(&path);
  1223. if (archive_string_append_from_wcs(&path, pathname,
  1224. wcslen(pathname)) != 0) {
  1225. if (errno == ENOMEM)
  1226. archive_set_error(&a->archive, ENOMEM,
  1227. "Can't allocate memory");
  1228. else
  1229. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1230. "Can't convert a path to a char string");
  1231. a->archive.state = ARCHIVE_STATE_FATAL;
  1232. ret = ARCHIVE_FATAL;
  1233. } else
  1234. ret = _archive_read_disk_open(_a, path.s);
  1235. archive_string_free(&path);
  1236. return (ret);
  1237. }
  1238. static int
  1239. _archive_read_disk_open(struct archive *_a, const char *pathname)
  1240. {
  1241. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1242. if (a->tree != NULL)
  1243. a->tree = tree_reopen(a->tree, pathname,
  1244. a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
  1245. else
  1246. a->tree = tree_open(pathname, a->symlink_mode,
  1247. a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
  1248. if (a->tree == NULL) {
  1249. archive_set_error(&a->archive, ENOMEM,
  1250. "Can't allocate tar data");
  1251. a->archive.state = ARCHIVE_STATE_FATAL;
  1252. return (ARCHIVE_FATAL);
  1253. }
  1254. a->archive.state = ARCHIVE_STATE_HEADER;
  1255. return (ARCHIVE_OK);
  1256. }
  1257. /*
  1258. * Return a current filesystem ID which is index of the filesystem entry
  1259. * you've visited through archive_read_disk.
  1260. */
  1261. int
  1262. archive_read_disk_current_filesystem(struct archive *_a)
  1263. {
  1264. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1265. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
  1266. "archive_read_disk_current_filesystem");
  1267. return (a->tree->current_filesystem_id);
  1268. }
  1269. static int
  1270. update_current_filesystem(struct archive_read_disk *a, int64_t dev)
  1271. {
  1272. struct tree *t = a->tree;
  1273. int i, fid;
  1274. if (t->current_filesystem != NULL &&
  1275. t->current_filesystem->dev == dev)
  1276. return (ARCHIVE_OK);
  1277. for (i = 0; i < t->max_filesystem_id; i++) {
  1278. if (t->filesystem_table[i].dev == dev) {
  1279. /* There is the filesystem ID we've already generated. */
  1280. t->current_filesystem_id = i;
  1281. t->current_filesystem = &(t->filesystem_table[i]);
  1282. return (ARCHIVE_OK);
  1283. }
  1284. }
  1285. /*
  1286. * This is the new filesystem which we have to generate a new ID for.
  1287. */
  1288. fid = t->max_filesystem_id++;
  1289. if (t->max_filesystem_id > t->allocated_filesystem) {
  1290. size_t s;
  1291. void *p;
  1292. s = t->max_filesystem_id * 2;
  1293. p = realloc(t->filesystem_table,
  1294. s * sizeof(*t->filesystem_table));
  1295. if (p == NULL) {
  1296. archive_set_error(&a->archive, ENOMEM,
  1297. "Can't allocate tar data");
  1298. return (ARCHIVE_FATAL);
  1299. }
  1300. t->filesystem_table = (struct filesystem *)p;
  1301. t->allocated_filesystem = s;
  1302. }
  1303. t->current_filesystem_id = fid;
  1304. t->current_filesystem = &(t->filesystem_table[fid]);
  1305. t->current_filesystem->dev = dev;
  1306. t->current_filesystem->allocation_ptr = NULL;
  1307. t->current_filesystem->buff = NULL;
  1308. /* Setup the current filesystem properties which depend on
  1309. * platform specific. */
  1310. return (setup_current_filesystem(a));
  1311. }
  1312. /*
  1313. * Returns 1 if current filesystem is generated filesystem, 0 if it is not
  1314. * or -1 if it is unknown.
  1315. */
  1316. int
  1317. archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
  1318. {
  1319. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1320. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
  1321. "archive_read_disk_current_filesystem");
  1322. return (a->tree->current_filesystem->synthetic);
  1323. }
  1324. /*
  1325. * Returns 1 if current filesystem is remote filesystem, 0 if it is not
  1326. * or -1 if it is unknown.
  1327. */
  1328. int
  1329. archive_read_disk_current_filesystem_is_remote(struct archive *_a)
  1330. {
  1331. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1332. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
  1333. "archive_read_disk_current_filesystem");
  1334. return (a->tree->current_filesystem->remote);
  1335. }
  1336. #if defined(_PC_REC_INCR_XFER_SIZE) && defined(_PC_REC_MAX_XFER_SIZE) &&\
  1337. defined(_PC_REC_MIN_XFER_SIZE) && defined(_PC_REC_XFER_ALIGN)
  1338. static int
  1339. get_xfer_size(struct tree *t, int fd, const char *path)
  1340. {
  1341. t->current_filesystem->xfer_align = -1;
  1342. errno = 0;
  1343. if (fd >= 0) {
  1344. t->current_filesystem->incr_xfer_size =
  1345. fpathconf(fd, _PC_REC_INCR_XFER_SIZE);
  1346. t->current_filesystem->max_xfer_size =
  1347. fpathconf(fd, _PC_REC_MAX_XFER_SIZE);
  1348. t->current_filesystem->min_xfer_size =
  1349. fpathconf(fd, _PC_REC_MIN_XFER_SIZE);
  1350. t->current_filesystem->xfer_align =
  1351. fpathconf(fd, _PC_REC_XFER_ALIGN);
  1352. } else if (path != NULL) {
  1353. t->current_filesystem->incr_xfer_size =
  1354. pathconf(path, _PC_REC_INCR_XFER_SIZE);
  1355. t->current_filesystem->max_xfer_size =
  1356. pathconf(path, _PC_REC_MAX_XFER_SIZE);
  1357. t->current_filesystem->min_xfer_size =
  1358. pathconf(path, _PC_REC_MIN_XFER_SIZE);
  1359. t->current_filesystem->xfer_align =
  1360. pathconf(path, _PC_REC_XFER_ALIGN);
  1361. }
  1362. /* At least we need an alignment size. */
  1363. if (t->current_filesystem->xfer_align == -1)
  1364. return ((errno == EINVAL)?1:-1);
  1365. else
  1366. return (0);
  1367. }
  1368. #else
  1369. static int
  1370. get_xfer_size(struct tree *t, int fd, const char *path)
  1371. {
  1372. (void)t; /* UNUSED */
  1373. (void)fd; /* UNUSED */
  1374. (void)path; /* UNUSED */
  1375. return (1);/* Not supported */
  1376. }
  1377. #endif
  1378. #if defined(HAVE_STATVFS)
  1379. static inline __LA_UNUSED void
  1380. set_statvfs_transfer_size(struct filesystem *fs, const struct statvfs *sfs)
  1381. {
  1382. fs->xfer_align = sfs->f_frsize > 0 ? (long)sfs->f_frsize : -1;
  1383. fs->max_xfer_size = -1;
  1384. #if defined(HAVE_STRUCT_STATVFS_F_IOSIZE)
  1385. fs->min_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
  1386. fs->incr_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
  1387. #else
  1388. fs->min_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
  1389. fs->incr_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
  1390. #endif
  1391. }
  1392. #endif
  1393. #if defined(HAVE_STRUCT_STATFS)
  1394. static inline __LA_UNUSED void
  1395. set_statfs_transfer_size(struct filesystem *fs, const struct statfs *sfs)
  1396. {
  1397. fs->xfer_align = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
  1398. fs->max_xfer_size = -1;
  1399. #if defined(HAVE_STRUCT_STATFS_F_IOSIZE)
  1400. fs->min_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
  1401. fs->incr_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
  1402. #else
  1403. fs->min_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
  1404. fs->incr_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
  1405. #endif
  1406. }
  1407. #endif
  1408. #if defined(HAVE_STRUCT_STATFS) && defined(HAVE_STATFS) && \
  1409. defined(HAVE_FSTATFS) && defined(MNT_LOCAL) && !defined(ST_LOCAL)
  1410. /*
  1411. * Gather current filesystem properties on FreeBSD, OpenBSD and Mac OS X.
  1412. */
  1413. static int
  1414. setup_current_filesystem(struct archive_read_disk *a)
  1415. {
  1416. struct tree *t = a->tree;
  1417. struct statfs sfs;
  1418. #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
  1419. /* TODO: configure should set GETVFSBYNAME_ARG_TYPE to make
  1420. * this accurate; some platforms have both and we need the one that's
  1421. * used by getvfsbyname()
  1422. *
  1423. * Then the following would become:
  1424. * #if defined(GETVFSBYNAME_ARG_TYPE)
  1425. * GETVFSBYNAME_ARG_TYPE vfc;
  1426. * #endif
  1427. */
  1428. # if defined(HAVE_STRUCT_XVFSCONF)
  1429. struct xvfsconf vfc;
  1430. # else
  1431. struct vfsconf vfc;
  1432. # endif
  1433. #endif
  1434. int r, xr = 0;
  1435. #if !defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
  1436. long nm;
  1437. #endif
  1438. t->current_filesystem->synthetic = -1;
  1439. t->current_filesystem->remote = -1;
  1440. if (tree_current_is_symblic_link_target(t)) {
  1441. #if defined(HAVE_OPENAT)
  1442. /*
  1443. * Get file system statistics on any directory
  1444. * where current is.
  1445. */
  1446. int fd = openat(tree_current_dir_fd(t),
  1447. tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
  1448. __archive_ensure_cloexec_flag(fd);
  1449. if (fd < 0) {
  1450. archive_set_error(&a->archive, errno,
  1451. "openat failed");
  1452. return (ARCHIVE_FAILED);
  1453. }
  1454. r = fstatfs(fd, &sfs);
  1455. if (r == 0)
  1456. xr = get_xfer_size(t, fd, NULL);
  1457. close(fd);
  1458. #else
  1459. if (tree_enter_working_dir(t) != 0) {
  1460. archive_set_error(&a->archive, errno, "fchdir failed");
  1461. return (ARCHIVE_FAILED);
  1462. }
  1463. r = statfs(tree_current_access_path(t), &sfs);
  1464. if (r == 0)
  1465. xr = get_xfer_size(t, -1, tree_current_access_path(t));
  1466. #endif
  1467. } else {
  1468. r = fstatfs(tree_current_dir_fd(t), &sfs);
  1469. if (r == 0)
  1470. xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
  1471. }
  1472. if (r == -1 || xr == -1) {
  1473. archive_set_error(&a->archive, errno, "statfs failed");
  1474. return (ARCHIVE_FAILED);
  1475. } else if (xr == 1) {
  1476. /* pathconf(_PC_REX_*) operations are not supported. */
  1477. set_statfs_transfer_size(t->current_filesystem, &sfs);
  1478. }
  1479. if (sfs.f_flags & MNT_LOCAL)
  1480. t->current_filesystem->remote = 0;
  1481. else
  1482. t->current_filesystem->remote = 1;
  1483. #if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
  1484. r = getvfsbyname(sfs.f_fstypename, &vfc);
  1485. if (r == -1) {
  1486. archive_set_error(&a->archive, errno, "getvfsbyname failed");
  1487. return (ARCHIVE_FAILED);
  1488. }
  1489. if (vfc.vfc_flags & VFCF_SYNTHETIC)
  1490. t->current_filesystem->synthetic = 1;
  1491. else
  1492. t->current_filesystem->synthetic = 0;
  1493. #endif
  1494. #if defined(MNT_NOATIME)
  1495. if (sfs.f_flags & MNT_NOATIME)
  1496. t->current_filesystem->noatime = 1;
  1497. else
  1498. #endif
  1499. t->current_filesystem->noatime = 0;
  1500. #if defined(USE_READDIR_R)
  1501. /* Set maximum filename length. */
  1502. #if defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
  1503. t->current_filesystem->name_max = sfs.f_namemax;
  1504. #else
  1505. # if defined(_PC_NAME_MAX)
  1506. /* Mac OS X does not have f_namemax in struct statfs. */
  1507. if (tree_current_is_symblic_link_target(t)) {
  1508. if (tree_enter_working_dir(t) != 0) {
  1509. archive_set_error(&a->archive, errno, "fchdir failed");
  1510. return (ARCHIVE_FAILED);
  1511. }
  1512. nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
  1513. } else
  1514. nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
  1515. # else
  1516. nm = -1;
  1517. # endif
  1518. if (nm == -1)
  1519. t->current_filesystem->name_max = NAME_MAX;
  1520. else
  1521. t->current_filesystem->name_max = nm;
  1522. #endif
  1523. if (t->current_filesystem->name_max == 0) {
  1524. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1525. "Cannot determine name_max");
  1526. return (ARCHIVE_FAILED);
  1527. }
  1528. #endif /* USE_READDIR_R */
  1529. return (ARCHIVE_OK);
  1530. }
  1531. #elif (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS)) && defined(ST_LOCAL)
  1532. /*
  1533. * Gather current filesystem properties on NetBSD
  1534. */
  1535. static int
  1536. setup_current_filesystem(struct archive_read_disk *a)
  1537. {
  1538. struct tree *t = a->tree;
  1539. struct statvfs svfs;
  1540. int r, xr = 0;
  1541. t->current_filesystem->synthetic = -1;
  1542. if (tree_enter_working_dir(t) != 0) {
  1543. archive_set_error(&a->archive, errno, "fchdir failed");
  1544. return (ARCHIVE_FAILED);
  1545. }
  1546. if (tree_current_is_symblic_link_target(t)) {
  1547. r = statvfs(tree_current_access_path(t), &svfs);
  1548. if (r == 0)
  1549. xr = get_xfer_size(t, -1, tree_current_access_path(t));
  1550. } else {
  1551. #ifdef HAVE_FSTATVFS
  1552. r = fstatvfs(tree_current_dir_fd(t), &svfs);
  1553. if (r == 0)
  1554. xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
  1555. #else
  1556. r = statvfs(".", &svfs);
  1557. if (r == 0)
  1558. xr = get_xfer_size(t, -1, ".");
  1559. #endif
  1560. }
  1561. if (r == -1 || xr == -1) {
  1562. t->current_filesystem->remote = -1;
  1563. archive_set_error(&a->archive, errno, "statvfs failed");
  1564. return (ARCHIVE_FAILED);
  1565. } else if (xr == 1) {
  1566. /* Usually come here unless NetBSD supports _PC_REC_XFER_ALIGN
  1567. * for pathconf() function. */
  1568. set_statvfs_transfer_size(t->current_filesystem, &svfs);
  1569. }
  1570. if (svfs.f_flag & ST_LOCAL)
  1571. t->current_filesystem->remote = 0;
  1572. else
  1573. t->current_filesystem->remote = 1;
  1574. #if defined(ST_NOATIME)
  1575. if (svfs.f_flag & ST_NOATIME)
  1576. t->current_filesystem->noatime = 1;
  1577. else
  1578. #endif
  1579. t->current_filesystem->noatime = 0;
  1580. /* Set maximum filename length. */
  1581. t->current_filesystem->name_max = svfs.f_namemax;
  1582. return (ARCHIVE_OK);
  1583. }
  1584. #elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_LINUX_MAGIC_H) &&\
  1585. defined(HAVE_STATFS) && defined(HAVE_FSTATFS)
  1586. /*
  1587. * Note: statfs is deprecated since LSB 3.2
  1588. */
  1589. #ifndef CIFS_SUPER_MAGIC
  1590. #define CIFS_SUPER_MAGIC 0xFF534D42
  1591. #endif
  1592. #ifndef DEVFS_SUPER_MAGIC
  1593. #define DEVFS_SUPER_MAGIC 0x1373
  1594. #endif
  1595. /*
  1596. * Gather current filesystem properties on Linux
  1597. */
  1598. static int
  1599. setup_current_filesystem(struct archive_read_disk *a)
  1600. {
  1601. struct tree *t = a->tree;
  1602. struct statfs sfs;
  1603. #if defined(HAVE_STATVFS)
  1604. struct statvfs svfs;
  1605. #endif
  1606. int r, vr = 0, xr = 0;
  1607. if (tree_current_is_symblic_link_target(t)) {
  1608. #if defined(HAVE_OPENAT)
  1609. /*
  1610. * Get file system statistics on any directory
  1611. * where current is.
  1612. */
  1613. int fd = openat(tree_current_dir_fd(t),
  1614. tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
  1615. __archive_ensure_cloexec_flag(fd);
  1616. if (fd < 0) {
  1617. archive_set_error(&a->archive, errno,
  1618. "openat failed");
  1619. return (ARCHIVE_FAILED);
  1620. }
  1621. #if defined(HAVE_FSTATVFS)
  1622. vr = fstatvfs(fd, &svfs);/* for f_flag, mount flags */
  1623. #endif
  1624. r = fstatfs(fd, &sfs);
  1625. if (r == 0)
  1626. xr = get_xfer_size(t, fd, NULL);
  1627. close(fd);
  1628. #else
  1629. if (tree_enter_working_dir(t) != 0) {
  1630. archive_set_error(&a->archive, errno, "fchdir failed");
  1631. return (ARCHIVE_FAILED);
  1632. }
  1633. #if defined(HAVE_STATVFS)
  1634. vr = statvfs(tree_current_access_path(t), &svfs);
  1635. #endif
  1636. r = statfs(tree_current_access_path(t), &sfs);
  1637. if (r == 0)
  1638. xr = get_xfer_size(t, -1, tree_current_access_path(t));
  1639. #endif
  1640. } else {
  1641. #ifdef HAVE_FSTATFS
  1642. #if defined(HAVE_FSTATVFS)
  1643. vr = fstatvfs(tree_current_dir_fd(t), &svfs);
  1644. #endif
  1645. r = fstatfs(tree_current_dir_fd(t), &sfs);
  1646. if (r == 0)
  1647. xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
  1648. #else
  1649. if (tree_enter_working_dir(t) != 0) {
  1650. archive_set_error(&a->archive, errno, "fchdir failed");
  1651. return (ARCHIVE_FAILED);
  1652. }
  1653. #if defined(HAVE_STATVFS)
  1654. vr = statvfs(".", &svfs);
  1655. #endif
  1656. r = statfs(".", &sfs);
  1657. if (r == 0)
  1658. xr = get_xfer_size(t, -1, ".");
  1659. #endif
  1660. }
  1661. if (r == -1 || xr == -1 || vr == -1) {
  1662. t->current_filesystem->synthetic = -1;
  1663. t->current_filesystem->remote = -1;
  1664. archive_set_error(&a->archive, errno, "statfs failed");
  1665. return (ARCHIVE_FAILED);
  1666. } else if (xr == 1) {
  1667. /* pathconf(_PC_REX_*) operations are not supported. */
  1668. #if defined(HAVE_STATVFS)
  1669. set_statvfs_transfer_size(t->current_filesystem, &svfs);
  1670. #else
  1671. set_statfs_transfer_size(t->current_filesystem, &sfs);
  1672. #endif
  1673. }
  1674. switch (sfs.f_type) {
  1675. case AFS_SUPER_MAGIC:
  1676. case CIFS_SUPER_MAGIC:
  1677. case CODA_SUPER_MAGIC:
  1678. case NCP_SUPER_MAGIC:/* NetWare */
  1679. case NFS_SUPER_MAGIC:
  1680. case SMB_SUPER_MAGIC:
  1681. t->current_filesystem->remote = 1;
  1682. t->current_filesystem->synthetic = 0;
  1683. break;
  1684. case DEVFS_SUPER_MAGIC:
  1685. case PROC_SUPER_MAGIC:
  1686. case USBDEVICE_SUPER_MAGIC:
  1687. t->current_filesystem->remote = 0;
  1688. t->current_filesystem->synthetic = 1;
  1689. break;
  1690. default:
  1691. t->current_filesystem->remote = 0;
  1692. t->current_filesystem->synthetic = 0;
  1693. break;
  1694. }
  1695. #if defined(ST_NOATIME)
  1696. #if defined(HAVE_STATVFS)
  1697. if (svfs.f_flag & ST_NOATIME)
  1698. #else
  1699. if (sfs.f_flags & ST_NOATIME)
  1700. #endif
  1701. t->current_filesystem->noatime = 1;
  1702. else
  1703. #endif
  1704. t->current_filesystem->noatime = 0;
  1705. #if defined(USE_READDIR_R)
  1706. /* Set maximum filename length. */
  1707. #if defined(HAVE_STATVFS)
  1708. t->current_filesystem->name_max = svfs.f_namemax;
  1709. #else
  1710. t->current_filesystem->name_max = sfs.f_namelen;
  1711. #endif
  1712. if (t->current_filesystem->name_max == 0) {
  1713. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1714. "Cannot determine name_max");
  1715. return (ARCHIVE_FAILED);
  1716. }
  1717. #endif
  1718. return (ARCHIVE_OK);
  1719. }
  1720. #elif defined(HAVE_SYS_STATVFS_H) &&\
  1721. (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS))
  1722. /*
  1723. * Gather current filesystem properties on other posix platform.
  1724. */
  1725. static int
  1726. setup_current_filesystem(struct archive_read_disk *a)
  1727. {
  1728. struct tree *t = a->tree;
  1729. struct statvfs svfs;
  1730. int r, xr = 0;
  1731. t->current_filesystem->synthetic = -1;/* Not supported */
  1732. t->current_filesystem->remote = -1;/* Not supported */
  1733. if (tree_current_is_symblic_link_target(t)) {
  1734. #if defined(HAVE_OPENAT)
  1735. /*
  1736. * Get file system statistics on any directory
  1737. * where current is.
  1738. */
  1739. int fd = openat(tree_current_dir_fd(t),
  1740. tree_current_access_path(t), O_RDONLY | O_CLOEXEC);
  1741. __archive_ensure_cloexec_flag(fd);
  1742. if (fd < 0) {
  1743. archive_set_error(&a->archive, errno,
  1744. "openat failed");
  1745. return (ARCHIVE_FAILED);
  1746. }
  1747. r = fstatvfs(fd, &svfs);
  1748. if (r == 0)
  1749. xr = get_xfer_size(t, fd, NULL);
  1750. close(fd);
  1751. #else
  1752. if (tree_enter_working_dir(t) != 0) {
  1753. archive_set_error(&a->archive, errno, "fchdir failed");
  1754. return (ARCHIVE_FAILED);
  1755. }
  1756. r = statvfs(tree_current_access_path(t), &svfs);
  1757. if (r == 0)
  1758. xr = get_xfer_size(t, -1, tree_current_access_path(t));
  1759. #endif
  1760. } else {
  1761. #ifdef HAVE_FSTATVFS
  1762. r = fstatvfs(tree_current_dir_fd(t), &svfs);
  1763. if (r == 0)
  1764. xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
  1765. #else
  1766. if (tree_enter_working_dir(t) != 0) {
  1767. archive_set_error(&a->archive, errno, "fchdir failed");
  1768. return (ARCHIVE_FAILED);
  1769. }
  1770. r = statvfs(".", &svfs);
  1771. if (r == 0)
  1772. xr = get_xfer_size(t, -1, ".");
  1773. #endif
  1774. }
  1775. if (r == -1 || xr == -1) {
  1776. t->current_filesystem->synthetic = -1;
  1777. t->current_filesystem->remote = -1;
  1778. archive_set_error(&a->archive, errno, "statvfs failed");
  1779. return (ARCHIVE_FAILED);
  1780. } else if (xr == 1) {
  1781. /* pathconf(_PC_REX_*) operations are not supported. */
  1782. set_statvfs_transfer_size(t->current_filesystem, &svfs);
  1783. }
  1784. #if defined(ST_NOATIME)
  1785. if (svfs.f_flag & ST_NOATIME)
  1786. t->current_filesystem->noatime = 1;
  1787. else
  1788. #endif
  1789. t->current_filesystem->noatime = 0;
  1790. #if defined(USE_READDIR_R)
  1791. /* Set maximum filename length. */
  1792. t->current_filesystem->name_max = svfs.f_namemax;
  1793. if (t->current_filesystem->name_max == 0) {
  1794. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1795. "Cannot determine name_max");
  1796. return (ARCHIVE_FAILED);
  1797. }
  1798. #endif
  1799. return (ARCHIVE_OK);
  1800. }
  1801. #else
  1802. /*
  1803. * Generic: Gather current filesystem properties.
  1804. * TODO: Is this generic function really needed?
  1805. */
  1806. static int
  1807. setup_current_filesystem(struct archive_read_disk *a)
  1808. {
  1809. struct tree *t = a->tree;
  1810. #if defined(_PC_NAME_MAX) && defined(USE_READDIR_R)
  1811. long nm;
  1812. #endif
  1813. t->current_filesystem->synthetic = -1;/* Not supported */
  1814. t->current_filesystem->remote = -1;/* Not supported */
  1815. t->current_filesystem->noatime = 0;
  1816. (void)get_xfer_size(t, -1, ".");/* Dummy call to avoid build error. */
  1817. t->current_filesystem->xfer_align = -1;/* Unknown */
  1818. t->current_filesystem->max_xfer_size = -1;
  1819. t->current_filesystem->min_xfer_size = -1;
  1820. t->current_filesystem->incr_xfer_size = -1;
  1821. #if defined(USE_READDIR_R)
  1822. /* Set maximum filename length. */
  1823. # if defined(_PC_NAME_MAX)
  1824. if (tree_current_is_symblic_link_target(t)) {
  1825. if (tree_enter_working_dir(t) != 0) {
  1826. archive_set_error(&a->archive, errno, "fchdir failed");
  1827. return (ARCHIVE_FAILED);
  1828. }
  1829. nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
  1830. } else
  1831. nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
  1832. if (nm == -1)
  1833. # endif /* _PC_NAME_MAX */
  1834. /*
  1835. * Some systems (HP-UX or others?) incorrectly defined
  1836. * NAME_MAX macro to be a smaller value.
  1837. */
  1838. # if defined(NAME_MAX) && NAME_MAX >= 255
  1839. t->current_filesystem->name_max = NAME_MAX;
  1840. # else
  1841. /* No way to get a trusted value of maximum filename
  1842. * length. */
  1843. t->current_filesystem->name_max = PATH_MAX;
  1844. # endif /* NAME_MAX */
  1845. # if defined(_PC_NAME_MAX)
  1846. else
  1847. t->current_filesystem->name_max = nm;
  1848. # endif /* _PC_NAME_MAX */
  1849. if (t->current_filesystem->name_max == 0) {
  1850. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1851. "Cannot determine name_max");
  1852. return (ARCHIVE_FAILED);
  1853. }
  1854. #endif /* USE_READDIR_R */
  1855. return (ARCHIVE_OK);
  1856. }
  1857. #endif
  1858. static int
  1859. close_and_restore_time(int fd, struct tree *t, struct restore_time *rt)
  1860. {
  1861. #ifndef HAVE_UTIMES
  1862. (void)t; /* UNUSED */
  1863. (void)rt; /* UNUSED */
  1864. return (close(fd));
  1865. #else
  1866. #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
  1867. struct timespec timespecs[2];
  1868. #endif
  1869. struct timeval times[2];
  1870. if ((t->flags & needsRestoreTimes) == 0 || rt->noatime) {
  1871. if (fd >= 0)
  1872. return (close(fd));
  1873. else
  1874. return (0);
  1875. }
  1876. #if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
  1877. timespecs[1].tv_sec = rt->mtime;
  1878. timespecs[1].tv_nsec = rt->mtime_nsec;
  1879. timespecs[0].tv_sec = rt->atime;
  1880. timespecs[0].tv_nsec = rt->atime_nsec;
  1881. /* futimens() is defined in POSIX.1-2008. */
  1882. if (futimens(fd, timespecs) == 0)
  1883. return (close(fd));
  1884. #endif
  1885. times[1].tv_sec = rt->mtime;
  1886. times[1].tv_usec = rt->mtime_nsec / 1000;
  1887. times[0].tv_sec = rt->atime;
  1888. times[0].tv_usec = rt->atime_nsec / 1000;
  1889. #if !defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
  1890. if (futimes(fd, times) == 0)
  1891. return (close(fd));
  1892. #endif
  1893. close(fd);
  1894. #if defined(HAVE_FUTIMESAT)
  1895. if (futimesat(tree_current_dir_fd(t), rt->name, times) == 0)
  1896. return (0);
  1897. #endif
  1898. #ifdef HAVE_LUTIMES
  1899. if (lutimes(rt->name, times) != 0)
  1900. #else
  1901. if (AE_IFLNK != rt->filetype && utimes(rt->name, times) != 0)
  1902. #endif
  1903. return (-1);
  1904. #endif
  1905. return (0);
  1906. }
  1907. static int
  1908. open_on_current_dir(struct tree *t, const char *path, int flags)
  1909. {
  1910. #ifdef HAVE_OPENAT
  1911. return (openat(tree_current_dir_fd(t), path, flags));
  1912. #else
  1913. if (tree_enter_working_dir(t) != 0)
  1914. return (-1);
  1915. return (open(path, flags));
  1916. #endif
  1917. }
  1918. static int
  1919. tree_dup(int fd)
  1920. {
  1921. int new_fd;
  1922. #ifdef F_DUPFD_CLOEXEC
  1923. static volatile int can_dupfd_cloexec = 1;
  1924. if (can_dupfd_cloexec) {
  1925. new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
  1926. if (new_fd != -1)
  1927. return (new_fd);
  1928. /* Linux 2.6.18 - 2.6.23 declare F_DUPFD_CLOEXEC,
  1929. * but it cannot be used. So we have to try dup(). */
  1930. /* We won't try F_DUPFD_CLOEXEC. */
  1931. can_dupfd_cloexec = 0;
  1932. }
  1933. #endif /* F_DUPFD_CLOEXEC */
  1934. new_fd = dup(fd);
  1935. __archive_ensure_cloexec_flag(new_fd);
  1936. return (new_fd);
  1937. }
  1938. /*
  1939. * Add a directory path to the current stack.
  1940. */
  1941. static void
  1942. tree_push(struct tree *t, const char *path, int filesystem_id,
  1943. int64_t dev, int64_t ino, struct restore_time *rt)
  1944. {
  1945. struct tree_entry *te;
  1946. te = calloc(1, sizeof(*te));
  1947. if (te == NULL)
  1948. __archive_errx(1, "Out of memory");
  1949. te->next = t->stack;
  1950. te->parent = t->current;
  1951. if (te->parent)
  1952. te->depth = te->parent->depth + 1;
  1953. t->stack = te;
  1954. archive_string_init(&te->name);
  1955. te->symlink_parent_fd = -1;
  1956. archive_strcpy(&te->name, path);
  1957. te->flags = needsDescent | needsOpen | needsAscent;
  1958. te->filesystem_id = filesystem_id;
  1959. te->dev = dev;
  1960. te->ino = ino;
  1961. te->dirname_length = t->dirname_length;
  1962. te->restore_time.name = te->name.s;
  1963. if (rt != NULL) {
  1964. te->restore_time.mtime = rt->mtime;
  1965. te->restore_time.mtime_nsec = rt->mtime_nsec;
  1966. te->restore_time.atime = rt->atime;
  1967. te->restore_time.atime_nsec = rt->atime_nsec;
  1968. te->restore_time.filetype = rt->filetype;
  1969. te->restore_time.noatime = rt->noatime;
  1970. }
  1971. }
  1972. /*
  1973. * Append a name to the current dir path.
  1974. */
  1975. static void
  1976. tree_append(struct tree *t, const char *name, size_t name_length)
  1977. {
  1978. size_t size_needed;
  1979. t->path.s[t->dirname_length] = '\0';
  1980. t->path.length = t->dirname_length;
  1981. /* Strip trailing '/' from name, unless entire name is "/". */
  1982. while (name_length > 1 && name[name_length - 1] == '/')
  1983. name_length--;
  1984. /* Resize pathname buffer as needed. */
  1985. size_needed = name_length + t->dirname_length + 2;
  1986. archive_string_ensure(&t->path, size_needed);
  1987. /* Add a separating '/' if it's needed. */
  1988. if (t->dirname_length > 0 && t->path.s[archive_strlen(&t->path)-1] != '/')
  1989. archive_strappend_char(&t->path, '/');
  1990. t->basename = t->path.s + archive_strlen(&t->path);
  1991. archive_strncat(&t->path, name, name_length);
  1992. t->restore_time.name = t->basename;
  1993. }
  1994. /*
  1995. * Open a directory tree for traversal.
  1996. */
  1997. static struct tree *
  1998. tree_open(const char *path, int symlink_mode, int restore_time)
  1999. {
  2000. struct tree *t;
  2001. if ((t = calloc(1, sizeof(*t))) == NULL)
  2002. return (NULL);
  2003. archive_string_init(&t->path);
  2004. archive_string_ensure(&t->path, 31);
  2005. t->initial_symlink_mode = symlink_mode;
  2006. return (tree_reopen(t, path, restore_time));
  2007. }
  2008. static struct tree *
  2009. tree_reopen(struct tree *t, const char *path, int restore_time)
  2010. {
  2011. #if defined(O_PATH)
  2012. /* Linux */
  2013. const int o_flag = O_PATH;
  2014. #elif defined(O_SEARCH)
  2015. /* SunOS */
  2016. const int o_flag = O_SEARCH;
  2017. #elif defined(__FreeBSD__) && defined(O_EXEC)
  2018. /* FreeBSD */
  2019. const int o_flag = O_EXEC;
  2020. #endif
  2021. t->flags = (restore_time != 0)?needsRestoreTimes:0;
  2022. t->flags |= onInitialDir;
  2023. t->visit_type = 0;
  2024. t->tree_errno = 0;
  2025. t->dirname_length = 0;
  2026. t->depth = 0;
  2027. t->descend = 0;
  2028. t->current = NULL;
  2029. t->d = INVALID_DIR_HANDLE;
  2030. t->symlink_mode = t->initial_symlink_mode;
  2031. archive_string_empty(&t->path);
  2032. t->entry_fd = -1;
  2033. t->entry_eof = 0;
  2034. t->entry_remaining_bytes = 0;
  2035. t->initial_filesystem_id = -1;
  2036. /* First item is set up a lot like a symlink traversal. */
  2037. tree_push(t, path, 0, 0, 0, NULL);
  2038. t->stack->flags = needsFirstVisit;
  2039. t->maxOpenCount = t->openCount = 1;
  2040. t->initial_dir_fd = open(".", O_RDONLY | O_CLOEXEC);
  2041. #if defined(O_PATH) || defined(O_SEARCH) || \
  2042. (defined(__FreeBSD__) && defined(O_EXEC))
  2043. /*
  2044. * Most likely reason to fail opening "." is that it's not readable,
  2045. * so try again for execute. The consequences of not opening this are
  2046. * unhelpful and unnecessary errors later.
  2047. */
  2048. if (t->initial_dir_fd < 0)
  2049. t->initial_dir_fd = open(".", o_flag | O_CLOEXEC);
  2050. #endif
  2051. __archive_ensure_cloexec_flag(t->initial_dir_fd);
  2052. t->working_dir_fd = tree_dup(t->initial_dir_fd);
  2053. return (t);
  2054. }
  2055. static int
  2056. tree_descent(struct tree *t)
  2057. {
  2058. int flag, new_fd, r = 0;
  2059. t->dirname_length = archive_strlen(&t->path);
  2060. flag = O_RDONLY | O_CLOEXEC;
  2061. #if defined(O_DIRECTORY)
  2062. flag |= O_DIRECTORY;
  2063. #endif
  2064. new_fd = open_on_current_dir(t, t->stack->name.s, flag);
  2065. __archive_ensure_cloexec_flag(new_fd);
  2066. if (new_fd < 0) {
  2067. t->tree_errno = errno;
  2068. r = TREE_ERROR_DIR;
  2069. } else {
  2070. t->depth++;
  2071. /* If it is a link, set up fd for the ascent. */
  2072. if (t->stack->flags & isDirLink) {
  2073. t->stack->symlink_parent_fd = t->working_dir_fd;
  2074. t->openCount++;
  2075. if (t->openCount > t->maxOpenCount)
  2076. t->maxOpenCount = t->openCount;
  2077. } else
  2078. close(t->working_dir_fd);
  2079. /* Renew the current working directory. */
  2080. t->working_dir_fd = new_fd;
  2081. t->flags &= ~onWorkingDir;
  2082. }
  2083. return (r);
  2084. }
  2085. /*
  2086. * We've finished a directory; ascend back to the parent.
  2087. */
  2088. static int
  2089. tree_ascend(struct tree *t)
  2090. {
  2091. struct tree_entry *te;
  2092. int new_fd, r = 0, prev_dir_fd;
  2093. te = t->stack;
  2094. prev_dir_fd = t->working_dir_fd;
  2095. if (te->flags & isDirLink)
  2096. new_fd = te->symlink_parent_fd;
  2097. else {
  2098. new_fd = open_on_current_dir(t, "..", O_RDONLY | O_CLOEXEC);
  2099. __archive_ensure_cloexec_flag(new_fd);
  2100. }
  2101. if (new_fd < 0) {
  2102. t->tree_errno = errno;
  2103. r = TREE_ERROR_FATAL;
  2104. } else {
  2105. /* Renew the current working directory. */
  2106. t->working_dir_fd = new_fd;
  2107. t->flags &= ~onWorkingDir;
  2108. /* Current directory has been changed, we should
  2109. * close an fd of previous working directory. */
  2110. close_and_restore_time(prev_dir_fd, t, &te->restore_time);
  2111. if (te->flags & isDirLink) {
  2112. t->openCount--;
  2113. te->symlink_parent_fd = -1;
  2114. }
  2115. t->depth--;
  2116. }
  2117. return (r);
  2118. }
  2119. /*
  2120. * Return to the initial directory where tree_open() was performed.
  2121. */
  2122. static int
  2123. tree_enter_initial_dir(struct tree *t)
  2124. {
  2125. int r = 0;
  2126. if ((t->flags & onInitialDir) == 0) {
  2127. r = fchdir(t->initial_dir_fd);
  2128. if (r == 0) {
  2129. t->flags &= ~onWorkingDir;
  2130. t->flags |= onInitialDir;
  2131. }
  2132. }
  2133. return (r);
  2134. }
  2135. /*
  2136. * Restore working directory of directory traversals.
  2137. */
  2138. static int
  2139. tree_enter_working_dir(struct tree *t)
  2140. {
  2141. int r = 0;
  2142. /*
  2143. * Change the current directory if really needed.
  2144. * Sometimes this is unneeded when we did not do
  2145. * descent.
  2146. */
  2147. if (t->depth > 0 && (t->flags & onWorkingDir) == 0) {
  2148. r = fchdir(t->working_dir_fd);
  2149. if (r == 0) {
  2150. t->flags &= ~onInitialDir;
  2151. t->flags |= onWorkingDir;
  2152. }
  2153. }
  2154. return (r);
  2155. }
  2156. static int
  2157. tree_current_dir_fd(struct tree *t)
  2158. {
  2159. return (t->working_dir_fd);
  2160. }
  2161. /*
  2162. * Pop the working stack.
  2163. */
  2164. static void
  2165. tree_pop(struct tree *t)
  2166. {
  2167. struct tree_entry *te;
  2168. t->path.s[t->dirname_length] = '\0';
  2169. t->path.length = t->dirname_length;
  2170. if (t->stack == t->current && t->current != NULL)
  2171. t->current = t->current->parent;
  2172. te = t->stack;
  2173. #ifdef __clang_analyzer__
  2174. assert(te);
  2175. #endif
  2176. t->stack = te->next;
  2177. t->dirname_length = te->dirname_length;
  2178. t->basename = t->path.s + t->dirname_length;
  2179. while (t->basename[0] == '/')
  2180. t->basename++;
  2181. archive_string_free(&te->name);
  2182. free(te);
  2183. }
  2184. /*
  2185. * Get the next item in the tree traversal.
  2186. */
  2187. static int
  2188. tree_next(struct tree *t)
  2189. {
  2190. int r;
  2191. while (t->stack != NULL) {
  2192. /* If there's an open dir, get the next entry from there. */
  2193. if (t->d != INVALID_DIR_HANDLE) {
  2194. r = tree_dir_next_posix(t);
  2195. if (r == 0)
  2196. continue;
  2197. return (r);
  2198. }
  2199. if (t->stack->flags & needsFirstVisit) {
  2200. /* Top stack item needs a regular visit. */
  2201. t->current = t->stack;
  2202. tree_append(t, t->stack->name.s,
  2203. archive_strlen(&(t->stack->name)));
  2204. /* t->dirname_length = t->path_length; */
  2205. /* tree_pop(t); */
  2206. t->stack->flags &= ~needsFirstVisit;
  2207. return (t->visit_type = TREE_REGULAR);
  2208. } else if (t->stack->flags & needsDescent) {
  2209. /* Top stack item is dir to descend into. */
  2210. t->current = t->stack;
  2211. tree_append(t, t->stack->name.s,
  2212. archive_strlen(&(t->stack->name)));
  2213. t->stack->flags &= ~needsDescent;
  2214. r = tree_descent(t);
  2215. if (r != 0) {
  2216. tree_pop(t);
  2217. t->visit_type = r;
  2218. } else
  2219. t->visit_type = TREE_POSTDESCENT;
  2220. return (t->visit_type);
  2221. } else if (t->stack->flags & needsOpen) {
  2222. t->stack->flags &= ~needsOpen;
  2223. r = tree_dir_next_posix(t);
  2224. if (r == 0)
  2225. continue;
  2226. return (r);
  2227. } else if (t->stack->flags & needsAscent) {
  2228. /* Top stack item is dir and we're done with it. */
  2229. r = tree_ascend(t);
  2230. tree_pop(t);
  2231. t->visit_type = r != 0 ? r : TREE_POSTASCENT;
  2232. return (t->visit_type);
  2233. } else {
  2234. /* Top item on stack is dead. */
  2235. tree_pop(t);
  2236. t->flags &= ~hasLstat;
  2237. t->flags &= ~hasStat;
  2238. }
  2239. }
  2240. return (t->visit_type = 0);
  2241. }
  2242. static int
  2243. tree_dir_next_posix(struct tree *t)
  2244. {
  2245. int r;
  2246. const char *name;
  2247. size_t namelen;
  2248. if (t->d == NULL) {
  2249. #if defined(USE_READDIR_R)
  2250. size_t dirent_size;
  2251. #endif
  2252. #if defined(HAVE_FDOPENDIR)
  2253. t->d = fdopendir(tree_dup(t->working_dir_fd));
  2254. #else /* HAVE_FDOPENDIR */
  2255. if (tree_enter_working_dir(t) == 0) {
  2256. t->d = opendir(".");
  2257. #ifdef HAVE_DIRFD
  2258. __archive_ensure_cloexec_flag(dirfd(t->d));
  2259. #endif
  2260. }
  2261. #endif /* HAVE_FDOPENDIR */
  2262. if (t->d == NULL) {
  2263. r = tree_ascend(t); /* Undo "chdir" */
  2264. tree_pop(t);
  2265. t->tree_errno = errno;
  2266. t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
  2267. return (t->visit_type);
  2268. }
  2269. #if defined(USE_READDIR_R)
  2270. dirent_size = offsetof(struct dirent, d_name) +
  2271. t->filesystem_table[t->current->filesystem_id].name_max + 1;
  2272. if (t->dirent == NULL || t->dirent_allocated < dirent_size) {
  2273. free(t->dirent);
  2274. t->dirent = malloc(dirent_size);
  2275. if (t->dirent == NULL) {
  2276. closedir(t->d);
  2277. t->d = INVALID_DIR_HANDLE;
  2278. (void)tree_ascend(t);
  2279. tree_pop(t);
  2280. t->tree_errno = ENOMEM;
  2281. t->visit_type = TREE_ERROR_DIR;
  2282. return (t->visit_type);
  2283. }
  2284. t->dirent_allocated = dirent_size;
  2285. }
  2286. #endif /* USE_READDIR_R */
  2287. }
  2288. for (;;) {
  2289. errno = 0;
  2290. #if defined(USE_READDIR_R)
  2291. r = readdir_r(t->d, t->dirent, &t->de);
  2292. #ifdef _AIX
  2293. /* Note: According to the man page, return value 9 indicates
  2294. * that the readdir_r was not successful and the error code
  2295. * is set to the global errno variable. And then if the end
  2296. * of directory entries was reached, the return value is 9
  2297. * and the third parameter is set to NULL and errno is
  2298. * unchanged. */
  2299. if (r == 9)
  2300. r = errno;
  2301. #endif /* _AIX */
  2302. if (r != 0 || t->de == NULL) {
  2303. #else
  2304. t->de = readdir(t->d);
  2305. if (t->de == NULL) {
  2306. r = errno;
  2307. #endif
  2308. closedir(t->d);
  2309. t->d = INVALID_DIR_HANDLE;
  2310. if (r != 0) {
  2311. t->tree_errno = r;
  2312. t->visit_type = TREE_ERROR_DIR;
  2313. return (t->visit_type);
  2314. } else
  2315. return (0);
  2316. }
  2317. name = t->de->d_name;
  2318. namelen = D_NAMELEN(t->de);
  2319. t->flags &= ~hasLstat;
  2320. t->flags &= ~hasStat;
  2321. if (name[0] == '.' && name[1] == '\0')
  2322. continue;
  2323. if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
  2324. continue;
  2325. tree_append(t, name, namelen);
  2326. return (t->visit_type = TREE_REGULAR);
  2327. }
  2328. }
  2329. /*
  2330. * Get the stat() data for the entry just returned from tree_next().
  2331. */
  2332. static const struct stat *
  2333. tree_current_stat(struct tree *t)
  2334. {
  2335. if (!(t->flags & hasStat)) {
  2336. #ifdef HAVE_FSTATAT
  2337. if (fstatat(tree_current_dir_fd(t),
  2338. tree_current_access_path(t), &t->st, 0) != 0)
  2339. #else
  2340. if (tree_enter_working_dir(t) != 0)
  2341. return NULL;
  2342. if (la_stat(tree_current_access_path(t), &t->st) != 0)
  2343. #endif
  2344. return NULL;
  2345. t->flags |= hasStat;
  2346. }
  2347. return (&t->st);
  2348. }
  2349. /*
  2350. * Get the lstat() data for the entry just returned from tree_next().
  2351. */
  2352. static const struct stat *
  2353. tree_current_lstat(struct tree *t)
  2354. {
  2355. if (!(t->flags & hasLstat)) {
  2356. #ifdef HAVE_FSTATAT
  2357. if (fstatat(tree_current_dir_fd(t),
  2358. tree_current_access_path(t), &t->lst,
  2359. AT_SYMLINK_NOFOLLOW) != 0)
  2360. #else
  2361. if (tree_enter_working_dir(t) != 0)
  2362. return NULL;
  2363. #ifdef HAVE_LSTAT
  2364. if (lstat(tree_current_access_path(t), &t->lst) != 0)
  2365. #else
  2366. if (la_stat(tree_current_access_path(t), &t->lst) != 0)
  2367. #endif
  2368. #endif
  2369. return NULL;
  2370. t->flags |= hasLstat;
  2371. }
  2372. return (&t->lst);
  2373. }
  2374. /*
  2375. * Test whether current entry is a dir or link to a dir.
  2376. */
  2377. static int
  2378. tree_current_is_dir(struct tree *t)
  2379. {
  2380. const struct stat *st;
  2381. /*
  2382. * If we already have lstat() info, then try some
  2383. * cheap tests to determine if this is a dir.
  2384. */
  2385. if (t->flags & hasLstat) {
  2386. /* If lstat() says it's a dir, it must be a dir. */
  2387. st = tree_current_lstat(t);
  2388. if (st == NULL)
  2389. return 0;
  2390. if (S_ISDIR(st->st_mode))
  2391. return 1;
  2392. /* Not a dir; might be a link to a dir. */
  2393. /* If it's not a link, then it's not a link to a dir. */
  2394. if (!S_ISLNK(st->st_mode))
  2395. return 0;
  2396. /*
  2397. * It's a link, but we don't know what it's a link to,
  2398. * so we'll have to use stat().
  2399. */
  2400. }
  2401. st = tree_current_stat(t);
  2402. /* If we can't stat it, it's not a dir. */
  2403. if (st == NULL)
  2404. return 0;
  2405. /* Use the definitive test. Hopefully this is cached. */
  2406. return (S_ISDIR(st->st_mode));
  2407. }
  2408. /*
  2409. * Test whether current entry is a physical directory. Usually, we
  2410. * already have at least one of stat() or lstat() in memory, so we
  2411. * use tricks to try to avoid an extra trip to the disk.
  2412. */
  2413. static int
  2414. tree_current_is_physical_dir(struct tree *t)
  2415. {
  2416. const struct stat *st;
  2417. /*
  2418. * If stat() says it isn't a dir, then it's not a dir.
  2419. * If stat() data is cached, this check is free, so do it first.
  2420. */
  2421. if (t->flags & hasStat) {
  2422. st = tree_current_stat(t);
  2423. if (st == NULL)
  2424. return (0);
  2425. if (!S_ISDIR(st->st_mode))
  2426. return (0);
  2427. }
  2428. /*
  2429. * Either stat() said it was a dir (in which case, we have
  2430. * to determine whether it's really a link to a dir) or
  2431. * stat() info wasn't available. So we use lstat(), which
  2432. * hopefully is already cached.
  2433. */
  2434. st = tree_current_lstat(t);
  2435. /* If we can't stat it, it's not a dir. */
  2436. if (st == NULL)
  2437. return 0;
  2438. /* Use the definitive test. Hopefully this is cached. */
  2439. return (S_ISDIR(st->st_mode));
  2440. }
  2441. /*
  2442. * Test whether the same file has been in the tree as its parent.
  2443. */
  2444. static int
  2445. tree_target_is_same_as_parent(struct tree *t, const struct stat *st)
  2446. {
  2447. struct tree_entry *te;
  2448. for (te = t->current->parent; te != NULL; te = te->parent) {
  2449. if (te->dev == (int64_t)st->st_dev &&
  2450. te->ino == (int64_t)st->st_ino)
  2451. return (1);
  2452. }
  2453. return (0);
  2454. }
  2455. /*
  2456. * Test whether the current file is symbolic link target and
  2457. * on the other filesystem.
  2458. */
  2459. static int
  2460. tree_current_is_symblic_link_target(struct tree *t)
  2461. {
  2462. static const struct stat *lst, *st;
  2463. lst = tree_current_lstat(t);
  2464. st = tree_current_stat(t);
  2465. return (st != NULL && lst != NULL &&
  2466. (int64_t)st->st_dev == t->current_filesystem->dev &&
  2467. st->st_dev != lst->st_dev);
  2468. }
  2469. /*
  2470. * Return the access path for the entry just returned from tree_next().
  2471. */
  2472. static const char *
  2473. tree_current_access_path(struct tree *t)
  2474. {
  2475. return (t->basename);
  2476. }
  2477. /*
  2478. * Return the full path for the entry just returned from tree_next().
  2479. */
  2480. static const char *
  2481. tree_current_path(struct tree *t)
  2482. {
  2483. return (t->path.s);
  2484. }
  2485. /*
  2486. * Terminate the traversal.
  2487. */
  2488. static void
  2489. tree_close(struct tree *t)
  2490. {
  2491. if (t == NULL)
  2492. return;
  2493. if (t->entry_fd >= 0) {
  2494. close_and_restore_time(t->entry_fd, t, &t->restore_time);
  2495. t->entry_fd = -1;
  2496. }
  2497. /* Close the handle of readdir(). */
  2498. if (t->d != INVALID_DIR_HANDLE) {
  2499. closedir(t->d);
  2500. t->d = INVALID_DIR_HANDLE;
  2501. }
  2502. /* Release anything remaining in the stack. */
  2503. while (t->stack != NULL) {
  2504. if (t->stack->flags & isDirLink)
  2505. close(t->stack->symlink_parent_fd);
  2506. tree_pop(t);
  2507. }
  2508. if (t->working_dir_fd >= 0) {
  2509. close(t->working_dir_fd);
  2510. t->working_dir_fd = -1;
  2511. }
  2512. if (t->initial_dir_fd >= 0) {
  2513. close(t->initial_dir_fd);
  2514. t->initial_dir_fd = -1;
  2515. }
  2516. }
  2517. /*
  2518. * Release any resources.
  2519. */
  2520. static void
  2521. tree_free(struct tree *t)
  2522. {
  2523. int i;
  2524. if (t == NULL)
  2525. return;
  2526. archive_string_free(&t->path);
  2527. #if defined(USE_READDIR_R)
  2528. free(t->dirent);
  2529. #endif
  2530. free(t->sparse_list);
  2531. for (i = 0; i < t->max_filesystem_id; i++)
  2532. free(t->filesystem_table[i].allocation_ptr);
  2533. free(t->filesystem_table);
  2534. free(t);
  2535. }
  2536. #endif