2021-10-21 21:29:15 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
2023-10-30 22:58:54 +01:00
|
|
|
* Copyright the Collabora Online contributors.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2023-11-09 19:23:00 +01:00
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2021-10-21 21:29:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-05-14 14:45:34 +02:00
|
|
|
#include <mutex>
|
2021-10-21 21:29:15 +02:00
|
|
|
#include <string>
|
2023-05-11 13:47:33 +02:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2021-10-21 21:29:15 +02:00
|
|
|
|
|
|
|
class DocumentBroker;
|
2023-05-08 13:57:30 +02:00
|
|
|
|
|
|
|
class Quarantine
|
2021-10-21 21:29:15 +02:00
|
|
|
{
|
2023-05-08 13:57:30 +02:00
|
|
|
public:
|
2023-05-13 19:30:41 +02:00
|
|
|
Quarantine(DocumentBroker& docBroker, const std::string& docName);
|
2023-05-08 14:11:01 +02:00
|
|
|
|
2023-05-08 13:57:30 +02:00
|
|
|
static void initialize(const std::string& path);
|
2021-10-21 21:29:15 +02:00
|
|
|
|
2024-01-01 16:00:03 +01:00
|
|
|
bool isEnabled() const { return !QuarantinePath.empty(); }
|
|
|
|
|
2023-07-17 13:29:22 +02:00
|
|
|
/// Quarantines a new version of the document.
|
2023-05-08 14:11:01 +02:00
|
|
|
bool quarantineFile(const std::string& docName);
|
2023-04-22 01:28:23 +02:00
|
|
|
|
2023-07-17 13:29:22 +02:00
|
|
|
/// Returns the last quarantined file's path.
|
|
|
|
std::string lastQuarantinedFilePath() const;
|
|
|
|
|
2023-12-01 13:35:01 +01:00
|
|
|
/// Removes the quarantined files for our DocKey.
|
2023-05-08 14:11:01 +02:00
|
|
|
void removeQuarantinedFiles();
|
2023-05-08 13:57:30 +02:00
|
|
|
|
|
|
|
private:
|
2023-05-13 21:50:42 +02:00
|
|
|
/// Returns quarantine directory size in bytes.
|
2023-05-08 14:11:01 +02:00
|
|
|
std::size_t quarantineSize();
|
2023-05-08 13:57:30 +02:00
|
|
|
|
2023-05-08 14:11:01 +02:00
|
|
|
void makeQuarantineSpace();
|
2023-05-08 13:57:30 +02:00
|
|
|
|
2023-05-11 12:46:16 +02:00
|
|
|
void clearOldQuarantineVersions();
|
2023-05-08 13:57:30 +02:00
|
|
|
|
2023-05-08 14:11:01 +02:00
|
|
|
void removeQuarantine();
|
2023-05-08 13:57:30 +02:00
|
|
|
|
|
|
|
private:
|
2023-05-11 13:47:33 +02:00
|
|
|
static std::unordered_map<std::string, std::vector<std::string>> QuarantineMap;
|
2023-05-14 14:45:34 +02:00
|
|
|
/// Protects the shared QuarantineMap from concurrent modification.
|
|
|
|
static std::mutex Mutex;
|
2023-05-08 13:57:30 +02:00
|
|
|
static std::string QuarantinePath;
|
2023-05-08 14:11:01 +02:00
|
|
|
|
2023-05-13 21:49:01 +02:00
|
|
|
/// The delimiter used in the quarantine filename.
|
|
|
|
static constexpr char Delimiter = '_';
|
|
|
|
|
2023-05-11 12:58:15 +02:00
|
|
|
const std::string _docKey;
|
2023-05-13 19:30:41 +02:00
|
|
|
const std::string _docName;
|
2023-05-13 21:49:01 +02:00
|
|
|
/// The quarantined filename is a multi-part string, formed
|
|
|
|
/// from the timestamp, pid, docKey, and document filename.
|
|
|
|
/// The Delimiter is used to join and later tokenize it.
|
|
|
|
/// The document filename is encoded to ensure tokenization.
|
2023-05-14 16:22:26 +02:00
|
|
|
const std::string _quarantinedFilename;
|
2023-05-11 13:00:39 +02:00
|
|
|
const std::size_t _maxSizeBytes;
|
2023-05-11 13:07:16 +02:00
|
|
|
const std::size_t _maxAgeSecs;
|
2023-05-11 13:36:12 +02:00
|
|
|
const std::size_t _maxVersions;
|
2023-05-08 13:57:30 +02:00
|
|
|
};
|