ftp.cc
来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· CC 代码 · 共 2,199 行 · 第 1/5 页
CC
2,199 行
// we should always get the following 5 fields ... const char *p_access, *p_junk, *p_owner, *p_group, *p_size; if( (p_access = strtok((char*)buffer," ")) == 0) continue; if( (p_junk = strtok(NULL," ")) == 0) continue; if( (p_owner = strtok(NULL," ")) == 0) continue; if( (p_group = strtok(NULL," ")) == 0) continue; if( (p_size = strtok(NULL," ")) == 0) continue; //kdDebug(7102) << "p_access=" << p_access << " p_junk=" << p_junk << " p_owner=" << p_owner << " p_group=" << p_group << " p_size=" << p_size << endl; de.access = 0; if ( strlen( p_access ) == 1 && p_junk[0] == '[' ) { // Netware de.access = S_IRWXU | S_IRWXG | S_IRWXO; // unknown -> give all permissions } const char *p_date_1, *p_date_2, *p_date_3, *p_name; // A special hack for "/dev". A listing may look like this: // crw-rw-rw- 1 root root 1, 5 Jun 29 1997 zero // So we just ignore the number in front of the ",". Ok, its a hack :-) if ( strchr( p_size, ',' ) != 0L ) { //kdDebug(7102) << "Size contains a ',' -> reading size again (/dev hack)" << endl; if ((p_size = strtok(NULL," ")) == 0) continue; } // Check whether the size we just read was really the size // or a month (this happens when the server lists no group) // Used to be the case on sunsite.uio.no, but not anymore // This is needed for the Netware case, too. if ( !isdigit( *p_size ) ) { p_date_1 = p_size; p_size = p_group; p_group = 0; //kdDebug(7102) << "Size didn't have a digit -> size=" << p_size << " date_1=" << p_date_1 << endl; } else { p_date_1 = strtok(NULL," "); //kdDebug(7102) << "Size has a digit -> ok. p_date_1=" << p_date_1 << endl; } if ( p_date_1 != 0 && (p_date_2 = strtok(NULL," ")) != 0 && (p_date_3 = strtok(NULL," ")) != 0 && (p_name = strtok(NULL,"\r\n")) != 0 ) { { QCString tmp( p_name ); if ( p_access[0] == 'l' ) { int i = tmp.findRev( " -> " ); if ( i != -1 ) { de.link = remoteEncoding()->decode(p_name + i + 4); tmp.truncate( i ); } else de.link = QString::null; } else de.link = QString::null; if ( tmp[0] == '/' ) // listing on ftp://ftp.gnupg.org/ starts with '/' tmp.remove( 0, 1 ); if (tmp.find('/') != -1) continue; // Don't trick us! // Some sites put more than one space between the date and the name // e.g. ftp://ftp.uni-marburg.de/mirror/ de.name = remoteEncoding()->decode(tmp.stripWhiteSpace()); } de.type = S_IFREG; switch ( p_access[0] ) { case 'd': de.type = S_IFDIR; break; case 's': de.type = S_IFSOCK; break; case 'b': de.type = S_IFBLK; break; case 'c': de.type = S_IFCHR; break; case 'l': de.type = S_IFREG; // we don't set S_IFLNK here. de.link says it. break; default: break; } if ( p_access[1] == 'r' ) de.access |= S_IRUSR; if ( p_access[2] == 'w' ) de.access |= S_IWUSR; if ( p_access[3] == 'x' || p_access[3] == 's' ) de.access |= S_IXUSR; if ( p_access[4] == 'r' ) de.access |= S_IRGRP; if ( p_access[5] == 'w' ) de.access |= S_IWGRP; if ( p_access[6] == 'x' || p_access[6] == 's' ) de.access |= S_IXGRP; if ( p_access[7] == 'r' ) de.access |= S_IROTH; if ( p_access[8] == 'w' ) de.access |= S_IWOTH; if ( p_access[9] == 'x' || p_access[9] == 't' ) de.access |= S_IXOTH; if ( p_access[3] == 's' || p_access[3] == 'S' ) de.access |= S_ISUID; if ( p_access[6] == 's' || p_access[6] == 'S' ) de.access |= S_ISGID; if ( p_access[9] == 't' || p_access[9] == 'T' ) de.access |= S_ISVTX; de.owner = remoteEncoding()->decode(p_owner); de.group = remoteEncoding()->decode(p_group); de.size = charToLongLong(p_size); // Parsing the date is somewhat tricky // Examples : "Oct 6 22:49", "May 13 1999" // First get current time - we need the current month and year time_t currentTime = time( 0L ); struct tm * tmptr = gmtime( ¤tTime ); int currentMonth = tmptr->tm_mon; //kdDebug(7102) << "Current time :" << asctime( tmptr ) << endl; // Reset time fields tmptr->tm_isdst = -1; // We do not know anything about day saving time (of any random day of the year) tmptr->tm_sec = 0; tmptr->tm_min = 0; tmptr->tm_hour = 0; // Get day number (always second field) tmptr->tm_mday = atoi( p_date_2 ); // Get month from first field // NOTE : no, we don't want to use KLocale here // It seems all FTP servers use the English way //kdDebug(7102) << "Looking for month " << p_date_1 << endl; static const char * s_months[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; for ( int c = 0 ; c < 12 ; c ++ ) if ( !strcmp( p_date_1, s_months[c]) ) { //kdDebug(7102) << "Found month " << c << " for " << p_date_1 << endl; tmptr->tm_mon = c; break; } // Parse third field if ( strlen( p_date_3 ) == 4 ) // 4 digits, looks like a year tmptr->tm_year = atoi( p_date_3 ) - 1900; else { // otherwise, the year is implicit // according to man ls, this happens when it is between than 6 months // old and 1 hour in the future. // So the year is : current year if tm_mon <= currentMonth+1 // otherwise current year minus one // (The +1 is a security for the "+1 hour" at the end of the month issue) if ( tmptr->tm_mon > currentMonth + 1 ) tmptr->tm_year--; // and p_date_3 contains probably a time char * semicolon; if ( ( semicolon = (char*)strchr( p_date_3, ':' ) ) ) { *semicolon = '\0'; tmptr->tm_min = atoi( semicolon + 1 ); tmptr->tm_hour = atoi( p_date_3 ); } else kdWarning(7102) << "Can't parse third field " << p_date_3 << endl; } //kdDebug(7102) << asctime( tmptr ) << endl; de.date = mktime( tmptr ); return true; } } // line invalid, loop to get another line return false;}//===============================================================================// public: get download file from server// helper: ftpGet called from get() and copy()//===============================================================================void Ftp::get( const KURL & url ){ kdDebug(7102) << "Ftp::get " << url.url() << endl; int iError = 0; ftpGet(iError, -1, url, 0); // iError gets status if(iError) // can have only server side errs error(iError, url.path()); ftpCloseCommand(); // must close command!}Ftp::StatusCode Ftp::ftpGet(int& iError, int iCopyFile, const KURL& url, KIO::fileoffset_t llOffset){ // Calls error() by itself! if( !ftpOpenConnection(loginImplicit) ) return statusServerError; // Try to find the size of the file (and check that it exists at // the same time). If we get back a 550, "File does not exist" // or "not a plain file", check if it is a directory. If it is a // directory, return an error; otherwise simply try to retrieve // the request... if ( !ftpSize( url.path(), '?' ) && (m_iRespCode == 550) && ftpFolder(url.path(), false) ) { // Ok it's a dir in fact kdDebug(7102) << "ftpGet: it is a directory in fact" << endl; iError = ERR_IS_DIRECTORY; return statusServerError; } QString resumeOffset = metaData("resume"); if ( !resumeOffset.isEmpty() ) { llOffset = resumeOffset.toLongLong(); kdDebug(7102) << "ftpGet: got offset from metadata : " << llOffset << endl; } if( !ftpOpenCommand("retr", url.path(), '?', ERR_CANNOT_OPEN_FOR_READING, llOffset) ) { kdWarning(7102) << "ftpGet: Can't open for reading" << endl; return statusServerError; } // Read the size from the response string if(m_size == UnknownSize) { const char* psz = strrchr( ftpResponse(4), '(' ); if(psz) m_size = charToLongLong(psz+1); } KIO::filesize_t bytesLeft = 0; if ( m_size != UnknownSize ) bytesLeft = m_size - llOffset; kdDebug(7102) << "ftpGet: starting with offset=" << llOffset << endl; KIO::fileoffset_t processed_size = llOffset; QByteArray array; bool mimetypeEmitted = false; char buffer[maximumIpcSize]; // start whith small data chunks in case of a slow data source (modem) // - unfortunately this has a negative impact on performance for large // - files - so we will increase the block size after a while ... int iBlockSize = initialIpcSize; int iBufferCur = 0; while(m_size == UnknownSize || bytesLeft > 0) { // let the buffer size grow if the file is larger 64kByte ... if(processed_size-llOffset > 1024 * 64) iBlockSize = maximumIpcSize; // read the data and detect EOF or error ... if(iBlockSize+iBufferCur > (int)sizeof(buffer)) iBlockSize = sizeof(buffer) - iBufferCur; int n = m_data->read( buffer+iBufferCur, iBlockSize ); if(n <= 0) { // this is how we detect EOF in case of unknown size if( m_size == UnknownSize && n == 0 ) break; // unexpected eof. Happens when the daemon gets killed. iError = ERR_COULD_NOT_READ; return statusServerError; } processed_size += n; // collect very small data chunks in buffer before processing ... if(m_size != UnknownSize) { bytesLeft -= n; iBufferCur += n; if(iBufferCur < mimimumMimeSize && bytesLeft > 0) { processedSize( processed_size ); continue; } n = iBufferCur; iBufferCur = 0; } // get the mime type and set the total size ... if(!mimetypeEmitted) { mimetypeEmitted = true; array.setRawData(buffer, n); KMimeMagicResult * result = KMimeMagic::self()->findBufferFileType(array, url.fileName()); array.resetRawData(buffer, n); kdDebug(7102) << "ftpGet: Emitting mimetype " << result->mimeType() << endl; mimeType( result->mimeType() ); if( m_size != UnknownSize ) // Emit total size AFTER mimetype totalSize( m_size ); } // write output file or pass to data pump ... if(iCopyFile == -1) { array.setRawData(buffer, n); data( array ); array.resetRawData(buffer, n); } else if( (iError = WriteToFile(iCopyFile, buffer, n)) != 0) return statusClientError; // client side error processedSize( processed_size ); } kdDebug(7102) << "ftpGet: done" << endl; if(iCopyFile == -1) // must signal EOF to data pump ... data(array); // array is empty and must be empty! processedSize( m_size == UnknownSize ? processed_size : m_size ); kdDebug(7102) << "ftpGet: emitting finished()" << endl; finished(); return statusSuccess;}/*void Ftp::mimetype( const KURL& url ){ if( !ftpOpenConnection(loginImplicit) ) return; if ( !ftpOpenCommand( "retr", url.path(), 'I', ERR_CANNOT_OPEN_FOR_READING, 0 ) ) { kdWarning(7102) << "Can't open for reading" << endl; return; } char buffer[ 2048 ]; QByteArray array; // Get one chunk of data only and send it, KIO::Job will determine the // mimetype from it using KMimeMagic int n = m_data->read( buffer, 2048 ); array.setRawData(buffer, n); data( array ); array.resetRawData(buffer, n); kdDebug(7102) << "aborting" << endl; ftpAbortTransfer(); kdDebug(7102) << "finished" << endl; finished(); kdDebug(7102) << "after finished" << endl;}void Ftp::ftpAbortTransfer(){ // RFC 959, page 34-35 // IAC (interpret as command) = 255 ; IP (interrupt process) = 254 // DM = 242 (data mark) char msg[4]; // 1. User system inserts the Telnet "Interrupt Process" (IP) signal // in the Telnet stream. msg[0] = (char) 255; //IAC msg[1] = (char) 254; //IP (void) send(sControl, msg, 2, 0); // 2. User system sends the Telnet "Sync" signal. msg[0] = (char) 255; //IAC msg[1] = (char) 242; //DM if (send(sControl, msg, 2, MSG_OOB) != 2) ; // error... // Send ABOR kdDebug(7102) << "send ABOR" << endl; QCString buf = "ABOR\r\n"; if ( KSocks::self()->write( sControl, buf.data(), buf.length() ) <= 0 ) { error( ERR_COULD_NOT_WRITE, QString::null ); return; } // kdDebug(7102) << "read resp" << endl; if ( readresp() != '2' ) { error( ERR_COULD_NOT_READ, QString::null ); return; } kdDebug(7102) << "close sockets" << endl; closeSockets();}*///===============================================================================// public: put upload file to server// helper: ftpPut called from put() and copy()//===============================================================================void Ftp::put(const KURL& url, int permissions, bool overwrite, bool resume){ kdDebug(7102) << "Ftp::put " << url.url() << endl; int iError = 0; // iError gets status ftpPut(iError, -1, url, permissions, overwrite, resume); if(iError) // can have only server side errs error(iError, url.path()); ftpCloseCommand(); // must close command!}Ftp::StatusCode Ftp::ftpPut(int& iError, int iCopyFile, const KURL& dest_url, int permissions, bool overwrite, bool resume){ if( !ftpOpenConnection(loginImplicit) ) return statusServerError; // Don't use mark partial over anonymous FTP. // My incoming dir allows put but not rename... bool bMarkPartial; if (m_user.isEmpty () || m_user == FTP_LOGIN) bMarkPartial = false; else bMarkPartial = config()->readBoolEntry("MarkPartial", true); QString dest_orig = dest_url.path(); QString dest_part( dest_orig ); dest_part += ".part"; if ( ftpSize( dest_orig, 'I' ) ) { if ( m_size == 0 ) { // delete files with zero size QCString cmd = "DELE "; cmd += remoteEncoding()->encode(dest_orig); if( !ftpSendCmd( cmd ) || (m_iRespType != 2) ) { iError = ERR_CANNOT_DELETE_PARTIAL; return statusServerError; } } else if ( !overwrite && !resume ) { iError = ERR_FILE_ALREADY_
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?