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