archive_write_disk_windows.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512
  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. *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 pointig 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 *)malloc(sizeof(*a));
  1117. if (a == NULL)
  1118. return (NULL);
  1119. memset(a, 0, sizeof(*a));
  1120. a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC;
  1121. /* We're ready to write a header immediately. */
  1122. a->archive.state = ARCHIVE_STATE_HEADER;
  1123. a->archive.vtable = archive_write_disk_vtable();
  1124. a->start_time = time(NULL);
  1125. /* Query and restore the umask. */
  1126. umask(a->user_umask = umask(0));
  1127. if (archive_wstring_ensure(&a->path_safe, 512) == NULL) {
  1128. free(a);
  1129. return (NULL);
  1130. }
  1131. return (&a->archive);
  1132. }
  1133. static int
  1134. disk_unlink(wchar_t *path)
  1135. {
  1136. wchar_t *fullname;
  1137. int r;
  1138. r = _wunlink(path);
  1139. if (r != 0 && GetLastError() == ERROR_INVALID_NAME) {
  1140. fullname = __la_win_permissive_name_w(path);
  1141. r = _wunlink(fullname);
  1142. free(fullname);
  1143. }
  1144. return (r);
  1145. }
  1146. static int
  1147. disk_rmdir(wchar_t *path)
  1148. {
  1149. wchar_t *fullname;
  1150. int r;
  1151. r = _wrmdir(path);
  1152. if (r != 0 && GetLastError() == ERROR_INVALID_NAME) {
  1153. fullname = __la_win_permissive_name_w(path);
  1154. r = _wrmdir(fullname);
  1155. free(fullname);
  1156. }
  1157. return (r);
  1158. }
  1159. /*
  1160. * The main restore function.
  1161. */
  1162. static int
  1163. restore_entry(struct archive_write_disk *a)
  1164. {
  1165. int ret = ARCHIVE_OK, en;
  1166. if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
  1167. /*
  1168. * TODO: Fix this. Apparently, there are platforms
  1169. * that still allow root to hose the entire filesystem
  1170. * by unlinking a dir. The S_ISDIR() test above
  1171. * prevents us from using unlink() here if the new
  1172. * object is a dir, but that doesn't mean the old
  1173. * object isn't a dir.
  1174. */
  1175. if (disk_unlink(a->name) == 0) {
  1176. /* We removed it, reset cached stat. */
  1177. a->pst = NULL;
  1178. } else if (errno == ENOENT) {
  1179. /* File didn't exist, that's just as good. */
  1180. } else if (disk_rmdir(a->name) == 0) {
  1181. /* It was a dir, but now it's gone. */
  1182. a->pst = NULL;
  1183. } else {
  1184. /* We tried, but couldn't get rid of it. */
  1185. archive_set_error(&a->archive, errno,
  1186. "Could not unlink");
  1187. return(ARCHIVE_FAILED);
  1188. }
  1189. }
  1190. /* Try creating it first; if this fails, we'll try to recover. */
  1191. en = create_filesystem_object(a);
  1192. if ((en == ENOTDIR || en == ENOENT)
  1193. && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) {
  1194. wchar_t *full;
  1195. /* If the parent dir doesn't exist, try creating it. */
  1196. create_parent_dir(a, a->name);
  1197. /* Now try to create the object again. */
  1198. full = __la_win_permissive_name_w(a->name);
  1199. if (full == NULL) {
  1200. en = EINVAL;
  1201. } else {
  1202. /* Remove multiple directories such as "a/../b../c" */
  1203. archive_wstrcpy(&(a->_name_data), full);
  1204. a->name = a->_name_data.s;
  1205. free(full);
  1206. en = create_filesystem_object(a);
  1207. }
  1208. }
  1209. if ((en == EISDIR || en == EEXIST)
  1210. && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
  1211. /* If we're not overwriting, we're done. */
  1212. archive_entry_unset_size(a->entry);
  1213. return (ARCHIVE_OK);
  1214. }
  1215. /*
  1216. * Some platforms return EISDIR if you call
  1217. * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some
  1218. * return EEXIST. POSIX is ambiguous, requiring EISDIR
  1219. * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT)
  1220. * on an existing item.
  1221. */
  1222. if (en == EISDIR) {
  1223. /* A dir is in the way of a non-dir, rmdir it. */
  1224. if (disk_rmdir(a->name) != 0) {
  1225. archive_set_error(&a->archive, errno,
  1226. "Can't remove already-existing dir");
  1227. return (ARCHIVE_FAILED);
  1228. }
  1229. a->pst = NULL;
  1230. /* Try again. */
  1231. en = create_filesystem_object(a);
  1232. } else if (en == EEXIST) {
  1233. mode_t st_mode;
  1234. /*
  1235. * We know something is in the way, but we don't know what;
  1236. * we need to find out before we go any further.
  1237. */
  1238. int r = 0;
  1239. /*
  1240. * The SECURE_SYMLINK logic has already removed a
  1241. * symlink to a dir if the client wants that. So
  1242. * follow the symlink if we're creating a dir.
  1243. */
  1244. if (S_ISDIR(a->mode))
  1245. r = file_information(a, a->name, &a->st, &st_mode, 0);
  1246. /*
  1247. * If it's not a dir (or it's a broken symlink),
  1248. * then don't follow it.
  1249. */
  1250. if (r != 0 || !S_ISDIR(a->mode))
  1251. r = file_information(a, a->name, &a->st, &st_mode, 1);
  1252. if (r != 0) {
  1253. archive_set_error(&a->archive, errno,
  1254. "Can't stat existing object");
  1255. return (ARCHIVE_FAILED);
  1256. }
  1257. /*
  1258. * NO_OVERWRITE_NEWER doesn't apply to directories.
  1259. */
  1260. if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER)
  1261. && !S_ISDIR(st_mode)) {
  1262. if (!older(&(a->st), a->entry)) {
  1263. archive_entry_unset_size(a->entry);
  1264. return (ARCHIVE_OK);
  1265. }
  1266. }
  1267. /* If it's our archive, we're done. */
  1268. if (a->skip_file_set &&
  1269. bhfi_dev(&a->st) == a->skip_file_dev &&
  1270. bhfi_ino(&a->st) == a->skip_file_ino) {
  1271. archive_set_error(&a->archive, 0,
  1272. "Refusing to overwrite archive");
  1273. return (ARCHIVE_FAILED);
  1274. }
  1275. if (!S_ISDIR(st_mode)) {
  1276. /* A non-dir is in the way, unlink it. */
  1277. if (disk_unlink(a->name) != 0) {
  1278. archive_set_error(&a->archive, errno,
  1279. "Can't unlink already-existing object");
  1280. return (ARCHIVE_FAILED);
  1281. }
  1282. a->pst = NULL;
  1283. /* Try again. */
  1284. en = create_filesystem_object(a);
  1285. } else if (!S_ISDIR(a->mode)) {
  1286. /* A dir is in the way of a non-dir, rmdir it. */
  1287. if (disk_rmdir(a->name) != 0) {
  1288. archive_set_error(&a->archive, errno,
  1289. "Can't remove already-existing dir");
  1290. return (ARCHIVE_FAILED);
  1291. }
  1292. /* Try again. */
  1293. en = create_filesystem_object(a);
  1294. } else {
  1295. /*
  1296. * There's a dir in the way of a dir. Don't
  1297. * waste time with rmdir()/mkdir(), just fix
  1298. * up the permissions on the existing dir.
  1299. * Note that we don't change perms on existing
  1300. * dirs unless _EXTRACT_PERM is specified.
  1301. */
  1302. if ((a->mode != st_mode)
  1303. && (a->todo & TODO_MODE_FORCE))
  1304. a->deferred |= (a->todo & TODO_MODE);
  1305. /* Ownership doesn't need deferred fixup. */
  1306. en = 0; /* Forget the EEXIST. */
  1307. }
  1308. }
  1309. if (en) {
  1310. /* Everything failed; give up here. */
  1311. archive_set_error(&a->archive, en, "Can't create '%ls'",
  1312. a->name);
  1313. return (ARCHIVE_FAILED);
  1314. }
  1315. a->pst = NULL; /* Cached stat data no longer valid. */
  1316. return (ret);
  1317. }
  1318. /*
  1319. * Returns 0 if creation succeeds, or else returns errno value from
  1320. * the failed system call. Note: This function should only ever perform
  1321. * a single system call.
  1322. */
  1323. static int
  1324. create_filesystem_object(struct archive_write_disk *a)
  1325. {
  1326. /* Create the entry. */
  1327. const wchar_t *linkname;
  1328. wchar_t *fullname;
  1329. mode_t final_mode, mode;
  1330. int r;
  1331. /* We identify hard/symlinks according to the link names. */
  1332. /* Since link(2) and symlink(2) don't handle modes, we're done here. */
  1333. linkname = archive_entry_hardlink_w(a->entry);
  1334. if (linkname != NULL) {
  1335. wchar_t *linkfull, *namefull;
  1336. linkfull = __la_win_permissive_name_w(linkname);
  1337. namefull = __la_win_permissive_name_w(a->name);
  1338. if (linkfull == NULL || namefull == NULL) {
  1339. errno = EINVAL;
  1340. r = -1;
  1341. } else {
  1342. r = la_CreateHardLinkW(namefull, linkfull);
  1343. if (r == 0) {
  1344. la_dosmaperr(GetLastError());
  1345. r = errno;
  1346. } else
  1347. r = 0;
  1348. }
  1349. /*
  1350. * New cpio and pax formats allow hardlink entries
  1351. * to carry data, so we may have to open the file
  1352. * for hardlink entries.
  1353. *
  1354. * If the hardlink was successfully created and
  1355. * the archive doesn't have carry data for it,
  1356. * consider it to be non-authoritative for meta data.
  1357. * This is consistent with GNU tar and BSD pax.
  1358. * If the hardlink does carry data, let the last
  1359. * archive entry decide ownership.
  1360. */
  1361. if (r == 0 && a->filesize <= 0) {
  1362. a->todo = 0;
  1363. a->deferred = 0;
  1364. } else if (r == 0 && a->filesize > 0) {
  1365. a->fh = CreateFileW(namefull, GENERIC_WRITE, 0, NULL,
  1366. TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1367. if (a->fh == INVALID_HANDLE_VALUE) {
  1368. la_dosmaperr(GetLastError());
  1369. r = errno;
  1370. }
  1371. }
  1372. free(linkfull);
  1373. free(namefull);
  1374. return (r);
  1375. }
  1376. linkname = archive_entry_symlink_w(a->entry);
  1377. if (linkname != NULL) {
  1378. #if HAVE_SYMLINK
  1379. return symlink(linkname, a->name) ? errno : 0;
  1380. #else
  1381. return (EPERM);
  1382. #endif
  1383. }
  1384. /*
  1385. * The remaining system calls all set permissions, so let's
  1386. * try to take advantage of that to avoid an extra chmod()
  1387. * call. (Recall that umask is set to zero right now!)
  1388. */
  1389. /* Mode we want for the final restored object (w/o file type bits). */
  1390. final_mode = a->mode & 07777;
  1391. /*
  1392. * The mode that will actually be restored in this step. Note
  1393. * that SUID, SGID, etc, require additional work to ensure
  1394. * security, so we never restore them at this point.
  1395. */
  1396. mode = final_mode & 0777 & ~a->user_umask;
  1397. switch (a->mode & AE_IFMT) {
  1398. default:
  1399. /* POSIX requires that we fall through here. */
  1400. /* FALLTHROUGH */
  1401. case AE_IFREG:
  1402. fullname = a->name;
  1403. /* O_WRONLY | O_CREAT | O_EXCL */
  1404. a->fh = CreateFileW(fullname, GENERIC_WRITE, 0, NULL,
  1405. CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
  1406. if (a->fh == INVALID_HANDLE_VALUE &&
  1407. GetLastError() == ERROR_INVALID_NAME &&
  1408. fullname == a->name) {
  1409. fullname = __la_win_permissive_name_w(a->name);
  1410. a->fh = CreateFileW(fullname, GENERIC_WRITE, 0, NULL,
  1411. CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
  1412. }
  1413. if (a->fh == INVALID_HANDLE_VALUE) {
  1414. if (GetLastError() == ERROR_ACCESS_DENIED) {
  1415. DWORD attr;
  1416. /* Simulate an errno of POSIX system. */
  1417. attr = GetFileAttributesW(fullname);
  1418. if (attr == (DWORD)-1)
  1419. la_dosmaperr(GetLastError());
  1420. else if (attr & FILE_ATTRIBUTE_DIRECTORY)
  1421. errno = EISDIR;
  1422. else
  1423. errno = EACCES;
  1424. } else
  1425. la_dosmaperr(GetLastError());
  1426. r = 1;
  1427. } else
  1428. r = 0;
  1429. if (fullname != a->name)
  1430. free(fullname);
  1431. break;
  1432. case AE_IFCHR:
  1433. case AE_IFBLK:
  1434. /* TODO: Find a better way to warn about our inability
  1435. * to restore a block device node. */
  1436. return (EINVAL);
  1437. case AE_IFDIR:
  1438. mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
  1439. fullname = a->name;
  1440. r = CreateDirectoryW(fullname, NULL);
  1441. if (r == 0 && GetLastError() == ERROR_INVALID_NAME &&
  1442. fullname == a->name) {
  1443. fullname = __la_win_permissive_name_w(a->name);
  1444. r = CreateDirectoryW(fullname, NULL);
  1445. }
  1446. if (r != 0) {
  1447. r = 0;
  1448. /* Defer setting dir times. */
  1449. a->deferred |= (a->todo & TODO_TIMES);
  1450. a->todo &= ~TODO_TIMES;
  1451. /* Never use an immediate chmod(). */
  1452. /* We can't avoid the chmod() entirely if EXTRACT_PERM
  1453. * because of SysV SGID inheritance. */
  1454. if ((mode != final_mode)
  1455. || (a->flags & ARCHIVE_EXTRACT_PERM))
  1456. a->deferred |= (a->todo & TODO_MODE);
  1457. a->todo &= ~TODO_MODE;
  1458. } else {
  1459. la_dosmaperr(GetLastError());
  1460. r = -1;
  1461. }
  1462. if (fullname != a->name)
  1463. free(fullname);
  1464. break;
  1465. case AE_IFIFO:
  1466. /* TODO: Find a better way to warn about our inability
  1467. * to restore a fifo. */
  1468. return (EINVAL);
  1469. }
  1470. /* All the system calls above set errno on failure. */
  1471. if (r)
  1472. return (errno);
  1473. /* If we managed to set the final mode, we've avoided a chmod(). */
  1474. if (mode == final_mode)
  1475. a->todo &= ~TODO_MODE;
  1476. return (0);
  1477. }
  1478. /*
  1479. * Cleanup function for archive_extract. Mostly, this involves processing
  1480. * the fixup list, which is used to address a number of problems:
  1481. * * Dir permissions might prevent us from restoring a file in that
  1482. * dir, so we restore the dir with minimum 0700 permissions first,
  1483. * then correct the mode at the end.
  1484. * * Similarly, the act of restoring a file touches the directory
  1485. * and changes the timestamp on the dir, so we have to touch-up dir
  1486. * timestamps at the end as well.
  1487. * * Some file flags can interfere with the restore by, for example,
  1488. * preventing the creation of hardlinks to those files.
  1489. * * Mac OS extended metadata includes ACLs, so must be deferred on dirs.
  1490. *
  1491. * Note that tar/cpio do not require that archives be in a particular
  1492. * order; there is no way to know when the last file has been restored
  1493. * within a directory, so there's no way to optimize the memory usage
  1494. * here by fixing up the directory any earlier than the
  1495. * end-of-archive.
  1496. *
  1497. * XXX TODO: Directory ACLs should be restored here, for the same
  1498. * reason we set directory perms here. XXX
  1499. */
  1500. static int
  1501. _archive_write_disk_close(struct archive *_a)
  1502. {
  1503. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1504. struct fixup_entry *next, *p;
  1505. int ret;
  1506. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1507. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1508. "archive_write_disk_close");
  1509. ret = _archive_write_disk_finish_entry(&a->archive);
  1510. /* Sort dir list so directories are fixed up in depth-first order. */
  1511. p = sort_dir_list(a->fixup_list);
  1512. while (p != NULL) {
  1513. a->pst = NULL; /* Mark stat cache as out-of-date. */
  1514. if (p->fixup & TODO_TIMES) {
  1515. set_times(a, INVALID_HANDLE_VALUE, p->mode, p->name,
  1516. p->atime, p->atime_nanos,
  1517. p->birthtime, p->birthtime_nanos,
  1518. p->mtime, p->mtime_nanos,
  1519. p->ctime, p->ctime_nanos);
  1520. }
  1521. if (p->fixup & TODO_MODE_BASE)
  1522. la_chmod(p->name, p->mode);
  1523. if (p->fixup & TODO_ACLS)
  1524. set_acls(a, INVALID_HANDLE_VALUE, p->name, &p->acl);
  1525. next = p->next;
  1526. archive_acl_clear(&p->acl);
  1527. free(p->name);
  1528. free(p);
  1529. p = next;
  1530. }
  1531. a->fixup_list = NULL;
  1532. return (ret);
  1533. }
  1534. static int
  1535. _archive_write_disk_free(struct archive *_a)
  1536. {
  1537. struct archive_write_disk *a;
  1538. int ret;
  1539. if (_a == NULL)
  1540. return (ARCHIVE_OK);
  1541. archive_check_magic(_a, ARCHIVE_WRITE_DISK_MAGIC,
  1542. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_disk_free");
  1543. a = (struct archive_write_disk *)_a;
  1544. ret = _archive_write_disk_close(&a->archive);
  1545. archive_write_disk_set_group_lookup(&a->archive, NULL, NULL, NULL);
  1546. archive_write_disk_set_user_lookup(&a->archive, NULL, NULL, NULL);
  1547. if (a->entry)
  1548. archive_entry_free(a->entry);
  1549. archive_wstring_free(&a->_name_data);
  1550. archive_string_free(&a->archive.error_string);
  1551. archive_wstring_free(&a->path_safe);
  1552. a->archive.magic = 0;
  1553. __archive_clean(&a->archive);
  1554. free(a);
  1555. return (ret);
  1556. }
  1557. /*
  1558. * Simple O(n log n) merge sort to order the fixup list. In
  1559. * particular, we want to restore dir timestamps depth-first.
  1560. */
  1561. static struct fixup_entry *
  1562. sort_dir_list(struct fixup_entry *p)
  1563. {
  1564. struct fixup_entry *a, *b, *t;
  1565. if (p == NULL)
  1566. return (NULL);
  1567. /* A one-item list is already sorted. */
  1568. if (p->next == NULL)
  1569. return (p);
  1570. /* Step 1: split the list. */
  1571. t = p;
  1572. a = p->next->next;
  1573. while (a != NULL) {
  1574. /* Step a twice, t once. */
  1575. a = a->next;
  1576. if (a != NULL)
  1577. a = a->next;
  1578. t = t->next;
  1579. }
  1580. /* Now, t is at the mid-point, so break the list here. */
  1581. b = t->next;
  1582. t->next = NULL;
  1583. a = p;
  1584. /* Step 2: Recursively sort the two sub-lists. */
  1585. a = sort_dir_list(a);
  1586. b = sort_dir_list(b);
  1587. /* Step 3: Merge the returned lists. */
  1588. /* Pick the first element for the merged list. */
  1589. if (wcscmp(a->name, b->name) > 0) {
  1590. t = p = a;
  1591. a = a->next;
  1592. } else {
  1593. t = p = b;
  1594. b = b->next;
  1595. }
  1596. /* Always put the later element on the list first. */
  1597. while (a != NULL && b != NULL) {
  1598. if (wcscmp(a->name, b->name) > 0) {
  1599. t->next = a;
  1600. a = a->next;
  1601. } else {
  1602. t->next = b;
  1603. b = b->next;
  1604. }
  1605. t = t->next;
  1606. }
  1607. /* Only one list is non-empty, so just splice it on. */
  1608. if (a != NULL)
  1609. t->next = a;
  1610. if (b != NULL)
  1611. t->next = b;
  1612. return (p);
  1613. }
  1614. /*
  1615. * Returns a new, initialized fixup entry.
  1616. *
  1617. * TODO: Reduce the memory requirements for this list by using a tree
  1618. * structure rather than a simple list of names.
  1619. */
  1620. static struct fixup_entry *
  1621. new_fixup(struct archive_write_disk *a, const wchar_t *pathname)
  1622. {
  1623. struct fixup_entry *fe;
  1624. fe = (struct fixup_entry *)calloc(1, sizeof(struct fixup_entry));
  1625. if (fe == NULL)
  1626. return (NULL);
  1627. fe->next = a->fixup_list;
  1628. a->fixup_list = fe;
  1629. fe->fixup = 0;
  1630. fe->name = _wcsdup(pathname);
  1631. return (fe);
  1632. }
  1633. /*
  1634. * Returns a fixup structure for the current entry.
  1635. */
  1636. static struct fixup_entry *
  1637. current_fixup(struct archive_write_disk *a, const wchar_t *pathname)
  1638. {
  1639. if (a->current_fixup == NULL)
  1640. a->current_fixup = new_fixup(a, pathname);
  1641. return (a->current_fixup);
  1642. }
  1643. /* TODO: Make this work. */
  1644. /*
  1645. * TODO: The deep-directory support bypasses this; disable deep directory
  1646. * support if we're doing symlink checks.
  1647. */
  1648. /*
  1649. * TODO: Someday, integrate this with the deep dir support; they both
  1650. * scan the path and both can be optimized by comparing against other
  1651. * recent paths.
  1652. */
  1653. /* TODO: Extend this to support symlinks on Windows Vista and later. */
  1654. static int
  1655. check_symlinks(struct archive_write_disk *a)
  1656. {
  1657. wchar_t *pn, *p;
  1658. wchar_t c;
  1659. int r;
  1660. BY_HANDLE_FILE_INFORMATION st;
  1661. mode_t st_mode;
  1662. /*
  1663. * Guard against symlink tricks. Reject any archive entry whose
  1664. * destination would be altered by a symlink.
  1665. */
  1666. /* Whatever we checked last time doesn't need to be re-checked. */
  1667. pn = a->name;
  1668. p = a->path_safe.s;
  1669. while ((*pn != '\0') && (*p == *pn))
  1670. ++p, ++pn;
  1671. c = pn[0];
  1672. /* Keep going until we've checked the entire name. */
  1673. while (pn[0] != '\0' && (pn[0] != '\\' || pn[1] != '\0')) {
  1674. /* Skip the next path element. */
  1675. while (*pn != '\0' && *pn != '\\')
  1676. ++pn;
  1677. c = pn[0];
  1678. pn[0] = '\0';
  1679. /* Check that we haven't hit a symlink. */
  1680. r = file_information(a, a->name, &st, &st_mode, 1);
  1681. if (r != 0) {
  1682. /* We've hit a dir that doesn't exist; stop now. */
  1683. if (errno == ENOENT)
  1684. break;
  1685. } else if (S_ISLNK(st_mode)) {
  1686. if (c == '\0') {
  1687. /*
  1688. * Last element is symlink; remove it
  1689. * so we can overwrite it with the
  1690. * item being extracted.
  1691. */
  1692. if (disk_unlink(a->name)) {
  1693. archive_set_error(&a->archive, errno,
  1694. "Could not remove symlink %ls",
  1695. a->name);
  1696. pn[0] = c;
  1697. return (ARCHIVE_FAILED);
  1698. }
  1699. a->pst = NULL;
  1700. /*
  1701. * Even if we did remove it, a warning
  1702. * is in order. The warning is silly,
  1703. * though, if we're just replacing one
  1704. * symlink with another symlink.
  1705. */
  1706. if (!S_ISLNK(a->mode)) {
  1707. archive_set_error(&a->archive, 0,
  1708. "Removing symlink %ls",
  1709. a->name);
  1710. }
  1711. /* Symlink gone. No more problem! */
  1712. pn[0] = c;
  1713. return (0);
  1714. } else if (a->flags & ARCHIVE_EXTRACT_UNLINK) {
  1715. /* User asked us to remove problems. */
  1716. if (disk_unlink(a->name) != 0) {
  1717. archive_set_error(&a->archive, 0,
  1718. "Cannot remove intervening "
  1719. "symlink %ls", a->name);
  1720. pn[0] = c;
  1721. return (ARCHIVE_FAILED);
  1722. }
  1723. a->pst = NULL;
  1724. } else {
  1725. archive_set_error(&a->archive, 0,
  1726. "Cannot extract through symlink %ls",
  1727. a->name);
  1728. pn[0] = c;
  1729. return (ARCHIVE_FAILED);
  1730. }
  1731. }
  1732. }
  1733. pn[0] = c;
  1734. /* We've checked and/or cleaned the whole path, so remember it. */
  1735. archive_wstrcpy(&a->path_safe, a->name);
  1736. return (ARCHIVE_OK);
  1737. }
  1738. static int
  1739. guidword(wchar_t *p, int n)
  1740. {
  1741. int i;
  1742. for (i = 0; i < n; i++) {
  1743. if ((*p >= L'0' && *p <= L'9') ||
  1744. (*p >= L'a' && *p <= L'f') ||
  1745. (*p >= L'A' && *p <= L'F'))
  1746. p++;
  1747. else
  1748. return (-1);
  1749. }
  1750. return (0);
  1751. }
  1752. /*
  1753. * Canonicalize the pathname. In particular, this strips duplicate
  1754. * '\' characters, '.' elements, and trailing '\'. It also raises an
  1755. * error for an empty path, a trailing '..' or (if _SECURE_NODOTDOT is
  1756. * set) any '..' in the path.
  1757. */
  1758. static int
  1759. cleanup_pathname(struct archive_write_disk *a)
  1760. {
  1761. wchar_t *dest, *src, *p, *top;
  1762. wchar_t separator = L'\0';
  1763. p = a->name;
  1764. if (*p == L'\0') {
  1765. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1766. "Invalid empty pathname");
  1767. return (ARCHIVE_FAILED);
  1768. }
  1769. /* Replace '/' by '\' */
  1770. for (; *p != L'\0'; p++) {
  1771. if (*p == L'/')
  1772. *p = L'\\';
  1773. }
  1774. p = a->name;
  1775. /* Skip leading "\\.\" or "\\?\" or "\\?\UNC\" or
  1776. * "\\?\Volume{GUID}\"
  1777. * (absolute path prefixes used by Windows API) */
  1778. if (p[0] == L'\\' && p[1] == L'\\' &&
  1779. (p[2] == L'.' || p[2] == L'?') && p[3] == L'\\')
  1780. {
  1781. /* A path begin with "\\?\UNC\" */
  1782. if (p[2] == L'?' &&
  1783. (p[4] == L'U' || p[4] == L'u') &&
  1784. (p[5] == L'N' || p[5] == L'n') &&
  1785. (p[6] == L'C' || p[6] == L'c') &&
  1786. p[7] == L'\\')
  1787. p += 8;
  1788. /* A path begin with "\\?\Volume{GUID}\" */
  1789. else if (p[2] == L'?' &&
  1790. (p[4] == L'V' || p[4] == L'v') &&
  1791. (p[5] == L'O' || p[5] == L'o') &&
  1792. (p[6] == L'L' || p[6] == L'l') &&
  1793. (p[7] == L'U' || p[7] == L'u') &&
  1794. (p[8] == L'M' || p[8] == L'm') &&
  1795. (p[9] == L'E' || p[9] == L'e') &&
  1796. p[10] == L'{') {
  1797. if (guidword(p+11, 8) == 0 && p[19] == L'-' &&
  1798. guidword(p+20, 4) == 0 && p[24] == L'-' &&
  1799. guidword(p+25, 4) == 0 && p[29] == L'-' &&
  1800. guidword(p+30, 4) == 0 && p[34] == L'-' &&
  1801. guidword(p+35, 12) == 0 && p[47] == L'}' &&
  1802. p[48] == L'\\')
  1803. p += 49;
  1804. else
  1805. p += 4;
  1806. /* A path begin with "\\.\PhysicalDriveX" */
  1807. } else if (p[2] == L'.' &&
  1808. (p[4] == L'P' || p[4] == L'p') &&
  1809. (p[5] == L'H' || p[5] == L'h') &&
  1810. (p[6] == L'Y' || p[6] == L'y') &&
  1811. (p[7] == L'S' || p[7] == L's') &&
  1812. (p[8] == L'I' || p[8] == L'i') &&
  1813. (p[9] == L'C' || p[9] == L'c') &&
  1814. (p[9] == L'A' || p[9] == L'a') &&
  1815. (p[9] == L'L' || p[9] == L'l') &&
  1816. (p[9] == L'D' || p[9] == L'd') &&
  1817. (p[9] == L'R' || p[9] == L'r') &&
  1818. (p[9] == L'I' || p[9] == L'i') &&
  1819. (p[9] == L'V' || p[9] == L'v') &&
  1820. (p[9] == L'E' || p[9] == L'e') &&
  1821. (p[10] >= L'0' && p[10] <= L'9') &&
  1822. p[11] == L'\0') {
  1823. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1824. "Path is a physical drive name");
  1825. return (ARCHIVE_FAILED);
  1826. } else
  1827. p += 4;
  1828. }
  1829. /* Skip leading drive letter from archives created
  1830. * on Windows. */
  1831. if (((p[0] >= L'a' && p[0] <= L'z') ||
  1832. (p[0] >= L'A' && p[0] <= L'Z')) &&
  1833. p[1] == L':') {
  1834. if (p[2] == L'\0') {
  1835. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1836. "Path is a drive name");
  1837. return (ARCHIVE_FAILED);
  1838. }
  1839. if (p[2] == L'\\')
  1840. p += 2;
  1841. }
  1842. top = dest = src = p;
  1843. /* Rewrite the path name if its character is a unusable. */
  1844. for (; *p != L'\0'; p++) {
  1845. if (*p == L':' || *p == L'*' || *p == L'?' || *p == L'"' ||
  1846. *p == L'<' || *p == L'>' || *p == L'|')
  1847. *p = L'_';
  1848. }
  1849. /* Skip leading '\'. */
  1850. if (*src == L'\\')
  1851. separator = *src++;
  1852. /* Scan the pathname one element at a time. */
  1853. for (;;) {
  1854. /* src points to first char after '\' */
  1855. if (src[0] == L'\0') {
  1856. break;
  1857. } else if (src[0] == L'\\') {
  1858. /* Found '\\'('//'), ignore second one. */
  1859. src++;
  1860. continue;
  1861. } else if (src[0] == L'.') {
  1862. if (src[1] == L'\0') {
  1863. /* Ignore trailing '.' */
  1864. break;
  1865. } else if (src[1] == L'\\') {
  1866. /* Skip '.\'. */
  1867. src += 2;
  1868. continue;
  1869. } else if (src[1] == L'.') {
  1870. if (src[2] == L'\\' || src[2] == L'\0') {
  1871. /* Conditionally warn about '..' */
  1872. if (a->flags &
  1873. ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
  1874. archive_set_error(&a->archive,
  1875. ARCHIVE_ERRNO_MISC,
  1876. "Path contains '..'");
  1877. return (ARCHIVE_FAILED);
  1878. }
  1879. }
  1880. /*
  1881. * Note: Under no circumstances do we
  1882. * remove '..' elements. In
  1883. * particular, restoring
  1884. * '\foo\..\bar\' should create the
  1885. * 'foo' dir as a side-effect.
  1886. */
  1887. }
  1888. }
  1889. /* Copy current element, including leading '\'. */
  1890. if (separator)
  1891. *dest++ = L'\\';
  1892. while (*src != L'\0' && *src != L'\\') {
  1893. *dest++ = *src++;
  1894. }
  1895. if (*src == L'\0')
  1896. break;
  1897. /* Skip '\' separator. */
  1898. separator = *src++;
  1899. }
  1900. /*
  1901. * We've just copied zero or more path elements, not including the
  1902. * final '\'.
  1903. */
  1904. if (dest == top) {
  1905. /*
  1906. * Nothing got copied. The path must have been something
  1907. * like '.' or '\' or './' or '/././././/./'.
  1908. */
  1909. if (separator)
  1910. *dest++ = L'\\';
  1911. else
  1912. *dest++ = L'.';
  1913. }
  1914. /* Terminate the result. */
  1915. *dest = L'\0';
  1916. return (ARCHIVE_OK);
  1917. }
  1918. /*
  1919. * Create the parent directory of the specified path, assuming path
  1920. * is already in mutable storage.
  1921. */
  1922. static int
  1923. create_parent_dir(struct archive_write_disk *a, wchar_t *path)
  1924. {
  1925. wchar_t *slash;
  1926. int r;
  1927. /* Remove tail element to obtain parent name. */
  1928. slash = wcsrchr(path, L'\\');
  1929. if (slash == NULL)
  1930. return (ARCHIVE_OK);
  1931. *slash = L'\0';
  1932. r = create_dir(a, path);
  1933. *slash = L'\\';
  1934. return (r);
  1935. }
  1936. /*
  1937. * Create the specified dir, recursing to create parents as necessary.
  1938. *
  1939. * Returns ARCHIVE_OK if the path exists when we're done here.
  1940. * Otherwise, returns ARCHIVE_FAILED.
  1941. * Assumes path is in mutable storage; path is unchanged on exit.
  1942. */
  1943. static int
  1944. create_dir(struct archive_write_disk *a, wchar_t *path)
  1945. {
  1946. BY_HANDLE_FILE_INFORMATION st;
  1947. struct fixup_entry *le;
  1948. wchar_t *slash, *base, *full;
  1949. mode_t mode_final, mode, st_mode;
  1950. int r;
  1951. /* Check for special names and just skip them. */
  1952. slash = wcsrchr(path, L'\\');
  1953. if (slash == NULL)
  1954. base = path;
  1955. else
  1956. base = slash + 1;
  1957. if (base[0] == L'\0' ||
  1958. (base[0] == L'.' && base[1] == L'\0') ||
  1959. (base[0] == L'.' && base[1] == L'.' && base[2] == L'\0')) {
  1960. /* Don't bother trying to create null path, '.', or '..'. */
  1961. if (slash != NULL) {
  1962. *slash = L'\0';
  1963. r = create_dir(a, path);
  1964. *slash = L'\\';
  1965. return (r);
  1966. }
  1967. return (ARCHIVE_OK);
  1968. }
  1969. /*
  1970. * Yes, this should be stat() and not lstat(). Using lstat()
  1971. * here loses the ability to extract through symlinks. Also note
  1972. * that this should not use the a->st cache.
  1973. */
  1974. if (file_information(a, path, &st, &st_mode, 0) == 0) {
  1975. if (S_ISDIR(st_mode))
  1976. return (ARCHIVE_OK);
  1977. if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
  1978. archive_set_error(&a->archive, EEXIST,
  1979. "Can't create directory '%ls'", path);
  1980. return (ARCHIVE_FAILED);
  1981. }
  1982. if (disk_unlink(path) != 0) {
  1983. archive_set_error(&a->archive, errno,
  1984. "Can't create directory '%ls': "
  1985. "Conflicting file cannot be removed",
  1986. path);
  1987. return (ARCHIVE_FAILED);
  1988. }
  1989. } else if (errno != ENOENT && errno != ENOTDIR) {
  1990. /* Stat failed? */
  1991. archive_set_error(&a->archive, errno,
  1992. "Can't test directory '%ls'", path);
  1993. return (ARCHIVE_FAILED);
  1994. } else if (slash != NULL) {
  1995. *slash = '\0';
  1996. r = create_dir(a, path);
  1997. *slash = '\\';
  1998. if (r != ARCHIVE_OK)
  1999. return (r);
  2000. }
  2001. /*
  2002. * Mode we want for the final restored directory. Per POSIX,
  2003. * implicitly-created dirs must be created obeying the umask.
  2004. * There's no mention whether this is different for privileged
  2005. * restores (which the rest of this code handles by pretending
  2006. * umask=0). I've chosen here to always obey the user's umask for
  2007. * implicit dirs, even if _EXTRACT_PERM was specified.
  2008. */
  2009. mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
  2010. /* Mode we want on disk during the restore process. */
  2011. mode = mode_final;
  2012. mode |= MINIMUM_DIR_MODE;
  2013. mode &= MAXIMUM_DIR_MODE;
  2014. /*
  2015. * Apply __la_win_permissive_name_w to path in order to
  2016. * remove '../' path string.
  2017. */
  2018. full = __la_win_permissive_name_w(path);
  2019. if (full == NULL)
  2020. errno = EINVAL;
  2021. else if (CreateDirectoryW(full, NULL) != 0) {
  2022. if (mode != mode_final) {
  2023. le = new_fixup(a, path);
  2024. le->fixup |=TODO_MODE_BASE;
  2025. le->mode = mode_final;
  2026. }
  2027. free(full);
  2028. return (ARCHIVE_OK);
  2029. } else {
  2030. la_dosmaperr(GetLastError());
  2031. }
  2032. free(full);
  2033. /*
  2034. * Without the following check, a/b/../b/c/d fails at the
  2035. * second visit to 'b', so 'd' can't be created. Note that we
  2036. * don't add it to the fixup list here, as it's already been
  2037. * added.
  2038. */
  2039. if (file_information(a, path, &st, &st_mode, 0) == 0 &&
  2040. S_ISDIR(st_mode))
  2041. return (ARCHIVE_OK);
  2042. archive_set_error(&a->archive, errno, "Failed to create dir '%ls'",
  2043. path);
  2044. return (ARCHIVE_FAILED);
  2045. }
  2046. /*
  2047. * Note: Although we can skip setting the user id if the desired user
  2048. * id matches the current user, we cannot skip setting the group, as
  2049. * many systems set the gid based on the containing directory. So
  2050. * we have to perform a chown syscall if we want to set the SGID
  2051. * bit. (The alternative is to stat() and then possibly chown(); it's
  2052. * more efficient to skip the stat() and just always chown().) Note
  2053. * that a successful chown() here clears the TODO_SGID_CHECK bit, which
  2054. * allows set_mode to skip the stat() check for the GID.
  2055. */
  2056. static int
  2057. set_ownership(struct archive_write_disk *a)
  2058. {
  2059. /* unfortunately, on win32 there is no 'root' user with uid 0,
  2060. so we just have to try the chown and see if it works */
  2061. /* If we know we can't change it, don't bother trying. */
  2062. if (a->user_uid != 0 && a->user_uid != a->uid) {
  2063. archive_set_error(&a->archive, errno,
  2064. "Can't set UID=%jd", (intmax_t)a->uid);
  2065. return (ARCHIVE_WARN);
  2066. }
  2067. archive_set_error(&a->archive, errno,
  2068. "Can't set user=%jd/group=%jd for %ls",
  2069. (intmax_t)a->uid, (intmax_t)a->gid, a->name);
  2070. return (ARCHIVE_WARN);
  2071. }
  2072. static int
  2073. set_times(struct archive_write_disk *a,
  2074. HANDLE h, int mode, const wchar_t *name,
  2075. time_t atime, long atime_nanos,
  2076. time_t birthtime, long birthtime_nanos,
  2077. time_t mtime, long mtime_nanos,
  2078. time_t ctime_sec, long ctime_nanos)
  2079. {
  2080. #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
  2081. #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
  2082. + (((nsec)/1000)*10))
  2083. HANDLE hw = 0;
  2084. ULARGE_INTEGER wintm;
  2085. FILETIME *pfbtime;
  2086. FILETIME fatime, fbtime, fmtime;
  2087. (void)ctime_sec; /* UNUSED */
  2088. (void)ctime_nanos; /* UNUSED */
  2089. if (h != INVALID_HANDLE_VALUE) {
  2090. hw = NULL;
  2091. } else {
  2092. wchar_t *ws;
  2093. if (S_ISLNK(mode))
  2094. return (ARCHIVE_OK);
  2095. ws = __la_win_permissive_name_w(name);
  2096. if (ws == NULL)
  2097. goto settimes_failed;
  2098. hw = CreateFileW(ws, FILE_WRITE_ATTRIBUTES,
  2099. 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  2100. free(ws);
  2101. if (hw == INVALID_HANDLE_VALUE)
  2102. goto settimes_failed;
  2103. h = hw;
  2104. }
  2105. wintm.QuadPart = WINTIME(atime, atime_nanos);
  2106. fatime.dwLowDateTime = wintm.LowPart;
  2107. fatime.dwHighDateTime = wintm.HighPart;
  2108. wintm.QuadPart = WINTIME(mtime, mtime_nanos);
  2109. fmtime.dwLowDateTime = wintm.LowPart;
  2110. fmtime.dwHighDateTime = wintm.HighPart;
  2111. /*
  2112. * SetFileTime() supports birthtime.
  2113. */
  2114. if (birthtime > 0 || birthtime_nanos > 0) {
  2115. wintm.QuadPart = WINTIME(birthtime, birthtime_nanos);
  2116. fbtime.dwLowDateTime = wintm.LowPart;
  2117. fbtime.dwHighDateTime = wintm.HighPart;
  2118. pfbtime = &fbtime;
  2119. } else
  2120. pfbtime = NULL;
  2121. if (SetFileTime(h, pfbtime, &fatime, &fmtime) == 0)
  2122. goto settimes_failed;
  2123. CloseHandle(hw);
  2124. return (ARCHIVE_OK);
  2125. settimes_failed:
  2126. CloseHandle(hw);
  2127. archive_set_error(&a->archive, EINVAL, "Can't restore time");
  2128. return (ARCHIVE_WARN);
  2129. }
  2130. static int
  2131. set_times_from_entry(struct archive_write_disk *a)
  2132. {
  2133. time_t atime, birthtime, mtime, ctime_sec;
  2134. long atime_nsec, birthtime_nsec, mtime_nsec, ctime_nsec;
  2135. /* Suitable defaults. */
  2136. atime = birthtime = mtime = ctime_sec = a->start_time;
  2137. atime_nsec = birthtime_nsec = mtime_nsec = ctime_nsec = 0;
  2138. /* If no time was provided, we're done. */
  2139. if (!archive_entry_atime_is_set(a->entry)
  2140. && !archive_entry_birthtime_is_set(a->entry)
  2141. && !archive_entry_mtime_is_set(a->entry))
  2142. return (ARCHIVE_OK);
  2143. if (archive_entry_atime_is_set(a->entry)) {
  2144. atime = archive_entry_atime(a->entry);
  2145. atime_nsec = archive_entry_atime_nsec(a->entry);
  2146. }
  2147. if (archive_entry_birthtime_is_set(a->entry)) {
  2148. birthtime = archive_entry_birthtime(a->entry);
  2149. birthtime_nsec = archive_entry_birthtime_nsec(a->entry);
  2150. }
  2151. if (archive_entry_mtime_is_set(a->entry)) {
  2152. mtime = archive_entry_mtime(a->entry);
  2153. mtime_nsec = archive_entry_mtime_nsec(a->entry);
  2154. }
  2155. if (archive_entry_ctime_is_set(a->entry)) {
  2156. ctime_sec = archive_entry_ctime(a->entry);
  2157. ctime_nsec = archive_entry_ctime_nsec(a->entry);
  2158. }
  2159. return set_times(a, a->fh, a->mode, a->name,
  2160. atime, atime_nsec,
  2161. birthtime, birthtime_nsec,
  2162. mtime, mtime_nsec,
  2163. ctime_sec, ctime_nsec);
  2164. }
  2165. static int
  2166. set_mode(struct archive_write_disk *a, int mode)
  2167. {
  2168. int r = ARCHIVE_OK;
  2169. mode &= 07777; /* Strip off file type bits. */
  2170. if (a->todo & TODO_SGID_CHECK) {
  2171. /*
  2172. * If we don't know the GID is right, we must stat()
  2173. * to verify it. We can't just check the GID of this
  2174. * process, since systems sometimes set GID from
  2175. * the enclosing dir or based on ACLs.
  2176. */
  2177. if ((r = lazy_stat(a)) != ARCHIVE_OK)
  2178. return (r);
  2179. if (0 != a->gid) {
  2180. mode &= ~ S_ISGID;
  2181. }
  2182. /* While we're here, double-check the UID. */
  2183. if (0 != a->uid
  2184. && (a->todo & TODO_SUID)) {
  2185. mode &= ~ S_ISUID;
  2186. }
  2187. a->todo &= ~TODO_SGID_CHECK;
  2188. a->todo &= ~TODO_SUID_CHECK;
  2189. } else if (a->todo & TODO_SUID_CHECK) {
  2190. /*
  2191. * If we don't know the UID is right, we can just check
  2192. * the user, since all systems set the file UID from
  2193. * the process UID.
  2194. */
  2195. if (a->user_uid != a->uid) {
  2196. mode &= ~ S_ISUID;
  2197. }
  2198. a->todo &= ~TODO_SUID_CHECK;
  2199. }
  2200. if (S_ISLNK(a->mode)) {
  2201. #ifdef HAVE_LCHMOD
  2202. /*
  2203. * If this is a symlink, use lchmod(). If the
  2204. * platform doesn't support lchmod(), just skip it. A
  2205. * platform that doesn't provide a way to set
  2206. * permissions on symlinks probably ignores
  2207. * permissions on symlinks, so a failure here has no
  2208. * impact.
  2209. */
  2210. if (lchmod(a->name, mode) != 0) {
  2211. archive_set_error(&a->archive, errno,
  2212. "Can't set permissions to 0%o", (int)mode);
  2213. r = ARCHIVE_WARN;
  2214. }
  2215. #endif
  2216. } else if (!S_ISDIR(a->mode)) {
  2217. /*
  2218. * If it's not a symlink and not a dir, then use
  2219. * fchmod() or chmod(), depending on whether we have
  2220. * an fd. Dirs get their perms set during the
  2221. * post-extract fixup, which is handled elsewhere.
  2222. */
  2223. #ifdef HAVE_FCHMOD
  2224. if (a->fd >= 0) {
  2225. if (fchmod(a->fd, mode) != 0) {
  2226. archive_set_error(&a->archive, errno,
  2227. "Can't set permissions to 0%o", (int)mode);
  2228. r = ARCHIVE_WARN;
  2229. }
  2230. } else
  2231. #endif
  2232. /* If this platform lacks fchmod(), then
  2233. * we'll just use chmod(). */
  2234. if (la_chmod(a->name, mode) != 0) {
  2235. archive_set_error(&a->archive, errno,
  2236. "Can't set permissions to 0%o", (int)mode);
  2237. r = ARCHIVE_WARN;
  2238. }
  2239. }
  2240. return (r);
  2241. }
  2242. static int
  2243. set_fflags(struct archive_write_disk *a)
  2244. {
  2245. (void)a; /* UNUSED */
  2246. return (ARCHIVE_OK);
  2247. }
  2248. /* Default empty function body to satisfy mainline code. */
  2249. static int
  2250. set_acls(struct archive_write_disk *a, HANDLE h, const wchar_t *name,
  2251. struct archive_acl *acl)
  2252. {
  2253. (void)a; /* UNUSED */
  2254. (void)h; /* UNUSED */
  2255. (void)name; /* UNUSED */
  2256. (void)acl; /* UNUSED */
  2257. return (ARCHIVE_OK);
  2258. }
  2259. /*
  2260. * Restore extended attributes - stub implementation for unsupported systems
  2261. */
  2262. static int
  2263. set_xattrs(struct archive_write_disk *a)
  2264. {
  2265. static int warning_done = 0;
  2266. /* If there aren't any extended attributes, then it's okay not
  2267. * to extract them, otherwise, issue a single warning. */
  2268. if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
  2269. warning_done = 1;
  2270. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2271. "Cannot restore extended attributes on this system");
  2272. return (ARCHIVE_WARN);
  2273. }
  2274. /* Warning was already emitted; suppress further warnings. */
  2275. return (ARCHIVE_OK);
  2276. }
  2277. static void
  2278. fileTimeToUtc(const FILETIME *filetime, time_t *t, long *ns)
  2279. {
  2280. ULARGE_INTEGER utc;
  2281. utc.HighPart = filetime->dwHighDateTime;
  2282. utc.LowPart = filetime->dwLowDateTime;
  2283. if (utc.QuadPart >= EPOC_TIME) {
  2284. utc.QuadPart -= EPOC_TIME;
  2285. /* milli seconds base */
  2286. *t = (time_t)(utc.QuadPart / 10000000);
  2287. /* nano seconds base */
  2288. *ns = (long)(utc.QuadPart % 10000000) * 100;
  2289. } else {
  2290. *t = 0;
  2291. *ns = 0;
  2292. }
  2293. }
  2294. /*
  2295. * Test if file on disk is older than entry.
  2296. */
  2297. static int
  2298. older(BY_HANDLE_FILE_INFORMATION *st, struct archive_entry *entry)
  2299. {
  2300. time_t sec;
  2301. long nsec;
  2302. fileTimeToUtc(&st->ftLastWriteTime, &sec, &nsec);
  2303. /* First, test the seconds and return if we have a definite answer. */
  2304. /* Definitely older. */
  2305. if (sec < archive_entry_mtime(entry))
  2306. return (1);
  2307. /* Definitely younger. */
  2308. if (sec > archive_entry_mtime(entry))
  2309. return (0);
  2310. if (nsec < archive_entry_mtime_nsec(entry))
  2311. return (1);
  2312. /* Same age or newer, so not older. */
  2313. return (0);
  2314. }
  2315. #endif /* _WIN32 && !__CYGWIN__ */