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