-rw-r--r-- | core/qws/qcopbridge.cpp | 374 | ||||
-rw-r--r-- | core/qws/transferserver.cpp | 2126 |
2 files changed, 1254 insertions, 1246 deletions
diff --git a/core/qws/qcopbridge.cpp b/core/qws/qcopbridge.cpp index 4fd0807..822efb7 100644 --- a/core/qws/qcopbridge.cpp +++ b/core/qws/qcopbridge.cpp @@ -23,2 +23,3 @@ +/* OPIE */ #include <qpe/qcopenvelope_qws.h> @@ -26,3 +27,5 @@ #include <qpe/version.h> +#include <opie2/odebug.h> +/* QT */ #include <qtextstream.h> @@ -32,2 +35,3 @@ +/* STD */ #define _XOPEN_SOURCE @@ -52,11 +56,11 @@ QCopBridge::QCopBridge( Q_UINT16 port, QObject *parent , if ( !ok() ) - qWarning( "Failed to bind to port %d", port ); + owarn << "Failed to bind to port " << port << "" << oendl; else { #ifndef QT_NO_COP - desktopChannel = new QCopChannel( "QPE/Desktop", this ); - connect( desktopChannel, SIGNAL(received(const QCString&,const QByteArray&)), - this, SLOT(desktopMessage(const QCString&,const QByteArray&)) ); - cardChannel = new QCopChannel( "QPE/Card", this ); - connect( cardChannel, SIGNAL(received(const QCString&,const QByteArray&)), - this, SLOT(desktopMessage(const QCString&,const QByteArray&)) ); + desktopChannel = new QCopChannel( "QPE/Desktop", this ); + connect( desktopChannel, SIGNAL(received(const QCString&,const QByteArray&)), + this, SLOT(desktopMessage(const QCString&,const QByteArray&)) ); + cardChannel = new QCopChannel( "QPE/Card", this ); + connect( cardChannel, SIGNAL(received(const QCString&,const QByteArray&)), + this, SLOT(desktopMessage(const QCString&,const QByteArray&)) ); #endif @@ -83,4 +87,4 @@ void QCopBridge::newConnection( int socket ) if ( sendSync ) { - pi ->startSync(); - sendSync = FALSE; + pi ->startSync(); + sendSync = FALSE; } @@ -93,3 +97,3 @@ void QCopBridge::connectionClosed( QCopBridgePI *pi ) #ifndef QT_NO_COP - QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Enable; + QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Enable; #endif @@ -102,3 +106,3 @@ void QCopBridge::closeOpenConnections() for ( pi = openConnections.first(); pi != 0; pi = openConnections.next() ) - pi->close(); + pi->close(); } @@ -112,4 +116,4 @@ void QCopBridge::desktopMessage( const QCString &command, const QByteArray &args if ( paren <= 0 ) { - qDebug("DesktopMessage: bad qcop syntax"); - return; + odebug << "DesktopMessage: bad qcop syntax" << oendl; + return; } @@ -118,4 +122,4 @@ void QCopBridge::desktopMessage( const QCString &command, const QByteArray &args if ( params[params.length()-1] != ')' ) { - qDebug("DesktopMessage: bad qcop syntax"); - return; + odebug << "DesktopMessage: bad qcop syntax" << oendl; + return; } @@ -127,47 +131,47 @@ void QCopBridge::desktopMessage( const QCString &command, const QByteArray &args if ( paramList.count() ) { - QDataStream stream( args, IO_ReadOnly ); - for ( QStringList::Iterator it = paramList.begin(); it != paramList.end(); ++it ) { - QString str; - if ( *it == "QString" ) { - stream >> str; - } else if ( *it == "QCString" ) { - QCString cstr; - stream >> cstr; - str = QString::fromLocal8Bit( cstr ); - } else if ( *it == "int" ) { - int i; - stream >> i; - str = QString::number( i ); - } else if ( *it == "bool" ) { - int i; - stream >> i; - str = QString::number( i ); - } else { - qDebug(" cannot route the argument type %s throught the qcop bridge", (*it).latin1() ); - return; - } - QString estr; - for (int i=0; i<(int)str.length(); i++) { - QChar ch = str[i]; - if ( ch.row() ) - goto quick; - switch (ch.cell()) { - case '&': - estr.append( "&" ); - break; - case ' ': - estr.append( "&0x20;" ); - break; - case '\n': - estr.append( "&0x0d;" ); - break; - case '\r': - estr.append( "&0x0a;" ); - break; - default: quick: - estr.append(ch); - } - } - data += " " + estr; - } + QDataStream stream( args, IO_ReadOnly ); + for ( QStringList::Iterator it = paramList.begin(); it != paramList.end(); ++it ) { + QString str; + if ( *it == "QString" ) { + stream >> str; + } else if ( *it == "QCString" ) { + QCString cstr; + stream >> cstr; + str = QString::fromLocal8Bit( cstr ); + } else if ( *it == "int" ) { + int i; + stream >> i; + str = QString::number( i ); + } else if ( *it == "bool" ) { + int i; + stream >> i; + str = QString::number( i ); + } else { + odebug << " cannot route the argument type " << (*it) << " throught the qcop bridge" << oendl; + return; + } + QString estr; + for (int i=0; i<(int)str.length(); i++) { + QChar ch = str[i]; + if ( ch.row() ) + goto quick; + switch (ch.cell()) { + case '&': + estr.append( "&" ); + break; + case ' ': + estr.append( "&0x20;" ); + break; + case '\n': + estr.append( "&0x0d;" ); + break; + case '\r': + estr.append( "&0x0a;" ); + break; + default: quick: + estr.append(ch); + } + } + data += " " + estr; + } } @@ -176,5 +180,5 @@ void QCopBridge::desktopMessage( const QCString &command, const QByteArray &args if ( command == "startSync()" ) { - // we need to buffer it a bit - sendSync = TRUE; - startTimer( 20000 ); + // we need to buffer it a bit + sendSync = TRUE; + startTimer( 20000 ); } @@ -183,3 +187,3 @@ void QCopBridge::desktopMessage( const QCString &command, const QByteArray &args for ( pi = openConnections.first(); pi != 0; pi = openConnections.next() ) { - pi->sendDesktopMessage( sendCommand ); + pi->sendDesktopMessage( sendCommand ); } @@ -204,23 +208,23 @@ QCopBridgePI::QCopBridgePI( int socket, QObject *parent , const char* name ) if ( !SyncAuthentication::isAuthorized(peeraddress) ) { - state = Forbidden; - startTimer( 0 ); + state = Forbidden; + startTimer( 0 ); } else -#endif +#endif { - state = Connected; - sendSync = FALSE; - connect( this, SIGNAL( readyRead() ), SLOT( read() ) ); - connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); - - QString intro="220 Qtopia "; - intro += QPE_VERSION; intro += ";"; - intro += "challenge="; intro += SyncAuthentication::serverId(); intro += ";"; - intro += "loginname="; intro += SyncAuthentication::loginName(); intro += ";"; - intro += "displayname="; intro += SyncAuthentication::ownerName(); intro += ";"; - send( intro ); - state = Wait_USER; - - // idle timer to close connections when not used anymore - startTimer( 60000 ); - connected = TRUE; + state = Connected; + sendSync = FALSE; + connect( this, SIGNAL( readyRead() ), SLOT( read() ) ); + connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); + + QString intro="220 Qtopia "; + intro += QPE_VERSION; intro += ";"; + intro += "challenge="; intro += SyncAuthentication::serverId(); intro += ";"; + intro += "loginname="; intro += SyncAuthentication::loginName(); intro += ";"; + intro += "displayname="; intro += SyncAuthentication::ownerName(); intro += ";"; + send( intro ); + state = Wait_USER; + + // idle timer to close connections when not used anymore + startTimer( 60000 ); + connected = TRUE; } @@ -237,3 +241,3 @@ void QCopBridgePI::connectionClosed() emit connectionClosed( this ); - // qDebug( "Debug: Connection closed" ); + // odebug << "Debug: Connection closed" << oendl; delete this; @@ -252,3 +256,3 @@ void QCopBridgePI::send( const QString& msg ) os << msg << endl; - //qDebug( "sending qcop message: %s", msg.latin1() ); + //odebug << "sending qcop message: " << msg << "" << oendl; } @@ -258,3 +262,3 @@ void QCopBridgePI::read() while ( canReadLine() ) - process( readLine().stripWhiteSpace() ); + process( readLine().stripWhiteSpace() ); } @@ -263,3 +267,3 @@ void QCopBridgePI::process( const QString& message ) { - //qDebug( "Command: %s", message.latin1() ); + //odebug << "Command: " << message << "" << oendl; @@ -275,3 +279,3 @@ void QCopBridgePI::process( const QString& message ) if ( msg.count() >= 2 ) - arg = msg[1]; + arg = msg[1]; @@ -279,5 +283,5 @@ void QCopBridgePI::process( const QString& message ) if ( cmd == "QUIT" ) { - send( "211 Have a nice day!" ); - delete this; - return; + send( "211 Have a nice day!" ); + delete this; + return; } @@ -286,3 +290,3 @@ void QCopBridgePI::process( const QString& message ) if ( Connected == state ) - return; + return; @@ -291,9 +295,9 @@ void QCopBridgePI::process( const QString& message ) - if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) { - send( "530 Please login with USER and PASS" ); - return; - } - send( "331 User name ok, need password" ); - state = Wait_PASS; - return; + if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) { + send( "530 Please login with USER and PASS" ); + return; + } + send( "331 User name ok, need password" ); + state = Wait_PASS; + return; } @@ -303,13 +307,13 @@ void QCopBridgePI::process( const QString& message ) - if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) { - send( "530 Please login with USER and PASS" ); - return; - } - send( "230 User logged in, proceed" ); - state = Ready; - if ( sendSync ) { - sendDesktopMessage( "startSync()" ); - sendSync = FALSE; - } - return; + if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) { + send( "530 Please login with USER and PASS" ); + return; + } + send( "230 User logged in, proceed" ); + state = Ready; + if ( sendSync ) { + sendDesktopMessage( "startSync()" ); + sendSync = FALSE; + } + return; } @@ -318,4 +322,4 @@ void QCopBridgePI::process( const QString& message ) else if ( cmd == "NOOP" ) { - connected = TRUE; - send( "200 Command okay" ); + connected = TRUE; + send( "200 Command okay" ); } @@ -325,78 +329,78 @@ void QCopBridgePI::process( const QString& message ) - // example: call QPE/System execute(QString) addressbook - - if ( msg.count() < 3 ) { - send( "500 Syntax error, command unrecognized" ); - } - else { - - QString channel = msg[1]; - QString command = msg[2]; - - command.stripWhiteSpace(); - - int paren = command.find( "(" ); - if ( paren <= 0 ) { - send( "500 Syntax error, command unrecognized" ); - return; - } - - QString params = command.mid( paren + 1 ); - if ( params[params.length()-1] != ')' ) { - send( "500 Syntax error, command unrecognized" ); - return; - } - - params.truncate( params.length()-1 ); - QByteArray buffer; - QDataStream ds( buffer, IO_WriteOnly ); - - int msgId = 3; - - QStringList paramList = QStringList::split( ",", params ); - if ( paramList.count() > msg.count() - 3 ) { - send( "500 Syntax error, command unrecognized" ); - return; - } - - for ( QStringList::Iterator it = paramList.begin(); it != paramList.end(); ++it ) { - - QString arg = msg[msgId]; - arg.replace( QRegExp("&0x20;"), " " ); - arg.replace( QRegExp("&"), "&" ); - arg.replace( QRegExp("&0x0d;"), "\n" ); - arg.replace( QRegExp("&0x0a;"), "\r" ); - if ( *it == "QString" ) - ds << arg; - else if ( *it == "QCString" ) - ds << arg.local8Bit(); - else if ( *it == "int" ) - ds << arg.toInt(); - else if ( *it == "bool" ) - ds << arg.toInt(); - else { - send( "500 Syntax error, command unrecognized" ); - return; - } - msgId++; - } + // example: call QPE/System execute(QString) addressbook + + if ( msg.count() < 3 ) { + send( "500 Syntax error, command unrecognized" ); + } + else { + + QString channel = msg[1]; + QString command = msg[2]; + + command.stripWhiteSpace(); + + int paren = command.find( "(" ); + if ( paren <= 0 ) { + send( "500 Syntax error, command unrecognized" ); + return; + } + + QString params = command.mid( paren + 1 ); + if ( params[params.length()-1] != ')' ) { + send( "500 Syntax error, command unrecognized" ); + return; + } + + params.truncate( params.length()-1 ); + QByteArray buffer; + QDataStream ds( buffer, IO_WriteOnly ); + + int msgId = 3; + + QStringList paramList = QStringList::split( ",", params ); + if ( paramList.count() > msg.count() - 3 ) { + send( "500 Syntax error, command unrecognized" ); + return; + } + + for ( QStringList::Iterator it = paramList.begin(); it != paramList.end(); ++it ) { + + QString arg = msg[msgId]; + arg.replace( QRegExp("&0x20;"), " " ); + arg.replace( QRegExp("&"), "&" ); + arg.replace( QRegExp("&0x0d;"), "\n" ); + arg.replace( QRegExp("&0x0a;"), "\r" ); + if ( *it == "QString" ) + ds << arg; + else if ( *it == "QCString" ) + ds << arg.local8Bit(); + else if ( *it == "int" ) + ds << arg.toInt(); + else if ( *it == "bool" ) + ds << arg.toInt(); + else { + send( "500 Syntax error, command unrecognized" ); + return; + } + msgId++; + } #ifndef QT_NO_COP - if ( !QCopChannel::isRegistered( channel.latin1() ) ) { - // send message back about it - QString answer = "599 ChannelNotRegistered " + channel; - send( answer ); - return; - } + if ( !QCopChannel::isRegistered( channel.latin1() ) ) { + // send message back about it + QString answer = "599 ChannelNotRegistered " + channel; + send( answer ); + return; + } #endif - + #ifndef QT_NO_COP - if ( paramList.count() ) - QCopChannel::send( channel.latin1(), command.latin1(), buffer ); - else - QCopChannel::send( channel.latin1(), command.latin1() ); + if ( paramList.count() ) + QCopChannel::send( channel.latin1(), command.latin1(), buffer ); + else + QCopChannel::send( channel.latin1(), command.latin1() ); - send( "200 Command okay" ); + send( "200 Command okay" ); #endif - } + } } @@ -404,3 +408,3 @@ void QCopBridgePI::process( const QString& message ) else - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); } @@ -412,5 +416,5 @@ void QCopBridgePI::timerEvent( QTimerEvent * ) if ( connected ) - connected = FALSE; + connected = FALSE; else - connectionClosed(); + connectionClosed(); } diff --git a/core/qws/transferserver.cpp b/core/qws/transferserver.cpp index d0fec89..777c384 100644 --- a/core/qws/transferserver.cpp +++ b/core/qws/transferserver.cpp @@ -19,2 +19,6 @@ **********************************************************************/ + +/* OPIE */ +#include <opie2/odebug.h> + #define _XOPEN_SOURCE @@ -73,6 +77,6 @@ TransferServer::TransferServer( Q_UINT16 port, QObject *parent , const char* name ) - : QServerSocket( port, 1, parent, name ) + : QServerSocket( port, 1, parent, name ) { - if ( !ok() ) - qWarning( "Failed to bind to port %d", port ); + if ( !ok() ) + owarn << "Failed to bind to port " << port << "" << oendl; } @@ -85,3 +89,3 @@ void TransferServer::newConnection( int socket ) { - (void) new ServerPI( socket, this ); + (void) new ServerPI( socket, this ); } @@ -96,3 +100,3 @@ struct UidGen { - QString uuid(); + QString uuid(); }; @@ -101,6 +105,6 @@ QString UidGen::uuid() { - srandom( random() ); - QString numStr = QString::number( random() ); + srandom( random() ); + QString numStr = QString::number( random() ); - return "{" + numStr + "}"; + return "{" + numStr + "}"; } @@ -113,9 +117,9 @@ QString UidGen::uuid() { - QFile file( "/proc/sys/kernel/random/uuid" ); - if (!file.open(IO_ReadOnly ) ) - return QString::null; + QFile file( "/proc/sys/kernel/random/uuid" ); + if (!file.open(IO_ReadOnly ) ) + return QString::null; - QTextStream stream(&file); + QTextStream stream(&file); - return "{" + stream.read().stripWhiteSpace() + "}"; + return "{" + stream.read().stripWhiteSpace() + "}"; } @@ -124,5 +128,5 @@ QString UidGen::uuid() { - uuid_t uuid; - ::uuid_generate( uuid ); - return QUUid( uuid ).toString(); + uuid_t uuid; + ::uuid_generate( uuid ); + return QUUid( uuid ).toString(); } @@ -133,11 +137,11 @@ QString SyncAuthentication::serverId() { - Config cfg("Security"); - cfg.setGroup("Sync"); - QString r = cfg.readEntry("serverid"); - if ( r.isEmpty() ) { - UidGen gen; - r = gen.uuid(); - cfg.writeEntry("serverid", r ); - } - return r; + Config cfg("Security"); + cfg.setGroup("Sync"); + QString r = cfg.readEntry("serverid"); + if ( r.isEmpty() ) { + UidGen gen; + r = gen.uuid(); + cfg.writeEntry("serverid", r ); + } + return r; } @@ -146,11 +150,11 @@ QString SyncAuthentication::ownerName() { - QString vfilename = Global::applicationFileName("addressbook", - "businesscard.vcf"); - if (QFile::exists(vfilename)) { - Contact c; - c = Contact::readVCard( vfilename )[0]; - return c.fullName(); - } - - return ""; + QString vfilename = Global::applicationFileName("addressbook", + "businesscard.vcf"); + if (QFile::exists(vfilename)) { + Contact c; + c = Contact::readVCard( vfilename )[0]; + return c.fullName(); + } + + return ""; } @@ -159,5 +163,5 @@ QString SyncAuthentication::loginName() { - struct passwd *pw; - pw = getpwuid( geteuid() ); - return QString::fromLocal8Bit( pw->pw_name ); + struct passwd *pw; + pw = getpwuid( geteuid() ); + return QString::fromLocal8Bit( pw->pw_name ); } @@ -166,14 +170,14 @@ int SyncAuthentication::isAuthorized(QHostAddress peeraddress) { - Config cfg("Security"); - cfg.setGroup("Sync"); - // QString allowedstr = cfg.readEntry("auth_peer","192.168.1.0"); - uint auth_peer = cfg.readNumEntry("auth_peer", 0xc0a80100); - - // QHostAddress allowed; - // allowed.setAddress(allowedstr); - // uint auth_peer = allowed.ip4Addr(); - uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits", 24); - uint mask = auth_peer_bits >= 32 // shifting by 32 is not defined - ? 0xffffffff : (((1 << auth_peer_bits) - 1) << (32 - auth_peer_bits)); - return (peeraddress.ip4Addr() & mask) == auth_peer; + Config cfg("Security"); + cfg.setGroup("Sync"); + // QString allowedstr = cfg.readEntry("auth_peer","192.168.1.0"); + uint auth_peer = cfg.readNumEntry("auth_peer", 0xc0a80100); + + // QHostAddress allowed; + // allowed.setAddress(allowedstr); + // uint auth_peer = allowed.ip4Addr(); + uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits", 24); + uint mask = auth_peer_bits >= 32 // shifting by 32 is not defined + ? 0xffffffff : (((1 << auth_peer_bits) - 1) << (32 - auth_peer_bits)); + return (peeraddress.ip4Addr() & mask) == auth_peer; } @@ -182,6 +186,6 @@ bool SyncAuthentication::checkUser( const QString& user ) { - if ( user.isEmpty() ) - return FALSE; - QString euser = loginName(); - return user == euser; + if ( user.isEmpty() ) + return FALSE; + QString euser = loginName(); + return user == euser; } @@ -191,74 +195,74 @@ bool SyncAuthentication::checkPassword( const QString& password ) #ifdef ALLOW_UNIX_USER_FTP - // First, check system password... + // First, check system password... - struct passwd *pw = 0; - struct spwd *spw = 0; + struct passwd *pw = 0; + struct spwd *spw = 0; - pw = getpwuid( geteuid() ); - spw = getspnam( pw->pw_name ); + pw = getpwuid( geteuid() ); + spw = getspnam( pw->pw_name ); - QString cpwd = QString::fromLocal8Bit( pw->pw_passwd ); - if ( cpwd == "x" && spw ) - cpwd = QString::fromLocal8Bit( spw->sp_pwdp ); + QString cpwd = QString::fromLocal8Bit( pw->pw_passwd ); + if ( cpwd == "x" && spw ) + cpwd = QString::fromLocal8Bit( spw->sp_pwdp ); - // Note: some systems use more than crypt for passwords. - QString cpassword = QString::fromLocal8Bit( crypt( password.local8Bit(), cpwd.local8Bit() ) ); - if ( cpwd == cpassword ) - return TRUE; + // Note: some systems use more than crypt for passwords. + QString cpassword = QString::fromLocal8Bit( crypt( password.local8Bit(), cpwd.local8Bit() ) ); + if ( cpwd == cpassword ) + return TRUE; #endif - static int lastdenial = 0; - static int denials = 0; - int now = time(0); - - // Detect old Qtopia Desktop (no password) - if ( password.isEmpty() ) { - if ( denials < 1 || now > lastdenial + 600 ) { - QMessageBox::warning( 0, tr("Sync Connection"), - tr("<p>An unauthorized system is requesting access to this device." - "<p>If you are using a version of Qtopia Desktop older than 1.5.1, " - "please upgrade."), - tr("Deny") ); - denials++; - lastdenial = now; - } - return FALSE; - } - - // Second, check sync password... - QString pass = password.left(6); - /* old QtopiaDesktops are sending - * rootme newer versions got a Qtopia - * prefixed. Qtopia prefix will suceed - * until the sync software syncs up - * FIXME - */ - if ( pass == "rootme" || pass == "Qtopia") { - - QString cpassword = QString::fromLocal8Bit( crypt( password.mid(8).local8Bit(), "qp" ) ); - Config cfg("Security"); - cfg.setGroup("Sync"); - QString pwds = cfg.readEntry("Passwords"); - if ( QStringList::split(QChar(' '), pwds).contains(cpassword) ) - return TRUE; - - // Unrecognized system. Be careful... - - if ( (denials > 2 && now < lastdenial + 600) - || QMessageBox::warning(0, tr("Sync Connection"), - tr("<p>An unrecognized system is requesting access to this device." - "<p>If you have just initiated a Sync for the first time, this is normal."), - tr("Allow"), tr("Deny"), 0, 1, 1 ) == 1 ) { - denials++; - lastdenial = now; - return FALSE; - } - else { - denials = 0; - cfg.writeEntry("Passwords", pwds + " " + cpassword); - return TRUE; - } - } - - return FALSE; + static int lastdenial = 0; + static int denials = 0; + int now = time(0); + + // Detect old Qtopia Desktop (no password) + if ( password.isEmpty() ) { + if ( denials < 1 || now > lastdenial + 600 ) { + QMessageBox::warning( 0, tr("Sync Connection"), + tr("<p>An unauthorized system is requesting access to this device." + "<p>If you are using a version of Qtopia Desktop older than 1.5.1, " + "please upgrade."), + tr("Deny") ); + denials++; + lastdenial = now; + } + return FALSE; + } + + // Second, check sync password... + QString pass = password.left(6); + /* old QtopiaDesktops are sending + * rootme newer versions got a Qtopia + * prefixed. Qtopia prefix will suceed + * until the sync software syncs up + * FIXME + */ + if ( pass == "rootme" || pass == "Qtopia") { + + QString cpassword = QString::fromLocal8Bit( crypt( password.mid(8).local8Bit(), "qp" ) ); + Config cfg("Security"); + cfg.setGroup("Sync"); + QString pwds = cfg.readEntry("Passwords"); + if ( QStringList::split(QChar(' '), pwds).contains(cpassword) ) + return TRUE; + + // Unrecognized system. Be careful... + + if ( (denials > 2 && now < lastdenial + 600) + || QMessageBox::warning(0, tr("Sync Connection"), + tr("<p>An unrecognized system is requesting access to this device." + "<p>If you have just initiated a Sync for the first time, this is normal."), + tr("Allow"), tr("Deny"), 0, 1, 1 ) == 1 ) { + denials++; + lastdenial = now; + return FALSE; + } + else { + denials = 0; + cfg.writeEntry("Passwords", pwds + " " + cpassword); + return TRUE; + } + } + + return FALSE; } @@ -266,10 +270,10 @@ bool SyncAuthentication::checkPassword( const QString& password ) ServerPI::ServerPI( int socket, QObject *parent , const char* name ) - : QSocket( parent, name ) , dtp( 0 ), serversocket( 0 ), waitsocket( 0 ) + : QSocket( parent, name ) , dtp( 0 ), serversocket( 0 ), waitsocket( 0 ) { - state = Connected; + state = Connected; - setSocket( socket ); + setSocket( socket ); - peerport = peerPort(); - peeraddress = peerAddress(); + peerport = peerPort(); + peeraddress = peerAddress(); @@ -277,36 +281,36 @@ ServerPI::ServerPI( int socket, QObject *parent , const char* name ) - if ( !SyncAuthentication::isAuthorized(peeraddress) ) { - state = Forbidden; - startTimer( 0 ); - } - else + if ( !SyncAuthentication::isAuthorized(peeraddress) ) { + state = Forbidden; + startTimer( 0 ); + } + else #endif - { - connect( this, SIGNAL( readyRead() ), SLOT( read() ) ); - connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); + { + connect( this, SIGNAL( readyRead() ), SLOT( read() ) ); + connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); - passiv = FALSE; - for ( int i = 0; i < 4; i++ ) - wait[i] = FALSE; + passiv = FALSE; + for ( int i = 0; i < 4; i++ ) + wait[i] = FALSE; - send( "220 Qtopia " QPE_VERSION " FTP Server" ); - state = Wait_USER; + send( "220 Qtopia " QPE_VERSION " FTP Server" ); + state = Wait_USER; - dtp = new ServerDTP( this ); - connect( dtp, SIGNAL( completed() ), SLOT( dtpCompleted() ) ); - connect( dtp, SIGNAL( failed() ), SLOT( dtpFailed() ) ); - connect( dtp, SIGNAL( error(int) ), SLOT( dtpError(int) ) ); + dtp = new ServerDTP( this ); + connect( dtp, SIGNAL( completed() ), SLOT( dtpCompleted() ) ); + connect( dtp, SIGNAL( failed() ), SLOT( dtpFailed() ) ); + connect( dtp, SIGNAL( error(int) ), SLOT( dtpError(int) ) ); - directory = QDir::currentDirPath(); + directory = QDir::currentDirPath(); - static int p = 1024; + static int p = 1024; - while ( !serversocket || !serversocket->ok() ) { - delete serversocket; - serversocket = new ServerSocket( ++p, this ); - } - connect( serversocket, SIGNAL( newIncomming(int) ), - SLOT( newConnection(int) ) ); - } + while ( !serversocket || !serversocket->ok() ) { + delete serversocket; + serversocket = new ServerSocket( ++p, this ); + } + connect( serversocket, SIGNAL( newIncomming(int) ), + SLOT( newConnection(int) ) ); + } } @@ -319,4 +323,4 @@ void ServerPI::connectionClosed() { - // qDebug( "Debug: Connection closed" ); - delete this; + // odebug << "Debug: Connection closed" << oendl; + delete this; } @@ -325,5 +329,5 @@ void ServerPI::send( const QString& msg ) { - QTextStream os( this ); - os << msg << endl; - //qDebug( "Reply: %s", msg.latin1() ); + QTextStream os( this ); + os << msg << endl; + //odebug << "Reply: " << msg << "" << oendl; } @@ -332,4 +336,4 @@ void ServerPI::read() { - while ( canReadLine() ) - process( readLine().stripWhiteSpace() ); + while ( canReadLine() ) + process( readLine().stripWhiteSpace() ); } @@ -338,11 +342,11 @@ bool ServerPI::checkReadFile( const QString& file ) { - QString filename; + QString filename; - if ( file[0] != "/" ) - filename = directory.path() + "/" + file; - else - filename = file; + if ( file[0] != "/" ) + filename = directory.path() + "/" + file; + else + filename = file; - QFileInfo fi( filename ); - return ( fi.exists() && fi.isReadable() ); + QFileInfo fi( filename ); + return ( fi.exists() && fi.isReadable() ); } @@ -351,15 +355,15 @@ bool ServerPI::checkWriteFile( const QString& file ) { - QString filename; + QString filename; - if ( file[0] != "/" ) - filename = directory.path() + "/" + file; - else - filename = file; + if ( file[0] != "/" ) + filename = directory.path() + "/" + file; + else + filename = file; - QFileInfo fi( filename ); + QFileInfo fi( filename ); - if ( fi.exists() ) - if ( !QFile( filename ).remove() ) - return FALSE; - return TRUE; + if ( fi.exists() ) + if ( !QFile( filename ).remove() ) + return FALSE; + return TRUE; } @@ -368,358 +372,358 @@ void ServerPI::process( const QString& message ) { - //qDebug( "Command: %s", message.latin1() ); - - // split message using "," as separator - QStringList msg = QStringList::split( " ", message ); - if ( msg.isEmpty() ) - return ; - - // command token - QString cmd = msg[0].upper(); - - // argument token - QString arg; - if ( msg.count() >= 2 ) - arg = msg[1]; - - // full argument string - QString args; - if ( msg.count() >= 2 ) { - QStringList copy( msg ); - // FIXME: for Qt3 - // copy.pop_front() - copy.remove( copy.begin() ); - args = copy.join( " " ); - } - - //qDebug( "args: %s", args.latin1() ); - - // we always respond to QUIT, regardless of state - if ( cmd == "QUIT" ) { - send( "211 Good bye!" ); - delete this; - return ; - } - - // connected to client - if ( Connected == state ) - return ; - - // waiting for user name - if ( Wait_USER == state ) { - - if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) { - send( "530 Please login with USER and PASS" ); - return ; - } - send( "331 User name ok, need password" ); - state = Wait_PASS; - return ; - } - - // waiting for password - if ( Wait_PASS == state ) { - - if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) { - send( "530 Please login with USER and PASS" ); - return ; - } - send( "230 User logged in, proceed" ); - state = Ready; - return ; - } - - // ACCESS CONTROL COMMANDS - - - // account (ACCT) - if ( cmd == "ACCT" ) { - // even wu-ftp does not support it - send( "502 Command not implemented" ); - } - - // change working directory (CWD) - else if ( cmd == "CWD" ) { - - if ( !args.isEmpty() ) { - if ( directory.cd( args, TRUE ) ) - send( "250 Requested file action okay, completed" ); - else - send( "550 Requested action not taken" ); - } - else - send( "500 Syntax error, command unrecognized" ); - } - - // change to parent directory (CDUP) - else if ( cmd == "CDUP" ) { - if ( directory.cdUp() ) - send( "250 Requested file action okay, completed" ); - else - send( "550 Requested action not taken" ); - } - - // structure mount (SMNT) - else if ( cmd == "SMNT" ) { - // even wu-ftp does not support it - send( "502 Command not implemented" ); - } - - // reinitialize (REIN) - else if ( cmd == "REIN" ) { - // even wu-ftp does not support it - send( "502 Command not implemented" ); - } - - - // TRANSFER PARAMETER COMMANDS - - - // data port (PORT) - else if ( cmd == "PORT" ) { - if ( parsePort( arg ) ) - send( "200 Command okay" ); - else - send( "500 Syntax error, command unrecognized" ); - } - - // passive (PASV) - else if ( cmd == "PASV" ) { - passiv = TRUE; - send( "227 Entering Passive Mode (" - + address().toString().replace( QRegExp( "\\." ), "," ) + "," - + QString::number( ( serversocket->port() ) >> 8 ) + "," - + QString::number( ( serversocket->port() ) & 0xFF ) + ")" ); - } - - // representation type (TYPE) - else if ( cmd == "TYPE" ) { - if ( arg.upper() == "A" || arg.upper() == "I" ) - send( "200 Command okay" ); - else - send( "504 Command not implemented for that parameter" ); - } - - // file structure (STRU) - else if ( cmd == "STRU" ) { - if ( arg.upper() == "F" ) - send( "200 Command okay" ); - else - send( "504 Command not implemented for that parameter" ); - } - - // transfer mode (MODE) - else if ( cmd == "MODE" ) { - if ( arg.upper() == "S" ) - send( "200 Command okay" ); - else - send( "504 Command not implemented for that parameter" ); - } - - - // FTP SERVICE COMMANDS - - - // retrieve (RETR) - else if ( cmd == "RETR" ) - if ( !args.isEmpty() && checkReadFile( absFilePath( args ) ) - || backupRestoreGzip( absFilePath( args ) ) ) { - send( "150 File status okay" ); - sendFile( absFilePath( args ) ); - } - else { - qDebug("550 Requested action not taken"); - send( "550 Requested action not taken" ); - } - - // store (STOR) - else if ( cmd == "STOR" ) - if ( !args.isEmpty() && checkWriteFile( absFilePath( args ) ) ) { - send( "150 File status okay" ); - retrieveFile( absFilePath( args ) ); - } - else - send( "550 Requested action not taken" ); - - // store unique (STOU) - else if ( cmd == "STOU" ) { - send( "502 Command not implemented" ); - } - - // append (APPE) - else if ( cmd == "APPE" ) { - send( "502 Command not implemented" ); - } - - // allocate (ALLO) - else if ( cmd == "ALLO" ) { - send( "200 Command okay" ); - } - - // restart (REST) - else if ( cmd == "REST" ) { - send( "502 Command not implemented" ); - } - - // rename from (RNFR) - else if ( cmd == "RNFR" ) { - renameFrom = QString::null; - if ( args.isEmpty() ) - send( "500 Syntax error, command unrecognized" ); - else { - QFile file( absFilePath( args ) ); - if ( file.exists() ) { - send( "350 File exists, ready for destination name" ); - renameFrom = absFilePath( args ); - } - else - send( "550 Requested action not taken" ); - } - } - - // rename to (RNTO) - else if ( cmd == "RNTO" ) { - if ( lastCommand != "RNFR" ) - send( "503 Bad sequence of commands" ); - else if ( args.isEmpty() ) - send( "500 Syntax error, command unrecognized" ); - else { - QDir dir( absFilePath( args ) ); - if ( dir.rename( renameFrom, absFilePath( args ), TRUE ) ) - send( "250 Requested file action okay, completed." ); - else - send( "550 Requested action not taken" ); - } - } - - // abort (ABOR) - else if ( cmd.contains( "ABOR" ) ) { - dtp->close(); - if ( dtp->dtpMode() != ServerDTP::Idle ) - send( "426 Connection closed; transfer aborted" ); - else - send( "226 Closing data connection" ); - } - - // delete (DELE) - else if ( cmd == "DELE" ) { - if ( args.isEmpty() ) - send( "500 Syntax error, command unrecognized" ); - else { - QFile file( absFilePath( args ) ) ; - if ( file.remove() ) { - send( "250 Requested file action okay, completed" ); - QCopEnvelope e("QPE/System", "linkChanged(QString)" ); - e << file.name(); - } - else { - send( "550 Requested action not taken" ); - } - } - } - - // remove directory (RMD) - else if ( cmd == "RMD" ) { - if ( args.isEmpty() ) - send( "500 Syntax error, command unrecognized" ); - else { - QDir dir; - if ( dir.rmdir( absFilePath( args ), TRUE ) ) - send( "250 Requested file action okay, completed" ); - else - send( "550 Requested action not taken" ); - } - } - - // make directory (MKD) - else if ( cmd == "MKD" ) { - if ( args.isEmpty() ) { - qDebug(" Error: no arg"); - send( "500 Syntax error, command unrecognized" ); - } - else { - QDir dir; - if ( dir.mkdir( absFilePath( args ), TRUE ) ) - send( "250 Requested file action okay, completed." ); - else - send( "550 Requested action not taken" ); - } - } - - // print working directory (PWD) - else if ( cmd == "PWD" ) { - send( "257 \"" + directory.path() + "\"" ); - } - - // list (LIST) - else if ( cmd == "LIST" ) { - if ( sendList( absFilePath( args ) ) ) - send( "150 File status okay" ); - else - send( "500 Syntax error, command unrecognized" ); - } - - // size (SIZE) - else if ( cmd == "SIZE" ) { - QString filePath = absFilePath( args ); - QFileInfo fi( filePath ); - bool gzipfile = backupRestoreGzip( filePath ); - if ( !fi.exists() && !gzipfile ) - send( "500 Syntax error, command unrecognized" ); - else { - if ( !gzipfile ) - send( "213 " + QString::number( fi.size() ) ); - else { - Process duproc( QString("du") ); - duproc.addArgument("-s"); - QString in, out; - if ( !duproc.exec(in, out) ) { - qDebug("du process failed; just sending back 1K"); - send( "213 1024"); - } - else { - QString size = out.left( out.find("\t") ); - int guess = size.toInt() / 5; - if ( filePath.contains("doc") ) - guess *= 1000; - qDebug("sending back gzip guess of %d", guess); - send( "213 " + QString::number(guess) ); - } - } - } - } - // name list (NLST) - else if ( cmd == "NLST" ) { - send( "502 Command not implemented" ); - } - - // site parameters (SITE) - else if ( cmd == "SITE" ) { - send( "502 Command not implemented" ); - } - - // system (SYST) - else if ( cmd == "SYST" ) { - send( "215 UNIX Type: L8" ); - } - - // status (STAT) - else if ( cmd == "STAT" ) { - send( "502 Command not implemented" ); - } - - // help (HELP ) - else if ( cmd == "HELP" ) { - send( "502 Command not implemented" ); - } - - // noop (NOOP) - else if ( cmd == "NOOP" ) { - send( "200 Command okay" ); - } - - // not implemented - else - send( "502 Command not implemented" ); - - lastCommand = cmd; + //odebug << "Command: " << message << "" << oendl; + + // split message using "," as separator + QStringList msg = QStringList::split( " ", message ); + if ( msg.isEmpty() ) + return ; + + // command token + QString cmd = msg[0].upper(); + + // argument token + QString arg; + if ( msg.count() >= 2 ) + arg = msg[1]; + + // full argument string + QString args; + if ( msg.count() >= 2 ) { + QStringList copy( msg ); + // FIXME: for Qt3 + // copy.pop_front() + copy.remove( copy.begin() ); + args = copy.join( " " ); + } + + //odebug << "args: " << args << "" << oendl; + + // we always respond to QUIT, regardless of state + if ( cmd == "QUIT" ) { + send( "211 Good bye!" ); + delete this; + return ; + } + + // connected to client + if ( Connected == state ) + return ; + + // waiting for user name + if ( Wait_USER == state ) { + + if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) { + send( "530 Please login with USER and PASS" ); + return ; + } + send( "331 User name ok, need password" ); + state = Wait_PASS; + return ; + } + + // waiting for password + if ( Wait_PASS == state ) { + + if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) { + send( "530 Please login with USER and PASS" ); + return ; + } + send( "230 User logged in, proceed" ); + state = Ready; + return ; + } + + // ACCESS CONTROL COMMANDS + + + // account (ACCT) + if ( cmd == "ACCT" ) { + // even wu-ftp does not support it + send( "502 Command not implemented" ); + } + + // change working directory (CWD) + else if ( cmd == "CWD" ) { + + if ( !args.isEmpty() ) { + if ( directory.cd( args, TRUE ) ) + send( "250 Requested file action okay, completed" ); + else + send( "550 Requested action not taken" ); + } + else + send( "500 Syntax error, command unrecognized" ); + } + + // change to parent directory (CDUP) + else if ( cmd == "CDUP" ) { + if ( directory.cdUp() ) + send( "250 Requested file action okay, completed" ); + else + send( "550 Requested action not taken" ); + } + + // structure mount (SMNT) + else if ( cmd == "SMNT" ) { + // even wu-ftp does not support it + send( "502 Command not implemented" ); + } + + // reinitialize (REIN) + else if ( cmd == "REIN" ) { + // even wu-ftp does not support it + send( "502 Command not implemented" ); + } + + + // TRANSFER PARAMETER COMMANDS + + + // data port (PORT) + else if ( cmd == "PORT" ) { + if ( parsePort( arg ) ) + send( "200 Command okay" ); + else + send( "500 Syntax error, command unrecognized" ); + } + + // passive (PASV) + else if ( cmd == "PASV" ) { + passiv = TRUE; + send( "227 Entering Passive Mode (" + + address().toString().replace( QRegExp( "\\." ), "," ) + "," + + QString::number( ( serversocket->port() ) >> 8 ) + "," + + QString::number( ( serversocket->port() ) & 0xFF ) + ")" ); + } + + // representation type (TYPE) + else if ( cmd == "TYPE" ) { + if ( arg.upper() == "A" || arg.upper() == "I" ) + send( "200 Command okay" ); + else + send( "504 Command not implemented for that parameter" ); + } + + // file structure (STRU) + else if ( cmd == "STRU" ) { + if ( arg.upper() == "F" ) + send( "200 Command okay" ); + else + send( "504 Command not implemented for that parameter" ); + } + + // transfer mode (MODE) + else if ( cmd == "MODE" ) { + if ( arg.upper() == "S" ) + send( "200 Command okay" ); + else + send( "504 Command not implemented for that parameter" ); + } + + + // FTP SERVICE COMMANDS + + + // retrieve (RETR) + else if ( cmd == "RETR" ) + if ( !args.isEmpty() && checkReadFile( absFilePath( args ) ) + || backupRestoreGzip( absFilePath( args ) ) ) { + send( "150 File status okay" ); + sendFile( absFilePath( args ) ); + } + else { + odebug << "550 Requested action not taken" << oendl; + send( "550 Requested action not taken" ); + } + + // store (STOR) + else if ( cmd == "STOR" ) + if ( !args.isEmpty() && checkWriteFile( absFilePath( args ) ) ) { + send( "150 File status okay" ); + retrieveFile( absFilePath( args ) ); + } + else + send( "550 Requested action not taken" ); + + // store unique (STOU) + else if ( cmd == "STOU" ) { + send( "502 Command not implemented" ); + } + + // append (APPE) + else if ( cmd == "APPE" ) { + send( "502 Command not implemented" ); + } + + // allocate (ALLO) + else if ( cmd == "ALLO" ) { + send( "200 Command okay" ); + } + + // restart (REST) + else if ( cmd == "REST" ) { + send( "502 Command not implemented" ); + } + + // rename from (RNFR) + else if ( cmd == "RNFR" ) { + renameFrom = QString::null; + if ( args.isEmpty() ) + send( "500 Syntax error, command unrecognized" ); + else { + QFile file( absFilePath( args ) ); + if ( file.exists() ) { + send( "350 File exists, ready for destination name" ); + renameFrom = absFilePath( args ); + } + else + send( "550 Requested action not taken" ); + } + } + + // rename to (RNTO) + else if ( cmd == "RNTO" ) { + if ( lastCommand != "RNFR" ) + send( "503 Bad sequence of commands" ); + else if ( args.isEmpty() ) + send( "500 Syntax error, command unrecognized" ); + else { + QDir dir( absFilePath( args ) ); + if ( dir.rename( renameFrom, absFilePath( args ), TRUE ) ) + send( "250 Requested file action okay, completed." ); + else + send( "550 Requested action not taken" ); + } + } + + // abort (ABOR) + else if ( cmd.contains( "ABOR" ) ) { + dtp->close(); + if ( dtp->dtpMode() != ServerDTP::Idle ) + send( "426 Connection closed; transfer aborted" ); + else + send( "226 Closing data connection" ); + } + + // delete (DELE) + else if ( cmd == "DELE" ) { + if ( args.isEmpty() ) + send( "500 Syntax error, command unrecognized" ); + else { + QFile file( absFilePath( args ) ) ; + if ( file.remove() ) { + send( "250 Requested file action okay, completed" ); + QCopEnvelope e("QPE/System", "linkChanged(QString)" ); + e << file.name(); + } + else { + send( "550 Requested action not taken" ); + } + } + } + + // remove directory (RMD) + else if ( cmd == "RMD" ) { + if ( args.isEmpty() ) + send( "500 Syntax error, command unrecognized" ); + else { + QDir dir; + if ( dir.rmdir( absFilePath( args ), TRUE ) ) + send( "250 Requested file action okay, completed" ); + else + send( "550 Requested action not taken" ); + } + } + + // make directory (MKD) + else if ( cmd == "MKD" ) { + if ( args.isEmpty() ) { + odebug << " Error: no arg" << oendl; + send( "500 Syntax error, command unrecognized" ); + } + else { + QDir dir; + if ( dir.mkdir( absFilePath( args ), TRUE ) ) + send( "250 Requested file action okay, completed." ); + else + send( "550 Requested action not taken" ); + } + } + + // print working directory (PWD) + else if ( cmd == "PWD" ) { + send( "257 \"" + directory.path() + "\"" ); + } + + // list (LIST) + else if ( cmd == "LIST" ) { + if ( sendList( absFilePath( args ) ) ) + send( "150 File status okay" ); + else + send( "500 Syntax error, command unrecognized" ); + } + + // size (SIZE) + else if ( cmd == "SIZE" ) { + QString filePath = absFilePath( args ); + QFileInfo fi( filePath ); + bool gzipfile = backupRestoreGzip( filePath ); + if ( !fi.exists() && !gzipfile ) + send( "500 Syntax error, command unrecognized" ); + else { + if ( !gzipfile ) + send( "213 " + QString::number( fi.size() ) ); + else { + Process duproc( QString("du") ); + duproc.addArgument("-s"); + QString in, out; + if ( !duproc.exec(in, out) ) { + odebug << "du process failed; just sending back 1K" << oendl; + send( "213 1024"); + } + else { + QString size = out.left( out.find("\t") ); + int guess = size.toInt() / 5; + if ( filePath.contains("doc") ) + guess *= 1000; + odebug << "sending back gzip guess of " << guess << "" << oendl; + send( "213 " + QString::number(guess) ); + } + } + } + } + // name list (NLST) + else if ( cmd == "NLST" ) { + send( "502 Command not implemented" ); + } + + // site parameters (SITE) + else if ( cmd == "SITE" ) { + send( "502 Command not implemented" ); + } + + // system (SYST) + else if ( cmd == "SYST" ) { + send( "215 UNIX Type: L8" ); + } + + // status (STAT) + else if ( cmd == "STAT" ) { + send( "502 Command not implemented" ); + } + + // help (HELP ) + else if ( cmd == "HELP" ) { + send( "502 Command not implemented" ); + } + + // noop (NOOP) + else if ( cmd == "NOOP" ) { + send( "200 Command okay" ); + } + + // not implemented + else + send( "502 Command not implemented" ); + + lastCommand = cmd; } @@ -728,4 +732,4 @@ bool ServerPI::backupRestoreGzip( const QString &file ) { - return (file.find( "backup" ) != -1 && - file.findRev( ".tgz" ) == (int)file.length() - 4 ); + return (file.find( "backup" ) != -1 && + file.findRev( ".tgz" ) == (int)file.length() - 4 ); } @@ -734,11 +738,11 @@ bool ServerPI::backupRestoreGzip( const QString &file, QStringList &targets ) { - if ( file.find( "backup" ) != -1 && - file.findRev( ".tgz" ) == (int)file.length() - 4 ) { - QFileInfo info( file ); - targets = info.dirPath( TRUE ); - qDebug("ServerPI::backupRestoreGzip for %s = %s", file.latin1(), - targets.join(" ").latin1() ); - return true; - } - return false; + if ( file.find( "backup" ) != -1 && + file.findRev( ".tgz" ) == (int)file.length() - 4 ) { + QFileInfo info( file ); + targets = info.dirPath( TRUE ); + odebug << "ServerPI::backupRestoreGzip for " << file.latin1() << " = " + << targets.join(" ").latin1() << oendl; + return true; + } + return false; } @@ -747,15 +751,15 @@ void ServerPI::sendFile( const QString& file ) { - if ( passiv ) { - wait[SendFile] = TRUE; - waitfile = file; - if ( waitsocket ) - newConnection( waitsocket ); - } - else { - QStringList targets; - if ( backupRestoreGzip( file, targets ) ) - dtp->sendGzipFile( file, targets, peeraddress, peerport ); - else - dtp->sendFile( file, peeraddress, peerport ); - } + if ( passiv ) { + wait[SendFile] = TRUE; + waitfile = file; + if ( waitsocket ) + newConnection( waitsocket ); + } + else { + QStringList targets; + if ( backupRestoreGzip( file, targets ) ) + dtp->sendGzipFile( file, targets, peeraddress, peerport ); + else + dtp->sendFile( file, peeraddress, peerport ); + } } @@ -764,15 +768,15 @@ void ServerPI::retrieveFile( const QString& file ) { - if ( passiv ) { - wait[RetrieveFile] = TRUE; - waitfile = file; - if ( waitsocket ) - newConnection( waitsocket ); - } - else { - QStringList targets; - if ( backupRestoreGzip( file, targets ) ) - dtp->retrieveGzipFile( file, peeraddress, peerport ); - else - dtp->retrieveFile( file, peeraddress, peerport ); - } + if ( passiv ) { + wait[RetrieveFile] = TRUE; + waitfile = file; + if ( waitsocket ) + newConnection( waitsocket ); + } + else { + QStringList targets; + if ( backupRestoreGzip( file, targets ) ) + dtp->retrieveGzipFile( file, peeraddress, peerport ); + else + dtp->retrieveFile( file, peeraddress, peerport ); + } } @@ -781,11 +785,11 @@ bool ServerPI::parsePort( const QString& pp ) { - QStringList p = QStringList::split( ",", pp ); - if ( p.count() != 6 ) - return FALSE; - - // h1,h2,h3,h4,p1,p2 - peeraddress = QHostAddress( ( p[0].toInt() << 24 ) + ( p[1].toInt() << 16 ) + - ( p[2].toInt() << 8 ) + p[3].toInt() ); - peerport = ( p[4].toInt() << 8 ) + p[5].toInt(); - return TRUE; + QStringList p = QStringList::split( ",", pp ); + if ( p.count() != 6 ) + return FALSE; + + // h1,h2,h3,h4,p1,p2 + peeraddress = QHostAddress( ( p[0].toInt() << 24 ) + ( p[1].toInt() << 16 ) + + ( p[2].toInt() << 8 ) + p[3].toInt() ); + peerport = ( p[4].toInt() << 8 ) + p[5].toInt(); + return TRUE; } @@ -794,12 +798,12 @@ void ServerPI::dtpCompleted() { - send( "226 Closing data connection, file transfer successful" ); - if ( dtp->dtpMode() == ServerDTP::RetrieveFile ) { - QString fn = dtp->fileName(); - if ( fn.right(8) == ".desktop" && fn.find("/Documents/") >= 0 ) { - QCopEnvelope e("QPE/System", "linkChanged(QString)" ); - e << fn; - } - } - waitsocket = 0; - dtp->close(); + send( "226 Closing data connection, file transfer successful" ); + if ( dtp->dtpMode() == ServerDTP::RetrieveFile ) { + QString fn = dtp->fileName(); + if ( fn.right(8) == ".desktop" && fn.find("/Documents/") >= 0 ) { + QCopEnvelope e("QPE/System", "linkChanged(QString)" ); + e << fn; + } + } + waitsocket = 0; + dtp->close(); } @@ -808,5 +812,5 @@ void ServerPI::dtpFailed() { - dtp->close(); - waitsocket = 0; - send( "451 Requested action aborted: local error in processing" ); + dtp->close(); + waitsocket = 0; + send( "451 Requested action aborted: local error in processing" ); } @@ -815,5 +819,5 @@ void ServerPI::dtpError( int ) { - dtp->close(); - waitsocket = 0; - send( "451 Requested action aborted: local error in processing" ); + dtp->close(); + waitsocket = 0; + send( "451 Requested action aborted: local error in processing" ); } @@ -822,60 +826,60 @@ bool ServerPI::sendList( const QString& arg ) { - QByteArray listing; - QBuffer buffer( listing ); - - if ( !buffer.open( IO_WriteOnly ) ) - return FALSE; - - QTextStream ts( &buffer ); - QString fn = arg; - - if ( fn.isEmpty() ) - fn = directory.path(); - - QFileInfo fi( fn ); - if ( !fi.exists() ) - return FALSE; - - // return file listing - if ( fi.isFile() ) { - ts << fileListing( &fi ) << endl; - } - - // return directory listing - else if ( fi.isDir() ) { - QDir dir( fn ); - const QFileInfoList *list = dir.entryInfoList( QDir::All | QDir::Hidden ); - - QFileInfoListIterator it( *list ); - QFileInfo *info; - - unsigned long total = 0; - while ( ( info = it.current() ) ) { - if ( info->fileName() != "." && info->fileName() != ".." ) - total += info->size(); - ++it; - } - - ts << "total " << QString::number( total / 1024 ) << endl; - - it.toFirst(); - while ( ( info = it.current() ) ) { - if ( info->fileName() == "." || info->fileName() == ".." ) { - ++it; - continue; - } - ts << fileListing( info ) << endl; - ++it; - } - } - - if ( passiv ) { - waitarray = buffer.buffer(); - wait[SendByteArray] = TRUE; - if ( waitsocket ) - newConnection( waitsocket ); - } - else - dtp->sendByteArray( buffer.buffer(), peeraddress, peerport ); - return TRUE; + QByteArray listing; + QBuffer buffer( listing ); + + if ( !buffer.open( IO_WriteOnly ) ) + return FALSE; + + QTextStream ts( &buffer ); + QString fn = arg; + + if ( fn.isEmpty() ) + fn = directory.path(); + + QFileInfo fi( fn ); + if ( !fi.exists() ) + return FALSE; + + // return file listing + if ( fi.isFile() ) { + ts << fileListing( &fi ) << endl; + } + + // return directory listing + else if ( fi.isDir() ) { + QDir dir( fn ); + const QFileInfoList *list = dir.entryInfoList( QDir::All | QDir::Hidden ); + + QFileInfoListIterator it( *list ); + QFileInfo *info; + + unsigned long total = 0; + while ( ( info = it.current() ) ) { + if ( info->fileName() != "." && info->fileName() != ".." ) + total += info->size(); + ++it; + } + + ts << "total " << QString::number( total / 1024 ) << endl; + + it.toFirst(); + while ( ( info = it.current() ) ) { + if ( info->fileName() == "." || info->fileName() == ".." ) { + ++it; + continue; + } + ts << fileListing( info ) << endl; + ++it; + } + } + + if ( passiv ) { + waitarray = buffer.buffer(); + wait[SendByteArray] = TRUE; + if ( waitsocket ) + newConnection( waitsocket ); + } + else + dtp->sendByteArray( buffer.buffer(), peeraddress, peerport ); + return TRUE; } @@ -884,49 +888,49 @@ QString ServerPI::fileListing( QFileInfo *info ) { - if ( !info ) - return QString::null; - QString s; + if ( !info ) + return QString::null; + QString s; - // type char - if ( info->isDir() ) - s += "d"; - else if ( info->isSymLink() ) - s += "l"; - else - s += "-"; + // type char + if ( info->isDir() ) + s += "d"; + else if ( info->isSymLink() ) + s += "l"; + else + s += "-"; - // permisson string - s += permissionString( info ) + " "; + // permisson string + s += permissionString( info ) + " "; - // number of hardlinks - int subdirs = 1; + // number of hardlinks + int subdirs = 1; - if ( info->isDir() ) - subdirs = 2; - // FIXME : this is to slow - //if ( info->isDir() ) - //subdirs = QDir( info->absFilePath() ).entryList( QDir::Dirs ).count(); + if ( info->isDir() ) + subdirs = 2; + // FIXME : this is to slow + //if ( info->isDir() ) + //subdirs = QDir( info->absFilePath() ).entryList( QDir::Dirs ).count(); - s += QString::number( subdirs ).rightJustify( 3, ' ', TRUE ) + " "; + s += QString::number( subdirs ).rightJustify( 3, ' ', TRUE ) + " "; - // owner - s += info->owner().leftJustify( 8, ' ', TRUE ) + " "; + // owner + s += info->owner().leftJustify( 8, ' ', TRUE ) + " "; - // group - s += info->group().leftJustify( 8, ' ', TRUE ) + " "; + // group + s += info->group().leftJustify( 8, ' ', TRUE ) + " "; - // file size in bytes - s += QString::number( info->size() ).rightJustify( 9, ' ', TRUE ) + " "; + // file size in bytes + s += QString::number( info->size() ).rightJustify( 9, ' ', TRUE ) + " "; - // last modified date - QDate date = info->lastModified().date(); - QTime time = info->lastModified().time(); - s += date.monthName( date.month() ) + " " - + QString::number( date.day() ).rightJustify( 2, ' ', TRUE ) + " " - + QString::number( time.hour() ).rightJustify( 2, '0', TRUE ) + ":" - + QString::number( time.minute() ).rightJustify( 2, '0', TRUE ) + " "; + // last modified date + QDate date = info->lastModified().date(); + QTime time = info->lastModified().time(); + s += date.monthName( date.month() ) + " " + + QString::number( date.day() ).rightJustify( 2, ' ', TRUE ) + " " + + QString::number( time.hour() ).rightJustify( 2, '0', TRUE ) + ":" + + QString::number( time.minute() ).rightJustify( 2, '0', TRUE ) + " "; - // file name - s += info->fileName(); + // file name + s += info->fileName(); - return s; + return s; } @@ -935,49 +939,49 @@ QString ServerPI::permissionString( QFileInfo *info ) { - if ( !info ) - return QString( "---------" ); - QString s; - - // user - if ( info->permission( QFileInfo::ReadUser ) ) - s += "r"; - else - s += "-"; - if ( info->permission( QFileInfo::WriteUser ) ) - s += "w"; - else - s += "-"; - if ( info->permission( QFileInfo::ExeUser ) ) - s += "x"; - else - s += "-"; - - // group - if ( info->permission( QFileInfo::ReadGroup ) ) - s += "r"; - else - s += "-"; - if ( info->permission( QFileInfo::WriteGroup ) ) - s += "w"; - else - s += "-"; - if ( info->permission( QFileInfo::ExeGroup ) ) - s += "x"; - else - s += "-"; - - // exec - if ( info->permission( QFileInfo::ReadOther ) ) - s += "r"; - else - s += "-"; - if ( info->permission( QFileInfo::WriteOther ) ) - s += "w"; - else - s += "-"; - if ( info->permission( QFileInfo::ExeOther ) ) - s += "x"; - else - s += "-"; - - return s; + if ( !info ) + return QString( "---------" ); + QString s; + + // user + if ( info->permission( QFileInfo::ReadUser ) ) + s += "r"; + else + s += "-"; + if ( info->permission( QFileInfo::WriteUser ) ) + s += "w"; + else + s += "-"; + if ( info->permission( QFileInfo::ExeUser ) ) + s += "x"; + else + s += "-"; + + // group + if ( info->permission( QFileInfo::ReadGroup ) ) + s += "r"; + else + s += "-"; + if ( info->permission( QFileInfo::WriteGroup ) ) + s += "w"; + else + s += "-"; + if ( info->permission( QFileInfo::ExeGroup ) ) + s += "x"; + else + s += "-"; + + // exec + if ( info->permission( QFileInfo::ReadOther ) ) + s += "r"; + else + s += "-"; + if ( info->permission( QFileInfo::WriteOther ) ) + s += "w"; + else + s += "-"; + if ( info->permission( QFileInfo::ExeOther ) ) + s += "x"; + else + s += "-"; + + return s; } @@ -986,37 +990,37 @@ void ServerPI::newConnection( int socket ) { - //qDebug( "New incomming connection" ); - - if ( !passiv ) - return ; - - if ( wait[SendFile] ) { - QStringList targets; - if ( backupRestoreGzip( waitfile, targets ) ) - dtp->sendGzipFile( waitfile, targets ); - else - dtp->sendFile( waitfile ); - dtp->setSocket( socket ); - } - else if ( wait[RetrieveFile] ) { - qDebug("check retrieve file"); - if ( backupRestoreGzip( waitfile ) ) - dtp->retrieveGzipFile( waitfile ); - else - dtp->retrieveFile( waitfile ); - dtp->setSocket( socket ); - } - else if ( wait[SendByteArray] ) { - dtp->sendByteArray( waitarray ); - dtp->setSocket( socket ); - } - else if ( wait[RetrieveByteArray] ) { - qDebug("retrieve byte array"); - dtp->retrieveByteArray(); - dtp->setSocket( socket ); - } - else - waitsocket = socket; - - for ( int i = 0; i < 4; i++ ) - wait[i] = FALSE; + //odebug << "New incomming connection" << oendl; + + if ( !passiv ) + return ; + + if ( wait[SendFile] ) { + QStringList targets; + if ( backupRestoreGzip( waitfile, targets ) ) + dtp->sendGzipFile( waitfile, targets ); + else + dtp->sendFile( waitfile ); + dtp->setSocket( socket ); + } + else if ( wait[RetrieveFile] ) { + odebug << "check retrieve file" << oendl; + if ( backupRestoreGzip( waitfile ) ) + dtp->retrieveGzipFile( waitfile ); + else + dtp->retrieveFile( waitfile ); + dtp->setSocket( socket ); + } + else if ( wait[SendByteArray] ) { + dtp->sendByteArray( waitarray ); + dtp->setSocket( socket ); + } + else if ( wait[RetrieveByteArray] ) { + odebug << "retrieve byte array" << oendl; + dtp->retrieveByteArray(); + dtp->setSocket( socket ); + } + else + waitsocket = socket; + + for ( int i = 0; i < 4; i++ ) + wait[i] = FALSE; } @@ -1025,10 +1029,10 @@ QString ServerPI::absFilePath( const QString& file ) { - if ( file.isEmpty() ) - return file; + if ( file.isEmpty() ) + return file; - QString filepath( file ); - if ( file[0] != "/" ) - filepath = directory.path() + "/" + file; + QString filepath( file ); + if ( file[0] != "/" ) + filepath = directory.path() + "/" + file; - return filepath; + return filepath; } @@ -1038,3 +1042,3 @@ void ServerPI::timerEvent( QTimerEvent * ) { - connectionClosed(); + connectionClosed(); } @@ -1043,25 +1047,25 @@ void ServerPI::timerEvent( QTimerEvent * ) ServerDTP::ServerDTP( QObject *parent, const char* name) - : QSocket( parent, name ), mode( Idle ), createTargzProc( 0 ), - retrieveTargzProc( 0 ), gzipProc( 0 ) + : QSocket( parent, name ), mode( Idle ), createTargzProc( 0 ), + retrieveTargzProc( 0 ), gzipProc( 0 ) { - connect( this, SIGNAL( connected() ), SLOT( connected() ) ); - connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); - connect( this, SIGNAL( bytesWritten(int) ), SLOT( bytesWritten(int) ) ); - connect( this, SIGNAL( readyRead() ), SLOT( readyRead() ) ); - - gzipProc = new Opie::Core::OProcess( this, "gzipProc" ); - - createTargzProc = new Opie::Core::OProcess( QString("tar"), this, "createTargzProc"); - createTargzProc->setWorkingDirectory( QDir::rootDirPath() ); - connect( createTargzProc, SIGNAL( processExited(Opie::Core::OProcess*) ), SLOT( targzDone() ) ); - - QStringList args = "tar"; - args += "-xv"; - retrieveTargzProc = new Opie::Core::OProcess( args, this, "retrieveTargzProc" ); - retrieveTargzProc->setWorkingDirectory( QDir::rootDirPath() ); - connect( retrieveTargzProc, SIGNAL( processExited(Opie::Core::OProcess*) ), - SIGNAL( completed() ) ); - connect( retrieveTargzProc, SIGNAL( processExited(Opie::Core::OProcess*) ), - SLOT( extractTarDone() ) ); + connect( this, SIGNAL( connected() ), SLOT( connected() ) ); + connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); + connect( this, SIGNAL( bytesWritten(int) ), SLOT( bytesWritten(int) ) ); + connect( this, SIGNAL( readyRead() ), SLOT( readyRead() ) ); + + gzipProc = new Opie::Core::OProcess( this, "gzipProc" ); + + createTargzProc = new Opie::Core::OProcess( QString("tar"), this, "createTargzProc"); + createTargzProc->setWorkingDirectory( QDir::rootDirPath() ); + connect( createTargzProc, SIGNAL( processExited(Opie::Core::OProcess*) ), SLOT( targzDone() ) ); + + QStringList args = "tar"; + args += "-xv"; + retrieveTargzProc = new Opie::Core::OProcess( args, this, "retrieveTargzProc" ); + retrieveTargzProc->setWorkingDirectory( QDir::rootDirPath() ); + connect( retrieveTargzProc, SIGNAL( processExited(Opie::Core::OProcess*) ), + SIGNAL( completed() ) ); + connect( retrieveTargzProc, SIGNAL( processExited(Opie::Core::OProcess*) ), + SLOT( extractTarDone() ) ); } @@ -1070,5 +1074,5 @@ ServerDTP::~ServerDTP() { - buf.close(); - file.close(); - createTargzProc->kill(); + buf.close(); + file.close(); + createTargzProc->kill(); } @@ -1077,7 +1081,7 @@ void ServerDTP::extractTarDone() { - qDebug("extract done"); + odebug << "extract done" << oendl; #ifndef QT_NO_COP - QCopEnvelope e( "QPE/Desktop", "restoreDone(QString)" ); - e << file.name(); + QCopEnvelope e( "QPE/Desktop", "restoreDone(QString)" ); + e << file.name(); #endif @@ -1087,88 +1091,88 @@ void ServerDTP::connected() { - // send file mode - switch ( mode ) { - case SendFile : - if ( !file.exists() || !file.open( IO_ReadOnly) ) { - emit failed(); - mode = Idle; - return ; - } - - //qDebug( "Debug: Sending file '%s'", file.name().latin1() ); - - bytes_written = 0; - if ( file.size() == 0 ) { - //make sure it doesn't hang on empty files - file.close(); - emit completed(); - mode = Idle; - } - else { - - if ( !file.atEnd() ) { - QCString s; - s.resize( block_size ); - int bytes = file.readBlock( s.data(), block_size ); - writeBlock( s.data(), bytes ); - } - } - break; - case SendGzipFile: - if ( createTargzProc->isRunning() ) { - // SHOULDN'T GET HERE, BUT DOING A SAFETY CHECK ANYWAY - qWarning("Previous tar --gzip process is still running; killing it..."); - createTargzProc->kill(); - } - - bytes_written = 0; - qDebug("==>start send tar process"); - if ( !createTargzProc->start(Opie::Core::OProcess::NotifyOnExit, Opie::Core::OProcess::Stdout) ) - qWarning("Error starting %s or %s", - createTargzProc->args()[0].data(), - gzipProc->args()[0].data()); - break; - case SendBuffer: - if ( !buf.open( IO_ReadOnly) ) { - emit failed(); - mode = Idle; - return ; - } - - // qDebug( "Debug: Sending byte array" ); - bytes_written = 0; - while ( !buf.atEnd() ) - putch( buf.getch() ); - buf.close(); - break; - case RetrieveFile: - // retrieve file mode - if ( file.exists() && !file.remove() ) { - emit failed(); - mode = Idle; - return ; - } - - if ( !file.open( IO_WriteOnly) ) { - emit failed(); - mode = Idle; - return ; - } - // qDebug( "Debug: Retrieving file %s", file.name().latin1() ); - break; - case RetrieveGzipFile: - qDebug("=-> starting tar process to receive .tgz file"); - break; - case RetrieveBuffer: - // retrieve buffer mode - if ( !buf.open( IO_WriteOnly) ) { - emit failed(); - mode = Idle; - return ; - } - // qDebug( "Debug: Retrieving byte array" ); - break; - case Idle: - qDebug("connection established but mode set to Idle; BUG!"); - break; - } + // send file mode + switch ( mode ) { + case SendFile : + if ( !file.exists() || !file.open( IO_ReadOnly) ) { + emit failed(); + mode = Idle; + return ; + } + + //odebug << "Debug: Sending file '" << file.name() << "'" << oendl; + + bytes_written = 0; + if ( file.size() == 0 ) { + //make sure it doesn't hang on empty files + file.close(); + emit completed(); + mode = Idle; + } + else { + + if ( !file.atEnd() ) { + QCString s; + s.resize( block_size ); + int bytes = file.readBlock( s.data(), block_size ); + writeBlock( s.data(), bytes ); + } + } + break; + case SendGzipFile: + if ( createTargzProc->isRunning() ) { + // SHOULDN'T GET HERE, BUT DOING A SAFETY CHECK ANYWAY + owarn << "Previous tar --gzip process is still running; killing it..." << oendl; + createTargzProc->kill(); + } + + bytes_written = 0; + odebug << "==>start send tar process" << oendl; + if ( !createTargzProc->start(Opie::Core::OProcess::NotifyOnExit, Opie::Core::OProcess::Stdout) ) + qWarning("Error starting %s or %s", + createTargzProc->args()[0].data(), + gzipProc->args()[0].data()); + break; + case SendBuffer: + if ( !buf.open( IO_ReadOnly) ) { + emit failed(); + mode = Idle; + return ; + } + + // odebug << "Debug: Sending byte array" << oendl; + bytes_written = 0; + while ( !buf.atEnd() ) + putch( buf.getch() ); + buf.close(); + break; + case RetrieveFile: + // retrieve file mode + if ( file.exists() && !file.remove() ) { + emit failed(); + mode = Idle; + return ; + } + + if ( !file.open( IO_WriteOnly) ) { + emit failed(); + mode = Idle; + return ; + } + // odebug << "Debug: Retrieving file " << file.name() << "" << oendl; + break; + case RetrieveGzipFile: + odebug << "=-> starting tar process to receive .tgz file" << oendl; + break; + case RetrieveBuffer: + // retrieve buffer mode + if ( !buf.open( IO_WriteOnly) ) { + emit failed(); + mode = Idle; + return ; + } + // odebug << "Debug: Retrieving byte array" << oendl; + break; + case Idle: + odebug << "connection established but mode set to Idle; BUG!" << oendl; + break; + } } @@ -1177,39 +1181,39 @@ void ServerDTP::connectionClosed() { - //qDebug( "Debug: Data connection closed %ld bytes written", bytes_written ); - - // send file mode - if ( SendFile == mode ) { - if ( bytes_written == file.size() ) - emit completed(); - else - emit failed(); - } - - // send buffer mode - else if ( SendBuffer == mode ) { - if ( bytes_written == buf.size() ) - emit completed(); - else - emit failed(); - } - - // retrieve file mode - else if ( RetrieveFile == mode ) { - file.close(); - emit completed(); - } - - else if ( RetrieveGzipFile == mode ) { - qDebug("Done writing ungzip file; closing input"); - gzipProc->flushStdin(); - gzipProc->closeStdin(); - } - - // retrieve buffer mode - else if ( RetrieveBuffer == mode ) { - buf.close(); - emit completed(); - } - - mode = Idle; + //odebug << "Debug: Data connection closed " << bytes_written << " bytes written" << oendl; + + // send file mode + if ( SendFile == mode ) { + if ( bytes_written == file.size() ) + emit completed(); + else + emit failed(); + } + + // send buffer mode + else if ( SendBuffer == mode ) { + if ( bytes_written == buf.size() ) + emit completed(); + else + emit failed(); + } + + // retrieve file mode + else if ( RetrieveFile == mode ) { + file.close(); + emit completed(); + } + + else if ( RetrieveGzipFile == mode ) { + odebug << "Done writing ungzip file; closing input" << oendl; + gzipProc->flushStdin(); + gzipProc->closeStdin(); + } + + // retrieve buffer mode + else if ( RetrieveBuffer == mode ) { + buf.close(); + emit completed(); + } + + mode = Idle; } @@ -1218,30 +1222,30 @@ void ServerDTP::bytesWritten( int bytes ) { - bytes_written += bytes; - - // send file mode - if ( SendFile == mode ) { - - if ( bytes_written == file.size() ) { - // qDebug( "Debug: Sending complete: %d bytes", file.size() ); - file.close(); - emit completed(); - mode = Idle; - } - else if ( !file.atEnd() ) { - QCString s; - s.resize( block_size ); - int bytes = file.readBlock( s.data(), block_size ); - writeBlock( s.data(), bytes ); - } - } - - // send buffer mode - if ( SendBuffer == mode ) { - - if ( bytes_written == buf.size() ) { - // qDebug( "Debug: Sending complete: %d bytes", buf.size() ); - emit completed(); - mode = Idle; - } - } + bytes_written += bytes; + + // send file mode + if ( SendFile == mode ) { + + if ( bytes_written == file.size() ) { + // odebug << "Debug: Sending complete: " << file.size() << " bytes" << oendl; + file.close(); + emit completed(); + mode = Idle; + } + else if ( !file.atEnd() ) { + QCString s; + s.resize( block_size ); + int bytes = file.readBlock( s.data(), block_size ); + writeBlock( s.data(), bytes ); + } + } + + // send buffer mode + if ( SendBuffer == mode ) { + + if ( bytes_written == buf.size() ) { + // odebug << "Debug: Sending complete: " << buf.size() << " bytes" << oendl; + emit completed(); + mode = Idle; + } + } } @@ -1250,26 +1254,26 @@ void ServerDTP::readyRead() { - // retrieve file mode - if ( RetrieveFile == mode ) { - QCString s; - s.resize( bytesAvailable() ); - readBlock( s.data(), bytesAvailable() ); - file.writeBlock( s.data(), s.size() ); - } - else if ( RetrieveGzipFile == mode ) { - if ( !gzipProc->isRunning() ) - gzipProc->start(Opie::Core::OProcess::NotifyOnExit, (Opie::Core::OProcess::Communication) ( Opie::Core::OProcess::Stdin | Opie::Core::OProcess::Stdout )); - - QByteArray s; - s.resize( bytesAvailable() ); - readBlock( s.data(), bytesAvailable() ); - gzipProc->writeStdin( s.data(), s.size() ); - qDebug("wrote %d bytes to ungzip ", s.size() ); - } - // retrieve buffer mode - else if ( RetrieveBuffer == mode ) { - QCString s; - s.resize( bytesAvailable() ); - readBlock( s.data(), bytesAvailable() ); - buf.writeBlock( s.data(), s.size() ); - } + // retrieve file mode + if ( RetrieveFile == mode ) { + QCString s; + s.resize( bytesAvailable() ); + readBlock( s.data(), bytesAvailable() ); + file.writeBlock( s.data(), s.size() ); + } + else if ( RetrieveGzipFile == mode ) { + if ( !gzipProc->isRunning() ) + gzipProc->start(Opie::Core::OProcess::NotifyOnExit, (Opie::Core::OProcess::Communication) ( Opie::Core::OProcess::Stdin | Opie::Core::OProcess::Stdout )); + + QByteArray s; + s.resize( bytesAvailable() ); + readBlock( s.data(), bytesAvailable() ); + gzipProc->writeStdin( s.data(), s.size() ); + odebug << "wrote " << s.size() << " bytes to ungzip " << oendl; + } + // retrieve buffer mode + else if ( RetrieveBuffer == mode ) { + QCString s; + s.resize( bytesAvailable() ); + readBlock( s.data(), bytesAvailable() ); + buf.writeBlock( s.data(), s.size() ); + } } @@ -1278,11 +1282,11 @@ void ServerDTP::writeTargzBlock(Opie::Core::OProcess *, char *buffer, int buflen { - writeBlock( buffer, buflen ); - qDebug("writeTargzBlock %d", buflen); - if ( !createTargzProc->isRunning() ) { - qDebug("tar and gzip done"); - emit completed(); - mode = Idle; - disconnect( gzipProc, SIGNAL( receivedStdout(Opie::Core::OProcess*,char*,int) ), - this, SLOT( writeTargzBlock(Opie::Core::OProcess*,char*,int) ) ); - } + writeBlock( buffer, buflen ); + odebug << "writeTargzBlock " << buflen << "" << oendl; + if ( !createTargzProc->isRunning() ) { + odebug << "tar and gzip done" << oendl; + emit completed(); + mode = Idle; + disconnect( gzipProc, SIGNAL( receivedStdout(Opie::Core::OProcess*,char*,int) ), + this, SLOT( writeTargzBlock(Opie::Core::OProcess*,char*,int) ) ); + } } @@ -1291,6 +1295,6 @@ void ServerDTP::targzDone() { - //qDebug("targz done"); - disconnect( createTargzProc, SIGNAL( receivedStdout(Opie::Core::OProcess*,char*,int) ), - this, SLOT( gzipTarBlock(Opie::Core::OProcess*,char*,int) ) ); - gzipProc->closeStdin(); + //odebug << "targz done" << oendl; + disconnect( createTargzProc, SIGNAL( receivedStdout(Opie::Core::OProcess*,char*,int) ), + this, SLOT( gzipTarBlock(Opie::Core::OProcess*,char*,int) ) ); + gzipProc->closeStdin(); } @@ -1299,8 +1303,8 @@ void ServerDTP::gzipTarBlock(Opie::Core::OProcess *, char *buffer, int buflen) { - //qDebug("gzipTarBlock"); - if ( !gzipProc->isRunning() ) { - //qDebug("auto start gzip proc"); - gzipProc->start(Opie::Core::OProcess::NotifyOnExit, (Opie::Core::OProcess::Communication) ( Opie::Core::OProcess::Stdin | Opie::Core::OProcess::Stdout )); - } - gzipProc->writeStdin( buffer, buflen ); + //odebug << "gzipTarBlock" << oendl; + if ( !gzipProc->isRunning() ) { + //odebug << "auto start gzip proc" << oendl; + gzipProc->start(Opie::Core::OProcess::NotifyOnExit, (Opie::Core::OProcess::Communication) ( Opie::Core::OProcess::Stdin | Opie::Core::OProcess::Stdout )); + } + gzipProc->writeStdin( buffer, buflen ); } @@ -1309,5 +1313,5 @@ void ServerDTP::sendFile( const QString fn, const QHostAddress& host, Q_UINT16 p { - file.setName( fn ); - mode = SendFile; - connectToHost( host.toString(), port ); + file.setName( fn ); + mode = SendFile; + connectToHost( host.toString(), port ); } @@ -1316,4 +1320,4 @@ void ServerDTP::sendFile( const QString fn ) { - file.setName( fn ); - mode = SendFile; + file.setName( fn ); + mode = SendFile; } @@ -1324,4 +1328,4 @@ void ServerDTP::sendGzipFile( const QString &fn, { - sendGzipFile( fn, archiveTargets ); - connectToHost( host.toString(), port ); + sendGzipFile( fn, archiveTargets ); + connectToHost( host.toString(), port ); } @@ -1331,18 +1335,18 @@ void ServerDTP::sendGzipFile( const QString &fn, { - mode = SendGzipFile; - file.setName( fn ); - - QStringList args = "tar"; - args += "-cv"; - args += archiveTargets; - qDebug("sendGzipFile %s", args.join(" ").latin1() ); - createTargzProc->clearArguments( ); - *createTargzProc << args; - connect( createTargzProc, - SIGNAL( receivedStdout(Opie::Core::OProcess*,char*,int) ), SLOT( gzipTarBlock(Opie::Core::OProcess*,char*,int) ) ); - - gzipProc->clearArguments( ); - *gzipProc << "gzip"; - connect( gzipProc, SIGNAL( receivedStdout(Opie::Core::OProcess*,char*,int) ), - SLOT( writeTargzBlock(Opie::Core::OProcess*,char*,int) ) ); + mode = SendGzipFile; + file.setName( fn ); + + QStringList args = "tar"; + args += "-cv"; + args += archiveTargets; + odebug << "sendGzipFile " << args.join(" ") << "" << oendl; + createTargzProc->clearArguments( ); + *createTargzProc << args; + connect( createTargzProc, + SIGNAL( receivedStdout(Opie::Core::OProcess*,char*,int) ), SLOT( gzipTarBlock(Opie::Core::OProcess*,char*,int) ) ); + + gzipProc->clearArguments( ); + *gzipProc << "gzip"; + connect( gzipProc, SIGNAL( receivedStdout(Opie::Core::OProcess*,char*,int) ), + SLOT( writeTargzBlock(Opie::Core::OProcess*,char*,int) ) ); } @@ -1351,8 +1355,8 @@ void ServerDTP::gunzipDone() { - qDebug("gunzipDone"); - disconnect( gzipProc, SIGNAL( processExited() ), - this, SLOT( gunzipDone() ) ); - retrieveTargzProc->closeStdin(); - disconnect( gzipProc, SIGNAL( receivedStdout(Opie::Core::OProcess*,char*,int) ), - this, SLOT( tarExtractBlock(Opie::Core::OProcess*,char*,int) ) ); + odebug << "gunzipDone" << oendl; + disconnect( gzipProc, SIGNAL( processExited() ), + this, SLOT( gunzipDone() ) ); + retrieveTargzProc->closeStdin(); + disconnect( gzipProc, SIGNAL( receivedStdout(Opie::Core::OProcess*,char*,int) ), + this, SLOT( tarExtractBlock(Opie::Core::OProcess*,char*,int) ) ); } @@ -1361,9 +1365,9 @@ void ServerDTP::tarExtractBlock(Opie::Core::OProcess *, char *buffer, int buflen { - qDebug("tarExtractBlock"); - if ( !retrieveTargzProc->isRunning() ) { - qDebug("auto start ungzip proc"); - if ( !retrieveTargzProc->start(Opie::Core::OProcess::NotifyOnExit, Opie::Core::OProcess::Stdin) ) - qWarning(" failed to start tar -x process"); - } - retrieveTargzProc->writeStdin( buffer, buflen ); + odebug << "tarExtractBlock" << oendl; + if ( !retrieveTargzProc->isRunning() ) { + odebug << "auto start ungzip proc" << oendl; + if ( !retrieveTargzProc->start(Opie::Core::OProcess::NotifyOnExit, Opie::Core::OProcess::Stdin) ) + owarn << " failed to start tar -x process" << oendl; + } + retrieveTargzProc->writeStdin( buffer, buflen ); } @@ -1373,5 +1377,5 @@ void ServerDTP::retrieveFile( const QString fn, const QHostAddress& host, Q_UINT { - file.setName( fn ); - mode = RetrieveFile; - connectToHost( host.toString(), port ); + file.setName( fn ); + mode = RetrieveFile; + connectToHost( host.toString(), port ); } @@ -1380,4 +1384,4 @@ void ServerDTP::retrieveFile( const QString fn ) { - file.setName( fn ); - mode = RetrieveFile; + file.setName( fn ); + mode = RetrieveFile; } @@ -1386,12 +1390,12 @@ void ServerDTP::retrieveGzipFile( const QString &fn ) { - qDebug("retrieveGzipFile %s", fn.latin1()); - file.setName( fn ); - mode = RetrieveGzipFile; - - gzipProc->clearArguments(); - *gzipProc << "gunzip"; - connect( gzipProc, SIGNAL( readyReadStdout() ), - SLOT( tarExtractBlock() ) ); - connect( gzipProc, SIGNAL( processExited() ), - SLOT( gunzipDone() ) ); + odebug << "retrieveGzipFile " << fn << "" << oendl; + file.setName( fn ); + mode = RetrieveGzipFile; + + gzipProc->clearArguments(); + *gzipProc << "gunzip"; + connect( gzipProc, SIGNAL( readyReadStdout() ), + SLOT( tarExtractBlock() ) ); + connect( gzipProc, SIGNAL( processExited() ), + SLOT( gunzipDone() ) ); } @@ -1400,4 +1404,4 @@ void ServerDTP::retrieveGzipFile( const QString &fn, const QHostAddress& host, Q { - retrieveGzipFile( fn ); - connectToHost( host.toString(), port ); + retrieveGzipFile( fn ); + connectToHost( host.toString(), port ); } @@ -1406,5 +1410,5 @@ void ServerDTP::sendByteArray( const QByteArray& array, const QHostAddress& host { - buf.setBuffer( array ); - mode = SendBuffer; - connectToHost( host.toString(), port ); + buf.setBuffer( array ); + mode = SendBuffer; + connectToHost( host.toString(), port ); } @@ -1413,4 +1417,4 @@ void ServerDTP::sendByteArray( const QByteArray& array ) { - buf.setBuffer( array ); - mode = SendBuffer; + buf.setBuffer( array ); + mode = SendBuffer; } @@ -1419,5 +1423,5 @@ void ServerDTP::retrieveByteArray( const QHostAddress& host, Q_UINT16 port ) { - buf.setBuffer( QByteArray() ); - mode = RetrieveBuffer; - connectToHost( host.toString(), port ); + buf.setBuffer( QByteArray() ); + mode = RetrieveBuffer; + connectToHost( host.toString(), port ); } @@ -1426,4 +1430,4 @@ void ServerDTP::retrieveByteArray() { - buf.setBuffer( QByteArray() ); - mode = RetrieveBuffer; + buf.setBuffer( QByteArray() ); + mode = RetrieveBuffer; } @@ -1432,4 +1436,4 @@ void ServerDTP::setSocket( int socket ) { - QSocket::setSocket( socket ); - connected(); + QSocket::setSocket( socket ); + connected(); } |