2 Copyright © 2003, The AROS Development Team. All rights reserved.
6 #include <exec/memory.h>
9 #include <proto/exec.h>
10 #include <proto/dos.h>
11 #include <aros/macros.h>
20 #define PKG_BUFFER_SIZE (32*1024) /* 32kiB */
22 /** Low-level functions *****************************************************/
24 APTR
PKG_Open( CONST_STRPTR filename
, LONG mode
)
26 if( mode
!= MODE_READ
&& mode
!= MODE_WRITE
) return NULL
;
28 return BZ2_Open( filename
, mode
);
31 void PKG_Close( APTR pkg
)
33 if( pkg
!= NULL
) BZ2_Close( pkg
);
36 LONG
PKG_Read( APTR pkg
, APTR buffer
, LONG length
)
38 return BZ2_Read( pkg
, buffer
, length
);
41 /** High-level functions ****************************************************/
43 UBYTE
/* version */ PKG_ReadHeader( APTR pkg
)
45 UBYTE data
[4] = { 0, 0, 0, 0 };
46 /*LONG actual = */ PKG_Read( pkg
, data
, 4 );
48 /* FIXME: error handling? naaaah... :S */
53 LONG
/* error */ PKG_ExtractFile( APTR pkg
)
55 LONG pathLength
, dataLength
, rc
, result
;
60 /* Read the path length */
61 rc
= PKG_Read( pkg
, &pathLength
, sizeof( pathLength
) );
62 pathLength
= AROS_BE2LONG(pathLength
);
64 if( rc
== -1 ) { result
= -1; goto cleanup
; }
65 if( rc
== 0 ) { result
= 0; goto cleanup
; }
68 path
= AllocMem( pathLength
+ 1, MEMF_ANY
);
69 if( path
== NULL
) { result
= -1; goto cleanup
; }
70 rc
= PKG_Read( pkg
, path
, pathLength
+ 1 );
71 if( rc
== -1 || rc
== 0) { result
= -1; goto cleanup
; }
73 /* Read the data lendth */
74 rc
= PKG_Read( pkg
, &dataLength
, sizeof( dataLength
) );
75 dataLength
= AROS_BE2LONG(dataLength
);
77 if( rc
== -1 || rc
== 0 ) { result
= -1; goto cleanup
; }
79 //Printf( "Extracting %s (%ld bytes)...\n", path, dataLength );
81 /* Make sure the destination directory exists */
82 if( !MakeDirs( path
) ) { Printf("E:makedirs\n"); result
= -1; goto cleanup
; }
84 /* Read and write the data in pieces */
85 buffer
= AllocMem( PKG_BUFFER_SIZE
, MEMF_ANY
);
86 if( buffer
== NULL
) { Printf("E:mem\n"); result
= -1; goto cleanup
; }
87 output
= Open( path
, MODE_NEWFILE
);
88 if( output
== NULL
) { Printf("E:create\n"); result
= -1; goto cleanup
; }
93 while( total
< dataLength
)
97 if( dataLength
- total
>= PKG_BUFFER_SIZE
)
99 length
= PKG_BUFFER_SIZE
;
103 length
= dataLength
- total
;
106 rc
= PKG_Read( pkg
, buffer
, length
);
107 if( rc
== -1 || rc
== 0 ) { Printf("E:read\n"); result
= -1; goto cleanup
; }
109 rc
= FILE_Write( output
, buffer
, length
);
110 if( rc
== -1 ) { Printf("E:write\n"); result
= -1; goto cleanup
; }
119 if( path
!= NULL
) FreeMem( path
, pathLength
+ 1 );
120 if( buffer
!= NULL
) FreeMem( buffer
, PKG_BUFFER_SIZE
);
121 if( output
!= NULL
) Close( output
);
126 LONG
/* error */ PKG_ExtractEverything( APTR pkg
)
130 PKG_ReadHeader( pkg
);
132 result
= PKG_ExtractFile( pkg
);
133 while( result
!= -1 && result
!= 0 )
135 result
= PKG_ExtractFile( pkg
);