Browse Source

Add support for overwriting read-only files.

Martijn Laan 3 months ago
parent
commit
64d20b80f3

+ 10 - 1
Components/Lzma2/Util/7z/7zMain.c

@@ -9,6 +9,7 @@
    -Return res on errors instead of always returning 1
    -Add optional progress reporting with abort option
    -Add optional output of SzArEx_Extract's output buffer sizes
+   -Add support for overwriting read-only files
    Otherwise unchanged */
 
 #include "Precomp.h"
@@ -840,6 +841,14 @@ int Z7_CDECL mainW(int numargs, WCHAR *args[])
           }
           else
           {
+            #ifdef USE_WINDOWS_FILE
+            {
+              const UInt32 existingattrib = GetFileAttributesW((LPCWSTR)destPath);
+              if (existingattrib != INVALID_FILE_ATTRIBUTES && (existingattrib & FILE_ATTRIBUTE_READONLY))
+                SetFileAttributesW((LPCWSTR)destPath, existingattrib & ~FILE_ATTRIBUTE_READONLY);
+            }
+            #endif
+
             const WRes wres = OutFile_OpenUtf16(&outFile, destPath);
             if (wres != 0)
             {
@@ -850,7 +859,7 @@ int Z7_CDECL mainW(int numargs, WCHAR *args[])
           }
 
           processedSize = outSizeProcessed;
-          
+
           {
             const WRes wres = File_Write(&outFile, outBuffer + offset, &processedSize);
             if (wres != 0 || processedSize != outSizeProcessed)

+ 12 - 0
Projects/Src/Compression.SevenZipDecoder.pas

@@ -113,6 +113,18 @@ begin
   Result := ReadFile(hFile, Buffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped);
 end;
 
+function __GetFileAttributesW(lpFileName: LPCWSTR): DWORD; cdecl;
+begin
+  { See above }
+  var ExpandedFileName: String;
+  if ValidateAndCombinePath(State.ExpandedDestDir, lpFileName, ExpandedFileName) then
+    Result := GetFileAttributesRedir(State.DisableFsRedir, ExpandedFileName)
+  else begin
+    Result := INVALID_FILE_ATTRIBUTES;
+    SetLastError(ERROR_ACCESS_DENIED);
+  end;
+end;
+
 function __SetFileAttributesW(lpFileName: LPCWSTR; dwFileAttributes: DWORD): BOOL; cdecl;
 begin
   { See above }

+ 3 - 0
Projects/Src/Compression.SevenZipDecoder/7zDecode/IS7zDec.c

@@ -39,6 +39,9 @@ DWORD _GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh);
 BOOL _ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped);
 #define ReadFile _ReadFile
 
+DWORD _GetFileAttributesW(LPCWSTR lpFileName);
+#define GetFileAttributesW _GetFileAttributesW
+
 BOOL _SetFileAttributesW(LPCWSTR lpFileName, DWORD dwFileAttributes);
 #define SetFileAttributesW _SetFileAttributesW
 

BIN
Projects/Src/Compression.SevenZipDecoder/7zDecode/IS7zDec.obj


+ 4 - 4
Projects/Src/Compression.SevenZipDecoder/7zDecode/IS7zDec.obj.issig

@@ -1,6 +1,6 @@
 format issig-v1
-file-size 93601
-file-hash aad21cad7686f881b461c5727995391a00b48df251893a97e2cb0f9567c10a00
+file-size 93692
+file-hash 3984b8b947779519be78c34858a3bd0f5eb75cd166fd5d43102af518bf35d4f7
 key-id def0147c3bbc17ab99bf7b7a9c2de1390283f38972152418d7c2a4a7d7131a38
-sig-r 72598c2e35dc647f0da0c714648ffb30271e6c4e96816aa651fd7d52b584b1e5
-sig-s db13c937f9bce1bf736b57cf62c30d836ae8604bf11a66f69443a6163864dfea
+sig-r ba2f9ec8f07c76da83c9fb175f82f939e712865069783c151775ba53d0dd00c7
+sig-s 1e56679de72a7ec1493aed723ee77ec0df4ddc469d0505a503d7641850395257