Debian Codegraph @

plugins/wcx/rpm/src/rpm_archive.pas

raw
1 //***************************************************************
2 // This file is part of RPMWCX, a archiver plugin for
3 // Windows Commander.
4 // Copyright (C) 2000 Mandryka Yurij e-mail:[email protected]
5 //***************************************************************
6 {
7 Add some changes for Lazarus and Linux compability
8 Copyright (C) 2007-2012 Koblov Alexander ([email protected])
9 }
10 //***************************************************************
11 // This code based on Christian Ghisler ([email protected]) sources
12 //***************************************************************
13
14 //***************************************************************
15 // This code was improved by Sergio Daniel Freue ([email protected])
16 //***************************************************************
17
18
19 // History
20 // 2001-02-04 Bug: Error Opening rpm file on CD (readonly)
21 // Fix: Add FileMode = 0 before Reset
22 // Who: Oliver Haeger <[email protected]>
23
24 unit rpm_archive;
25
26 {$mode delphi}{$A-,I-}
27 {$include calling.inc}
28
29 interface
30
31 uses
32 Classes,
33 WcxPlugin,
34 rpm_def, rpm_io;
35
36 type
37 PArchiveRec = ^TArchiveRec;
38 TArchiveRec = record
39 handle_io : THandle;
40 handle_file : file;
41 fname : AnsiString;
42 fdate : Integer;
43 headers : Integer;
44 header : RPM_Header;
45 arch_len : LongWord;
46 process_proc : TProcessDataProc;
47 changevol_proc : TChangeVolProc;
48 //- RPM tags -------------------------------------------
49 info : RPM_InfoRec;
50 deps : RPM_DepsRec;
51 datasig : RPM_DataSig;
52 end;{ArchiveRec}
53
54 function GetPackerCaps : Integer; dcpcall; export;
55 function GetBackgroundFlags: Integer; dcpcall; export;
56 function OpenArchive(var ArchiveData : TOpenArchiveData) : TArcHandle; dcpcall; export;
57 function CloseArchive(hArcData : TArcHandle) : Integer; dcpcall; export;
58 function ReadHeader(hArcData : TArcHandle; var HeaderData : THeaderData) : Integer; dcpcall; export;
59 function ProcessFile(hArcData : TArcHandle; Operation : Integer; DestPath : PChar; DestName : PChar) : Integer; dcpcall; export;
60 procedure SetProcessDataProc(hArcData : TArcHandle; ProcessDataProc : TProcessDataProc); dcpcall; export;
61 procedure SetChangeVolProc(hArcData : TArcHandle; ChangeVolProc : TChangeVolProc); dcpcall; export;
62
63 implementation
64
65 uses
66 SysUtils, DCDateTimeUtils, DCBasicTypes, DCFileAttributes;
67
68 function GetPackerCaps: Integer;
69 begin
70 Result := PK_CAPS_MULTIPLE;
71 end;
72
73 function GetBackgroundFlags: Integer;
74 begin
75 Result := BACKGROUND_UNPACK;
76 end;
77
78 function OpenArchive(var ArchiveData : TOpenArchiveData) : TArcHandle;
79 var
80 arch : THandle;
81 filename : String;
82 r_lead : RPM_Lead;
83 signature : RPM_Header;
84 fgError : Boolean;
85 headerend : integer;
86 arec : PArchiveRec absolute Result;
87 begin
88 arec := nil;
89 arch := 0;
90 fgError := False;
91
92 filename := String(ArchiveData.ArcName);
93 arch := FileOpen(filename, fmOpenRead or fmShareDenyNone);
94 if arch = THandle(-1) then begin
95 fgError := True;
96 end
97 else begin
98 New(arec);
99 with arec^ do begin
100 handle_io := arch;
101 fname := filename;
102 headers := HDR_INFO;
103 arch_len := 0;
104 fdate := FileAge(filename);
105 process_proc := nil;
106 changevol_proc := nil;
107 if fdate = -1 then fdate := 0;
108 end;
109 AssignFile(arec^.handle_file, filename);
110 FileMode := 0;
111 Reset(arec^.handle_file, 1);
112 if IOResult <> 0 then begin
113 fgError := True;
114 end
115 else begin
116 RPM_ReadLead(arec^.handle_file, r_lead);
117 if r_lead.magic <> RPM_MAGIC then fgError := True
118 else begin
119 if not RPM_ReadSignature(arec^.handle_file, r_lead.signature_type, signature) then fgError := True
120 else
121 if not RPM_ReadHeader(arec^.handle_file, False, arec^.header, arec^.info, arec^.deps) then fgError := True
122 else
123 arec^.arch_len := FileSize(arec^.handle_file) - FilePos(arec^.handle_file);
124 if not fgError then begin
125 headerend:=FilePos(arec^.handle_file);
126 BlockRead(arec^.handle_file, arec^.datasig, SizeOf(RPM_DataSig));
127 Seek(arec^.handle_file, headerend);
128 end;
129 end;
130 end;{ioresult}
131 end;{arch = -1}
132 if fgError then begin
133 if arec <> nil then begin
134 CloseFile(arec^.handle_file);
135 Dispose(arec);
136 end;
137 FileClose(arch);
138 Result := 0;
139 ArchiveData.OpenResult := E_EOPEN
140 end;
141 end;
142
143 function CloseArchive(hArcData: TArcHandle): Integer;
144 var
145 arec : PArchiveRec absolute hArcData;
146 begin
147 CloseFile(arec^.handle_file);
148 FileClose(arec^.handle_io);
149 Dispose(arec);
150 Result := E_SUCCESS;
151 end;
152
153 function ReadHeader(hArcData: TArcHandle; var HeaderData: THeaderData): Integer;
154 var
155 arec : PArchiveRec absolute hArcData;
156 begin
157 Result := E_SUCCESS;
158 with HeaderData do
159 begin
160 case arec^.headers of
161 HDR_DATA: begin
162 copy_str2buf(TStrBuf(FileName), get_archivename(arec^.fname,arec^.datasig));
163 PackSize := arec^.arch_len;
164 UnpSize := arec^.arch_len;
165 end;
166 HDR_INFO: begin
167 copy_str2buf(TStrBuf(FileName), 'INFO.TXT');
168 PackSize := -1;
169 UnpSize := -1;
170 end;
171 else
172 Result := E_END_ARCHIVE;
173 end;
174 if Result = E_SUCCESS then
175 begin
176 copy_str2buf(TStrBuf(ArcName), arec^.fname);
177 FileAttr := GENERIC_ATTRIBUTE_FILE;
178 FileTime := UnixFileTimeToWcxTime(TUnixFileTime(arec^.info.buildtime));
179 Inc(arec^.headers);
180 end;
181 end;
182 end;
183
184 function ProcessFile(hArcData: TArcHandle; Operation: Integer; DestPath: PChar; DestName: PChar): Integer;
185 var
186 rpm_file : file;
187 rpm_name : String;
188 index : Integer;
189 buf : Pointer;
190 buf_size : LongWord;
191 fsize : LongWord;
192 fgReadError : Boolean;
193 fgWriteError: Boolean;
194 faborted : Boolean;
195 testonly : Boolean;
196 arec : PArchiveRec absolute hArcData;
197
198 // Helper function to output one line of text to rpm_file
199 function Line(S: AnsiString): Integer;
200 begin
201 Result := 0;
202 if not fgReadError and not fgWriteError then
203 if testonly then
204 Result := Length(S) + 2
205 else
206 begin
207 S := S + #13#10;
208 BlockWrite(rpm_file, S[1], Length(S), Result);
209 if IOResult <> 0 then fgWriteError := True;
210 end;
211 end;
212
213 begin
214 case Operation of
215 // Because rpm archive doesn't contains length of _alone_ attached
216 // gzipped cpio archive, plugin cann't skip or test rpm archive
217 // correctly without extracting archive.
218 PK_SKIP : Result := E_SUCCESS;
219 PK_TEST,
220 PK_EXTRACT : begin
221 testonly:=Operation=PK_TEST;
222 if not testonly then begin
223 rpm_name := String(DestName);
224 AssignFile(rpm_file, rpm_name);
225 Rewrite(rpm_file, 1);
226 end;
227 if not testonly and (IOResult <> 0) then begin
228 Result := E_EWRITE
229 end
230 else begin
231 fgReadError := False;
232 fgWriteError := False;
233 faborted:=false;
234 case (arec^.headers-1) of
235 HDR_DATA: begin
236 fsize := arec^.arch_len;
237 buf_size := 65536;
238 GetMem(buf, buf_size);
239 while not faborted do begin
240 if fsize < buf_size then Break;
241 BlockRead(arec^.handle_file, buf^, buf_size);
242 if IOResult <> 0 then begin
243 fgReadError := True;
244 Break;
245 end;{if IO error}
246 if not testonly then begin
247 BlockWrite(rpm_file, buf^, buf_size);
248 if IOResult <> 0 then begin
249 fgWriteError := True;
250 Break;
251 end;{if IO error}
252 end;
253 Dec(fsize, buf_size);
254 if Assigned(arec^.process_proc) then
255 if arec^.process_proc(nil, buf_size)=0 then
256 faborted:=true;
257 end;{while}
258 if not fgReadError and not fgWriteError and not faborted then begin
259 if fsize <> 0 then begin
260 BlockRead(arec^.handle_file, buf^, fsize);
261 if IOResult <> 0 then fgReadError := True;
262 if not testonly and not fgReadError then begin
263 BlockWrite(rpm_file, buf^, fsize);
264 if IOResult <> 0 then
265 fgWriteError := True;
266 end;
267 if Assigned(arec^.process_proc) then
268 if arec^.process_proc(nil, fsize)=0 then
269 faborted:=true;
270 end;
271 end;
272 Freemem(buf, buf_size);
273 //Other pseudo-files
274 end;
275 HDR_INFO: with arec^.info do begin
276 Line('NAME: ' + name);
277 Line('VERSION: ' + version);
278 Line('RELEASE: ' + release);
279 Line('SUMMARY: ' + summary);
280 Line('DISTRIBUTION: ' + distribution);
281 Line('VENDOR: ' + vendor);
282 Line('LICENSE: ' + license);
283 Line('PACKAGER: ' + packager);
284 Line('GROUP: ' + group);
285 Line('OS: ' + os);
286 Line('ARCH: ' + arch);
287 Line('SOURCE RPM: ' + sourcerpm);
288 Line('DESCRIPTION: ');
289 Line(description);
290 if Length(arec^.deps.names) > 0 then
291 begin
292 Line(EmptyStr);
293 Line('REQUIRES: ');
294 for index:= 0 to High(arec^.deps.names) do
295 begin
296 Line(' ' + arec^.deps.names[index]);
297 end;
298 end;
299 end;
300 end;
301 if faborted then Result:=E_EABORTED
302 else if fgReadError then Result := E_BAD_DATA
303 else if fgWriteError then Result:= E_EWRITE
304 else
305 Result := E_SUCCESS;
306 if not testonly then begin
307 if result = E_SUCCESS then
308 FileSetDate(tfilerec(rpm_file).handle, UnixFileTimeToWcxTime(TUnixFileTime(arec^.info.buildtime)));
309 CloseFile(rpm_file);
310 if result <> E_SUCCESS then
311 Erase(rpm_file);
312 end;
313 end;
314 end
315 else
316 Result := E_SUCCESS;
317 end;{case operation}
318 end;
319
320 procedure SetProcessDataProc(hArcData: TArcHandle; ProcessDataProc: TProcessDataProc);
321 var
322 arec : PArchiveRec absolute hArcData;
323 begin
324 if hArcData <> wcxInvalidHandle then
325 begin
326 arec^.process_proc := ProcessDataProc;
327 end;
328 end;
329
330 procedure SetChangeVolProc(hArcData: TArcHandle; ChangeVolProc: TChangeVolProc);
331 var
332 arec : PArchiveRec absolute hArcData;
333 begin
334 if hArcData <> wcxInvalidHandle then
335 begin
336 arec^.changevol_proc := ChangeVolProc;
337 end;
338 end;
339
340 end.