archive_write_disk_windows.c 78 KB

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