1
0

archive_write_disk_windows.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502
  1. /*-
  2. * Copyright (c) 2003-2010 Tim Kientzle
  3. * Copyright (c) 2011-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. #include "archive_platform.h"
  28. __FBSDID("$FreeBSD$");
  29. #if defined(_WIN32) && !defined(__CYGWIN__)
  30. #ifdef HAVE_SYS_TYPES_H
  31. #include <sys/types.h>
  32. #endif
  33. #ifdef HAVE_SYS_UTIME_H
  34. #include <sys/utime.h>
  35. #endif
  36. #ifdef HAVE_ERRNO_H
  37. #include <errno.h>
  38. #endif
  39. #ifdef HAVE_FCNTL_H
  40. #include <fcntl.h>
  41. #endif
  42. #ifdef HAVE_LIMITS_H
  43. #include <limits.h>
  44. #endif
  45. #ifdef HAVE_STDLIB_H
  46. #include <stdlib.h>
  47. #endif
  48. #include <winioctl.h>
  49. /* TODO: Support Mac OS 'quarantine' feature. This is really just a
  50. * standard tag to mark files that have been downloaded as "tainted".
  51. * On Mac OS, we should mark the extracted files as tainted if the
  52. * archive being read was tainted. Windows has a similar feature; we
  53. * should investigate ways to support this generically. */
  54. #include "archive.h"
  55. #include "archive_acl_private.h"
  56. #include "archive_string.h"
  57. #include "archive_entry.h"
  58. #include "archive_private.h"
  59. #ifndef O_BINARY
  60. #define O_BINARY 0
  61. #endif
  62. #ifndef IO_REPARSE_TAG_SYMLINK
  63. /* Old SDKs do not provide IO_REPARSE_TAG_SYMLINK */
  64. #define IO_REPARSE_TAG_SYMLINK 0xA000000CL
  65. #endif
  66. static BOOL SetFilePointerEx_perso(HANDLE hFile,
  67. LARGE_INTEGER liDistanceToMove,
  68. PLARGE_INTEGER lpNewFilePointer,
  69. DWORD dwMoveMethod)
  70. {
  71. LARGE_INTEGER li;
  72. li.QuadPart = liDistanceToMove.QuadPart;
  73. li.LowPart = SetFilePointer(
  74. hFile, li.LowPart, &li.HighPart, dwMoveMethod);
  75. if(lpNewFilePointer) {
  76. lpNewFilePointer->QuadPart = li.QuadPart;
  77. }
  78. return li.LowPart != (DWORD)-1 || GetLastError() == NO_ERROR;
  79. }
  80. struct fixup_entry {
  81. struct fixup_entry *next;
  82. struct archive_acl acl;
  83. mode_t mode;
  84. int64_t atime;
  85. int64_t birthtime;
  86. int64_t mtime;
  87. int64_t ctime;
  88. unsigned long atime_nanos;
  89. unsigned long birthtime_nanos;
  90. unsigned long mtime_nanos;
  91. unsigned long ctime_nanos;
  92. unsigned long fflags_set;
  93. int fixup; /* bitmask of what needs fixing */
  94. wchar_t *name;
  95. };
  96. /*
  97. * We use a bitmask to track which operations remain to be done for
  98. * this file. In particular, this helps us avoid unnecessary
  99. * operations when it's possible to take care of one step as a
  100. * side-effect of another. For example, mkdir() can specify the mode
  101. * for the newly-created object but symlink() cannot. This means we
  102. * can skip chmod() if mkdir() succeeded, but we must explicitly
  103. * chmod() if we're trying to create a directory that already exists
  104. * (mkdir() failed) or if we're restoring a symlink. Similarly, we
  105. * need to verify UID/GID before trying to restore SUID/SGID bits;
  106. * that verification can occur explicitly through a stat() call or
  107. * implicitly because of a successful chown() call.
  108. */
  109. #define TODO_MODE_FORCE 0x40000000
  110. #define TODO_MODE_BASE 0x20000000
  111. #define TODO_SUID 0x10000000
  112. #define TODO_SUID_CHECK 0x08000000
  113. #define TODO_SGID 0x04000000
  114. #define TODO_SGID_CHECK 0x02000000
  115. #define TODO_MODE (TODO_MODE_BASE|TODO_SUID|TODO_SGID)
  116. #define TODO_TIMES ARCHIVE_EXTRACT_TIME
  117. #define TODO_OWNER ARCHIVE_EXTRACT_OWNER
  118. #define TODO_FFLAGS ARCHIVE_EXTRACT_FFLAGS
  119. #define TODO_ACLS ARCHIVE_EXTRACT_ACL
  120. #define TODO_XATTR ARCHIVE_EXTRACT_XATTR
  121. #define TODO_MAC_METADATA ARCHIVE_EXTRACT_MAC_METADATA
  122. struct archive_write_disk {
  123. struct archive archive;
  124. mode_t user_umask;
  125. struct fixup_entry *fixup_list;
  126. struct fixup_entry *current_fixup;
  127. int64_t user_uid;
  128. int skip_file_set;
  129. int64_t skip_file_dev;
  130. int64_t skip_file_ino;
  131. time_t start_time;
  132. int64_t (*lookup_gid)(void *private, const char *gname, int64_t gid);
  133. void (*cleanup_gid)(void *private);
  134. void *lookup_gid_data;
  135. int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid);
  136. void (*cleanup_uid)(void *private);
  137. void *lookup_uid_data;
  138. /*
  139. * Full path of last file to satisfy symlink checks.
  140. */
  141. struct archive_wstring path_safe;
  142. /*
  143. * Cached stat data from disk for the current entry.
  144. * If this is valid, pst points to st. Otherwise,
  145. * pst is null.
  146. */
  147. BY_HANDLE_FILE_INFORMATION st;
  148. BY_HANDLE_FILE_INFORMATION *pst;
  149. /* Information about the object being restored right now. */
  150. struct archive_entry *entry; /* Entry being extracted. */
  151. wchar_t *name; /* Name of entry, possibly edited. */
  152. struct archive_wstring _name_data; /* backing store for 'name' */
  153. /* Tasks remaining for this object. */
  154. int todo;
  155. /* Tasks deferred until end-of-archive. */
  156. int deferred;
  157. /* Options requested by the client. */
  158. int flags;
  159. /* Handle for the file we're restoring. */
  160. HANDLE fh;
  161. /* Current offset for writing data to the file. */
  162. int64_t offset;
  163. /* Last offset actually written to disk. */
  164. int64_t fd_offset;
  165. /* Total bytes actually written to files. */
  166. int64_t total_bytes_written;
  167. /* Maximum size of file, -1 if unknown. */
  168. int64_t filesize;
  169. /* Dir we were in before this restore; only for deep paths. */
  170. int restore_pwd;
  171. /* Mode we should use for this entry; affected by _PERM and umask. */
  172. mode_t mode;
  173. /* UID/GID to use in restoring this entry. */
  174. int64_t uid;
  175. int64_t gid;
  176. };
  177. /*
  178. * Default mode for dirs created automatically (will be modified by umask).
  179. * Note that POSIX specifies 0777 for implicity-created dirs, "modified
  180. * by the process' file creation mask."
  181. */
  182. #define DEFAULT_DIR_MODE 0777
  183. /*
  184. * Dir modes are restored in two steps: During the extraction, the permissions
  185. * in the archive are modified to match the following limits. During
  186. * the post-extract fixup pass, the permissions from the archive are
  187. * applied.
  188. */
  189. #define MINIMUM_DIR_MODE 0700
  190. #define MAXIMUM_DIR_MODE 0775
  191. static int check_symlinks(struct archive_write_disk *);
  192. static int create_filesystem_object(struct archive_write_disk *);
  193. static struct fixup_entry *current_fixup(struct archive_write_disk *,
  194. const wchar_t *pathname);
  195. static int cleanup_pathname(struct archive_write_disk *);
  196. static int create_dir(struct archive_write_disk *, wchar_t *);
  197. static int create_parent_dir(struct archive_write_disk *, wchar_t *);
  198. static int la_chmod(const wchar_t *, mode_t);
  199. static int older(BY_HANDLE_FILE_INFORMATION *, struct archive_entry *);
  200. static int permissive_name_w(struct archive_write_disk *);
  201. static int restore_entry(struct archive_write_disk *);
  202. static int set_acls(struct archive_write_disk *, HANDLE h,
  203. const wchar_t *, struct archive_acl *);
  204. static int set_xattrs(struct archive_write_disk *);
  205. static int set_fflags(struct archive_write_disk *);
  206. static int set_ownership(struct archive_write_disk *);
  207. static int set_mode(struct archive_write_disk *, int mode);
  208. static int set_times(struct archive_write_disk *, HANDLE, int,
  209. const wchar_t *, time_t, long, time_t, long, time_t,
  210. long, time_t, long);
  211. static int set_times_from_entry(struct archive_write_disk *);
  212. static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
  213. static ssize_t write_data_block(struct archive_write_disk *,
  214. const char *, size_t);
  215. static struct archive_vtable *archive_write_disk_vtable(void);
  216. static int _archive_write_disk_close(struct archive *);
  217. static int _archive_write_disk_free(struct archive *);
  218. static int _archive_write_disk_header(struct archive *,
  219. struct archive_entry *);
  220. static int64_t _archive_write_disk_filter_bytes(struct archive *, int);
  221. static int _archive_write_disk_finish_entry(struct archive *);
  222. static ssize_t _archive_write_disk_data(struct archive *, const void *,
  223. size_t);
  224. static ssize_t _archive_write_disk_data_block(struct archive *, const void *,
  225. size_t, int64_t);
  226. #define bhfi_dev(bhfi) ((bhfi)->dwVolumeSerialNumber)
  227. /* Treat FileIndex as i-node. We should remove a sequence number
  228. * which is high-16-bits of nFileIndexHigh. */
  229. #define bhfi_ino(bhfi) \
  230. ((((int64_t)((bhfi)->nFileIndexHigh & 0x0000FFFFUL)) << 32) \
  231. + (bhfi)->nFileIndexLow)
  232. #define bhfi_size(bhfi) \
  233. ((((int64_t)(bhfi)->nFileSizeHigh) << 32) + (bhfi)->nFileSizeLow)
  234. static int
  235. file_information(struct archive_write_disk *a, wchar_t *path,
  236. BY_HANDLE_FILE_INFORMATION *st, mode_t *mode, int sim_lstat)
  237. {
  238. HANDLE h;
  239. int r;
  240. DWORD flag = FILE_FLAG_BACKUP_SEMANTICS;
  241. WIN32_FIND_DATAW findData;
  242. if (sim_lstat || mode != NULL) {
  243. h = FindFirstFileW(path, &findData);
  244. if (h == INVALID_HANDLE_VALUE &&
  245. GetLastError() == ERROR_INVALID_NAME) {
  246. wchar_t *full;
  247. full = __la_win_permissive_name_w(path);
  248. h = FindFirstFileW(full, &findData);
  249. free(full);
  250. }
  251. if (h == INVALID_HANDLE_VALUE) {
  252. la_dosmaperr(GetLastError());
  253. return (-1);
  254. }
  255. FindClose(h);
  256. }
  257. /* Is symlink file ? */
  258. if (sim_lstat &&
  259. ((findData.dwFileAttributes
  260. & FILE_ATTRIBUTE_REPARSE_POINT) &&
  261. (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK)))
  262. flag |= FILE_FLAG_OPEN_REPARSE_POINT;
  263. h = CreateFileW(a->name, 0, 0, NULL,
  264. OPEN_EXISTING, flag, NULL);
  265. if (h == INVALID_HANDLE_VALUE &&
  266. GetLastError() == ERROR_INVALID_NAME) {
  267. wchar_t *full;
  268. full = __la_win_permissive_name_w(path);
  269. h = CreateFileW(full, 0, 0, NULL,
  270. OPEN_EXISTING, flag, NULL);
  271. free(full);
  272. }
  273. if (h == INVALID_HANDLE_VALUE) {
  274. la_dosmaperr(GetLastError());
  275. return (-1);
  276. }
  277. r = GetFileInformationByHandle(h, st);
  278. CloseHandle(h);
  279. if (r == 0) {
  280. la_dosmaperr(GetLastError());
  281. return (-1);
  282. }
  283. if (mode == NULL)
  284. return (0);
  285. *mode = S_IRUSR | S_IRGRP | S_IROTH;
  286. if ((st->dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
  287. *mode |= S_IWUSR | S_IWGRP | S_IWOTH;
  288. if ((st->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
  289. findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK)
  290. *mode |= S_IFLNK;
  291. else if (st->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  292. *mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  293. else {
  294. const wchar_t *p;
  295. *mode |= S_IFREG;
  296. p = wcsrchr(path, L'.');
  297. if (p != NULL && wcslen(p) == 4) {
  298. switch (p[1]) {
  299. case L'B': case L'b':
  300. if ((p[2] == L'A' || p[2] == L'a' ) &&
  301. (p[3] == L'T' || p[3] == L't' ))
  302. *mode |= S_IXUSR | S_IXGRP | S_IXOTH;
  303. break;
  304. case L'C': case L'c':
  305. if (((p[2] == L'M' || p[2] == L'm' ) &&
  306. (p[3] == L'D' || p[3] == L'd' )) ||
  307. ((p[2] == L'M' || p[2] == L'm' ) &&
  308. (p[3] == L'D' || p[3] == L'd' )))
  309. *mode |= S_IXUSR | S_IXGRP | S_IXOTH;
  310. break;
  311. case L'E': case L'e':
  312. if ((p[2] == L'X' || p[2] == L'x' ) &&
  313. (p[3] == L'E' || p[3] == L'e' ))
  314. *mode |= S_IXUSR | S_IXGRP | S_IXOTH;
  315. break;
  316. default:
  317. break;
  318. }
  319. }
  320. }
  321. return (0);
  322. }
  323. /*
  324. * Note: The path, for example, "aa/a/../b../c" will be converted to "aa/c"
  325. * by GetFullPathNameW() W32 API, which __la_win_permissive_name_w uses.
  326. * It means we cannot handle multiple dirs in one archive_entry.
  327. * So we have to make the full-pathname in another way, which does not
  328. * break "../" path string.
  329. */
  330. static int
  331. permissive_name_w(struct archive_write_disk *a)
  332. {
  333. wchar_t *wn, *wnp;
  334. wchar_t *ws, *wsp;
  335. DWORD l;
  336. wnp = a->name;
  337. if (wnp[0] == L'\\' && wnp[1] == L'\\' &&
  338. wnp[2] == L'?' && wnp[3] == L'\\')
  339. /* We have already a permissive name. */
  340. return (0);
  341. if (wnp[0] == L'\\' && wnp[1] == L'\\' &&
  342. wnp[2] == L'.' && wnp[3] == L'\\') {
  343. /* This is a device name */
  344. if (((wnp[4] >= L'a' && wnp[4] <= L'z') ||
  345. (wnp[4] >= L'A' && wnp[4] <= L'Z')) &&
  346. wnp[5] == L':' && wnp[6] == L'\\') {
  347. wnp[2] = L'?';/* Not device name. */
  348. return (0);
  349. }
  350. }
  351. /*
  352. * A full-pathname starting with a drive name like "C:\abc".
  353. */
  354. if (((wnp[0] >= L'a' && wnp[0] <= L'z') ||
  355. (wnp[0] >= L'A' && wnp[0] <= L'Z')) &&
  356. wnp[1] == L':' && wnp[2] == L'\\') {
  357. wn = _wcsdup(wnp);
  358. if (wn == NULL)
  359. return (-1);
  360. archive_wstring_ensure(&(a->_name_data), 4 + wcslen(wn) + 1);
  361. a->name = a->_name_data.s;
  362. /* Prepend "\\?\" */
  363. archive_wstrncpy(&(a->_name_data), L"\\\\?\\", 4);
  364. archive_wstrcat(&(a->_name_data), wn);
  365. free(wn);
  366. return (0);
  367. }
  368. /*
  369. * A full-pathname pointig a network drive
  370. * like "\\<server-name>\<share-name>\file".
  371. */
  372. if (wnp[0] == L'\\' && wnp[1] == L'\\' && wnp[2] != L'\\') {
  373. const wchar_t *p = &wnp[2];
  374. /* Skip server-name letters. */
  375. while (*p != L'\\' && *p != L'\0')
  376. ++p;
  377. if (*p == L'\\') {
  378. const wchar_t *rp = ++p;
  379. /* Skip share-name letters. */
  380. while (*p != L'\\' && *p != L'\0')
  381. ++p;
  382. if (*p == L'\\' && p != rp) {
  383. /* Now, match patterns such as
  384. * "\\server-name\share-name\" */
  385. wn = _wcsdup(wnp);
  386. if (wn == NULL)
  387. return (-1);
  388. archive_wstring_ensure(&(a->_name_data),
  389. 8 + wcslen(wn) + 1);
  390. a->name = a->_name_data.s;
  391. /* Prepend "\\?\UNC\" */
  392. archive_wstrncpy(&(a->_name_data),
  393. L"\\\\?\\UNC\\", 8);
  394. archive_wstrcat(&(a->_name_data), wn+2);
  395. free(wn);
  396. return (0);
  397. }
  398. }
  399. return (0);
  400. }
  401. /*
  402. * Get current working directory.
  403. */
  404. l = GetCurrentDirectoryW(0, NULL);
  405. if (l == 0)
  406. return (-1);
  407. ws = malloc(l * sizeof(wchar_t));
  408. l = GetCurrentDirectoryW(l, ws);
  409. if (l == 0) {
  410. free(ws);
  411. return (-1);
  412. }
  413. wsp = ws;
  414. /*
  415. * A full-pathname starting without a drive name like "\abc".
  416. */
  417. if (wnp[0] == L'\\') {
  418. wn = _wcsdup(wnp);
  419. if (wn == NULL)
  420. return (-1);
  421. archive_wstring_ensure(&(a->_name_data),
  422. 4 + 2 + wcslen(wn) + 1);
  423. a->name = a->_name_data.s;
  424. /* Prepend "\\?\" and drive name. */
  425. archive_wstrncpy(&(a->_name_data), L"\\\\?\\", 4);
  426. archive_wstrncat(&(a->_name_data), wsp, 2);
  427. archive_wstrcat(&(a->_name_data), wn);
  428. free(wsp);
  429. free(wn);
  430. return (0);
  431. }
  432. wn = _wcsdup(wnp);
  433. if (wn == NULL)
  434. return (-1);
  435. archive_wstring_ensure(&(a->_name_data), 4 + l + 1 + wcslen(wn) + 1);
  436. a->name = a->_name_data.s;
  437. /* Prepend "\\?\" and drive name. */
  438. archive_wstrncpy(&(a->_name_data), L"\\\\?\\", 4);
  439. archive_wstrncat(&(a->_name_data), wsp, l);
  440. archive_wstrncat(&(a->_name_data), L"\\", 1);
  441. archive_wstrcat(&(a->_name_data), wn);
  442. a->name = a->_name_data.s;
  443. free(wsp);
  444. free(wn);
  445. return (0);
  446. }
  447. static int
  448. la_chmod(const wchar_t *path, mode_t mode)
  449. {
  450. DWORD attr;
  451. BOOL r;
  452. wchar_t *fullname;
  453. int ret = 0;
  454. fullname = NULL;
  455. attr = GetFileAttributesW(path);
  456. if (attr == (DWORD)-1 &&
  457. GetLastError() == ERROR_INVALID_NAME) {
  458. fullname = __la_win_permissive_name_w(path);
  459. attr = GetFileAttributesW(fullname);
  460. }
  461. if (attr == (DWORD)-1) {
  462. la_dosmaperr(GetLastError());
  463. ret = -1;
  464. goto exit_chmode;
  465. }
  466. if (mode & _S_IWRITE)
  467. attr &= ~FILE_ATTRIBUTE_READONLY;
  468. else
  469. attr |= FILE_ATTRIBUTE_READONLY;
  470. if (fullname != NULL)
  471. r = SetFileAttributesW(fullname, attr);
  472. else
  473. r = SetFileAttributesW(path, attr);
  474. if (r == 0) {
  475. la_dosmaperr(GetLastError());
  476. ret = -1;
  477. }
  478. exit_chmode:
  479. free(fullname);
  480. return (ret);
  481. }
  482. static void *
  483. la_GetFunctionKernel32(const char *name)
  484. {
  485. static HINSTANCE lib;
  486. static int set;
  487. if (!set) {
  488. set = 1;
  489. lib = LoadLibrary(TEXT("kernel32.dll"));
  490. }
  491. if (lib == NULL) {
  492. fprintf(stderr, "Can't load kernel32.dll?!\n");
  493. exit(1);
  494. }
  495. return (void *)GetProcAddress(lib, name);
  496. }
  497. static int
  498. la_CreateHardLinkW(wchar_t *linkname, wchar_t *target)
  499. {
  500. static BOOLEAN (WINAPI *f)(LPWSTR, LPWSTR, LPSECURITY_ATTRIBUTES);
  501. static int set;
  502. BOOL ret;
  503. if (!set) {
  504. set = 1;
  505. f = la_GetFunctionKernel32("CreateHardLinkW");
  506. }
  507. if (!f)
  508. return (0);
  509. ret = (*f)(linkname, target, NULL);
  510. if (!ret) {
  511. /* Under windows 2000, it is necessary to remove
  512. * the "\\?\" prefix. */
  513. #define IS_UNC(name) ((name[0] == L'U' || name[0] == L'u') && \
  514. (name[1] == L'N' || name[1] == L'n') && \
  515. (name[2] == L'C' || name[2] == L'c') && \
  516. name[3] == L'\\')
  517. if (!wcsncmp(linkname,L"\\\\?\\", 4)) {
  518. linkname += 4;
  519. if (IS_UNC(linkname))
  520. linkname += 4;
  521. }
  522. if (!wcsncmp(target,L"\\\\?\\", 4)) {
  523. target += 4;
  524. if (IS_UNC(target))
  525. target += 4;
  526. }
  527. #undef IS_UNC
  528. ret = (*f)(linkname, target, NULL);
  529. }
  530. return (ret);
  531. }
  532. static int
  533. la_ftruncate(HANDLE handle, int64_t length)
  534. {
  535. LARGE_INTEGER distance;
  536. if (GetFileType(handle) != FILE_TYPE_DISK) {
  537. errno = EBADF;
  538. return (-1);
  539. }
  540. distance.QuadPart = length;
  541. if (!SetFilePointerEx_perso(handle, distance, NULL, FILE_BEGIN)) {
  542. la_dosmaperr(GetLastError());
  543. return (-1);
  544. }
  545. if (!SetEndOfFile(handle)) {
  546. la_dosmaperr(GetLastError());
  547. return (-1);
  548. }
  549. return (0);
  550. }
  551. static int
  552. lazy_stat(struct archive_write_disk *a)
  553. {
  554. if (a->pst != NULL) {
  555. /* Already have stat() data available. */
  556. return (ARCHIVE_OK);
  557. }
  558. if (a->fh != INVALID_HANDLE_VALUE &&
  559. GetFileInformationByHandle(a->fh, &a->st) == 0) {
  560. a->pst = &a->st;
  561. return (ARCHIVE_OK);
  562. }
  563. /*
  564. * XXX At this point, symlinks should not be hit, otherwise
  565. * XXX a race occurred. Do we want to check explicitly for that?
  566. */
  567. if (file_information(a, a->name, &a->st, NULL, 1) == 0) {
  568. a->pst = &a->st;
  569. return (ARCHIVE_OK);
  570. }
  571. archive_set_error(&a->archive, errno, "Couldn't stat file");
  572. return (ARCHIVE_WARN);
  573. }
  574. static struct archive_vtable *
  575. archive_write_disk_vtable(void)
  576. {
  577. static struct archive_vtable av;
  578. static int inited = 0;
  579. if (!inited) {
  580. av.archive_close = _archive_write_disk_close;
  581. av.archive_filter_bytes = _archive_write_disk_filter_bytes;
  582. av.archive_free = _archive_write_disk_free;
  583. av.archive_write_header = _archive_write_disk_header;
  584. av.archive_write_finish_entry
  585. = _archive_write_disk_finish_entry;
  586. av.archive_write_data = _archive_write_disk_data;
  587. av.archive_write_data_block = _archive_write_disk_data_block;
  588. inited = 1;
  589. }
  590. return (&av);
  591. }
  592. static int64_t
  593. _archive_write_disk_filter_bytes(struct archive *_a, int n)
  594. {
  595. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  596. (void)n; /* UNUSED */
  597. if (n == -1 || n == 0)
  598. return (a->total_bytes_written);
  599. return (-1);
  600. }
  601. int
  602. archive_write_disk_set_options(struct archive *_a, int flags)
  603. {
  604. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  605. a->flags = flags;
  606. return (ARCHIVE_OK);
  607. }
  608. /*
  609. * Extract this entry to disk.
  610. *
  611. * TODO: Validate hardlinks. According to the standards, we're
  612. * supposed to check each extracted hardlink and squawk if it refers
  613. * to a file that we didn't restore. I'm not entirely convinced this
  614. * is a good idea, but more importantly: Is there any way to validate
  615. * hardlinks without keeping a complete list of filenames from the
  616. * entire archive?? Ugh.
  617. *
  618. */
  619. static int
  620. _archive_write_disk_header(struct archive *_a, struct archive_entry *entry)
  621. {
  622. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  623. struct fixup_entry *fe;
  624. int ret, r;
  625. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  626. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  627. "archive_write_disk_header");
  628. archive_clear_error(&a->archive);
  629. if (a->archive.state & ARCHIVE_STATE_DATA) {
  630. r = _archive_write_disk_finish_entry(&a->archive);
  631. if (r == ARCHIVE_FATAL)
  632. return (r);
  633. }
  634. /* Set up for this particular entry. */
  635. a->pst = NULL;
  636. a->current_fixup = NULL;
  637. a->deferred = 0;
  638. if (a->entry) {
  639. archive_entry_free(a->entry);
  640. a->entry = NULL;
  641. }
  642. a->entry = archive_entry_clone(entry);
  643. a->fh = INVALID_HANDLE_VALUE;
  644. a->fd_offset = 0;
  645. a->offset = 0;
  646. a->restore_pwd = -1;
  647. a->uid = a->user_uid;
  648. a->mode = archive_entry_mode(a->entry);
  649. if (archive_entry_size_is_set(a->entry))
  650. a->filesize = archive_entry_size(a->entry);
  651. else
  652. a->filesize = -1;
  653. archive_wstrcpy(&(a->_name_data), archive_entry_pathname_w(a->entry));
  654. a->name = a->_name_data.s;
  655. archive_clear_error(&a->archive);
  656. /*
  657. * Clean up the requested path. This is necessary for correct
  658. * dir restores; the dir restore logic otherwise gets messed
  659. * up by nonsense like "dir/.".
  660. */
  661. ret = cleanup_pathname(a);
  662. if (ret != ARCHIVE_OK)
  663. return (ret);
  664. /*
  665. * Generate a full-pathname and use it from here.
  666. */
  667. if (permissive_name_w(a) < 0) {
  668. errno = EINVAL;
  669. return (ARCHIVE_FAILED);
  670. }
  671. /*
  672. * Query the umask so we get predictable mode settings.
  673. * This gets done on every call to _write_header in case the
  674. * user edits their umask during the extraction for some
  675. * reason.
  676. */
  677. umask(a->user_umask = umask(0));
  678. /* Figure out what we need to do for this entry. */
  679. a->todo = TODO_MODE_BASE;
  680. if (a->flags & ARCHIVE_EXTRACT_PERM) {
  681. a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */
  682. /*
  683. * SGID requires an extra "check" step because we
  684. * cannot easily predict the GID that the system will
  685. * assign. (Different systems assign GIDs to files
  686. * based on a variety of criteria, including process
  687. * credentials and the gid of the enclosing
  688. * directory.) We can only restore the SGID bit if
  689. * the file has the right GID, and we only know the
  690. * GID if we either set it (see set_ownership) or if
  691. * we've actually called stat() on the file after it
  692. * was restored. Since there are several places at
  693. * which we might verify the GID, we need a TODO bit
  694. * to keep track.
  695. */
  696. if (a->mode & S_ISGID)
  697. a->todo |= TODO_SGID | TODO_SGID_CHECK;
  698. /*
  699. * Verifying the SUID is simpler, but can still be
  700. * done in multiple ways, hence the separate "check" bit.
  701. */
  702. if (a->mode & S_ISUID)
  703. a->todo |= TODO_SUID | TODO_SUID_CHECK;
  704. } else {
  705. /*
  706. * User didn't request full permissions, so don't
  707. * restore SUID, SGID bits and obey umask.
  708. */
  709. a->mode &= ~S_ISUID;
  710. a->mode &= ~S_ISGID;
  711. a->mode &= ~S_ISVTX;
  712. a->mode &= ~a->user_umask;
  713. }
  714. #if 0
  715. if (a->flags & ARCHIVE_EXTRACT_OWNER)
  716. a->todo |= TODO_OWNER;
  717. #endif
  718. if (a->flags & ARCHIVE_EXTRACT_TIME)
  719. a->todo |= TODO_TIMES;
  720. if (a->flags & ARCHIVE_EXTRACT_ACL) {
  721. if (archive_entry_filetype(a->entry) == AE_IFDIR)
  722. a->deferred |= TODO_ACLS;
  723. else
  724. a->todo |= TODO_ACLS;
  725. }
  726. if (a->flags & ARCHIVE_EXTRACT_XATTR)
  727. a->todo |= TODO_XATTR;
  728. if (a->flags & ARCHIVE_EXTRACT_FFLAGS)
  729. a->todo |= TODO_FFLAGS;
  730. if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) {
  731. ret = check_symlinks(a);
  732. if (ret != ARCHIVE_OK)
  733. return (ret);
  734. }
  735. ret = restore_entry(a);
  736. /*
  737. * TODO: There are rumours that some extended attributes must
  738. * be restored before file data is written. If this is true,
  739. * then we either need to write all extended attributes both
  740. * before and after restoring the data, or find some rule for
  741. * determining which must go first and which last. Due to the
  742. * many ways people are using xattrs, this may prove to be an
  743. * intractable problem.
  744. */
  745. /*
  746. * Fixup uses the unedited pathname from archive_entry_pathname(),
  747. * because it is relative to the base dir and the edited path
  748. * might be relative to some intermediate dir as a result of the
  749. * deep restore logic.
  750. */
  751. if (a->deferred & TODO_MODE) {
  752. fe = current_fixup(a, archive_entry_pathname_w(entry));
  753. fe->fixup |= TODO_MODE_BASE;
  754. fe->mode = a->mode;
  755. }
  756. if ((a->deferred & TODO_TIMES)
  757. && (archive_entry_mtime_is_set(entry)
  758. || archive_entry_atime_is_set(entry))) {
  759. fe = current_fixup(a, archive_entry_pathname_w(entry));
  760. fe->mode = a->mode;
  761. fe->fixup |= TODO_TIMES;
  762. if (archive_entry_atime_is_set(entry)) {
  763. fe->atime = archive_entry_atime(entry);
  764. fe->atime_nanos = archive_entry_atime_nsec(entry);
  765. } else {
  766. /* If atime is unset, use start time. */
  767. fe->atime = a->start_time;
  768. fe->atime_nanos = 0;
  769. }
  770. if (archive_entry_mtime_is_set(entry)) {
  771. fe->mtime = archive_entry_mtime(entry);
  772. fe->mtime_nanos = archive_entry_mtime_nsec(entry);
  773. } else {
  774. /* If mtime is unset, use start time. */
  775. fe->mtime = a->start_time;
  776. fe->mtime_nanos = 0;
  777. }
  778. if (archive_entry_birthtime_is_set(entry)) {
  779. fe->birthtime = archive_entry_birthtime(entry);
  780. fe->birthtime_nanos = archive_entry_birthtime_nsec(entry);
  781. } else {
  782. /* If birthtime is unset, use mtime. */
  783. fe->birthtime = fe->mtime;
  784. fe->birthtime_nanos = fe->mtime_nanos;
  785. }
  786. }
  787. if (a->deferred & TODO_ACLS) {
  788. fe = current_fixup(a, archive_entry_pathname_w(entry));
  789. archive_acl_copy(&fe->acl, archive_entry_acl(entry));
  790. }
  791. if (a->deferred & TODO_FFLAGS) {
  792. fe = current_fixup(a, archive_entry_pathname_w(entry));
  793. fe->fixup |= TODO_FFLAGS;
  794. /* TODO: Complete this.. defer fflags from below. */
  795. }
  796. /*
  797. * On Windows, A creating sparse file requires a special mark.
  798. */
  799. if (a->fh != INVALID_HANDLE_VALUE &&
  800. archive_entry_sparse_count(entry) > 0) {
  801. int64_t base = 0, offset, length;
  802. int i, cnt = archive_entry_sparse_reset(entry);
  803. int sparse = 0;
  804. for (i = 0; i < cnt; i++) {
  805. archive_entry_sparse_next(entry, &offset, &length);
  806. if (offset - base >= 4096) {
  807. sparse = 1;/* we have a hole. */
  808. break;
  809. }
  810. base = offset + length;
  811. }
  812. if (sparse) {
  813. DWORD dmy;
  814. /* Mark this file as sparse. */
  815. DeviceIoControl(a->fh, FSCTL_SET_SPARSE,
  816. NULL, 0, NULL, 0, &dmy, NULL);
  817. }
  818. }
  819. /* We've created the object and are ready to pour data into it. */
  820. if (ret >= ARCHIVE_WARN)
  821. a->archive.state = ARCHIVE_STATE_DATA;
  822. /*
  823. * If it's not open, tell our client not to try writing.
  824. * In particular, dirs, links, etc, don't get written to.
  825. */
  826. if (a->fh == INVALID_HANDLE_VALUE) {
  827. archive_entry_set_size(entry, 0);
  828. a->filesize = 0;
  829. }
  830. return (ret);
  831. }
  832. int
  833. archive_write_disk_set_skip_file(struct archive *_a, int64_t d, int64_t i)
  834. {
  835. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  836. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  837. ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file");
  838. a->skip_file_set = 1;
  839. a->skip_file_dev = d;
  840. a->skip_file_ino = i;
  841. return (ARCHIVE_OK);
  842. }
  843. static ssize_t
  844. write_data_block(struct archive_write_disk *a, const char *buff, size_t size)
  845. {
  846. OVERLAPPED ol;
  847. uint64_t start_size = size;
  848. DWORD bytes_written = 0;
  849. ssize_t block_size = 0, bytes_to_write;
  850. if (size == 0)
  851. return (ARCHIVE_OK);
  852. if (a->filesize == 0 || a->fh == INVALID_HANDLE_VALUE) {
  853. archive_set_error(&a->archive, 0,
  854. "Attempt to write to an empty file");
  855. return (ARCHIVE_WARN);
  856. }
  857. if (a->flags & ARCHIVE_EXTRACT_SPARSE) {
  858. /* XXX TODO XXX Is there a more appropriate choice here ? */
  859. /* This needn't match the filesystem allocation size. */
  860. block_size = 16*1024;
  861. }
  862. /* If this write would run beyond the file size, truncate it. */
  863. if (a->filesize >= 0 && (int64_t)(a->offset + size) > a->filesize)
  864. start_size = size = (size_t)(a->filesize - a->offset);
  865. /* Write the data. */
  866. while (size > 0) {
  867. if (block_size == 0) {
  868. bytes_to_write = size;
  869. } else {
  870. /* We're sparsifying the file. */
  871. const char *p, *end;
  872. int64_t block_end;
  873. /* Skip leading zero bytes. */
  874. for (p = buff, end = buff + size; p < end; ++p) {
  875. if (*p != '\0')
  876. break;
  877. }
  878. a->offset += p - buff;
  879. size -= p - buff;
  880. buff = p;
  881. if (size == 0)
  882. break;
  883. /* Calculate next block boundary after offset. */
  884. block_end
  885. = (a->offset / block_size + 1) * block_size;
  886. /* If the adjusted write would cross block boundary,
  887. * truncate it to the block boundary. */
  888. bytes_to_write = size;
  889. if (a->offset + bytes_to_write > block_end)
  890. bytes_to_write = (DWORD)(block_end - a->offset);
  891. }
  892. memset(&ol, 0, sizeof(ol));
  893. ol.Offset = (DWORD)(a->offset & 0xFFFFFFFF);
  894. ol.OffsetHigh = (DWORD)(a->offset >> 32);
  895. if (!WriteFile(a->fh, buff, (uint32_t)bytes_to_write,
  896. &bytes_written, &ol)) {
  897. DWORD lasterr;
  898. lasterr = GetLastError();
  899. if (lasterr == ERROR_ACCESS_DENIED)
  900. errno = EBADF;
  901. else
  902. la_dosmaperr(lasterr);
  903. archive_set_error(&a->archive, errno, "Write failed");
  904. return (ARCHIVE_WARN);
  905. }
  906. buff += bytes_written;
  907. size -= bytes_written;
  908. a->total_bytes_written += bytes_written;
  909. a->offset += bytes_written;
  910. a->fd_offset = a->offset;
  911. }
  912. return ((ssize_t)(start_size - size));
  913. }
  914. static ssize_t
  915. _archive_write_disk_data_block(struct archive *_a,
  916. const void *buff, size_t size, int64_t offset)
  917. {
  918. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  919. ssize_t r;
  920. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  921. ARCHIVE_STATE_DATA, "archive_write_data_block");
  922. a->offset = offset;
  923. r = write_data_block(a, buff, size);
  924. if (r < ARCHIVE_OK)
  925. return (r);
  926. if ((size_t)r < size) {
  927. archive_set_error(&a->archive, 0,
  928. "Write request too large");
  929. return (ARCHIVE_WARN);
  930. }
  931. return (ARCHIVE_OK);
  932. }
  933. static ssize_t
  934. _archive_write_disk_data(struct archive *_a, const void *buff, size_t size)
  935. {
  936. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  937. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  938. ARCHIVE_STATE_DATA, "archive_write_data");
  939. return (write_data_block(a, buff, size));
  940. }
  941. static int
  942. _archive_write_disk_finish_entry(struct archive *_a)
  943. {
  944. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  945. int ret = ARCHIVE_OK;
  946. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  947. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  948. "archive_write_finish_entry");
  949. if (a->archive.state & ARCHIVE_STATE_HEADER)
  950. return (ARCHIVE_OK);
  951. archive_clear_error(&a->archive);
  952. /* Pad or truncate file to the right size. */
  953. if (a->fh == INVALID_HANDLE_VALUE) {
  954. /* There's no file. */
  955. } else if (a->filesize < 0) {
  956. /* File size is unknown, so we can't set the size. */
  957. } else if (a->fd_offset == a->filesize) {
  958. /* Last write ended at exactly the filesize; we're done. */
  959. /* Hopefully, this is the common case. */
  960. } else {
  961. if (la_ftruncate(a->fh, a->filesize) == -1) {
  962. archive_set_error(&a->archive, errno,
  963. "File size could not be restored");
  964. return (ARCHIVE_FAILED);
  965. }
  966. }
  967. /* Restore metadata. */
  968. /*
  969. * Look up the "real" UID only if we're going to need it.
  970. * TODO: the TODO_SGID condition can be dropped here, can't it?
  971. */
  972. if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) {
  973. a->uid = archive_write_disk_uid(&a->archive,
  974. archive_entry_uname(a->entry),
  975. archive_entry_uid(a->entry));
  976. }
  977. /* Look up the "real" GID only if we're going to need it. */
  978. /* TODO: the TODO_SUID condition can be dropped here, can't it? */
  979. if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) {
  980. a->gid = archive_write_disk_gid(&a->archive,
  981. archive_entry_gname(a->entry),
  982. archive_entry_gid(a->entry));
  983. }
  984. /*
  985. * Restore ownership before set_mode tries to restore suid/sgid
  986. * bits. If we set the owner, we know what it is and can skip
  987. * a stat() call to examine the ownership of the file on disk.
  988. */
  989. if (a->todo & TODO_OWNER)
  990. ret = set_ownership(a);
  991. /*
  992. * set_mode must precede ACLs on systems such as Solaris and
  993. * FreeBSD where setting the mode implicitly clears extended ACLs
  994. */
  995. if (a->todo & TODO_MODE) {
  996. int r2 = set_mode(a, a->mode);
  997. if (r2 < ret) ret = r2;
  998. }
  999. /*
  1000. * Security-related extended attributes (such as
  1001. * security.capability on Linux) have to be restored last,
  1002. * since they're implicitly removed by other file changes.
  1003. */
  1004. if (a->todo & TODO_XATTR) {
  1005. int r2 = set_xattrs(a);
  1006. if (r2 < ret) ret = r2;
  1007. }
  1008. /*
  1009. * Some flags prevent file modification; they must be restored after
  1010. * file contents are written.
  1011. */
  1012. if (a->todo & TODO_FFLAGS) {
  1013. int r2 = set_fflags(a);
  1014. if (r2 < ret) ret = r2;
  1015. }
  1016. /*
  1017. * Time must follow most other metadata;
  1018. * otherwise atime will get changed.
  1019. */
  1020. if (a->todo & TODO_TIMES) {
  1021. int r2 = set_times_from_entry(a);
  1022. if (r2 < ret) ret = r2;
  1023. }
  1024. /*
  1025. * ACLs must be restored after timestamps because there are
  1026. * ACLs that prevent attribute changes (including time).
  1027. */
  1028. if (a->todo & TODO_ACLS) {
  1029. int r2 = set_acls(a, a->fh,
  1030. archive_entry_pathname_w(a->entry),
  1031. archive_entry_acl(a->entry));
  1032. if (r2 < ret) ret = r2;
  1033. }
  1034. /* If there's an fd, we can close it now. */
  1035. if (a->fh != INVALID_HANDLE_VALUE) {
  1036. CloseHandle(a->fh);
  1037. a->fh = INVALID_HANDLE_VALUE;
  1038. }
  1039. /* If there's an entry, we can release it now. */
  1040. if (a->entry) {
  1041. archive_entry_free(a->entry);
  1042. a->entry = NULL;
  1043. }
  1044. a->archive.state = ARCHIVE_STATE_HEADER;
  1045. return (ret);
  1046. }
  1047. int
  1048. archive_write_disk_set_group_lookup(struct archive *_a,
  1049. void *private_data,
  1050. int64_t (*lookup_gid)(void *private, const char *gname, int64_t gid),
  1051. void (*cleanup_gid)(void *private))
  1052. {
  1053. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1054. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1055. ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup");
  1056. if (a->cleanup_gid != NULL && a->lookup_gid_data != NULL)
  1057. (a->cleanup_gid)(a->lookup_gid_data);
  1058. a->lookup_gid = lookup_gid;
  1059. a->cleanup_gid = cleanup_gid;
  1060. a->lookup_gid_data = private_data;
  1061. return (ARCHIVE_OK);
  1062. }
  1063. int
  1064. archive_write_disk_set_user_lookup(struct archive *_a,
  1065. void *private_data,
  1066. int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid),
  1067. void (*cleanup_uid)(void *private))
  1068. {
  1069. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1070. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1071. ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup");
  1072. if (a->cleanup_uid != NULL && a->lookup_uid_data != NULL)
  1073. (a->cleanup_uid)(a->lookup_uid_data);
  1074. a->lookup_uid = lookup_uid;
  1075. a->cleanup_uid = cleanup_uid;
  1076. a->lookup_uid_data = private_data;
  1077. return (ARCHIVE_OK);
  1078. }
  1079. int64_t
  1080. archive_write_disk_gid(struct archive *_a, const char *name, int64_t id)
  1081. {
  1082. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1083. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1084. ARCHIVE_STATE_ANY, "archive_write_disk_gid");
  1085. if (a->lookup_gid)
  1086. return (a->lookup_gid)(a->lookup_gid_data, name, id);
  1087. return (id);
  1088. }
  1089. int64_t
  1090. archive_write_disk_uid(struct archive *_a, const char *name, int64_t id)
  1091. {
  1092. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1093. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1094. ARCHIVE_STATE_ANY, "archive_write_disk_uid");
  1095. if (a->lookup_uid)
  1096. return (a->lookup_uid)(a->lookup_uid_data, name, id);
  1097. return (id);
  1098. }
  1099. /*
  1100. * Create a new archive_write_disk object and initialize it with global state.
  1101. */
  1102. struct archive *
  1103. archive_write_disk_new(void)
  1104. {
  1105. struct archive_write_disk *a;
  1106. a = (struct archive_write_disk *)malloc(sizeof(*a));
  1107. if (a == NULL)
  1108. return (NULL);
  1109. memset(a, 0, sizeof(*a));
  1110. a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC;
  1111. /* We're ready to write a header immediately. */
  1112. a->archive.state = ARCHIVE_STATE_HEADER;
  1113. a->archive.vtable = archive_write_disk_vtable();
  1114. a->start_time = time(NULL);
  1115. /* Query and restore the umask. */
  1116. umask(a->user_umask = umask(0));
  1117. if (archive_wstring_ensure(&a->path_safe, 512) == NULL) {
  1118. free(a);
  1119. return (NULL);
  1120. }
  1121. return (&a->archive);
  1122. }
  1123. static int
  1124. disk_unlink(wchar_t *path)
  1125. {
  1126. wchar_t *fullname;
  1127. int r;
  1128. r = _wunlink(path);
  1129. if (r != 0 && GetLastError() == ERROR_INVALID_NAME) {
  1130. fullname = __la_win_permissive_name_w(path);
  1131. r = _wunlink(fullname);
  1132. free(fullname);
  1133. }
  1134. return (r);
  1135. }
  1136. static int
  1137. disk_rmdir(wchar_t *path)
  1138. {
  1139. wchar_t *fullname;
  1140. int r;
  1141. r = _wrmdir(path);
  1142. if (r != 0 && GetLastError() == ERROR_INVALID_NAME) {
  1143. fullname = __la_win_permissive_name_w(path);
  1144. r = _wrmdir(fullname);
  1145. free(fullname);
  1146. }
  1147. return (r);
  1148. }
  1149. /*
  1150. * The main restore function.
  1151. */
  1152. static int
  1153. restore_entry(struct archive_write_disk *a)
  1154. {
  1155. int ret = ARCHIVE_OK, en;
  1156. if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
  1157. /*
  1158. * TODO: Fix this. Apparently, there are platforms
  1159. * that still allow root to hose the entire filesystem
  1160. * by unlinking a dir. The S_ISDIR() test above
  1161. * prevents us from using unlink() here if the new
  1162. * object is a dir, but that doesn't mean the old
  1163. * object isn't a dir.
  1164. */
  1165. if (disk_unlink(a->name) == 0) {
  1166. /* We removed it, reset cached stat. */
  1167. a->pst = NULL;
  1168. } else if (errno == ENOENT) {
  1169. /* File didn't exist, that's just as good. */
  1170. } else if (disk_rmdir(a->name) == 0) {
  1171. /* It was a dir, but now it's gone. */
  1172. a->pst = NULL;
  1173. } else {
  1174. /* We tried, but couldn't get rid of it. */
  1175. archive_set_error(&a->archive, errno,
  1176. "Could not unlink");
  1177. return(ARCHIVE_FAILED);
  1178. }
  1179. }
  1180. /* Try creating it first; if this fails, we'll try to recover. */
  1181. en = create_filesystem_object(a);
  1182. if ((en == ENOTDIR || en == ENOENT)
  1183. && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) {
  1184. wchar_t *full;
  1185. /* If the parent dir doesn't exist, try creating it. */
  1186. create_parent_dir(a, a->name);
  1187. /* Now try to create the object again. */
  1188. full = __la_win_permissive_name_w(a->name);
  1189. if (full == NULL) {
  1190. en = EINVAL;
  1191. } else {
  1192. /* Remove multiple directories such as "a/../b../c" */
  1193. archive_wstrcpy(&(a->_name_data), full);
  1194. a->name = a->_name_data.s;
  1195. free(full);
  1196. en = create_filesystem_object(a);
  1197. }
  1198. }
  1199. if ((en == EISDIR || en == EEXIST)
  1200. && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
  1201. /* If we're not overwriting, we're done. */
  1202. archive_entry_unset_size(a->entry);
  1203. return (ARCHIVE_OK);
  1204. }
  1205. /*
  1206. * Some platforms return EISDIR if you call
  1207. * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some
  1208. * return EEXIST. POSIX is ambiguous, requiring EISDIR
  1209. * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT)
  1210. * on an existing item.
  1211. */
  1212. if (en == EISDIR) {
  1213. /* A dir is in the way of a non-dir, rmdir it. */
  1214. if (disk_rmdir(a->name) != 0) {
  1215. archive_set_error(&a->archive, errno,
  1216. "Can't remove already-existing dir");
  1217. return (ARCHIVE_FAILED);
  1218. }
  1219. a->pst = NULL;
  1220. /* Try again. */
  1221. en = create_filesystem_object(a);
  1222. } else if (en == EEXIST) {
  1223. mode_t st_mode;
  1224. /*
  1225. * We know something is in the way, but we don't know what;
  1226. * we need to find out before we go any further.
  1227. */
  1228. int r = 0;
  1229. /*
  1230. * The SECURE_SYMLINK logic has already removed a
  1231. * symlink to a dir if the client wants that. So
  1232. * follow the symlink if we're creating a dir.
  1233. */
  1234. if (S_ISDIR(a->mode))
  1235. r = file_information(a, a->name, &a->st, &st_mode, 0);
  1236. /*
  1237. * If it's not a dir (or it's a broken symlink),
  1238. * then don't follow it.
  1239. */
  1240. if (r != 0 || !S_ISDIR(a->mode))
  1241. r = file_information(a, a->name, &a->st, &st_mode, 1);
  1242. if (r != 0) {
  1243. archive_set_error(&a->archive, errno,
  1244. "Can't stat existing object");
  1245. return (ARCHIVE_FAILED);
  1246. }
  1247. /*
  1248. * NO_OVERWRITE_NEWER doesn't apply to directories.
  1249. */
  1250. if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER)
  1251. && !S_ISDIR(st_mode)) {
  1252. if (!older(&(a->st), a->entry)) {
  1253. archive_entry_unset_size(a->entry);
  1254. return (ARCHIVE_OK);
  1255. }
  1256. }
  1257. /* If it's our archive, we're done. */
  1258. if (a->skip_file_set &&
  1259. bhfi_dev(&a->st) == a->skip_file_dev &&
  1260. bhfi_ino(&a->st) == a->skip_file_ino) {
  1261. archive_set_error(&a->archive, 0,
  1262. "Refusing to overwrite archive");
  1263. return (ARCHIVE_FAILED);
  1264. }
  1265. if (!S_ISDIR(st_mode)) {
  1266. /* A non-dir is in the way, unlink it. */
  1267. if (disk_unlink(a->name) != 0) {
  1268. archive_set_error(&a->archive, errno,
  1269. "Can't unlink already-existing object");
  1270. return (ARCHIVE_FAILED);
  1271. }
  1272. a->pst = NULL;
  1273. /* Try again. */
  1274. en = create_filesystem_object(a);
  1275. } else if (!S_ISDIR(a->mode)) {
  1276. /* A dir is in the way of a non-dir, rmdir it. */
  1277. if (disk_rmdir(a->name) != 0) {
  1278. archive_set_error(&a->archive, errno,
  1279. "Can't remove already-existing dir");
  1280. return (ARCHIVE_FAILED);
  1281. }
  1282. /* Try again. */
  1283. en = create_filesystem_object(a);
  1284. } else {
  1285. /*
  1286. * There's a dir in the way of a dir. Don't
  1287. * waste time with rmdir()/mkdir(), just fix
  1288. * up the permissions on the existing dir.
  1289. * Note that we don't change perms on existing
  1290. * dirs unless _EXTRACT_PERM is specified.
  1291. */
  1292. if ((a->mode != st_mode)
  1293. && (a->todo & TODO_MODE_FORCE))
  1294. a->deferred |= (a->todo & TODO_MODE);
  1295. /* Ownership doesn't need deferred fixup. */
  1296. en = 0; /* Forget the EEXIST. */
  1297. }
  1298. }
  1299. if (en) {
  1300. /* Everything failed; give up here. */
  1301. archive_set_error(&a->archive, en, "Can't create '%ls'",
  1302. a->name);
  1303. return (ARCHIVE_FAILED);
  1304. }
  1305. a->pst = NULL; /* Cached stat data no longer valid. */
  1306. return (ret);
  1307. }
  1308. /*
  1309. * Returns 0 if creation succeeds, or else returns errno value from
  1310. * the failed system call. Note: This function should only ever perform
  1311. * a single system call.
  1312. */
  1313. static int
  1314. create_filesystem_object(struct archive_write_disk *a)
  1315. {
  1316. /* Create the entry. */
  1317. const wchar_t *linkname;
  1318. wchar_t *fullname;
  1319. mode_t final_mode, mode;
  1320. int r;
  1321. /* We identify hard/symlinks according to the link names. */
  1322. /* Since link(2) and symlink(2) don't handle modes, we're done here. */
  1323. linkname = archive_entry_hardlink_w(a->entry);
  1324. if (linkname != NULL) {
  1325. wchar_t *linkfull, *namefull;
  1326. linkfull = __la_win_permissive_name_w(linkname);
  1327. namefull = __la_win_permissive_name_w(a->name);
  1328. if (linkfull == NULL || namefull == NULL) {
  1329. errno = EINVAL;
  1330. r = -1;
  1331. } else {
  1332. r = la_CreateHardLinkW(namefull, linkfull);
  1333. if (r == 0) {
  1334. la_dosmaperr(GetLastError());
  1335. r = errno;
  1336. } else
  1337. r = 0;
  1338. }
  1339. /*
  1340. * New cpio and pax formats allow hardlink entries
  1341. * to carry data, so we may have to open the file
  1342. * for hardlink entries.
  1343. *
  1344. * If the hardlink was successfully created and
  1345. * the archive doesn't have carry data for it,
  1346. * consider it to be non-authoritative for meta data.
  1347. * This is consistent with GNU tar and BSD pax.
  1348. * If the hardlink does carry data, let the last
  1349. * archive entry decide ownership.
  1350. */
  1351. if (r == 0 && a->filesize <= 0) {
  1352. a->todo = 0;
  1353. a->deferred = 0;
  1354. } else if (r == 0 && a->filesize > 0) {
  1355. a->fh = CreateFileW(namefull, GENERIC_WRITE, 0, NULL,
  1356. TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1357. if (a->fh == INVALID_HANDLE_VALUE) {
  1358. la_dosmaperr(GetLastError());
  1359. r = errno;
  1360. }
  1361. }
  1362. free(linkfull);
  1363. free(namefull);
  1364. return (r);
  1365. }
  1366. linkname = archive_entry_symlink_w(a->entry);
  1367. if (linkname != NULL) {
  1368. #if HAVE_SYMLINK
  1369. return symlink(linkname, a->name) ? errno : 0;
  1370. #else
  1371. return (EPERM);
  1372. #endif
  1373. }
  1374. /*
  1375. * The remaining system calls all set permissions, so let's
  1376. * try to take advantage of that to avoid an extra chmod()
  1377. * call. (Recall that umask is set to zero right now!)
  1378. */
  1379. /* Mode we want for the final restored object (w/o file type bits). */
  1380. final_mode = a->mode & 07777;
  1381. /*
  1382. * The mode that will actually be restored in this step. Note
  1383. * that SUID, SGID, etc, require additional work to ensure
  1384. * security, so we never restore them at this point.
  1385. */
  1386. mode = final_mode & 0777 & ~a->user_umask;
  1387. switch (a->mode & AE_IFMT) {
  1388. default:
  1389. /* POSIX requires that we fall through here. */
  1390. /* FALLTHROUGH */
  1391. case AE_IFREG:
  1392. fullname = a->name;
  1393. /* O_WRONLY | O_CREAT | O_EXCL */
  1394. a->fh = CreateFileW(fullname, GENERIC_WRITE, 0, NULL,
  1395. CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
  1396. if (a->fh == INVALID_HANDLE_VALUE &&
  1397. GetLastError() == ERROR_INVALID_NAME &&
  1398. fullname == a->name) {
  1399. fullname = __la_win_permissive_name_w(a->name);
  1400. a->fh = CreateFileW(fullname, GENERIC_WRITE, 0, NULL,
  1401. CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
  1402. }
  1403. if (a->fh == INVALID_HANDLE_VALUE) {
  1404. if (GetLastError() == ERROR_ACCESS_DENIED) {
  1405. DWORD attr;
  1406. /* Simulate an errno of POSIX system. */
  1407. attr = GetFileAttributesW(fullname);
  1408. if (attr == (DWORD)-1)
  1409. la_dosmaperr(GetLastError());
  1410. else if (attr & FILE_ATTRIBUTE_DIRECTORY)
  1411. errno = EISDIR;
  1412. else
  1413. errno = EACCES;
  1414. } else
  1415. la_dosmaperr(GetLastError());
  1416. r = 1;
  1417. } else
  1418. r = 0;
  1419. if (fullname != a->name)
  1420. free(fullname);
  1421. break;
  1422. case AE_IFCHR:
  1423. case AE_IFBLK:
  1424. /* TODO: Find a better way to warn about our inability
  1425. * to restore a block device node. */
  1426. return (EINVAL);
  1427. case AE_IFDIR:
  1428. mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
  1429. fullname = a->name;
  1430. r = CreateDirectoryW(fullname, NULL);
  1431. if (r == 0 && GetLastError() == ERROR_INVALID_NAME &&
  1432. fullname == a->name) {
  1433. fullname = __la_win_permissive_name_w(a->name);
  1434. r = CreateDirectoryW(fullname, NULL);
  1435. }
  1436. if (r != 0) {
  1437. r = 0;
  1438. /* Defer setting dir times. */
  1439. a->deferred |= (a->todo & TODO_TIMES);
  1440. a->todo &= ~TODO_TIMES;
  1441. /* Never use an immediate chmod(). */
  1442. /* We can't avoid the chmod() entirely if EXTRACT_PERM
  1443. * because of SysV SGID inheritance. */
  1444. if ((mode != final_mode)
  1445. || (a->flags & ARCHIVE_EXTRACT_PERM))
  1446. a->deferred |= (a->todo & TODO_MODE);
  1447. a->todo &= ~TODO_MODE;
  1448. } else {
  1449. la_dosmaperr(GetLastError());
  1450. r = -1;
  1451. }
  1452. if (fullname != a->name)
  1453. free(fullname);
  1454. break;
  1455. case AE_IFIFO:
  1456. /* TODO: Find a better way to warn about our inability
  1457. * to restore a fifo. */
  1458. return (EINVAL);
  1459. }
  1460. /* All the system calls above set errno on failure. */
  1461. if (r)
  1462. return (errno);
  1463. /* If we managed to set the final mode, we've avoided a chmod(). */
  1464. if (mode == final_mode)
  1465. a->todo &= ~TODO_MODE;
  1466. return (0);
  1467. }
  1468. /*
  1469. * Cleanup function for archive_extract. Mostly, this involves processing
  1470. * the fixup list, which is used to address a number of problems:
  1471. * * Dir permissions might prevent us from restoring a file in that
  1472. * dir, so we restore the dir with minimum 0700 permissions first,
  1473. * then correct the mode at the end.
  1474. * * Similarly, the act of restoring a file touches the directory
  1475. * and changes the timestamp on the dir, so we have to touch-up dir
  1476. * timestamps at the end as well.
  1477. * * Some file flags can interfere with the restore by, for example,
  1478. * preventing the creation of hardlinks to those files.
  1479. * * Mac OS extended metadata includes ACLs, so must be deferred on dirs.
  1480. *
  1481. * Note that tar/cpio do not require that archives be in a particular
  1482. * order; there is no way to know when the last file has been restored
  1483. * within a directory, so there's no way to optimize the memory usage
  1484. * here by fixing up the directory any earlier than the
  1485. * end-of-archive.
  1486. *
  1487. * XXX TODO: Directory ACLs should be restored here, for the same
  1488. * reason we set directory perms here. XXX
  1489. */
  1490. static int
  1491. _archive_write_disk_close(struct archive *_a)
  1492. {
  1493. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1494. struct fixup_entry *next, *p;
  1495. int ret;
  1496. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1497. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1498. "archive_write_disk_close");
  1499. ret = _archive_write_disk_finish_entry(&a->archive);
  1500. /* Sort dir list so directories are fixed up in depth-first order. */
  1501. p = sort_dir_list(a->fixup_list);
  1502. while (p != NULL) {
  1503. a->pst = NULL; /* Mark stat cache as out-of-date. */
  1504. if (p->fixup & TODO_TIMES) {
  1505. set_times(a, INVALID_HANDLE_VALUE, p->mode, p->name,
  1506. p->atime, p->atime_nanos,
  1507. p->birthtime, p->birthtime_nanos,
  1508. p->mtime, p->mtime_nanos,
  1509. p->ctime, p->ctime_nanos);
  1510. }
  1511. if (p->fixup & TODO_MODE_BASE)
  1512. la_chmod(p->name, p->mode);
  1513. if (p->fixup & TODO_ACLS)
  1514. set_acls(a, INVALID_HANDLE_VALUE, p->name, &p->acl);
  1515. next = p->next;
  1516. archive_acl_clear(&p->acl);
  1517. free(p->name);
  1518. free(p);
  1519. p = next;
  1520. }
  1521. a->fixup_list = NULL;
  1522. return (ret);
  1523. }
  1524. static int
  1525. _archive_write_disk_free(struct archive *_a)
  1526. {
  1527. struct archive_write_disk *a;
  1528. int ret;
  1529. if (_a == NULL)
  1530. return (ARCHIVE_OK);
  1531. archive_check_magic(_a, ARCHIVE_WRITE_DISK_MAGIC,
  1532. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_disk_free");
  1533. a = (struct archive_write_disk *)_a;
  1534. ret = _archive_write_disk_close(&a->archive);
  1535. archive_write_disk_set_group_lookup(&a->archive, NULL, NULL, NULL);
  1536. archive_write_disk_set_user_lookup(&a->archive, NULL, NULL, NULL);
  1537. if (a->entry)
  1538. archive_entry_free(a->entry);
  1539. archive_wstring_free(&a->_name_data);
  1540. archive_string_free(&a->archive.error_string);
  1541. archive_wstring_free(&a->path_safe);
  1542. a->archive.magic = 0;
  1543. __archive_clean(&a->archive);
  1544. free(a);
  1545. return (ret);
  1546. }
  1547. /*
  1548. * Simple O(n log n) merge sort to order the fixup list. In
  1549. * particular, we want to restore dir timestamps depth-first.
  1550. */
  1551. static struct fixup_entry *
  1552. sort_dir_list(struct fixup_entry *p)
  1553. {
  1554. struct fixup_entry *a, *b, *t;
  1555. if (p == NULL)
  1556. return (NULL);
  1557. /* A one-item list is already sorted. */
  1558. if (p->next == NULL)
  1559. return (p);
  1560. /* Step 1: split the list. */
  1561. t = p;
  1562. a = p->next->next;
  1563. while (a != NULL) {
  1564. /* Step a twice, t once. */
  1565. a = a->next;
  1566. if (a != NULL)
  1567. a = a->next;
  1568. t = t->next;
  1569. }
  1570. /* Now, t is at the mid-point, so break the list here. */
  1571. b = t->next;
  1572. t->next = NULL;
  1573. a = p;
  1574. /* Step 2: Recursively sort the two sub-lists. */
  1575. a = sort_dir_list(a);
  1576. b = sort_dir_list(b);
  1577. /* Step 3: Merge the returned lists. */
  1578. /* Pick the first element for the merged list. */
  1579. if (wcscmp(a->name, b->name) > 0) {
  1580. t = p = a;
  1581. a = a->next;
  1582. } else {
  1583. t = p = b;
  1584. b = b->next;
  1585. }
  1586. /* Always put the later element on the list first. */
  1587. while (a != NULL && b != NULL) {
  1588. if (wcscmp(a->name, b->name) > 0) {
  1589. t->next = a;
  1590. a = a->next;
  1591. } else {
  1592. t->next = b;
  1593. b = b->next;
  1594. }
  1595. t = t->next;
  1596. }
  1597. /* Only one list is non-empty, so just splice it on. */
  1598. if (a != NULL)
  1599. t->next = a;
  1600. if (b != NULL)
  1601. t->next = b;
  1602. return (p);
  1603. }
  1604. /*
  1605. * Returns a new, initialized fixup entry.
  1606. *
  1607. * TODO: Reduce the memory requirements for this list by using a tree
  1608. * structure rather than a simple list of names.
  1609. */
  1610. static struct fixup_entry *
  1611. new_fixup(struct archive_write_disk *a, const wchar_t *pathname)
  1612. {
  1613. struct fixup_entry *fe;
  1614. fe = (struct fixup_entry *)calloc(1, sizeof(struct fixup_entry));
  1615. if (fe == NULL)
  1616. return (NULL);
  1617. fe->next = a->fixup_list;
  1618. a->fixup_list = fe;
  1619. fe->fixup = 0;
  1620. fe->name = _wcsdup(pathname);
  1621. return (fe);
  1622. }
  1623. /*
  1624. * Returns a fixup structure for the current entry.
  1625. */
  1626. static struct fixup_entry *
  1627. current_fixup(struct archive_write_disk *a, const wchar_t *pathname)
  1628. {
  1629. if (a->current_fixup == NULL)
  1630. a->current_fixup = new_fixup(a, pathname);
  1631. return (a->current_fixup);
  1632. }
  1633. /* TODO: Make this work. */
  1634. /*
  1635. * TODO: The deep-directory support bypasses this; disable deep directory
  1636. * support if we're doing symlink checks.
  1637. */
  1638. /*
  1639. * TODO: Someday, integrate this with the deep dir support; they both
  1640. * scan the path and both can be optimized by comparing against other
  1641. * recent paths.
  1642. */
  1643. /* TODO: Extend this to support symlinks on Windows Vista and later. */
  1644. static int
  1645. check_symlinks(struct archive_write_disk *a)
  1646. {
  1647. wchar_t *pn, *p;
  1648. wchar_t c;
  1649. int r;
  1650. BY_HANDLE_FILE_INFORMATION st;
  1651. mode_t st_mode;
  1652. /*
  1653. * Guard against symlink tricks. Reject any archive entry whose
  1654. * destination would be altered by a symlink.
  1655. */
  1656. /* Whatever we checked last time doesn't need to be re-checked. */
  1657. pn = a->name;
  1658. p = a->path_safe.s;
  1659. while ((*pn != '\0') && (*p == *pn))
  1660. ++p, ++pn;
  1661. c = pn[0];
  1662. /* Keep going until we've checked the entire name. */
  1663. while (pn[0] != '\0' && (pn[0] != '\\' || pn[1] != '\0')) {
  1664. /* Skip the next path element. */
  1665. while (*pn != '\0' && *pn != '\\')
  1666. ++pn;
  1667. c = pn[0];
  1668. pn[0] = '\0';
  1669. /* Check that we haven't hit a symlink. */
  1670. r = file_information(a, a->name, &st, &st_mode, 1);
  1671. if (r != 0) {
  1672. /* We've hit a dir that doesn't exist; stop now. */
  1673. if (errno == ENOENT)
  1674. break;
  1675. } else if (S_ISLNK(st_mode)) {
  1676. if (c == '\0') {
  1677. /*
  1678. * Last element is symlink; remove it
  1679. * so we can overwrite it with the
  1680. * item being extracted.
  1681. */
  1682. if (disk_unlink(a->name)) {
  1683. archive_set_error(&a->archive, errno,
  1684. "Could not remove symlink %ls",
  1685. a->name);
  1686. pn[0] = c;
  1687. return (ARCHIVE_FAILED);
  1688. }
  1689. a->pst = NULL;
  1690. /*
  1691. * Even if we did remove it, a warning
  1692. * is in order. The warning is silly,
  1693. * though, if we're just replacing one
  1694. * symlink with another symlink.
  1695. */
  1696. if (!S_ISLNK(a->mode)) {
  1697. archive_set_error(&a->archive, 0,
  1698. "Removing symlink %ls",
  1699. a->name);
  1700. }
  1701. /* Symlink gone. No more problem! */
  1702. pn[0] = c;
  1703. return (0);
  1704. } else if (a->flags & ARCHIVE_EXTRACT_UNLINK) {
  1705. /* User asked us to remove problems. */
  1706. if (disk_unlink(a->name) != 0) {
  1707. archive_set_error(&a->archive, 0,
  1708. "Cannot remove intervening "
  1709. "symlink %ls", a->name);
  1710. pn[0] = c;
  1711. return (ARCHIVE_FAILED);
  1712. }
  1713. a->pst = NULL;
  1714. } else {
  1715. archive_set_error(&a->archive, 0,
  1716. "Cannot extract through symlink %ls",
  1717. a->name);
  1718. pn[0] = c;
  1719. return (ARCHIVE_FAILED);
  1720. }
  1721. }
  1722. }
  1723. pn[0] = c;
  1724. /* We've checked and/or cleaned the whole path, so remember it. */
  1725. archive_wstrcpy(&a->path_safe, a->name);
  1726. return (ARCHIVE_OK);
  1727. }
  1728. static int
  1729. guidword(wchar_t *p, int n)
  1730. {
  1731. int i;
  1732. for (i = 0; i < n; i++) {
  1733. if ((*p >= L'0' && *p <= L'9') ||
  1734. (*p >= L'a' && *p <= L'f') ||
  1735. (*p >= L'A' && *p <= L'F'))
  1736. p++;
  1737. else
  1738. return (-1);
  1739. }
  1740. return (0);
  1741. }
  1742. /*
  1743. * Canonicalize the pathname. In particular, this strips duplicate
  1744. * '\' characters, '.' elements, and trailing '\'. It also raises an
  1745. * error for an empty path, a trailing '..' or (if _SECURE_NODOTDOT is
  1746. * set) any '..' in the path.
  1747. */
  1748. static int
  1749. cleanup_pathname(struct archive_write_disk *a)
  1750. {
  1751. wchar_t *dest, *src, *p, *top;
  1752. wchar_t separator = L'\0';
  1753. p = a->name;
  1754. if (*p == L'\0') {
  1755. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1756. "Invalid empty pathname");
  1757. return (ARCHIVE_FAILED);
  1758. }
  1759. /* Replace '/' by '\' */
  1760. for (; *p != L'\0'; p++) {
  1761. if (*p == L'/')
  1762. *p = L'\\';
  1763. }
  1764. p = a->name;
  1765. /* Skip leading "\\.\" or "\\?\" or "\\?\UNC\" or
  1766. * "\\?\Volume{GUID}\"
  1767. * (absolute path prefixes used by Windows API) */
  1768. if (p[0] == L'\\' && p[1] == L'\\' &&
  1769. (p[2] == L'.' || p[2] == L'?') && p[3] == L'\\')
  1770. {
  1771. /* A path begin with "\\?\UNC\" */
  1772. if (p[2] == L'?' &&
  1773. (p[4] == L'U' || p[4] == L'u') &&
  1774. (p[5] == L'N' || p[5] == L'n') &&
  1775. (p[6] == L'C' || p[6] == L'c') &&
  1776. p[7] == L'\\')
  1777. p += 8;
  1778. /* A path begin with "\\?\Volume{GUID}\" */
  1779. else if (p[2] == L'?' &&
  1780. (p[4] == L'V' || p[4] == L'v') &&
  1781. (p[5] == L'O' || p[5] == L'o') &&
  1782. (p[6] == L'L' || p[6] == L'l') &&
  1783. (p[7] == L'U' || p[7] == L'u') &&
  1784. (p[8] == L'M' || p[8] == L'm') &&
  1785. (p[9] == L'E' || p[9] == L'e') &&
  1786. p[10] == L'{') {
  1787. if (guidword(p+11, 8) == 0 && p[19] == L'-' &&
  1788. guidword(p+20, 4) == 0 && p[24] == L'-' &&
  1789. guidword(p+25, 4) == 0 && p[29] == L'-' &&
  1790. guidword(p+30, 4) == 0 && p[34] == L'-' &&
  1791. guidword(p+35, 12) == 0 && p[47] == L'}' &&
  1792. p[48] == L'\\')
  1793. p += 49;
  1794. else
  1795. p += 4;
  1796. /* A path begin with "\\.\PhysicalDriveX" */
  1797. } else if (p[2] == L'.' &&
  1798. (p[4] == L'P' || p[4] == L'p') &&
  1799. (p[5] == L'H' || p[5] == L'h') &&
  1800. (p[6] == L'Y' || p[6] == L'y') &&
  1801. (p[7] == L'S' || p[7] == L's') &&
  1802. (p[8] == L'I' || p[8] == L'i') &&
  1803. (p[9] == L'C' || p[9] == L'c') &&
  1804. (p[9] == L'A' || p[9] == L'a') &&
  1805. (p[9] == L'L' || p[9] == L'l') &&
  1806. (p[9] == L'D' || p[9] == L'd') &&
  1807. (p[9] == L'R' || p[9] == L'r') &&
  1808. (p[9] == L'I' || p[9] == L'i') &&
  1809. (p[9] == L'V' || p[9] == L'v') &&
  1810. (p[9] == L'E' || p[9] == L'e') &&
  1811. (p[10] >= L'0' && p[10] <= L'9') &&
  1812. p[11] == L'\0') {
  1813. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1814. "Path is a physical drive name");
  1815. return (ARCHIVE_FAILED);
  1816. } else
  1817. p += 4;
  1818. }
  1819. /* Skip leading drive letter from archives created
  1820. * on Windows. */
  1821. if (((p[0] >= L'a' && p[0] <= L'z') ||
  1822. (p[0] >= L'A' && p[0] <= L'Z')) &&
  1823. p[1] == L':') {
  1824. if (p[2] == L'\0') {
  1825. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1826. "Path is a drive name");
  1827. return (ARCHIVE_FAILED);
  1828. }
  1829. if (p[2] == L'\\')
  1830. p += 2;
  1831. }
  1832. top = dest = src = p;
  1833. /* Rewrite the path name if its character is a unusable. */
  1834. for (; *p != L'\0'; p++) {
  1835. if (*p == L':' || *p == L'*' || *p == L'?' || *p == L'"' ||
  1836. *p == L'<' || *p == L'>' || *p == L'|')
  1837. *p = L'_';
  1838. }
  1839. /* Skip leading '\'. */
  1840. if (*src == L'\\')
  1841. separator = *src++;
  1842. /* Scan the pathname one element at a time. */
  1843. for (;;) {
  1844. /* src points to first char after '\' */
  1845. if (src[0] == L'\0') {
  1846. break;
  1847. } else if (src[0] == L'\\') {
  1848. /* Found '\\'('//'), ignore second one. */
  1849. src++;
  1850. continue;
  1851. } else if (src[0] == L'.') {
  1852. if (src[1] == L'\0') {
  1853. /* Ignore trailing '.' */
  1854. break;
  1855. } else if (src[1] == L'\\') {
  1856. /* Skip '.\'. */
  1857. src += 2;
  1858. continue;
  1859. } else if (src[1] == L'.') {
  1860. if (src[2] == L'\\' || src[2] == L'\0') {
  1861. /* Conditionally warn about '..' */
  1862. if (a->flags &
  1863. ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
  1864. archive_set_error(&a->archive,
  1865. ARCHIVE_ERRNO_MISC,
  1866. "Path contains '..'");
  1867. return (ARCHIVE_FAILED);
  1868. }
  1869. }
  1870. /*
  1871. * Note: Under no circumstances do we
  1872. * remove '..' elements. In
  1873. * particular, restoring
  1874. * '\foo\..\bar\' should create the
  1875. * 'foo' dir as a side-effect.
  1876. */
  1877. }
  1878. }
  1879. /* Copy current element, including leading '\'. */
  1880. if (separator)
  1881. *dest++ = L'\\';
  1882. while (*src != L'\0' && *src != L'\\') {
  1883. *dest++ = *src++;
  1884. }
  1885. if (*src == L'\0')
  1886. break;
  1887. /* Skip '\' separator. */
  1888. separator = *src++;
  1889. }
  1890. /*
  1891. * We've just copied zero or more path elements, not including the
  1892. * final '\'.
  1893. */
  1894. if (dest == top) {
  1895. /*
  1896. * Nothing got copied. The path must have been something
  1897. * like '.' or '\' or './' or '/././././/./'.
  1898. */
  1899. if (separator)
  1900. *dest++ = L'\\';
  1901. else
  1902. *dest++ = L'.';
  1903. }
  1904. /* Terminate the result. */
  1905. *dest = L'\0';
  1906. return (ARCHIVE_OK);
  1907. }
  1908. /*
  1909. * Create the parent directory of the specified path, assuming path
  1910. * is already in mutable storage.
  1911. */
  1912. static int
  1913. create_parent_dir(struct archive_write_disk *a, wchar_t *path)
  1914. {
  1915. wchar_t *slash;
  1916. int r;
  1917. /* Remove tail element to obtain parent name. */
  1918. slash = wcsrchr(path, L'\\');
  1919. if (slash == NULL)
  1920. return (ARCHIVE_OK);
  1921. *slash = L'\0';
  1922. r = create_dir(a, path);
  1923. *slash = L'\\';
  1924. return (r);
  1925. }
  1926. /*
  1927. * Create the specified dir, recursing to create parents as necessary.
  1928. *
  1929. * Returns ARCHIVE_OK if the path exists when we're done here.
  1930. * Otherwise, returns ARCHIVE_FAILED.
  1931. * Assumes path is in mutable storage; path is unchanged on exit.
  1932. */
  1933. static int
  1934. create_dir(struct archive_write_disk *a, wchar_t *path)
  1935. {
  1936. BY_HANDLE_FILE_INFORMATION st;
  1937. struct fixup_entry *le;
  1938. wchar_t *slash, *base, *full;
  1939. mode_t mode_final, mode, st_mode;
  1940. int r;
  1941. /* Check for special names and just skip them. */
  1942. slash = wcsrchr(path, L'\\');
  1943. if (slash == NULL)
  1944. base = path;
  1945. else
  1946. base = slash + 1;
  1947. if (base[0] == L'\0' ||
  1948. (base[0] == L'.' && base[1] == L'\0') ||
  1949. (base[0] == L'.' && base[1] == L'.' && base[2] == L'\0')) {
  1950. /* Don't bother trying to create null path, '.', or '..'. */
  1951. if (slash != NULL) {
  1952. *slash = L'\0';
  1953. r = create_dir(a, path);
  1954. *slash = L'\\';
  1955. return (r);
  1956. }
  1957. return (ARCHIVE_OK);
  1958. }
  1959. /*
  1960. * Yes, this should be stat() and not lstat(). Using lstat()
  1961. * here loses the ability to extract through symlinks. Also note
  1962. * that this should not use the a->st cache.
  1963. */
  1964. if (file_information(a, path, &st, &st_mode, 0) == 0) {
  1965. if (S_ISDIR(st_mode))
  1966. return (ARCHIVE_OK);
  1967. if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
  1968. archive_set_error(&a->archive, EEXIST,
  1969. "Can't create directory '%ls'", path);
  1970. return (ARCHIVE_FAILED);
  1971. }
  1972. if (disk_unlink(path) != 0) {
  1973. archive_set_error(&a->archive, errno,
  1974. "Can't create directory '%ls': "
  1975. "Conflicting file cannot be removed",
  1976. path);
  1977. return (ARCHIVE_FAILED);
  1978. }
  1979. } else if (errno != ENOENT && errno != ENOTDIR) {
  1980. /* Stat failed? */
  1981. archive_set_error(&a->archive, errno,
  1982. "Can't test directory '%ls'", path);
  1983. return (ARCHIVE_FAILED);
  1984. } else if (slash != NULL) {
  1985. *slash = '\0';
  1986. r = create_dir(a, path);
  1987. *slash = '\\';
  1988. if (r != ARCHIVE_OK)
  1989. return (r);
  1990. }
  1991. /*
  1992. * Mode we want for the final restored directory. Per POSIX,
  1993. * implicitly-created dirs must be created obeying the umask.
  1994. * There's no mention whether this is different for privileged
  1995. * restores (which the rest of this code handles by pretending
  1996. * umask=0). I've chosen here to always obey the user's umask for
  1997. * implicit dirs, even if _EXTRACT_PERM was specified.
  1998. */
  1999. mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
  2000. /* Mode we want on disk during the restore process. */
  2001. mode = mode_final;
  2002. mode |= MINIMUM_DIR_MODE;
  2003. mode &= MAXIMUM_DIR_MODE;
  2004. /*
  2005. * Apply __la_win_permissive_name_w to path in order to
  2006. * remove '../' path string.
  2007. */
  2008. full = __la_win_permissive_name_w(path);
  2009. if (full == NULL)
  2010. errno = EINVAL;
  2011. else if (CreateDirectoryW(full, NULL) != 0) {
  2012. if (mode != mode_final) {
  2013. le = new_fixup(a, path);
  2014. le->fixup |=TODO_MODE_BASE;
  2015. le->mode = mode_final;
  2016. }
  2017. free(full);
  2018. return (ARCHIVE_OK);
  2019. } else {
  2020. la_dosmaperr(GetLastError());
  2021. }
  2022. free(full);
  2023. /*
  2024. * Without the following check, a/b/../b/c/d fails at the
  2025. * second visit to 'b', so 'd' can't be created. Note that we
  2026. * don't add it to the fixup list here, as it's already been
  2027. * added.
  2028. */
  2029. if (file_information(a, path, &st, &st_mode, 0) == 0 &&
  2030. S_ISDIR(st_mode))
  2031. return (ARCHIVE_OK);
  2032. archive_set_error(&a->archive, errno, "Failed to create dir '%ls'",
  2033. path);
  2034. return (ARCHIVE_FAILED);
  2035. }
  2036. /*
  2037. * Note: Although we can skip setting the user id if the desired user
  2038. * id matches the current user, we cannot skip setting the group, as
  2039. * many systems set the gid based on the containing directory. So
  2040. * we have to perform a chown syscall if we want to set the SGID
  2041. * bit. (The alternative is to stat() and then possibly chown(); it's
  2042. * more efficient to skip the stat() and just always chown().) Note
  2043. * that a successful chown() here clears the TODO_SGID_CHECK bit, which
  2044. * allows set_mode to skip the stat() check for the GID.
  2045. */
  2046. static int
  2047. set_ownership(struct archive_write_disk *a)
  2048. {
  2049. /* unfortunately, on win32 there is no 'root' user with uid 0,
  2050. so we just have to try the chown and see if it works */
  2051. /* If we know we can't change it, don't bother trying. */
  2052. if (a->user_uid != 0 && a->user_uid != a->uid) {
  2053. archive_set_error(&a->archive, errno,
  2054. "Can't set UID=%jd", (intmax_t)a->uid);
  2055. return (ARCHIVE_WARN);
  2056. }
  2057. archive_set_error(&a->archive, errno,
  2058. "Can't set user=%jd/group=%jd for %ls",
  2059. (intmax_t)a->uid, (intmax_t)a->gid, a->name);
  2060. return (ARCHIVE_WARN);
  2061. }
  2062. static int
  2063. set_times(struct archive_write_disk *a,
  2064. HANDLE h, int mode, const wchar_t *name,
  2065. time_t atime, long atime_nanos,
  2066. time_t birthtime, long birthtime_nanos,
  2067. time_t mtime, long mtime_nanos,
  2068. time_t ctime_sec, long ctime_nanos)
  2069. {
  2070. #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
  2071. #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
  2072. + (((nsec)/1000)*10))
  2073. HANDLE hw = 0;
  2074. ULARGE_INTEGER wintm;
  2075. FILETIME *pfbtime;
  2076. FILETIME fatime, fbtime, fmtime;
  2077. (void)ctime_sec; /* UNUSED */
  2078. (void)ctime_nanos; /* UNUSED */
  2079. if (h != INVALID_HANDLE_VALUE) {
  2080. hw = NULL;
  2081. } else {
  2082. wchar_t *ws;
  2083. if (S_ISLNK(mode))
  2084. return (ARCHIVE_OK);
  2085. ws = __la_win_permissive_name_w(name);
  2086. if (ws == NULL)
  2087. goto settimes_failed;
  2088. hw = CreateFileW(ws, FILE_WRITE_ATTRIBUTES,
  2089. 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  2090. free(ws);
  2091. if (hw == INVALID_HANDLE_VALUE)
  2092. goto settimes_failed;
  2093. h = hw;
  2094. }
  2095. wintm.QuadPart = WINTIME(atime, atime_nanos);
  2096. fatime.dwLowDateTime = wintm.LowPart;
  2097. fatime.dwHighDateTime = wintm.HighPart;
  2098. wintm.QuadPart = WINTIME(mtime, mtime_nanos);
  2099. fmtime.dwLowDateTime = wintm.LowPart;
  2100. fmtime.dwHighDateTime = wintm.HighPart;
  2101. /*
  2102. * SetFileTime() supports birthtime.
  2103. */
  2104. if (birthtime > 0 || birthtime_nanos > 0) {
  2105. wintm.QuadPart = WINTIME(birthtime, birthtime_nanos);
  2106. fbtime.dwLowDateTime = wintm.LowPart;
  2107. fbtime.dwHighDateTime = wintm.HighPart;
  2108. pfbtime = &fbtime;
  2109. } else
  2110. pfbtime = NULL;
  2111. if (SetFileTime(h, pfbtime, &fatime, &fmtime) == 0)
  2112. goto settimes_failed;
  2113. CloseHandle(hw);
  2114. return (ARCHIVE_OK);
  2115. settimes_failed:
  2116. CloseHandle(hw);
  2117. archive_set_error(&a->archive, EINVAL, "Can't restore time");
  2118. return (ARCHIVE_WARN);
  2119. }
  2120. static int
  2121. set_times_from_entry(struct archive_write_disk *a)
  2122. {
  2123. time_t atime, birthtime, mtime, ctime_sec;
  2124. long atime_nsec, birthtime_nsec, mtime_nsec, ctime_nsec;
  2125. /* Suitable defaults. */
  2126. atime = birthtime = mtime = ctime_sec = a->start_time;
  2127. atime_nsec = birthtime_nsec = mtime_nsec = ctime_nsec = 0;
  2128. /* If no time was provided, we're done. */
  2129. if (!archive_entry_atime_is_set(a->entry)
  2130. && !archive_entry_birthtime_is_set(a->entry)
  2131. && !archive_entry_mtime_is_set(a->entry))
  2132. return (ARCHIVE_OK);
  2133. if (archive_entry_atime_is_set(a->entry)) {
  2134. atime = archive_entry_atime(a->entry);
  2135. atime_nsec = archive_entry_atime_nsec(a->entry);
  2136. }
  2137. if (archive_entry_birthtime_is_set(a->entry)) {
  2138. birthtime = archive_entry_birthtime(a->entry);
  2139. birthtime_nsec = archive_entry_birthtime_nsec(a->entry);
  2140. }
  2141. if (archive_entry_mtime_is_set(a->entry)) {
  2142. mtime = archive_entry_mtime(a->entry);
  2143. mtime_nsec = archive_entry_mtime_nsec(a->entry);
  2144. }
  2145. if (archive_entry_ctime_is_set(a->entry)) {
  2146. ctime_sec = archive_entry_ctime(a->entry);
  2147. ctime_nsec = archive_entry_ctime_nsec(a->entry);
  2148. }
  2149. return set_times(a, a->fh, a->mode, a->name,
  2150. atime, atime_nsec,
  2151. birthtime, birthtime_nsec,
  2152. mtime, mtime_nsec,
  2153. ctime_sec, ctime_nsec);
  2154. }
  2155. static int
  2156. set_mode(struct archive_write_disk *a, int mode)
  2157. {
  2158. int r = ARCHIVE_OK;
  2159. mode &= 07777; /* Strip off file type bits. */
  2160. if (a->todo & TODO_SGID_CHECK) {
  2161. /*
  2162. * If we don't know the GID is right, we must stat()
  2163. * to verify it. We can't just check the GID of this
  2164. * process, since systems sometimes set GID from
  2165. * the enclosing dir or based on ACLs.
  2166. */
  2167. if ((r = lazy_stat(a)) != ARCHIVE_OK)
  2168. return (r);
  2169. if (0 != a->gid) {
  2170. mode &= ~ S_ISGID;
  2171. }
  2172. /* While we're here, double-check the UID. */
  2173. if (0 != a->uid
  2174. && (a->todo & TODO_SUID)) {
  2175. mode &= ~ S_ISUID;
  2176. }
  2177. a->todo &= ~TODO_SGID_CHECK;
  2178. a->todo &= ~TODO_SUID_CHECK;
  2179. } else if (a->todo & TODO_SUID_CHECK) {
  2180. /*
  2181. * If we don't know the UID is right, we can just check
  2182. * the user, since all systems set the file UID from
  2183. * the process UID.
  2184. */
  2185. if (a->user_uid != a->uid) {
  2186. mode &= ~ S_ISUID;
  2187. }
  2188. a->todo &= ~TODO_SUID_CHECK;
  2189. }
  2190. if (S_ISLNK(a->mode)) {
  2191. #ifdef HAVE_LCHMOD
  2192. /*
  2193. * If this is a symlink, use lchmod(). If the
  2194. * platform doesn't support lchmod(), just skip it. A
  2195. * platform that doesn't provide a way to set
  2196. * permissions on symlinks probably ignores
  2197. * permissions on symlinks, so a failure here has no
  2198. * impact.
  2199. */
  2200. if (lchmod(a->name, mode) != 0) {
  2201. archive_set_error(&a->archive, errno,
  2202. "Can't set permissions to 0%o", (int)mode);
  2203. r = ARCHIVE_WARN;
  2204. }
  2205. #endif
  2206. } else if (!S_ISDIR(a->mode)) {
  2207. /*
  2208. * If it's not a symlink and not a dir, then use
  2209. * fchmod() or chmod(), depending on whether we have
  2210. * an fd. Dirs get their perms set during the
  2211. * post-extract fixup, which is handled elsewhere.
  2212. */
  2213. #ifdef HAVE_FCHMOD
  2214. if (a->fd >= 0) {
  2215. if (fchmod(a->fd, mode) != 0) {
  2216. archive_set_error(&a->archive, errno,
  2217. "Can't set permissions to 0%o", (int)mode);
  2218. r = ARCHIVE_WARN;
  2219. }
  2220. } else
  2221. #endif
  2222. /* If this platform lacks fchmod(), then
  2223. * we'll just use chmod(). */
  2224. if (la_chmod(a->name, mode) != 0) {
  2225. archive_set_error(&a->archive, errno,
  2226. "Can't set permissions to 0%o", (int)mode);
  2227. r = ARCHIVE_WARN;
  2228. }
  2229. }
  2230. return (r);
  2231. }
  2232. static int
  2233. set_fflags(struct archive_write_disk *a)
  2234. {
  2235. (void)a; /* UNUSED */
  2236. return (ARCHIVE_OK);
  2237. }
  2238. /* Default empty function body to satisfy mainline code. */
  2239. static int
  2240. set_acls(struct archive_write_disk *a, HANDLE h, const wchar_t *name,
  2241. struct archive_acl *acl)
  2242. {
  2243. (void)a; /* UNUSED */
  2244. (void)h; /* UNUSED */
  2245. (void)name; /* UNUSED */
  2246. (void)acl; /* UNUSED */
  2247. return (ARCHIVE_OK);
  2248. }
  2249. /*
  2250. * Restore extended attributes - stub implementation for unsupported systems
  2251. */
  2252. static int
  2253. set_xattrs(struct archive_write_disk *a)
  2254. {
  2255. static int warning_done = 0;
  2256. /* If there aren't any extended attributes, then it's okay not
  2257. * to extract them, otherwise, issue a single warning. */
  2258. if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
  2259. warning_done = 1;
  2260. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2261. "Cannot restore extended attributes on this system");
  2262. return (ARCHIVE_WARN);
  2263. }
  2264. /* Warning was already emitted; suppress further warnings. */
  2265. return (ARCHIVE_OK);
  2266. }
  2267. static void
  2268. fileTimeToUtc(const FILETIME *filetime, time_t *t, long *ns)
  2269. {
  2270. ULARGE_INTEGER utc;
  2271. utc.HighPart = filetime->dwHighDateTime;
  2272. utc.LowPart = filetime->dwLowDateTime;
  2273. if (utc.QuadPart >= EPOC_TIME) {
  2274. utc.QuadPart -= EPOC_TIME;
  2275. /* milli seconds base */
  2276. *t = (time_t)(utc.QuadPart / 10000000);
  2277. /* nano seconds base */
  2278. *ns = (long)(utc.QuadPart % 10000000) * 100;
  2279. } else {
  2280. *t = 0;
  2281. *ns = 0;
  2282. }
  2283. }
  2284. /*
  2285. * Test if file on disk is older than entry.
  2286. */
  2287. static int
  2288. older(BY_HANDLE_FILE_INFORMATION *st, struct archive_entry *entry)
  2289. {
  2290. time_t sec;
  2291. long nsec;
  2292. fileTimeToUtc(&st->ftLastWriteTime, &sec, &nsec);
  2293. /* First, test the seconds and return if we have a definite answer. */
  2294. /* Definitely older. */
  2295. if (sec < archive_entry_mtime(entry))
  2296. return (1);
  2297. /* Definitely younger. */
  2298. if (sec > archive_entry_mtime(entry))
  2299. return (0);
  2300. if (nsec < archive_entry_mtime_nsec(entry))
  2301. return (1);
  2302. /* Same age or newer, so not older. */
  2303. return (0);
  2304. }
  2305. #endif /* _WIN32 && !__CYGWIN__ */