SVN_SILENT made messages (.desktop file)
[kdeadmin.git] / kdat / TarParser.cpp
blob4529d6493da426c36b927dfeed35d0b8e7021002
1 // KDat - a tar-based DAT archiver
2 // Copyright (C) 1998-2000 Sean Vyain, svyain@mail.tds.net
3 // Copyright (C) 2001-2002 Lawrence Widman, kdat@cardiothink.com
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "TarParser.h"
21 #include "TarParser.moc"
23 TarParser::TarParser()
24 : _bufIdx( 0 ),
25 _blocksToSkip( 0 ),
26 _record( 0 ),
27 _longname( FALSE ),
28 _extended( FALSE ),
29 _archnameIdx( 0 )
33 void TarParser::slotData( const char * data, int length )
35 for ( int i = 0; i < length; i++ ) {
36 if ( _bufIdx >= 512 ) {
37 if ( _blocksToSkip > 0 ) {
38 if ( _extended ) {
39 record* rec = (record*)_buf;
40 _extended = rec->ext_hdr.isextended;
41 } else {
42 _blocksToSkip--;
43 if ( _longname ) {
44 memcpy( _archname + _archnameIdx, _buf, 512 );
45 _archnameIdx += 512;
48 } else {
49 parseTarBlock();
51 _bufIdx = 0;
52 _record++;
54 _buf[_bufIdx++] = data[i];
58 int TarParser::parseOctal( const char* buf, int length )
60 int val = 0;
62 for ( int i = 0; i < length; i++ ) {
63 val *= 8;
64 if ( ( buf[i] >= '1' ) && ( buf[i] <= '7' ) ) {
65 val += buf[i] - '0';
69 return val;
72 void TarParser::parseTarBlock()
74 record* rec = (record*)_buf;
76 #if 0
77 printf( "----- parsing tar block -----\n" );
78 printf( "arch_name = '%s'\n", rec->header.arch_name );
79 printf( "mode = '%s'\n", rec->header.mode );
80 printf( "uid = %d\n", parseOctal( rec->header.uid, 6 ) );
81 printf( "gid = %d\n", parseOctal( rec->header.gid, 6 ) );
82 printf( "size = %d\n", parseOctal( rec->header.size, 11 ) );
83 printf( "mtime = %d\n", parseOctal( rec->header.mtime, 11 ) );
84 printf( "chksum = '%s'\n", rec->header.chksum );
85 printf( "linkflag = '%c'\n", rec->header.linkflag );
86 printf( "arch_linkname = '%s'\n", rec->header.arch_linkname );
87 printf( "magic = '%s'\n", rec->header.magic );
88 printf( "uname = '%s'\n", rec->header.uname );
89 printf( "gname = '%s'\n", rec->header.gname );
90 printf( "devmajor = %d\n", parseOctal( rec->header.devmajor, 8 ) );
91 printf( "devminor = %d\n", parseOctal( rec->header.devminor, 8 ) );
92 printf( "atime = %d\n", parseOctal( rec->header.atime, 11 ) );
93 printf( "ctime = %d\n", parseOctal( rec->header.ctime, 11 ) );
94 printf( "offset = %d\n", parseOctal( rec->header.offset, 11 ) );
95 printf( "longnames = '%s'\n", rec->header.longnames );
96 printf( "isextended = %d\n", rec->header.isextended );
97 printf( "realsize = %d\n", parseOctal( rec->header.realsize, 11 ) );
98 #endif
100 if ( rec->header.magic[0] != 0 ) {
101 _blocksToSkip = ( parseOctal( rec->header.size, 11 ) + 511 ) / 512;
102 _extended = rec->header.isextended;
104 if ( rec->header.linkflag == LF_LONGNAME ) {
105 // The actual file name is stored in the next _blocksToSkip blocks of the tar-file.
106 _longname = TRUE;
107 _archnameIdx = 0;
108 } else {
109 if ( _longname ) {
110 _longname = FALSE;
111 emit sigEntry( _archname, parseOctal( rec->header.size, 11 ), parseOctal( rec->header.mtime, 11 ), _record );
112 } else {
113 emit sigEntry( rec->header.arch_name, parseOctal( rec->header.size, 11 ), parseOctal( rec->header.mtime, 11 ), _record );