wusa: Add support for extracting MSU files.

Based on patches by Michael Müller and Sebastian Lackner.

Signed-off-by: Hans Leidekker <hans@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
stable
Hans Leidekker 2019-12-02 10:11:49 +01:00 committed by Alexandre Julliard
parent b276bd58a7
commit 9934b25313
3 changed files with 428 additions and 6 deletions

View File

@ -1,4 +1,5 @@
MODULE = wusa.exe
IMPORTS = cabinet shlwapi
EXTRADLLFLAGS = -mconsole -municode -mno-cygwin

View File

@ -1,5 +1,7 @@
/*
* Copyright 2012 Austin English
* Copyright 2015 Michael Müller
* Copyright 2015 Sebastian Lackner
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -16,18 +18,375 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <windows.h>
#include <fcntl.h>
#include <fdi.h>
#include <shlwapi.h>
#include "wine/debug.h"
#include "wine/list.h"
#include "wusa.h"
WINE_DEFAULT_DEBUG_CHANNEL(wusa);
int __cdecl wmain(int argc, WCHAR *argv[])
struct installer_tempdir
{
int i;
struct list entry;
WCHAR *path;
};
WINE_FIXME("stub:");
for (i = 0; i < argc; i++)
WINE_FIXME(" %s", wine_dbgstr_w(argv[i]));
WINE_FIXME("\n");
struct installer_state
{
BOOL norestart;
BOOL quiet;
struct list tempdirs;
};
static void * CDECL cabinet_alloc(ULONG cb)
{
return heap_alloc(cb);
}
static void CDECL cabinet_free(void *pv)
{
heap_free(pv);
}
static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
{
DWORD dwAccess = 0;
DWORD dwShareMode = 0;
DWORD dwCreateDisposition = OPEN_EXISTING;
switch (oflag & _O_ACCMODE)
{
case _O_RDONLY:
dwAccess = GENERIC_READ;
dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
break;
case _O_WRONLY:
dwAccess = GENERIC_WRITE;
dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
break;
case _O_RDWR:
dwAccess = GENERIC_READ | GENERIC_WRITE;
dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
break;
}
if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
dwCreateDisposition = CREATE_NEW;
else if (oflag & _O_CREAT)
dwCreateDisposition = CREATE_ALWAYS;
return (INT_PTR)CreateFileA(pszFile, dwAccess, dwShareMode, NULL, dwCreateDisposition, 0, NULL);
}
static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
{
HANDLE handle = (HANDLE)hf;
DWORD read;
if (ReadFile(handle, pv, cb, &read, NULL))
return read;
return 0;
}
static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
{
HANDLE handle = (HANDLE)hf;
DWORD written;
if (WriteFile(handle, pv, cb, &written, NULL))
return written;
return 0;
}
static int CDECL cabinet_close(INT_PTR hf)
{
HANDLE handle = (HANDLE)hf;
return CloseHandle(handle) ? 0 : -1;
}
static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
{
HANDLE handle = (HANDLE)hf;
/* flags are compatible and so are passed straight through */
return SetFilePointer(handle, dist, NULL, seektype);
}
static WCHAR *path_combine(const WCHAR *path, const WCHAR *filename)
{
WCHAR *result;
DWORD length;
if (!path || !filename) return NULL;
length = lstrlenW(path) + lstrlenW(filename) + 2;
if (!(result = heap_alloc(length * sizeof(WCHAR)))) return NULL;
lstrcpyW(result, path);
if (result[0] && result[lstrlenW(result) - 1] != '\\') lstrcatW(result, L"\\");
lstrcatW(result, filename);
return result;
}
static WCHAR *get_uncompressed_path(PFDINOTIFICATION pfdin)
{
WCHAR *file = strdupAtoW(pfdin->psz1);
WCHAR *path = path_combine(pfdin->pv, file);
heap_free(file);
return path;
}
static BOOL is_directory(const WCHAR *path)
{
DWORD attrs = GetFileAttributesW(path);
if (attrs == INVALID_FILE_ATTRIBUTES) return FALSE;
return (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
static BOOL create_directory(const WCHAR *path)
{
if (is_directory(path)) return TRUE;
if (CreateDirectoryW(path, NULL)) return TRUE;
return (GetLastError() == ERROR_ALREADY_EXISTS);
}
static BOOL create_parent_directory(const WCHAR *filename)
{
WCHAR *p, *path = strdupW(filename);
BOOL ret = FALSE;
if (!path) return FALSE;
if (!PathRemoveFileSpecW(path)) goto done;
if (is_directory(path))
{
ret = TRUE;
goto done;
}
for (p = path; *p; p++)
{
if (*p != '\\') continue;
*p = 0;
if (!create_directory(path)) goto done;
*p = '\\';
}
ret = create_directory(path);
done:
heap_free(path);
return ret;
}
static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
{
HANDLE handle = INVALID_HANDLE_VALUE;
WCHAR *file;
DWORD attrs;
if (!(file = get_uncompressed_path(pfdin)))
return -1;
TRACE("Extracting %s -> %s\n", debugstr_a(pfdin->psz1), debugstr_w(file));
if (create_parent_directory(file))
{
attrs = pfdin->attribs;
if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
handle = CreateFileW(file, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
}
heap_free(file);
return (handle != INVALID_HANDLE_VALUE) ? (INT_PTR)handle : -1;
}
static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
{
HANDLE handle = (HANDLE)pfdin->hf;
CloseHandle(handle);
return 1;
}
static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
{
switch (fdint)
{
case fdintPARTIAL_FILE:
FIXME("fdintPARTIAL_FILE not implemented\n");
return 0;
case fdintNEXT_CABINET:
FIXME("fdintNEXT_CABINET not implemented\n");
return 0;
case fdintCOPY_FILE:
return cabinet_copy_file(fdint, pfdin);
case fdintCLOSE_FILE_INFO:
return cabinet_close_file_info(fdint, pfdin);
default:
return 0;
}
}
static BOOL extract_cabinet(const WCHAR *filename, const WCHAR *destination)
{
char *filenameA = NULL;
BOOL ret = FALSE;
HFDI hfdi;
ERF erf;
hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
if (!hfdi) return FALSE;
if ((filenameA = strdupWtoA(filename)))
{
ret = FDICopy(hfdi, filenameA, NULL, 0, cabinet_notify, NULL, (void *)destination);
heap_free(filenameA);
}
FDIDestroy(hfdi);
return ret;
}
static const WCHAR *create_temp_directory(struct installer_state *state)
{
static UINT id;
struct installer_tempdir *entry;
WCHAR tmp[MAX_PATH];
if (!GetTempPathW(ARRAY_SIZE(tmp), tmp)) return NULL;
if (!(entry = heap_alloc(sizeof(*entry)))) return NULL;
if (!(entry->path = heap_alloc((MAX_PATH + 20) * sizeof(WCHAR))))
{
heap_free(entry);
return NULL;
}
for (;;)
{
if (!GetTempFileNameW(tmp, L"msu", ++id, entry->path))
{
heap_free(entry->path);
heap_free(entry);
return NULL;
}
if (CreateDirectoryW(entry->path, NULL)) break;
}
list_add_tail(&state->tempdirs, &entry->entry);
return entry->path;
}
static BOOL delete_directory(const WCHAR *path)
{
WIN32_FIND_DATAW data;
WCHAR *full_path;
HANDLE search;
if (!(full_path = path_combine(path, L"*"))) return FALSE;
search = FindFirstFileW(full_path, &data);
heap_free(full_path);
if (search != INVALID_HANDLE_VALUE)
{
do
{
if (!wcscmp(data.cFileName, L".")) continue;
if (!wcscmp(data.cFileName, L"..")) continue;
if (!(full_path = path_combine(path, data.cFileName))) continue;
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
delete_directory(full_path);
else
DeleteFileW(full_path);
heap_free(full_path);
}
while (FindNextFileW(search, &data));
FindClose(search);
}
return RemoveDirectoryW(path);
}
static void installer_cleanup(struct installer_state *state)
{
struct installer_tempdir *tempdir, *tempdir2;
LIST_FOR_EACH_ENTRY_SAFE(tempdir, tempdir2, &state->tempdirs, struct installer_tempdir, entry)
{
list_remove(&tempdir->entry);
delete_directory(tempdir->path);
heap_free(tempdir->path);
heap_free(tempdir);
}
}
static BOOL install_msu(const WCHAR *filename, struct installer_state *state)
{
const WCHAR *temp_path;
BOOL ret = FALSE;
list_init(&state->tempdirs);
TRACE("Processing msu file %s\n", debugstr_w(filename));
if (!(temp_path = create_temp_directory(state))) return FALSE;
if (!extract_cabinet(filename, temp_path))
{
ERR("Failed to extract %s\n", debugstr_w(filename));
goto done;
}
ret = TRUE;
done:
installer_cleanup(state);
return ret;
}
int __cdecl wmain(int argc, WCHAR *argv[])
{
struct installer_state state;
const WCHAR *filename = NULL;
int i;
state.norestart = FALSE;
state.quiet = FALSE;
if (TRACE_ON(wusa))
{
TRACE("Command line:");
for (i = 0; i < argc; i++)
TRACE(" %s", wine_dbgstr_w(argv[i]));
TRACE("\n");
}
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '/')
{
if (!wcscmp(argv[i], L"/norestart"))
state.norestart = TRUE;
else if (!wcscmp(argv[i], L"/quiet"))
state.quiet = TRUE;
else
FIXME("Unknown option: %s\n", wine_dbgstr_w(argv[i]));
}
else if (!filename)
filename = argv[i];
else
FIXME("Unknown option: %s\n", wine_dbgstr_w(argv[i]));
}
if (!filename)
{
FIXME("Missing filename argument\n");
return 1;
}
return !install_msu(filename, &state);
}

View File

@ -0,0 +1,62 @@
/*
* Copyright 2015 Michael Müller
* Copyright 2015 Sebastian Lackner
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
static void *heap_alloc(size_t len) __WINE_ALLOC_SIZE(1);
static inline void *heap_alloc(size_t len)
{
return HeapAlloc(GetProcessHeap(), 0, len);
}
static inline BOOL heap_free(void *mem)
{
return HeapFree(GetProcessHeap(), 0, mem);
}
static inline char *strdupWtoA(const WCHAR *str)
{
char *ret = NULL;
DWORD len;
if (!str) return ret;
len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
if ((ret = heap_alloc(len)))
WideCharToMultiByte(CP_ACP, 0, str, -1, ret, len, NULL, NULL);
return ret;
}
static inline WCHAR *strdupAtoW(const char *str)
{
WCHAR *ret = NULL;
DWORD len;
if (!str) return ret;
len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
if ((ret = heap_alloc(len * sizeof(WCHAR))))
MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
return ret;
}
static inline WCHAR *strdupW(const WCHAR *str)
{
WCHAR *ret;
if (!str) return NULL;
ret = heap_alloc((lstrlenW(str) + 1) * sizeof(WCHAR));
if (ret) lstrcpyW(ret, str);
return ret;
}