forked from CollaboraOnline/online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuarantineUtil.hpp
68 lines (52 loc) · 2.02 KB
/
QuarantineUtil.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* Copyright the Collabora Online contributors.
*
* SPDX-License-Identifier: MPL-2.0
*
* 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/.
*/
#pragma once
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
class DocumentBroker;
class Quarantine
{
public:
Quarantine(DocumentBroker& docBroker, const std::string& docName);
static void initialize(const std::string& path);
/// Quarantines a new version of the document.
bool quarantineFile(const std::string& docName);
/// Returns the last quarantined file's path.
std::string lastQuarantinedFilePath() const;
/// Removes the quarantined files for the given DocKey when we unload gracefully.
void removeQuarantinedFiles();
private:
bool isQuarantineEnabled() const { return !QuarantinePath.empty(); }
/// Returns quarantine directory size in bytes.
std::size_t quarantineSize();
void makeQuarantineSpace();
void clearOldQuarantineVersions();
void removeQuarantine();
private:
static std::unordered_map<std::string, std::vector<std::string>> QuarantineMap;
/// Protects the shared QuarantineMap from concurrent modification.
static std::mutex Mutex;
static std::string QuarantinePath;
/// The delimiter used in the quarantine filename.
static constexpr char Delimiter = '_';
const std::string _docKey;
const std::string _docName;
/// 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.
const std::string _quarantinedFilename;
const std::size_t _maxSizeBytes;
const std::size_t _maxAgeSecs;
const std::size_t _maxVersions;
};