2021-10-21 14:29:15 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
2023-10-30 16:58:54 -05:00
|
|
|
* Copyright the Collabora Online contributors.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2023-11-09 12:23:00 -06: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 14:29:15 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-05-14 07:45:34 -05:00
|
|
|
#include <mutex>
|
2021-10-21 14:29:15 -05:00
|
|
|
#include <string>
|
2023-05-11 06:47:33 -05:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2021-10-21 14:29:15 -05:00
|
|
|
|
|
|
|
class DocumentBroker;
|
2023-05-08 06:57:30 -05:00
|
|
|
|
|
|
|
class Quarantine
|
2021-10-21 14:29:15 -05:00
|
|
|
{
|
2023-05-08 06:57:30 -05:00
|
|
|
public:
|
2023-05-13 12:30:41 -05:00
|
|
|
Quarantine(DocumentBroker& docBroker, const std::string& docName);
|
2023-05-08 07:11:01 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
static void initialize(const std::string& path);
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2024-01-01 09:00:03 -06:00
|
|
|
bool isEnabled() const { return !QuarantinePath.empty(); }
|
|
|
|
|
2023-07-17 06:29:22 -05:00
|
|
|
/// Quarantines a new version of the document.
|
2023-05-08 07:11:01 -05:00
|
|
|
bool quarantineFile(const std::string& docName);
|
2023-04-21 18:28:23 -05:00
|
|
|
|
2023-07-17 06:29:22 -05:00
|
|
|
/// Returns the last quarantined file's path.
|
|
|
|
std::string lastQuarantinedFilePath() const;
|
|
|
|
|
2023-12-01 06:35:01 -06:00
|
|
|
/// Removes the quarantined files for our DocKey.
|
2023-05-08 07:11:01 -05:00
|
|
|
void removeQuarantinedFiles();
|
2023-05-08 06:57:30 -05:00
|
|
|
|
|
|
|
private:
|
2023-05-13 14:50:42 -05:00
|
|
|
/// Returns quarantine directory size in bytes.
|
2023-05-08 07:11:01 -05:00
|
|
|
std::size_t quarantineSize();
|
2023-05-08 06:57:30 -05:00
|
|
|
|
2023-05-08 07:11:01 -05:00
|
|
|
void makeQuarantineSpace();
|
2023-05-08 06:57:30 -05:00
|
|
|
|
2023-05-11 05:46:16 -05:00
|
|
|
void clearOldQuarantineVersions();
|
2023-05-08 06:57:30 -05:00
|
|
|
|
2023-05-08 07:11:01 -05:00
|
|
|
void removeQuarantine();
|
2023-05-08 06:57:30 -05:00
|
|
|
|
|
|
|
private:
|
2023-05-11 06:47:33 -05:00
|
|
|
static std::unordered_map<std::string, std::vector<std::string>> QuarantineMap;
|
2023-05-14 07:45:34 -05:00
|
|
|
/// Protects the shared QuarantineMap from concurrent modification.
|
|
|
|
static std::mutex Mutex;
|
2023-05-08 06:57:30 -05:00
|
|
|
static std::string QuarantinePath;
|
2023-05-08 07:11:01 -05:00
|
|
|
|
2023-05-13 14:49:01 -05:00
|
|
|
/// The delimiter used in the quarantine filename.
|
|
|
|
static constexpr char Delimiter = '_';
|
|
|
|
|
2023-05-11 05:58:15 -05:00
|
|
|
const std::string _docKey;
|
2023-05-13 12:30:41 -05:00
|
|
|
const std::string _docName;
|
2023-05-13 14:49:01 -05: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 09:22:26 -05:00
|
|
|
const std::string _quarantinedFilename;
|
2023-05-11 06:00:39 -05:00
|
|
|
const std::size_t _maxSizeBytes;
|
2023-05-11 06:07:16 -05:00
|
|
|
const std::size_t _maxAgeSecs;
|
2023-05-11 06:36:12 -05:00
|
|
|
const std::size_t _maxVersions;
|
2023-05-08 06:57:30 -05:00
|
|
|
};
|