[PATCH 2/4] wusa: add extraction of cabinet files

Vijay Kiran Kamuju infyquest at gmail.com
Thu Mar 22 09:28:08 CDT 2018


From: Michael Müller <michael at fds-team.de>

Signed-off-by: Vijay Kiran Kamuju <infyquest at gmail.com>
---
 programs/wusa/Makefile.in |   1 +
 programs/wusa/main.c      | 242 +++++++++++++++++++++++++++++++++++++++++++++-
 programs/wusa/wusa.h      |  56 +++++++++++
 3 files changed, 295 insertions(+), 4 deletions(-)
 create mode 100644 programs/wusa/wusa.h

diff --git a/programs/wusa/Makefile.in b/programs/wusa/Makefile.in
index 5068456a20b..75e41295aea 100644
--- a/programs/wusa/Makefile.in
+++ b/programs/wusa/Makefile.in
@@ -1,5 +1,6 @@
 MODULE    = wusa.exe
 APPMODE   = -mconsole -municode
+IMPORTS   = cabinet shlwapi
 
 C_SRCS = \
 	main.c
diff --git a/programs/wusa/main.c b/programs/wusa/main.c
index 77dd46d8bd4..7a289315a4f 100644
--- a/programs/wusa/main.c
+++ b/programs/wusa/main.c
@@ -18,13 +18,31 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
+#include <shlwapi.h>
+#include <fdi.h>
+#include "wusa.h"
 #include "wine/debug.h"
-#include "wine/heap.h"
 #include "wine/list.h"
-#include "wine/unicode.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(wusa);
 
+/* from msvcrt/fcntl.h */
+#define _O_RDONLY      0
+#define _O_WRONLY      1
+#define _O_RDWR        2
+#define _O_ACCMODE     (_O_RDONLY|_O_WRONLY|_O_RDWR)
+#define _O_APPEND      0x0008
+#define _O_RANDOM      0x0010
+#define _O_SEQUENTIAL  0x0020
+#define _O_TEMPORARY   0x0040
+#define _O_NOINHERIT   0x0080
+#define _O_CREAT       0x0100
+#define _O_TRUNC       0x0200
+#define _O_EXCL        0x0400
+#define _O_SHORT_LIVED 0x1000
+#define _O_TEXT        0x4000
+#define _O_BINARY      0x8000
+
 struct installer_tempdir {
    struct list entry;
    WCHAR *path;
@@ -55,6 +73,54 @@ static const WCHAR *path_combine(const WCHAR *path, const WCHAR *filename)
     return result;
 }
 
+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 BOOL delete_directory(const WCHAR *path)
 {
     static const WCHAR starW[] = {'*',0};
@@ -91,6 +157,168 @@ static BOOL delete_directory(const WCHAR *path)
     return RemoveDirectoryW(full_path);
 }
 
+static WCHAR *get_uncompressed_path(PFDINOTIFICATION pfdin)
+{
+    WCHAR *file = strdupAtoW(pfdin->psz1);
+    WCHAR *path = (WCHAR *)path_combine(pfdin->pv, file);
+    heap_free(file);
+    return path;
+}
+
+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 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;
+
+    WINE_TRACE("extracting %s -> %s\n", wine_dbgstr_a(pfdin->psz1), wine_dbgstr_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:
+        WINE_FIXME("fdintPARTIAL_FILE not implemented\n");
+        return 0;
+
+    case fdintNEXT_CABINET:
+        WINE_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 const WCHAR msuW[] = {'m','s','u',0};
@@ -140,15 +368,21 @@ static void installer_cleanup(struct installer_state *state)
 static BOOL install_msu(WCHAR *filename, struct installer_state *state)
 {
     const WCHAR *temp_path;
+    BOOL ret = FALSE;
 
     list_init(&state->tempdirs);
 
     WINE_TRACE("Processing msu file %s\n", wine_dbgstr_w(filename));
     if (!(temp_path = create_temp_directory(state)))
-        return FALSE;
+        return ret;
+
+    if (!extract_cabinet(filename, temp_path))
+        WINE_ERR("Failed to extract %s\n", wine_dbgstr_w(filename));
+    else
+        ret = TRUE;
 
     installer_cleanup(state);
-    return TRUE;
+    return ret;
 }
 
 int wmain(int argc, WCHAR *argv[])
diff --git a/programs/wusa/wusa.h b/programs/wusa/wusa.h
new file mode 100644
index 00000000000..e9da7372678
--- /dev/null
+++ b/programs/wusa/wusa.h
@@ -0,0 +1,56 @@
+/*
+ * 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
+ */
+
+#include <windows.h>
+#include "wine/heap.h"
+#include "wine/unicode.h"
+
+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((strlenW(str) + 1) * sizeof(WCHAR));
+    if (ret)
+        strcpyW(ret, str);
+    return ret;
+}
-- 
2.16.2




More information about the wine-devel mailing list