From 9c8428ae17083ab75c4b057f524f3456f879249e Mon Sep 17 00:00:00 2001 From: hama Date: Tue, 9 Feb 2016 00:22:25 +0100 Subject: [PATCH] dayly work --- appl/rebackgui/BackupEngine.cpp | 53 +++++++++++++++++++++++---------- appl/rebackgui/mainwindow.cpp | 6 ++-- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/appl/rebackgui/BackupEngine.cpp b/appl/rebackgui/BackupEngine.cpp index dbab03f..5ce4c66 100644 --- a/appl/rebackgui/BackupEngine.cpp +++ b/appl/rebackgui/BackupEngine.cpp @@ -77,14 +77,16 @@ bool BackupEngine::error(const QString& message){ */ void BackupEngine::initializeShadowDir(){ if (m_shadowBaseDir.isEmpty()){ - int fullLength = m_targetBaseDir.length(); - QString node = ReFileUtils::nodeOf(m_targetBaseDir); + QString baseDir = m_targetBaseDir; + ReQStringUtils::chomp(baseDir, OS_SEPARATOR); + int fullLength = baseDir.length(); + QString node = ReFileUtils::nodeOf(baseDir); int nodeLength = node.length(); if (nodeLength <= 2) - m_shadowBaseDir = ReFileUtils::parentOf(m_targetBaseDir) + "$"; + m_shadowBaseDir = ReFileUtils::parentOf(baseDir) + "$"; else { int middle = nodeLength / 2; - m_shadowBaseDir = "." + m_targetBaseDir; + m_shadowBaseDir = ReFileUtils::parentOf(baseDir) + "." + node; m_shadowBaseDir.remove(fullLength - middle, 1); } } @@ -147,7 +149,7 @@ void BackupEngine::removeOlder(const QString& directory, const QDateTime& time, } } else if (it.fileInfo().lastModified() < time){ isEmpty = false; - info = prefix + QChar(CmdRemove) + it.fileName(); + info = prefix + QChar(CmdRemove) + it.filePath(); m_mutex.lock(); m_files.append(info); m_hotFiles++; @@ -157,7 +159,7 @@ void BackupEngine::removeOlder(const QString& directory, const QDateTime& time, } } if (isEmpty){ - info = prefix + QChar(CmdRemoveDir) + it.fileName(); + info = QChar(index + 1) + it.filePath() + "\t" + QChar(CmdRemoveDir); m_mutex.lock(); m_files.append(info); m_matchedFiles++; @@ -492,7 +494,7 @@ CleanTask::CleanTask(const QString& name, const QStringList& sourceDirs, const QString& targetDir, MainWindow* mainWindow) : BackupEngine(name, sourceDirs, targetDir, mainWindow) { - + initializeShadowDir(); } /** @@ -529,20 +531,35 @@ void CleanTask::run() node = info.mid(pos + 2); switch(command){ case CmdRemove: - m_mainWindow->addToFileList("-" + relPath); + if (unlink(I18N::s2b(relPath)) != 0) + error(QObject::tr("cannot delete target (%1): %2").arg(errno) + .arg(relPath)); + else + m_mainWindow->addToFileList("- " + relPath); break; case CmdRemoveDir: - m_mainWindow->addToFileList("/" + relPath); + m_mainWindow->addToFileList("/ " + relPath); break; case CmdMove: { QString target = m_targetDirs.at(index) + relPath + node; QString shadowDir = m_shadowDirs.at(index) + relPath; + ReQStringUtils::chomp(shadowDir, OS_SEPARATOR); QString shadow = shadowDir + node; if (! ReFileUtils::makeDirWithParents(shadowDir)) error(QObject::tr("cannot create the shadow directory: %1") .arg(shadowDir)); - m_mainWindow->addToFileList(">" + target + " -> " + shadow); + QByteArray shadow2 = I18N::s2b(shadow); + struct stat info; + if (stat(shadow2.constData(), &info) == 0){ + if (unlink(shadow2.constData()) != 0){ + error(QObject::tr("cannot delete shadow file (%1): %2").arg(errno).arg(shadow)); + } + } + if (rename(I18N::s2b(target).constData(), shadow2.constData()) != 0) + error(QObject::tr("cannot rename target file (%1): %2 -> %3") + .arg(errno).arg(target).arg(shadow)); + m_mainWindow->addToFileList(">" + target); break; } default: @@ -733,9 +750,9 @@ void SearchTask::searchOneDirectory(const QString& source, SearchTargetTask::SearchTargetTask(const QString& name, const QStringList& sourceDirs, const QString& targetDir, qint64 maxAgeSec, MainWindow* mainWindow) : BackupEngine(name, sourceDirs, targetDir, mainWindow), - m_maxAge() + m_maxAge(QDateTime::currentDateTime()) { - m_maxAge.setMSecsSinceEpoch(maxAgeSec * 1000); + m_maxAge.addSecs(- maxAgeSec); initializeShadowDir(); } @@ -775,11 +792,17 @@ void SearchTargetTask::searchOneDirectory(const QString& target, // nothing to do } else{ Command command = CmdUndef; - QFileInfo src(source + it.fileName()); + QString fullSrc = source + it.fileName(); + QFileInfo src(fullSrc); if (! src.exists()){ const QFileInfo trg = it.fileInfo(); - command = trg.lastModified() < m_maxAge ? CmdRemove : CmdMove; - info = prefix + QChar(command) + it.fileName(); + if (trg.lastModified() < m_maxAge){ + command = CmdRemove; + info = QChar(1 + index) + it.filePath() + "\t" + QChar(command); + } else { + command = CmdMove; + info = prefix + QChar(command) + it.fileName(); + } } m_mutex.lock(); if (command != CmdUndef){ diff --git a/appl/rebackgui/mainwindow.cpp b/appl/rebackgui/mainwindow.cpp index 63970b1..b3bc904 100644 --- a/appl/rebackgui/mainwindow.cpp +++ b/appl/rebackgui/mainwindow.cpp @@ -381,12 +381,12 @@ void MainWindow::onClean() initializeStart(); delete m_searchTargetTask; m_searchTargetTask = new SearchTargetTask(item.m_name, - item.m_sources, target, this); + item.m_sources, target, 14*24*3600, this); m_searchTargetTask->start(); delete m_cleanTask; m_cleanTask = new CleanTask(item.m_name, item.m_sources, target, - this); - m_backupTask->start(); + this); + m_cleanTask->start(); } } } -- 2.39.5