Skip to content

Commit 6a0e549

Browse files
committed
Add remove_binding functionality
1 parent 7d9186a commit 6a0e549

14 files changed

Lines changed: 424 additions & 76 deletions

File tree

‎TASKS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
v1.0.1
22
TODO: Add "can_delete_binding" check to flags or access mode. Nautilus GUI should not offer to delete if it cannot
3+
TODO: Add "can_add_binding" logic to the nautilus menu. Should catch for e.g. xml files with existing labels, and not show the Classify sub menu
34
TODO: Refactor constants into respective namespaces
45
TODO: Remove presence checks from binding::supports
56
TODO: .gschema.xml i18n

‎targets/NMBS-Library/include/private/nmbs/nmbs_private.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,28 @@ namespace nmbs
3636
{
3737
namespace binding
3838
{
39+
/// Holder for nmbs internal metadata. This data is not part of the ADatP-4778
40+
/// standard.
41+
struct InternalMetadata
42+
{
43+
/// @brief Set when reading bindings.
44+
std::optional<std::string> binding_profile;
45+
};
46+
3947
struct DataReference
4048
{
4149
std::string uri;
4250
std::optional<std::string> content_type;
4351
};
4452

53+
/// @brief The structure representing the XML Binding structure.
54+
/// @details This is not a pure representation, the deserialization will also
55+
/// store some metadata.
4556
struct BindingInformation
4657
{
4758
std::vector<ConfidentialityLabel> labels;
4859
DataReference reference;
60+
InternalMetadata internal_metadata;
4961
};
5062

5163
/// @brief XML namespace defined by ADatP‑4774 Ed. A, Ver. 1.
@@ -112,8 +124,23 @@ namespace nmbs
112124
return binding_flags & check;
113125
}
114126

127+
/// @brief Detects the Read and Write possibilities of the path for the current user.
128+
/// @param path
129+
/// @return flags with bits set for read or write.
115130
[[nodiscard]] AccessMode access_mode_filesystem(const std::filesystem::path& path);
116131

132+
/// @brief Reads the full ADatP-4778 binding from the file.
133+
/// @details Returns the full deserialised object. Note that if multiple bindings are present, only one will be
134+
/// returned and the precedence is not defined.
135+
/// @param path to the file
136+
/// @param binding_support flags if already known. The presence of this parameter will save CPU cycles in
137+
/// determining the available binding method for the file. Use manually, or via nmbs::binding::support.
138+
/// @return the raw XML of the stored binding.
139+
/// @since 1.0.0
140+
[[nodiscard]] Expected<std::optional<BindingInformation>> read_binding(
141+
const std::filesystem::path& path,
142+
std::optional<ProfileSupport> binding_support = std::nullopt);
143+
117144
namespace xmp
118145
{
119146
inline constexpr std::string_view profile_canonical_identifier = "urn:nato:stanag:4778:profile:xmp";
@@ -240,6 +267,10 @@ namespace nmbs
240267

241268
namespace xml
242269
{
270+
inline constexpr std::string_view profile_canonical_identifier = "urn:nato:stanag:4778:profile:xml:schema";
271+
272+
inline constexpr std::string_view profile_version_identifier = "urn:nato:stanag:4778:profile:xml:schema:1:0";
273+
243274
[[nodiscard]] AccessMode access_mode(const std::filesystem::path& path);
244275

245276
[[nodiscard]] std::expected<BindingInformation, Error> read(const std::filesystem::path& path);

‎targets/NMBS-Library/include/public/nmbs/nmbs_c.h‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ typedef struct nmbs_security_policies* nmbs_security_policies_ptr;
115115
/// @see nmbs_security_policies_get
116116
typedef struct nmbs_security_policy* nmbs_security_policy_ptr;
117117

118+
/// @brief Opaque Pointer to a security binding.
119+
/// @details Note, that the struct type is not implemented! It is used only to ensure type safety.
120+
/// @ingroup c_spif
121+
/// @see nmbs_binding_read
122+
typedef struct nmbs_binding_information* nmbs_binding_information_ptr;
123+
typedef struct nmbs_binding_information const * nmbs_binding_information_const_ptr;
124+
125+
118126
/// @brief
119127
/// @ingroup c_bindings
120128
/// @param file
@@ -134,6 +142,34 @@ int nmbs_remove_binding(const char* file) NMBS_NOEXCEPT;
134142
/// @return
135143
[[nodiscard]] bool nmbs_binding_flags_supports_labels(uint32_t flags) NMBS_NOEXCEPT;
136144

145+
/// @brief Creates and returns an instance of nmbs_binding_information.
146+
/// @ingroup c_bindings
147+
/// @return Pointer to a "new" nmbs_binding_information
148+
/// @see nmbs_binding_information_delete
149+
nmbs_binding_information_ptr nmbs_binding_information_new() NMBS_NOEXCEPT;
150+
151+
/// @brief "delete"s an instance of nmbs_binding_information.
152+
/// @ingroup c_bindings
153+
/// @see nmbs_binding_information_new
154+
void nmbs_binding_information_delete(nmbs_binding_information_ptr binding_information) NMBS_NOEXCEPT;
155+
156+
/// @brief Reads the nmbs_binding_information from a file, regardless of binding profile.
157+
/// @ingroup c_bindings
158+
/// @param binding_out A pointer to your buffer which we will fill.
159+
/// @param file Path to the file to read labels from.
160+
/// @see nmbs_binding_information_new
161+
void nmbs_binding_information_read(nmbs_binding_information_ptr binding_out, const char* file) NMBS_NOEXCEPT;
162+
163+
void nmbs_binding_information_read_with_known_binding(nmbs_binding_information_ptr binding_out, const char* file, uint32_t binding_support) NMBS_NOEXCEPT;
164+
165+
/// @brief Opaque Accessor
166+
/// @ingroup c_bindings
167+
/// @param binding
168+
/// @return Pointer to the underlying data. Do not free or delete this!
169+
const char* nmbs_binding_information_get_binding_profile(nmbs_binding_information_const_ptr binding) NMBS_NOEXCEPT;
170+
171+
nmbs_confidentiality_labels_ptr nmbs_binding_information_get_labels(nmbs_binding_information_ptr binding) NMBS_NOEXCEPT;
172+
137173
/// @brief
138174
/// @ingroup c_api
139175
void nmbs_cleanup() NMBS_NOEXCEPT;

‎targets/NMBS-Library/src/binding/binding.cpp‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,36 @@ namespace nmbs::binding
3838
return result_flags;
3939
}
4040

41+
Expected<std::optional<BindingInformation>> read_binding(const std::filesystem::path& path,
42+
std::optional<ProfileSupport> binding_support)
43+
{
44+
if (!std::filesystem::exists(path))
45+
{
46+
return std::unexpected(Error::file_not_found());
47+
}
48+
if (!binding_support.has_value())
49+
{
50+
binding_support = binding::support(path);
51+
}
52+
if (!binding::supports_labels(binding_support.value()))
53+
{
54+
return std::nullopt;
55+
}
56+
if (binding::supports_xmp(binding_support.value()))
57+
{
58+
return binding::xmp::read(path);
59+
}
60+
if (binding::supports_xml(binding_support.value()))
61+
{
62+
return serialisation::deserialise_binding_information_from_file(path);
63+
}
64+
if (binding::supports_sidecar(binding_support.value()))
65+
{
66+
return binding::sidecar::read(path);
67+
}
68+
return std::nullopt;
69+
}
70+
4171
ProfileSupport support(const std::filesystem::path& path)
4272
{
4373
if (!std::filesystem::exists(path)) [[unlikely]]

‎targets/NMBS-Library/src/binding/http.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ namespace nmbs::binding::http
6464
const std::string_view base64_xml = binding_data.substr(start_pos, end_pos - start_pos);
6565
const std::string xml = nmbs::serialisation::decode_base64(base64_xml);
6666

67-
return nmbs::serialisation::deserialise_binding_information(xml);
67+
return serialisation::deserialise_binding_information(xml).transform([](auto info) {
68+
info.internal_metadata.binding_profile = profile_version_identifier;
69+
return info;
70+
});
6871
}
6972
}

‎targets/NMBS-Library/src/binding/sidecar.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ namespace nmbs::binding::sidecar
6565

6666
Expected<BindingInformation> read(const std::filesystem::path& path)
6767
{
68-
return read_xml(path).and_then(nmbs::serialisation::deserialise_binding_information);
68+
return read_xml(path)
69+
.and_then(serialisation::deserialise_binding_information)
70+
.transform([](auto info)
71+
{
72+
info.internal_metadata.binding_profile = profile_version_identifier;
73+
return info;
74+
});
6975
}
7076

7177

‎targets/NMBS-Library/src/binding/xml.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ namespace nmbs::binding::xml
4444

4545
std::expected<BindingInformation, Error> read(const std::filesystem::path& path)
4646
{
47-
return read_xml(path).and_then(nmbs::serialisation::deserialise_binding_information);
47+
return read_xml(path)
48+
.and_then(serialisation::deserialise_binding_information)
49+
.transform([](auto info)
50+
{
51+
info.internal_metadata.binding_profile = profile_version_identifier;
52+
return info;
53+
});
4854
}
4955

5056

‎targets/NMBS-Library/src/binding/xmp.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,16 @@ namespace nmbs::binding::xmp
8989
}
9090
}
9191

92+
9293
Expected<BindingInformation> read(const std::filesystem::path& path)
9394
{
94-
return read_xml(path).and_then(nmbs::serialisation::deserialise_binding_information);
95+
return read_xml(path)
96+
.and_then(serialisation::deserialise_binding_information)
97+
.transform([](auto info)
98+
{
99+
info.internal_metadata.binding_profile = profile_version_identifier;
100+
return info;
101+
});
95102
}
96103

97104

0 commit comments

Comments
 (0)