Skip to content

Commit

Permalink
Merge branch 'develop' for release v5.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
epoupon committed Nov 20, 2021
2 parents 7ac35b5 + d065d63 commit a1a6914
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/fileshelter/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ int main(int argc, char *argv[])
const std::string deployPath {Service<IConfig>::get()->getString("deploy-path", "/")};

Service<Share::IShareManager> shareManager {Share::createShareManager(Service<IConfig>::get()->getPath("working-dir") / "fileshelter.db", true /* enableCleaner */)};
shareManager->removeOrphanFiles(uploadedFilesPath);

ShareResource shareResource;
server.addResource(&shareResource, std::string {shareResource.getDeployPath()});
Expand Down
6 changes: 6 additions & 0 deletions src/libs/share/impl/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ namespace Share

session.flush();
return res;
}

File::pointer
File::getByPath(Wt::Dbo::Session& session, const std::filesystem::path& filePath)
{
return session.find<File>().where("path = ?").bind(filePath);
}

}

1 change: 1 addition & 0 deletions src/libs/share/impl/File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace Share

// Helpers
static pointer create(Wt::Dbo::Session& session, const FileCreateParameters& parameters, Wt::Dbo::ptr<Share> share);
static pointer getByPath(Wt::Dbo::Session& session, const std::filesystem::path& path);

// Getters
const FileUUID& getUUID() const { return _uuid; }
Expand Down
6 changes: 6 additions & 0 deletions src/libs/share/impl/Share.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ namespace Share
std::error_code ec;
std::filesystem::remove(file->getPath(), ec);
if (ec)
{
FS_LOG(SHARE, ERROR) << "Cannot remove file '" << file->getPath().string() << "' from share '" << share->getUUID().toString() << "': " << ec.message();
}
else
{
FS_LOG(SHARE, DEBUG) << "Removed file '" << file->getPath().string() << "' from share '" << share->getUUID().toString() << "'";
}
}
});

Expand Down
44 changes: 44 additions & 0 deletions src/libs/share/impl/ShareCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
#include "Db.hpp"
#include "File.hpp"
#include "Share.hpp"

namespace Share
Expand All @@ -47,6 +48,44 @@ namespace Share
FS_LOG(SHARE, DEBUG) << "Stopped cleaner";
}

void
ShareCleaner::removeOrphanFiles(const std::filesystem::path& directory)
{
FS_LOG(SHARE, DEBUG) << "Removing orphan files in directory '" << directory.string() << "'";

for (const std::filesystem::path& directoryEntry: std::filesystem::directory_iterator {directory})
{
if (!std::filesystem::is_regular_file(directoryEntry))
{
FS_LOG(SHARE, DEBUG) << "Skipping '" << directoryEntry.string() << "': not regular";
continue;
}

if (isOrphanFile(directoryEntry))
{
std::error_code ec;
std::filesystem::remove(directoryEntry, ec);
if (ec)
{
FS_LOG(SHARE, ERROR) << "Cannot remove file '" << directoryEntry.string() << "'";
}
else
{
FS_LOG(SHARE, INFO) << "Removed orphan file '" << directoryEntry.string() << "'";
}
}
}
}

bool
ShareCleaner::isOrphanFile(const std::filesystem::path& filePath)
{
Wt::Dbo::Session& session {_db.getTLSSession()};
Wt::Dbo::Transaction transaction {session};

return !File::getByPath(session, filePath);
}

void
ShareCleaner::scheduleNextCheck()
{
Expand All @@ -58,6 +97,7 @@ namespace Share
return;

checkExpiredShares();
scheduleNextCheck();
});
}

Expand All @@ -80,6 +120,10 @@ namespace Share
FS_LOG(SHARE, INFO) << "Removing expired share '" << share->getUUID().toString() << "'";
Share::destroy(share);
}
else
{
FS_LOG(SHARE, DEBUG) << "Share '" << share->getUUID().toString() << "' not due to removal";
}
});
}
} // namespace Share
5 changes: 5 additions & 0 deletions src/libs/share/impl/ShareCleaner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
#pragma once

#include <chrono>
#include <filesystem>
#include <boost/asio/steady_timer.hpp>
#include <Wt/WIOService.h>

namespace Share
{
class Db;
class Share;
class ShareCleaner
{
public:
Expand All @@ -37,7 +39,10 @@ namespace Share
ShareCleaner& operator=(const ShareCleaner&) = delete;
ShareCleaner& operator=(ShareCleaner&&) = delete;

void removeOrphanFiles(const std::filesystem::path& directory);

private:
bool isOrphanFile(const std::filesystem::path& file);
void scheduleNextCheck();
void checkExpiredShares();

Expand Down
7 changes: 7 additions & 0 deletions src/libs/share/impl/ShareManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ namespace Share
share.modify()->incReadCount();
}

void
ShareManager::removeOrphanFiles(const std::filesystem::path& directory)
{
if (_shareCleaner)
_shareCleaner->removeOrphanFiles(directory);
}

void
ShareManager::validateShareSizes(const std::vector<FileCreateParameters>& files, const std::vector<FileSize>& fileSizes)
{
Expand Down
1 change: 1 addition & 0 deletions src/libs/share/impl/ShareManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace Share
ShareDesc getShareDesc(const ShareEditUUID& shareUUID) override;
void visitShares(std::function<void(const ShareDesc&)>) override;
void incrementReadCount(const ShareUUID& shareUUID) override;
void removeOrphanFiles(const std::filesystem::path& directory) override;

void validateShareSizes(const std::vector<FileCreateParameters>& files, const std::vector<FileSize>& fileSizes);

Expand Down
1 change: 1 addition & 0 deletions src/libs/share/include/share/IShareManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace Share
virtual void visitShares(std::function<void(const ShareDesc&)>) = 0;

virtual void incrementReadCount(const ShareUUID& shareUUID) = 0;
virtual void removeOrphanFiles(const std::filesystem::path& directory) = 0;
};

std::unique_ptr<IShareManager> createShareManager(const std::filesystem::path& dbFile, bool enableCleaner);
Expand Down

0 comments on commit a1a6914

Please sign in to comment.