summaryrefslogtreecommitdiffstats
path: root/tar/tar.cc
blob: 1187291542b78b340edfa7ec5c696fc2a22858ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>

#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqptrqueue.h>
#include <kurl.h>
#include <kdebug.h>
#include <kinstance.h>
#include <ktar.h>
#include <kzip.h>
#include <kar.h>
#include <kmimemagic.h>
#include <klocale.h>
#include <tdeversion.h>

#include <errno.h> // to be removed

#include "tar.h"

using namespace KIO;

#if KDE_IS_VERSION(3,4,0)
extern "C" { int KDE_EXPORT kdemain( int argc, char **argv ); }
#else
extern "C" { int kdemain( int argc, char **argv ); }
#endif

int kdemain( int argc, char **argv ) {
	TDEInstance instance( "kio_tar" );

	kdDebug( 7109 ) << "Starting " << getpid() << endl;

	if ( argc != 4 ) {
		fprintf( stderr, "Usage: kio_tar protocol domain-socket1 domain-socket2\n" );
		exit( -1 );
	}

	ArchiveProtocol slave( argv[ 2 ], argv[ 3 ] );
	slave.dispatchLoop();

	kdDebug( 7109 ) << "Done" << endl;
	return 0;
}

ArchiveProtocol::ArchiveProtocol( const TQCString &pool, const TQCString &app ) : SlaveBase( "tar", pool, app ) {
	kdDebug( 7109 ) << "ArchiveProtocol::ArchiveProtocol" << endl;
	m_archiveFile = 0L;
}

ArchiveProtocol::~ArchiveProtocol() {
	delete m_archiveFile;
}

void ArchiveProtocol::put( const KURL& url, int, bool, bool resume ){
	if( resume ){
		error(ERR_UNSUPPORTED_ACTION,i18n("This protocol does not support resuming") );	
		return;
	}
	
	TQByteArray  tempBuffer;
	TQPtrQueue<TQByteArray> buffer;
	buffer.setAutoDelete(true);
	int readResult=0;
	int size = 0;
	TQByteArray* temp;
	do{
		dataReq();
		temp = new TQByteArray();
		readResult = readData(*temp);
		buffer.enqueue(temp);
		size += temp->size();
 	} while( readResult > 0 );
	
	TQString filename = url.path().mid(m_archiveName.length()+1);

	if( !m_archiveFile->prepareWriting(filename,user,group,size) ){
		error(ERR_UNSUPPORTED_ACTION,
		      i18n("Writing to %1 is not supported").arg(filename) );
		return;
	}
	while( (temp=buffer.dequeue()) ){
		m_archiveFile->writeData(temp->data(),temp->size());
	}
	m_archiveFile->doneWriting(size);

	finished();
}

void ArchiveProtocol::mkdir(const KURL& url,int){
	TQString filename = url.path().mid(m_archiveName.length()+1);
	m_archiveFile->writeDir(filename,user,group);
	finished();
}

