main.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /*
  2. * Copyright 2012 Austin English
  3. * Copyright 2015 Michael Müller
  4. * Copyright 2015 Sebastian Lackner
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include <windows.h>
  21. #include <fcntl.h>
  22. #include <fdi.h>
  23. #include <shlwapi.h>
  24. #include "shlobj.h"
  25. #include "wine/debug.h"
  26. #include "wine/list.h"
  27. #include "wusa.h"
  28. WINE_DEFAULT_DEBUG_CHANNEL(wusa);
  29. struct strbuf
  30. {
  31. WCHAR *buf;
  32. DWORD pos;
  33. DWORD len;
  34. };
  35. struct installer_tempdir
  36. {
  37. struct list entry;
  38. WCHAR *path;
  39. };
  40. struct installer_state
  41. {
  42. BOOL norestart;
  43. BOOL quiet;
  44. struct list tempdirs;
  45. struct list assemblies;
  46. struct list updates;
  47. };
  48. static void * CDECL cabinet_alloc(ULONG cb)
  49. {
  50. return malloc(cb);
  51. }
  52. static void CDECL cabinet_free(void *pv)
  53. {
  54. free(pv);
  55. }
  56. static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
  57. {
  58. DWORD dwAccess = 0;
  59. DWORD dwShareMode = 0;
  60. DWORD dwCreateDisposition = OPEN_EXISTING;
  61. switch (oflag & _O_ACCMODE)
  62. {
  63. case _O_RDONLY:
  64. dwAccess = GENERIC_READ;
  65. dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
  66. break;
  67. case _O_WRONLY:
  68. dwAccess = GENERIC_WRITE;
  69. dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
  70. break;
  71. case _O_RDWR:
  72. dwAccess = GENERIC_READ | GENERIC_WRITE;
  73. dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
  74. break;
  75. }
  76. if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
  77. dwCreateDisposition = CREATE_NEW;
  78. else if (oflag & _O_CREAT)
  79. dwCreateDisposition = CREATE_ALWAYS;
  80. return (INT_PTR)CreateFileA(pszFile, dwAccess, dwShareMode, NULL, dwCreateDisposition, 0, NULL);
  81. }
  82. static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
  83. {
  84. HANDLE handle = (HANDLE)hf;
  85. DWORD read;
  86. if (ReadFile(handle, pv, cb, &read, NULL))
  87. return read;
  88. return 0;
  89. }
  90. static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
  91. {
  92. HANDLE handle = (HANDLE)hf;
  93. DWORD written;
  94. if (WriteFile(handle, pv, cb, &written, NULL))
  95. return written;
  96. return 0;
  97. }
  98. static int CDECL cabinet_close(INT_PTR hf)
  99. {
  100. HANDLE handle = (HANDLE)hf;
  101. return CloseHandle(handle) ? 0 : -1;
  102. }
  103. static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
  104. {
  105. HANDLE handle = (HANDLE)hf;
  106. /* flags are compatible and so are passed straight through */
  107. return SetFilePointer(handle, dist, NULL, seektype);
  108. }
  109. static WCHAR *path_combine(const WCHAR *path, const WCHAR *filename)
  110. {
  111. WCHAR *result;
  112. DWORD length;
  113. if (!path || !filename) return NULL;
  114. length = lstrlenW(path) + lstrlenW(filename) + 2;
  115. if (!(result = malloc(length * sizeof(WCHAR)))) return NULL;
  116. lstrcpyW(result, path);
  117. if (result[0] && result[lstrlenW(result) - 1] != '\\') lstrcatW(result, L"\\");
  118. lstrcatW(result, filename);
  119. return result;
  120. }
  121. static WCHAR *get_uncompressed_path(PFDINOTIFICATION pfdin)
  122. {
  123. WCHAR *file = strdupAtoW(pfdin->psz1);
  124. WCHAR *path = path_combine(pfdin->pv, file);
  125. free(file);
  126. return path;
  127. }
  128. static BOOL is_directory(const WCHAR *path)
  129. {
  130. DWORD attrs = GetFileAttributesW(path);
  131. if (attrs == INVALID_FILE_ATTRIBUTES) return FALSE;
  132. return (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0;
  133. }
  134. static BOOL create_directory(const WCHAR *path)
  135. {
  136. if (is_directory(path)) return TRUE;
  137. if (CreateDirectoryW(path, NULL)) return TRUE;
  138. return (GetLastError() == ERROR_ALREADY_EXISTS);
  139. }
  140. static BOOL create_parent_directory(const WCHAR *filename)
  141. {
  142. WCHAR *p, *path = strdupW(filename);
  143. BOOL ret = FALSE;
  144. if (!path) return FALSE;
  145. if (!PathRemoveFileSpecW(path)) goto done;
  146. if (is_directory(path))
  147. {
  148. ret = TRUE;
  149. goto done;
  150. }
  151. for (p = path; *p; p++)
  152. {
  153. if (*p != '\\') continue;
  154. *p = 0;
  155. if (!create_directory(path)) goto done;
  156. *p = '\\';
  157. }
  158. ret = create_directory(path);
  159. done:
  160. free(path);
  161. return ret;
  162. }
  163. static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
  164. {
  165. HANDLE handle = INVALID_HANDLE_VALUE;
  166. WCHAR *file;
  167. DWORD attrs;
  168. if (!(file = get_uncompressed_path(pfdin)))
  169. return -1;
  170. TRACE("Extracting %s -> %s\n", debugstr_a(pfdin->psz1), debugstr_w(file));
  171. if (create_parent_directory(file))
  172. {
  173. attrs = pfdin->attribs;
  174. if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
  175. handle = CreateFileW(file, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
  176. }
  177. free(file);
  178. return (handle != INVALID_HANDLE_VALUE) ? (INT_PTR)handle : -1;
  179. }
  180. static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
  181. {
  182. HANDLE handle = (HANDLE)pfdin->hf;
  183. CloseHandle(handle);
  184. return 1;
  185. }
  186. static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
  187. {
  188. switch (fdint)
  189. {
  190. case fdintPARTIAL_FILE:
  191. FIXME("fdintPARTIAL_FILE not implemented\n");
  192. return 0;
  193. case fdintNEXT_CABINET:
  194. FIXME("fdintNEXT_CABINET not implemented\n");
  195. return 0;
  196. case fdintCOPY_FILE:
  197. return cabinet_copy_file(fdint, pfdin);
  198. case fdintCLOSE_FILE_INFO:
  199. return cabinet_close_file_info(fdint, pfdin);
  200. default:
  201. return 0;
  202. }
  203. }
  204. static BOOL extract_cabinet(const WCHAR *filename, const WCHAR *destination)
  205. {
  206. char *filenameA = NULL;
  207. BOOL ret = FALSE;
  208. HFDI hfdi;
  209. ERF erf;
  210. hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
  211. cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
  212. if (!hfdi) return FALSE;
  213. if ((filenameA = strdupWtoA(filename)))
  214. {
  215. ret = FDICopy(hfdi, filenameA, NULL, 0, cabinet_notify, NULL, (void *)destination);
  216. free(filenameA);
  217. }
  218. FDIDestroy(hfdi);
  219. return ret;
  220. }
  221. static const WCHAR *create_temp_directory(struct installer_state *state)
  222. {
  223. static UINT id;
  224. struct installer_tempdir *entry;
  225. WCHAR tmp[MAX_PATH];
  226. if (!GetTempPathW(ARRAY_SIZE(tmp), tmp)) return NULL;
  227. if (!(entry = malloc(sizeof(*entry)))) return NULL;
  228. if (!(entry->path = malloc((MAX_PATH + 20) * sizeof(WCHAR))))
  229. {
  230. free(entry);
  231. return NULL;
  232. }
  233. for (;;)
  234. {
  235. if (!GetTempFileNameW(tmp, L"msu", ++id, entry->path))
  236. {
  237. free(entry->path);
  238. free(entry);
  239. return NULL;
  240. }
  241. if (CreateDirectoryW(entry->path, NULL)) break;
  242. }
  243. list_add_tail(&state->tempdirs, &entry->entry);
  244. return entry->path;
  245. }
  246. static BOOL delete_directory(const WCHAR *path)
  247. {
  248. WIN32_FIND_DATAW data;
  249. WCHAR *full_path;
  250. HANDLE search;
  251. if (!(full_path = path_combine(path, L"*"))) return FALSE;
  252. search = FindFirstFileW(full_path, &data);
  253. free(full_path);
  254. if (search != INVALID_HANDLE_VALUE)
  255. {
  256. do
  257. {
  258. if (!wcscmp(data.cFileName, L".")) continue;
  259. if (!wcscmp(data.cFileName, L"..")) continue;
  260. if (!(full_path = path_combine(path, data.cFileName))) continue;
  261. if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  262. delete_directory(full_path);
  263. else
  264. DeleteFileW(full_path);
  265. free(full_path);
  266. }
  267. while (FindNextFileW(search, &data));
  268. FindClose(search);
  269. }
  270. return RemoveDirectoryW(path);
  271. }
  272. static void installer_cleanup(struct installer_state *state)
  273. {
  274. struct installer_tempdir *tempdir, *tempdir2;
  275. struct assembly_entry *assembly, *assembly2;
  276. struct dependency_entry *dependency, *dependency2;
  277. LIST_FOR_EACH_ENTRY_SAFE(tempdir, tempdir2, &state->tempdirs, struct installer_tempdir, entry)
  278. {
  279. list_remove(&tempdir->entry);
  280. delete_directory(tempdir->path);
  281. free(tempdir->path);
  282. free(tempdir);
  283. }
  284. LIST_FOR_EACH_ENTRY_SAFE(assembly, assembly2, &state->assemblies, struct assembly_entry, entry)
  285. {
  286. list_remove(&assembly->entry);
  287. free_assembly(assembly);
  288. }
  289. LIST_FOR_EACH_ENTRY_SAFE(dependency, dependency2, &state->updates, struct dependency_entry, entry)
  290. {
  291. list_remove(&dependency->entry);
  292. free_dependency(dependency);
  293. }
  294. }
  295. static BOOL str_ends_with(const WCHAR *str, const WCHAR *suffix)
  296. {
  297. DWORD str_len = lstrlenW(str), suffix_len = lstrlenW(suffix);
  298. if (suffix_len > str_len) return FALSE;
  299. return !wcsicmp(str + str_len - suffix_len, suffix);
  300. }
  301. static BOOL load_assemblies_from_cab(const WCHAR *filename, struct installer_state *state)
  302. {
  303. struct assembly_entry *assembly;
  304. const WCHAR *temp_path;
  305. WIN32_FIND_DATAW data;
  306. HANDLE search;
  307. WCHAR *path;
  308. TRACE("Processing cab file %s\n", debugstr_w(filename));
  309. if (!(temp_path = create_temp_directory(state))) return FALSE;
  310. if (!extract_cabinet(filename, temp_path))
  311. {
  312. ERR("Failed to extract %s\n", debugstr_w(filename));
  313. return FALSE;
  314. }
  315. if (!(path = path_combine(temp_path, L"_manifest_.cix.xml"))) return FALSE;
  316. if (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
  317. {
  318. FIXME("Cabinet uses proprietary msdelta file compression which is not (yet) supported\n");
  319. FIXME("Installation of msu file will most likely fail\n");
  320. }
  321. free(path);
  322. if (!(path = path_combine(temp_path, L"*"))) return FALSE;
  323. search = FindFirstFileW(path, &data);
  324. free(path);
  325. if (search != INVALID_HANDLE_VALUE)
  326. {
  327. do
  328. {
  329. if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
  330. if (!str_ends_with(data.cFileName, L".manifest") &&
  331. !str_ends_with(data.cFileName, L".mum")) continue;
  332. if (!(path = path_combine(temp_path, data.cFileName))) continue;
  333. if ((assembly = load_manifest(path)))
  334. list_add_tail(&state->assemblies, &assembly->entry);
  335. free(path);
  336. }
  337. while (FindNextFileW(search, &data));
  338. FindClose(search);
  339. }
  340. return TRUE;
  341. }
  342. static BOOL compare_assembly_string(const WCHAR *str1, const WCHAR *str2)
  343. {
  344. return !wcscmp(str1, str2) || !wcscmp(str1, L"*") || !wcscmp(str2, L"*");
  345. }
  346. static struct assembly_entry *lookup_assembly(struct list *manifest_list, struct assembly_identity *identity)
  347. {
  348. struct assembly_entry *assembly;
  349. LIST_FOR_EACH_ENTRY(assembly, manifest_list, struct assembly_entry, entry)
  350. {
  351. if (wcsicmp(assembly->identity.name, identity->name)) continue;
  352. if (!compare_assembly_string(assembly->identity.architecture, identity->architecture)) continue;
  353. if (!compare_assembly_string(assembly->identity.language, identity->language)) continue;
  354. if (!compare_assembly_string(assembly->identity.pubkey_token, identity->pubkey_token)) continue;
  355. if (!compare_assembly_string(assembly->identity.version, identity->version))
  356. {
  357. WARN("Ignoring version difference for %s (expected %s, found %s)\n",
  358. debugstr_w(identity->name), debugstr_w(identity->version), debugstr_w(assembly->identity.version));
  359. }
  360. return assembly;
  361. }
  362. return NULL;
  363. }
  364. static WCHAR *get_assembly_source(struct assembly_entry *assembly)
  365. {
  366. WCHAR *p, *path = strdupW(assembly->filename);
  367. if (path && (p = wcsrchr(path, '.'))) *p = 0;
  368. return path;
  369. }
  370. static BOOL strbuf_init(struct strbuf *buf)
  371. {
  372. buf->pos = 0;
  373. buf->len = 64;
  374. buf->buf = malloc(buf->len * sizeof(WCHAR));
  375. return buf->buf != NULL;
  376. }
  377. static void strbuf_free(struct strbuf *buf)
  378. {
  379. free(buf->buf);
  380. buf->buf = NULL;
  381. }
  382. static BOOL strbuf_append(struct strbuf *buf, const WCHAR *str, DWORD len)
  383. {
  384. DWORD new_len;
  385. WCHAR *new_buf;
  386. if (!buf->buf) return FALSE;
  387. if (!str) return TRUE;
  388. if (len == ~0U) len = lstrlenW(str);
  389. if (buf->pos + len + 1 > buf->len)
  390. {
  391. new_len = max(buf->pos + len + 1, buf->len * 2);
  392. new_buf = realloc(buf->buf, new_len * sizeof(WCHAR));
  393. if (!new_buf)
  394. {
  395. strbuf_free(buf);
  396. return FALSE;
  397. }
  398. buf->buf = new_buf;
  399. buf->len = new_len;
  400. }
  401. memcpy(&buf->buf[buf->pos], str, len * sizeof(WCHAR));
  402. buf->buf[buf->pos + len] = 0;
  403. buf->pos += len;
  404. return TRUE;
  405. }
  406. static BOOL assembly_is_wow64(const struct assembly_entry *assembly)
  407. {
  408. #ifdef __x86_64__
  409. return !wcsicmp(assembly->identity.architecture, L"x86") || !wcsicmp(assembly->identity.architecture, L"wow64");
  410. #endif
  411. return FALSE;
  412. }
  413. static WCHAR *lookup_expression(struct assembly_entry *assembly, const WCHAR *key)
  414. {
  415. WCHAR path[MAX_PATH];
  416. int csidl = 0;
  417. if (!wcsicmp(key, L"runtime.system32") || !wcsicmp(key, L"runtime.drivers") || !wcsicmp(key, L"runtime.wbem"))
  418. {
  419. if (assembly_is_wow64(assembly)) csidl = CSIDL_SYSTEMX86;
  420. else csidl = CSIDL_SYSTEM;
  421. }
  422. else if (!wcsicmp(key, L"runtime.windows") || !wcsicmp(key, L"runtime.inf")) csidl = CSIDL_WINDOWS;
  423. else if (!wcsicmp(key, L"runtime.programfiles"))
  424. {
  425. if (assembly_is_wow64(assembly)) csidl = CSIDL_PROGRAM_FILESX86;
  426. else csidl = CSIDL_PROGRAM_FILES;
  427. }
  428. else if (!wcsicmp(key, L"runtime.commonfiles"))
  429. {
  430. if (assembly_is_wow64(assembly)) csidl = CSIDL_PROGRAM_FILES_COMMONX86;
  431. else csidl = CSIDL_PROGRAM_FILES_COMMON;
  432. }
  433. #ifdef __x86_64__
  434. else if (!wcsicmp(key, L"runtime.programfilesx86")) csidl = CSIDL_PROGRAM_FILESX86;
  435. else if (!wcsicmp(key, L"runtime.commonfilesx86")) csidl = CSIDL_PROGRAM_FILES_COMMONX86;
  436. #endif
  437. else if (!wcsicmp(key, L"runtime.programdata")) csidl = CSIDL_COMMON_APPDATA;
  438. else if (!wcsicmp(key, L"runtime.fonts")) csidl = CSIDL_FONTS;
  439. if (!csidl)
  440. {
  441. FIXME("Unknown expression %s\n", debugstr_w(key));
  442. return NULL;
  443. }
  444. if (!SHGetSpecialFolderPathW(NULL, path, csidl, TRUE))
  445. {
  446. ERR("Failed to get folder path for %s\n", debugstr_w(key));
  447. return NULL;
  448. }
  449. if (!wcsicmp(key, L"runtime.inf")) wcscat(path, L"\\inf");
  450. else if (!wcsicmp(key, L"runtime.drivers")) wcscat(path, L"\\drivers");
  451. else if (!wcsicmp(key, L"runtime.wbem")) wcscat(path, L"\\wbem");
  452. return strdupW(path);
  453. }
  454. static WCHAR *expand_expression(struct assembly_entry *assembly, const WCHAR *expression)
  455. {
  456. const WCHAR *pos, *next;
  457. WCHAR *key, *value;
  458. struct strbuf buf;
  459. if (!expression || !strbuf_init(&buf)) return NULL;
  460. for (pos = expression; (next = wcsstr(pos, L"$(")); pos = next + 1)
  461. {
  462. strbuf_append(&buf, pos, next - pos);
  463. pos = next + 2;
  464. if (!(next = wcsstr(pos, L")")))
  465. {
  466. strbuf_append(&buf, L"$(", 2);
  467. break;
  468. }
  469. if (!(key = strdupWn(pos, next - pos))) goto error;
  470. value = lookup_expression(assembly, key);
  471. free(key);
  472. if (!value) goto error;
  473. strbuf_append(&buf, value, ~0U);
  474. free(value);
  475. }
  476. strbuf_append(&buf, pos, ~0U);
  477. return buf.buf;
  478. error:
  479. FIXME("Couldn't resolve expression %s\n", debugstr_w(expression));
  480. strbuf_free(&buf);
  481. return NULL;
  482. }
  483. static BOOL install_files_copy(struct assembly_entry *assembly, const WCHAR *source_path, struct fileop_entry *fileop, BOOL dryrun)
  484. {
  485. WCHAR *target_path, *target, *source = NULL;
  486. BOOL ret = FALSE;
  487. if (!(target_path = expand_expression(assembly, fileop->target))) return FALSE;
  488. if (!(target = path_combine(target_path, fileop->source))) goto error;
  489. if (!(source = path_combine(source_path, fileop->source))) goto error;
  490. if (dryrun)
  491. {
  492. if (!(ret = PathFileExistsW(source)))
  493. {
  494. ERR("Required file %s not found\n", debugstr_w(source));
  495. goto error;
  496. }
  497. }
  498. else
  499. {
  500. TRACE("Copying %s -> %s\n", debugstr_w(source), debugstr_w(target));
  501. if (!create_parent_directory(target))
  502. {
  503. ERR("Failed to create parent directory for %s\n", debugstr_w(target));
  504. goto error;
  505. }
  506. if (!(ret = CopyFileExW(source, target, NULL, NULL, NULL, 0)))
  507. {
  508. ERR("Failed to copy %s to %s\n", debugstr_w(source), debugstr_w(target));
  509. goto error;
  510. }
  511. }
  512. error:
  513. free(target_path);
  514. free(target);
  515. free(source);
  516. return ret;
  517. }
  518. static BOOL install_files(struct assembly_entry *assembly, BOOL dryrun)
  519. {
  520. struct fileop_entry *fileop;
  521. WCHAR *source_path;
  522. BOOL ret = TRUE;
  523. if (!(source_path = get_assembly_source(assembly)))
  524. {
  525. ERR("Failed to get assembly source directory\n");
  526. return FALSE;
  527. }
  528. LIST_FOR_EACH_ENTRY(fileop, &assembly->fileops, struct fileop_entry, entry)
  529. {
  530. if (!(ret = install_files_copy(assembly, source_path, fileop, dryrun))) break;
  531. }
  532. free(source_path);
  533. return ret;
  534. }
  535. static WCHAR *split_registry_key(WCHAR *key, HKEY *root)
  536. {
  537. DWORD size;
  538. WCHAR *p;
  539. if (!(p = wcschr(key, '\\'))) return NULL;
  540. size = p - key;
  541. if (lstrlenW(L"HKEY_CLASSES_ROOT") == size && !wcsncmp(key, L"HKEY_CLASSES_ROOT", size))
  542. *root = HKEY_CLASSES_ROOT;
  543. else if (lstrlenW(L"HKEY_CURRENT_CONFIG") == size && !wcsncmp(key, L"HKEY_CURRENT_CONFIG", size))
  544. *root = HKEY_CURRENT_CONFIG;
  545. else if (lstrlenW(L"HKEY_CURRENT_USER") == size && !wcsncmp(key, L"HKEY_CURRENT_USER", size))
  546. *root = HKEY_CURRENT_USER;
  547. else if (lstrlenW(L"HKEY_LOCAL_MACHINE") == size && !wcsncmp(key, L"HKEY_LOCAL_MACHINE", size))
  548. *root = HKEY_LOCAL_MACHINE;
  549. else if (lstrlenW(L"HKEY_USERS") == size && !wcsncmp(key, L"HKEY_USERS", size))
  550. *root = HKEY_USERS;
  551. else
  552. {
  553. FIXME("Unknown root key %s\n", debugstr_wn(key, size));
  554. return NULL;
  555. }
  556. return p + 1;
  557. }
  558. static BOOL install_registry_string(struct assembly_entry *assembly, HKEY key, struct registrykv_entry *registrykv, DWORD type, BOOL dryrun)
  559. {
  560. DWORD value_size;
  561. WCHAR *value = expand_expression(assembly, registrykv->value);
  562. BOOL ret = TRUE;
  563. if (registrykv->value && !value)
  564. return FALSE;
  565. value_size = value ? (lstrlenW(value) + 1) * sizeof(WCHAR) : 0;
  566. if (!dryrun && RegSetValueExW(key, registrykv->name, 0, type, (void *)value, value_size))
  567. {
  568. ERR("Failed to set registry key %s\n", debugstr_w(registrykv->name));
  569. ret = FALSE;
  570. }
  571. free(value);
  572. return ret;
  573. }
  574. static WCHAR *parse_multisz(const WCHAR *input, DWORD *size)
  575. {
  576. const WCHAR *pos, *next;
  577. struct strbuf buf;
  578. *size = 0;
  579. if (!input || !input[0] || !strbuf_init(&buf)) return NULL;
  580. for (pos = input; pos[0] == '"'; pos++)
  581. {
  582. pos++;
  583. if (!(next = wcsstr(pos, L"\""))) goto error;
  584. strbuf_append(&buf, pos, next - pos);
  585. strbuf_append(&buf, L"", ARRAY_SIZE(L""));
  586. pos = next + 1;
  587. if (!pos[0]) break;
  588. if (pos[0] != ',')
  589. {
  590. FIXME("Error while parsing REG_MULTI_SZ string: Expected comma but got '%c'\n", pos[0]);
  591. goto error;
  592. }
  593. }
  594. if (pos[0])
  595. {
  596. FIXME("Error while parsing REG_MULTI_SZ string: Garbage at end of string\n");
  597. goto error;
  598. }
  599. strbuf_append(&buf, L"", ARRAY_SIZE(L""));
  600. *size = buf.pos * sizeof(WCHAR);
  601. return buf.buf;
  602. error:
  603. strbuf_free(&buf);
  604. return NULL;
  605. }
  606. static BOOL install_registry_multisz(struct assembly_entry *assembly, HKEY key, struct registrykv_entry *registrykv, BOOL dryrun)
  607. {
  608. DWORD value_size;
  609. WCHAR *value = parse_multisz(registrykv->value, &value_size);
  610. BOOL ret = TRUE;
  611. if (registrykv->value && registrykv->value[0] && !value)
  612. return FALSE;
  613. if (!dryrun && RegSetValueExW(key, registrykv->name, 0, REG_MULTI_SZ, (void *)value, value_size))
  614. {
  615. ERR("Failed to set registry key %s\n", debugstr_w(registrykv->name));
  616. ret = FALSE;
  617. }
  618. free(value);
  619. return ret;
  620. }
  621. static BOOL install_registry_dword(struct assembly_entry *assembly, HKEY key, struct registrykv_entry *registrykv, BOOL dryrun)
  622. {
  623. DWORD value = registrykv->value_type ? wcstoul(registrykv->value_type, NULL, 16) : 0;
  624. BOOL ret = TRUE;
  625. if (!dryrun && RegSetValueExW(key, registrykv->name, 0, REG_DWORD, (void *)&value, sizeof(value)))
  626. {
  627. ERR("Failed to set registry key %s\n", debugstr_w(registrykv->name));
  628. ret = FALSE;
  629. }
  630. return ret;
  631. }
  632. static BYTE *parse_hex(const WCHAR *input, DWORD *size)
  633. {
  634. WCHAR number[3] = {0, 0, 0};
  635. BYTE *output, *p;
  636. int length;
  637. *size = 0;
  638. if (!input) return NULL;
  639. length = lstrlenW(input);
  640. if (length & 1) return NULL;
  641. length >>= 1;
  642. if (!(output = malloc(length))) return NULL;
  643. for (p = output; *input; input += 2)
  644. {
  645. number[0] = input[0];
  646. number[1] = input[1];
  647. *p++ = wcstoul(number, 0, 16);
  648. }
  649. *size = length;
  650. return output;
  651. }
  652. static BOOL install_registry_binary(struct assembly_entry *assembly, HKEY key, struct registrykv_entry *registrykv, BOOL dryrun)
  653. {
  654. DWORD value_size;
  655. BYTE *value = parse_hex(registrykv->value, &value_size);
  656. BOOL ret = TRUE;
  657. if (registrykv->value && !value)
  658. return FALSE;
  659. if (!dryrun && RegSetValueExW(key, registrykv->name, 0, REG_BINARY, value, value_size))
  660. {
  661. ERR("Failed to set registry key %s\n", debugstr_w(registrykv->name));
  662. ret = FALSE;
  663. }
  664. free(value);
  665. return ret;
  666. }
  667. static BOOL install_registry_value(struct assembly_entry *assembly, HKEY key, struct registrykv_entry *registrykv, BOOL dryrun)
  668. {
  669. TRACE("Setting registry key %s = %s\n", debugstr_w(registrykv->name), debugstr_w(registrykv->value));
  670. if (!wcscmp(registrykv->value_type, L"REG_SZ"))
  671. return install_registry_string(assembly, key, registrykv, REG_SZ, dryrun);
  672. if (!wcscmp(registrykv->value_type, L"REG_EXPAND_SZ"))
  673. return install_registry_string(assembly, key, registrykv, REG_EXPAND_SZ, dryrun);
  674. if (!wcscmp(registrykv->value_type, L"REG_MULTI_SZ"))
  675. return install_registry_multisz(assembly, key, registrykv, dryrun);
  676. if (!wcscmp(registrykv->value_type, L"REG_DWORD"))
  677. return install_registry_dword(assembly, key, registrykv, dryrun);
  678. if (!wcscmp(registrykv->value_type, L"REG_BINARY"))
  679. return install_registry_binary(assembly, key, registrykv, dryrun);
  680. FIXME("Unsupported registry value type %s\n", debugstr_w(registrykv->value_type));
  681. return FALSE;
  682. }
  683. static BOOL install_registry(struct assembly_entry *assembly, BOOL dryrun)
  684. {
  685. struct registryop_entry *registryop;
  686. struct registrykv_entry *registrykv;
  687. HKEY root, subkey;
  688. WCHAR *path;
  689. REGSAM sam = KEY_ALL_ACCESS;
  690. BOOL ret = TRUE;
  691. #ifdef __x86_64__
  692. if (!wcscmp(assembly->identity.architecture, L"x86")) sam |= KEY_WOW64_32KEY;
  693. #endif
  694. LIST_FOR_EACH_ENTRY(registryop, &assembly->registryops, struct registryop_entry, entry)
  695. {
  696. if (!(path = split_registry_key(registryop->key, &root)))
  697. {
  698. ret = FALSE;
  699. break;
  700. }
  701. TRACE("Processing registry key %s\n", debugstr_w(registryop->key));
  702. if (!dryrun && RegCreateKeyExW(root, path, 0, NULL, 0, sam, NULL, &subkey, NULL))
  703. {
  704. ERR("Failed to open registry key %s\n", debugstr_w(registryop->key));
  705. ret = FALSE;
  706. break;
  707. }
  708. LIST_FOR_EACH_ENTRY(registrykv, &registryop->keyvalues, struct registrykv_entry, entry)
  709. {
  710. if (!(ret = install_registry_value(assembly, subkey, registrykv, dryrun))) break;
  711. }
  712. if (!dryrun) RegCloseKey(subkey);
  713. if (!ret) break;
  714. }
  715. return ret;
  716. }
  717. static BOOL install_assembly(struct list *manifest_list, struct assembly_identity *identity, BOOL dryrun)
  718. {
  719. struct dependency_entry *dependency;
  720. struct assembly_entry *assembly;
  721. const WCHAR *name;
  722. if (!(assembly = lookup_assembly(manifest_list, identity)))
  723. {
  724. FIXME("Assembly %s not found\n", debugstr_w(identity->name));
  725. return FALSE;
  726. }
  727. name = assembly->identity.name;
  728. if (assembly->status == ASSEMBLY_STATUS_INSTALLED)
  729. {
  730. TRACE("Assembly %s already installed\n", debugstr_w(name));
  731. return TRUE;
  732. }
  733. if (assembly->status == ASSEMBLY_STATUS_IN_PROGRESS)
  734. {
  735. ERR("Assembly %s caused circular dependency\n", debugstr_w(name));
  736. return FALSE;
  737. }
  738. #ifdef __i386__
  739. if (!wcscmp(assembly->identity.architecture, L"amd64"))
  740. {
  741. ERR("Cannot install amd64 assembly in 32-bit prefix\n");
  742. return FALSE;
  743. }
  744. #endif
  745. assembly->status = ASSEMBLY_STATUS_IN_PROGRESS;
  746. LIST_FOR_EACH_ENTRY(dependency, &assembly->dependencies, struct dependency_entry, entry)
  747. {
  748. if (!install_assembly(manifest_list, &dependency->identity, dryrun)) return FALSE;
  749. }
  750. TRACE("Installing assembly %s%s\n", debugstr_w(name), dryrun ? " (dryrun)" : "");
  751. if (!install_files(assembly, dryrun))
  752. {
  753. ERR("Failed to install all files for %s\n", debugstr_w(name));
  754. return FALSE;
  755. }
  756. if (!install_registry(assembly, dryrun))
  757. {
  758. ERR("Failed to install registry keys for %s\n", debugstr_w(name));
  759. return FALSE;
  760. }
  761. TRACE("Installation of %s finished\n", debugstr_w(name));
  762. assembly->status = ASSEMBLY_STATUS_INSTALLED;
  763. return TRUE;
  764. }
  765. static BOOL install_updates(struct installer_state *state, BOOL dryrun)
  766. {
  767. struct dependency_entry *dependency;
  768. LIST_FOR_EACH_ENTRY(dependency, &state->updates, struct dependency_entry, entry)
  769. {
  770. if (!install_assembly(&state->assemblies, &dependency->identity, dryrun))
  771. {
  772. ERR("Failed to install update %s\n", debugstr_w(dependency->identity.name));
  773. return FALSE;
  774. }
  775. }
  776. return TRUE;
  777. }
  778. static void set_assembly_status(struct list *manifest_list, DWORD status)
  779. {
  780. struct assembly_entry *assembly;
  781. LIST_FOR_EACH_ENTRY(assembly, manifest_list, struct assembly_entry, entry)
  782. {
  783. assembly->status = status;
  784. }
  785. }
  786. static BOOL install_msu(const WCHAR *filename, struct installer_state *state)
  787. {
  788. const WCHAR *temp_path;
  789. WIN32_FIND_DATAW data;
  790. HANDLE search;
  791. WCHAR *path;
  792. BOOL ret = FALSE;
  793. list_init(&state->tempdirs);
  794. list_init(&state->assemblies);
  795. list_init(&state->updates);
  796. CoInitialize(NULL);
  797. TRACE("Processing msu file %s\n", debugstr_w(filename));
  798. if (!(temp_path = create_temp_directory(state))) return FALSE;
  799. if (!extract_cabinet(filename, temp_path))
  800. {
  801. ERR("Failed to extract %s\n", debugstr_w(filename));
  802. goto done;
  803. }
  804. /* load all manifests from contained cabinet archives */
  805. if (!(path = path_combine(temp_path, L"*.cab"))) goto done;
  806. search = FindFirstFileW(path, &data);
  807. free(path);
  808. if (search != INVALID_HANDLE_VALUE)
  809. {
  810. do
  811. {
  812. if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
  813. if (!wcsicmp(data.cFileName, L"WSUSSCAN.cab")) continue;
  814. if (!(path = path_combine(temp_path, data.cFileName))) continue;
  815. if (!load_assemblies_from_cab(path, state))
  816. ERR("Failed to load all manifests from %s, ignoring\n", debugstr_w(path));
  817. free(path);
  818. }
  819. while (FindNextFileW(search, &data));
  820. FindClose(search);
  821. }
  822. /* load all update descriptions */
  823. if (!(path = path_combine(temp_path, L"*.xml"))) goto done;
  824. search = FindFirstFileW(path, &data);
  825. free(path);
  826. if (search != INVALID_HANDLE_VALUE)
  827. {
  828. do
  829. {
  830. if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
  831. if (!(path = path_combine(temp_path, data.cFileName))) continue;
  832. if (!load_update(path, &state->updates))
  833. ERR("Failed to load all updates from %s, ignoring\n", debugstr_w(path));
  834. free(path);
  835. }
  836. while (FindNextFileW(search, &data));
  837. FindClose(search);
  838. }
  839. /* dump package information (for debugging) */
  840. if (TRACE_ON(wusa))
  841. {
  842. struct dependency_entry *dependency;
  843. struct assembly_entry *assembly;
  844. TRACE("List of updates:\n");
  845. LIST_FOR_EACH_ENTRY(dependency, &state->updates, struct dependency_entry, entry)
  846. TRACE(" * %s\n", debugstr_w(dependency->identity.name));
  847. TRACE("List of manifests (with dependencies):\n");
  848. LIST_FOR_EACH_ENTRY(assembly, &state->assemblies, struct assembly_entry, entry)
  849. {
  850. TRACE(" * %s\n", debugstr_w(assembly->identity.name));
  851. LIST_FOR_EACH_ENTRY(dependency, &assembly->dependencies, struct dependency_entry, entry)
  852. TRACE(" -> %s\n", debugstr_w(dependency->identity.name));
  853. }
  854. }
  855. if (list_empty(&state->updates))
  856. {
  857. ERR("No updates found, probably incompatible MSU file format?\n");
  858. goto done;
  859. }
  860. /* perform dry run */
  861. set_assembly_status(&state->assemblies, ASSEMBLY_STATUS_NONE);
  862. if (!install_updates(state, TRUE))
  863. {
  864. ERR("Dry run failed, aborting installation\n");
  865. goto done;
  866. }
  867. /* installation */
  868. set_assembly_status(&state->assemblies, ASSEMBLY_STATUS_NONE);
  869. if (!install_updates(state, FALSE))
  870. {
  871. ERR("Installation failed\n");
  872. goto done;
  873. }
  874. TRACE("Installation finished\n");
  875. ret = TRUE;
  876. done:
  877. installer_cleanup(state);
  878. return ret;
  879. }
  880. static void restart_as_x86_64(void)
  881. {
  882. WCHAR filename[MAX_PATH];
  883. PROCESS_INFORMATION pi;
  884. STARTUPINFOW si;
  885. DWORD exit_code = 1;
  886. void *redir;
  887. memset(&si, 0, sizeof(si));
  888. si.cb = sizeof(si);
  889. GetSystemDirectoryW( filename, MAX_PATH );
  890. wcscat( filename, L"\\wusa.exe" );
  891. Wow64DisableWow64FsRedirection(&redir);
  892. if (CreateProcessW(filename, GetCommandLineW(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
  893. {
  894. TRACE("Restarting %s\n", wine_dbgstr_w(filename));
  895. WaitForSingleObject(pi.hProcess, INFINITE);
  896. GetExitCodeProcess(pi.hProcess, &exit_code);
  897. CloseHandle(pi.hProcess);
  898. CloseHandle(pi.hThread);
  899. }
  900. else ERR("Failed to restart 64-bit %s, err %lu\n", wine_dbgstr_w(filename), GetLastError());
  901. Wow64RevertWow64FsRedirection(redir);
  902. ExitProcess(exit_code);
  903. }
  904. int __cdecl wmain(int argc, WCHAR *argv[])
  905. {
  906. struct installer_state state;
  907. const WCHAR *filename = NULL;
  908. BOOL is_wow64;
  909. int i;
  910. if (IsWow64Process( GetCurrentProcess(), &is_wow64 ) && is_wow64) restart_as_x86_64();
  911. state.norestart = FALSE;
  912. state.quiet = FALSE;
  913. if (TRACE_ON(wusa))
  914. {
  915. TRACE("Command line:");
  916. for (i = 0; i < argc; i++)
  917. TRACE(" %s", wine_dbgstr_w(argv[i]));
  918. TRACE("\n");
  919. }
  920. for (i = 1; i < argc; i++)
  921. {
  922. if (argv[i][0] == '/')
  923. {
  924. if (!wcscmp(argv[i], L"/norestart"))
  925. state.norestart = TRUE;
  926. else if (!wcscmp(argv[i], L"/quiet"))
  927. state.quiet = TRUE;
  928. else
  929. FIXME("Unknown option: %s\n", wine_dbgstr_w(argv[i]));
  930. }
  931. else if (!filename)
  932. filename = argv[i];
  933. else
  934. FIXME("Unknown option: %s\n", wine_dbgstr_w(argv[i]));
  935. }
  936. if (!filename)
  937. {
  938. FIXME("Missing filename argument\n");
  939. return 1;
  940. }
  941. return !install_msu(filename, &state);
  942. }