archive_write_disk_windows.c 69 KB

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