bool ArchiveProtocol::checkNewFile( const KURL & url, TQString & path ) {
	TQString fullPath = url.path();
	kdDebug( 7109 ) << "ArchiveProtocol::checkNewFile " << fullPath << endl;


	// Are we already looking at that file ?
	if ( m_archiveFile && m_archiveName == fullPath.left( m_archiveName.length() ) ) {
		// Has it changed ?
		struct stat statbuf;
		if ( ::stat( TQFile::encodeName( m_archiveName ), &statbuf ) == 0 ) {
			if ( m_mtime == statbuf.st_mtime ) {
				path = fullPath.mid( m_archiveName.length() );
				kdDebug( 7109 ) << "ArchiveProtocol::checkNewFile returning " << path << endl;
				return true;
			}
		}
	}
	kdDebug( 7109 ) << "Need to open a new file" << endl;

	// Close previous file
	if ( m_archiveFile ) {
		m_archiveFile->close();
		delete m_archiveFile;
		m_archiveFile = 0L;
	}

	// Find where the tar file is in the full path
	int pos = 0;
	TQString archiveFile;
	path = TQString();

	int len = fullPath.length();
	if ( len != 0 && fullPath[ len - 1 ] != '/' )
		fullPath += '/';

	kdDebug( 7109 ) << "the full path is " << fullPath << endl;
	while ( ( pos = fullPath.find( '/', pos + 1 ) ) != -1 ) {
		TQString tryPath = fullPath.left( pos );
		kdDebug( 7109 ) << fullPath << "  trying " << tryPath << endl;
		struct stat statbuf;
		if ( ::stat( TQFile::encodeName( tryPath ), &statbuf ) == 0 && !S_ISDIR( statbuf.st_mode ) ) {
			archiveFile = tryPath;
			m_mtime = statbuf.st_mtime;
			user = TQFileInfo(archiveFile).owner();
			group = TQFileInfo(archiveFile).group();
			path = fullPath.mid( pos + 1 );
			kdDebug( 7109 ) << "fullPath=" << fullPath << " path=" << path << endl;
			len = path.length();
			if ( len > 1 ) {
				if ( path[ len - 1 ] == '/' )
					path.truncate( len - 1 );
			} else
				path = TQString::fromLatin1( "/" );
			kdDebug( 7109 ) << "Found. archiveFile=" << archiveFile << " path=" << path << endl;
			break;
		}
	}
	if ( archiveFile.isEmpty() ) {
		kdDebug( 7109 ) << "ArchiveProtocol::checkNewFile: not found" << endl;
		return false;
	}

	// Open new file
	if ( url.protocol() == "tar" ) {
		kdDebug( 7109 ) << "Opening KTar on " << archiveFile << endl;
		m_archiveFile = new KTar( archiveFile );
	} else if ( url.protocol() == "ar" ) {
		kdDebug( 7109 ) << "Opening KAr on " << archiveFile << endl;
		m_archiveFile = new KAr( archiveFile );
	} else if ( url.protocol() == "zip" ) {
		kdDebug( 7109 ) << "Opening KZip on " << archiveFile << endl;
		m_archiveFile = new KZip( archiveFile );
	} else {
		kdWarning( 7109 ) << "Protocol " << url.protocol() << " not supported by this IOSlave" << endl;
		return false;
	}

	if ( !m_archiveFile->open( IO_ReadWrite ) ) {
		kdDebug( 7109 ) << "Opening " << archiveFile << "failed." << endl;
		delete m_archiveFile;
		m_archiveFile = 0L;
		return false;
	}

	m_archiveName = archiveFile;
	return true;
}


void ArchiveProtocol::createUDSEntry( const KArchiveEntry * archiveEntry, UDSEntry & entry ) {
	UDSAtom atom;
	entry.clear();
	atom.m_uds = UDS_NAME;
	atom.m_str = archiveEntry->name();
	entry.append( atom );

	atom.m_uds = UDS_FILE_TYPE;
	atom.m_long = archiveEntry->permissions() & S_IFMT; // keep file type only
	entry.append( atom );

	atom.m_uds = UDS_SIZE;
	atom.m_long = archiveEntry->isFile() ? ( ( KArchiveFile * ) archiveEntry ) ->size() : 0L ;
	entry.append( atom );

	atom.m_uds = UDS_MODIFICATION_TIME;
	atom.m_long = archiveEntry->date();
	entry.append( atom );

	atom.m_uds = UDS_ACCESS;
	atom.m_long = archiveEntry->permissions() & 07777; // keep permissions only
	entry.append( atom );

	atom.m_uds = UDS_USER;
	atom.m_str = archiveEntry->user();
	entry.append( atom );

	atom.m_uds = UDS_GROUP;
	atom.m_str = archiveEntry->group();
	entry.append( atom );

	atom.m_uds = UDS_LINK_DEST;
	atom.m_str = archiveEntry->symlink();
	entry.append( atom );
}

