From 3de2a9f486a77a911fab986daabbe50ce7c85b04 Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Mon, 22 Sep 2025 20:09:21 +0300 Subject: [PATCH] Don't create lock file in internal data folders PR #23279. --- src/base/asyncfilestorage.cpp | 43 +++-------------------------------- src/base/asyncfilestorage.h | 12 +--------- 2 files changed, 4 insertions(+), 51 deletions(-) diff --git a/src/base/asyncfilestorage.cpp b/src/base/asyncfilestorage.cpp index ba96aa09f..be3f2a2dc 100644 --- a/src/base/asyncfilestorage.cpp +++ b/src/base/asyncfilestorage.cpp @@ -1,6 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2017-2024 Vladimir Golovnev + * Copyright (C) 2017-2025 Vladimir Golovnev * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -34,53 +34,16 @@ #include "base/utils/fs.h" #include "base/utils/io.h" -QHash> AsyncFileStorage::m_reservedPaths; -QReadWriteLock AsyncFileStorage::m_reservedPathsLock; - AsyncFileStorage::AsyncFileStorage(const Path &storageFolderPath, QObject *parent) : QObject(parent) , m_storageDir(storageFolderPath) { Q_ASSERT(m_storageDir.isAbsolute()); - const Path lockFilePath = m_storageDir / Path(u"storage.lock"_s); - - { - const QReadLocker readLocker {&m_reservedPathsLock}; - m_lockFile = m_reservedPaths.value(lockFilePath).lock(); - } - - if (!m_lockFile) - { - const QWriteLocker writeLocker {&m_reservedPathsLock}; - if (std::weak_ptr &lockFile = m_reservedPaths[lockFilePath]; lockFile.expired()) [[likely]] - { - if (!Utils::Fs::mkpath(m_storageDir)) - throw AsyncFileStorageError(tr("Could not create directory '%1'.").arg(m_storageDir.toString())); - - auto lockFileDeleter = [](QFile *file) - { - file->close(); - file->remove(); - delete file; - }; - m_lockFile = std::shared_ptr(new QFile(lockFilePath.data()), std::move(lockFileDeleter)); - - // TODO: This folder locking approach does not work for UNIX systems. Implement it. - if (!m_lockFile->open(QFile::WriteOnly)) - throw AsyncFileStorageError(m_lockFile->errorString()); - - lockFile = m_lockFile; - } - else - { - m_lockFile = lockFile.lock(); - } - } + if (!Utils::Fs::mkpath(m_storageDir)) + throw AsyncFileStorageError(tr("Could not create directory '%1'.").arg(m_storageDir.toString())); } -AsyncFileStorage::~AsyncFileStorage() = default; - void AsyncFileStorage::store(const Path &filePath, const QByteArray &data) { QMetaObject::invokeMethod(this, [this, data, filePath] { store_impl(filePath, data); }, Qt::QueuedConnection); diff --git a/src/base/asyncfilestorage.h b/src/base/asyncfilestorage.h index 47937de1e..6456e0928 100644 --- a/src/base/asyncfilestorage.h +++ b/src/base/asyncfilestorage.h @@ -1,6 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2017-2024 Vladimir Golovnev + * Copyright (C) 2017-2025 Vladimir Golovnev * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -28,12 +28,7 @@ #pragma once -#include - -#include -#include #include -#include #include "base/exceptions.h" #include "base/path.h" @@ -51,7 +46,6 @@ class AsyncFileStorage final : public QObject public: explicit AsyncFileStorage(const Path &storageFolderPath, QObject *parent = nullptr); - ~AsyncFileStorage() override; void store(const Path &filePath, const QByteArray &data); @@ -64,8 +58,4 @@ private: Q_INVOKABLE void store_impl(const Path &fileName, const QByteArray &data); Path m_storageDir; - std::shared_ptr m_lockFile; - - static QHash> m_reservedPaths; - static QReadWriteLock m_reservedPathsLock; };