Skip to content
API Documentation
BastionGuard™
anonymous_namespace{Quarantine.cpp} Namespace Reference

Functions

bool is_regular_file_without_following_symlinks (const fs::path &path, std::error_code &ec)
fs::path normalize_path (const fs::path &path, std::error_code &ec)
bool is_path_inside (const fs::path &candidate, const fs::path &directory)
void remove_without_throwing (const fs::path &path)
bool copy_then_remove (const fs::path &source, const fs::path &destination)

Function Documentation

◆ copy_then_remove()

bool anonymous_namespace{Quarantine.cpp}::copy_then_remove ( const fs::path & source,
const fs::path & destination )

Definition at line 90 of file Quarantine.cpp.

91{
92 std::error_code copy_ec;
93 fs::copy_file(source, destination, fs::copy_options::none, copy_ec);
94 if (copy_ec) {
95 std::cerr << _("❌ Errore copia in quarantena: ")
96 << copy_ec.message() << std::endl;
97 return false;
98 }
99
100 std::error_code remove_ec;
101 fs::remove(source, remove_ec);
102 if (remove_ec) {
103 std::cerr << _("❌ Impossibile rimuovere il file originale dopo la copia: ")
104 << remove_ec.message() << std::endl;
105
106 // La quarantena non è riuscita: non lasciare una copia che possa
107 // essere interpretata come isolamento completato.
108 remove_without_throwing(destination);
109 return false;
110 }
111
112 return true;
113}
#define _(s)
void remove_without_throwing(const fs::path &path)

References _, copy_then_remove(), and remove_without_throwing().

Referenced by copy_then_remove().

◆ is_path_inside()

bool anonymous_namespace{Quarantine.cpp}::is_path_inside ( const fs::path & candidate,
const fs::path & directory )

Definition at line 60 of file Quarantine.cpp.

61{
62 std::error_code candidate_ec;
63 std::error_code directory_ec;
64
65 const fs::path normalized_candidate = normalize_path(candidate, candidate_ec);
66 const fs::path normalized_directory = normalize_path(directory, directory_ec);
67
68 if (candidate_ec || directory_ec || normalized_directory.empty())
69 return false;
70
71 auto candidate_it = normalized_candidate.begin();
72 for (auto directory_it = normalized_directory.begin();
73 directory_it != normalized_directory.end();
74 ++directory_it, ++candidate_it) {
75 if (candidate_it == normalized_candidate.end() ||
76 *candidate_it != *directory_it) {
77 return false;
78 }
79 }
80
81 return true;
82}
fs::path normalize_path(const fs::path &path, std::error_code &ec)

References is_path_inside(), and normalize_path().

Referenced by is_path_inside().

◆ is_regular_file_without_following_symlinks()

bool anonymous_namespace{Quarantine.cpp}::is_regular_file_without_following_symlinks ( const fs::path & path,
std::error_code & ec )

Definition at line 37 of file Quarantine.cpp.

39{
40 ec.clear();
41 const fs::file_status status = fs::symlink_status(path, ec);
42 return !ec && fs::exists(status) && fs::is_regular_file(status);
43}

References is_regular_file_without_following_symlinks().

Referenced by is_regular_file_without_following_symlinks().

◆ normalize_path()

fs::path anonymous_namespace{Quarantine.cpp}::normalize_path ( const fs::path & path,
std::error_code & ec )

Definition at line 45 of file Quarantine.cpp.

46{
47 ec.clear();
48 fs::path normalized = fs::weakly_canonical(path, ec);
49 if (!ec)
50 return normalized.lexically_normal();
51
52 ec.clear();
53 normalized = fs::absolute(path, ec);
54 if (!ec)
55 return normalized.lexically_normal();
56
57 return path.lexically_normal();
58}

References normalize_path().

Referenced by is_path_inside(), and normalize_path().

◆ remove_without_throwing()

void anonymous_namespace{Quarantine.cpp}::remove_without_throwing ( const fs::path & path)

Definition at line 84 of file Quarantine.cpp.

85{
86 std::error_code ec;
87 fs::remove(path, ec);
88}

References remove_without_throwing().

Referenced by copy_then_remove(), and remove_without_throwing().