Debian Codegraph @

plugins/wcx/unrar/src/rarfunc.pas

raw
1 {
2 Double commander
3 -------------------------------------------------------------------------
4 Wcx plugin for packing RAR archives
5
6 Copyright (C) 2015-2023 Alexander Koblov ([email protected])
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 }
22
23 unit RarFunc;
24
25 {$mode objfpc}{$H+}
26 {$include calling.inc}
27
28 interface
29
30 uses
31 Classes, SysUtils, WcxPlugin;
32
33 procedure PackSetDefaultParams(dps: PPackDefaultParamStruct); dcpcall; export;
34 procedure ConfigurePacker(Parent: HWND; DllInstance: THandle); dcpcall; export;
35 function DeleteFilesW(PackedFile, DeleteList: PWideChar): Integer; dcpcall; export;
36 function PackFilesW(PackedFile: PWideChar; SubPath: PWideChar; SrcPath: PWideChar; AddList: PWideChar; Flags: Integer): Integer; dcpcall; export;
37
38 var
39 IniFileName: String;
40
41 implementation
42
43 uses
44 Process, LazUTF8, DCConvertEncoding, DCProcessUtf8, DCOSUtils, UnRARFunc,
45 RarConfDlg, RarLng, Extension;
46
47 const
48 UTF16LEBOM: WideChar = #$FEFF;
49 LineEndingW = WideChar(#13) + WideChar(#10);
50
51 function RarToWcx(Error: Integer): Integer;
52 begin
53 case Error of
54 0: Result:= E_SUCCESS; // Successful operation
55 1: Result:= E_SUCCESS; // Warning. Non fatal error(s) occurred
56 2: Result:= E_BAD_ARCHIVE; // A fatal error occurred
57 3: Result:= E_BAD_DATA; // Invalid checksum. Data is damaged
58 4: Result:= E_EOPEN; // Attempt to modify a locked archive
59 5: Result:= E_EWRITE; // Write error
60 6: Result:= E_EOPEN; // File open error
61 7: Result:= E_NOT_SUPPORTED; // Wrong command line option
62 8: Result:= E_NO_MEMORY; // Not enough memory
63 9: Result:= E_ECREATE; // File create error
64 10: Result:= E_BAD_DATA; // No files matching the specified mask and options were found
65 11: Result:= E_BAD_DATA; // Wrong password
66 12: Result:= E_EREAD; // Read error
67 255: Result:= E_EABORTED; // User break
68 else Result:= E_UNKNOWN; // Unknown
69 end;
70 end;
71
72 function RarExists(const FileName: String): Boolean;
73 var
74 Message: String;
75 begin
76 Result:= mbFileExists(FileName);
77 if not Result then
78 begin
79 Message:= Format(rsMsgExecutableNotFound, [FileName]);
80 gStartupInfo.MessageBox(PAnsiChar(Message), nil, MB_OK or MB_ICONERROR);
81 end;
82 end;
83
84 function ExecuteRar(Process: TProcessUtf8; FileList : UnicodeString): Integer;
85 var
86 TempFile: THandle;
87 S, FileName: String;
88 Percent: Integer = 0;
89 begin
90 FileName:= GetTempName('');
91 TempFile:= mbFileCreate(FileName);
92 if (TempFile = feInvalidHandle) then Exit(E_ECREATE);
93 try
94 FileWrite(TempFile, FileList[1], Length(FileList) * SizeOf(WideChar));
95 FileClose(TempFile);
96
97 Process.Parameters.Add('@' + FileName);
98
99 Process.Execute;
100
101 if poUsePipes in Process.Options then
102 begin
103 S:= EmptyStr;
104 SetLength(FileName, MAX_PATH);
105 while Process.Running do
106 begin
107 if Process.Output.NumBytesAvailable = 0 then
108 Sleep(100)
109 else begin
110 SetLength(FileName, Process.Output.Read(FileName[1], Length(FileName)));
111 S+= FileName;
112 Result:= Pos('%', S);
113 if Result > 0 then
114 begin
115 TempFile:= Result - 1;
116 while S[TempFile] in ['0'..'9'] do
117 Dec(TempFile);
118 if (Result - TempFile) > 1 then
119 begin
120 Percent:= StrToIntDef(Copy(S, TempFile + 1, Result - TempFile - 1), Percent);
121 end;
122 S:= EmptyStr;
123 end;
124 end;
125 if ProcessDataProcW(nil, -(Percent + 1000)) = 0 then
126 begin
127 Process.Terminate(255);
128 Exit(E_EABORTED);
129 end;
130 end;
131 end;
132
133 Process.WaitOnExit;
134
135 Result:= RarToWcx(Process.ExitStatus);
136 finally
137 DeleteFile(FileName);
138 end;
139 end;
140
141 procedure PackSetDefaultParams(dps: PPackDefaultParamStruct); dcpcall; export;
142 begin
143 IniFileName:= CeSysToUtf8(dps^.DefaultIniName);
144
145 LoadConfig;
146 end;
147
148 procedure ConfigurePacker(Parent: HWND; DllInstance: THandle); dcpcall; export;
149 begin
150 CreateRarConfDlg;
151 end;
152
153 function DeleteFilesW(PackedFile, DeleteList: PWideChar): Integer; dcpcall; export;
154 var
155 Rar: String;
156 Process : TProcessUtf8;
157 FileName : UnicodeString;
158 FileList : UnicodeString;
159 FolderName: UnicodeString;
160 begin
161 Rar:= mbExpandEnvironmentStrings(WinRar);
162 if not RarExists(Rar) then Exit(E_HANDLED);
163
164 Process := TProcessUtf8.Create(nil);
165 try
166 Process.Executable:= Rar;
167 Process.Parameters.Add('d');
168 Process.Parameters.Add('-c-');
169 Process.Parameters.Add('-r-');
170 Process.Parameters.Add(CeUtf16ToUtf8(UnicodeString(PackedFile)));
171
172 try
173 // Parse file list
174 FileList:= UTF16LEBOM;
175 while DeleteList^ <> #0 do
176 begin
177 FileName := DeleteList; // Convert PWideChar to UnicodeString (up to first #0).
178
179 FileList += FileName + LineEndingW;
180
181 // If ends with '*' then delete directory
182 if FileName[Length(FileName)] = '*' then
183 begin
184 FolderName:= FileName;
185 Delete(FolderName, Length(FileName) - 3, 4);
186 FileList += FolderName + LineEndingW;
187 end;
188
189 DeleteList := DeleteList + Length(FileName) + 1; // move after filename and ending #0
190 end;
191
192 Result:= ExecuteRar(Process, FileList);
193
194 except
195 Result:= E_EOPEN;
196 end;
197 finally
198 Process.Free;
199 end;
200 end;
201
202 function PackFilesW(PackedFile: PWideChar; SubPath: PWideChar; SrcPath: PWideChar; AddList: PWideChar; Flags: Integer): Integer;dcpcall; export;
203 const
204 {$IF DEFINED(MSWINDOWS)}
205 SFXExt = '.exe';
206 {$ELSE}
207 SFXExt = '.run';
208 {$ENDIF}
209 var
210 Rar: String;
211 Process : TProcessUtf8;
212 FileList: UnicodeString;
213 FileName: UnicodeString;
214 FolderName: UnicodeString;
215 Password: array[0..MAX_PATH] of AnsiChar;
216 begin
217 Rar:= mbExpandEnvironmentStrings(WinRar);
218 if not RarExists(Rar) then Exit(E_HANDLED);
219
220 Process := TProcessUtf8.Create(nil);
221 try
222 Process.Executable:= Rar;
223
224 if FileIsConsoleExe(Process.Executable) then begin
225 Process.Options:= [poUsePipes, poNoConsole, poNewProcessGroup];
226 end;
227
228 if (Flags and PK_PACK_MOVE_FILES <> 0) then
229 Process.Parameters.Add('m')
230 else begin
231 Process.Parameters.Add('a');
232 end;
233 Process.Parameters.Add('-c-');
234 Process.Parameters.Add('-r-');
235 // Create solid archive
236 if Solid then Process.Parameters.Add('-s');
237 // Compression method
238 Process.Parameters.Add('-m' + IntToStr(Method));
239
240 if SameStr(ExtractFileExt(CeUtf16ToUtf8(UnicodeString(PackedFile))), SFXExt) then
241 Process.Parameters.Add('-sfx');
242
243 // Add user command line parameters
244 if Length(Args) > 0 then CommandToList(Args, Process.Parameters);
245
246 // Add data recovery record
247 if Recovery and (Pos('-rr', Args) = 0) then
248 Process.Parameters.Add('-rr3p');
249
250 // Encrypt archive
251 if (Flags and PK_PACK_ENCRYPT <> 0) then
252 begin
253 FillChar(Password, SizeOf(Password), #0);
254 if gStartupInfo.InputBox('Rar', PAnsiChar(rsMsgPasswordEnter), True, PAnsiChar(Password), MAX_PATH) then
255 begin
256 if Encrypt then
257 Process.Parameters.Add('-hp' + Password)
258 else begin
259 Process.Parameters.Add('-p' + Password);
260 end;
261 end
262 else begin
263 Exit(E_EABORTED);
264 end;
265 end;
266
267 // Destination path
268 if Assigned(SubPath) then
269 begin
270 Process.Parameters.Add('-ap' + CeUtf16ToUtf8(UnicodeString(SubPath)));
271 end;
272
273 Process.Parameters.Add(CeUtf16ToUtf8(UnicodeString(PackedFile)));
274
275 // Source path
276 if Assigned(SrcPath) then
277 begin
278 Process.CurrentDirectory:= CeUtf16ToUtf8(UnicodeString(SrcPath));
279 end;
280
281 try
282 // Parse file list
283 FileList:= UTF16LEBOM;
284 while AddList^ <> #0 do
285 begin
286 FileName := UnicodeString(AddList);
287
288 FileList += FileName + LineEndingW;
289
290 // If ends with '/' then add directory
291 if FileName[Length(FileName)] = PathDelim then
292 begin
293 FolderName:= FileName;
294 Delete(FolderName, Length(FileName), 1);
295 FileList += FolderName + LineEndingW;
296 end;
297
298 Inc(AddList, Length(FileName) + 1);
299 end;
300
301 Result:= ExecuteRar(Process, FileList);
302
303 except
304 Result:= E_EOPEN;
305 end;
306 finally
307 Process.Free;
308 end;
309 end;
310
311 end.
312