Implement class for handling filesystem paths

PR #15915.
This commit is contained in:
Vladimir Golovnev 2022-02-08 06:03:48 +03:00 committed by GitHub
parent facfa26eed
commit dd1bd8ad10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
131 changed files with 2252 additions and 1868 deletions

View file

@ -44,7 +44,9 @@ namespace
const std::chrono::seconds FLUSH_INTERVAL {2};
}
FileLogger::FileLogger(const QString &path, const bool backup, const int maxSize, const bool deleteOld, const int age, const FileLogAgeType ageType)
FileLogger::FileLogger(const Path &path, const bool backup
, const int maxSize, const bool deleteOld, const int age
, const FileLogAgeType ageType)
: m_backup(backup)
, m_maxSize(maxSize)
{
@ -68,26 +70,25 @@ FileLogger::~FileLogger()
closeLogFile();
}
void FileLogger::changePath(const QString &newPath)
void FileLogger::changePath(const Path &newPath)
{
const QDir dir(newPath);
dir.mkpath(newPath);
const QString tmpPath = dir.absoluteFilePath("qbittorrent.log");
// compare paths as strings to perform case sensitive comparison on all the platforms
if (newPath.data() == m_path.parentPath().data())
return;
if (tmpPath != m_path)
{
m_path = tmpPath;
closeLogFile();
closeLogFile();
m_logFile.setFileName(m_path);
openLogFile();
}
m_path = newPath / Path("qbittorrent.log");
m_logFile.setFileName(m_path.data());
Utils::Fs::mkpath(newPath);
openLogFile();
}
void FileLogger::deleteOld(const int age, const FileLogAgeType ageType)
{
const QDateTime date = QDateTime::currentDateTime();
const QDir dir(Utils::Fs::branchPath(m_path));
const QDir dir {m_path.parentPath().data()};
const QFileInfoList fileList = dir.entryInfoList(QStringList("qbittorrent.log.bak*")
, (QDir::Files | QDir::Writable), (QDir::Time | QDir::Reversed));
@ -107,7 +108,7 @@ void FileLogger::deleteOld(const int age, const FileLogAgeType ageType)
}
if (modificationDate > date)
break;
Utils::Fs::forceRemove(file.absoluteFilePath());
Utils::Fs::removeFile(Path(file.absoluteFilePath()));
}
}
@ -151,15 +152,15 @@ void FileLogger::addLogMessage(const Log::Msg &msg)
{
closeLogFile();
int counter = 0;
QString backupLogFilename = m_path + ".bak";
Path backupLogFilename = m_path + ".bak";
while (QFile::exists(backupLogFilename))
while (backupLogFilename.exists())
{
++counter;
backupLogFilename = m_path + ".bak" + QString::number(counter);
}
QFile::rename(m_path, backupLogFilename);
Utils::Fs::renameFile(m_path, backupLogFilename);
openLogFile();
}
else