void ArchiveProtocol::listDir( const KURL & url ) {
	kdDebug( 7109 ) << "ArchiveProtocol::listDir " << url.url() << endl;

	TQString path;
	if ( !checkNewFile( url, path ) ) {
		TQCString _path( TQFile::encodeName( url.path() ) );
		kdDebug( 7109 ) << "Checking (stat) on " << _path << endl;
		struct stat buff;
		if ( ::stat( _path.data(), &buff ) == -1 || !S_ISDIR( buff.st_mode ) ) {
			error( KIO::ERR_DOES_NOT_EXIST, url.prettyURL() );
			return ;
		}
		// It's a real dir -> redirect
		KURL redir;
		redir.setPath( url.path() );
		kdDebug( 7109 ) << "Ok, redirection to " << redir.url() << endl;
		redirection( redir );
		finished();
		// And let go of the tar file - for people who want to unmount a cdrom after that
		delete m_archiveFile;
		m_archiveFile = 0L;
		return ;
	}

	if ( path.isEmpty() ) {
		KURL redir( url.protocol() + TQString::fromLatin1( ":/" ) );
		kdDebug( 7109 ) << "url.path()==" << url.path() << endl;
		redir.setPath( url.path() + TQString::fromLatin1( "/" ) );
		kdDebug( 7109 ) << "ArchiveProtocol::listDir: redirection " << redir.url() << endl;
		redirection( redir );
		finished();
		return ;
	}

	kdDebug( 7109 ) << "checkNewFile done" << endl;
	const KArchiveDirectory* root = m_archiveFile->directory();
	const KArchiveDirectory* dir;
	if ( !path.isEmpty() && path != "/" ) {
		kdDebug( 7109 ) << TQString( "Looking for entry %1" ).arg( path ) << endl;
		const KArchiveEntry* e = root->entry( path );
		if ( !e ) {
			error( KIO::ERR_DOES_NOT_EXIST, url.prettyURL() );
			return ;
		}
		if ( ! e->isDirectory() ) {
			error( KIO::ERR_IS_FILE, url.prettyURL() );
			return ;
		}
		dir = ( KArchiveDirectory* ) e;
	} else {
		dir = root;
	}

	TQStringList l = dir->entries();
	totalSize( l.count() );

	UDSEntry entry;
	TQStringList::Iterator it = l.begin();
	for ( ; it != l.end(); ++it ) {
		kdDebug( 7109 ) << ( *it ) << endl;
		const KArchiveEntry* archiveEntry = dir->entry( ( *it ) );

		createUDSEntry( archiveEntry, entry );

		listEntry( entry, false );
	}

	listEntry( entry, true ); // ready

	finished();

	kdDebug( 7109 ) << "ArchiveProtocol::listDir done" << endl;
}

void ArchiveProtocol::stat( const KURL & url ) {
	TQString path;
	UDSEntry entry;
	if ( !checkNewFile( url, path ) ) {
		// We may be looking at a real directory - this happens
		// when pressing up after being in the root of an archive
		TQCString _path( TQFile::encodeName( url.path() ) );
		kdDebug( 7109 ) << "ArchiveProtocol::stat (stat) on " << _path << endl;
		struct stat buff;
		if ( ::stat( _path.data(), &buff ) == -1 || !S_ISDIR( buff.st_mode ) ) {
			kdDebug( 7109 ) << "isdir=" << S_ISDIR( buff.st_mode ) << "  errno=" << strerror( errno ) << endl;
			error( KIO::ERR_DOES_NOT_EXIST, url.path() );
			return ;
		}
		// Real directory. Return just enough information for KRun to work
		UDSAtom atom;
		atom.m_uds = KIO::UDS_NAME;
		atom.m_str = url.fileName();
		entry.append( atom );
		kdDebug( 7109 ) << "ArchiveProtocol::stat returning name=" << url.fileName() << endl;

		atom.m_uds = KIO::UDS_FILE_TYPE;
		atom.m_long = buff.st_mode & S_IFMT;
		entry.append( atom );

		statEntry( entry );

		finished();

		// And let go of the tar file - for people who want to unmount a cdrom after that
		delete m_archiveFile;
		m_archiveFile = 0L;
		return ;
	}

	const KArchiveDirectory* root = m_archiveFile->directory();
	const KArchiveEntry* archiveEntry;
	if ( path.isEmpty() ) {
		path = TQString::fromLatin1( "/" );
		archiveEntry = root;
	} else {
		archiveEntry = root->entry( path );
	}
	if ( !archiveEntry ) {
		error( KIO::ERR_DOES_NOT_EXIST, url.prettyURL() );
		return ;
	}

	createUDSEntry( archiveEntry, entry );
	statEntry( entry );

	finished();
}

