• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.2 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kio
copyjob.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright 2000 Stephan Kulow <coolo@kde.org>
3  Copyright 2000-2006 David Faure <faure@kde.org>
4  Copyright 2000 Waldo Bastian <bastian@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "copyjob.h"
23 #include <errno.h>
24 #include "kdirlister.h"
25 #include "kfileitem.h"
26 #include "deletejob.h"
27 
28 #include <klocale.h>
29 #include <kdesktopfile.h>
30 #include <kdebug.h>
31 #include <kde_file.h>
32 
33 #include "slave.h"
34 #include "scheduler.h"
35 #include "kdirwatch.h"
36 #include "kprotocolmanager.h"
37 
38 #include "jobuidelegate.h"
39 
40 #include <kdirnotify.h>
41 #include <ktemporaryfile.h>
42 
43 #ifdef Q_OS_UNIX
44 #include <utime.h>
45 #endif
46 #include <assert.h>
47 
48 #include <QtCore/QTimer>
49 #include <QtCore/QFile>
50 #include <sys/stat.h> // mode_t
51 #include <QPointer>
52 
53 #include "job_p.h"
54 #include <kdiskfreespaceinfo.h>
55 #include <kfilesystemtype_p.h>
56 
57 using namespace KIO;
58 
59 //this will update the report dialog with 5 Hz, I think this is fast enough, aleXXX
60 #define REPORT_TIMEOUT 200
61 
62 enum DestinationState {
63  DEST_NOT_STATED,
64  DEST_IS_DIR,
65  DEST_IS_FILE,
66  DEST_DOESNT_EXIST
67 };
68 
85 enum CopyJobState {
86  STATE_STATING,
87  STATE_RENAMING,
88  STATE_LISTING,
89  STATE_CREATING_DIRS,
90  STATE_CONFLICT_CREATING_DIRS,
91  STATE_COPYING_FILES,
92  STATE_CONFLICT_COPYING_FILES,
93  STATE_DELETING_DIRS,
94  STATE_SETTING_DIR_ATTRIBUTES
95 };
96 
98 class KIO::CopyJobPrivate: public KIO::JobPrivate
99 {
100 public:
101  CopyJobPrivate(const KUrl::List& src, const KUrl& dest,
102  CopyJob::CopyMode mode, bool asMethod)
103  : m_globalDest(dest)
104  , m_globalDestinationState(DEST_NOT_STATED)
105  , m_defaultPermissions(false)
106  , m_bURLDirty(false)
107  , m_mode(mode)
108  , m_asMethod(asMethod)
109  , destinationState(DEST_NOT_STATED)
110  , state(STATE_STATING)
111  , m_freeSpace(-1)
112  , m_totalSize(0)
113  , m_processedSize(0)
114  , m_fileProcessedSize(0)
115  , m_processedFiles(0)
116  , m_processedDirs(0)
117  , m_srcList(src)
118  , m_currentStatSrc(m_srcList.constBegin())
119  , m_bCurrentOperationIsLink(false)
120  , m_bSingleFileCopy(false)
121  , m_bOnlyRenames(mode==CopyJob::Move)
122  , m_dest(dest)
123  , m_bAutoRenameFiles(false)
124  , m_bAutoRenameDirs(false)
125  , m_bAutoSkipFiles( false )
126  , m_bAutoSkipDirs( false )
127  , m_bOverwriteAllFiles( false )
128  , m_bOverwriteAllDirs( false )
129  , m_conflictError(0)
130  , m_reportTimer(0)
131  {
132  }
133 
134  // This is the dest URL that was initially given to CopyJob
135  // It is copied into m_dest, which can be changed for a given src URL
136  // (when using the RENAME dialog in slotResult),
137  // and which will be reset for the next src URL.
138  KUrl m_globalDest;
139  // The state info about that global dest
140  DestinationState m_globalDestinationState;
141  // See setDefaultPermissions
142  bool m_defaultPermissions;
143  // Whether URLs changed (and need to be emitted by the next slotReport call)
144  bool m_bURLDirty;
145  // Used after copying all the files into the dirs, to set mtime (TODO: and permissions?)
146  // after the copy is done
147  QLinkedList<CopyInfo> m_directoriesCopied;
148  QLinkedList<CopyInfo>::const_iterator m_directoriesCopiedIterator;
149 
150  CopyJob::CopyMode m_mode;
151  bool m_asMethod;
152  DestinationState destinationState;
153  CopyJobState state;
154 
155  KIO::filesize_t m_freeSpace;
156 
157  KIO::filesize_t m_totalSize;
158  KIO::filesize_t m_processedSize;
159  KIO::filesize_t m_fileProcessedSize;
160  int m_processedFiles;
161  int m_processedDirs;
162  QList<CopyInfo> files;
163  QList<CopyInfo> dirs;
164  KUrl::List dirsToRemove;
165  KUrl::List m_srcList;
166  KUrl::List m_successSrcList; // Entries in m_srcList that have successfully been moved
167  KUrl::List::const_iterator m_currentStatSrc;
168  bool m_bCurrentSrcIsDir;
169  bool m_bCurrentOperationIsLink;
170  bool m_bSingleFileCopy;
171  bool m_bOnlyRenames;
172  KUrl m_dest;
173  KUrl m_currentDest; // set during listing, used by slotEntries
174  //
175  QStringList m_skipList;
176  QSet<QString> m_overwriteList;
177  bool m_bAutoRenameFiles;
178  bool m_bAutoRenameDirs;
179  bool m_bAutoSkipFiles;
180  bool m_bAutoSkipDirs;
181  bool m_bOverwriteAllFiles;
182  bool m_bOverwriteAllDirs;
183  int m_conflictError;
184 
185  QTimer *m_reportTimer;
186 
187  // The current src url being stat'ed or copied
188  // During the stat phase, this is initially equal to *m_currentStatSrc but it can be resolved to a local file equivalent (#188903).
189  KUrl m_currentSrcURL;
190  KUrl m_currentDestURL;
191 
192  QSet<QString> m_parentDirs;
193 
194  void statCurrentSrc();
195  void statNextSrc();
196 
197  // Those aren't slots but submethods for slotResult.
198  void slotResultStating( KJob * job );
199  void startListing( const KUrl & src );
200  void slotResultCreatingDirs( KJob * job );
201  void slotResultConflictCreatingDirs( KJob * job );
202  void createNextDir();
203  void slotResultCopyingFiles( KJob * job );
204  void slotResultConflictCopyingFiles( KJob * job );
205 // KIO::Job* linkNextFile( const KUrl& uSource, const KUrl& uDest, bool overwrite );
206  KIO::Job* linkNextFile( const KUrl& uSource, const KUrl& uDest, JobFlags flags );
207  void copyNextFile();
208  void slotResultDeletingDirs( KJob * job );
209  void deleteNextDir();
210  void sourceStated(const UDSEntry& entry, const KUrl& sourceUrl);
211  void skip(const KUrl & sourceURL, bool isDir);
212  void slotResultRenaming( KJob * job );
213  void slotResultSettingDirAttributes( KJob * job );
214  void setNextDirAttribute();
215 
216  void startRenameJob(const KUrl &slave_url);
217  bool shouldOverwriteDir( const QString& path ) const;
218  bool shouldOverwriteFile( const QString& path ) const;
219  bool shouldSkip( const QString& path ) const;
220  void skipSrc(bool isDir);
221 
222  void slotStart();
223  void slotEntries( KIO::Job*, const KIO::UDSEntryList& list );
224  void slotSubError(KIO::ListJob* job, KIO::ListJob *subJob);
225  void addCopyInfoFromUDSEntry(const UDSEntry& entry, const KUrl& srcUrl, bool srcIsDir, const KUrl& currentDest);
229  void slotProcessedSize( KJob*, qulonglong data_size );
234  void slotTotalSize( KJob*, qulonglong size );
235 
236  void slotReport();
237 
238  Q_DECLARE_PUBLIC(CopyJob)
239 
240  static inline CopyJob *newJob(const KUrl::List& src, const KUrl& dest,
241  CopyJob::CopyMode mode, bool asMethod, JobFlags flags)
242  {
243  CopyJob *job = new CopyJob(*new CopyJobPrivate(src,dest,mode,asMethod));
244  job->setUiDelegate(new JobUiDelegate);
245  if (!(flags & HideProgressInfo))
246  KIO::getJobTracker()->registerJob(job);
247  if (flags & KIO::Overwrite) {
248  job->d_func()->m_bOverwriteAllDirs = true;
249  job->d_func()->m_bOverwriteAllFiles = true;
250  }
251  return job;
252  }
253 };
254 
255 CopyJob::CopyJob(CopyJobPrivate &dd)
256  : Job(dd)
257 {
258  setProperty("destUrl", d_func()->m_dest.url());
259  QTimer::singleShot(0, this, SLOT(slotStart()));
260 }
261 
262 CopyJob::~CopyJob()
263 {
264 }
265 
266 KUrl::List CopyJob::srcUrls() const
267 {
268  return d_func()->m_srcList;
269 }
270 
271 KUrl CopyJob::destUrl() const
272 {
273  return d_func()->m_dest;
274 }
275 
276 void CopyJobPrivate::slotStart()
277 {
278  Q_Q(CopyJob);
284  m_reportTimer = new QTimer(q);
285 
286  q->connect(m_reportTimer,SIGNAL(timeout()),q,SLOT(slotReport()));
287  m_reportTimer->start(REPORT_TIMEOUT);
288 
289  // Stat the dest
290  KIO::Job * job = KIO::stat( m_dest, StatJob::DestinationSide, 2, KIO::HideProgressInfo );
291  //kDebug(7007) << "CopyJob:stating the dest " << m_dest;
292  q->addSubjob(job);
293 }
294 
295 // For unit test purposes
296 KIO_EXPORT bool kio_resolve_local_urls = true;
297 
298 void CopyJobPrivate::slotResultStating( KJob *job )
299 {
300  Q_Q(CopyJob);
301  //kDebug(7007);
302  // Was there an error while stating the src ?
303  if (job->error() && destinationState != DEST_NOT_STATED )
304  {
305  const KUrl srcurl = static_cast<SimpleJob*>(job)->url();
306  if ( !srcurl.isLocalFile() )
307  {
308  // Probably : src doesn't exist. Well, over some protocols (e.g. FTP)
309  // this info isn't really reliable (thanks to MS FTP servers).
310  // We'll assume a file, and try to download anyway.
311  kDebug(7007) << "Error while stating source. Activating hack";
312  q->removeSubjob( job );
313  assert ( !q->hasSubjobs() ); // We should have only one job at a time ...
314  struct CopyInfo info;
315  info.permissions = (mode_t) -1;
316  info.mtime = (time_t) -1;
317  info.ctime = (time_t) -1;
318  info.size = (KIO::filesize_t)-1;
319  info.uSource = srcurl;
320  info.uDest = m_dest;
321  // Append filename or dirname to destination URL, if allowed
322  if ( destinationState == DEST_IS_DIR && !m_asMethod )
323  info.uDest.addPath( srcurl.fileName() );
324 
325  files.append( info );
326  statNextSrc();
327  return;
328  }
329  // Local file. If stat fails, the file definitely doesn't exist.
330  // yes, q->Job::, because we don't want to call our override
331  q->Job::slotResult( job ); // will set the error and emit result(this)
332  return;
333  }
334 
335  // Keep copy of the stat result
336  const UDSEntry entry = static_cast<StatJob*>(job)->statResult();
337 
338  if ( destinationState == DEST_NOT_STATED ) {
339  if ( m_dest.isLocalFile() ) { //works for dirs as well
340  QString path = m_dest.toLocalFile();
341  if (m_asMethod) {
342  // In copy-as mode, we want to check the directory to which we're
343  // copying. The target file or directory does not exist yet, which
344  // might confuse KDiskFreeSpaceInfo.
345  path = QFileInfo(path).absolutePath();
346  }
347  KFileSystemType::Type fsType = KFileSystemType::fileSystemType( path );
348  if ( fsType != KFileSystemType::Nfs && fsType != KFileSystemType::Smb ) {
349  m_freeSpace = KDiskFreeSpaceInfo::freeSpaceInfo( path ).available();
350  }
351  //TODO actually preliminary check is even more valuable for slow NFS/SMB mounts,
352  //but we need to find a way to report connection errors to user
353  }
354 
355  const bool isGlobalDest = m_dest == m_globalDest;
356  const bool isDir = entry.isDir();
357  // we were stating the dest
358  if (job->error()) {
359  destinationState = DEST_DOESNT_EXIST;
360  //kDebug(7007) << "dest does not exist";
361  } else {
362  // Treat symlinks to dirs as dirs here, so no test on isLink
363  destinationState = isDir ? DEST_IS_DIR : DEST_IS_FILE;
364  //kDebug(7007) << "dest is dir:" << isDir;
365 
366  const QString sLocalPath = entry.stringValue( KIO::UDSEntry::UDS_LOCAL_PATH );
367  if ( !sLocalPath.isEmpty() && kio_resolve_local_urls && destinationState != DEST_DOESNT_EXIST ) {
368  m_dest = KUrl();
369  m_dest.setPath(sLocalPath);
370  if ( isGlobalDest )
371  m_globalDest = m_dest;
372  }
373  }
374  if ( isGlobalDest )
375  m_globalDestinationState = destinationState;
376 
377  q->removeSubjob( job );
378  assert ( !q->hasSubjobs() );
379 
380  // After knowing what the dest is, we can start stat'ing the first src.
381  statCurrentSrc();
382  } else {
383  sourceStated(entry, static_cast<SimpleJob*>(job)->url());
384  q->removeSubjob( job );
385  }
386 }
387 
388 void CopyJobPrivate::sourceStated(const UDSEntry& entry, const KUrl& sourceUrl)
389 {
390  const QString sLocalPath = entry.stringValue( KIO::UDSEntry::UDS_LOCAL_PATH );
391  const bool isDir = entry.isDir();
392 
393  // We were stating the current source URL
394  // Is it a file or a dir ?
395 
396  // There 6 cases, and all end up calling addCopyInfoFromUDSEntry first :
397  // 1 - src is a dir, destination is a directory,
398  // slotEntries will append the source-dir-name to the destination
399  // 2 - src is a dir, destination is a file -- will offer to overwrite, later on.
400  // 3 - src is a dir, destination doesn't exist, then it's the destination dirname,
401  // so slotEntries will use it as destination.
402 
403  // 4 - src is a file, destination is a directory,
404  // slotEntries will append the filename to the destination.
405  // 5 - src is a file, destination is a file, m_dest is the exact destination name
406  // 6 - src is a file, destination doesn't exist, m_dest is the exact destination name
407 
408  KUrl srcurl;
409  if (!sLocalPath.isEmpty() && destinationState != DEST_DOESNT_EXIST) {
410  kDebug() << "Using sLocalPath. destinationState=" << destinationState;
411  // Prefer the local path -- but only if we were able to stat() the dest.
412  // Otherwise, renaming a desktop:/ url would copy from src=file to dest=desktop (#218719)
413  srcurl.setPath(sLocalPath);
414  } else {
415  srcurl = sourceUrl;
416  }
417  addCopyInfoFromUDSEntry(entry, srcurl, false, m_dest);
418 
419  m_currentDest = m_dest;
420  m_bCurrentSrcIsDir = false;
421 
422  if ( isDir
423  // treat symlinks as files (no recursion)
424  && !entry.isLink()
425  && m_mode != CopyJob::Link ) // No recursion in Link mode either.
426  {
427  //kDebug(7007) << "Source is a directory";
428 
429  if (srcurl.isLocalFile()) {
430  const QString parentDir = srcurl.toLocalFile(KUrl::RemoveTrailingSlash);
431  m_parentDirs.insert(parentDir);
432  }
433 
434  m_bCurrentSrcIsDir = true; // used by slotEntries
435  if ( destinationState == DEST_IS_DIR ) // (case 1)
436  {
437  if ( !m_asMethod )
438  {
439  // Use <desturl>/<directory_copied> as destination, from now on
440  QString directory = srcurl.fileName();
441  const QString sName = entry.stringValue( KIO::UDSEntry::UDS_NAME );
442  KProtocolInfo::FileNameUsedForCopying fnu = KProtocolManager::fileNameUsedForCopying(srcurl);
443  if (fnu == KProtocolInfo::Name) {
444  if (!sName.isEmpty())
445  directory = sName;
446  } else if (fnu == KProtocolInfo::DisplayName) {
447  const QString dispName = entry.stringValue( KIO::UDSEntry::UDS_DISPLAY_NAME );
448  if (!dispName.isEmpty())
449  directory = dispName;
450  else if (!sName.isEmpty())
451  directory = sName;
452  }
453  m_currentDest.addPath( directory );
454  }
455  }
456  else // (case 3)
457  {
458  // otherwise dest is new name for toplevel dir
459  // so the destination exists, in fact, from now on.
460  // (This even works with other src urls in the list, since the
461  // dir has effectively been created)
462  destinationState = DEST_IS_DIR;
463  if ( m_dest == m_globalDest )
464  m_globalDestinationState = destinationState;
465  }
466 
467  startListing( srcurl );
468  }
469  else
470  {
471  //kDebug(7007) << "Source is a file (or a symlink), or we are linking -> no recursive listing";
472 
473  if (srcurl.isLocalFile()) {
474  const QString parentDir = srcurl.directory(KUrl::ObeyTrailingSlash);
475  m_parentDirs.insert(parentDir);
476  }
477 
478  statNextSrc();
479  }
480 }
481 
482 bool CopyJob::doSuspend()
483 {
484  Q_D(CopyJob);
485  d->slotReport();
486  return Job::doSuspend();
487 }
488 
489 void CopyJobPrivate::slotReport()
490 {
491  Q_Q(CopyJob);
492  if ( q->isSuspended() )
493  return;
494  // If showProgressInfo was set, progressId() is > 0.
495  switch (state) {
496  case STATE_RENAMING:
497  q->setTotalAmount(KJob::Files, m_srcList.count());
498  // fall-through intended
499  case STATE_COPYING_FILES:
500  q->setProcessedAmount( KJob::Files, m_processedFiles );
501  if (m_bURLDirty)
502  {
503  // Only emit urls when they changed. This saves time, and fixes #66281
504  m_bURLDirty = false;
505  if (m_mode==CopyJob::Move)
506  {
507  emitMoving(q, m_currentSrcURL, m_currentDestURL);
508  emit q->moving( q, m_currentSrcURL, m_currentDestURL);
509  }
510  else if (m_mode==CopyJob::Link)
511  {
512  emitCopying( q, m_currentSrcURL, m_currentDestURL ); // we don't have a delegate->linking
513  emit q->linking( q, m_currentSrcURL.path(), m_currentDestURL );
514  }
515  else
516  {
517  emitCopying( q, m_currentSrcURL, m_currentDestURL );
518  emit q->copying( q, m_currentSrcURL, m_currentDestURL );
519  }
520  }
521  break;
522 
523  case STATE_CREATING_DIRS:
524  q->setProcessedAmount( KJob::Directories, m_processedDirs );
525  if (m_bURLDirty)
526  {
527  m_bURLDirty = false;
528  emit q->creatingDir( q, m_currentDestURL );
529  emitCreatingDir( q, m_currentDestURL );
530  }
531  break;
532 
533  case STATE_STATING:
534  case STATE_LISTING:
535  if (m_bURLDirty)
536  {
537  m_bURLDirty = false;
538  if (m_mode==CopyJob::Move)
539  {
540  emitMoving( q, m_currentSrcURL, m_currentDestURL );
541  }
542  else
543  {
544  emitCopying( q, m_currentSrcURL, m_currentDestURL );
545  }
546  }
547  q->setTotalAmount(KJob::Bytes, m_totalSize);
548  q->setTotalAmount(KJob::Files, files.count());
549  q->setTotalAmount(KJob::Directories, dirs.count());
550  break;
551 
552  default:
553  break;
554  }
555 }
556 
557 void CopyJobPrivate::slotEntries(KIO::Job* job, const UDSEntryList& list)
558 {
559  //Q_Q(CopyJob);
560  UDSEntryList::ConstIterator it = list.constBegin();
561  UDSEntryList::ConstIterator end = list.constEnd();
562  for (; it != end; ++it) {
563  const UDSEntry& entry = *it;
564  addCopyInfoFromUDSEntry(entry, static_cast<SimpleJob *>(job)->url(), m_bCurrentSrcIsDir, m_currentDest);
565  }
566 }
567 
568 void CopyJobPrivate::slotSubError(ListJob* job, ListJob* subJob)
569 {
570  const KUrl url = subJob->url();
571  kWarning() << url << subJob->errorString();
572 
573  Q_Q(CopyJob);
574 
575  emit q->warning(job, subJob->errorString(), QString());
576  skip(url, true);
577 }
578 
579 
580 void CopyJobPrivate::addCopyInfoFromUDSEntry(const UDSEntry& entry, const KUrl& srcUrl, bool srcIsDir, const KUrl& currentDest)
581 {
582  struct CopyInfo info;
583  info.permissions = entry.numberValue(KIO::UDSEntry::UDS_ACCESS, -1);
584  info.mtime = (time_t) entry.numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME, -1);
585  info.ctime = (time_t) entry.numberValue(KIO::UDSEntry::UDS_CREATION_TIME, -1);
586  info.size = (KIO::filesize_t) entry.numberValue(KIO::UDSEntry::UDS_SIZE, -1);
587  if (info.size != (KIO::filesize_t) -1)
588  m_totalSize += info.size;
589 
590  // recursive listing, displayName can be a/b/c/d
591  const QString fileName = entry.stringValue(KIO::UDSEntry::UDS_NAME);
592  const QString urlStr = entry.stringValue(KIO::UDSEntry::UDS_URL);
593  KUrl url;
594  if (!urlStr.isEmpty())
595  url = urlStr;
596  QString localPath = entry.stringValue(KIO::UDSEntry::UDS_LOCAL_PATH);
597  const bool isDir = entry.isDir();
598  info.linkDest = entry.stringValue(KIO::UDSEntry::UDS_LINK_DEST);
599 
600  if (fileName != QLatin1String("..") && fileName != QLatin1String(".")) {
601  const bool hasCustomURL = !url.isEmpty() || !localPath.isEmpty();
602  if (!hasCustomURL) {
603  // Make URL from displayName
604  url = srcUrl;
605  if (srcIsDir) { // Only if src is a directory. Otherwise uSource is fine as is
606  //kDebug(7007) << "adding path" << displayName;
607  url.addPath(fileName);
608  }
609  }
610  //kDebug(7007) << "displayName=" << displayName << "url=" << url;
611  if (!localPath.isEmpty() && kio_resolve_local_urls && destinationState != DEST_DOESNT_EXIST) {
612  url = KUrl(localPath);
613  }
614 
615  info.uSource = url;
616  info.uDest = currentDest;
617  //kDebug(7007) << "uSource=" << info.uSource << "uDest(1)=" << info.uDest;
618  // Append filename or dirname to destination URL, if allowed
619  if (destinationState == DEST_IS_DIR &&
620  // "copy/move as <foo>" means 'foo' is the dest for the base srcurl
621  // (passed here during stating) but not its children (during listing)
622  (! (m_asMethod && state == STATE_STATING)))
623  {
624  QString destFileName;
625  KProtocolInfo::FileNameUsedForCopying fnu = KProtocolManager::fileNameUsedForCopying(url);
626  if (hasCustomURL &&
627  fnu == KProtocolInfo::FromUrl) {
628  //destFileName = url.fileName(); // Doesn't work for recursive listing
629  // Count the number of prefixes used by the recursive listjob
630  int numberOfSlashes = fileName.count('/'); // don't make this a find()!
631  QString path = url.path();
632  int pos = 0;
633  for (int n = 0; n < numberOfSlashes + 1; ++n) {
634  pos = path.lastIndexOf('/', pos - 1);
635  if (pos == -1) { // error
636  kWarning(7007) << "kioslave bug: not enough slashes in UDS_URL" << path << "- looking for" << numberOfSlashes << "slashes";
637  break;
638  }
639  }
640  if (pos >= 0) {
641  destFileName = path.mid(pos + 1);
642  }
643 
644  } else if ( fnu == KProtocolInfo::Name ) { // destination filename taken from UDS_NAME
645  destFileName = fileName;
646  } else { // from display name (with fallback to name)
647  const QString displayName = entry.stringValue(KIO::UDSEntry::UDS_DISPLAY_NAME);
648  destFileName = displayName.isEmpty() ? fileName : displayName;
649  }
650 
651  // Here we _really_ have to add some filename to the dest.
652  // Otherwise, we end up with e.g. dest=..../Desktop/ itself.
653  // (This can happen when dropping a link to a webpage with no path)
654  if (destFileName.isEmpty()) {
655  destFileName = KIO::encodeFileName(info.uSource.prettyUrl());
656  }
657 
658  //kDebug(7007) << " adding destFileName=" << destFileName;
659  info.uDest.addPath(destFileName);
660  }
661  //kDebug(7007) << " uDest(2)=" << info.uDest;
662  //kDebug(7007) << " " << info.uSource << "->" << info.uDest;
663  if (info.linkDest.isEmpty() && isDir && m_mode != CopyJob::Link) { // Dir
664  dirs.append(info); // Directories
665  if (m_mode == CopyJob::Move) {
666  dirsToRemove.append(info.uSource);
667  }
668  } else {
669  files.append(info); // Files and any symlinks
670  }
671  }
672 }
673 
674 void CopyJobPrivate::skipSrc(bool isDir)
675 {
676  m_dest = m_globalDest;
677  destinationState = m_globalDestinationState;
678  skip(*m_currentStatSrc, isDir);
679  ++m_currentStatSrc;
680  statCurrentSrc();
681 }
682 
683 void CopyJobPrivate::statNextSrc()
684 {
685  /* Revert to the global destination, the one that applies to all source urls.
686  * 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.
687  * d->m_dest is /foo/b for b, but we have to revert to /d for item c and following.
688  */
689  m_dest = m_globalDest;
690  destinationState = m_globalDestinationState;
691  ++m_currentStatSrc;
692  statCurrentSrc();
693 }
694 
695 void CopyJobPrivate::statCurrentSrc()
696 {
697  Q_Q(CopyJob);
698  if (m_currentStatSrc != m_srcList.constEnd()) {
699  m_currentSrcURL = (*m_currentStatSrc);
700  m_bURLDirty = true;
701  if (m_mode == CopyJob::Link) {
702  // Skip the "stating the source" stage, we don't need it for linking
703  m_currentDest = m_dest;
704  struct CopyInfo info;
705  info.permissions = -1;
706  info.mtime = (time_t) -1;
707  info.ctime = (time_t) -1;
708  info.size = (KIO::filesize_t)-1;
709  info.uSource = m_currentSrcURL;
710  info.uDest = m_currentDest;
711  // Append filename or dirname to destination URL, if allowed
712  if (destinationState == DEST_IS_DIR && !m_asMethod) {
713  if (
714  (m_currentSrcURL.protocol() == info.uDest.protocol()) &&
715  (m_currentSrcURL.host() == info.uDest.host()) &&
716  (m_currentSrcURL.port() == info.uDest.port()) &&
717  (m_currentSrcURL.user() == info.uDest.user()) &&
718  (m_currentSrcURL.pass() == info.uDest.pass()) ) {
719  // This is the case of creating a real symlink
720  info.uDest.addPath( m_currentSrcURL.fileName() );
721  } else {
722  // Different protocols, we'll create a .desktop file
723  // We have to change the extension anyway, so while we're at it,
724  // name the file like the URL
725  info.uDest.addPath(KIO::encodeFileName(m_currentSrcURL.prettyUrl()) + ".desktop");
726  }
727  }
728  files.append( info ); // Files and any symlinks
729  statNextSrc(); // we could use a loop instead of a recursive call :)
730  return;
731  }
732 
733  // Let's see if we can skip stat'ing, for the case where a directory view has the info already
734  const KFileItem cachedItem = KDirLister::cachedItemForUrl(m_currentSrcURL);
735  KIO::UDSEntry entry;
736  if (!cachedItem.isNull()) {
737  entry = cachedItem.entry();
738  if (destinationState != DEST_DOESNT_EXIST) { // only resolve src if we could resolve dest (#218719)
739  bool dummyIsLocal;
740  m_currentSrcURL = cachedItem.mostLocalUrl(dummyIsLocal); // #183585
741  }
742  }
743 
744  if (m_mode == CopyJob::Move && (
745  // Don't go renaming right away if we need a stat() to find out the destination filename
746  KProtocolManager::fileNameUsedForCopying(m_currentSrcURL) == KProtocolInfo::FromUrl ||
747  destinationState != DEST_IS_DIR || m_asMethod)
748  ) {
749  // If moving, before going for the full stat+[list+]copy+del thing, try to rename
750  // The logic is pretty similar to FileCopyJobPrivate::slotStart()
751  if ( (m_currentSrcURL.protocol() == m_dest.protocol()) &&
752  (m_currentSrcURL.host() == m_dest.host()) &&
753  (m_currentSrcURL.port() == m_dest.port()) &&
754  (m_currentSrcURL.user() == m_dest.user()) &&
755  (m_currentSrcURL.pass() == m_dest.pass()) )
756  {
757  startRenameJob( m_currentSrcURL );
758  return;
759  }
760  else if ( m_currentSrcURL.isLocalFile() && KProtocolManager::canRenameFromFile( m_dest ) )
761  {
762  startRenameJob( m_dest );
763  return;
764  }
765  else if ( m_dest.isLocalFile() && KProtocolManager::canRenameToFile( m_currentSrcURL ) )
766  {
767  startRenameJob( m_currentSrcURL );
768  return;
769  }
770  }
771 
772  // if the file system doesn't support deleting, we do not even stat
773  if (m_mode == CopyJob::Move && !KProtocolManager::supportsDeleting(m_currentSrcURL)) {
774  QPointer<CopyJob> that = q;
775  emit q->warning( q, buildErrorString(ERR_CANNOT_DELETE, m_currentSrcURL.prettyUrl()) );
776  if (that)
777  statNextSrc(); // we could use a loop instead of a recursive call :)
778  return;
779  }
780 
781  m_bOnlyRenames = false;
782 
783  // Testing for entry.count()>0 here is not good enough; KFileItem inserts
784  // entries for UDS_USER and UDS_GROUP even on initially empty UDSEntries (#192185)
785  if (entry.contains(KIO::UDSEntry::UDS_NAME)) {
786  kDebug(7007) << "fast path! found info about" << m_currentSrcURL << "in KDirLister";
787  sourceStated(entry, m_currentSrcURL);
788  return;
789  }
790 
791  // Stat the next src url
792  Job * job = KIO::stat( m_currentSrcURL, StatJob::SourceSide, 2, KIO::HideProgressInfo );
793  //kDebug(7007) << "KIO::stat on" << m_currentSrcURL;
794  state = STATE_STATING;
795  q->addSubjob(job);
796  m_currentDestURL = m_dest;
797  m_bURLDirty = true;
798  }
799  else
800  {
801  // Finished the stat'ing phase
802  // First make sure that the totals were correctly emitted
803  state = STATE_STATING;
804  m_bURLDirty = true;
805  slotReport();
806 
807  kDebug(7007)<<"Stating finished. To copy:"<<m_totalSize<<", available:"<<m_freeSpace;
808  //TODO warn user beforehand if space is not enough
809 
810  if (!dirs.isEmpty())
811  emit q->aboutToCreate( q, dirs );
812  if (!files.isEmpty())
813  emit q->aboutToCreate( q, files );
814  // Check if we are copying a single file
815  m_bSingleFileCopy = ( files.count() == 1 && dirs.isEmpty() );
816  // Then start copying things
817  state = STATE_CREATING_DIRS;
818  createNextDir();
819  }
820 }
821 
822 void CopyJobPrivate::startRenameJob( const KUrl& slave_url )
823 {
824  Q_Q(CopyJob);
825 
826  // Silence KDirWatch notifications, otherwise performance is horrible
827  if (m_currentSrcURL.isLocalFile()) {
828  const QString parentDir = m_currentSrcURL.directory(KUrl::ObeyTrailingSlash);
829  if (!m_parentDirs.contains(parentDir)) {
830  KDirWatch::self()->stopDirScan(parentDir);
831  m_parentDirs.insert(parentDir);
832  }
833  }
834 
835  KUrl dest = m_dest;
836  // Append filename or dirname to destination URL, if allowed
837  if ( destinationState == DEST_IS_DIR && !m_asMethod )
838  dest.addPath( m_currentSrcURL.fileName() );
839  m_currentDestURL = dest;
840  kDebug(7007) << m_currentSrcURL << "->" << dest << "trying direct rename first";
841  state = STATE_RENAMING;
842 
843  struct CopyInfo info;
844  info.permissions = -1;
845  info.mtime = (time_t) -1;
846  info.ctime = (time_t) -1;
847  info.size = (KIO::filesize_t)-1;
848  info.uSource = m_currentSrcURL;
849  info.uDest = dest;
850  QList<CopyInfo> files;
851  files.append(info);
852  emit q->aboutToCreate( q, files );
853 
854  KIO_ARGS << m_currentSrcURL << dest << (qint8) false /*no overwrite*/;
855  SimpleJob * newJob = SimpleJobPrivate::newJobNoUi(slave_url, CMD_RENAME, packedArgs);
856  Scheduler::setJobPriority(newJob, 1);
857  q->addSubjob( newJob );
858  if ( m_currentSrcURL.directory() != dest.directory() ) // For the user, moving isn't renaming. Only renaming is.
859  m_bOnlyRenames = false;
860 }
861 
862 void CopyJobPrivate::startListing( const KUrl & src )
863 {
864  Q_Q(CopyJob);
865  state = STATE_LISTING;
866  m_bURLDirty = true;
867  ListJob * newjob = listRecursive(src, KIO::HideProgressInfo);
868  newjob->setUnrestricted(true);
869  q->connect(newjob, SIGNAL(entries(KIO::Job*,KIO::UDSEntryList)),
870  SLOT(slotEntries(KIO::Job*,KIO::UDSEntryList)));
871  q->connect(newjob, SIGNAL(subError(KIO::ListJob*,KIO::ListJob*)),
872  SLOT(slotSubError(KIO::ListJob*,KIO::ListJob*)));
873  q->addSubjob( newjob );
874 }
875 
876 void CopyJobPrivate::skip(const KUrl & sourceUrl, bool isDir)
877 {
878  KUrl dir = sourceUrl;
879  if (!isDir) {
880  // Skipping a file: make sure not to delete the parent dir (#208418)
881  dir.setPath(dir.directory());
882  }
883  while (dirsToRemove.removeAll(dir) > 0) {
884  // Do not rely on rmdir() on the parent directories aborting.
885  // Exclude the parent dirs explicitly.
886  dir.setPath(dir.directory());
887  }
888 }
889 
890 bool CopyJobPrivate::shouldOverwriteDir( const QString& path ) const
891 {
892  if ( m_bOverwriteAllDirs )
893  return true;
894  return m_overwriteList.contains(path);
895 }
896 
897 bool CopyJobPrivate::shouldOverwriteFile( const QString& path ) const
898 {
899  if ( m_bOverwriteAllFiles )
900  return true;
901  return m_overwriteList.contains(path);
902 }
903 
904 bool CopyJobPrivate::shouldSkip( const QString& path ) const
905 {
906  Q_FOREACH(const QString& skipPath, m_skipList) {
907  if ( path.startsWith(skipPath) )
908  return true;
909  }
910  return false;
911 }
912 
913 void CopyJobPrivate::slotResultCreatingDirs( KJob * job )
914 {
915  Q_Q(CopyJob);
916  // The dir we are trying to create:
917  QList<CopyInfo>::Iterator it = dirs.begin();
918  // Was there an error creating a dir ?
919  if ( job->error() )
920  {
921  m_conflictError = job->error();
922  if ( (m_conflictError == ERR_DIR_ALREADY_EXIST)
923  || (m_conflictError == ERR_FILE_ALREADY_EXIST) ) // can't happen?
924  {
925  KUrl oldURL = ((SimpleJob*)job)->url();
926  // Should we skip automatically ?
927  if ( m_bAutoSkipDirs ) {
928  // We don't want to copy files in this directory, so we put it on the skip list
929  m_skipList.append( oldURL.path( KUrl::AddTrailingSlash ) );
930  skip(oldURL, true);
931  dirs.erase( it ); // Move on to next dir
932  } else {
933  // Did the user choose to overwrite already?
934  const QString destDir = (*it).uDest.path();
935  if ( shouldOverwriteDir( destDir ) ) { // overwrite => just skip
936  emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true /* directory */, false /* renamed */ );
937  dirs.erase( it ); // Move on to next dir
938  } else {
939  if (m_bAutoRenameDirs) {
940  QString oldPath = (*it).uDest.path(KUrl::AddTrailingSlash);
941 
942  KUrl destDirectory((*it).uDest);
943  destDirectory.setPath(destDirectory.directory());
944  QString newName = KIO::RenameDialog::suggestName(destDirectory, (*it).uDest.fileName());
945 
946  KUrl newUrl((*it).uDest);
947  newUrl.setFileName(newName);
948 
949  emit q->renamed(q, (*it).uDest, newUrl); // for e.g. kpropsdlg
950 
951  // Change the current one and strip the trailing '/'
952  (*it).uDest.setPath(newUrl.path(KUrl::RemoveTrailingSlash));
953 
954  QString newPath = newUrl.path(KUrl::AddTrailingSlash); // With trailing slash
955  QList<CopyInfo>::Iterator renamedirit = it;
956  ++renamedirit;
957  // Change the name of subdirectories inside the directory
958  for(; renamedirit != dirs.end() ; ++renamedirit) {
959  QString path = (*renamedirit).uDest.path();
960  if (path.startsWith(oldPath)) {
961  QString n = path;
962  n.replace(0, oldPath.length(), newPath);
963  kDebug(7007) << "dirs list:" << (*renamedirit).uSource.path()
964  << "was going to be" << path
965  << ", changed into" << n;
966  (*renamedirit).uDest.setPath(n);
967  }
968  }
969  // Change filenames inside the directory
970  QList<CopyInfo>::Iterator renamefileit = files.begin();
971  for(; renamefileit != files.end() ; ++renamefileit) {
972  QString path = (*renamefileit).uDest.path();
973  if (path.startsWith(oldPath)) {
974  QString n = path;
975  n.replace(0, oldPath.length(), newPath);
976  kDebug(7007) << "files list:" << (*renamefileit).uSource.path()
977  << "was going to be" << path
978  << ", changed into" << n;
979  (*renamefileit).uDest.setPath(n);
980  }
981  }
982  if (!dirs.isEmpty()) {
983  emit q->aboutToCreate(q, dirs);
984  }
985  if (!files.isEmpty()) {
986  emit q->aboutToCreate(q, files);
987  }
988 
989  }
990  else {
991  if (!q->isInteractive()) {
992  q->Job::slotResult(job); // will set the error and emit result(this)
993  return;
994  }
995 
996  assert(((SimpleJob*)job)->url().url() == (*it).uDest.url());
997  q->removeSubjob(job);
998  assert (!q->hasSubjobs()); // We should have only one job at a time ...
999 
1000  // We need to stat the existing dir, to get its last-modification time
1001  KUrl existingDest((*it).uDest);
1002  SimpleJob * newJob = KIO::stat(existingDest, StatJob::DestinationSide, 2, KIO::HideProgressInfo);
1003  Scheduler::setJobPriority(newJob, 1);
1004  kDebug(7007) << "KIO::stat for resolving conflict on " << existingDest;
1005  state = STATE_CONFLICT_CREATING_DIRS;
1006  q->addSubjob(newJob);
1007  return; // Don't move to next dir yet !
1008  }
1009  }
1010  }
1011  }
1012  else
1013  {
1014  // Severe error, abort
1015  q->Job::slotResult( job ); // will set the error and emit result(this)
1016  return;
1017  }
1018  }
1019  else // no error : remove from list, to move on to next dir
1020  {
1021  //this is required for the undo feature
1022  emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true, false );
1023  m_directoriesCopied.append( *it );
1024  dirs.erase( it );
1025  }
1026 
1027  m_processedDirs++;
1028  //emit processedAmount( this, KJob::Directories, m_processedDirs );
1029  q->removeSubjob( job );
1030  assert( !q->hasSubjobs() ); // We should have only one job at a time ...
1031  createNextDir();
1032 }
1033 
1034 void CopyJobPrivate::slotResultConflictCreatingDirs( KJob * job )
1035 {
1036  Q_Q(CopyJob);
1037  // We come here after a conflict has been detected and we've stated the existing dir
1038 
1039  // The dir we were trying to create:
1040  QList<CopyInfo>::Iterator it = dirs.begin();
1041 
1042  const UDSEntry entry = ((KIO::StatJob*)job)->statResult();
1043 
1044  // Its modification time:
1045  const time_t destmtime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_MODIFICATION_TIME, -1 );
1046  const time_t destctime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_CREATION_TIME, -1 );
1047 
1048  const KIO::filesize_t destsize = entry.numberValue( KIO::UDSEntry::UDS_SIZE );
1049  const QString linkDest = entry.stringValue( KIO::UDSEntry::UDS_LINK_DEST );
1050 
1051  q->removeSubjob( job );
1052  assert ( !q->hasSubjobs() ); // We should have only one job at a time ...
1053 
1054  // Always multi and skip (since there are files after that)
1055  RenameDialog_Mode mode = (RenameDialog_Mode)( M_MULTI | M_SKIP | M_ISDIR );
1056  // Overwrite only if the existing thing is a dir (no chance with a file)
1057  if ( m_conflictError == ERR_DIR_ALREADY_EXIST )
1058  {
1059  if( (*it).uSource == (*it).uDest ||
1060  ((*it).uSource.protocol() == (*it).uDest.protocol() &&
1061  (*it).uSource.path( KUrl::RemoveTrailingSlash ) == linkDest) )
1062  mode = (RenameDialog_Mode)( mode | M_OVERWRITE_ITSELF);
1063  else
1064  mode = (RenameDialog_Mode)( mode | M_OVERWRITE );
1065  }
1066 
1067  QString existingDest = (*it).uDest.path();
1068  QString newPath;
1069  if (m_reportTimer)
1070  m_reportTimer->stop();
1071  RenameDialog_Result r = q->ui()->askFileRename( q, i18n("Folder Already Exists"),
1072  (*it).uSource.url(),
1073  (*it).uDest.url(),
1074  mode, newPath,
1075  (*it).size, destsize,
1076  (*it).ctime, destctime,
1077  (*it).mtime, destmtime );
1078  if (m_reportTimer)
1079  m_reportTimer->start(REPORT_TIMEOUT);
1080  switch ( r ) {
1081  case R_CANCEL:
1082  q->setError( ERR_USER_CANCELED );
1083  q->emitResult();
1084  return;
1085  case R_AUTO_RENAME:
1086  m_bAutoRenameDirs = true;
1087  // fall through
1088  case R_RENAME:
1089  {
1090  QString oldPath = (*it).uDest.path( KUrl::AddTrailingSlash );
1091  KUrl newUrl( (*it).uDest );
1092  newUrl.setPath( newPath );
1093  emit q->renamed( q, (*it).uDest, newUrl ); // for e.g. kpropsdlg
1094 
1095  // Change the current one and strip the trailing '/'
1096  (*it).uDest.setPath( newUrl.path( KUrl::RemoveTrailingSlash ) );
1097  newPath = newUrl.path( KUrl::AddTrailingSlash ); // With trailing slash
1098  QList<CopyInfo>::Iterator renamedirit = it;
1099  ++renamedirit;
1100  // Change the name of subdirectories inside the directory
1101  for( ; renamedirit != dirs.end() ; ++renamedirit )
1102  {
1103  QString path = (*renamedirit).uDest.path();
1104  if ( path.startsWith( oldPath ) ) {
1105  QString n = path;
1106  n.replace( 0, oldPath.length(), newPath );
1107  kDebug(7007) << "dirs list:" << (*renamedirit).uSource.path()
1108  << "was going to be" << path
1109  << ", changed into" << n;
1110  (*renamedirit).uDest.setPath( n );
1111  }
1112  }
1113  // Change filenames inside the directory
1114  QList<CopyInfo>::Iterator renamefileit = files.begin();
1115  for( ; renamefileit != files.end() ; ++renamefileit )
1116  {
1117  QString path = (*renamefileit).uDest.path();
1118  if ( path.startsWith( oldPath ) ) {
1119  QString n = path;
1120  n.replace( 0, oldPath.length(), newPath );
1121  kDebug(7007) << "files list:" << (*renamefileit).uSource.path()
1122  << "was going to be" << path
1123  << ", changed into" << n;
1124  (*renamefileit).uDest.setPath( n );
1125  }
1126  }
1127  if (!dirs.isEmpty())
1128  emit q->aboutToCreate( q, dirs );
1129  if (!files.isEmpty())
1130  emit q->aboutToCreate( q, files );
1131  }
1132  break;
1133  case R_AUTO_SKIP:
1134  m_bAutoSkipDirs = true;
1135  // fall through
1136  case R_SKIP:
1137  m_skipList.append( existingDest );
1138  skip((*it).uSource, true);
1139  // Move on to next dir
1140  dirs.erase( it );
1141  m_processedDirs++;
1142  break;
1143  case R_OVERWRITE:
1144  m_overwriteList.insert( existingDest );
1145  emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true /* directory */, false /* renamed */ );
1146  // Move on to next dir
1147  dirs.erase( it );
1148  m_processedDirs++;
1149  break;
1150  case R_OVERWRITE_ALL:
1151  m_bOverwriteAllDirs = true;
1152  emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true /* directory */, false /* renamed */ );
1153  // Move on to next dir
1154  dirs.erase( it );
1155  m_processedDirs++;
1156  break;
1157  default:
1158  assert( 0 );
1159  }
1160  state = STATE_CREATING_DIRS;
1161  //emit processedAmount( this, KJob::Directories, m_processedDirs );
1162  createNextDir();
1163 }
1164 
1165 void CopyJobPrivate::createNextDir()
1166 {
1167  Q_Q(CopyJob);
1168  KUrl udir;
1169  if ( !dirs.isEmpty() )
1170  {
1171  // Take first dir to create out of list
1172  QList<CopyInfo>::Iterator it = dirs.begin();
1173  // Is this URL on the skip list or the overwrite list ?
1174  while( it != dirs.end() && udir.isEmpty() )
1175  {
1176  const QString dir = (*it).uDest.path();
1177  if ( shouldSkip( dir ) ) {
1178  dirs.erase( it );
1179  it = dirs.begin();
1180  } else
1181  udir = (*it).uDest;
1182  }
1183  }
1184  if ( !udir.isEmpty() ) // any dir to create, finally ?
1185  {
1186  // Create the directory - with default permissions so that we can put files into it
1187  // TODO : change permissions once all is finished; but for stuff coming from CDROM it sucks...
1188  KIO::SimpleJob *newjob = KIO::mkdir( udir, -1 );
1189  Scheduler::setJobPriority(newjob, 1);
1190  if (shouldOverwriteFile(udir.path())) { // if we are overwriting an existing file or symlink
1191  newjob->addMetaData("overwrite", "true");
1192  }
1193 
1194  m_currentDestURL = udir;
1195  m_bURLDirty = true;
1196 
1197  q->addSubjob(newjob);
1198  return;
1199  }
1200  else // we have finished creating dirs
1201  {
1202  q->setProcessedAmount( KJob::Directories, m_processedDirs ); // make sure final number appears
1203 
1204  if (m_mode == CopyJob::Move) {
1205  // Now we know which dirs hold the files we're going to delete.
1206  // To speed things up and prevent double-notification, we disable KDirWatch
1207  // on those dirs temporarily (using KDirWatch::self, that's the instanced
1208  // used by e.g. kdirlister).
1209  for ( QSet<QString>::const_iterator it = m_parentDirs.constBegin() ; it != m_parentDirs.constEnd() ; ++it )
1210  KDirWatch::self()->stopDirScan( *it );
1211  }
1212 
1213  state = STATE_COPYING_FILES;
1214  m_processedFiles++; // Ralf wants it to start at 1, not 0
1215  copyNextFile();
1216  }
1217 }
1218 
1219 void CopyJobPrivate::slotResultCopyingFiles( KJob * job )
1220 {
1221  Q_Q(CopyJob);
1222  // The file we were trying to copy:
1223  QList<CopyInfo>::Iterator it = files.begin();
1224  if ( job->error() )
1225  {
1226  // Should we skip automatically ?
1227  if ( m_bAutoSkipFiles )
1228  {
1229  skip((*it).uSource, false);
1230  m_fileProcessedSize = (*it).size;
1231  files.erase( it ); // Move on to next file
1232  }
1233  else
1234  {
1235  m_conflictError = job->error(); // save for later
1236  // Existing dest ?
1237  if ( ( m_conflictError == ERR_FILE_ALREADY_EXIST )
1238  || ( m_conflictError == ERR_DIR_ALREADY_EXIST )
1239  || ( m_conflictError == ERR_IDENTICAL_FILES ) )
1240  {
1241  if (m_bAutoRenameFiles) {
1242  KUrl destDirectory((*it).uDest);
1243  destDirectory.setPath(destDirectory.directory());
1244  const QString newName = KIO::RenameDialog::suggestName(destDirectory, (*it).uDest.fileName());
1245 
1246  KUrl newUrl((*it).uDest);
1247  newUrl.setFileName(newName);
1248 
1249  emit q->renamed(q, (*it).uDest, newUrl); // for e.g. kpropsdlg
1250  (*it).uDest = newUrl;
1251 
1252  QList<CopyInfo> files;
1253  files.append(*it);
1254  emit q->aboutToCreate(q, files);
1255  }
1256  else {
1257  if ( !q->isInteractive() ) {
1258  q->Job::slotResult( job ); // will set the error and emit result(this)
1259  return;
1260  }
1261 
1262  q->removeSubjob(job);
1263  assert (!q->hasSubjobs());
1264  // We need to stat the existing file, to get its last-modification time
1265  KUrl existingFile((*it).uDest);
1266  SimpleJob * newJob = KIO::stat(existingFile, StatJob::DestinationSide, 2, KIO::HideProgressInfo);
1267  Scheduler::setJobPriority(newJob, 1);
1268  kDebug(7007) << "KIO::stat for resolving conflict on " << existingFile;
1269  state = STATE_CONFLICT_COPYING_FILES;
1270  q->addSubjob(newJob);
1271  return; // Don't move to next file yet !
1272  }
1273  }
1274  else
1275  {
1276  if ( m_bCurrentOperationIsLink && qobject_cast<KIO::DeleteJob*>( job ) )
1277  {
1278  // Very special case, see a few lines below
1279  // We are deleting the source of a symlink we successfully moved... ignore error
1280  m_fileProcessedSize = (*it).size;
1281  files.erase( it );
1282  } else {
1283  if ( !q->isInteractive() ) {
1284  q->Job::slotResult( job ); // will set the error and emit result(this)
1285  return;
1286  }
1287 
1288  // Go directly to the conflict resolution, there is nothing to stat
1289  slotResultConflictCopyingFiles( job );
1290  return;
1291  }
1292  }
1293  }
1294  } else // no error
1295  {
1296  // Special case for moving links. That operation needs two jobs, unlike others.
1297  if ( m_bCurrentOperationIsLink && m_mode == CopyJob::Move
1298  && !qobject_cast<KIO::DeleteJob *>( job ) // Deleting source not already done
1299  )
1300  {
1301  q->removeSubjob( job );
1302  assert ( !q->hasSubjobs() );
1303  // The only problem with this trick is that the error handling for this del operation
1304  // is not going to be right... see 'Very special case' above.
1305  KIO::Job * newjob = KIO::del( (*it).uSource, HideProgressInfo );
1306  q->addSubjob( newjob );
1307  return; // Don't move to next file yet !
1308  }
1309 
1310  if ( m_bCurrentOperationIsLink )
1311  {
1312  QString target = ( m_mode == CopyJob::Link ? (*it).uSource.path() : (*it).linkDest );
1313  //required for the undo feature
1314  emit q->copyingLinkDone( q, (*it).uSource, target, (*it).uDest );
1315  }
1316  else {
1317  //required for the undo feature
1318  emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, false, false );
1319  if (m_mode == CopyJob::Move)
1320  {
1321  org::kde::KDirNotify::emitFileMoved( (*it).uSource.url(), (*it).uDest.url() );
1322  }
1323  m_successSrcList.append((*it).uSource);
1324  if (m_freeSpace != (KIO::filesize_t)-1 && (*it).size != (KIO::filesize_t)-1) {
1325  m_freeSpace -= (*it).size;
1326  }
1327 
1328  }
1329  // remove from list, to move on to next file
1330  files.erase( it );
1331  }
1332  m_processedFiles++;
1333 
1334  // clear processed size for last file and add it to overall processed size
1335  m_processedSize += m_fileProcessedSize;
1336  m_fileProcessedSize = 0;
1337 
1338  //kDebug(7007) << files.count() << "files remaining";
1339 
1340  // Merge metadata from subjob
1341  KIO::Job* kiojob = dynamic_cast<KIO::Job*>(job);
1342  Q_ASSERT(kiojob);
1343  m_incomingMetaData += kiojob->metaData();
1344  q->removeSubjob( job );
1345  assert( !q->hasSubjobs() ); // We should have only one job at a time ...
1346  copyNextFile();
1347 }
1348 
1349 void CopyJobPrivate::slotResultConflictCopyingFiles( KJob * job )
1350 {
1351  Q_Q(CopyJob);
1352  // We come here after a conflict has been detected and we've stated the existing file
1353  // The file we were trying to create:
1354  QList<CopyInfo>::Iterator it = files.begin();
1355 
1356  RenameDialog_Result res;
1357  QString newPath;
1358 
1359  if (m_reportTimer)
1360  m_reportTimer->stop();
1361 
1362  if ( ( m_conflictError == ERR_FILE_ALREADY_EXIST )
1363  || ( m_conflictError == ERR_DIR_ALREADY_EXIST )
1364  || ( m_conflictError == ERR_IDENTICAL_FILES ) )
1365  {
1366  // Its modification time:
1367  const UDSEntry entry = ((KIO::StatJob*)job)->statResult();
1368 
1369  const time_t destmtime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_MODIFICATION_TIME, -1 );
1370  const time_t destctime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_CREATION_TIME, -1 );
1371  const KIO::filesize_t destsize = entry.numberValue( KIO::UDSEntry::UDS_SIZE );
1372  const QString linkDest = entry.stringValue( KIO::UDSEntry::UDS_LINK_DEST );
1373 
1374  // Offer overwrite only if the existing thing is a file
1375  // If src==dest, use "overwrite-itself"
1376  RenameDialog_Mode mode;
1377  bool isDir = true;
1378 
1379  if( m_conflictError == ERR_DIR_ALREADY_EXIST )
1380  mode = M_ISDIR;
1381  else
1382  {
1383  if ( (*it).uSource == (*it).uDest ||
1384  ((*it).uSource.protocol() == (*it).uDest.protocol() &&
1385  (*it).uSource.path( KUrl::RemoveTrailingSlash ) == linkDest) )
1386  mode = M_OVERWRITE_ITSELF;
1387  else
1388  mode = M_OVERWRITE;
1389  isDir = false;
1390  }
1391 
1392  if ( !m_bSingleFileCopy )
1393  mode = (RenameDialog_Mode) ( mode | M_MULTI | M_SKIP );
1394 
1395  res = q->ui()->askFileRename( q, !isDir ?
1396  i18n("File Already Exists") : i18n("Already Exists as Folder"),
1397  (*it).uSource.url(),
1398  (*it).uDest.url(),
1399  mode, newPath,
1400  (*it).size, destsize,
1401  (*it).ctime, destctime,
1402  (*it).mtime, destmtime );
1403 
1404  }
1405  else
1406  {
1407  if ( job->error() == ERR_USER_CANCELED )
1408  res = R_CANCEL;
1409  else if ( !q->isInteractive() ) {
1410  q->Job::slotResult( job ); // will set the error and emit result(this)
1411  return;
1412  }
1413  else
1414  {
1415  SkipDialog_Result skipResult = q->ui()->askSkip( q, files.count() > 1,
1416  job->errorString() );
1417 
1418  // Convert the return code from SkipDialog into a RenameDialog code
1419  res = ( skipResult == S_SKIP ) ? R_SKIP :
1420  ( skipResult == S_AUTO_SKIP ) ? R_AUTO_SKIP :
1421  R_CANCEL;
1422  }
1423  }
1424 
1425  if (m_reportTimer)
1426  m_reportTimer->start(REPORT_TIMEOUT);
1427 
1428  q->removeSubjob( job );
1429  assert ( !q->hasSubjobs() );
1430  switch ( res ) {
1431  case R_CANCEL:
1432  q->setError( ERR_USER_CANCELED );
1433  q->emitResult();
1434  return;
1435  case R_AUTO_RENAME:
1436  m_bAutoRenameFiles = true;
1437  // fall through
1438  case R_RENAME:
1439  {
1440  KUrl newUrl( (*it).uDest );
1441  newUrl.setPath( newPath );
1442  emit q->renamed( q, (*it).uDest, newUrl ); // for e.g. kpropsdlg
1443  (*it).uDest = newUrl;
1444 
1445  QList<CopyInfo> files;
1446  files.append(*it);
1447  emit q->aboutToCreate( q, files );
1448  }
1449  break;
1450  case R_AUTO_SKIP:
1451  m_bAutoSkipFiles = true;
1452  // fall through
1453  case R_SKIP:
1454  // Move on to next file
1455  skip((*it).uSource, false);
1456  m_processedSize += (*it).size;
1457  files.erase( it );
1458  m_processedFiles++;
1459  break;
1460  case R_OVERWRITE_ALL:
1461  m_bOverwriteAllFiles = true;
1462  break;
1463  case R_OVERWRITE:
1464  // Add to overwrite list, so that copyNextFile knows to overwrite
1465  m_overwriteList.insert( (*it).uDest.path() );
1466  break;
1467  default:
1468  assert( 0 );
1469  }
1470  state = STATE_COPYING_FILES;
1471  copyNextFile();
1472 }
1473 
1474 KIO::Job* CopyJobPrivate::linkNextFile( const KUrl& uSource, const KUrl& uDest, JobFlags flags )
1475 {
1476  //kDebug(7007) << "Linking";
1477  if (
1478  (uSource.protocol() == uDest.protocol()) &&
1479  (uSource.host() == uDest.host()) &&
1480  (uSource.port() == uDest.port()) &&
1481  (uSource.user() == uDest.user()) &&
1482  (uSource.pass() == uDest.pass()) )
1483  {
1484  // This is the case of creating a real symlink
1485  KIO::SimpleJob *newJob = KIO::symlink( uSource.path(), uDest, flags|HideProgressInfo /*no GUI*/ );
1486  Scheduler::setJobPriority(newJob, 1);
1487  //kDebug(7007) << "Linking target=" << uSource.path() << "link=" << uDest;
1488  //emit linking( this, uSource.path(), uDest );
1489  m_bCurrentOperationIsLink = true;
1490  m_currentSrcURL=uSource;
1491  m_currentDestURL=uDest;
1492  m_bURLDirty = true;
1493  //Observer::self()->slotCopying( this, uSource, uDest ); // should be slotLinking perhaps
1494  return newJob;
1495  } else {
1496  Q_Q(CopyJob);
1497  //kDebug(7007) << "Linking URL=" << uSource << "link=" << uDest;
1498  if ( uDest.isLocalFile() ) {
1499  // if the source is a devices url, handle it a littlebit special
1500 
1501  QString path = uDest.toLocalFile();
1502  //kDebug(7007) << "path=" << path;
1503  QFile f( path );
1504  if ( f.open( QIODevice::ReadWrite ) )
1505  {
1506  f.close();
1507  KDesktopFile desktopFile( path );
1508  KConfigGroup config = desktopFile.desktopGroup();
1509  KUrl url = uSource;
1510  url.setPass( "" );
1511  config.writePathEntry( "URL", url.url() );
1512  config.writeEntry( "Name", url.url() );
1513  config.writeEntry( "Type", QString::fromLatin1("Link") );
1514  QString protocol = uSource.protocol();
1515  if ( protocol == QLatin1String("ftp") )
1516  config.writeEntry( "Icon", QString::fromLatin1("folder-remote") );
1517  else if ( protocol == QLatin1String("http") )
1518  config.writeEntry( "Icon", QString::fromLatin1("text-html") );
1519  else if ( protocol == QLatin1String("info") )
1520  config.writeEntry( "Icon", QString::fromLatin1("text-x-texinfo") );
1521  else if ( protocol == QLatin1String("mailto") ) // sven:
1522  config.writeEntry( "Icon", QString::fromLatin1("internet-mail") ); // added mailto: support
1523  else
1524  config.writeEntry( "Icon", QString::fromLatin1("unknown") );
1525  config.sync();
1526  files.erase( files.begin() ); // done with this one, move on
1527  m_processedFiles++;
1528  //emit processedAmount( this, KJob::Files, m_processedFiles );
1529  copyNextFile();
1530  return 0;
1531  }
1532  else
1533  {
1534  kDebug(7007) << "ERR_CANNOT_OPEN_FOR_WRITING";
1535  q->setError( ERR_CANNOT_OPEN_FOR_WRITING );
1536  q->setErrorText( uDest.toLocalFile() );
1537  q->emitResult();
1538  return 0;
1539  }
1540  } else {
1541  // Todo: not show "link" on remote dirs if the src urls are not from the same protocol+host+...
1542  q->setError( ERR_CANNOT_SYMLINK );
1543  q->setErrorText( uDest.prettyUrl() );
1544  q->emitResult();
1545  return 0;
1546  }
1547  }
1548 }
1549 
1550 void CopyJobPrivate::copyNextFile()
1551 {
1552  Q_Q(CopyJob);
1553  bool bCopyFile = false;
1554  //kDebug(7007);
1555  // Take the first file in the list
1556  QList<CopyInfo>::Iterator it = files.begin();
1557  // Is this URL on the skip list ?
1558  while (it != files.end() && !bCopyFile)
1559  {
1560  const QString destFile = (*it).uDest.path();
1561  bCopyFile = !shouldSkip( destFile );
1562  if ( !bCopyFile ) {
1563  files.erase( it );
1564  it = files.begin();
1565  }
1566  }
1567 
1568  if (bCopyFile) // any file to create, finally ?
1569  {
1570  //kDebug()<<"preparing to copy"<<(*it).uSource<<(*it).size<<m_freeSpace;
1571  if (m_freeSpace != (KIO::filesize_t)-1 && (*it).size != (KIO::filesize_t)-1) {
1572  if (m_freeSpace < (*it).size) {
1573  q->setError( ERR_DISK_FULL );
1574  q->emitResult();
1575  return;
1576  }
1577  //TODO check if dst mount is msdos and (*it).size exceeds it's limits
1578  }
1579 
1580  const KUrl& uSource = (*it).uSource;
1581  const KUrl& uDest = (*it).uDest;
1582  // Do we set overwrite ?
1583  bool bOverwrite;
1584  const QString destFile = uDest.path();
1585  // kDebug(7007) << "copying" << destFile;
1586  if ( uDest == uSource )
1587  bOverwrite = false;
1588  else
1589  bOverwrite = shouldOverwriteFile( destFile );
1590 
1591  m_bCurrentOperationIsLink = false;
1592  KIO::Job * newjob = 0;
1593  if ( m_mode == CopyJob::Link ) {
1594  // User requested that a symlink be made
1595  const JobFlags flags = bOverwrite ? Overwrite : DefaultFlags;
1596  newjob = linkNextFile(uSource, uDest, flags);
1597  if (!newjob)
1598  return;
1599  } else if ( !(*it).linkDest.isEmpty() &&
1600  (uSource.protocol() == uDest.protocol()) &&
1601  (uSource.host() == uDest.host()) &&
1602  (uSource.port() == uDest.port()) &&
1603  (uSource.user() == uDest.user()) &&
1604  (uSource.pass() == uDest.pass()))
1605  // Copying a symlink - only on the same protocol/host/etc. (#5601, downloading an FTP file through its link),
1606  {
1607  const JobFlags flags = bOverwrite ? Overwrite : DefaultFlags;
1608  KIO::SimpleJob *newJob = KIO::symlink( (*it).linkDest, uDest, flags | HideProgressInfo /*no GUI*/ );
1609  Scheduler::setJobPriority(newJob, 1);
1610  newjob = newJob;
1611  //kDebug(7007) << "Linking target=" << (*it).linkDest << "link=" << uDest;
1612  m_currentSrcURL = KUrl( (*it).linkDest );
1613  m_currentDestURL = uDest;
1614  m_bURLDirty = true;
1615  //emit linking( this, (*it).linkDest, uDest );
1616  //Observer::self()->slotCopying( this, m_currentSrcURL, uDest ); // should be slotLinking perhaps
1617  m_bCurrentOperationIsLink = true;
1618  // NOTE: if we are moving stuff, the deletion of the source will be done in slotResultCopyingFiles
1619  } else if (m_mode == CopyJob::Move) // Moving a file
1620  {
1621  JobFlags flags = bOverwrite ? Overwrite : DefaultFlags;
1622  KIO::FileCopyJob * moveJob = KIO::file_move( uSource, uDest, (*it).permissions, flags | HideProgressInfo/*no GUI*/ );
1623  moveJob->setSourceSize( (*it).size );
1624  if ((*it).mtime != -1) {
1625  moveJob->setModificationTime( QDateTime::fromTime_t( (*it).mtime ) ); // #55804
1626  }
1627  newjob = moveJob;
1628  //kDebug(7007) << "Moving" << uSource << "to" << uDest;
1629  //emit moving( this, uSource, uDest );
1630  m_currentSrcURL=uSource;
1631  m_currentDestURL=uDest;
1632  m_bURLDirty = true;
1633  //Observer::self()->slotMoving( this, uSource, uDest );
1634  }
1635  else // Copying a file
1636  {
1637  // If source isn't local and target is local, we ignore the original permissions
1638  // Otherwise, files downloaded from HTTP end up with -r--r--r--
1639  bool remoteSource = !KProtocolManager::supportsListing(uSource);
1640  int permissions = (*it).permissions;
1641  if ( m_defaultPermissions || ( remoteSource && uDest.isLocalFile() ) )
1642  permissions = -1;
1643  JobFlags flags = bOverwrite ? Overwrite : DefaultFlags;
1644  KIO::FileCopyJob * copyJob = KIO::file_copy( uSource, uDest, permissions, flags | HideProgressInfo/*no GUI*/ );
1645  copyJob->setParentJob( q ); // in case of rename dialog
1646  copyJob->setSourceSize( (*it).size );
1647  if ((*it).mtime != -1) {
1648  copyJob->setModificationTime( QDateTime::fromTime_t( (*it).mtime ) );
1649  }
1650  newjob = copyJob;
1651  //kDebug(7007) << "Copying" << uSource << "to" << uDest;
1652  m_currentSrcURL=uSource;
1653  m_currentDestURL=uDest;
1654  m_bURLDirty = true;
1655  }
1656  q->addSubjob(newjob);
1657  q->connect( newjob, SIGNAL(processedSize(KJob*,qulonglong)),
1658  SLOT(slotProcessedSize(KJob*,qulonglong)) );
1659  q->connect( newjob, SIGNAL(totalSize(KJob*,qulonglong)),
1660  SLOT(slotTotalSize(KJob*,qulonglong)) );
1661  }
1662  else
1663  {
1664  // We're done
1665  //kDebug(7007) << "copyNextFile finished";
1666  deleteNextDir();
1667  }
1668 }
1669 
1670 void CopyJobPrivate::deleteNextDir()
1671 {
1672  Q_Q(CopyJob);
1673  if ( m_mode == CopyJob::Move && !dirsToRemove.isEmpty() ) // some dirs to delete ?
1674  {
1675  state = STATE_DELETING_DIRS;
1676  m_bURLDirty = true;
1677  // Take first dir to delete out of list - last ones first !
1678  KUrl::List::Iterator it = --dirsToRemove.end();
1679  SimpleJob *job = KIO::rmdir( *it );
1680  Scheduler::setJobPriority(job, 1);
1681  dirsToRemove.erase(it);
1682  q->addSubjob( job );
1683  }
1684  else
1685  {
1686  // This step is done, move on
1687  state = STATE_SETTING_DIR_ATTRIBUTES;
1688  m_directoriesCopiedIterator = m_directoriesCopied.constBegin();
1689  setNextDirAttribute();
1690  }
1691 }
1692 
1693 void CopyJobPrivate::setNextDirAttribute()
1694 {
1695  Q_Q(CopyJob);
1696  while (m_directoriesCopiedIterator != m_directoriesCopied.constEnd() &&
1697  (*m_directoriesCopiedIterator).mtime == -1) {
1698  ++m_directoriesCopiedIterator;
1699  }
1700  if ( m_directoriesCopiedIterator != m_directoriesCopied.constEnd() ) {
1701  const KUrl url = (*m_directoriesCopiedIterator).uDest;
1702  const time_t mtime = (*m_directoriesCopiedIterator).mtime;
1703  const QDateTime dt = QDateTime::fromTime_t(mtime);
1704  ++m_directoriesCopiedIterator;
1705 
1706  KIO::SimpleJob *job = KIO::setModificationTime( url, dt );
1707  Scheduler::setJobPriority(job, 1);
1708  q->addSubjob( job );
1709 
1710 
1711 #if 0 // ifdef Q_OS_UNIX
1712  // TODO: can be removed now. Or reintroduced as a fast path for local files
1713  // if launching even more jobs as done above is a performance problem.
1714  //
1715  QLinkedList<CopyInfo>::const_iterator it = m_directoriesCopied.constBegin();
1716  for ( ; it != m_directoriesCopied.constEnd() ; ++it ) {
1717  const KUrl& url = (*it).uDest;
1718  if ( url.isLocalFile() && (*it).mtime != (time_t)-1 ) {
1719  KDE_struct_stat statbuf;
1720  if (KDE::lstat(url.path(), &statbuf) == 0) {
1721  struct utimbuf utbuf;
1722  utbuf.actime = statbuf.st_atime; // access time, unchanged
1723  utbuf.modtime = (*it).mtime; // modification time
1724  utime( path, &utbuf );
1725  }
1726 
1727  }
1728  }
1729  m_directoriesCopied.clear();
1730  // but then we need to jump to the else part below. Maybe with a recursive call?
1731 #endif
1732  } else {
1733  if (m_reportTimer)
1734  m_reportTimer->stop();
1735  --m_processedFiles; // undo the "start at 1" hack
1736  slotReport(); // display final numbers, important if progress dialog stays up
1737 
1738  q->emitResult();
1739  }
1740 }
1741 
1742 void CopyJob::emitResult()
1743 {
1744  Q_D(CopyJob);
1745  // Before we go, tell the world about the changes that were made.
1746  // Even if some error made us abort midway, we might still have done
1747  // part of the job so we better update the views! (#118583)
1748  if (!d->m_bOnlyRenames) {
1749  // If only renaming happened, KDirNotify::FileRenamed was emitted by the rename jobs
1750  KUrl url(d->m_globalDest);
1751  if (d->m_globalDestinationState != DEST_IS_DIR || d->m_asMethod)
1752  url.setPath(url.directory());
1753  //kDebug(7007) << "KDirNotify'ing FilesAdded" << url;
1754  org::kde::KDirNotify::emitFilesAdded( url.url() );
1755 
1756  if (d->m_mode == CopyJob::Move && !d->m_successSrcList.isEmpty()) {
1757  kDebug(7007) << "KDirNotify'ing FilesRemoved" << d->m_successSrcList.toStringList();
1758  org::kde::KDirNotify::emitFilesRemoved(d->m_successSrcList.toStringList());
1759  }
1760  }
1761 
1762  // Re-enable watching on the dirs that held the deleted/moved files
1763  if (d->m_mode == CopyJob::Move) {
1764  for (QSet<QString>::const_iterator it = d->m_parentDirs.constBegin() ; it != d->m_parentDirs.constEnd() ; ++it)
1765  KDirWatch::self()->restartDirScan( *it );
1766  }
1767  Job::emitResult();
1768 }
1769 
1770 void CopyJobPrivate::slotProcessedSize( KJob*, qulonglong data_size )
1771 {
1772  Q_Q(CopyJob);
1773  //kDebug(7007) << data_size;
1774  m_fileProcessedSize = data_size;
1775  q->setProcessedAmount(KJob::Bytes, m_processedSize + m_fileProcessedSize);
1776 
1777  if ( m_processedSize + m_fileProcessedSize > m_totalSize )
1778  {
1779  // Example: download any attachment from bugs.kde.org
1780  m_totalSize = m_processedSize + m_fileProcessedSize;
1781  //kDebug(7007) << "Adjusting m_totalSize to" << m_totalSize;
1782  q->setTotalAmount(KJob::Bytes, m_totalSize); // safety
1783  }
1784  //kDebug(7007) << "emit processedSize" << (unsigned long) (m_processedSize + m_fileProcessedSize);
1785  q->setProcessedAmount(KJob::Bytes, m_processedSize + m_fileProcessedSize);
1786 }
1787 
1788 void CopyJobPrivate::slotTotalSize( KJob*, qulonglong size )
1789 {
1790  Q_Q(CopyJob);
1791  //kDebug(7007) << size;
1792  // Special case for copying a single file
1793  // This is because some protocols don't implement stat properly
1794  // (e.g. HTTP), and don't give us a size in some cases (redirection)
1795  // so we'd rather rely on the size given for the transfer
1796  if ( m_bSingleFileCopy && size != m_totalSize)
1797  {
1798  //kDebug(7007) << "slotTotalSize: updating totalsize to" << size;
1799  m_totalSize = size;
1800  q->setTotalAmount(KJob::Bytes, size);
1801  }
1802 }
1803 
1804 void CopyJobPrivate::slotResultDeletingDirs( KJob * job )
1805 {
1806  Q_Q(CopyJob);
1807  if (job->error()) {
1808  // Couldn't remove directory. Well, perhaps it's not empty
1809  // because the user pressed Skip for a given file in it.
1810  // Let's not display "Could not remove dir ..." for each of those dir !
1811  } else {
1812  m_successSrcList.append(static_cast<KIO::SimpleJob*>(job)->url());
1813  }
1814  q->removeSubjob( job );
1815  assert( !q->hasSubjobs() );
1816  deleteNextDir();
1817 }
1818 
1819 void CopyJobPrivate::slotResultSettingDirAttributes( KJob * job )
1820 {
1821  Q_Q(CopyJob);
1822  if (job->error())
1823  {
1824  // Couldn't set directory attributes. Ignore the error, it can happen
1825  // with inferior file systems like VFAT.
1826  // Let's not display warnings for each dir like "cp -a" does.
1827  }
1828  q->removeSubjob( job );
1829  assert( !q->hasSubjobs() );
1830  setNextDirAttribute();
1831 }
1832 
1833 // We were trying to do a direct renaming, before even stat'ing
1834 void CopyJobPrivate::slotResultRenaming( KJob* job )
1835 {
1836  Q_Q(CopyJob);
1837  int err = job->error();
1838  const QString errText = job->errorText();
1839  // Merge metadata from subjob
1840  KIO::Job* kiojob = dynamic_cast<KIO::Job*>(job);
1841  Q_ASSERT(kiojob);
1842  m_incomingMetaData += kiojob->metaData();
1843  q->removeSubjob( job );
1844  assert ( !q->hasSubjobs() );
1845  // Determine dest again
1846  KUrl dest = m_dest;
1847  if ( destinationState == DEST_IS_DIR && !m_asMethod )
1848  dest.addPath( m_currentSrcURL.fileName() );
1849  if ( err )
1850  {
1851  // Direct renaming didn't work. Try renaming to a temp name,
1852  // this can help e.g. when renaming 'a' to 'A' on a VFAT partition.
1853  // In that case it's the _same_ dir, we don't want to copy+del (data loss!)
1854  if ( m_currentSrcURL.isLocalFile() && m_currentSrcURL.url(KUrl::RemoveTrailingSlash) != dest.url(KUrl::RemoveTrailingSlash) &&
1855  m_currentSrcURL.url(KUrl::RemoveTrailingSlash).toLower() == dest.url(KUrl::RemoveTrailingSlash).toLower() &&
1856  ( err == ERR_FILE_ALREADY_EXIST ||
1857  err == ERR_DIR_ALREADY_EXIST ||
1858  err == ERR_IDENTICAL_FILES ) )
1859  {
1860  kDebug(7007) << "Couldn't rename directly, dest already exists. Detected special case of lower/uppercase renaming in same dir, try with 2 rename calls";
1861  const QString _src( m_currentSrcURL.toLocalFile() );
1862  const QString _dest( dest.toLocalFile() );
1863  const QString _tmpPrefix = m_currentSrcURL.directory(KUrl::ObeyTrailingSlash|KUrl::AppendTrailingSlash);
1864  KTemporaryFile tmpFile;
1865  tmpFile.setPrefix(_tmpPrefix);
1866  const bool openOk = tmpFile.open();
1867  if (!openOk) {
1868  kWarning(7007) << "Couldn't open temp file in" << _tmpPrefix;
1869  } else {
1870  const QString _tmp( tmpFile.fileName() );
1871  tmpFile.close();
1872  tmpFile.remove();
1873  kDebug(7007) << "KTemporaryFile using" << _tmp << "as intermediary";
1874  if (KDE::rename( _src, _tmp ) == 0) {
1875  //kDebug(7007) << "Renaming" << _src << "to" << _tmp << "succeeded";
1876  if (!QFile::exists( _dest ) && KDE::rename(_tmp, _dest) == 0) {
1877  err = 0;
1878  org::kde::KDirNotify::emitFileRenamed(m_currentSrcURL.url(), dest.url());
1879  } else {
1880  kDebug(7007) << "Didn't manage to rename" << _tmp << "to" << _dest << ", reverting";
1881  // Revert back to original name!
1882  if (KDE::rename( _tmp, _src ) != 0) {
1883  kError(7007) << "Couldn't rename" << _tmp << "back to" << _src << '!';
1884  // Severe error, abort
1885  q->Job::slotResult(job); // will set the error and emit result(this)
1886  return;
1887  }
1888  }
1889  } else {
1890  kDebug(7007) << "mv" << _src << _tmp << "failed:" << strerror(errno);
1891  }
1892  }
1893  }
1894  }
1895  if ( err )
1896  {
1897  // This code is similar to CopyJobPrivate::slotResultConflictCopyingFiles
1898  // but here it's about the base src url being moved/renamed
1899  // (m_currentSrcURL) and its dest (m_dest), not about a single file.
1900  // It also means we already stated the dest, here.
1901  // On the other hand we haven't stated the src yet (we skipped doing it
1902  // to save time, since it's not necessary to rename directly!)...
1903 
1904  // Existing dest?
1905  if ( err == ERR_DIR_ALREADY_EXIST ||
1906  err == ERR_FILE_ALREADY_EXIST ||
1907  err == ERR_IDENTICAL_FILES )
1908  {
1909  // Should we skip automatically ?
1910  bool isDir = (err == ERR_DIR_ALREADY_EXIST); // ## technically, isDir means "source is dir", not "dest is dir" #######
1911  if ((isDir && m_bAutoSkipDirs) || (!isDir && m_bAutoSkipFiles)) {
1912  // Move on to next source url
1913  skipSrc(isDir);
1914  return;
1915  } else if ((isDir && m_bOverwriteAllDirs) || (!isDir && m_bOverwriteAllFiles)) {
1916  ; // nothing to do, stat+copy+del will overwrite
1917  } else if ((isDir && m_bAutoRenameDirs) || (!isDir && m_bAutoRenameFiles)) {
1918  KUrl destDirectory(m_currentDestURL); // dest including filename
1919  destDirectory.setPath(destDirectory.directory());
1920  const QString newName = KIO::RenameDialog::suggestName(destDirectory, m_currentDestURL.fileName());
1921 
1922  m_dest.setPath(m_currentDestURL.path());
1923  m_dest.setFileName(newName);
1924  KIO::Job* job = KIO::stat(m_dest, StatJob::DestinationSide, 2, KIO::HideProgressInfo);
1925  state = STATE_STATING;
1926  destinationState = DEST_NOT_STATED;
1927  q->addSubjob(job);
1928  return;
1929  } else if ( q->isInteractive() ) {
1930  QString newPath;
1931  // we lack mtime info for both the src (not stated)
1932  // and the dest (stated but this info wasn't stored)
1933  // Let's do it for local files, at least
1934  KIO::filesize_t sizeSrc = (KIO::filesize_t) -1;
1935  KIO::filesize_t sizeDest = (KIO::filesize_t) -1;
1936  time_t ctimeSrc = (time_t) -1;
1937  time_t ctimeDest = (time_t) -1;
1938  time_t mtimeSrc = (time_t) -1;
1939  time_t mtimeDest = (time_t) -1;
1940 
1941  bool destIsDir = err == ERR_DIR_ALREADY_EXIST;
1942 
1943  // ## TODO we need to stat the source using KIO::stat
1944  // so that this code is properly network-transparent.
1945 
1946  KDE_struct_stat stat_buf;
1947  if ( m_currentSrcURL.isLocalFile() &&
1948  KDE::stat(m_currentSrcURL.toLocalFile(), &stat_buf) == 0 ) {
1949  sizeSrc = stat_buf.st_size;
1950  ctimeSrc = stat_buf.st_ctime;
1951  mtimeSrc = stat_buf.st_mtime;
1952  isDir = S_ISDIR(stat_buf.st_mode);
1953  }
1954  if ( dest.isLocalFile() &&
1955  KDE::stat(dest.toLocalFile(), &stat_buf) == 0 ) {
1956  sizeDest = stat_buf.st_size;
1957  ctimeDest = stat_buf.st_ctime;
1958  mtimeDest = stat_buf.st_mtime;
1959  destIsDir = S_ISDIR(stat_buf.st_mode);
1960  }
1961 
1962  // If src==dest, use "overwrite-itself"
1963  RenameDialog_Mode mode = ( m_currentSrcURL == dest ) ? M_OVERWRITE_ITSELF : M_OVERWRITE;
1964  if (!isDir && destIsDir) {
1965  // We can't overwrite a dir with a file.
1966  mode = (RenameDialog_Mode) 0;
1967  }
1968 
1969  if ( m_srcList.count() > 1 )
1970  mode = (RenameDialog_Mode) ( mode | M_MULTI | M_SKIP );
1971  if (destIsDir)
1972  mode = (RenameDialog_Mode) ( mode | M_ISDIR );
1973 
1974  if (m_reportTimer)
1975  m_reportTimer->stop();
1976 
1977  RenameDialog_Result r = q->ui()->askFileRename(
1978  q,
1979  err != ERR_DIR_ALREADY_EXIST ? i18n("File Already Exists") : i18n("Already Exists as Folder"),
1980  m_currentSrcURL.url(),
1981  dest.url(),
1982  mode, newPath,
1983  sizeSrc, sizeDest,
1984  ctimeSrc, ctimeDest,
1985  mtimeSrc, mtimeDest );
1986 
1987  if (m_reportTimer)
1988  m_reportTimer->start(REPORT_TIMEOUT);
1989 
1990  switch ( r )
1991  {
1992  case R_CANCEL:
1993  {
1994  q->setError( ERR_USER_CANCELED );
1995  q->emitResult();
1996  return;
1997  }
1998  case R_AUTO_RENAME:
1999  if (isDir) {
2000  m_bAutoRenameDirs = true;
2001  }
2002  else {
2003  m_bAutoRenameFiles = true;
2004  }
2005  // fall through
2006  case R_RENAME:
2007  {
2008  // Set m_dest to the chosen destination
2009  // This is only for this src url; the next one will revert to m_globalDest
2010  m_dest.setPath( newPath );
2011  KIO::Job* job = KIO::stat( m_dest, StatJob::DestinationSide, 2, KIO::HideProgressInfo );
2012  state = STATE_STATING;
2013  destinationState = DEST_NOT_STATED;
2014  q->addSubjob(job);
2015  return;
2016  }
2017  case R_AUTO_SKIP:
2018  if (isDir)
2019  m_bAutoSkipDirs = true;
2020  else
2021  m_bAutoSkipFiles = true;
2022  // fall through
2023  case R_SKIP:
2024  // Move on to next url
2025  skipSrc(isDir);
2026  return;
2027  case R_OVERWRITE_ALL:
2028  if (destIsDir)
2029  m_bOverwriteAllDirs = true;
2030  else
2031  m_bOverwriteAllFiles = true;
2032  break;
2033  case R_OVERWRITE:
2034  // Add to overwrite list
2035  // Note that we add dest, not m_dest.
2036  // This ensures that when moving several urls into a dir (m_dest),
2037  // we only overwrite for the current one, not for all.
2038  // When renaming a single file (m_asMethod), it makes no difference.
2039  kDebug(7007) << "adding to overwrite list: " << dest.path();
2040  m_overwriteList.insert( dest.path() );
2041  break;
2042  default:
2043  //assert( 0 );
2044  break;
2045  }
2046  } else if ( err != KIO::ERR_UNSUPPORTED_ACTION ) {
2047  // Dest already exists, and job is not interactive -> abort with error
2048  q->setError( err );
2049  q->setErrorText( errText );
2050  q->emitResult();
2051  return;
2052  }
2053  } else if ( err != KIO::ERR_UNSUPPORTED_ACTION ) {
2054  kDebug(7007) << "Couldn't rename" << m_currentSrcURL << "to" << dest << ", aborting";
2055  q->setError( err );
2056  q->setErrorText( errText );
2057  q->emitResult();
2058  return;
2059  }
2060  kDebug(7007) << "Couldn't rename" << m_currentSrcURL << "to" << dest << ", reverting to normal way, starting with stat";
2061  //kDebug(7007) << "KIO::stat on" << m_currentSrcURL;
2062  KIO::Job* job = KIO::stat( m_currentSrcURL, StatJob::SourceSide, 2, KIO::HideProgressInfo );
2063  state = STATE_STATING;
2064  q->addSubjob(job);
2065  m_bOnlyRenames = false;
2066  }
2067  else
2068  {
2069  kDebug(7007) << "Renaming succeeded, move on";
2070  ++m_processedFiles;
2071  emit q->copyingDone( q, *m_currentStatSrc, dest, -1 /*mtime unknown, and not needed*/, true, true );
2072  m_successSrcList.append(*m_currentStatSrc);
2073  statNextSrc();
2074  }
2075 }
2076 
2077 void CopyJob::slotResult( KJob *job )
2078 {
2079  Q_D(CopyJob);
2080  //kDebug(7007) << "d->state=" << (int) d->state;
2081  // In each case, what we have to do is :
2082  // 1 - check for errors and treat them
2083  // 2 - removeSubjob(job);
2084  // 3 - decide what to do next
2085 
2086  switch ( d->state ) {
2087  case STATE_STATING: // We were trying to stat a src url or the dest
2088  d->slotResultStating( job );
2089  break;
2090  case STATE_RENAMING: // We were trying to do a direct renaming, before even stat'ing
2091  {
2092  d->slotResultRenaming( job );
2093  break;
2094  }
2095  case STATE_LISTING: // recursive listing finished
2096  //kDebug(7007) << "totalSize:" << (unsigned int) d->m_totalSize << "files:" << d->files.count() << "d->dirs:" << d->dirs.count();
2097  // Was there an error ?
2098  if (job->error())
2099  {
2100  Job::slotResult( job ); // will set the error and emit result(this)
2101  return;
2102  }
2103 
2104  removeSubjob( job );
2105  assert ( !hasSubjobs() );
2106 
2107  d->statNextSrc();
2108  break;
2109  case STATE_CREATING_DIRS:
2110  d->slotResultCreatingDirs( job );
2111  break;
2112  case STATE_CONFLICT_CREATING_DIRS:
2113  d->slotResultConflictCreatingDirs( job );
2114  break;
2115  case STATE_COPYING_FILES:
2116  d->slotResultCopyingFiles( job );
2117  break;
2118  case STATE_CONFLICT_COPYING_FILES:
2119  d->slotResultConflictCopyingFiles( job );
2120  break;
2121  case STATE_DELETING_DIRS:
2122  d->slotResultDeletingDirs( job );
2123  break;
2124  case STATE_SETTING_DIR_ATTRIBUTES:
2125  d->slotResultSettingDirAttributes( job );
2126  break;
2127  default:
2128  assert( 0 );
2129  }
2130 }
2131 
2132 void KIO::CopyJob::setDefaultPermissions( bool b )
2133 {
2134  d_func()->m_defaultPermissions = b;
2135 }
2136 
2137 KIO::CopyJob::CopyMode KIO::CopyJob::operationMode() const
2138 {
2139  return d_func()->m_mode;
2140 }
2141 
2142 void KIO::CopyJob::setAutoSkip(bool autoSkip)
2143 {
2144  d_func()->m_bAutoSkipFiles = autoSkip;
2145  d_func()->m_bAutoSkipDirs = autoSkip;
2146 }
2147 
2148 void KIO::CopyJob::setAutoRename(bool autoRename)
2149 {
2150  d_func()->m_bAutoRenameFiles = autoRename;
2151  d_func()->m_bAutoRenameDirs = autoRename;
2152 }
2153 
2154 void KIO::CopyJob::setWriteIntoExistingDirectories(bool overwriteAll) // #65926
2155 {
2156  d_func()->m_bOverwriteAllDirs = overwriteAll;
2157 }
2158 
2159 CopyJob *KIO::copy(const KUrl& src, const KUrl& dest, JobFlags flags)
2160 {
2161  //kDebug(7007) << "src=" << src << "dest=" << dest;
2162  KUrl::List srcList;
2163  srcList.append( src );
2164  return CopyJobPrivate::newJob(srcList, dest, CopyJob::Copy, false, flags);
2165 }
2166 
2167 CopyJob *KIO::copyAs(const KUrl& src, const KUrl& dest, JobFlags flags)
2168 {
2169  //kDebug(7007) << "src=" << src << "dest=" << dest;
2170  KUrl::List srcList;
2171  srcList.append( src );
2172  return CopyJobPrivate::newJob(srcList, dest, CopyJob::Copy, true, flags);
2173 }
2174 
2175 CopyJob *KIO::copy( const KUrl::List& src, const KUrl& dest, JobFlags flags )
2176 {
2177  //kDebug(7007) << src << dest;
2178  return CopyJobPrivate::newJob(src, dest, CopyJob::Copy, false, flags);
2179 }
2180 
2181 CopyJob *KIO::move(const KUrl& src, const KUrl& dest, JobFlags flags)
2182 {
2183  //kDebug(7007) << src << dest;
2184  KUrl::List srcList;
2185  srcList.append( src );
2186  return CopyJobPrivate::newJob(srcList, dest, CopyJob::Move, false, flags);
2187 }
2188 
2189 CopyJob *KIO::moveAs(const KUrl& src, const KUrl& dest, JobFlags flags)
2190 {
2191  //kDebug(7007) << src << dest;
2192  KUrl::List srcList;
2193  srcList.append( src );
2194  return CopyJobPrivate::newJob(srcList, dest, CopyJob::Move, true, flags);
2195 }
2196 
2197 CopyJob *KIO::move( const KUrl::List& src, const KUrl& dest, JobFlags flags)
2198 {
2199  //kDebug(7007) << src << dest;
2200  return CopyJobPrivate::newJob(src, dest, CopyJob::Move, false, flags);
2201 }
2202 
2203 CopyJob *KIO::link(const KUrl& src, const KUrl& destDir, JobFlags flags)
2204 {
2205  KUrl::List srcList;
2206  srcList.append( src );
2207  return CopyJobPrivate::newJob(srcList, destDir, CopyJob::Link, false, flags);
2208 }
2209 
2210 CopyJob *KIO::link(const KUrl::List& srcList, const KUrl& destDir, JobFlags flags)
2211 {
2212  return CopyJobPrivate::newJob(srcList, destDir, CopyJob::Link, false, flags);
2213 }
2214 
2215 CopyJob *KIO::linkAs(const KUrl& src, const KUrl& destDir, JobFlags flags )
2216 {
2217  KUrl::List srcList;
2218  srcList.append( src );
2219  return CopyJobPrivate::newJob(srcList, destDir, CopyJob::Link, false, flags);
2220 }
2221 
2222 CopyJob *KIO::trash(const KUrl& src, JobFlags flags)
2223 {
2224  KUrl::List srcList;
2225  srcList.append( src );
2226  return CopyJobPrivate::newJob(srcList, KUrl( "trash:/" ), CopyJob::Move, false, flags);
2227 }
2228 
2229 CopyJob *KIO::trash(const KUrl::List& srcList, JobFlags flags)
2230 {
2231  return CopyJobPrivate::newJob(srcList, KUrl( "trash:/" ), CopyJob::Move, false, flags);
2232 }
2233 
2234 #include "copyjob.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Apr 20 2013 06:03:08 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.10.2 API Reference

Skip menu "kdelibs-4.10.2 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal