| 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-2009 Koblov Alexander ([email protected]) |
| 9 |
} |
| 10 |
//*************************************************************** |
| 11 |
// This code based on Christian Ghisler ([email protected]) sources |
| 12 |
//*************************************************************** |
| 13 |
|
| 14 |
|
| 15 |
// History |
| 16 |
// 2001-02-04 Bug: Error Opening rpm file on CD (readonly) |
| 17 |
// Fix: Add FileMode = 0 before Reset |
| 18 |
// Who: Oliver Haeger <[email protected]> |
| 19 |
// 2001-02-27 Bug: My or Ghisler I don't know : WC incorrectly |
| 20 |
// work with names in archive started with |
| 21 |
// "./" or "/" (normal UNIX filenames form) |
| 22 |
|
| 23 |
unit cpio_archive; |
| 24 |
|
| 25 |
interface |
| 26 |
|
| 27 |
{$mode delphi}{$A-,I-} |
| 28 |
{$include calling.inc} |
| 29 |
|
| 30 |
uses |
| 31 |
Classes, |
| 32 |
WcxPlugin, |
| 33 |
cpio_def, cpio_io; |
| 34 |
|
| 35 |
type |
| 36 |
PArchiveRec = ^TArchiveRec; |
| 37 |
TArchiveRec = record |
| 38 |
handle_io : THandle; |
| 39 |
handle_file : file; |
| 40 |
fname : AnsiString; |
| 41 |
fdate : Integer; |
| 42 |
fgEndArchive : Boolean; |
| 43 |
process_proc : TProcessDataProc; |
| 44 |
changevol_proc : TChangeVolProc; |
| 45 |
last_header : CPIO_Header; |
| 46 |
end;{ArchiveRec} |
| 47 |
|
| 48 |
function GetPackerCaps : Integer; dcpcall; export; |
| 49 |
function GetBackgroundFlags: Integer; dcpcall; export; |
| 50 |
function OpenArchive(var ArchiveData : TOpenArchiveData) : TArcHandle; dcpcall; export; |
| 51 |
function CloseArchive(hArcData : TArcHandle) : Integer; dcpcall; export; |
| 52 |
function ReadHeader(hArcData : TArcHandle; var HeaderData : THeaderData) : Integer; dcpcall; export; |
| 53 |
function ProcessFile(hArcData : TArcHandle; Operation : Integer; DestPath : PChar; DestName : PChar) : Integer; dcpcall; export; |
| 54 |
procedure SetProcessDataProc(hArcData : TArcHandle; ProcessDataProc : TProcessDataProc); dcpcall; export; |
| 55 |
procedure SetChangeVolProc(hArcData : TArcHandle; ChangeVolProc : TChangeVolProc); dcpcall; export; |
| 56 |
function CanYouHandleThisFile(FileName: PAnsiChar): Boolean; dcpcall; export; |
| 57 |
|
| 58 |
implementation |
| 59 |
|
| 60 |
uses |
| 61 |
SysUtils, DCDateTimeUtils, DCBasicTypes, DCFileAttributes, DCOSUtils; |
| 62 |
|
| 63 |
function GetPackerCaps: Integer; |
| 64 |
begin |
| 65 |
Result := PK_CAPS_MULTIPLE; |
| 66 |
end; |
| 67 |
|
| 68 |
function GetBackgroundFlags: Integer; |
| 69 |
begin |
| 70 |
Result := BACKGROUND_UNPACK; |
| 71 |
end; |
| 72 |
|
| 73 |
function OpenArchive(var ArchiveData : TOpenArchiveData) : TArcHandle; |
| 74 |
var |
| 75 |
arch : THandle; |
| 76 |
filename : String; |
| 77 |
fgError : Boolean; |
| 78 |
arec : PArchiveRec absolute Result; |
| 79 |
begin |
| 80 |
arec := nil; |
| 81 |
arch := 0; |
| 82 |
fgError := False; |
| 83 |
|
| 84 |
filename := String(ArchiveData.ArcName); |
| 85 |
arch := FileOpen(filename, fmOpenRead or fmShareDenyNone); |
| 86 |
if arch = feInvalidHandle then |
| 87 |
begin |
| 88 |
fgError := True; |
| 89 |
end |
| 90 |
else begin |
| 91 |
New(arec); |
| 92 |
with arec^ do begin |
| 93 |
handle_io := arch; |
| 94 |
fname := filename; |
| 95 |
fdate := FileAge(filename); |
| 96 |
fgEndArchive := False; |
| 97 |
process_proc := nil; |
| 98 |
changevol_proc := nil; |
| 99 |
if fdate = -1 then fdate := 0; |
| 100 |
end; |
| 101 |
AssignFile(arec^.handle_file, filename); |
| 102 |
FileMode := 0; |
| 103 |
Reset(arec^.handle_file, 1); |
| 104 |
if IOResult <> 0 then begin |
| 105 |
fgError := True; |
| 106 |
end;{ioresult} |
| 107 |
end;{arch = -1} |
| 108 |
if fgError then begin |
| 109 |
if arec <> nil then begin |
| 110 |
CloseFile(arec^.handle_file); |
| 111 |
Dispose(arec); |
| 112 |
end; |
| 113 |
FileClose(arch); |
| 114 |
Result := 0; |
| 115 |
ArchiveData.OpenResult := E_EOPEN |
| 116 |
end; |
| 117 |
end; |
| 118 |
|
| 119 |
function CloseArchive(hArcData: TArcHandle): Integer; |
| 120 |
var |
| 121 |
arec : PArchiveRec absolute hArcData; |
| 122 |
begin |
| 123 |
CloseFile(arec^.handle_file); |
| 124 |
FileClose(arec^.handle_io); |
| 125 |
Dispose(arec); |
| 126 |
Result := E_SUCCESS; |
| 127 |
end; |
| 128 |
|
| 129 |
function ReadHeader(hArcData : TArcHandle; var HeaderData : THeaderData): Integer; |
| 130 |
var |
| 131 |
header : CPIO_Header; |
| 132 |
arec : PArchiveRec absolute hArcData; |
| 133 |
begin |
| 134 |
Result := E_EREAD; |
| 135 |
if arec^.fgEndArchive then Result := E_END_ARCHIVE |
| 136 |
else begin |
| 137 |
while True do begin |
| 138 |
if CPIO_ReadHeader(arec^.handle_file, header) then begin |
| 139 |
if header.filename = 'TRAILER!!!' then begin |
| 140 |
Result := E_END_ARCHIVE; |
| 141 |
Break |
| 142 |
end |
| 143 |
else begin |
| 144 |
if header.filesize <> 0 then begin |
| 145 |
with HeaderData do begin |
| 146 |
copy_str2buf(TStrBuf(ArcName), arec^.fname); |
| 147 |
copy_str2buf(TStrBuf(FileName), header.filename); |
| 148 |
PackSize := header.filesize; |
| 149 |
UnpSize := header.filesize; |
| 150 |
FileAttr := UnixToWcxFileAttr(header.mode); |
| 151 |
FileTime := UnixFileTimeToWcxTime(TUnixFileTime(header.mtime)); |
| 152 |
end;{with} |
| 153 |
Result := 0; |
| 154 |
Break; |
| 155 |
end |
| 156 |
else |
| 157 |
Continue; |
| 158 |
end;{not end of file "TRAILER!!!"} |
| 159 |
end{if header readed} |
| 160 |
else begin |
| 161 |
Result := E_EREAD; |
| 162 |
Break; |
| 163 |
end; |
| 164 |
end;{while true} |
| 165 |
arec^.last_header := header; |
| 166 |
end;{if not end of archive} |
| 167 |
end; |
| 168 |
|
| 169 |
function ProcessFile(hArcData: TArcHandle; Operation: Integer; DestPath: PChar; DestName: PChar): Integer; |
| 170 |
var |
| 171 |
cpio_file : file; |
| 172 |
cpio_name : String; |
| 173 |
cpio_dir : String; |
| 174 |
buf : Pointer; |
| 175 |
buf_size : LongWord; |
| 176 |
fsize : LongWord; |
| 177 |
fgReadError : Boolean; |
| 178 |
fgWriteError: Boolean; |
| 179 |
fAborted : Boolean; |
| 180 |
head : CPIO_Header; |
| 181 |
arec : PArchiveRec absolute hArcData; |
| 182 |
begin |
| 183 |
head := arec^.last_header; |
| 184 |
case Operation of |
| 185 |
PK_TEST : begin |
| 186 |
faborted:=false; |
| 187 |
fsize := head.filesize; |
| 188 |
buf_size := 65536; |
| 189 |
GetMem(buf, buf_size); |
| 190 |
fgReadError := False; |
| 191 |
while not faborted do begin |
| 192 |
if fsize < buf_size then Break; |
| 193 |
BlockRead(arec^.handle_file, buf^, buf_size); |
| 194 |
if IOResult <> 0 then begin |
| 195 |
fgReadError := True; |
| 196 |
Break; |
| 197 |
end;{if IO error} |
| 198 |
Dec(fsize, buf_size); |
| 199 |
if Assigned(arec^.process_proc) then |
| 200 |
if arec^.process_proc(nil, buf_size)=0 then |
| 201 |
faborted:=true; |
| 202 |
end;{while} |
| 203 |
if not fgReadError and not faborted then begin |
| 204 |
if fsize <> 0 then begin |
| 205 |
BlockRead(arec^.handle_file, buf^, fsize); |
| 206 |
if IOResult <> 0 then fgReadError := True; |
| 207 |
if Assigned(arec^.process_proc) then |
| 208 |
arec^.process_proc(nil, fsize); |
| 209 |
end; |
| 210 |
end; |
| 211 |
if faborted then Result:=E_EABORTED |
| 212 |
else if fgReadError then Result := E_EREAD |
| 213 |
else begin |
| 214 |
Result := 0; |
| 215 |
if arec^.last_header.IsOldHeader then begin |
| 216 |
if not AlignFilePointer(arec^.handle_file, 2) then Result := E_EREAD; |
| 217 |
end else |
| 218 |
if not AlignFilePointer(arec^.handle_file, 4) then Result := E_EREAD; |
| 219 |
end; |
| 220 |
FreeMem(buf, 65536); |
| 221 |
end;{PK_TEST} |
| 222 |
PK_SKIP : begin |
| 223 |
Seek(arec^.handle_file, FilePos(arec^.handle_file) + LongInt(head.filesize)); |
| 224 |
if IOResult = 0 then begin |
| 225 |
Result := 0; |
| 226 |
if arec^.last_header.IsOldHeader then begin |
| 227 |
if not AlignFilePointer(arec^.handle_file, 2) then Result := E_EREAD; |
| 228 |
end else |
| 229 |
if not AlignFilePointer(arec^.handle_file, 4) then Result := E_EREAD; |
| 230 |
end else Result := E_EREAD; |
| 231 |
end;{PK_SKIP} |
| 232 |
PK_EXTRACT : begin |
| 233 |
cpio_name := String(DestName); |
| 234 |
cpio_dir := ExtractFileDir(cpio_name); |
| 235 |
if CreateDirectories(cpio_dir) then begin |
| 236 |
AssignFile(cpio_file, cpio_name); |
| 237 |
Rewrite(cpio_file, 1); |
| 238 |
if IOResult <> 0 then Result := E_ECREATE |
| 239 |
else begin |
| 240 |
fsize := head.filesize; |
| 241 |
buf_size := 65536; |
| 242 |
GetMem(buf, buf_size); |
| 243 |
fgReadError := False; |
| 244 |
fgWriteError :=False; |
| 245 |
fAborted := False; |
| 246 |
while not fAborted do begin |
| 247 |
if fsize < buf_size then Break; |
| 248 |
BlockRead(arec^.handle_file, buf^, buf_size); |
| 249 |
if IOResult <> 0 then begin |
| 250 |
fgReadError := True; |
| 251 |
Break; |
| 252 |
end;{if IO error} |
| 253 |
BlockWrite(cpio_file, buf^, buf_size); |
| 254 |
if ioresult<>0 then begin |
| 255 |
fgWriteError:=true; |
| 256 |
break; |
| 257 |
end; |
| 258 |
Dec(fsize, buf_size); |
| 259 |
if Assigned(arec^.process_proc) then |
| 260 |
if arec^.process_proc(nil, buf_size)=0 then |
| 261 |
fAborted:=true; |
| 262 |
end;{while} |
| 263 |
if not fgReadError then begin |
| 264 |
if fsize <> 0 then begin |
| 265 |
BlockRead(arec^.handle_file, buf^, fsize); |
| 266 |
if IOResult <> 0 then fgReadError := True; |
| 267 |
BlockWrite(cpio_file, buf^, fsize); |
| 268 |
if ioresult<>0 then |
| 269 |
fgWriteError:=true; |
| 270 |
if Assigned(arec^.process_proc) then |
| 271 |
if arec^.process_proc(nil, fsize)=0 then |
| 272 |
fAborted:=true; |
| 273 |
end; |
| 274 |
end; |
| 275 |
if fAborted then Result:= E_EABORTED |
| 276 |
else if fgWriteError then Result := E_EWRITE |
| 277 |
else if fgReadError then Result := E_EREAD |
| 278 |
else begin |
| 279 |
Result := 0; |
| 280 |
if arec^.last_header.IsOldHeader then begin |
| 281 |
if not AlignFilePointer(arec^.handle_file, 2) then Result := E_EREAD; |
| 282 |
end else |
| 283 |
if not AlignFilePointer(arec^.handle_file, 4) then Result := E_EREAD; |
| 284 |
end; |
| 285 |
CloseFile(cpio_file); |
| 286 |
if Result <> 0 then |
| 287 |
Erase(cpio_file) |
| 288 |
else begin |
| 289 |
mbFileSetAttr(cpio_name, UnixToWcxFileAttr(head.mode)); |
| 290 |
FileSetDate(cpio_name, UnixFileTimeToWcxTime(TUnixFileTime(head.mtime))); |
| 291 |
end; |
| 292 |
FreeMem(buf, 65536); |
| 293 |
end; |
| 294 |
end |
| 295 |
else Result := E_ECREATE; |
| 296 |
end{PK_EXTRACT} |
| 297 |
else |
| 298 |
Result := 0; |
| 299 |
end;{case operation} |
| 300 |
end; |
| 301 |
|
| 302 |
procedure SetProcessDataProc(hArcData: TArcHandle; ProcessDataProc: TProcessDataProc); |
| 303 |
var |
| 304 |
arec : PArchiveRec absolute hArcData; |
| 305 |
begin |
| 306 |
if hArcData <> wcxInvalidHandle then |
| 307 |
begin |
| 308 |
arec^.process_proc := ProcessDataProc; |
| 309 |
end; |
| 310 |
end; |
| 311 |
|
| 312 |
procedure SetChangeVolProc(hArcData: TArcHandle; ChangeVolProc: TChangeVolProc); |
| 313 |
var |
| 314 |
arec : PArchiveRec absolute hArcData; |
| 315 |
begin |
| 316 |
if hArcData <> wcxInvalidHandle then |
| 317 |
begin |
| 318 |
arec^.changevol_proc := ChangeVolProc; |
| 319 |
end; |
| 320 |
end; |
| 321 |
|
| 322 |
function CanYouHandleThisFile; |
| 323 |
begin |
| 324 |
try |
| 325 |
Result:= IsCPIOArchive(StrPas(FileName)); |
| 326 |
except |
| 327 |
Result := False; |
| 328 |
end; |
| 329 |
end; |
| 330 |
|
| 331 |
end. |