void ArchiveProtocol::get( const KURL & url ) {
	kdDebug( 7109 ) << "ArchiveProtocol::get" << url.url() << endl;

	TQString path;
	if ( !checkNewFile( url, path ) ) {
		error( KIO::ERR_DOES_NOT_EXIST, url.prettyURL() );
		return ;
	}

	const KArchiveDirectory* root = m_archiveFile->directory();
	const KArchiveEntry* archiveEntry = root->entry( path );

	if ( !archiveEntry ) {
		error( KIO::ERR_DOES_NOT_EXIST, url.prettyURL() );
		return ;
	}
	if ( archiveEntry->isDirectory() ) {
		error( KIO::ERR_IS_DIRECTORY, url.prettyURL() );
		return ;
	}
	const KArchiveFile* archiveFileEntry = static_cast<const KArchiveFile *>( archiveEntry );
	if ( !archiveEntry->symlink().isEmpty() ) {
		kdDebug( 7109 ) << "Redirection to " << archiveEntry->symlink() << endl;
		KURL realURL( url, archiveEntry->symlink() );
		kdDebug( 7109 ) << "realURL= " << realURL.url() << endl;
		redirection( realURL );
		finished();
		return ;
	}

	totalSize( archiveFileEntry->size() );

	TQByteArray completeData = archiveFileEntry->data();

	KMimeMagicResult * result = KMimeMagic::self() ->findBufferFileType( completeData, path );
	kdDebug( 7109 ) << "Emitting mimetype " << result->mimeType() << endl;
	mimeType( result->mimeType() );

	data( completeData );

	processedSize( archiveFileEntry->size() );
	data( TQByteArray() );

	finished();
}

/*
  In case someone wonders how the old filter stuff looked like :    :)
void TARProtocol::slotData(void *_p, int _len)
{
  switch (m_cmd) {
    case CMD_PUT:
      assert(m_pFilter);
      m_pFilter->send(_p, _len);
      break;
    default:
      abort();
      break;
    }
}
 
void TARProtocol::slotDataEnd()
{
  switch (m_cmd) {
    case CMD_PUT:
      assert(m_pFilter && m_pJob);
      m_pFilter->finish();
      m_pJob->dataEnd();
      m_cmd = CMD_NONE;
      break;
    default:
      abort();
      break;
    }
}
 
void TARProtocol::jobData(void *_p, int _len)
{
  switch (m_cmd) {
  case CMD_GET:
    assert(m_pFilter);
    m_pFilter->send(_p, _len);
    break;
  case CMD_COPY:
    assert(m_pFilter);
    m_pFilter->send(_p, _len);
    break;
  default:
    abort();
  }
}
 
void TARProtocol::jobDataEnd()
{
  switch (m_cmd) {
  case CMD_GET:
    assert(m_pFilter);
    m_pFilter->finish();
    dataEnd();
    break;
  case CMD_COPY:
    assert(m_pFilter);
    m_pFilter->finish();
    m_pJob->dataEnd();
    break;
  default:
    abort();
  }
}
 
void TARProtocol::filterData(void *_p, int _len)
{
debug("void TARProtocol::filterData");
  switch (m_cmd) {
  case CMD_GET:
    data(_p, _len);
    break;
  case CMD_PUT:
    assert (m_pJob);
    m_pJob->data(_p, _len);
    break;
  case CMD_COPY:
    assert(m_pJob);
    m_pJob->data(_p, _len);
    break;
  default:
    abort();
  }
}
*/