KIO
copyjob.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright 2000 Stephan Kulow <coolo@kde.org> 00003 Copyright 2000-2006 David Faure <faure@kde.org> 00004 Copyright 2000 Waldo Bastian <bastian@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "copyjob.h" 00023 #include <errno.h> 00024 #include "kdirlister.h" 00025 #include "kfileitem.h" 00026 #include "deletejob.h" 00027 00028 #include <klocale.h> 00029 #include <kdesktopfile.h> 00030 #include <kdebug.h> 00031 #include <kde_file.h> 00032 00033 #include "slave.h" 00034 #include "scheduler.h" 00035 #include "kdirwatch.h" 00036 #include "kprotocolmanager.h" 00037 00038 #include "jobuidelegate.h" 00039 00040 #include <kdirnotify.h> 00041 #include <ktemporaryfile.h> 00042 00043 #ifdef Q_OS_UNIX 00044 #include <utime.h> 00045 #endif 00046 #include <assert.h> 00047 00048 #include <QtCore/QTimer> 00049 #include <QtCore/QFile> 00050 #include <sys/stat.h> // mode_t 00051 #include <QPointer> 00052 00053 #include "job_p.h" 00054 #include <kdiskfreespaceinfo.h> 00055 #include <kfilesystemtype_p.h> 00056 00057 using namespace KIO; 00058 00059 //this will update the report dialog with 5 Hz, I think this is fast enough, aleXXX 00060 #define REPORT_TIMEOUT 200 00061 00062 enum DestinationState { 00063 DEST_NOT_STATED, 00064 DEST_IS_DIR, 00065 DEST_IS_FILE, 00066 DEST_DOESNT_EXIST 00067 }; 00068 00085 enum CopyJobState { 00086 STATE_STATING, 00087 STATE_RENAMING, 00088 STATE_LISTING, 00089 STATE_CREATING_DIRS, 00090 STATE_CONFLICT_CREATING_DIRS, 00091 STATE_COPYING_FILES, 00092 STATE_CONFLICT_COPYING_FILES, 00093 STATE_DELETING_DIRS, 00094 STATE_SETTING_DIR_ATTRIBUTES 00095 }; 00096 00098 class KIO::CopyJobPrivate: public KIO::JobPrivate 00099 { 00100 public: 00101 CopyJobPrivate(const KUrl::List& src, const KUrl& dest, 00102 CopyJob::CopyMode mode, bool asMethod) 00103 : m_globalDest(dest) 00104 , m_globalDestinationState(DEST_NOT_STATED) 00105 , m_defaultPermissions(false) 00106 , m_bURLDirty(false) 00107 , m_mode(mode) 00108 , m_asMethod(asMethod) 00109 , destinationState(DEST_NOT_STATED) 00110 , state(STATE_STATING) 00111 , m_freeSpace(-1) 00112 , m_totalSize(0) 00113 , m_processedSize(0) 00114 , m_fileProcessedSize(0) 00115 , m_processedFiles(0) 00116 , m_processedDirs(0) 00117 , m_srcList(src) 00118 , m_currentStatSrc(m_srcList.constBegin()) 00119 , m_bCurrentOperationIsLink(false) 00120 , m_bSingleFileCopy(false) 00121 , m_bOnlyRenames(mode==CopyJob::Move) 00122 , m_dest(dest) 00123 , m_bAutoRenameFiles(false) 00124 , m_bAutoRenameDirs(false) 00125 , m_bAutoSkipFiles( false ) 00126 , m_bAutoSkipDirs( false ) 00127 , m_bOverwriteAllFiles( false ) 00128 , m_bOverwriteAllDirs( false ) 00129 , m_conflictError(0) 00130 , m_reportTimer(0) 00131 { 00132 } 00133 00134 // This is the dest URL that was initially given to CopyJob 00135 // It is copied into m_dest, which can be changed for a given src URL 00136 // (when using the RENAME dialog in slotResult), 00137 // and which will be reset for the next src URL. 00138 KUrl m_globalDest; 00139 // The state info about that global dest 00140 DestinationState m_globalDestinationState; 00141 // See setDefaultPermissions 00142 bool m_defaultPermissions; 00143 // Whether URLs changed (and need to be emitted by the next slotReport call) 00144 bool m_bURLDirty; 00145 // Used after copying all the files into the dirs, to set mtime (TODO: and permissions?) 00146 // after the copy is done 00147 QLinkedList<CopyInfo> m_directoriesCopied; 00148 QLinkedList<CopyInfo>::const_iterator m_directoriesCopiedIterator; 00149 00150 CopyJob::CopyMode m_mode; 00151 bool m_asMethod; 00152 DestinationState destinationState; 00153 CopyJobState state; 00154 00155 KIO::filesize_t m_freeSpace; 00156 00157 KIO::filesize_t m_totalSize; 00158 KIO::filesize_t m_processedSize; 00159 KIO::filesize_t m_fileProcessedSize; 00160 int m_processedFiles; 00161 int m_processedDirs; 00162 QList<CopyInfo> files; 00163 QList<CopyInfo> dirs; 00164 KUrl::List dirsToRemove; 00165 KUrl::List m_srcList; 00166 KUrl::List m_successSrcList; // Entries in m_srcList that have successfully been moved 00167 KUrl::List::const_iterator m_currentStatSrc; 00168 bool m_bCurrentSrcIsDir; 00169 bool m_bCurrentOperationIsLink; 00170 bool m_bSingleFileCopy; 00171 bool m_bOnlyRenames; 00172 KUrl m_dest; 00173 KUrl m_currentDest; // set during listing, used by slotEntries 00174 // 00175 QStringList m_skipList; 00176 QSet<QString> m_overwriteList; 00177 bool m_bAutoRenameFiles; 00178 bool m_bAutoRenameDirs; 00179 bool m_bAutoSkipFiles; 00180 bool m_bAutoSkipDirs; 00181 bool m_bOverwriteAllFiles; 00182 bool m_bOverwriteAllDirs; 00183 int m_conflictError; 00184 00185 QTimer *m_reportTimer; 00186 00187 // The current src url being stat'ed or copied 00188 // During the stat phase, this is initially equal to *m_currentStatSrc but it can be resolved to a local file equivalent (#188903). 00189 KUrl m_currentSrcURL; 00190 KUrl m_currentDestURL; 00191 00192 QSet<QString> m_parentDirs; 00193 00194 void statCurrentSrc(); 00195 void statNextSrc(); 00196 00197 // Those aren't slots but submethods for slotResult. 00198 void slotResultStating( KJob * job ); 00199 void startListing( const KUrl & src ); 00200 void slotResultCreatingDirs( KJob * job ); 00201 void slotResultConflictCreatingDirs( KJob * job ); 00202 void createNextDir(); 00203 void slotResultCopyingFiles( KJob * job ); 00204 void slotResultConflictCopyingFiles( KJob * job ); 00205 // KIO::Job* linkNextFile( const KUrl& uSource, const KUrl& uDest, bool overwrite ); 00206 KIO::Job* linkNextFile( const KUrl& uSource, const KUrl& uDest, JobFlags flags ); 00207 void copyNextFile(); 00208 void slotResultDeletingDirs( KJob * job ); 00209 void deleteNextDir(); 00210 void sourceStated(const UDSEntry& entry, const KUrl& sourceUrl); 00211 void skip(const KUrl & sourceURL, bool isDir); 00212 void slotResultRenaming( KJob * job ); 00213 void slotResultSettingDirAttributes( KJob * job ); 00214 void setNextDirAttribute(); 00215 00216 void startRenameJob(const KUrl &slave_url); 00217 bool shouldOverwriteDir( const QString& path ) const; 00218 bool shouldOverwriteFile( const QString& path ) const; 00219 bool shouldSkip( const QString& path ) const; 00220 void skipSrc(bool isDir); 00221 00222 void slotStart(); 00223 void slotEntries( KIO::Job*, const KIO::UDSEntryList& list ); 00224 void slotSubError(KIO::ListJob* job, KIO::ListJob *subJob); 00225 void addCopyInfoFromUDSEntry(const UDSEntry& entry, const KUrl& srcUrl, bool srcIsDir, const KUrl& currentDest); 00229 void slotProcessedSize( KJob*, qulonglong data_size ); 00234 void slotTotalSize( KJob*, qulonglong size ); 00235 00236 void slotReport(); 00237 00238 Q_DECLARE_PUBLIC(CopyJob) 00239 00240 static inline CopyJob *newJob(const KUrl::List& src, const KUrl& dest, 00241 CopyJob::CopyMode mode, bool asMethod, JobFlags flags) 00242 { 00243 CopyJob *job = new CopyJob(*new CopyJobPrivate(src,dest,mode,asMethod)); 00244 job->setUiDelegate(new JobUiDelegate); 00245 if (!(flags & HideProgressInfo)) 00246 KIO::getJobTracker()->registerJob(job); 00247 if (flags & KIO::Overwrite) { 00248 job->d_func()->m_bOverwriteAllDirs = true; 00249 job->d_func()->m_bOverwriteAllFiles = true; 00250 } 00251 return job; 00252 } 00253 }; 00254 00255 CopyJob::CopyJob(CopyJobPrivate &dd) 00256 : Job(dd) 00257 { 00258 setProperty("destUrl", d_func()->m_dest.url()); 00259 QTimer::singleShot(0, this, SLOT(slotStart())); 00260 } 00261 00262 CopyJob::~CopyJob() 00263 { 00264 } 00265 00266 KUrl::List CopyJob::srcUrls() const 00267 { 00268 return d_func()->m_srcList; 00269 } 00270 00271 KUrl CopyJob::destUrl() const 00272 { 00273 return d_func()->m_dest; 00274 } 00275 00276 void CopyJobPrivate::slotStart() 00277 { 00278 Q_Q(CopyJob); 00284 m_reportTimer = new QTimer(q); 00285 00286 q->connect(m_reportTimer,SIGNAL(timeout()),q,SLOT(slotReport())); 00287 m_reportTimer->start(REPORT_TIMEOUT); 00288 00289 // Stat the dest 00290 KIO::Job * job = KIO::stat( m_dest, StatJob::DestinationSide, 2, KIO::HideProgressInfo ); 00291 //kDebug(7007) << "CopyJob:stating the dest " << m_dest; 00292 q->addSubjob(job); 00293 } 00294 00295 // For unit test purposes 00296 KIO_EXPORT bool kio_resolve_local_urls = true; 00297 00298 void CopyJobPrivate::slotResultStating( KJob *job ) 00299 { 00300 Q_Q(CopyJob); 00301 //kDebug(7007); 00302 // Was there an error while stating the src ? 00303 if (job->error() && destinationState != DEST_NOT_STATED ) 00304 { 00305 const KUrl srcurl = static_cast<SimpleJob*>(job)->url(); 00306 if ( !srcurl.isLocalFile() ) 00307 { 00308 // Probably : src doesn't exist. Well, over some protocols (e.g. FTP) 00309 // this info isn't really reliable (thanks to MS FTP servers). 00310 // We'll assume a file, and try to download anyway. 00311 kDebug(7007) << "Error while stating source. Activating hack"; 00312 q->removeSubjob( job ); 00313 assert ( !q->hasSubjobs() ); // We should have only one job at a time ... 00314 struct CopyInfo info; 00315 info.permissions = (mode_t) -1; 00316 info.mtime = (time_t) -1; 00317 info.ctime = (time_t) -1; 00318 info.size = (KIO::filesize_t)-1; 00319 info.uSource = srcurl; 00320 info.uDest = m_dest; 00321 // Append filename or dirname to destination URL, if allowed 00322 if ( destinationState == DEST_IS_DIR && !m_asMethod ) 00323 info.uDest.addPath( srcurl.fileName() ); 00324 00325 files.append( info ); 00326 statNextSrc(); 00327 return; 00328 } 00329 // Local file. If stat fails, the file definitely doesn't exist. 00330 // yes, q->Job::, because we don't want to call our override 00331 q->Job::slotResult( job ); // will set the error and emit result(this) 00332 return; 00333 } 00334 00335 // Keep copy of the stat result 00336 const UDSEntry entry = static_cast<StatJob*>(job)->statResult(); 00337 00338 if ( destinationState == DEST_NOT_STATED ) { 00339 if ( m_dest.isLocalFile() ) { //works for dirs as well 00340 QString path = m_dest.toLocalFile(); 00341 if (m_asMethod) { 00342 // In copy-as mode, we want to check the directory to which we're 00343 // copying. The target file or directory does not exist yet, which 00344 // might confuse KDiskFreeSpaceInfo. 00345 path = QFileInfo(path).absolutePath(); 00346 } 00347 KFileSystemType::Type fsType = KFileSystemType::fileSystemType( path ); 00348 if ( fsType != KFileSystemType::Nfs && fsType != KFileSystemType::Smb ) { 00349 m_freeSpace = KDiskFreeSpaceInfo::freeSpaceInfo( path ).available(); 00350 } 00351 //TODO actually preliminary check is even more valuable for slow NFS/SMB mounts, 00352 //but we need to find a way to report connection errors to user 00353 } 00354 00355 const bool isGlobalDest = m_dest == m_globalDest; 00356 const bool isDir = entry.isDir(); 00357 // we were stating the dest 00358 if (job->error()) { 00359 destinationState = DEST_DOESNT_EXIST; 00360 //kDebug(7007) << "dest does not exist"; 00361 } else { 00362 // Treat symlinks to dirs as dirs here, so no test on isLink 00363 destinationState = isDir ? DEST_IS_DIR : DEST_IS_FILE; 00364 //kDebug(7007) << "dest is dir:" << isDir; 00365 00366 const QString sLocalPath = entry.stringValue( KIO::UDSEntry::UDS_LOCAL_PATH ); 00367 if ( !sLocalPath.isEmpty() && kio_resolve_local_urls && destinationState != DEST_DOESNT_EXIST ) { 00368 m_dest = KUrl(); 00369 m_dest.setPath(sLocalPath); 00370 if ( isGlobalDest ) 00371 m_globalDest = m_dest; 00372 } 00373 } 00374 if ( isGlobalDest ) 00375 m_globalDestinationState = destinationState; 00376 00377 q->removeSubjob( job ); 00378 assert ( !q->hasSubjobs() ); 00379 00380 // After knowing what the dest is, we can start stat'ing the first src. 00381 statCurrentSrc(); 00382 } else { 00383 sourceStated(entry, static_cast<SimpleJob*>(job)->url()); 00384 q->removeSubjob( job ); 00385 } 00386 } 00387 00388 void CopyJobPrivate::sourceStated(const UDSEntry& entry, const KUrl& sourceUrl) 00389 { 00390 const QString sLocalPath = entry.stringValue( KIO::UDSEntry::UDS_LOCAL_PATH ); 00391 const bool isDir = entry.isDir(); 00392 00393 // We were stating the current source URL 00394 // Is it a file or a dir ? 00395 00396 // There 6 cases, and all end up calling addCopyInfoFromUDSEntry first : 00397 // 1 - src is a dir, destination is a directory, 00398 // slotEntries will append the source-dir-name to the destination 00399 // 2 - src is a dir, destination is a file -- will offer to overwrite, later on. 00400 // 3 - src is a dir, destination doesn't exist, then it's the destination dirname, 00401 // so slotEntries will use it as destination. 00402 00403 // 4 - src is a file, destination is a directory, 00404 // slotEntries will append the filename to the destination. 00405 // 5 - src is a file, destination is a file, m_dest is the exact destination name 00406 // 6 - src is a file, destination doesn't exist, m_dest is the exact destination name 00407 00408 KUrl srcurl; 00409 if (!sLocalPath.isEmpty() && destinationState != DEST_DOESNT_EXIST) { 00410 kDebug() << "Using sLocalPath. destinationState=" << destinationState; 00411 // Prefer the local path -- but only if we were able to stat() the dest. 00412 // Otherwise, renaming a desktop:/ url would copy from src=file to dest=desktop (#218719) 00413 srcurl.setPath(sLocalPath); 00414 } else { 00415 srcurl = sourceUrl; 00416 } 00417 addCopyInfoFromUDSEntry(entry, srcurl, false, m_dest); 00418 00419 m_currentDest = m_dest; 00420 m_bCurrentSrcIsDir = false; 00421 00422 if ( isDir 00423 // treat symlinks as files (no recursion) 00424 && !entry.isLink() 00425 && m_mode != CopyJob::Link ) // No recursion in Link mode either. 00426 { 00427 //kDebug(7007) << "Source is a directory"; 00428 00429 if (srcurl.isLocalFile()) { 00430 const QString parentDir = srcurl.toLocalFile(KUrl::RemoveTrailingSlash); 00431 m_parentDirs.insert(parentDir); 00432 } 00433 00434 m_bCurrentSrcIsDir = true; // used by slotEntries 00435 if ( destinationState == DEST_IS_DIR ) // (case 1) 00436 { 00437 if ( !m_asMethod ) 00438 { 00439 // Use <desturl>/<directory_copied> as destination, from now on 00440 QString directory = srcurl.fileName(); 00441 const QString sName = entry.stringValue( KIO::UDSEntry::UDS_NAME ); 00442 KProtocolInfo::FileNameUsedForCopying fnu = KProtocolManager::fileNameUsedForCopying(srcurl); 00443 if (fnu == KProtocolInfo::Name) { 00444 if (!sName.isEmpty()) 00445 directory = sName; 00446 } else if (fnu == KProtocolInfo::DisplayName) { 00447 const QString dispName = entry.stringValue( KIO::UDSEntry::UDS_DISPLAY_NAME ); 00448 if (!dispName.isEmpty()) 00449 directory = dispName; 00450 else if (!sName.isEmpty()) 00451 directory = sName; 00452 } 00453 m_currentDest.addPath( directory ); 00454 } 00455 } 00456 else // (case 3) 00457 { 00458 // otherwise dest is new name for toplevel dir 00459 // so the destination exists, in fact, from now on. 00460 // (This even works with other src urls in the list, since the 00461 // dir has effectively been created) 00462 destinationState = DEST_IS_DIR; 00463 if ( m_dest == m_globalDest ) 00464 m_globalDestinationState = destinationState; 00465 } 00466 00467 startListing( srcurl ); 00468 } 00469 else 00470 { 00471 //kDebug(7007) << "Source is a file (or a symlink), or we are linking -> no recursive listing"; 00472 00473 if (srcurl.isLocalFile()) { 00474 const QString parentDir = srcurl.directory(KUrl::ObeyTrailingSlash); 00475 m_parentDirs.insert(parentDir); 00476 } 00477 00478 statNextSrc(); 00479 } 00480 } 00481 00482 bool CopyJob::doSuspend() 00483 { 00484 Q_D(CopyJob); 00485 d->slotReport(); 00486 return Job::doSuspend(); 00487 } 00488 00489 void CopyJobPrivate::slotReport() 00490 { 00491 Q_Q(CopyJob); 00492 if ( q->isSuspended() ) 00493 return; 00494 // If showProgressInfo was set, progressId() is > 0. 00495 switch (state) { 00496 case STATE_RENAMING: 00497 q->setTotalAmount(KJob::Files, m_srcList.count()); 00498 // fall-through intended 00499 case STATE_COPYING_FILES: 00500 q->setProcessedAmount( KJob::Files, m_processedFiles ); 00501 if (m_bURLDirty) 00502 { 00503 // Only emit urls when they changed. This saves time, and fixes #66281 00504 m_bURLDirty = false; 00505 if (m_mode==CopyJob::Move) 00506 { 00507 emitMoving(q, m_currentSrcURL, m_currentDestURL); 00508 emit q->moving( q, m_currentSrcURL, m_currentDestURL); 00509 } 00510 else if (m_mode==CopyJob::Link) 00511 { 00512 emitCopying( q, m_currentSrcURL, m_currentDestURL ); // we don't have a delegate->linking 00513 emit q->linking( q, m_currentSrcURL.path(), m_currentDestURL ); 00514 } 00515 else 00516 { 00517 emitCopying( q, m_currentSrcURL, m_currentDestURL ); 00518 emit q->copying( q, m_currentSrcURL, m_currentDestURL ); 00519 } 00520 } 00521 break; 00522 00523 case STATE_CREATING_DIRS: 00524 q->setProcessedAmount( KJob::Directories, m_processedDirs ); 00525 if (m_bURLDirty) 00526 { 00527 m_bURLDirty = false; 00528 emit q->creatingDir( q, m_currentDestURL ); 00529 emitCreatingDir( q, m_currentDestURL ); 00530 } 00531 break; 00532 00533 case STATE_STATING: 00534 case STATE_LISTING: 00535 if (m_bURLDirty) 00536 { 00537 m_bURLDirty = false; 00538 if (m_mode==CopyJob::Move) 00539 { 00540 emitMoving( q, m_currentSrcURL, m_currentDestURL ); 00541 } 00542 else 00543 { 00544 emitCopying( q, m_currentSrcURL, m_currentDestURL ); 00545 } 00546 } 00547 q->setTotalAmount(KJob::Bytes, m_totalSize); 00548 q->setTotalAmount(KJob::Files, files.count()); 00549 q->setTotalAmount(KJob::Directories, dirs.count()); 00550 break; 00551 00552 default: 00553 break; 00554 } 00555 } 00556 00557 void CopyJobPrivate::slotEntries(KIO::Job* job, const UDSEntryList& list) 00558 { 00559 //Q_Q(CopyJob); 00560 UDSEntryList::ConstIterator it = list.constBegin(); 00561 UDSEntryList::ConstIterator end = list.constEnd(); 00562 for (; it != end; ++it) { 00563 const UDSEntry& entry = *it; 00564 addCopyInfoFromUDSEntry(entry, static_cast<SimpleJob *>(job)->url(), m_bCurrentSrcIsDir, m_currentDest); 00565 } 00566 } 00567 00568 void CopyJobPrivate::slotSubError(ListJob* job, ListJob* subJob) 00569 { 00570 const KUrl url = subJob->url(); 00571 kWarning() << url << subJob->errorString(); 00572 00573 Q_Q(CopyJob); 00574 00575 emit q->warning(job, subJob->errorString(), QString()); 00576 skip(url, true); 00577 } 00578 00579 00580 void CopyJobPrivate::addCopyInfoFromUDSEntry(const UDSEntry& entry, const KUrl& srcUrl, bool srcIsDir, const KUrl& currentDest) 00581 { 00582 struct CopyInfo info; 00583 info.permissions = entry.numberValue(KIO::UDSEntry::UDS_ACCESS, -1); 00584 info.mtime = (time_t) entry.numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME, -1); 00585 info.ctime = (time_t) entry.numberValue(KIO::UDSEntry::UDS_CREATION_TIME, -1); 00586 info.size = (KIO::filesize_t) entry.numberValue(KIO::UDSEntry::UDS_SIZE, -1); 00587 if (info.size != (KIO::filesize_t) -1) 00588 m_totalSize += info.size; 00589 00590 // recursive listing, displayName can be a/b/c/d 00591 const QString fileName = entry.stringValue(KIO::UDSEntry::UDS_NAME); 00592 const QString urlStr = entry.stringValue(KIO::UDSEntry::UDS_URL); 00593 KUrl url; 00594 if (!urlStr.isEmpty()) 00595 url = urlStr; 00596 QString localPath = entry.stringValue(KIO::UDSEntry::UDS_LOCAL_PATH); 00597 const bool isDir = entry.isDir(); 00598 info.linkDest = entry.stringValue(KIO::UDSEntry::UDS_LINK_DEST); 00599 00600 if (fileName != QLatin1String("..") && fileName != QLatin1String(".")) { 00601 const bool hasCustomURL = !url.isEmpty() || !localPath.isEmpty(); 00602 if (!hasCustomURL) { 00603 // Make URL from displayName 00604 url = srcUrl; 00605 if (srcIsDir) { // Only if src is a directory. Otherwise uSource is fine as is 00606 //kDebug(7007) << "adding path" << displayName; 00607 url.addPath(fileName); 00608 } 00609 } 00610 //kDebug(7007) << "displayName=" << displayName << "url=" << url; 00611 if (!localPath.isEmpty() && kio_resolve_local_urls && destinationState != DEST_DOESNT_EXIST) { 00612 url = KUrl(localPath); 00613 } 00614 00615 info.uSource = url; 00616 info.uDest = currentDest; 00617 //kDebug(7007) << "uSource=" << info.uSource << "uDest(1)=" << info.uDest; 00618 // Append filename or dirname to destination URL, if allowed 00619 if (destinationState == DEST_IS_DIR && 00620 // "copy/move as <foo>" means 'foo' is the dest for the base srcurl 00621 // (passed here during stating) but not its children (during listing) 00622 (! (m_asMethod && state == STATE_STATING))) 00623 { 00624 QString destFileName; 00625 KProtocolInfo::FileNameUsedForCopying fnu = KProtocolManager::fileNameUsedForCopying(url); 00626 if (hasCustomURL && 00627 fnu == KProtocolInfo::FromUrl) { 00628 //destFileName = url.fileName(); // Doesn't work for recursive listing 00629 // Count the number of prefixes used by the recursive listjob 00630 int numberOfSlashes = fileName.count('/'); // don't make this a find()! 00631 QString path = url.path(); 00632 int pos = 0; 00633 for (int n = 0; n < numberOfSlashes + 1; ++n) { 00634 pos = path.lastIndexOf('/', pos - 1); 00635 if (pos == -1) { // error 00636 kWarning(7007) << "kioslave bug: not enough slashes in UDS_URL" << path << "- looking for" << numberOfSlashes << "slashes"; 00637 break; 00638 } 00639 } 00640 if (pos >= 0) { 00641 destFileName = path.mid(pos + 1); 00642 } 00643 00644 } else if ( fnu == KProtocolInfo::Name ) { // destination filename taken from UDS_NAME 00645 destFileName = fileName; 00646 } else { // from display name (with fallback to name) 00647 const QString displayName = entry.stringValue(KIO::UDSEntry::UDS_DISPLAY_NAME); 00648 destFileName = displayName.isEmpty() ? fileName : displayName; 00649 } 00650 00651 // Here we _really_ have to add some filename to the dest. 00652 // Otherwise, we end up with e.g. dest=..../Desktop/ itself. 00653 // (This can happen when dropping a link to a webpage with no path) 00654 if (destFileName.isEmpty()) { 00655 destFileName = KIO::encodeFileName(info.uSource.prettyUrl()); 00656 } 00657 00658 //kDebug(7007) << " adding destFileName=" << destFileName; 00659 info.uDest.addPath(destFileName); 00660 } 00661 //kDebug(7007) << " uDest(2)=" << info.uDest; 00662 //kDebug(7007) << " " << info.uSource << "->" << info.uDest; 00663 if (info.linkDest.isEmpty() && isDir && m_mode != CopyJob::Link) { // Dir 00664 dirs.append(info); // Directories 00665 if (m_mode == CopyJob::Move) { 00666 dirsToRemove.append(info.uSource); 00667 } 00668 } else { 00669 files.append(info); // Files and any symlinks 00670 } 00671 } 00672 } 00673 00674 void CopyJobPrivate::skipSrc(bool isDir) 00675 { 00676 m_dest = m_globalDest; 00677 destinationState = m_globalDestinationState; 00678 skip(*m_currentStatSrc, isDir); 00679 ++m_currentStatSrc; 00680 statCurrentSrc(); 00681 } 00682 00683 void CopyJobPrivate::statNextSrc() 00684 { 00685 /* Revert to the global destination, the one that applies to all source urls. 00686 * Imagine you copy the items a b and c into /d, but /d/b exists so the user uses "Rename" to put it in /foo/b instead. 00687 * d->m_dest is /foo/b for b, but we have to revert to /d for item c and following. 00688 */ 00689 m_dest = m_globalDest; 00690 destinationState = m_globalDestinationState; 00691 ++m_currentStatSrc; 00692 statCurrentSrc(); 00693 } 00694 00695 void CopyJobPrivate::statCurrentSrc() 00696 { 00697 Q_Q(CopyJob); 00698 if (m_currentStatSrc != m_srcList.constEnd()) { 00699 m_currentSrcURL = (*m_currentStatSrc); 00700 m_bURLDirty = true; 00701 if (m_mode == CopyJob::Link) { 00702 // Skip the "stating the source" stage, we don't need it for linking 00703 m_currentDest = m_dest; 00704 struct CopyInfo info; 00705 info.permissions = -1; 00706 info.mtime = (time_t) -1; 00707 info.ctime = (time_t) -1; 00708 info.size = (KIO::filesize_t)-1; 00709 info.uSource = m_currentSrcURL; 00710 info.uDest = m_currentDest; 00711 // Append filename or dirname to destination URL, if allowed 00712 if (destinationState == DEST_IS_DIR && !m_asMethod) { 00713 if ( 00714 (m_currentSrcURL.protocol() == info.uDest.protocol()) && 00715 (m_currentSrcURL.host() == info.uDest.host()) && 00716 (m_currentSrcURL.port() == info.uDest.port()) && 00717 (m_currentSrcURL.user() == info.uDest.user()) && 00718 (m_currentSrcURL.pass() == info.uDest.pass()) ) { 00719 // This is the case of creating a real symlink 00720 info.uDest.addPath( m_currentSrcURL.fileName() ); 00721 } else { 00722 // Different protocols, we'll create a .desktop file 00723 // We have to change the extension anyway, so while we're at it, 00724 // name the file like the URL 00725 info.uDest.addPath(KIO::encodeFileName(m_currentSrcURL.prettyUrl()) + ".desktop"); 00726 } 00727 } 00728 files.append( info ); // Files and any symlinks 00729 statNextSrc(); // we could use a loop instead of a recursive call :) 00730 return; 00731 } 00732 00733 // Let's see if we can skip stat'ing, for the case where a directory view has the info already 00734 const KFileItem cachedItem = KDirLister::cachedItemForUrl(m_currentSrcURL); 00735 KIO::UDSEntry entry; 00736 if (!cachedItem.isNull()) { 00737 entry = cachedItem.entry(); 00738 if (destinationState != DEST_DOESNT_EXIST) { // only resolve src if we could resolve dest (#218719) 00739 bool dummyIsLocal; 00740 m_currentSrcURL = cachedItem.mostLocalUrl(dummyIsLocal); // #183585 00741 } 00742 } 00743 00744 if (m_mode == CopyJob::Move && ( 00745 // Don't go renaming right away if we need a stat() to find out the destination filename 00746 KProtocolManager::fileNameUsedForCopying(m_currentSrcURL) == KProtocolInfo::FromUrl || 00747 destinationState != DEST_IS_DIR || m_asMethod) 00748 ) { 00749 // If moving, before going for the full stat+[list+]copy+del thing, try to rename 00750 // The logic is pretty similar to FileCopyJobPrivate::slotStart() 00751 if ( (m_currentSrcURL.protocol() == m_dest.protocol()) && 00752 (m_currentSrcURL.host() == m_dest.host()) && 00753 (m_currentSrcURL.port() == m_dest.port()) && 00754 (m_currentSrcURL.user() == m_dest.user()) && 00755 (m_currentSrcURL.pass() == m_dest.pass()) ) 00756 { 00757 startRenameJob( m_currentSrcURL ); 00758 return; 00759 } 00760 else if ( m_currentSrcURL.isLocalFile() && KProtocolManager::canRenameFromFile( m_dest ) ) 00761 { 00762 startRenameJob( m_dest ); 00763 return; 00764 } 00765 else if ( m_dest.isLocalFile() && KProtocolManager::canRenameToFile( m_currentSrcURL ) ) 00766 { 00767 startRenameJob( m_currentSrcURL ); 00768 return; 00769 } 00770 } 00771 00772 // if the file system doesn't support deleting, we do not even stat 00773 if (m_mode == CopyJob::Move && !KProtocolManager::supportsDeleting(m_currentSrcURL)) { 00774 QPointer<CopyJob> that = q; 00775 emit q->warning( q, buildErrorString(ERR_CANNOT_DELETE, m_currentSrcURL.prettyUrl()) ); 00776 if (that) 00777 statNextSrc(); // we could use a loop instead of a recursive call :) 00778 return; 00779 } 00780 00781 m_bOnlyRenames = false; 00782 00783 // Testing for entry.count()>0 here is not good enough; KFileItem inserts 00784 // entries for UDS_USER and UDS_GROUP even on initially empty UDSEntries (#192185) 00785 if (entry.contains(KIO::UDSEntry::UDS_NAME)) { 00786 kDebug(7007) << "fast path! found info about" << m_currentSrcURL << "in KDirLister"; 00787 sourceStated(entry, m_currentSrcURL); 00788 return; 00789 } 00790 00791 // Stat the next src url 00792 Job * job = KIO::stat( m_currentSrcURL, StatJob::SourceSide, 2, KIO::HideProgressInfo ); 00793 //kDebug(7007) << "KIO::stat on" << m_currentSrcURL; 00794 state = STATE_STATING; 00795 q->addSubjob(job); 00796 m_currentDestURL = m_dest; 00797 m_bURLDirty = true; 00798 } 00799 else 00800 { 00801 // Finished the stat'ing phase 00802 // First make sure that the totals were correctly emitted 00803 state = STATE_STATING; 00804 m_bURLDirty = true; 00805 slotReport(); 00806 00807 kDebug(7007)<<"Stating finished. To copy:"<<m_totalSize<<", available:"<<m_freeSpace; 00808 //TODO warn user beforehand if space is not enough 00809 00810 if (!dirs.isEmpty()) 00811 emit q->aboutToCreate( q, dirs ); 00812 if (!files.isEmpty()) 00813 emit q->aboutToCreate( q, files ); 00814 // Check if we are copying a single file 00815 m_bSingleFileCopy = ( files.count() == 1 && dirs.isEmpty() ); 00816 // Then start copying things 00817 state = STATE_CREATING_DIRS; 00818 createNextDir(); 00819 } 00820 } 00821 00822 void CopyJobPrivate::startRenameJob( const KUrl& slave_url ) 00823 { 00824 Q_Q(CopyJob); 00825 00826 // Silence KDirWatch notifications, otherwise performance is horrible 00827 if (m_currentSrcURL.isLocalFile()) { 00828 const QString parentDir = m_currentSrcURL.directory(KUrl::ObeyTrailingSlash); 00829 if (!m_parentDirs.contains(parentDir)) { 00830 KDirWatch::self()->stopDirScan(parentDir); 00831 m_parentDirs.insert(parentDir); 00832 } 00833 } 00834 00835 KUrl dest = m_dest; 00836 // Append filename or dirname to destination URL, if allowed 00837 if ( destinationState == DEST_IS_DIR && !m_asMethod ) 00838 dest.addPath( m_currentSrcURL.fileName() ); 00839 m_currentDestURL = dest; 00840 kDebug(7007) << m_currentSrcURL << "->" << dest << "trying direct rename first"; 00841 state = STATE_RENAMING; 00842 00843 struct CopyInfo info; 00844 info.permissions = -1; 00845 info.mtime = (time_t) -1; 00846 info.ctime = (time_t) -1; 00847 info.size = (KIO::filesize_t)-1; 00848 info.uSource = m_currentSrcURL; 00849 info.uDest = dest; 00850 QList<CopyInfo> files; 00851 files.append(info); 00852 emit q->aboutToCreate( q, files ); 00853 00854 KIO_ARGS << m_currentSrcURL << dest << (qint8) false /*no overwrite*/; 00855 SimpleJob * newJob = SimpleJobPrivate::newJobNoUi(slave_url, CMD_RENAME, packedArgs); 00856 Scheduler::setJobPriority(newJob, 1); 00857 q->addSubjob( newJob ); 00858 if ( m_currentSrcURL.directory() != dest.directory() ) // For the user, moving isn't renaming. Only renaming is. 00859 m_bOnlyRenames = false; 00860 } 00861 00862 void CopyJobPrivate::startListing( const KUrl & src ) 00863 { 00864 Q_Q(CopyJob); 00865 state = STATE_LISTING; 00866 m_bURLDirty = true; 00867 ListJob * newjob = listRecursive(src, KIO::HideProgressInfo); 00868 newjob->setUnrestricted(true); 00869 q->connect(newjob, SIGNAL(entries(KIO::Job*,KIO::UDSEntryList)), 00870 SLOT(slotEntries(KIO::Job*,KIO::UDSEntryList))); 00871 q->connect(newjob, SIGNAL(subError(KIO::ListJob*,KIO::ListJob*)), 00872 SLOT(slotSubError(KIO::ListJob*,KIO::ListJob*))); 00873 q->addSubjob( newjob ); 00874 } 00875 00876 void CopyJobPrivate::skip(const KUrl & sourceUrl, bool isDir) 00877 { 00878 KUrl dir = sourceUrl; 00879 if (!isDir) { 00880 // Skipping a file: make sure not to delete the parent dir (#208418) 00881 dir.setPath(dir.directory()); 00882 } 00883 while (dirsToRemove.removeAll(dir) > 0) { 00884 // Do not rely on rmdir() on the parent directories aborting. 00885 // Exclude the parent dirs explicitly. 00886 dir.setPath(dir.directory()); 00887 } 00888 } 00889 00890 bool CopyJobPrivate::shouldOverwriteDir( const QString& path ) const 00891 { 00892 if ( m_bOverwriteAllDirs ) 00893 return true; 00894 return m_overwriteList.contains(path); 00895 } 00896 00897 bool CopyJobPrivate::shouldOverwriteFile( const QString& path ) const 00898 { 00899 if ( m_bOverwriteAllFiles ) 00900 return true; 00901 return m_overwriteList.contains(path); 00902 } 00903 00904 bool CopyJobPrivate::shouldSkip( const QString& path ) const 00905 { 00906 Q_FOREACH(const QString& skipPath, m_skipList) { 00907 if ( path.startsWith(skipPath) ) 00908 return true; 00909 } 00910 return false; 00911 } 00912 00913 void CopyJobPrivate::slotResultCreatingDirs( KJob * job ) 00914 { 00915 Q_Q(CopyJob); 00916 // The dir we are trying to create: 00917 QList<CopyInfo>::Iterator it = dirs.begin(); 00918 // Was there an error creating a dir ? 00919 if ( job->error() ) 00920 { 00921 m_conflictError = job->error(); 00922 if ( (m_conflictError == ERR_DIR_ALREADY_EXIST) 00923 || (m_conflictError == ERR_FILE_ALREADY_EXIST) ) // can't happen? 00924 { 00925 KUrl oldURL = ((SimpleJob*)job)->url(); 00926 // Should we skip automatically ? 00927 if ( m_bAutoSkipDirs ) { 00928 // We don't want to copy files in this directory, so we put it on the skip list 00929 m_skipList.append( oldURL.path( KUrl::AddTrailingSlash ) ); 00930 skip(oldURL, true); 00931 dirs.erase( it ); // Move on to next dir 00932 } else { 00933 // Did the user choose to overwrite already? 00934 const QString destDir = (*it).uDest.path(); 00935 if ( shouldOverwriteDir( destDir ) ) { // overwrite => just skip 00936 emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true /* directory */, false /* renamed */ ); 00937 dirs.erase( it ); // Move on to next dir 00938 } else { 00939 if (m_bAutoRenameDirs) { 00940 QString oldPath = (*it).uDest.path(KUrl::AddTrailingSlash); 00941 00942 KUrl destDirectory((*it).uDest); 00943 destDirectory.setPath(destDirectory.directory()); 00944 QString newName = KIO::RenameDialog::suggestName(destDirectory, (*it).uDest.fileName()); 00945 00946 KUrl newUrl((*it).uDest); 00947 newUrl.setFileName(newName); 00948 00949 emit q->renamed(q, (*it).uDest, newUrl); // for e.g. kpropsdlg 00950 00951 // Change the current one and strip the trailing '/' 00952 (*it).uDest.setPath(newUrl.path(KUrl::RemoveTrailingSlash)); 00953 00954 QString newPath = newUrl.path(KUrl::AddTrailingSlash); // With trailing slash 00955 QList<CopyInfo>::Iterator renamedirit = it; 00956 ++renamedirit; 00957 // Change the name of subdirectories inside the directory 00958 for(; renamedirit != dirs.end() ; ++renamedirit) { 00959 QString path = (*renamedirit).uDest.path(); 00960 if (path.startsWith(oldPath)) { 00961 QString n = path; 00962 n.replace(0, oldPath.length(), newPath); 00963 kDebug(7007) << "dirs list:" << (*renamedirit).uSource.path() 00964 << "was going to be" << path 00965 << ", changed into" << n; 00966 (*renamedirit).uDest.setPath(n); 00967 } 00968 } 00969 // Change filenames inside the directory 00970 QList<CopyInfo>::Iterator renamefileit = files.begin(); 00971 for(; renamefileit != files.end() ; ++renamefileit) { 00972 QString path = (*renamefileit).uDest.path(); 00973 if (path.startsWith(oldPath)) { 00974 QString n = path; 00975 n.replace(0, oldPath.length(), newPath); 00976 kDebug(7007) << "files list:" << (*renamefileit).uSource.path() 00977 << "was going to be" << path 00978 << ", changed into" << n; 00979 (*renamefileit).uDest.setPath(n); 00980 } 00981 } 00982 if (!dirs.isEmpty()) { 00983 emit q->aboutToCreate(q, dirs); 00984 } 00985 if (!files.isEmpty()) { 00986 emit q->aboutToCreate(q, files); 00987 } 00988 00989 } 00990 else { 00991 if (!q->isInteractive()) { 00992 q->Job::slotResult(job); // will set the error and emit result(this) 00993 return; 00994 } 00995 00996 assert(((SimpleJob*)job)->url().url() == (*it).uDest.url()); 00997 q->removeSubjob(job); 00998 assert (!q->hasSubjobs()); // We should have only one job at a time ... 00999 01000 // We need to stat the existing dir, to get its last-modification time 01001 KUrl existingDest((*it).uDest); 01002 SimpleJob * newJob = KIO::stat(existingDest, StatJob::DestinationSide, 2, KIO::HideProgressInfo); 01003 Scheduler::setJobPriority(newJob, 1); 01004 kDebug(7007) << "KIO::stat for resolving conflict on " << existingDest; 01005 state = STATE_CONFLICT_CREATING_DIRS; 01006 q->addSubjob(newJob); 01007 return; // Don't move to next dir yet ! 01008 } 01009 } 01010 } 01011 } 01012 else 01013 { 01014 // Severe error, abort 01015 q->Job::slotResult( job ); // will set the error and emit result(this) 01016 return; 01017 } 01018 } 01019 else // no error : remove from list, to move on to next dir 01020 { 01021 //this is required for the undo feature 01022 emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true, false ); 01023 m_directoriesCopied.append( *it ); 01024 dirs.erase( it ); 01025 } 01026 01027 m_processedDirs++; 01028 //emit processedAmount( this, KJob::Directories, m_processedDirs ); 01029 q->removeSubjob( job ); 01030 assert( !q->hasSubjobs() ); // We should have only one job at a time ... 01031 createNextDir(); 01032 } 01033 01034 void CopyJobPrivate::slotResultConflictCreatingDirs( KJob * job ) 01035 { 01036 Q_Q(CopyJob); 01037 // We come here after a conflict has been detected and we've stated the existing dir 01038 01039 // The dir we were trying to create: 01040 QList<CopyInfo>::Iterator it = dirs.begin(); 01041 01042 const UDSEntry entry = ((KIO::StatJob*)job)->statResult(); 01043 01044 // Its modification time: 01045 const time_t destmtime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_MODIFICATION_TIME, -1 ); 01046 const time_t destctime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_CREATION_TIME, -1 ); 01047 01048 const KIO::filesize_t destsize = entry.numberValue( KIO::UDSEntry::UDS_SIZE ); 01049 const QString linkDest = entry.stringValue( KIO::UDSEntry::UDS_LINK_DEST ); 01050 01051 q->removeSubjob( job ); 01052 assert ( !q->hasSubjobs() ); // We should have only one job at a time ... 01053 01054 // Always multi and skip (since there are files after that) 01055 RenameDialog_Mode mode = (RenameDialog_Mode)( M_MULTI | M_SKIP | M_ISDIR ); 01056 // Overwrite only if the existing thing is a dir (no chance with a file) 01057 if ( m_conflictError == ERR_DIR_ALREADY_EXIST ) 01058 { 01059 if( (*it).uSource == (*it).uDest || 01060 ((*it).uSource.protocol() == (*it).uDest.protocol() && 01061 (*it).uSource.path( KUrl::RemoveTrailingSlash ) == linkDest) ) 01062 mode = (RenameDialog_Mode)( mode | M_OVERWRITE_ITSELF); 01063 else 01064 mode = (RenameDialog_Mode)( mode | M_OVERWRITE ); 01065 } 01066 01067 QString existingDest = (*it).uDest.path(); 01068 QString newPath; 01069 if (m_reportTimer) 01070 m_reportTimer->stop(); 01071 RenameDialog_Result r = q->ui()->askFileRename( q, i18n("Folder Already Exists"), 01072 (*it).uSource.url(), 01073 (*it).uDest.url(), 01074 mode, newPath, 01075 (*it).size, destsize, 01076 (*it).ctime, destctime, 01077 (*it).mtime, destmtime ); 01078 if (m_reportTimer) 01079 m_reportTimer->start(REPORT_TIMEOUT); 01080 switch ( r ) { 01081 case R_CANCEL: 01082 q->setError( ERR_USER_CANCELED ); 01083 q->emitResult(); 01084 return; 01085 case R_AUTO_RENAME: 01086 m_bAutoRenameDirs = true; 01087 // fall through 01088 case R_RENAME: 01089 { 01090 QString oldPath = (*it).uDest.path( KUrl::AddTrailingSlash ); 01091 KUrl newUrl( (*it).uDest ); 01092 newUrl.setPath( newPath ); 01093 emit q->renamed( q, (*it).uDest, newUrl ); // for e.g. kpropsdlg 01094 01095 // Change the current one and strip the trailing '/' 01096 (*it).uDest.setPath( newUrl.path( KUrl::RemoveTrailingSlash ) ); 01097 newPath = newUrl.path( KUrl::AddTrailingSlash ); // With trailing slash 01098 QList<CopyInfo>::Iterator renamedirit = it; 01099 ++renamedirit; 01100 // Change the name of subdirectories inside the directory 01101 for( ; renamedirit != dirs.end() ; ++renamedirit ) 01102 { 01103 QString path = (*renamedirit).uDest.path(); 01104 if ( path.startsWith( oldPath ) ) { 01105 QString n = path; 01106 n.replace( 0, oldPath.length(), newPath ); 01107 kDebug(7007) << "dirs list:" << (*renamedirit).uSource.path() 01108 << "was going to be" << path 01109 << ", changed into" << n; 01110 (*renamedirit).uDest.setPath( n ); 01111 } 01112 } 01113 // Change filenames inside the directory 01114 QList<CopyInfo>::Iterator renamefileit = files.begin(); 01115 for( ; renamefileit != files.end() ; ++renamefileit ) 01116 { 01117 QString path = (*renamefileit).uDest.path(); 01118 if ( path.startsWith( oldPath ) ) { 01119 QString n = path; 01120 n.replace( 0, oldPath.length(), newPath ); 01121 kDebug(7007) << "files list:" << (*renamefileit).uSource.path() 01122 << "was going to be" << path 01123 << ", changed into" << n; 01124 (*renamefileit).uDest.setPath( n ); 01125 } 01126 } 01127 if (!dirs.isEmpty()) 01128 emit q->aboutToCreate( q, dirs ); 01129 if (!files.isEmpty()) 01130 emit q->aboutToCreate( q, files ); 01131 } 01132 break; 01133 case R_AUTO_SKIP: 01134 m_bAutoSkipDirs = true; 01135 // fall through 01136 case R_SKIP: 01137 m_skipList.append( existingDest ); 01138 skip((*it).uSource, true); 01139 // Move on to next dir 01140 dirs.erase( it ); 01141 m_processedDirs++; 01142 break; 01143 case R_OVERWRITE: 01144 m_overwriteList.insert( existingDest ); 01145 emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true /* directory */, false /* renamed */ ); 01146 // Move on to next dir 01147 dirs.erase( it ); 01148 m_processedDirs++; 01149 break; 01150 case R_OVERWRITE_ALL: 01151 m_bOverwriteAllDirs = true; 01152 emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true /* directory */, false /* renamed */ ); 01153 // Move on to next dir 01154 dirs.erase( it ); 01155 m_processedDirs++; 01156 break; 01157 default: 01158 assert( 0 ); 01159 } 01160 state = STATE_CREATING_DIRS; 01161 //emit processedAmount( this, KJob::Directories, m_processedDirs ); 01162 createNextDir(); 01163 } 01164 01165 void CopyJobPrivate::createNextDir() 01166 { 01167 Q_Q(CopyJob); 01168 KUrl udir; 01169 if ( !dirs.isEmpty() ) 01170 { 01171 // Take first dir to create out of list 01172 QList<CopyInfo>::Iterator it = dirs.begin(); 01173 // Is this URL on the skip list or the overwrite list ? 01174 while( it != dirs.end() && udir.isEmpty() ) 01175 { 01176 const QString dir = (*it).uDest.path(); 01177 if ( shouldSkip( dir ) ) { 01178 dirs.erase( it ); 01179 it = dirs.begin(); 01180 } else 01181 udir = (*it).uDest; 01182 } 01183 } 01184 if ( !udir.isEmpty() ) // any dir to create, finally ? 01185 { 01186 // Create the directory - with default permissions so that we can put files into it 01187 // TODO : change permissions once all is finished; but for stuff coming from CDROM it sucks... 01188 KIO::SimpleJob *newjob = KIO::mkdir( udir, -1 ); 01189 Scheduler::setJobPriority(newjob, 1); 01190 if (shouldOverwriteFile(udir.path())) { // if we are overwriting an existing file or symlink 01191 newjob->addMetaData("overwrite", "true"); 01192 } 01193 01194 m_currentDestURL = udir; 01195 m_bURLDirty = true; 01196 01197 q->addSubjob(newjob); 01198 return; 01199 } 01200 else // we have finished creating dirs 01201 { 01202 q->setProcessedAmount( KJob::Directories, m_processedDirs ); // make sure final number appears 01203 01204 if (m_mode == CopyJob::Move) { 01205 // Now we know which dirs hold the files we're going to delete. 01206 // To speed things up and prevent double-notification, we disable KDirWatch 01207 // on those dirs temporarily (using KDirWatch::self, that's the instanced 01208 // used by e.g. kdirlister). 01209 for ( QSet<QString>::const_iterator it = m_parentDirs.constBegin() ; it != m_parentDirs.constEnd() ; ++it ) 01210 KDirWatch::self()->stopDirScan( *it ); 01211 } 01212 01213 state = STATE_COPYING_FILES; 01214 m_processedFiles++; // Ralf wants it to start at 1, not 0 01215 copyNextFile(); 01216 } 01217 } 01218 01219 void CopyJobPrivate::slotResultCopyingFiles( KJob * job ) 01220 { 01221 Q_Q(CopyJob); 01222 // The file we were trying to copy: 01223 QList<CopyInfo>::Iterator it = files.begin(); 01224 if ( job->error() ) 01225 { 01226 // Should we skip automatically ? 01227 if ( m_bAutoSkipFiles ) 01228 { 01229 skip((*it).uSource, false); 01230 m_fileProcessedSize = (*it).size; 01231 files.erase( it ); // Move on to next file 01232 } 01233 else 01234 { 01235 m_conflictError = job->error(); // save for later 01236 // Existing dest ? 01237 if ( ( m_conflictError == ERR_FILE_ALREADY_EXIST ) 01238 || ( m_conflictError == ERR_DIR_ALREADY_EXIST ) 01239 || ( m_conflictError == ERR_IDENTICAL_FILES ) ) 01240 { 01241 if (m_bAutoRenameFiles) { 01242 KUrl destDirectory((*it).uDest); 01243 destDirectory.setPath(destDirectory.directory()); 01244 const QString newName = KIO::RenameDialog::suggestName(destDirectory, (*it).uDest.fileName()); 01245 01246 KUrl newUrl((*it).uDest); 01247 newUrl.setFileName(newName); 01248 01249 emit q->renamed(q, (*it).uDest, newUrl); // for e.g. kpropsdlg 01250 (*it).uDest = newUrl; 01251 01252 QList<CopyInfo> files; 01253 files.append(*it); 01254 emit q->aboutToCreate(q, files); 01255 } 01256 else { 01257 if ( !q->isInteractive() ) { 01258 q->Job::slotResult( job ); // will set the error and emit result(this) 01259 return; 01260 } 01261 01262 q->removeSubjob(job); 01263 assert (!q->hasSubjobs()); 01264 // We need to stat the existing file, to get its last-modification time 01265 KUrl existingFile((*it).uDest); 01266 SimpleJob * newJob = KIO::stat(existingFile, StatJob::DestinationSide, 2, KIO::HideProgressInfo); 01267 Scheduler::setJobPriority(newJob, 1); 01268 kDebug(7007) << "KIO::stat for resolving conflict on " << existingFile; 01269 state = STATE_CONFLICT_COPYING_FILES; 01270 q->addSubjob(newJob); 01271 return; // Don't move to next file yet ! 01272 } 01273 } 01274 else 01275 { 01276 if ( m_bCurrentOperationIsLink && qobject_cast<KIO::DeleteJob*>( job ) ) 01277 { 01278 // Very special case, see a few lines below 01279 // We are deleting the source of a symlink we successfully moved... ignore error 01280 m_fileProcessedSize = (*it).size; 01281 files.erase( it ); 01282 } else { 01283 if ( !q->isInteractive() ) { 01284 q->Job::slotResult( job ); // will set the error and emit result(this) 01285 return; 01286 } 01287 01288 // Go directly to the conflict resolution, there is nothing to stat 01289 slotResultConflictCopyingFiles( job ); 01290 return; 01291 } 01292 } 01293 } 01294 } else // no error 01295 { 01296 // Special case for moving links. That operation needs two jobs, unlike others. 01297 if ( m_bCurrentOperationIsLink && m_mode == CopyJob::Move 01298 && !qobject_cast<KIO::DeleteJob *>( job ) // Deleting source not already done 01299 ) 01300 { 01301 q->removeSubjob( job ); 01302 assert ( !q->hasSubjobs() ); 01303 // The only problem with this trick is that the error handling for this del operation 01304 // is not going to be right... see 'Very special case' above. 01305 KIO::Job * newjob = KIO::del( (*it).uSource, HideProgressInfo ); 01306 q->addSubjob( newjob ); 01307 return; // Don't move to next file yet ! 01308 } 01309 01310 if ( m_bCurrentOperationIsLink ) 01311 { 01312 QString target = ( m_mode == CopyJob::Link ? (*it).uSource.path() : (*it).linkDest ); 01313 //required for the undo feature 01314 emit q->copyingLinkDone( q, (*it).uSource, target, (*it).uDest ); 01315 } 01316 else { 01317 //required for the undo feature 01318 emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, false, false ); 01319 if (m_mode == CopyJob::Move) 01320 { 01321 org::kde::KDirNotify::emitFileMoved( (*it).uSource.url(), (*it).uDest.url() ); 01322 } 01323 m_successSrcList.append((*it).uSource); 01324 if (m_freeSpace != (KIO::filesize_t)-1 && (*it).size != (KIO::filesize_t)-1) { 01325 m_freeSpace -= (*it).size; 01326 } 01327 01328 } 01329 // remove from list, to move on to next file 01330 files.erase( it ); 01331 } 01332 m_processedFiles++; 01333 01334 // clear processed size for last file and add it to overall processed size 01335 m_processedSize += m_fileProcessedSize; 01336 m_fileProcessedSize = 0; 01337 01338 //kDebug(7007) << files.count() << "files remaining"; 01339 01340 // Merge metadata from subjob 01341 KIO::Job* kiojob = dynamic_cast<KIO::Job*>(job); 01342 Q_ASSERT(kiojob); 01343 m_incomingMetaData += kiojob->metaData(); 01344 q->removeSubjob( job ); 01345 assert( !q->hasSubjobs() ); // We should have only one job at a time ... 01346 copyNextFile(); 01347 } 01348 01349 void CopyJobPrivate::slotResultConflictCopyingFiles( KJob * job ) 01350 { 01351 Q_Q(CopyJob); 01352 // We come here after a conflict has been detected and we've stated the existing file 01353 // The file we were trying to create: 01354 QList<CopyInfo>::Iterator it = files.begin(); 01355 01356 RenameDialog_Result res; 01357 QString newPath; 01358 01359 if (m_reportTimer) 01360 m_reportTimer->stop(); 01361 01362 if ( ( m_conflictError == ERR_FILE_ALREADY_EXIST ) 01363 || ( m_conflictError == ERR_DIR_ALREADY_EXIST ) 01364 || ( m_conflictError == ERR_IDENTICAL_FILES ) ) 01365 { 01366 // Its modification time: 01367 const UDSEntry entry = ((KIO::StatJob*)job)->statResult(); 01368 01369 const time_t destmtime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_MODIFICATION_TIME, -1 ); 01370 const time_t destctime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_CREATION_TIME, -1 ); 01371 const KIO::filesize_t destsize = entry.numberValue( KIO::UDSEntry::UDS_SIZE ); 01372 const QString linkDest = entry.stringValue( KIO::UDSEntry::UDS_LINK_DEST ); 01373 01374 // Offer overwrite only if the existing thing is a file 01375 // If src==dest, use "overwrite-itself" 01376 RenameDialog_Mode mode; 01377 bool isDir = true; 01378 01379 if( m_conflictError == ERR_DIR_ALREADY_EXIST ) 01380 mode = M_ISDIR; 01381 else 01382 { 01383 if ( (*it).uSource == (*it).uDest || 01384 ((*it).uSource.protocol() == (*it).uDest.protocol() && 01385 (*it).uSource.path( KUrl::RemoveTrailingSlash ) == linkDest) ) 01386 mode = M_OVERWRITE_ITSELF; 01387 else 01388 mode = M_OVERWRITE; 01389 isDir = false; 01390 } 01391 01392 if ( !m_bSingleFileCopy ) 01393 mode = (RenameDialog_Mode) ( mode | M_MULTI | M_SKIP ); 01394 01395 res = q->ui()->askFileRename( q, !isDir ? 01396 i18n("File Already Exists") : i18n("Already Exists as Folder"), 01397 (*it).uSource.url(), 01398 (*it).uDest.url(), 01399 mode, newPath, 01400 (*it).size, destsize, 01401 (*it).ctime, destctime, 01402 (*it).mtime, destmtime ); 01403 01404 } 01405 else 01406 { 01407 if ( job->error() == ERR_USER_CANCELED ) 01408 res = R_CANCEL; 01409 else if ( !q->isInteractive() ) { 01410 q->Job::slotResult( job ); // will set the error and emit result(this) 01411 return; 01412 } 01413 else 01414 { 01415 SkipDialog_Result skipResult = q->ui()->askSkip( q, files.count() > 1, 01416 job->errorString() ); 01417 01418 // Convert the return code from SkipDialog into a RenameDialog code 01419 res = ( skipResult == S_SKIP ) ? R_SKIP : 01420 ( skipResult == S_AUTO_SKIP ) ? R_AUTO_SKIP : 01421 R_CANCEL; 01422 } 01423 } 01424 01425 if (m_reportTimer) 01426 m_reportTimer->start(REPORT_TIMEOUT); 01427 01428 q->removeSubjob( job ); 01429 assert ( !q->hasSubjobs() ); 01430 switch ( res ) { 01431 case R_CANCEL: 01432 q->setError( ERR_USER_CANCELED ); 01433 q->emitResult(); 01434 return; 01435 case R_AUTO_RENAME: 01436 m_bAutoRenameFiles = true; 01437 // fall through 01438 case R_RENAME: 01439 { 01440 KUrl newUrl( (*it).uDest ); 01441 newUrl.setPath( newPath ); 01442 emit q->renamed( q, (*it).uDest, newUrl ); // for e.g. kpropsdlg 01443 (*it).uDest = newUrl; 01444 01445 QList<CopyInfo> files; 01446 files.append(*it); 01447 emit q->aboutToCreate( q, files ); 01448 } 01449 break; 01450 case R_AUTO_SKIP: 01451 m_bAutoSkipFiles = true; 01452 // fall through 01453 case R_SKIP: 01454 // Move on to next file 01455 skip((*it).uSource, false); 01456 m_processedSize += (*it).size; 01457 files.erase( it ); 01458 m_processedFiles++; 01459 break; 01460 case R_OVERWRITE_ALL: 01461 m_bOverwriteAllFiles = true; 01462 break; 01463 case R_OVERWRITE: 01464 // Add to overwrite list, so that copyNextFile knows to overwrite 01465 m_overwriteList.insert( (*it).uDest.path() ); 01466 break; 01467 default: 01468 assert( 0 ); 01469 } 01470 state = STATE_COPYING_FILES; 01471 copyNextFile(); 01472 } 01473 01474 KIO::Job* CopyJobPrivate::linkNextFile( const KUrl& uSource, const KUrl& uDest, JobFlags flags ) 01475 { 01476 //kDebug(7007) << "Linking"; 01477 if ( 01478 (uSource.protocol() == uDest.protocol()) && 01479 (uSource.host() == uDest.host()) && 01480 (uSource.port() == uDest.port()) && 01481 (uSource.user() == uDest.user()) && 01482 (uSource.pass() == uDest.pass()) ) 01483 { 01484 // This is the case of creating a real symlink 01485 KIO::SimpleJob *newJob = KIO::symlink( uSource.path(), uDest, flags|HideProgressInfo /*no GUI*/ ); 01486 Scheduler::setJobPriority(newJob, 1); 01487 //kDebug(7007) << "Linking target=" << uSource.path() << "link=" << uDest; 01488 //emit linking( this, uSource.path(), uDest ); 01489 m_bCurrentOperationIsLink = true; 01490 m_currentSrcURL=uSource; 01491 m_currentDestURL=uDest; 01492 m_bURLDirty = true; 01493 //Observer::self()->slotCopying( this, uSource, uDest ); // should be slotLinking perhaps 01494 return newJob; 01495 } else { 01496 Q_Q(CopyJob); 01497 //kDebug(7007) << "Linking URL=" << uSource << "link=" << uDest; 01498 if ( uDest.isLocalFile() ) { 01499 // if the source is a devices url, handle it a littlebit special 01500 01501 QString path = uDest.toLocalFile(); 01502 //kDebug(7007) << "path=" << path; 01503 QFile f( path ); 01504 if ( f.open( QIODevice::ReadWrite ) ) 01505 { 01506 f.close(); 01507 KDesktopFile desktopFile( path ); 01508 KConfigGroup config = desktopFile.desktopGroup(); 01509 KUrl url = uSource; 01510 url.setPass( "" ); 01511 config.writePathEntry( "URL", url.url() ); 01512 config.writeEntry( "Name", url.url() ); 01513 config.writeEntry( "Type", QString::fromLatin1("Link") ); 01514 QString protocol = uSource.protocol(); 01515 if ( protocol == QLatin1String("ftp") ) 01516 config.writeEntry( "Icon", QString::fromLatin1("folder-remote") ); 01517 else if ( protocol == QLatin1String("http") ) 01518 config.writeEntry( "Icon", QString::fromLatin1("text-html") ); 01519 else if ( protocol == QLatin1String("info") ) 01520 config.writeEntry( "Icon", QString::fromLatin1("text-x-texinfo") ); 01521 else if ( protocol == QLatin1String("mailto") ) // sven: 01522 config.writeEntry( "Icon", QString::fromLatin1("internet-mail") ); // added mailto: support 01523 else 01524 config.writeEntry( "Icon", QString::fromLatin1("unknown") ); 01525 config.sync(); 01526 files.erase( files.begin() ); // done with this one, move on 01527 m_processedFiles++; 01528 //emit processedAmount( this, KJob::Files, m_processedFiles ); 01529 copyNextFile(); 01530 return 0; 01531 } 01532 else 01533 { 01534 kDebug(7007) << "ERR_CANNOT_OPEN_FOR_WRITING"; 01535 q->setError( ERR_CANNOT_OPEN_FOR_WRITING ); 01536 q->setErrorText( uDest.toLocalFile() ); 01537 q->emitResult(); 01538 return 0; 01539 } 01540 } else { 01541 // Todo: not show "link" on remote dirs if the src urls are not from the same protocol+host+... 01542 q->setError( ERR_CANNOT_SYMLINK ); 01543 q->setErrorText( uDest.prettyUrl() ); 01544 q->emitResult(); 01545 return 0; 01546 } 01547 } 01548 } 01549 01550 void CopyJobPrivate::copyNextFile() 01551 { 01552 Q_Q(CopyJob); 01553 bool bCopyFile = false; 01554 //kDebug(7007); 01555 // Take the first file in the list 01556 QList<CopyInfo>::Iterator it = files.begin(); 01557 // Is this URL on the skip list ? 01558 while (it != files.end() && !bCopyFile) 01559 { 01560 const QString destFile = (*it).uDest.path(); 01561 bCopyFile = !shouldSkip( destFile ); 01562 if ( !bCopyFile ) { 01563 files.erase( it ); 01564 it = files.begin(); 01565 } 01566 } 01567 01568 if (bCopyFile) // any file to create, finally ? 01569 { 01570 //kDebug()<<"preparing to copy"<<(*it).uSource<<(*it).size<<m_freeSpace; 01571 if (m_freeSpace != (KIO::filesize_t)-1 && (*it).size != (KIO::filesize_t)-1) { 01572 if (m_freeSpace < (*it).size) { 01573 q->setError( ERR_DISK_FULL ); 01574 q->emitResult(); 01575 return; 01576 } 01577 //TODO check if dst mount is msdos and (*it).size exceeds it's limits 01578 } 01579 01580 const KUrl& uSource = (*it).uSource; 01581 const KUrl& uDest = (*it).uDest; 01582 // Do we set overwrite ? 01583 bool bOverwrite; 01584 const QString destFile = uDest.path(); 01585 // kDebug(7007) << "copying" << destFile; 01586 if ( uDest == uSource ) 01587 bOverwrite = false; 01588 else 01589 bOverwrite = shouldOverwriteFile( destFile ); 01590 01591 m_bCurrentOperationIsLink = false; 01592 KIO::Job * newjob = 0; 01593 if ( m_mode == CopyJob::Link ) { 01594 // User requested that a symlink be made 01595 const JobFlags flags = bOverwrite ? Overwrite : DefaultFlags; 01596 newjob = linkNextFile(uSource, uDest, flags); 01597 if (!newjob) 01598 return; 01599 } else if ( !(*it).linkDest.isEmpty() && 01600 (uSource.protocol() == uDest.protocol()) && 01601 (uSource.host() == uDest.host()) && 01602 (uSource.port() == uDest.port()) && 01603 (uSource.user() == uDest.user()) && 01604 (uSource.pass() == uDest.pass())) 01605 // Copying a symlink - only on the same protocol/host/etc. (#5601, downloading an FTP file through its link), 01606 { 01607 const JobFlags flags = bOverwrite ? Overwrite : DefaultFlags; 01608 KIO::SimpleJob *newJob = KIO::symlink( (*it).linkDest, uDest, flags | HideProgressInfo /*no GUI*/ ); 01609 Scheduler::setJobPriority(newJob, 1); 01610 newjob = newJob; 01611 //kDebug(7007) << "Linking target=" << (*it).linkDest << "link=" << uDest; 01612 m_currentSrcURL = KUrl( (*it).linkDest ); 01613 m_currentDestURL = uDest; 01614 m_bURLDirty = true; 01615 //emit linking( this, (*it).linkDest, uDest ); 01616 //Observer::self()->slotCopying( this, m_currentSrcURL, uDest ); // should be slotLinking perhaps 01617 m_bCurrentOperationIsLink = true; 01618 // NOTE: if we are moving stuff, the deletion of the source will be done in slotResultCopyingFiles 01619 } else if (m_mode == CopyJob::Move) // Moving a file 01620 { 01621 JobFlags flags = bOverwrite ? Overwrite : DefaultFlags; 01622 KIO::FileCopyJob * moveJob = KIO::file_move( uSource, uDest, (*it).permissions, flags | HideProgressInfo/*no GUI*/ ); 01623 moveJob->setSourceSize( (*it).size ); 01624 if ((*it).mtime != -1) { 01625 moveJob->setModificationTime( QDateTime::fromTime_t( (*it).mtime ) ); // #55804 01626 } 01627 newjob = moveJob; 01628 //kDebug(7007) << "Moving" << uSource << "to" << uDest; 01629 //emit moving( this, uSource, uDest ); 01630 m_currentSrcURL=uSource; 01631 m_currentDestURL=uDest; 01632 m_bURLDirty = true; 01633 //Observer::self()->slotMoving( this, uSource, uDest ); 01634 } 01635 else // Copying a file 01636 { 01637 // If source isn't local and target is local, we ignore the original permissions 01638 // Otherwise, files downloaded from HTTP end up with -r--r--r-- 01639 bool remoteSource = !KProtocolManager::supportsListing(uSource); 01640 int permissions = (*it).permissions; 01641 if ( m_defaultPermissions || ( remoteSource && uDest.isLocalFile() ) ) 01642 permissions = -1; 01643 JobFlags flags = bOverwrite ? Overwrite : DefaultFlags; 01644 KIO::FileCopyJob * copyJob = KIO::file_copy( uSource, uDest, permissions, flags | HideProgressInfo/*no GUI*/ ); 01645 copyJob->setParentJob( q ); // in case of rename dialog 01646 copyJob->setSourceSize( (*it).size ); 01647 if ((*it).mtime != -1) { 01648 copyJob->setModificationTime( QDateTime::fromTime_t( (*it).mtime ) ); 01649 } 01650 newjob = copyJob; 01651 //kDebug(7007) << "Copying" << uSource << "to" << uDest; 01652 m_currentSrcURL=uSource; 01653 m_currentDestURL=uDest; 01654 m_bURLDirty = true; 01655 } 01656 q->addSubjob(newjob); 01657 q->connect( newjob, SIGNAL(processedSize(KJob*,qulonglong)), 01658 SLOT(slotProcessedSize(KJob*,qulonglong)) ); 01659 q->connect( newjob, SIGNAL(totalSize(KJob*,qulonglong)), 01660 SLOT(slotTotalSize(KJob*,qulonglong)) ); 01661 } 01662 else 01663 { 01664 // We're done 01665 //kDebug(7007) << "copyNextFile finished"; 01666 deleteNextDir(); 01667 } 01668 } 01669 01670 void CopyJobPrivate::deleteNextDir() 01671 { 01672 Q_Q(CopyJob); 01673 if ( m_mode == CopyJob::Move && !dirsToRemove.isEmpty() ) // some dirs to delete ? 01674 { 01675 state = STATE_DELETING_DIRS; 01676 m_bURLDirty = true; 01677 // Take first dir to delete out of list - last ones first ! 01678 KUrl::List::Iterator it = --dirsToRemove.end(); 01679 SimpleJob *job = KIO::rmdir( *it ); 01680 Scheduler::setJobPriority(job, 1); 01681 dirsToRemove.erase(it); 01682 q->addSubjob( job ); 01683 } 01684 else 01685 { 01686 // This step is done, move on 01687 state = STATE_SETTING_DIR_ATTRIBUTES; 01688 m_directoriesCopiedIterator = m_directoriesCopied.constBegin(); 01689 setNextDirAttribute(); 01690 } 01691 } 01692 01693 void CopyJobPrivate::setNextDirAttribute() 01694 { 01695 Q_Q(CopyJob); 01696 while (m_directoriesCopiedIterator != m_directoriesCopied.constEnd() && 01697 (*m_directoriesCopiedIterator).mtime == -1) { 01698 ++m_directoriesCopiedIterator; 01699 } 01700 if ( m_directoriesCopiedIterator != m_directoriesCopied.constEnd() ) { 01701 const KUrl url = (*m_directoriesCopiedIterator).uDest; 01702 const time_t mtime = (*m_directoriesCopiedIterator).mtime; 01703 const QDateTime dt = QDateTime::fromTime_t(mtime); 01704 ++m_directoriesCopiedIterator; 01705 01706 KIO::SimpleJob *job = KIO::setModificationTime( url, dt ); 01707 Scheduler::setJobPriority(job, 1); 01708 q->addSubjob( job ); 01709 01710 01711 #if 0 // ifdef Q_OS_UNIX 01712 // TODO: can be removed now. Or reintroduced as a fast path for local files 01713 // if launching even more jobs as done above is a performance problem. 01714 // 01715 QLinkedList<CopyInfo>::const_iterator it = m_directoriesCopied.constBegin(); 01716 for ( ; it != m_directoriesCopied.constEnd() ; ++it ) { 01717 const KUrl& url = (*it).uDest; 01718 if ( url.isLocalFile() && (*it).mtime != (time_t)-1 ) { 01719 KDE_struct_stat statbuf; 01720 if (KDE::lstat(url.path(), &statbuf) == 0) { 01721 struct utimbuf utbuf; 01722 utbuf.actime = statbuf.st_atime; // access time, unchanged 01723 utbuf.modtime = (*it).mtime; // modification time 01724 utime( path, &utbuf ); 01725 } 01726 01727 } 01728 } 01729 m_directoriesCopied.clear(); 01730 // but then we need to jump to the else part below. Maybe with a recursive call? 01731 #endif 01732 } else { 01733 if (m_reportTimer) 01734 m_reportTimer->stop(); 01735 --m_processedFiles; // undo the "start at 1" hack 01736 slotReport(); // display final numbers, important if progress dialog stays up 01737 01738 q->emitResult(); 01739 } 01740 } 01741 01742 void CopyJob::emitResult() 01743 { 01744 Q_D(CopyJob); 01745 // Before we go, tell the world about the changes that were made. 01746 // Even if some error made us abort midway, we might still have done 01747 // part of the job so we better update the views! (#118583) 01748 if (!d->m_bOnlyRenames) { 01749 KUrl url(d->m_globalDest); 01750 if (d->m_globalDestinationState != DEST_IS_DIR || d->m_asMethod) 01751 url.setPath(url.directory()); 01752 //kDebug(7007) << "KDirNotify'ing FilesAdded" << url; 01753 org::kde::KDirNotify::emitFilesAdded( url.url() ); 01754 01755 if (d->m_mode == CopyJob::Move && !d->m_successSrcList.isEmpty()) { 01756 kDebug(7007) << "KDirNotify'ing FilesRemoved" << d->m_successSrcList.toStringList(); 01757 org::kde::KDirNotify::emitFilesRemoved(d->m_successSrcList.toStringList()); 01758 } 01759 01760 // Re-enable watching on the dirs that held the deleted files 01761 if (d->m_mode == CopyJob::Move) { 01762 for (QSet<QString>::const_iterator it = d->m_parentDirs.constBegin() ; it != d->m_parentDirs.constEnd() ; ++it) 01763 KDirWatch::self()->restartDirScan( *it ); 01764 } 01765 } 01766 Job::emitResult(); 01767 } 01768 01769 void CopyJobPrivate::slotProcessedSize( KJob*, qulonglong data_size ) 01770 { 01771 Q_Q(CopyJob); 01772 //kDebug(7007) << data_size; 01773 m_fileProcessedSize = data_size; 01774 q->setProcessedAmount(KJob::Bytes, m_processedSize + m_fileProcessedSize); 01775 01776 if ( m_processedSize + m_fileProcessedSize > m_totalSize ) 01777 { 01778 // Example: download any attachment from bugs.kde.org 01779 m_totalSize = m_processedSize + m_fileProcessedSize; 01780 //kDebug(7007) << "Adjusting m_totalSize to" << m_totalSize; 01781 q->setTotalAmount(KJob::Bytes, m_totalSize); // safety 01782 } 01783 //kDebug(7007) << "emit processedSize" << (unsigned long) (m_processedSize + m_fileProcessedSize); 01784 q->setProcessedAmount(KJob::Bytes, m_processedSize + m_fileProcessedSize); 01785 } 01786 01787 void CopyJobPrivate::slotTotalSize( KJob*, qulonglong size ) 01788 { 01789 Q_Q(CopyJob); 01790 //kDebug(7007) << size; 01791 // Special case for copying a single file 01792 // This is because some protocols don't implement stat properly 01793 // (e.g. HTTP), and don't give us a size in some cases (redirection) 01794 // so we'd rather rely on the size given for the transfer 01795 if ( m_bSingleFileCopy && size != m_totalSize) 01796 { 01797 //kDebug(7007) << "slotTotalSize: updating totalsize to" << size; 01798 m_totalSize = size; 01799 q->setTotalAmount(KJob::Bytes, size); 01800 } 01801 } 01802 01803 void CopyJobPrivate::slotResultDeletingDirs( KJob * job ) 01804 { 01805 Q_Q(CopyJob); 01806 if (job->error()) { 01807 // Couldn't remove directory. Well, perhaps it's not empty 01808 // because the user pressed Skip for a given file in it. 01809 // Let's not display "Could not remove dir ..." for each of those dir ! 01810 } else { 01811 m_successSrcList.append(static_cast<KIO::SimpleJob*>(job)->url()); 01812 } 01813 q->removeSubjob( job ); 01814 assert( !q->hasSubjobs() ); 01815 deleteNextDir(); 01816 } 01817 01818 void CopyJobPrivate::slotResultSettingDirAttributes( KJob * job ) 01819 { 01820 Q_Q(CopyJob); 01821 if (job->error()) 01822 { 01823 // Couldn't set directory attributes. Ignore the error, it can happen 01824 // with inferior file systems like VFAT. 01825 // Let's not display warnings for each dir like "cp -a" does. 01826 } 01827 q->removeSubjob( job ); 01828 assert( !q->hasSubjobs() ); 01829 setNextDirAttribute(); 01830 } 01831 01832 // We were trying to do a direct renaming, before even stat'ing 01833 void CopyJobPrivate::slotResultRenaming( KJob* job ) 01834 { 01835 Q_Q(CopyJob); 01836 int err = job->error(); 01837 const QString errText = job->errorText(); 01838 // Merge metadata from subjob 01839 KIO::Job* kiojob = dynamic_cast<KIO::Job*>(job); 01840 Q_ASSERT(kiojob); 01841 m_incomingMetaData += kiojob->metaData(); 01842 q->removeSubjob( job ); 01843 assert ( !q->hasSubjobs() ); 01844 // Determine dest again 01845 KUrl dest = m_dest; 01846 if ( destinationState == DEST_IS_DIR && !m_asMethod ) 01847 dest.addPath( m_currentSrcURL.fileName() ); 01848 if ( err ) 01849 { 01850 // Direct renaming didn't work. Try renaming to a temp name, 01851 // this can help e.g. when renaming 'a' to 'A' on a VFAT partition. 01852 // In that case it's the _same_ dir, we don't want to copy+del (data loss!) 01853 if ( m_currentSrcURL.isLocalFile() && m_currentSrcURL.url(KUrl::RemoveTrailingSlash) != dest.url(KUrl::RemoveTrailingSlash) && 01854 m_currentSrcURL.url(KUrl::RemoveTrailingSlash).toLower() == dest.url(KUrl::RemoveTrailingSlash).toLower() && 01855 ( err == ERR_FILE_ALREADY_EXIST || 01856 err == ERR_DIR_ALREADY_EXIST || 01857 err == ERR_IDENTICAL_FILES ) ) 01858 { 01859 kDebug(7007) << "Couldn't rename directly, dest already exists. Detected special case of lower/uppercase renaming in same dir, try with 2 rename calls"; 01860 const QString _src( m_currentSrcURL.toLocalFile() ); 01861 const QString _dest( dest.toLocalFile() ); 01862 const QString _tmpPrefix = m_currentSrcURL.directory(KUrl::ObeyTrailingSlash|KUrl::AppendTrailingSlash); 01863 KTemporaryFile tmpFile; 01864 tmpFile.setPrefix(_tmpPrefix); 01865 const bool openOk = tmpFile.open(); 01866 if (!openOk) { 01867 kWarning(7007) << "Couldn't open temp file in" << _tmpPrefix; 01868 } else { 01869 const QString _tmp( tmpFile.fileName() ); 01870 tmpFile.close(); 01871 tmpFile.remove(); 01872 kDebug(7007) << "KTemporaryFile using" << _tmp << "as intermediary"; 01873 if (KDE::rename( _src, _tmp ) == 0) { 01874 //kDebug(7007) << "Renaming" << _src << "to" << _tmp << "succeeded"; 01875 if (!QFile::exists( _dest ) && KDE::rename(_tmp, _dest) == 0) { 01876 err = 0; 01877 org::kde::KDirNotify::emitFileRenamed(m_currentSrcURL.url(), dest.url()); 01878 } else { 01879 kDebug(7007) << "Didn't manage to rename" << _tmp << "to" << _dest << ", reverting"; 01880 // Revert back to original name! 01881 if (KDE::rename( _tmp, _src ) != 0) { 01882 kError(7007) << "Couldn't rename" << _tmp << "back to" << _src << '!'; 01883 // Severe error, abort 01884 q->Job::slotResult(job); // will set the error and emit result(this) 01885 return; 01886 } 01887 } 01888 } else { 01889 kDebug(7007) << "mv" << _src << _tmp << "failed:" << strerror(errno); 01890 } 01891 } 01892 } 01893 } 01894 if ( err ) 01895 { 01896 // This code is similar to CopyJobPrivate::slotResultConflictCopyingFiles 01897 // but here it's about the base src url being moved/renamed 01898 // (m_currentSrcURL) and its dest (m_dest), not about a single file. 01899 // It also means we already stated the dest, here. 01900 // On the other hand we haven't stated the src yet (we skipped doing it 01901 // to save time, since it's not necessary to rename directly!)... 01902 01903 // Existing dest? 01904 if ( err == ERR_DIR_ALREADY_EXIST || 01905 err == ERR_FILE_ALREADY_EXIST || 01906 err == ERR_IDENTICAL_FILES ) 01907 { 01908 // Should we skip automatically ? 01909 bool isDir = (err == ERR_DIR_ALREADY_EXIST); // ## technically, isDir means "source is dir", not "dest is dir" ####### 01910 if ((isDir && m_bAutoSkipDirs) || (!isDir && m_bAutoSkipFiles)) { 01911 // Move on to next source url 01912 skipSrc(isDir); 01913 return; 01914 } else if ((isDir && m_bOverwriteAllDirs) || (!isDir && m_bOverwriteAllFiles)) { 01915 ; // nothing to do, stat+copy+del will overwrite 01916 } else if ((isDir && m_bAutoRenameDirs) || (!isDir && m_bAutoRenameFiles)) { 01917 KUrl destDirectory(m_currentDestURL); // dest including filename 01918 destDirectory.setPath(destDirectory.directory()); 01919 const QString newName = KIO::RenameDialog::suggestName(destDirectory, m_currentDestURL.fileName()); 01920 01921 m_dest.setPath(m_currentDestURL.path()); 01922 m_dest.setFileName(newName); 01923 KIO::Job* job = KIO::stat(m_dest, StatJob::DestinationSide, 2, KIO::HideProgressInfo); 01924 state = STATE_STATING; 01925 destinationState = DEST_NOT_STATED; 01926 q->addSubjob(job); 01927 return; 01928 } else if ( q->isInteractive() ) { 01929 QString newPath; 01930 // we lack mtime info for both the src (not stated) 01931 // and the dest (stated but this info wasn't stored) 01932 // Let's do it for local files, at least 01933 KIO::filesize_t sizeSrc = (KIO::filesize_t) -1; 01934 KIO::filesize_t sizeDest = (KIO::filesize_t) -1; 01935 time_t ctimeSrc = (time_t) -1; 01936 time_t ctimeDest = (time_t) -1; 01937 time_t mtimeSrc = (time_t) -1; 01938 time_t mtimeDest = (time_t) -1; 01939 01940 bool destIsDir = err == ERR_DIR_ALREADY_EXIST; 01941 01942 // ## TODO we need to stat the source using KIO::stat 01943 // so that this code is properly network-transparent. 01944 01945 KDE_struct_stat stat_buf; 01946 if ( m_currentSrcURL.isLocalFile() && 01947 KDE::stat(m_currentSrcURL.toLocalFile(), &stat_buf) == 0 ) { 01948 sizeSrc = stat_buf.st_size; 01949 ctimeSrc = stat_buf.st_ctime; 01950 mtimeSrc = stat_buf.st_mtime; 01951 isDir = S_ISDIR(stat_buf.st_mode); 01952 } 01953 if ( dest.isLocalFile() && 01954 KDE::stat(dest.toLocalFile(), &stat_buf) == 0 ) { 01955 sizeDest = stat_buf.st_size; 01956 ctimeDest = stat_buf.st_ctime; 01957 mtimeDest = stat_buf.st_mtime; 01958 destIsDir = S_ISDIR(stat_buf.st_mode); 01959 } 01960 01961 // If src==dest, use "overwrite-itself" 01962 RenameDialog_Mode mode = ( m_currentSrcURL == dest ) ? M_OVERWRITE_ITSELF : M_OVERWRITE; 01963 if (!isDir && destIsDir) { 01964 // We can't overwrite a dir with a file. 01965 mode = (RenameDialog_Mode) 0; 01966 } 01967 01968 if ( m_srcList.count() > 1 ) 01969 mode = (RenameDialog_Mode) ( mode | M_MULTI | M_SKIP ); 01970 if (destIsDir) 01971 mode = (RenameDialog_Mode) ( mode | M_ISDIR ); 01972 01973 if (m_reportTimer) 01974 m_reportTimer->stop(); 01975 01976 RenameDialog_Result r = q->ui()->askFileRename( 01977 q, 01978 err != ERR_DIR_ALREADY_EXIST ? i18n("File Already Exists") : i18n("Already Exists as Folder"), 01979 m_currentSrcURL.url(), 01980 dest.url(), 01981 mode, newPath, 01982 sizeSrc, sizeDest, 01983 ctimeSrc, ctimeDest, 01984 mtimeSrc, mtimeDest ); 01985 01986 if (m_reportTimer) 01987 m_reportTimer->start(REPORT_TIMEOUT); 01988 01989 switch ( r ) 01990 { 01991 case R_CANCEL: 01992 { 01993 q->setError( ERR_USER_CANCELED ); 01994 q->emitResult(); 01995 return; 01996 } 01997 case R_AUTO_RENAME: 01998 if (isDir) { 01999 m_bAutoRenameDirs = true; 02000 } 02001 else { 02002 m_bAutoRenameFiles = true; 02003 } 02004 // fall through 02005 case R_RENAME: 02006 { 02007 // Set m_dest to the chosen destination 02008 // This is only for this src url; the next one will revert to m_globalDest 02009 m_dest.setPath( newPath ); 02010 KIO::Job* job = KIO::stat( m_dest, StatJob::DestinationSide, 2, KIO::HideProgressInfo ); 02011 state = STATE_STATING; 02012 destinationState = DEST_NOT_STATED; 02013 q->addSubjob(job); 02014 return; 02015 } 02016 case R_AUTO_SKIP: 02017 if (isDir) 02018 m_bAutoSkipDirs = true; 02019 else 02020 m_bAutoSkipFiles = true; 02021 // fall through 02022 case R_SKIP: 02023 // Move on to next url 02024 skipSrc(isDir); 02025 return; 02026 case R_OVERWRITE_ALL: 02027 if (destIsDir) 02028 m_bOverwriteAllDirs = true; 02029 else 02030 m_bOverwriteAllFiles = true; 02031 break; 02032 case R_OVERWRITE: 02033 // Add to overwrite list 02034 // Note that we add dest, not m_dest. 02035 // This ensures that when moving several urls into a dir (m_dest), 02036 // we only overwrite for the current one, not for all. 02037 // When renaming a single file (m_asMethod), it makes no difference. 02038 kDebug(7007) << "adding to overwrite list: " << dest.path(); 02039 m_overwriteList.insert( dest.path() ); 02040 break; 02041 default: 02042 //assert( 0 ); 02043 break; 02044 } 02045 } else if ( err != KIO::ERR_UNSUPPORTED_ACTION ) { 02046 // Dest already exists, and job is not interactive -> abort with error 02047 q->setError( err ); 02048 q->setErrorText( errText ); 02049 q->emitResult(); 02050 return; 02051 } 02052 } else if ( err != KIO::ERR_UNSUPPORTED_ACTION ) { 02053 kDebug(7007) << "Couldn't rename" << m_currentSrcURL << "to" << dest << ", aborting"; 02054 q->setError( err ); 02055 q->setErrorText( errText ); 02056 q->emitResult(); 02057 return; 02058 } 02059 kDebug(7007) << "Couldn't rename" << m_currentSrcURL << "to" << dest << ", reverting to normal way, starting with stat"; 02060 //kDebug(7007) << "KIO::stat on" << m_currentSrcURL; 02061 KIO::Job* job = KIO::stat( m_currentSrcURL, StatJob::SourceSide, 2, KIO::HideProgressInfo ); 02062 state = STATE_STATING; 02063 q->addSubjob(job); 02064 m_bOnlyRenames = false; 02065 } 02066 else 02067 { 02068 kDebug(7007) << "Renaming succeeded, move on"; 02069 ++m_processedFiles; 02070 emit q->copyingDone( q, *m_currentStatSrc, dest, -1 /*mtime unknown, and not needed*/, true, true ); 02071 m_successSrcList.append(*m_currentStatSrc); 02072 statNextSrc(); 02073 } 02074 } 02075 02076 void CopyJob::slotResult( KJob *job ) 02077 { 02078 Q_D(CopyJob); 02079 //kDebug(7007) << "d->state=" << (int) d->state; 02080 // In each case, what we have to do is : 02081 // 1 - check for errors and treat them 02082 // 2 - removeSubjob(job); 02083 // 3 - decide what to do next 02084 02085 switch ( d->state ) { 02086 case STATE_STATING: // We were trying to stat a src url or the dest 02087 d->slotResultStating( job ); 02088 break; 02089 case STATE_RENAMING: // We were trying to do a direct renaming, before even stat'ing 02090 { 02091 d->slotResultRenaming( job ); 02092 break; 02093 } 02094 case STATE_LISTING: // recursive listing finished 02095 //kDebug(7007) << "totalSize:" << (unsigned int) d->m_totalSize << "files:" << d->files.count() << "d->dirs:" << d->dirs.count(); 02096 // Was there an error ? 02097 if (job->error()) 02098 { 02099 Job::slotResult( job ); // will set the error and emit result(this) 02100 return; 02101 } 02102 02103 removeSubjob( job ); 02104 assert ( !hasSubjobs() ); 02105 02106 d->statNextSrc(); 02107 break; 02108 case STATE_CREATING_DIRS: 02109 d->slotResultCreatingDirs( job ); 02110 break; 02111 case STATE_CONFLICT_CREATING_DIRS: 02112 d->slotResultConflictCreatingDirs( job ); 02113 break; 02114 case STATE_COPYING_FILES: 02115 d->slotResultCopyingFiles( job ); 02116 break; 02117 case STATE_CONFLICT_COPYING_FILES: 02118 d->slotResultConflictCopyingFiles( job ); 02119 break; 02120 case STATE_DELETING_DIRS: 02121 d->slotResultDeletingDirs( job ); 02122 break; 02123 case STATE_SETTING_DIR_ATTRIBUTES: 02124 d->slotResultSettingDirAttributes( job ); 02125 break; 02126 default: 02127 assert( 0 ); 02128 } 02129 } 02130 02131 void KIO::CopyJob::setDefaultPermissions( bool b ) 02132 { 02133 d_func()->m_defaultPermissions = b; 02134 } 02135 02136 KIO::CopyJob::CopyMode KIO::CopyJob::operationMode() const 02137 { 02138 return d_func()->m_mode; 02139 } 02140 02141 void KIO::CopyJob::setAutoSkip(bool autoSkip) 02142 { 02143 d_func()->m_bAutoSkipFiles = autoSkip; 02144 d_func()->m_bAutoSkipDirs = autoSkip; 02145 } 02146 02147 void KIO::CopyJob::setAutoRename(bool autoRename) 02148 { 02149 d_func()->m_bAutoRenameFiles = autoRename; 02150 d_func()->m_bAutoRenameDirs = autoRename; 02151 } 02152 02153 void KIO::CopyJob::setWriteIntoExistingDirectories(bool overwriteAll) // #65926 02154 { 02155 d_func()->m_bOverwriteAllDirs = overwriteAll; 02156 } 02157 02158 CopyJob *KIO::copy(const KUrl& src, const KUrl& dest, JobFlags flags) 02159 { 02160 //kDebug(7007) << "src=" << src << "dest=" << dest; 02161 KUrl::List srcList; 02162 srcList.append( src ); 02163 return CopyJobPrivate::newJob(srcList, dest, CopyJob::Copy, false, flags); 02164 } 02165 02166 CopyJob *KIO::copyAs(const KUrl& src, const KUrl& dest, JobFlags flags) 02167 { 02168 //kDebug(7007) << "src=" << src << "dest=" << dest; 02169 KUrl::List srcList; 02170 srcList.append( src ); 02171 return CopyJobPrivate::newJob(srcList, dest, CopyJob::Copy, true, flags); 02172 } 02173 02174 CopyJob *KIO::copy( const KUrl::List& src, const KUrl& dest, JobFlags flags ) 02175 { 02176 //kDebug(7007) << src << dest; 02177 return CopyJobPrivate::newJob(src, dest, CopyJob::Copy, false, flags); 02178 } 02179 02180 CopyJob *KIO::move(const KUrl& src, const KUrl& dest, JobFlags flags) 02181 { 02182 //kDebug(7007) << src << dest; 02183 KUrl::List srcList; 02184 srcList.append( src ); 02185 return CopyJobPrivate::newJob(srcList, dest, CopyJob::Move, false, flags); 02186 } 02187 02188 CopyJob *KIO::moveAs(const KUrl& src, const KUrl& dest, JobFlags flags) 02189 { 02190 //kDebug(7007) << src << dest; 02191 KUrl::List srcList; 02192 srcList.append( src ); 02193 return CopyJobPrivate::newJob(srcList, dest, CopyJob::Move, true, flags); 02194 } 02195 02196 CopyJob *KIO::move( const KUrl::List& src, const KUrl& dest, JobFlags flags) 02197 { 02198 //kDebug(7007) << src << dest; 02199 return CopyJobPrivate::newJob(src, dest, CopyJob::Move, false, flags); 02200 } 02201 02202 CopyJob *KIO::link(const KUrl& src, const KUrl& destDir, JobFlags flags) 02203 { 02204 KUrl::List srcList; 02205 srcList.append( src ); 02206 return CopyJobPrivate::newJob(srcList, destDir, CopyJob::Link, false, flags); 02207 } 02208 02209 CopyJob *KIO::link(const KUrl::List& srcList, const KUrl& destDir, JobFlags flags) 02210 { 02211 return CopyJobPrivate::newJob(srcList, destDir, CopyJob::Link, false, flags); 02212 } 02213 02214 CopyJob *KIO::linkAs(const KUrl& src, const KUrl& destDir, JobFlags flags ) 02215 { 02216 KUrl::List srcList; 02217 srcList.append( src ); 02218 return CopyJobPrivate::newJob(srcList, destDir, CopyJob::Link, false, flags); 02219 } 02220 02221 CopyJob *KIO::trash(const KUrl& src, JobFlags flags) 02222 { 02223 KUrl::List srcList; 02224 srcList.append( src ); 02225 return CopyJobPrivate::newJob(srcList, KUrl( "trash:/" ), CopyJob::Move, false, flags); 02226 } 02227 02228 CopyJob *KIO::trash(const KUrl::List& srcList, JobFlags flags) 02229 { 02230 return CopyJobPrivate::newJob(srcList, KUrl( "trash:/" ), CopyJob::Move, false, flags); 02231 } 02232 02233 #include "copyjob.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:34:57 by doxygen 1.7.5.1 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:34:57 by doxygen 1.7.5.1 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.