summaryrefslogtreecommitdiff
authorllornkcor <llornkcor>2002-07-18 03:11:17 (UTC)
committer llornkcor <llornkcor>2002-07-18 03:11:17 (UTC)
commit7fba4ce72b9201e3a04214c75a1031958090a618 (patch) (unidiff)
tree77f7ea331fd38707f3c74dc5a67b47940ea36038
parent786393948f3cd5f67a48f44a7a40422636f4b46e (diff)
downloadopie-7fba4ce72b9201e3a04214c75a1031958090a618.zip
opie-7fba4ce72b9201e3a04214c75a1031958090a618.tar.gz
opie-7fba4ce72b9201e3a04214c75a1031958090a618.tar.bz2
attempt for change ip back to hex range from string
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--core/launcher/transferserver.cpp803
1 files changed, 402 insertions, 401 deletions
diff --git a/core/launcher/transferserver.cpp b/core/launcher/transferserver.cpp
index a6dab07..ed3e2c6 100644
--- a/core/launcher/transferserver.cpp
+++ b/core/launcher/transferserver.cpp
@@ -1,1344 +1,1345 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. 2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of the Qtopia Environment. 4** This file is part of the Qtopia Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software 7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20#define _XOPEN_SOURCE 20#define _XOPEN_SOURCE
21#include <pwd.h> 21#include <pwd.h>
22#include <sys/types.h> 22#include <sys/types.h>
23#include <unistd.h> 23#include <unistd.h>
24#include <stdlib.h> 24#include <stdlib.h>
25#include <time.h> 25#include <time.h>
26#include <shadow.h> 26#include <shadow.h>
27 27
28extern "C" { 28extern "C" {
29#include <uuid/uuid.h> 29#include <uuid/uuid.h>
30#define UUID_H_INCLUDED 30#define UUID_H_INCLUDED
31} 31}
32 32
33#if defined(_OS_LINUX_) 33#if defined(_OS_LINUX_)
34#include <shadow.h> 34#include <shadow.h>
35#endif 35#endif
36 36
37#include <qdir.h> 37#include <qdir.h>
38#include <qfile.h> 38#include <qfile.h>
39#include <qtextstream.h> 39#include <qtextstream.h>
40#include <qdatastream.h> 40#include <qdatastream.h>
41#include <qmessagebox.h> 41#include <qmessagebox.h>
42#include <qstringlist.h> 42#include <qstringlist.h>
43#include <qfileinfo.h> 43#include <qfileinfo.h>
44#include <qregexp.h> 44#include <qregexp.h>
45//#include <qpe/qcopchannel_qws.h> 45//#include <qpe/qcopchannel_qws.h>
46#include <qpe/process.h> 46#include <qpe/process.h>
47#include <qpe/global.h> 47#include <qpe/global.h>
48#include <qpe/config.h> 48#include <qpe/config.h>
49#include <qpe/contact.h> 49#include <qpe/contact.h>
50#include <qpe/quuid.h> 50#include <qpe/quuid.h>
51#include <qpe/version.h> 51#include <qpe/version.h>
52#ifdef QWS 52#ifdef QWS
53#include <qpe/qcopenvelope_qws.h> 53#include <qpe/qcopenvelope_qws.h>
54#endif 54#endif
55 55
56#include "transferserver.h" 56#include "transferserver.h"
57#include "qprocess.h" 57#include "qprocess.h"
58 58
59const int block_size = 51200; 59const int block_size = 51200;
60 60
61TransferServer::TransferServer( Q_UINT16 port, QObject *parent , 61TransferServer::TransferServer( Q_UINT16 port, QObject *parent ,
62 const char* name ) 62 const char* name )
63 : QServerSocket( port, 1, parent, name ) 63 : QServerSocket( port, 1, parent, name )
64{ 64{
65 if ( !ok() ) 65 if ( !ok() )
66 qWarning( "Failed to bind to port %d", port ); 66 qWarning( "Failed to bind to port %d", port );
67} 67}
68 68
69TransferServer::~TransferServer() 69TransferServer::~TransferServer()
70{ 70{
71 71
72} 72}
73 73
74void TransferServer::newConnection( int socket ) 74void TransferServer::newConnection( int socket )
75{ 75{
76 (void) new ServerPI( socket, this ); 76 (void) new ServerPI( socket, this );
77} 77}
78 78
79QString SyncAuthentication::serverId() 79QString SyncAuthentication::serverId()
80{ 80{
81 Config cfg("Security"); 81 Config cfg("Security");
82 cfg.setGroup("Sync"); 82 cfg.setGroup("Sync");
83 QString r=cfg.readEntry("serverid"); 83 QString r=cfg.readEntry("serverid");
84 if ( r.isEmpty() ) { 84 if ( r.isEmpty() ) {
85 uuid_t uuid; 85 uuid_t uuid;
86 uuid_generate( uuid ); 86 uuid_generate( uuid );
87 cfg.writeEntry("serverid",(r = QUuid( uuid ).toString())); 87 cfg.writeEntry("serverid",(r = QUuid( uuid ).toString()));
88 } 88 }
89 return r; 89 return r;
90} 90}
91 91
92QString SyncAuthentication::ownerName() 92QString SyncAuthentication::ownerName()
93{ 93{
94 QString vfilename = Global::applicationFileName("addressbook", 94 QString vfilename = Global::applicationFileName("addressbook",
95 "businesscard.vcf"); 95 "businesscard.vcf");
96 if (QFile::exists(vfilename)) { 96 if (QFile::exists(vfilename)) {
97 Contact c; 97 Contact c;
98 c = Contact::readVCard( vfilename )[0]; 98 c = Contact::readVCard( vfilename )[0];
99 return c.fullName(); 99 return c.fullName();
100 } 100 }
101 101
102 return ""; 102 return "";
103} 103}
104 104
105QString SyncAuthentication::loginName() 105QString SyncAuthentication::loginName()
106{ 106{
107 struct passwd *pw; 107 struct passwd *pw;
108 pw = getpwuid( geteuid() ); 108 pw = getpwuid( geteuid() );
109 return QString::fromLocal8Bit( pw->pw_name ); 109 return QString::fromLocal8Bit( pw->pw_name );
110} 110}
111 111
112int SyncAuthentication::isAuthorized(QHostAddress peeraddress) 112int SyncAuthentication::isAuthorized(QHostAddress peeraddress)
113{ 113{
114 Config cfg("Security"); 114 Config cfg("Security");
115 cfg.setGroup("Sync"); 115 cfg.setGroup("Sync");
116 QString allowedstr = cfg.readEntry("auth_peer","192.168.1.0"); 116// QString allowedstr = cfg.readEntry("auth_peer","192.168.1.0");
117 QHostAddress allowed; 117 uint auth_peer = cfg.readNumEntry("auth_peer",0xc0a80100);
118 allowed.setAddress(allowedstr); 118
119 uint auth_peer = allowed.ip4Addr(); 119// QHostAddress allowed;
120// allowed.setAddress(allowedstr);
121// uint auth_peer = allowed.ip4Addr();
120 uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits",24); 122 uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits",24);
121 uint mask = auth_peer_bits >= 32 // shifting by 32 is not defined 123 uint mask = auth_peer_bits >= 32 // shifting by 32 is not defined
122 ? 0xffffffff : (((1<<auth_peer_bits)-1)<<(32-auth_peer_bits)); 124 ? 0xffffffff : (((1<<auth_peer_bits)-1)<<(32-auth_peer_bits));
123 return (peeraddress.ip4Addr() & mask) == auth_peer; 125 return (peeraddress.ip4Addr() & mask) == auth_peer;
124} 126}
125 127
126bool SyncAuthentication::checkUser( const QString& user ) 128bool SyncAuthentication::checkUser( const QString& user )
127{ 129{
128 if ( user.isEmpty() ) return FALSE; 130 if ( user.isEmpty() ) return FALSE;
129 QString euser = loginName(); 131 QString euser = loginName();
130 return user == euser; 132 return user == euser;
131} 133}
132 134
133bool SyncAuthentication::checkPassword( const QString& password ) 135bool SyncAuthentication::checkPassword( const QString& password )
134{ 136{
135#ifdef ALLOW_UNIX_USER_FTP 137#ifdef ALLOW_UNIX_USER_FTP
136 // First, check system password... 138 // First, check system password...
137 139
138 struct passwd *pw = 0; 140 struct passwd *pw = 0;
139 struct spwd *spw = 0; 141 struct spwd *spw = 0;
140 142
141 pw = getpwuid( geteuid() ); 143 pw = getpwuid( geteuid() );
142 spw = getspnam( pw->pw_name ); 144 spw = getspnam( pw->pw_name );
143 145
144 QString cpwd = QString::fromLocal8Bit( pw->pw_passwd ); 146 QString cpwd = QString::fromLocal8Bit( pw->pw_passwd );
145 if ( cpwd == "x" && spw ) 147 if ( cpwd == "x" && spw )
146 cpwd = QString::fromLocal8Bit( spw->sp_pwdp ); 148 cpwd = QString::fromLocal8Bit( spw->sp_pwdp );
147 149
148 // Note: some systems use more than crypt for passwords. 150 // Note: some systems use more than crypt for passwords.
149 QString cpassword = QString::fromLocal8Bit( crypt( password.local8Bit(), cpwd.local8Bit() ) ); 151 QString cpassword = QString::fromLocal8Bit( crypt( password.local8Bit(), cpwd.local8Bit() ) );
150 if ( cpwd == cpassword ) 152 if ( cpwd == cpassword )
151 return TRUE; 153 return TRUE;
152#endif 154#endif
153 155
154 static int lastdenial=0; 156 static int lastdenial=0;
155 static int denials=0; 157 static int denials=0;
156 int now = time(0); 158 int now = time(0);
157 159
158 // Detect old Qtopia Desktop (no password) 160 // Detect old Qtopia Desktop (no password)
159 if ( password.isEmpty() ) { 161 if ( password.isEmpty() ) {
160 if ( denials < 1 || now > lastdenial+600 ) { 162 if ( denials < 1 || now > lastdenial+600 ) {
161 QMessageBox::warning( 0,tr("Sync Connection"), 163 QMessageBox::warning( 0,tr("Sync Connection"),
162 tr("<p>An unauthorized system is requesting access to this device." 164 tr("<p>An unauthorized system is requesting access to this device."
163 "<p>If you are using a version of Qtopia Desktop older than 1.5.1, " 165 "<p>If you are using a version of Qtopia Desktop older than 1.5.1, "
164 "please upgrade."), 166 "please upgrade."),
165 tr("Deny") ); 167 tr("Deny") );
166 denials++; 168 denials++;
167 lastdenial=now; 169 lastdenial=now;
168 } 170 }
169 return FALSE; 171 return FALSE;
170 } 172 }
171 173
172 // Second, check sync password... 174 // Second, check sync password...
173 if ( password.left(6) == "Qtopia" ) { 175 if ( password.left(6) == "Qtopia" ) {
174 QString cpassword = QString::fromLocal8Bit( crypt( password.mid(8).local8Bit(), "qp" ) ); 176 QString cpassword = QString::fromLocal8Bit( crypt( password.mid(8).local8Bit(), "qp" ) );
175 Config cfg("Security"); 177 Config cfg("Security");
176 cfg.setGroup("Sync"); 178 cfg.setGroup("Sync");
177 QString pwds = cfg.readEntry("Passwords"); 179 QString pwds = cfg.readEntry("Passwords");
178 if ( QStringList::split(QChar(' '),pwds).contains(cpassword) ) 180 if ( QStringList::split(QChar(' '),pwds).contains(cpassword) )
179 return TRUE; 181 return TRUE;
180 182
181 // Unrecognized system. Be careful... 183 // Unrecognized system. Be careful...
182 184
183 if ( (denials > 2 && now < lastdenial+600) 185 if ( (denials > 2 && now < lastdenial+600)
184 || QMessageBox::warning(0,tr("Sync Connection"), 186 || QMessageBox::warning(0,tr("Sync Connection"),
185 tr("<p>An unrecognized system is requesting access to this device." 187 tr("<p>An unrecognized system is requesting access to this device."
186 "<p>If you have just initiated a Sync for the first time, this is normal."), 188 "<p>If you have just initiated a Sync for the first time, this is normal."),
187 tr("Allow"),tr("Deny"))==1 ) 189 tr("Allow"),tr("Deny"))==1 )
188 { 190 {
189 denials++; 191 denials++;
190 lastdenial=now; 192 lastdenial=now;
191 return FALSE; 193 return FALSE;
192 } else { 194 } else {
193 denials=0; 195 denials=0;
194 cfg.writeEntry("Passwords",pwds+" "+cpassword); 196 cfg.writeEntry("Passwords",pwds+" "+cpassword);
195 return TRUE; 197 return TRUE;
196 } 198 }
197 } 199 }
198 200
199 return FALSE; 201 return FALSE;
200} 202}
201 203
202
203ServerPI::ServerPI( int socket, QObject *parent , const char* name ) 204ServerPI::ServerPI( int socket, QObject *parent , const char* name )
204 : QSocket( parent, name ) , dtp( 0 ), serversocket( 0 ), waitsocket( 0 ) 205 : QSocket( parent, name ) , dtp( 0 ), serversocket( 0 ), waitsocket( 0 )
205{ 206{
206 state = Connected; 207 state = Connected;
207 208
208 setSocket( socket ); 209 setSocket( socket );
209 210
210 peerport = peerPort(); 211 peerport = peerPort();
211 peeraddress = peerAddress(); 212 peeraddress = peerAddress();
212 213
213#ifndef INSECURE 214#ifndef INSECURE
214 if ( !SyncAuthentication::isAuthorized(peeraddress) ) { 215 if ( !SyncAuthentication::isAuthorized(peeraddress) ) {
215 state = Forbidden; 216 state = Forbidden;
216 startTimer( 0 ); 217 startTimer( 0 );
217 } else 218 } else
218 #endif 219#endif
219 { 220 {
220 connect( this, SIGNAL( readyRead() ), SLOT( read() ) ); 221 connect( this, SIGNAL( readyRead() ), SLOT( read() ) );
221 connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); 222 connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) );
222 223
223 passiv = FALSE; 224 passiv = FALSE;
224 for( int i = 0; i < 4; i++ ) 225 for( int i = 0; i < 4; i++ )
225 wait[i] = FALSE; 226 wait[i] = FALSE;
226 227
227 send( "220 Qtopia " QPE_VERSION " FTP Server" ); 228 send( "220 Qtopia " QPE_VERSION " FTP Server" );
228 state = Wait_USER; 229 state = Wait_USER;
229 230
230 dtp = new ServerDTP( this ); 231 dtp = new ServerDTP( this );
231 connect( dtp, SIGNAL( completed() ), SLOT( dtpCompleted() ) ); 232 connect( dtp, SIGNAL( completed() ), SLOT( dtpCompleted() ) );
232 connect( dtp, SIGNAL( failed() ), SLOT( dtpFailed() ) ); 233 connect( dtp, SIGNAL( failed() ), SLOT( dtpFailed() ) );
233 connect( dtp, SIGNAL( error( int ) ), SLOT( dtpError( int ) ) ); 234 connect( dtp, SIGNAL( error( int ) ), SLOT( dtpError( int ) ) );
234 235
235 236
236 directory = QDir::currentDirPath(); 237 directory = QDir::currentDirPath();
237 238
238 static int p = 1024; 239 static int p = 1024;
239 240
240 while ( !serversocket || !serversocket->ok() ) { 241 while ( !serversocket || !serversocket->ok() ) {
241 delete serversocket; 242 delete serversocket;
242 serversocket = new ServerSocket( ++p, this ); 243 serversocket = new ServerSocket( ++p, this );
243 } 244 }
244 connect( serversocket, SIGNAL( newIncomming( int ) ), 245 connect( serversocket, SIGNAL( newIncomming( int ) ),
245 SLOT( newConnection( int ) ) ); 246 SLOT( newConnection( int ) ) );
246 } 247 }
247} 248}
248 249
249ServerPI::~ServerPI() 250ServerPI::~ServerPI()
250{ 251{
251 252
252} 253}
253 254
254void ServerPI::connectionClosed() 255void ServerPI::connectionClosed()
255{ 256{
256 // qDebug( "Debug: Connection closed" ); 257 // qDebug( "Debug: Connection closed" );
257 delete this; 258 delete this;
258} 259}
259 260
260void ServerPI::send( const QString& msg ) 261void ServerPI::send( const QString& msg )
261{ 262{
262 QTextStream os( this ); 263 QTextStream os( this );
263 os << msg << endl; 264 os << msg << endl;
264 //qDebug( "Reply: %s", msg.latin1() ); 265 //qDebug( "Reply: %s", msg.latin1() );
265} 266}
266 267
267void ServerPI::read() 268void ServerPI::read()
268{ 269{
269 while ( canReadLine() ) 270 while ( canReadLine() )
270 process( readLine().stripWhiteSpace() ); 271 process( readLine().stripWhiteSpace() );
271} 272}
272 273
273bool ServerPI::checkReadFile( const QString& file ) 274bool ServerPI::checkReadFile( const QString& file )
274{ 275{
275 QString filename; 276 QString filename;
276 277
277 if ( file[0] != "/" ) 278 if ( file[0] != "/" )
278 filename = directory.path() + "/" + file; 279 filename = directory.path() + "/" + file;
279 else 280 else
280 filename = file; 281 filename = file;
281 282
282 QFileInfo fi( filename ); 283 QFileInfo fi( filename );
283 return ( fi.exists() && fi.isReadable() ); 284 return ( fi.exists() && fi.isReadable() );
284} 285}
285 286
286bool ServerPI::checkWriteFile( const QString& file ) 287bool ServerPI::checkWriteFile( const QString& file )
287{ 288{
288 QString filename; 289 QString filename;
289 290
290 if ( file[0] != "/" ) 291 if ( file[0] != "/" )
291 filename = directory.path() + "/" + file; 292 filename = directory.path() + "/" + file;
292 else 293 else
293 filename = file; 294 filename = file;
294 295
295 QFileInfo fi( filename ); 296 QFileInfo fi( filename );
296 297
297 if ( fi.exists() ) 298 if ( fi.exists() )
298 if ( !QFile( filename ).remove() ) 299 if ( !QFile( filename ).remove() )
299 return FALSE; 300 return FALSE;
300 return TRUE; 301 return TRUE;
301} 302}
302 303
303void ServerPI::process( const QString& message ) 304void ServerPI::process( const QString& message )
304{ 305{
305 //qDebug( "Command: %s", message.latin1() ); 306 //qDebug( "Command: %s", message.latin1() );
306 307
307 // split message using "," as separator 308 // split message using "," as separator
308 QStringList msg = QStringList::split( " ", message ); 309 QStringList msg = QStringList::split( " ", message );
309 if ( msg.isEmpty() ) return; 310 if ( msg.isEmpty() ) return;
310 311
311 // command token 312 // command token
312 QString cmd = msg[0].upper(); 313 QString cmd = msg[0].upper();
313 314
314 // argument token 315 // argument token
315 QString arg; 316 QString arg;
316 if ( msg.count() >= 2 ) 317 if ( msg.count() >= 2 )
317 arg = msg[1]; 318 arg = msg[1];
318 319
319 // full argument string 320 // full argument string
320 QString args; 321 QString args;
321 if ( msg.count() >= 2 ) { 322 if ( msg.count() >= 2 ) {
322 QStringList copy( msg ); 323 QStringList copy( msg );
323 // FIXME: for Qt3 324 // FIXME: for Qt3
324 // copy.pop_front() 325 // copy.pop_front()
325 copy.remove( copy.begin() ); 326 copy.remove( copy.begin() );
326 args = copy.join( " " ); 327 args = copy.join( " " );
327 } 328 }
328 329
329 //qDebug( "args: %s", args.latin1() ); 330 //qDebug( "args: %s", args.latin1() );
330 331
331 // we always respond to QUIT, regardless of state 332 // we always respond to QUIT, regardless of state
332 if ( cmd == "QUIT" ) { 333 if ( cmd == "QUIT" ) {
333 send( "211 Good bye!" ); 334 send( "211 Good bye!" );
334 delete this; 335 delete this;
335 return; 336 return;
336 } 337 }
337 338
338 // connected to client 339 // connected to client
339 if ( Connected == state ) 340 if ( Connected == state )
340 return; 341 return;
341 342
342 // waiting for user name 343 // waiting for user name
343 if ( Wait_USER == state ) { 344 if ( Wait_USER == state ) {
344 345
345 if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) { 346 if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) {
346 send( "530 Please login with USER and PASS" ); 347 send( "530 Please login with USER and PASS" );
347 return; 348 return;
348 } 349 }
349 send( "331 User name ok, need password" ); 350 send( "331 User name ok, need password" );
350 state = Wait_PASS; 351 state = Wait_PASS;
351 return; 352 return;
352 } 353 }
353 354
354 // waiting for password 355 // waiting for password
355 if ( Wait_PASS == state ) { 356 if ( Wait_PASS == state ) {
356 357
357 if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) { 358 if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) {
358 send( "530 Please login with USER and PASS" ); 359 send( "530 Please login with USER and PASS" );
359 return; 360 return;
360 } 361 }
361 send( "230 User logged in, proceed" ); 362 send( "230 User logged in, proceed" );
362 state = Ready; 363 state = Ready;
363 return; 364 return;
364 } 365 }
365 366
366 // ACCESS CONTROL COMMANDS 367 // ACCESS CONTROL COMMANDS
367 368
368 369
369 // account (ACCT) 370 // account (ACCT)
370 if ( cmd == "ACCT" ) { 371 if ( cmd == "ACCT" ) {
371 // even wu-ftp does not support it 372 // even wu-ftp does not support it
372 send( "502 Command not implemented" ); 373 send( "502 Command not implemented" );
373 } 374 }
374 375
375 // change working directory (CWD) 376 // change working directory (CWD)
376 else if ( cmd == "CWD" ) { 377 else if ( cmd == "CWD" ) {
377 378
378 if ( !args.isEmpty() ) { 379 if ( !args.isEmpty() ) {
379 if ( directory.cd( args, TRUE ) ) 380 if ( directory.cd( args, TRUE ) )
380 send( "250 Requested file action okay, completed" ); 381 send( "250 Requested file action okay, completed" );
381 else 382 else
382 send( "550 Requested action not taken" ); 383 send( "550 Requested action not taken" );
383 } 384 }
384 else 385 else
385 send( "500 Syntax error, command unrecognized" ); 386 send( "500 Syntax error, command unrecognized" );
386 } 387 }
387 388
388 // change to parent directory (CDUP) 389 // change to parent directory (CDUP)
389 else if ( cmd == "CDUP" ) { 390 else if ( cmd == "CDUP" ) {
390 if ( directory.cdUp() ) 391 if ( directory.cdUp() )
391 send( "250 Requested file action okay, completed" ); 392 send( "250 Requested file action okay, completed" );
392 else 393 else
393 send( "550 Requested action not taken" ); 394 send( "550 Requested action not taken" );
394 } 395 }
395 396
396 // structure mount (SMNT) 397 // structure mount (SMNT)
397 else if ( cmd == "SMNT" ) { 398 else if ( cmd == "SMNT" ) {
398 // even wu-ftp does not support it 399 // even wu-ftp does not support it
399 send( "502 Command not implemented" ); 400 send( "502 Command not implemented" );
400 } 401 }
401 402
402 // reinitialize (REIN) 403 // reinitialize (REIN)
403 else if ( cmd == "REIN" ) { 404 else if ( cmd == "REIN" ) {
404 // even wu-ftp does not support it 405 // even wu-ftp does not support it
405 send( "502 Command not implemented" ); 406 send( "502 Command not implemented" );
406 } 407 }
407 408
408 409
409 // TRANSFER PARAMETER COMMANDS 410 // TRANSFER PARAMETER COMMANDS
410 411
411 412
412 // data port (PORT) 413 // data port (PORT)
413 else if ( cmd == "PORT" ) { 414 else if ( cmd == "PORT" ) {
414 if ( parsePort( arg ) ) 415 if ( parsePort( arg ) )
415 send( "200 Command okay" ); 416 send( "200 Command okay" );
416 else 417 else
417 send( "500 Syntax error, command unrecognized" ); 418 send( "500 Syntax error, command unrecognized" );
418 } 419 }
419 420
420 // passive (PASV) 421 // passive (PASV)
421 else if ( cmd == "PASV" ) { 422 else if ( cmd == "PASV" ) {
422 passiv = TRUE; 423 passiv = TRUE;
423 send( "227 Entering Passive Mode (" 424 send( "227 Entering Passive Mode ("
424 + address().toString().replace( QRegExp( "\\." ), "," ) + "," 425 + address().toString().replace( QRegExp( "\\." ), "," ) + ","
425 + QString::number( ( serversocket->port() ) >> 8 ) + "," 426 + QString::number( ( serversocket->port() ) >> 8 ) + ","
426 + QString::number( ( serversocket->port() ) & 0xFF ) +")" ); 427 + QString::number( ( serversocket->port() ) & 0xFF ) +")" );
427 } 428 }
428 429
429 // representation type (TYPE) 430 // representation type (TYPE)
430 else if ( cmd == "TYPE" ) { 431 else if ( cmd == "TYPE" ) {
431 if ( arg.upper() == "A" || arg.upper() == "I" ) 432 if ( arg.upper() == "A" || arg.upper() == "I" )
432 send( "200 Command okay" ); 433 send( "200 Command okay" );
433 else 434 else
434 send( "504 Command not implemented for that parameter" ); 435 send( "504 Command not implemented for that parameter" );
435 } 436 }
436 437
437 // file structure (STRU) 438 // file structure (STRU)
438 else if ( cmd == "STRU" ) { 439 else if ( cmd == "STRU" ) {
439 if ( arg.upper() == "F" ) 440 if ( arg.upper() == "F" )
440 send( "200 Command okay" ); 441 send( "200 Command okay" );
441 else 442 else
442 send( "504 Command not implemented for that parameter" ); 443 send( "504 Command not implemented for that parameter" );
443 } 444 }
444 445
445 // transfer mode (MODE) 446 // transfer mode (MODE)
446 else if ( cmd == "MODE" ) { 447 else if ( cmd == "MODE" ) {
447 if ( arg.upper() == "S" ) 448 if ( arg.upper() == "S" )
448 send( "200 Command okay" ); 449 send( "200 Command okay" );
449 else 450 else
450 send( "504 Command not implemented for that parameter" ); 451 send( "504 Command not implemented for that parameter" );
451 } 452 }
452 453
453 454
454 // FTP SERVICE COMMANDS 455 // FTP SERVICE COMMANDS
455 456
456 457
457 // retrieve (RETR) 458 // retrieve (RETR)
458 else if ( cmd == "RETR" ) 459 else if ( cmd == "RETR" )
459 if ( !args.isEmpty() && checkReadFile( absFilePath( args ) ) 460 if ( !args.isEmpty() && checkReadFile( absFilePath( args ) )
460 || backupRestoreGzip( absFilePath( args ) ) ) { 461 || backupRestoreGzip( absFilePath( args ) ) ) {
461 send( "150 File status okay" ); 462 send( "150 File status okay" );
462 sendFile( absFilePath( args ) ); 463 sendFile( absFilePath( args ) );
463 } 464 }
464 else { 465 else {
465 qDebug("550 Requested action not taken"); 466 qDebug("550 Requested action not taken");
466 send( "550 Requested action not taken" ); 467 send( "550 Requested action not taken" );
467 } 468 }
468 469
469 // store (STOR) 470 // store (STOR)
470 else if ( cmd == "STOR" ) 471 else if ( cmd == "STOR" )
471 if ( !args.isEmpty() && checkWriteFile( absFilePath( args ) ) ) { 472 if ( !args.isEmpty() && checkWriteFile( absFilePath( args ) ) ) {
472 send( "150 File status okay" ); 473 send( "150 File status okay" );
473 retrieveFile( absFilePath( args ) ); 474 retrieveFile( absFilePath( args ) );
474 } 475 }
475 else 476 else
476 send( "550 Requested action not taken" ); 477 send( "550 Requested action not taken" );
477 478
478 // store unique (STOU) 479 // store unique (STOU)
479 else if ( cmd == "STOU" ) { 480 else if ( cmd == "STOU" ) {
480 send( "502 Command not implemented" ); 481 send( "502 Command not implemented" );
481 } 482 }
482 483
483 // append (APPE) 484 // append (APPE)
484 else if ( cmd == "APPE" ) { 485 else if ( cmd == "APPE" ) {
485 send( "502 Command not implemented" ); 486 send( "502 Command not implemented" );
486 } 487 }
487 488
488 // allocate (ALLO) 489 // allocate (ALLO)
489 else if ( cmd == "ALLO" ) { 490 else if ( cmd == "ALLO" ) {
490 send( "200 Command okay" ); 491 send( "200 Command okay" );
491 } 492 }
492 493
493 // restart (REST) 494 // restart (REST)
494 else if ( cmd == "REST" ) { 495 else if ( cmd == "REST" ) {
495 send( "502 Command not implemented" ); 496 send( "502 Command not implemented" );
496 } 497 }
497 498
498 // rename from (RNFR) 499 // rename from (RNFR)
499 else if ( cmd == "RNFR" ) { 500 else if ( cmd == "RNFR" ) {
500 renameFrom = QString::null; 501 renameFrom = QString::null;
501 if ( args.isEmpty() ) 502 if ( args.isEmpty() )
502 send( "500 Syntax error, command unrecognized" ); 503 send( "500 Syntax error, command unrecognized" );
503 else { 504 else {
504 QFile file( absFilePath( args ) ); 505 QFile file( absFilePath( args ) );
505 if ( file.exists() ) { 506 if ( file.exists() ) {
506 send( "350 File exists, ready for destination name" ); 507 send( "350 File exists, ready for destination name" );
507 renameFrom = absFilePath( args ); 508 renameFrom = absFilePath( args );
508 } 509 }
509 else 510 else
510 send( "550 Requested action not taken" ); 511 send( "550 Requested action not taken" );
511 } 512 }
512 } 513 }
513 514
514 // rename to (RNTO) 515 // rename to (RNTO)
515 else if ( cmd == "RNTO" ) { 516 else if ( cmd == "RNTO" ) {
516 if ( lastCommand != "RNFR" ) 517 if ( lastCommand != "RNFR" )
517 send( "503 Bad sequence of commands" ); 518 send( "503 Bad sequence of commands" );
518 else if ( args.isEmpty() ) 519 else if ( args.isEmpty() )
519 send( "500 Syntax error, command unrecognized" ); 520 send( "500 Syntax error, command unrecognized" );
520 else { 521 else {
521 QDir dir( absFilePath( args ) ); 522 QDir dir( absFilePath( args ) );
522 if ( dir.rename( renameFrom, absFilePath( args ), TRUE ) ) 523 if ( dir.rename( renameFrom, absFilePath( args ), TRUE ) )
523 send( "250 Requested file action okay, completed." ); 524 send( "250 Requested file action okay, completed." );
524 else 525 else
525 send( "550 Requested action not taken" ); 526 send( "550 Requested action not taken" );
526 } 527 }
527 } 528 }
528 529
529 // abort (ABOR) 530 // abort (ABOR)
530 else if ( cmd.contains( "ABOR" ) ) { 531 else if ( cmd.contains( "ABOR" ) ) {
531 dtp->close(); 532 dtp->close();
532 if ( dtp->dtpMode() != ServerDTP::Idle ) 533 if ( dtp->dtpMode() != ServerDTP::Idle )
533 send( "426 Connection closed; transfer aborted" ); 534 send( "426 Connection closed; transfer aborted" );
534 else 535 else
535 send( "226 Closing data connection" ); 536 send( "226 Closing data connection" );
536 } 537 }
537 538
538 // delete (DELE) 539 // delete (DELE)
539 else if ( cmd == "DELE" ) { 540 else if ( cmd == "DELE" ) {
540 if ( args.isEmpty() ) 541 if ( args.isEmpty() )
541 send( "500 Syntax error, command unrecognized" ); 542 send( "500 Syntax error, command unrecognized" );
542 else { 543 else {
543 QFile file( absFilePath( args ) ) ; 544 QFile file( absFilePath( args ) ) ;
544 if ( file.remove() ) { 545 if ( file.remove() ) {
545 send( "250 Requested file action okay, completed" ); 546 send( "250 Requested file action okay, completed" );
546 QCopEnvelope e("QPE/System", "linkChanged(QString)" ); 547 QCopEnvelope e("QPE/System", "linkChanged(QString)" );
547 e << file.name(); 548 e << file.name();
548 } else { 549 } else {
549 send( "550 Requested action not taken" ); 550 send( "550 Requested action not taken" );
550 } 551 }
551 } 552 }
552 } 553 }
553 554
554 // remove directory (RMD) 555 // remove directory (RMD)
555 else if ( cmd == "RMD" ) { 556 else if ( cmd == "RMD" ) {
556 if ( args.isEmpty() ) 557 if ( args.isEmpty() )
557 send( "500 Syntax error, command unrecognized" ); 558 send( "500 Syntax error, command unrecognized" );
558 else { 559 else {
559 QDir dir; 560 QDir dir;
560 if ( dir.rmdir( absFilePath( args ), TRUE ) ) 561 if ( dir.rmdir( absFilePath( args ), TRUE ) )
561 send( "250 Requested file action okay, completed" ); 562 send( "250 Requested file action okay, completed" );
562 else 563 else
563 send( "550 Requested action not taken" ); 564 send( "550 Requested action not taken" );
564 } 565 }
565 } 566 }
566 567
567 // make directory (MKD) 568 // make directory (MKD)
568 else if ( cmd == "MKD" ) { 569 else if ( cmd == "MKD" ) {
569 if ( args.isEmpty() ) { 570 if ( args.isEmpty() ) {
570 qDebug(" Error: no arg"); 571 qDebug(" Error: no arg");
571 send( "500 Syntax error, command unrecognized" ); 572 send( "500 Syntax error, command unrecognized" );
572 } 573 }
573 else { 574 else {
574 QDir dir; 575 QDir dir;
575 if ( dir.mkdir( absFilePath( args ), TRUE ) ) 576 if ( dir.mkdir( absFilePath( args ), TRUE ) )
576 send( "250 Requested file action okay, completed." ); 577 send( "250 Requested file action okay, completed." );
577 else 578 else
578 send( "550 Requested action not taken" ); 579 send( "550 Requested action not taken" );
579 } 580 }
580 } 581 }
581 582
582 // print working directory (PWD) 583 // print working directory (PWD)
583 else if ( cmd == "PWD" ) { 584 else if ( cmd == "PWD" ) {
584 send( "257 \"" + directory.path() +"\"" ); 585 send( "257 \"" + directory.path() +"\"" );
585 } 586 }
586 587
587 // list (LIST) 588 // list (LIST)
588 else if ( cmd == "LIST" ) { 589 else if ( cmd == "LIST" ) {
589 if ( sendList( absFilePath( args ) ) ) 590 if ( sendList( absFilePath( args ) ) )
590 send( "150 File status okay" ); 591 send( "150 File status okay" );
591 else 592 else
592 send( "500 Syntax error, command unrecognized" ); 593 send( "500 Syntax error, command unrecognized" );
593 } 594 }
594 595
595 // size (SIZE) 596 // size (SIZE)
596 else if ( cmd == "SIZE" ) { 597 else if ( cmd == "SIZE" ) {
597 QString filePath = absFilePath( args ); 598 QString filePath = absFilePath( args );
598 QFileInfo fi( filePath ); 599 QFileInfo fi( filePath );
599 bool gzipfile = backupRestoreGzip( filePath ); 600 bool gzipfile = backupRestoreGzip( filePath );
600 if ( !fi.exists() && !gzipfile ) 601 if ( !fi.exists() && !gzipfile )
601 send( "500 Syntax error, command unrecognized" ); 602 send( "500 Syntax error, command unrecognized" );
602 else { 603 else {
603 if ( !gzipfile ) 604 if ( !gzipfile )
604 send( "213 " + QString::number( fi.size() ) ); 605 send( "213 " + QString::number( fi.size() ) );
605 else { 606 else {
606 Process duproc( QString("du") ); 607 Process duproc( QString("du") );
607 duproc.addArgument("-s"); 608 duproc.addArgument("-s");
608 QString in, out; 609 QString in, out;
609 if ( !duproc.exec(in, out) ) { 610 if ( !duproc.exec(in, out) ) {
610 qDebug("du process failed; just sending back 1K"); 611 qDebug("du process failed; just sending back 1K");
611 send( "213 1024"); 612 send( "213 1024");
612 } 613 }
613 else { 614 else {
614 QString size = out.left( out.find("\t") ); 615 QString size = out.left( out.find("\t") );
615 int guess = size.toInt()/5; 616 int guess = size.toInt()/5;
616 if ( filePath.contains("doc") ) 617 if ( filePath.contains("doc") )
617 guess *= 1000; 618 guess *= 1000;
618 qDebug("sending back gzip guess of %d", guess); 619 qDebug("sending back gzip guess of %d", guess);
619 send( "213 " + QString::number(guess) ); 620 send( "213 " + QString::number(guess) );
620 } 621 }
621 } 622 }
622 } 623 }
623 } 624 }
624 // name list (NLST) 625 // name list (NLST)
625 else if ( cmd == "NLST" ) { 626 else if ( cmd == "NLST" ) {
626 send( "502 Command not implemented" ); 627 send( "502 Command not implemented" );
627 } 628 }
628 629
629 // site parameters (SITE) 630 // site parameters (SITE)
630 else if ( cmd == "SITE" ) { 631 else if ( cmd == "SITE" ) {
631 send( "502 Command not implemented" ); 632 send( "502 Command not implemented" );
632 } 633 }
633 634
634 // system (SYST) 635 // system (SYST)
635 else if ( cmd == "SYST" ) { 636 else if ( cmd == "SYST" ) {
636 send( "215 UNIX Type: L8" ); 637 send( "215 UNIX Type: L8" );
637 } 638 }
638 639
639 // status (STAT) 640 // status (STAT)
640 else if ( cmd == "STAT" ) { 641 else if ( cmd == "STAT" ) {
641 send( "502 Command not implemented" ); 642 send( "502 Command not implemented" );
642 } 643 }
643 644
644 // help (HELP ) 645 // help (HELP )
645 else if ( cmd == "HELP" ) { 646 else if ( cmd == "HELP" ) {
646 send( "502 Command not implemented" ); 647 send( "502 Command not implemented" );
647 } 648 }
648 649
649 // noop (NOOP) 650 // noop (NOOP)
650 else if ( cmd == "NOOP" ) { 651 else if ( cmd == "NOOP" ) {
651 send( "200 Command okay" ); 652 send( "200 Command okay" );
652 } 653 }
653 654
654 // not implemented 655 // not implemented
655 else 656 else
656 send( "502 Command not implemented" ); 657 send( "502 Command not implemented" );
657 658
658 lastCommand = cmd; 659 lastCommand = cmd;
659} 660}
660 661
661bool ServerPI::backupRestoreGzip( const QString &file ) 662bool ServerPI::backupRestoreGzip( const QString &file )
662{ 663{
663 return (file.find( "backup" ) != -1 && 664 return (file.find( "backup" ) != -1 &&
664 file.findRev( ".tgz" ) == (int)file.length()-4 ); 665 file.findRev( ".tgz" ) == (int)file.length()-4 );
665} 666}
666 667
667bool ServerPI::backupRestoreGzip( const QString &file, QStringList &targets ) 668bool ServerPI::backupRestoreGzip( const QString &file, QStringList &targets )
668{ 669{
669 if ( file.find( "backup" ) != -1 && 670 if ( file.find( "backup" ) != -1 &&
670 file.findRev( ".tgz" ) == (int)file.length()-4 ) { 671 file.findRev( ".tgz" ) == (int)file.length()-4 ) {
671 QFileInfo info( file ); 672 QFileInfo info( file );
672 targets = info.dirPath( TRUE ); 673 targets = info.dirPath( TRUE );
673 qDebug("ServerPI::backupRestoreGzip for %s = %s", file.latin1(), 674 qDebug("ServerPI::backupRestoreGzip for %s = %s", file.latin1(),
674 targets.join(" ").latin1() ); 675 targets.join(" ").latin1() );
675 return true; 676 return true;
676 } 677 }
677 return false; 678 return false;
678} 679}
679 680
680void ServerPI::sendFile( const QString& file ) 681void ServerPI::sendFile( const QString& file )
681{ 682{
682 if ( passiv ) { 683 if ( passiv ) {
683 wait[SendFile] = TRUE; 684 wait[SendFile] = TRUE;
684 waitfile = file; 685 waitfile = file;
685 if ( waitsocket ) 686 if ( waitsocket )
686 newConnection( waitsocket ); 687 newConnection( waitsocket );
687 } 688 }
688 else { 689 else {
689 QStringList targets; 690 QStringList targets;
690 if ( backupRestoreGzip( file, targets ) ) 691 if ( backupRestoreGzip( file, targets ) )
691 dtp->sendGzipFile( file, targets, peeraddress, peerport ); 692 dtp->sendGzipFile( file, targets, peeraddress, peerport );
692 else dtp->sendFile( file, peeraddress, peerport ); 693 else dtp->sendFile( file, peeraddress, peerport );
693 } 694 }
694} 695}
695 696
696void ServerPI::retrieveFile( const QString& file ) 697void ServerPI::retrieveFile( const QString& file )
697{ 698{
698 if ( passiv ) { 699 if ( passiv ) {
699 wait[RetrieveFile] = TRUE; 700 wait[RetrieveFile] = TRUE;
700 waitfile = file; 701 waitfile = file;
701 if ( waitsocket ) 702 if ( waitsocket )
702 newConnection( waitsocket ); 703 newConnection( waitsocket );
703 } 704 }
704 else { 705 else {
705 QStringList targets; 706 QStringList targets;
706 if ( backupRestoreGzip( file, targets ) ) 707 if ( backupRestoreGzip( file, targets ) )
707 dtp->retrieveGzipFile( file, peeraddress, peerport ); 708 dtp->retrieveGzipFile( file, peeraddress, peerport );
708 else 709 else
709 dtp->retrieveFile( file, peeraddress, peerport ); 710 dtp->retrieveFile( file, peeraddress, peerport );
710 } 711 }
711} 712}
712 713
713bool ServerPI::parsePort( const QString& pp ) 714bool ServerPI::parsePort( const QString& pp )
714{ 715{
715 QStringList p = QStringList::split( ",", pp ); 716 QStringList p = QStringList::split( ",", pp );
716 if ( p.count() != 6 ) return FALSE; 717 if ( p.count() != 6 ) return FALSE;
717 718
718 // h1,h2,h3,h4,p1,p2 719 // h1,h2,h3,h4,p1,p2
719 peeraddress = QHostAddress( ( p[0].toInt() << 24 ) + ( p[1].toInt() << 16 ) + 720 peeraddress = QHostAddress( ( p[0].toInt() << 24 ) + ( p[1].toInt() << 16 ) +
720 ( p[2].toInt() << 8 ) + p[3].toInt() ); 721 ( p[2].toInt() << 8 ) + p[3].toInt() );
721 peerport = ( p[4].toInt() << 8 ) + p[5].toInt(); 722 peerport = ( p[4].toInt() << 8 ) + p[5].toInt();
722 return TRUE; 723 return TRUE;
723} 724}
724 725
725void ServerPI::dtpCompleted() 726void ServerPI::dtpCompleted()
726{ 727{
727 send( "226 Closing data connection, file transfer successful" ); 728 send( "226 Closing data connection, file transfer successful" );
728 if ( dtp->dtpMode() == ServerDTP::RetrieveFile ) { 729 if ( dtp->dtpMode() == ServerDTP::RetrieveFile ) {
729 QString fn = dtp->fileName(); 730 QString fn = dtp->fileName();
730 if ( fn.right(8)==".desktop" && fn.find("/Documents/")>=0 ) { 731 if ( fn.right(8)==".desktop" && fn.find("/Documents/")>=0 ) {
731 QCopEnvelope e("QPE/System", "linkChanged(QString)" ); 732 QCopEnvelope e("QPE/System", "linkChanged(QString)" );
732 e << fn; 733 e << fn;
733 } 734 }
734 } 735 }
735 waitsocket = 0; 736 waitsocket = 0;
736 dtp->close(); 737 dtp->close();
737} 738}
738 739
739void ServerPI::dtpFailed() 740void ServerPI::dtpFailed()
740{ 741{
741 dtp->close(); 742 dtp->close();
742 waitsocket = 0; 743 waitsocket = 0;
743 send( "451 Requested action aborted: local error in processing" ); 744 send( "451 Requested action aborted: local error in processing" );
744} 745}
745 746
746void ServerPI::dtpError( int ) 747void ServerPI::dtpError( int )
747{ 748{
748 dtp->close(); 749 dtp->close();
749 waitsocket = 0; 750 waitsocket = 0;
750 send( "451 Requested action aborted: local error in processing" ); 751 send( "451 Requested action aborted: local error in processing" );
751} 752}
752 753
753bool ServerPI::sendList( const QString& arg ) 754bool ServerPI::sendList( const QString& arg )
754{ 755{
755 QByteArray listing; 756 QByteArray listing;
756 QBuffer buffer( listing ); 757 QBuffer buffer( listing );
757 758
758 if ( !buffer.open( IO_WriteOnly ) ) 759 if ( !buffer.open( IO_WriteOnly ) )
759 return FALSE; 760 return FALSE;
760 761
761 QTextStream ts( &buffer ); 762 QTextStream ts( &buffer );
762 QString fn = arg; 763 QString fn = arg;
763 764
764 if ( fn.isEmpty() ) 765 if ( fn.isEmpty() )
765 fn = directory.path(); 766 fn = directory.path();
766 767
767 QFileInfo fi( fn ); 768 QFileInfo fi( fn );
768 if ( !fi.exists() ) return FALSE; 769 if ( !fi.exists() ) return FALSE;
769 770
770 // return file listing 771 // return file listing
771 if ( fi.isFile() ) { 772 if ( fi.isFile() ) {
772 ts << fileListing( &fi ) << endl; 773 ts << fileListing( &fi ) << endl;
773 } 774 }
774 775
775 // return directory listing 776 // return directory listing
776 else if ( fi.isDir() ) { 777 else if ( fi.isDir() ) {
777 QDir dir( fn ); 778 QDir dir( fn );
778 const QFileInfoList *list = dir.entryInfoList( QDir::All | QDir::Hidden ); 779 const QFileInfoList *list = dir.entryInfoList( QDir::All | QDir::Hidden );
779 780
780 QFileInfoListIterator it( *list ); 781 QFileInfoListIterator it( *list );
781 QFileInfo *info; 782 QFileInfo *info;
782 783
783 unsigned long total = 0; 784 unsigned long total = 0;
784 while ( ( info = it.current() ) ) { 785 while ( ( info = it.current() ) ) {
785 if ( info->fileName() != "." && info->fileName() != ".." ) 786 if ( info->fileName() != "." && info->fileName() != ".." )
786 total += info->size(); 787 total += info->size();
787 ++it; 788 ++it;
788 } 789 }
789 790
790 ts << "total " << QString::number( total / 1024 ) << endl; 791 ts << "total " << QString::number( total / 1024 ) << endl;
791 792
792 it.toFirst(); 793 it.toFirst();
793 while ( ( info = it.current() ) ) { 794 while ( ( info = it.current() ) ) {
794 if ( info->fileName() == "." || info->fileName() == ".." ) { 795 if ( info->fileName() == "." || info->fileName() == ".." ) {
795 ++it; 796 ++it;
796 continue; 797 continue;
797 } 798 }
798 ts << fileListing( info ) << endl; 799 ts << fileListing( info ) << endl;
799 ++it; 800 ++it;
800 } 801 }
801 } 802 }
802 803
803 if ( passiv ) { 804 if ( passiv ) {
804 waitarray = buffer.buffer(); 805 waitarray = buffer.buffer();
805 wait[SendByteArray] = TRUE; 806 wait[SendByteArray] = TRUE;
806 if ( waitsocket ) 807 if ( waitsocket )
807 newConnection( waitsocket ); 808 newConnection( waitsocket );
808 } 809 }
809 else 810 else
810 dtp->sendByteArray( buffer.buffer(), peeraddress, peerport ); 811 dtp->sendByteArray( buffer.buffer(), peeraddress, peerport );
811 return TRUE; 812 return TRUE;
812} 813}
813 814
814QString ServerPI::fileListing( QFileInfo *info ) 815QString ServerPI::fileListing( QFileInfo *info )
815{ 816{
816 if ( !info ) return QString::null; 817 if ( !info ) return QString::null;
817 QString s; 818 QString s;
818 819
819 // type char 820 // type char
820 if ( info->isDir() ) 821 if ( info->isDir() )
821 s += "d"; 822 s += "d";
822 else if ( info->isSymLink() ) 823 else if ( info->isSymLink() )
823 s += "l"; 824 s += "l";
824 else 825 else
825 s += "-"; 826 s += "-";
826 827
827 // permisson string 828 // permisson string
828 s += permissionString( info ) + " "; 829 s += permissionString( info ) + " ";
829 830
830 // number of hardlinks 831 // number of hardlinks
831 int subdirs = 1; 832 int subdirs = 1;
832 833
833 if ( info->isDir() ) 834 if ( info->isDir() )
834 subdirs = 2; 835 subdirs = 2;
835 // FIXME : this is to slow 836 // FIXME : this is to slow
836 //if ( info->isDir() ) 837 //if ( info->isDir() )
837 //subdirs = QDir( info->absFilePath() ).entryList( QDir::Dirs ).count(); 838 //subdirs = QDir( info->absFilePath() ).entryList( QDir::Dirs ).count();
838 839
839 s += QString::number( subdirs ).rightJustify( 3, ' ', TRUE ) + " "; 840 s += QString::number( subdirs ).rightJustify( 3, ' ', TRUE ) + " ";
840 841
841 // owner 842 // owner
842 s += info->owner().leftJustify( 8, ' ', TRUE ) + " "; 843 s += info->owner().leftJustify( 8, ' ', TRUE ) + " ";
843 844
844 // group 845 // group
845 s += info->group().leftJustify( 8, ' ', TRUE ) + " "; 846 s += info->group().leftJustify( 8, ' ', TRUE ) + " ";
846 847
847 // file size in bytes 848 // file size in bytes
848 s += QString::number( info->size() ).rightJustify( 9, ' ', TRUE ) + " "; 849 s += QString::number( info->size() ).rightJustify( 9, ' ', TRUE ) + " ";
849 850
850 // last modified date 851 // last modified date
851 QDate date = info->lastModified().date(); 852 QDate date = info->lastModified().date();
852 QTime time = info->lastModified().time(); 853 QTime time = info->lastModified().time();
853 s += date.monthName( date.month() ) + " " 854 s += date.monthName( date.month() ) + " "
854 + QString::number( date.day() ).rightJustify( 2, ' ', TRUE ) + " " 855 + QString::number( date.day() ).rightJustify( 2, ' ', TRUE ) + " "
855 + QString::number( time.hour() ).rightJustify( 2, '0', TRUE ) + ":" 856 + QString::number( time.hour() ).rightJustify( 2, '0', TRUE ) + ":"
856 + QString::number( time.minute() ).rightJustify( 2,'0', TRUE ) + " "; 857 + QString::number( time.minute() ).rightJustify( 2,'0', TRUE ) + " ";
857 858
858 // file name 859 // file name
859 s += info->fileName(); 860 s += info->fileName();
860 861
861 return s; 862 return s;
862} 863}
863 864
864QString ServerPI::permissionString( QFileInfo *info ) 865QString ServerPI::permissionString( QFileInfo *info )
865{ 866{
866 if ( !info ) return QString( "---------" ); 867 if ( !info ) return QString( "---------" );
867 QString s; 868 QString s;
868 869
869 // user 870 // user
870 if ( info->permission( QFileInfo::ReadUser ) ) s += "r"; 871 if ( info->permission( QFileInfo::ReadUser ) ) s += "r";
871 else s += "-"; 872 else s += "-";
872 if ( info->permission( QFileInfo::WriteUser ) ) s += "w"; 873 if ( info->permission( QFileInfo::WriteUser ) ) s += "w";
873 else s += "-"; 874 else s += "-";
874 if ( info->permission( QFileInfo::ExeUser ) ) s += "x"; 875 if ( info->permission( QFileInfo::ExeUser ) ) s += "x";
875 else s += "-"; 876 else s += "-";
876 877
877 // group 878 // group
878 if ( info->permission( QFileInfo::ReadGroup ) ) s += "r"; 879 if ( info->permission( QFileInfo::ReadGroup ) ) s += "r";
879 else s += "-"; 880 else s += "-";
880 if ( info->permission( QFileInfo::WriteGroup ) )s += "w"; 881 if ( info->permission( QFileInfo::WriteGroup ) )s += "w";
881 else s += "-"; 882 else s += "-";
882 if ( info->permission( QFileInfo::ExeGroup ) ) s += "x"; 883 if ( info->permission( QFileInfo::ExeGroup ) ) s += "x";
883 else s += "-"; 884 else s += "-";
884 885
885 // exec 886 // exec
886 if ( info->permission( QFileInfo::ReadOther ) ) s += "r"; 887 if ( info->permission( QFileInfo::ReadOther ) ) s += "r";
887 else s += "-"; 888 else s += "-";
888 if ( info->permission( QFileInfo::WriteOther ) ) s += "w"; 889 if ( info->permission( QFileInfo::WriteOther ) ) s += "w";
889 else s += "-"; 890 else s += "-";
890 if ( info->permission( QFileInfo::ExeOther ) ) s += "x"; 891 if ( info->permission( QFileInfo::ExeOther ) ) s += "x";
891 else s += "-"; 892 else s += "-";
892 893
893 return s; 894 return s;
894} 895}
895 896
896void ServerPI::newConnection( int socket ) 897void ServerPI::newConnection( int socket )
897{ 898{
898 //qDebug( "New incomming connection" ); 899 //qDebug( "New incomming connection" );
899 900
900 if ( !passiv ) return; 901 if ( !passiv ) return;
901 902
902 if ( wait[SendFile] ) { 903 if ( wait[SendFile] ) {
903 QStringList targets; 904 QStringList targets;
904 if ( backupRestoreGzip( waitfile, targets ) ) 905 if ( backupRestoreGzip( waitfile, targets ) )
905 dtp->sendGzipFile( waitfile, targets ); 906 dtp->sendGzipFile( waitfile, targets );
906 else 907 else
907 dtp->sendFile( waitfile ); 908 dtp->sendFile( waitfile );
908 dtp->setSocket( socket ); 909 dtp->setSocket( socket );
909 } 910 }
910 else if ( wait[RetrieveFile] ) { 911 else if ( wait[RetrieveFile] ) {
911 qDebug("check retrieve file"); 912 qDebug("check retrieve file");
912 if ( backupRestoreGzip( waitfile ) ) 913 if ( backupRestoreGzip( waitfile ) )
913 dtp->retrieveGzipFile( waitfile ); 914 dtp->retrieveGzipFile( waitfile );
914 else 915 else
915 dtp->retrieveFile( waitfile ); 916 dtp->retrieveFile( waitfile );
916 dtp->setSocket( socket ); 917 dtp->setSocket( socket );
917 } 918 }
918 else if ( wait[SendByteArray] ) { 919 else if ( wait[SendByteArray] ) {
919 dtp->sendByteArray( waitarray ); 920 dtp->sendByteArray( waitarray );
920 dtp->setSocket( socket ); 921 dtp->setSocket( socket );
921 } 922 }
922 else if ( wait[RetrieveByteArray] ) { 923 else if ( wait[RetrieveByteArray] ) {
923 qDebug("retrieve byte array"); 924 qDebug("retrieve byte array");
924 dtp->retrieveByteArray(); 925 dtp->retrieveByteArray();
925 dtp->setSocket( socket ); 926 dtp->setSocket( socket );
926 } 927 }
927 else 928 else
928 waitsocket = socket; 929 waitsocket = socket;
929 930
930 for( int i = 0; i < 4; i++ ) 931 for( int i = 0; i < 4; i++ )
931 wait[i] = FALSE; 932 wait[i] = FALSE;
932} 933}
933 934
934QString ServerPI::absFilePath( const QString& file ) 935QString ServerPI::absFilePath( const QString& file )
935{ 936{
936 if ( file.isEmpty() ) return file; 937 if ( file.isEmpty() ) return file;
937 938
938 QString filepath( file ); 939 QString filepath( file );
939 if ( file[0] != "/" ) 940 if ( file[0] != "/" )
940 filepath = directory.path() + "/" + file; 941 filepath = directory.path() + "/" + file;
941 942
942 return filepath; 943 return filepath;
943} 944}
944 945
945 946
946void ServerPI::timerEvent( QTimerEvent * ) 947void ServerPI::timerEvent( QTimerEvent * )
947{ 948{
948 connectionClosed(); 949 connectionClosed();
949} 950}
950 951
951 952
952ServerDTP::ServerDTP( QObject *parent = 0, const char* name = 0) 953ServerDTP::ServerDTP( QObject *parent = 0, const char* name = 0)
953 : QSocket( parent, name ), mode( Idle ), createTargzProc( 0 ), 954 : QSocket( parent, name ), mode( Idle ), createTargzProc( 0 ),
954retrieveTargzProc( 0 ), gzipProc( 0 ) 955retrieveTargzProc( 0 ), gzipProc( 0 )
955{ 956{
956 957
957 connect( this, SIGNAL( connected() ), SLOT( connected() ) ); 958 connect( this, SIGNAL( connected() ), SLOT( connected() ) );
958 connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); 959 connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) );
959 connect( this, SIGNAL( bytesWritten( int ) ), SLOT( bytesWritten( int ) ) ); 960 connect( this, SIGNAL( bytesWritten( int ) ), SLOT( bytesWritten( int ) ) );
960 connect( this, SIGNAL( readyRead() ), SLOT( readyRead() ) ); 961 connect( this, SIGNAL( readyRead() ), SLOT( readyRead() ) );
961 962
962 gzipProc = new QProcess( this, "gzipProc" ); 963 gzipProc = new QProcess( this, "gzipProc" );
963 gzipProc->setCommunication( QProcess::Stdin | QProcess::Stdout ); 964 gzipProc->setCommunication( QProcess::Stdin | QProcess::Stdout );
964 965
965 createTargzProc = new QProcess( QString("tar"), this, "createTargzProc"); 966 createTargzProc = new QProcess( QString("tar"), this, "createTargzProc");
966 createTargzProc->setCommunication( QProcess::Stdout ); 967 createTargzProc->setCommunication( QProcess::Stdout );
967 createTargzProc->setWorkingDirectory( QDir::rootDirPath() ); 968 createTargzProc->setWorkingDirectory( QDir::rootDirPath() );
968 connect( createTargzProc, SIGNAL( processExited() ), SLOT( targzDone() ) ); 969 connect( createTargzProc, SIGNAL( processExited() ), SLOT( targzDone() ) );
969 970
970 QStringList args = "tar"; 971 QStringList args = "tar";
971 args += "-xv"; 972 args += "-xv";
972 retrieveTargzProc = new QProcess( args, this, "retrieveTargzProc" ); 973 retrieveTargzProc = new QProcess( args, this, "retrieveTargzProc" );
973 retrieveTargzProc->setCommunication( QProcess::Stdin ); 974 retrieveTargzProc->setCommunication( QProcess::Stdin );
974 retrieveTargzProc->setWorkingDirectory( QDir::rootDirPath() ); 975 retrieveTargzProc->setWorkingDirectory( QDir::rootDirPath() );
975 connect( retrieveTargzProc, SIGNAL( processExited() ), 976 connect( retrieveTargzProc, SIGNAL( processExited() ),
976 SIGNAL( completed() ) ); 977 SIGNAL( completed() ) );
977 connect( retrieveTargzProc, SIGNAL( processExited() ), 978 connect( retrieveTargzProc, SIGNAL( processExited() ),
978 SLOT( extractTarDone() ) ); 979 SLOT( extractTarDone() ) );
979} 980}
980 981
981ServerDTP::~ServerDTP() 982ServerDTP::~ServerDTP()
982{ 983{
983 buf.close(); 984 buf.close();
984 file.close(); 985 file.close();
985 createTargzProc->kill(); 986 createTargzProc->kill();
986} 987}
987 988
988void ServerDTP::extractTarDone() 989void ServerDTP::extractTarDone()
989{ 990{
990 qDebug("extract done"); 991 qDebug("extract done");
991#ifndef QT_NO_COP 992#ifndef QT_NO_COP
992 QCopEnvelope e( "QPE/Desktop", "restoreDone(QString)" ); 993 QCopEnvelope e( "QPE/Desktop", "restoreDone(QString)" );
993 e << file.name(); 994 e << file.name();
994#endif 995#endif
995} 996}
996 997
997void ServerDTP::connected() 998void ServerDTP::connected()
998{ 999{
999 // send file mode 1000 // send file mode
1000 switch ( mode ) { 1001 switch ( mode ) {
1001 case SendFile : 1002 case SendFile :
1002 if ( !file.exists() || !file.open( IO_ReadOnly) ) { 1003 if ( !file.exists() || !file.open( IO_ReadOnly) ) {
1003 emit failed(); 1004 emit failed();
1004 mode = Idle; 1005 mode = Idle;
1005 return; 1006 return;
1006 } 1007 }
1007 1008
1008 //qDebug( "Debug: Sending file '%s'", file.name().latin1() ); 1009 //qDebug( "Debug: Sending file '%s'", file.name().latin1() );
1009 1010
1010 bytes_written = 0; 1011 bytes_written = 0;
1011 if ( file.size() == 0 ) { 1012 if ( file.size() == 0 ) {
1012 //make sure it doesn't hang on empty files 1013 //make sure it doesn't hang on empty files
1013 file.close(); 1014 file.close();
1014 emit completed(); 1015 emit completed();
1015 mode = Idle; 1016 mode = Idle;
1016 } else { 1017 } else {
1017 1018
1018 if( !file.atEnd() ) { 1019 if( !file.atEnd() ) {
1019 QCString s; 1020 QCString s;
1020 s.resize( block_size ); 1021 s.resize( block_size );
1021 int bytes = file.readBlock( s.data(), block_size ); 1022 int bytes = file.readBlock( s.data(), block_size );
1022 writeBlock( s.data(), bytes ); 1023 writeBlock( s.data(), bytes );
1023 } 1024 }
1024 } 1025 }
1025 break; 1026 break;
1026 case SendGzipFile: 1027 case SendGzipFile:
1027 if ( createTargzProc->isRunning() ) { 1028 if ( createTargzProc->isRunning() ) {
1028 // SHOULDN'T GET HERE, BUT DOING A SAFETY CHECK ANYWAY 1029 // SHOULDN'T GET HERE, BUT DOING A SAFETY CHECK ANYWAY
1029 qWarning("Previous tar --gzip process is still running; killing it..."); 1030 qWarning("Previous tar --gzip process is still running; killing it...");
1030 createTargzProc->kill(); 1031 createTargzProc->kill();
1031 } 1032 }
1032 1033
1033 bytes_written = 0; 1034 bytes_written = 0;
1034 qDebug("==>start send tar process"); 1035 qDebug("==>start send tar process");
1035 if ( !createTargzProc->start() ) 1036 if ( !createTargzProc->start() )
1036 qWarning("Error starting %s or %s", 1037 qWarning("Error starting %s or %s",
1037 createTargzProc->arguments().join(" ").latin1(), 1038 createTargzProc->arguments().join(" ").latin1(),
1038 gzipProc->arguments().join(" ").latin1() ); 1039 gzipProc->arguments().join(" ").latin1() );
1039 break; 1040 break;
1040 case SendBuffer: 1041 case SendBuffer:
1041 if ( !buf.open( IO_ReadOnly) ) { 1042 if ( !buf.open( IO_ReadOnly) ) {
1042 emit failed(); 1043 emit failed();
1043 mode = Idle; 1044 mode = Idle;
1044 return; 1045 return;
1045 } 1046 }
1046 1047
1047 // qDebug( "Debug: Sending byte array" ); 1048 // qDebug( "Debug: Sending byte array" );
1048 bytes_written = 0; 1049 bytes_written = 0;
1049 while( !buf.atEnd() ) 1050 while( !buf.atEnd() )
1050 putch( buf.getch() ); 1051 putch( buf.getch() );
1051 buf.close(); 1052 buf.close();
1052 break; 1053 break;
1053 case RetrieveFile: 1054 case RetrieveFile:
1054 // retrieve file mode 1055 // retrieve file mode
1055 if ( file.exists() && !file.remove() ) { 1056 if ( file.exists() && !file.remove() ) {
1056 emit failed(); 1057 emit failed();
1057 mode = Idle; 1058 mode = Idle;
1058 return; 1059 return;
1059 } 1060 }
1060 1061
1061 if ( !file.open( IO_WriteOnly) ) { 1062 if ( !file.open( IO_WriteOnly) ) {
1062 emit failed(); 1063 emit failed();
1063 mode = Idle; 1064 mode = Idle;
1064 return; 1065 return;
1065 } 1066 }
1066 // qDebug( "Debug: Retrieving file %s", file.name().latin1() ); 1067 // qDebug( "Debug: Retrieving file %s", file.name().latin1() );
1067 break; 1068 break;
1068 case RetrieveGzipFile: 1069 case RetrieveGzipFile:
1069 qDebug("=-> starting tar process to receive .tgz file"); 1070 qDebug("=-> starting tar process to receive .tgz file");
1070 break; 1071 break;
1071 case RetrieveBuffer: 1072 case RetrieveBuffer:
1072 // retrieve buffer mode 1073 // retrieve buffer mode
1073 if ( !buf.open( IO_WriteOnly) ) { 1074 if ( !buf.open( IO_WriteOnly) ) {
1074 emit failed(); 1075 emit failed();
1075 mode = Idle; 1076 mode = Idle;
1076 return; 1077 return;
1077 } 1078 }
1078 // qDebug( "Debug: Retrieving byte array" ); 1079 // qDebug( "Debug: Retrieving byte array" );
1079 break; 1080 break;
1080 case Idle: 1081 case Idle:
1081 qDebug("connection established but mode set to Idle; BUG!"); 1082 qDebug("connection established but mode set to Idle; BUG!");
1082 break; 1083 break;
1083 } 1084 }
1084} 1085}
1085 1086
1086void ServerDTP::connectionClosed() 1087void ServerDTP::connectionClosed()
1087{ 1088{
1088 //qDebug( "Debug: Data connection closed %ld bytes written", bytes_written ); 1089 //qDebug( "Debug: Data connection closed %ld bytes written", bytes_written );
1089 1090
1090 // send file mode 1091 // send file mode
1091 if ( SendFile == mode ) { 1092 if ( SendFile == mode ) {
1092 if ( bytes_written == file.size() ) 1093 if ( bytes_written == file.size() )
1093 emit completed(); 1094 emit completed();
1094 else 1095 else
1095 emit failed(); 1096 emit failed();
1096 } 1097 }
1097 1098
1098 // send buffer mode 1099 // send buffer mode
1099 else if ( SendBuffer == mode ) { 1100 else if ( SendBuffer == mode ) {
1100 if ( bytes_written == buf.size() ) 1101 if ( bytes_written == buf.size() )
1101 emit completed(); 1102 emit completed();
1102 else 1103 else
1103 emit failed(); 1104 emit failed();
1104 } 1105 }
1105 1106
1106 // retrieve file mode 1107 // retrieve file mode
1107 else if ( RetrieveFile == mode ) { 1108 else if ( RetrieveFile == mode ) {
1108 file.close(); 1109 file.close();
1109 emit completed(); 1110 emit completed();
1110 } 1111 }
1111 1112
1112 else if ( RetrieveGzipFile == mode ) { 1113 else if ( RetrieveGzipFile == mode ) {
1113 qDebug("Done writing ungzip file; closing input"); 1114 qDebug("Done writing ungzip file; closing input");
1114 gzipProc->flushStdin(); 1115 gzipProc->flushStdin();
1115 gzipProc->closeStdin(); 1116 gzipProc->closeStdin();
1116 } 1117 }
1117 1118
1118 // retrieve buffer mode 1119 // retrieve buffer mode
1119 else if ( RetrieveBuffer == mode ) { 1120 else if ( RetrieveBuffer == mode ) {
1120 buf.close(); 1121 buf.close();
1121 emit completed(); 1122 emit completed();
1122 } 1123 }
1123 1124
1124 mode = Idle; 1125 mode = Idle;
1125} 1126}
1126 1127
1127void ServerDTP::bytesWritten( int bytes ) 1128void ServerDTP::bytesWritten( int bytes )
1128{ 1129{
1129 bytes_written += bytes; 1130 bytes_written += bytes;
1130 1131
1131 // send file mode 1132 // send file mode
1132 if ( SendFile == mode ) { 1133 if ( SendFile == mode ) {
1133 1134
1134 if ( bytes_written == file.size() ) { 1135 if ( bytes_written == file.size() ) {
1135 // qDebug( "Debug: Sending complete: %d bytes", file.size() ); 1136 // qDebug( "Debug: Sending complete: %d bytes", file.size() );
1136 file.close(); 1137 file.close();
1137 emit completed(); 1138 emit completed();
1138 mode = Idle; 1139 mode = Idle;
1139 } 1140 }
1140 else if( !file.atEnd() ) { 1141 else if( !file.atEnd() ) {
1141 QCString s; 1142 QCString s;
1142 s.resize( block_size ); 1143 s.resize( block_size );
1143 int bytes = file.readBlock( s.data(), block_size ); 1144 int bytes = file.readBlock( s.data(), block_size );
1144 writeBlock( s.data(), bytes ); 1145 writeBlock( s.data(), bytes );
1145 } 1146 }
1146 } 1147 }
1147 1148
1148 // send buffer mode 1149 // send buffer mode
1149 if ( SendBuffer == mode ) { 1150 if ( SendBuffer == mode ) {
1150 1151
1151 if ( bytes_written == buf.size() ) { 1152 if ( bytes_written == buf.size() ) {
1152 // qDebug( "Debug: Sending complete: %d bytes", buf.size() ); 1153 // qDebug( "Debug: Sending complete: %d bytes", buf.size() );
1153 emit completed(); 1154 emit completed();
1154 mode = Idle; 1155 mode = Idle;
1155 } 1156 }
1156 } 1157 }
1157} 1158}
1158 1159
1159void ServerDTP::readyRead() 1160void ServerDTP::readyRead()
1160{ 1161{
1161 // retrieve file mode 1162 // retrieve file mode
1162 if ( RetrieveFile == mode ) { 1163 if ( RetrieveFile == mode ) {
1163 QCString s; 1164 QCString s;
1164 s.resize( bytesAvailable() ); 1165 s.resize( bytesAvailable() );
1165 readBlock( s.data(), bytesAvailable() ); 1166 readBlock( s.data(), bytesAvailable() );
1166 file.writeBlock( s.data(), s.size() ); 1167 file.writeBlock( s.data(), s.size() );
1167 } 1168 }
1168 else if ( RetrieveGzipFile == mode ) { 1169 else if ( RetrieveGzipFile == mode ) {
1169 if ( !gzipProc->isRunning() ) 1170 if ( !gzipProc->isRunning() )
1170 gzipProc->start(); 1171 gzipProc->start();
1171 1172
1172 QByteArray s; 1173 QByteArray s;
1173 s.resize( bytesAvailable() ); 1174 s.resize( bytesAvailable() );
1174 readBlock( s.data(), bytesAvailable() ); 1175 readBlock( s.data(), bytesAvailable() );
1175 gzipProc->writeToStdin( s ); 1176 gzipProc->writeToStdin( s );
1176 qDebug("wrote %d bytes to ungzip ", s.size() ); 1177 qDebug("wrote %d bytes to ungzip ", s.size() );
1177 } 1178 }
1178 // retrieve buffer mode 1179 // retrieve buffer mode
1179 else if ( RetrieveBuffer == mode ) { 1180 else if ( RetrieveBuffer == mode ) {
1180 QCString s; 1181 QCString s;
1181 s.resize( bytesAvailable() ); 1182 s.resize( bytesAvailable() );
1182 readBlock( s.data(), bytesAvailable() ); 1183 readBlock( s.data(), bytesAvailable() );
1183 buf.writeBlock( s.data(), s.size() ); 1184 buf.writeBlock( s.data(), s.size() );
1184 } 1185 }
1185} 1186}
1186 1187
1187void ServerDTP::writeTargzBlock() 1188void ServerDTP::writeTargzBlock()
1188{ 1189{
1189 QByteArray block = gzipProc->readStdout(); 1190 QByteArray block = gzipProc->readStdout();
1190 writeBlock( block.data(), block.size() ); 1191 writeBlock( block.data(), block.size() );
1191 qDebug("writeTargzBlock %d", block.size()); 1192 qDebug("writeTargzBlock %d", block.size());
1192 if ( !createTargzProc->isRunning() ) { 1193 if ( !createTargzProc->isRunning() ) {
1193 qDebug("tar and gzip done"); 1194 qDebug("tar and gzip done");
1194 emit completed(); 1195 emit completed();
1195 mode = Idle; 1196 mode = Idle;
1196 disconnect( gzipProc, SIGNAL( readyReadStdout() ), 1197 disconnect( gzipProc, SIGNAL( readyReadStdout() ),
1197 this, SLOT( writeTargzBlock() ) ); 1198 this, SLOT( writeTargzBlock() ) );
1198 } 1199 }
1199} 1200}
1200 1201
1201void ServerDTP::targzDone() 1202void ServerDTP::targzDone()
1202{ 1203{
1203 //qDebug("targz done"); 1204 //qDebug("targz done");
1204 disconnect( createTargzProc, SIGNAL( readyReadStdout() ), 1205 disconnect( createTargzProc, SIGNAL( readyReadStdout() ),
1205 this, SLOT( gzipTarBlock() ) ); 1206 this, SLOT( gzipTarBlock() ) );
1206 gzipProc->closeStdin(); 1207 gzipProc->closeStdin();
1207} 1208}
1208 1209
1209void ServerDTP::gzipTarBlock() 1210void ServerDTP::gzipTarBlock()
1210{ 1211{
1211 //qDebug("gzipTarBlock"); 1212 //qDebug("gzipTarBlock");
1212 if ( !gzipProc->isRunning() ) { 1213 if ( !gzipProc->isRunning() ) {
1213 //qDebug("auto start gzip proc"); 1214 //qDebug("auto start gzip proc");
1214 gzipProc->start(); 1215 gzipProc->start();
1215 } 1216 }
1216 gzipProc->writeToStdin( createTargzProc->readStdout() ); 1217 gzipProc->writeToStdin( createTargzProc->readStdout() );
1217} 1218}
1218 1219
1219void ServerDTP::sendFile( const QString fn, const QHostAddress& host, Q_UINT16 port ) 1220void ServerDTP::sendFile( const QString fn, const QHostAddress& host, Q_UINT16 port )
1220{ 1221{
1221 file.setName( fn ); 1222 file.setName( fn );
1222 mode = SendFile; 1223 mode = SendFile;
1223 connectToHost( host.toString(), port ); 1224 connectToHost( host.toString(), port );
1224} 1225}
1225 1226
1226void ServerDTP::sendFile( const QString fn ) 1227void ServerDTP::sendFile( const QString fn )
1227{ 1228{
1228 file.setName( fn ); 1229 file.setName( fn );
1229 mode = SendFile; 1230 mode = SendFile;
1230} 1231}
1231 1232
1232void ServerDTP::sendGzipFile( const QString &fn, 1233void ServerDTP::sendGzipFile( const QString &fn,
1233 const QStringList &archiveTargets, 1234 const QStringList &archiveTargets,
1234 const QHostAddress& host, Q_UINT16 port ) 1235 const QHostAddress& host, Q_UINT16 port )
1235{ 1236{
1236 sendGzipFile( fn, archiveTargets ); 1237 sendGzipFile( fn, archiveTargets );
1237 connectToHost( host.toString(), port ); 1238 connectToHost( host.toString(), port );
1238} 1239}
1239 1240
1240void ServerDTP::sendGzipFile( const QString &fn, 1241void ServerDTP::sendGzipFile( const QString &fn,
1241 const QStringList &archiveTargets ) 1242 const QStringList &archiveTargets )
1242{ 1243{
1243 mode = SendGzipFile; 1244 mode = SendGzipFile;
1244 file.setName( fn ); 1245 file.setName( fn );
1245 1246
1246 QStringList args = "tar"; 1247 QStringList args = "tar";
1247 args += "-cv"; 1248 args += "-cv";
1248 args += archiveTargets; 1249 args += archiveTargets;
1249 qDebug("sendGzipFile %s", args.join(" ").latin1() ); 1250 qDebug("sendGzipFile %s", args.join(" ").latin1() );
1250 createTargzProc->setArguments( args ); 1251 createTargzProc->setArguments( args );
1251 connect( createTargzProc, 1252 connect( createTargzProc,
1252 SIGNAL( readyReadStdout() ), SLOT( gzipTarBlock() ) ); 1253 SIGNAL( readyReadStdout() ), SLOT( gzipTarBlock() ) );
1253 1254
1254 gzipProc->setArguments( "gzip" ); 1255 gzipProc->setArguments( "gzip" );
1255 connect( gzipProc, SIGNAL( readyReadStdout() ), 1256 connect( gzipProc, SIGNAL( readyReadStdout() ),
1256 SLOT( writeTargzBlock() ) ); 1257 SLOT( writeTargzBlock() ) );
1257} 1258}
1258 1259
1259void ServerDTP::gunzipDone() 1260void ServerDTP::gunzipDone()
1260{ 1261{
1261 qDebug("gunzipDone"); 1262 qDebug("gunzipDone");
1262 disconnect( gzipProc, SIGNAL( processExited() ), 1263 disconnect( gzipProc, SIGNAL( processExited() ),
1263 this, SLOT( gunzipDone() ) ); 1264 this, SLOT( gunzipDone() ) );
1264 retrieveTargzProc->closeStdin(); 1265 retrieveTargzProc->closeStdin();
1265 disconnect( gzipProc, SIGNAL( readyReadStdout() ), 1266 disconnect( gzipProc, SIGNAL( readyReadStdout() ),
1266 this, SLOT( tarExtractBlock() ) ); 1267 this, SLOT( tarExtractBlock() ) );
1267} 1268}
1268 1269
1269void ServerDTP::tarExtractBlock() 1270void ServerDTP::tarExtractBlock()
1270{ 1271{
1271 qDebug("ungzipTarBlock"); 1272 qDebug("ungzipTarBlock");
1272 if ( !retrieveTargzProc->isRunning() ) { 1273 if ( !retrieveTargzProc->isRunning() ) {
1273 qDebug("auto start ungzip proc"); 1274 qDebug("auto start ungzip proc");
1274 if ( !retrieveTargzProc->start() ) 1275 if ( !retrieveTargzProc->start() )
1275 qWarning(" failed to start tar -x process"); 1276 qWarning(" failed to start tar -x process");
1276 } 1277 }
1277 retrieveTargzProc->writeToStdin( gzipProc->readStdout() ); 1278 retrieveTargzProc->writeToStdin( gzipProc->readStdout() );
1278} 1279}
1279 1280
1280 1281
1281void ServerDTP::retrieveFile( const QString fn, const QHostAddress& host, Q_UINT16 port ) 1282void ServerDTP::retrieveFile( const QString fn, const QHostAddress& host, Q_UINT16 port )
1282{ 1283{
1283 file.setName( fn ); 1284 file.setName( fn );
1284 mode = RetrieveFile; 1285 mode = RetrieveFile;
1285 connectToHost( host.toString(), port ); 1286 connectToHost( host.toString(), port );
1286} 1287}
1287 1288
1288void ServerDTP::retrieveFile( const QString fn ) 1289void ServerDTP::retrieveFile( const QString fn )
1289{ 1290{
1290 file.setName( fn ); 1291 file.setName( fn );
1291 mode = RetrieveFile; 1292 mode = RetrieveFile;
1292} 1293}
1293 1294
1294void ServerDTP::retrieveGzipFile( const QString &fn ) 1295void ServerDTP::retrieveGzipFile( const QString &fn )
1295{ 1296{
1296 qDebug("retrieveGzipFile %s", fn.latin1()); 1297 qDebug("retrieveGzipFile %s", fn.latin1());
1297 file.setName( fn ); 1298 file.setName( fn );
1298 mode = RetrieveGzipFile; 1299 mode = RetrieveGzipFile;
1299 1300
1300 gzipProc->setArguments( "gunzip" ); 1301 gzipProc->setArguments( "gunzip" );
1301 connect( gzipProc, SIGNAL( readyReadStdout() ), 1302 connect( gzipProc, SIGNAL( readyReadStdout() ),
1302 SLOT( tarExtractBlock() ) ); 1303 SLOT( tarExtractBlock() ) );
1303 connect( gzipProc, SIGNAL( processExited() ), 1304 connect( gzipProc, SIGNAL( processExited() ),
1304 SLOT( gunzipDone() ) ); 1305 SLOT( gunzipDone() ) );
1305} 1306}
1306 1307
1307void ServerDTP::retrieveGzipFile( const QString &fn, const QHostAddress& host, Q_UINT16 port ) 1308void ServerDTP::retrieveGzipFile( const QString &fn, const QHostAddress& host, Q_UINT16 port )
1308{ 1309{
1309 retrieveGzipFile( fn ); 1310 retrieveGzipFile( fn );
1310 connectToHost( host.toString(), port ); 1311 connectToHost( host.toString(), port );
1311} 1312}
1312 1313
1313void ServerDTP::sendByteArray( const QByteArray& array, const QHostAddress& host, Q_UINT16 port ) 1314void ServerDTP::sendByteArray( const QByteArray& array, const QHostAddress& host, Q_UINT16 port )
1314{ 1315{
1315 buf.setBuffer( array ); 1316 buf.setBuffer( array );
1316 mode = SendBuffer; 1317 mode = SendBuffer;
1317 connectToHost( host.toString(), port ); 1318 connectToHost( host.toString(), port );
1318} 1319}
1319 1320
1320void ServerDTP::sendByteArray( const QByteArray& array ) 1321void ServerDTP::sendByteArray( const QByteArray& array )
1321{ 1322{
1322 buf.setBuffer( array ); 1323 buf.setBuffer( array );
1323 mode = SendBuffer; 1324 mode = SendBuffer;
1324} 1325}
1325 1326
1326void ServerDTP::retrieveByteArray( const QHostAddress& host, Q_UINT16 port ) 1327void ServerDTP::retrieveByteArray( const QHostAddress& host, Q_UINT16 port )
1327{ 1328{
1328 buf.setBuffer( QByteArray() ); 1329 buf.setBuffer( QByteArray() );
1329 mode = RetrieveBuffer; 1330 mode = RetrieveBuffer;
1330 connectToHost( host.toString(), port ); 1331 connectToHost( host.toString(), port );
1331} 1332}
1332 1333
1333void ServerDTP::retrieveByteArray() 1334void ServerDTP::retrieveByteArray()
1334{ 1335{
1335 buf.setBuffer( QByteArray() ); 1336 buf.setBuffer( QByteArray() );
1336 mode = RetrieveBuffer; 1337 mode = RetrieveBuffer;
1337} 1338}
1338 1339
1339void ServerDTP::setSocket( int socket ) 1340void ServerDTP::setSocket( int socket )
1340{ 1341{
1341 QSocket::setSocket( socket ); 1342 QSocket::setSocket( socket );
1342 connected(); 1343 connected();
1343} 1344}
1344 1345