📄 file.cc
字号:
bool skip = false; list<string>::iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( strncmp( sit->c_str(), d.c_str(), sit->size() ) == 0 ) skip = true; if ( skip ) continue; string realpath = "file:"; realpath += fit->m_strAbsSource; copyingFile( realpath.c_str(), d.c_str() ); // debug( "kio_file : Writing to %s", d ); // Is this URL on the overwrite list ? list<string>::iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( strncmp( oit->c_str(), d.c_str(), oit->size() ) == 0 ) overwrite = true; job.put( d.c_str(), fit->m_mode, overwrite_all || overwrite, false, fit->m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); debug("################# COULD NOT PUT %d", currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } string tmp2 = l.back().url(); SkipDlg_Result r; r = open_SkipDlg( tmp2.c_str(), ( files.size() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } string n; RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( files.size() > 1 ) m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); string tmp2 = l.back().url(); RenameDlg_Result r = open_RenameDlg( fit->m_strAbsSource.c_str(), tmp2.c_str(), m, n ); if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { K2URL u( n.c_str() ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ).c_str() ); // Change the destination name of the current file fit->m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; //debug( "kio_file : Opening %s", fit->m_strAbsSource ); FILE *f = fopen( fit->m_strAbsSource.c_str(), "rb" ); if ( f == 0L ) { error( ERR_CANNOT_OPEN_FOR_READING, fit->m_strAbsSource.c_str() ); m_cmd = CMD_NONE; return; } char buffer[ 4096 ]; while( !feof( f ) ) { int n = fread( buffer, 1, 2048, f ); // !!! slow down loop for local testing// for ( int tmpi = 0; tmpi < 800000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { fclose( f ); m_cmd = CMD_NONE; finished(); return; } } job.dataEnd(); fclose( f ); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( processed_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } debug( "kio_file : Copied files %s", dest.c_str() ); if ( _move ) { slotDel( _source ); } finished(); m_cmd = CMD_NONE;} void FileProtocol::slotGet( const char *_url ){ string url = _url; K2URL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, url.c_str() ); m_cmd = CMD_NONE; return; } if ( !usrc.isLocalFile() ) { error( ERR_INTERNAL, "kio_file got non local file in get command" ); m_cmd = CMD_NONE; return; } struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url.c_str() ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( buff.st_mode ) ) { error( ERR_IS_DIRECTORY, url.c_str() ); m_cmd = CMD_NONE; return; } m_cmd = CMD_GET; FILE *f = fopen( usrc.path(), "rb" ); if ( f == 0L ) { error( ERR_CANNOT_OPEN_FOR_READING, url.c_str() ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( buff.st_size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 4096 ]; while( !feof( f ) ) { int n = fread( buffer, 1, 2048, f ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); fclose( f ); processedSize( buff.st_size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FileProtocol::slotGetSize( const char *_url ){ string url = _url; // debug( "kio_file : Getting size" ); m_cmd = CMD_GET_SIZE; K2URL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, url.c_str() ); m_cmd = CMD_NONE; return; } if ( !usrc.isLocalFile() ) { error( ERR_INTERNAL, "kio_file got non local file in get size command" ); m_cmd = CMD_NONE; return; } struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url.c_str() ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( buff.st_mode ) ) // !!! needed ? { error( ERR_IS_DIRECTORY, url.c_str() ); m_cmd = CMD_NONE; return; } totalSize( buff.st_size ); finished(); m_cmd = CMD_NONE;}void FileProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ string url_orig = _url; string url_part = url_orig + ".part"; K2URL udest_orig( url_orig ); K2URL udest_part( url_part ); bool m_bMarkPartial = ProtocolManager::self()->getMarkPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig.c_str() ); finished(); m_cmd = CMD_NONE; return; } if ( !udest_orig.isLocalFile() ) { error( ERR_INTERNAL, "kio_file got non local file as destination in put command" ); finished(); m_cmd = CMD_NONE; return; } m_cmd = CMD_PUT; struct stat buff; if ( stat( udest_orig.path(), &buff ) != -1 ) { // if original file exists but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) rename ( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( buff.st_size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); finished(); m_cmd = CMD_NONE; return; } } else if ( stat( udest_part.path(), &buff ) != -1 ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) rename ( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( buff.st_size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); finished(); m_cmd = CMD_NONE; return; } } K2URL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { debug( "kio_file : Adding .part extension to %s", udest_orig.path() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { debug("Write Access denied for '%s' %d",udest.path(),errno ); error( ERR_WRITE_ACCESS_DENIED, url.c_str() ); finished(); m_cmd = CMD_NONE; return; } */ if ( _resume ) m_fPut = fopen( udest.path(), "ab" ); // append if resuming else m_fPut = fopen( udest.path(), "wb" ); if ( m_fPut == 0L ) { debug( "kio_file : ####################### COULD NOT WRITE %s", udest.path() ); if ( errno == EACCES ) error( ERR_WRITE_ACCESS_DENIED, udest.path() ); else error( ERR_CANNOT_OPEN_FOR_WRITING, udest.path() ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); fclose( m_fPut ); if ( stat( udest.path(), &buff ) != -1 ) { if ( buff.st_size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { if ( rename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { if ( chmod( udest_orig.path(), _mode ) == -1 ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( buff.st_size < ProtocolManager::self()->getMinimumKeepSize() ) { remove( udest.path() ); } } // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FileProtocol::slotDel( const char *_url ){ list<string> lst; lst.push_back( _url ); slotDel( lst );}void FileProtocol::slotDel( list<string>& _source ){ // Check wether the URLs are wellformed list<string>::iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { debug( "kio_file : Checking %s", soit->c_str() ); K2URL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, soit->c_str() ); m_cmd = CMD_NONE; return; } if ( !usrc.isLocalFile() ) { error( ERR_INTERNAL, "kio_file got non local file in delete command" ); m_cmd = CMD_NONE; return; } } debug( "kio_file : All URLs ok" ); // Get a list of all source files and directories list<Copy> fs; list<CopyDir> ds; int size = 0; debug( "kio_file : Iterating" ); soit = _source.begin(); debug( "kio_file : Looping" ); for( ; soit != _source.end(); ++soit ) { debug( "kio_file : Executing %s", soit->c_str() ); K2URL usrc( *soit ); debug( "kio_file : Parsed URL" ); // Did an error occur ? int s;// if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 )// {// // Error message is already sent// m_cmd = CMD_NONE;// return;// } // Sum up the total amount of bytes we have to copy size += s; } debug( "kio_file : Recursive ok" ); if ( fs.size() == 1 ) m_cmd = CMD_DEL; else m_cmd = CMD_MDEL; // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.size() ); totalDirs( ds.size() ); /***** * Delete files *****/ list<Copy>::iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -