[RAPPS] Add directory extraction capabilities for the .cab file
authorMark Jansen <mark.jansen@reactos.org>
Sun, 6 Sep 2020 21:14:11 +0000 (23:14 +0200)
committerMark Jansen <mark.jansen@reactos.org>
Sun, 11 Oct 2020 15:01:05 +0000 (17:01 +0200)
base/applications/rapps/cabinet.cpp

index e10d64f..8b4d8a1 100644 (file)
@@ -5,6 +5,7 @@
  * COPYRIGHT:   Copyright 2018 Alexander Shaposhnikov     (sanchaez@reactos.org)            
  */
 #include "rapps.h"
+#include <debug.h>
 
 #include <fdi.h>
 #include <fcntl.h>
@@ -178,18 +179,42 @@ FNFDINOTIFY(fnNotify)
     {
     case fdintCOPY_FILE:
     {
-        ATL::CStringW szNewFileName, szExtractDir, szCabFileName;
-        ATL::CStringA szFilePathUTF8;
+        CStringW szExtractDir, szCabFileName;
 
         // Append the destination directory to the file name.
         MultiByteToWide((LPCSTR) pfdin->pv, szExtractDir, CP_UTF8);
         MultiByteToWide(pfdin->psz1, szCabFileName, CP_ACP);
 
-        szNewFileName = szExtractDir + L"\\" + szCabFileName;
+        if (szCabFileName.Find('\\') >= 0)
+        {
+            CStringW szNewDirName = szExtractDir;
+            int nTokenPos = 0;
+            // We do not want to interpret the filename as directory,
+            // so bail out before the last token!
+            while (szCabFileName.Find('\\', nTokenPos) >= 0)
+            {
+                CStringW token = szCabFileName.Tokenize(L"\\", nTokenPos);
+                if (token.IsEmpty())
+                    break;
+
+                szNewDirName += L"\\" + token;
+                if (!CreateDirectoryW(szNewDirName, NULL))
+                {
+                    DWORD dwErr = GetLastError();
+                    if (dwErr != ERROR_ALREADY_EXISTS)
+                    {
+                        DPRINT1("ERROR: Unable to create directory %S (err %lu)\n", szNewDirName.GetString(), dwErr);
+                    }
+                }
+            }
+        }
+
+        CStringW szNewFileName = szExtractDir + L"\\" + szCabFileName;
 
+        CStringA szFilePathUTF8;
         WideToMultiByte(szNewFileName, szFilePathUTF8, CP_UTF8);
 
-        // Copy file
+        // Open the file
         iResult = fnFileOpen((LPSTR) szFilePathUTF8.GetString(), 
                              _O_WRONLY | _O_CREAT,
                              0);