szaszm commented on code in PR #2289:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2289#discussion_r4564713261
##########
extensions/opc/tests/features/resources/opcua-file-server/file_server.cpp:
##########
@@ -197,27 +228,69 @@ UA_StatusCode fileMethodCallback(UA_Server*, const
UA_NodeId*, void*, const UA_N
return Method(*context, std::span(input, input_size), output);
}
-UA_StatusCode addFileNode(UA_Server* server, const std::filesystem::path&
path) {
+/** Adds a FileType node for the file to the Files node. The server assigns
the node id if the requested node id is numeric 0. */
+std::expected<FileContext*, UA_StatusCode> addFileNode(UA_Server* server,
const std::filesystem::path& path, const UA_NodeId& requested_node_id,
+ UA_NodeId* file_node_id = nullptr) {
auto context = std::make_unique<FileContext>(path);
const std::string name = path.filename().string();
UA_ObjectAttributes attr = UA_ObjectAttributes_default;
attr.displayName = UA_LOCALIZEDTEXT(mutableChars("en-US"),
mutableChars(name.c_str()));
const UA_StatusCode status = UA_Server_addObjectNode(server,
- UA_NODEID_STRING(1, mutableChars(name.c_str())),
+ requested_node_id,
FILES_NODE_ID,
UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
UA_QUALIFIEDNAME(1, mutableChars(name.c_str())),
UA_NODEID_NUMERIC(0, UA_NS0ID_FILETYPE),
attr,
context.get(),
- nullptr);
+ file_node_id);
if (status != UA_STATUSCODE_GOOD) {
- return status;
+ return std::unexpected{status};
}
logInfo("Added file node for {}", path.string());
- file_contexts.push_back(std::move(context));
- return UA_STATUSCODE_GOOD;
+ return file_contexts.emplace_back(std::move(context)).get();
+}
+
+/** Implements the CreateFile method of FileDirectoryType, the object context
being the path of the directory of the called node */
+UA_StatusCode createFileMethod(UA_Server* server, const UA_NodeId*, void*,
const UA_NodeId*, void*, const UA_NodeId*, void* object_context, size_t,
+ const UA_Variant* input, size_t, UA_Variant* output) {
+ const auto* directory = static_cast<const
std::filesystem::path*>(object_context);
+ if (!directory) {
+ return UA_STATUSCODE_BADNOTFOUND;
+ }
+ const auto ua_name = scalarInput<UA_String>(input[0]);
+ const auto request_file_open = scalarInput<UA_Boolean>(input[1]);
+ const std::string name(reinterpret_cast<const char*>(ua_name.data),
ua_name.length);
+ if (name.empty() || name.size() >= NAME_MAX || name.contains('/') || name ==
"." || name == "..") {
+ return UA_STATUSCODE_BADINVALIDARGUMENT;
+ }
+
+ const std::filesystem::path path = *directory / name;
+ std::error_code error;
+ if (std::filesystem::exists(path, error)) {
+ return UA_STATUSCODE_BADBROWSENAMEDUPLICATED;
+ }
+ if (!std::ofstream(path, std::ios::binary)) {
Review Comment:
I'd change this to clarify the meaning. {} makes it clear that this is
indended to be a temporary object, not a function call, and the comment further
clarifies the purpose.
```suggestion
// create empty file
if (!std::ofstream{path, std::ios::binary}) {
```
##########
extensions/opc/tests/features/resources/opcua-file-server/file_server.cpp:
##########
@@ -249,19 +323,21 @@ UA_StatusCode setupAddressSpace(UA_Server* server, const
std::filesystem::path&
UA_QUALIFIEDNAME(1, mutableChars("Files")),
UA_NODEID_NUMERIC(0, UA_NS0ID_FILEDIRECTORYTYPE),
attr,
- nullptr,
+ const_cast<std::filesystem::path*>(&data_dir),
Review Comment:
Could you add a comment explaining why this const_cast is safe in practice?
##########
extensions/opc/src/OPCCommon.cpp:
##########
@@ -123,6 +124,30 @@ core::logging::LOG_LEVEL MapOPCLogLevel(UA_LogLevel ualvl)
{
return core::logging::critical;
}
}
+
+template<typename BrowseResponse>
+UA_StatusCode singleBrowseResultStatus(const BrowseResponse& response) {
+ if (response.responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
+ return response.responseHeader.serviceResult;
+ }
+ if (response.resultsSize != 1) {
+ return UA_STATUSCODE_BADUNEXPECTEDERROR;
+ }
+ return response.results[0].statusCode;
+}
+
+// Searches a page of browse results for a reference with the given browse
name, and takes over the continuation point of the page
+std::optional<NodeId> findReferenceByBrowseName(UA_BrowseResult& result,
std::string_view browse_name, UA_ByteString& continuation_point) {
+ UA_ByteString_clear(&continuation_point);
+ std::swap(continuation_point, result.continuationPoint);
+ for (size_t i = 0; i < result.referencesSize; ++i) {
+ const auto& ref = result.references[i];
+ if (std::string_view(reinterpret_cast<const
char*>(ref.browseName.name.data), ref.browseName.name.length) == browse_name) {
Review Comment:
I'd extract `ref.browseName.name` too, with a type if possible, to make
future changes less error-prone. (the two long distance references shouldn't be
able to drift from each other)
##########
extensions/opc/src/OPCCommon.cpp:
##########
@@ -508,6 +533,62 @@ std::expected<NodeId, UA_StatusCode>
Client::findChild(const UA_NodeId& parent_n
return NodeId::copyOf(response.results[0].targets[0].targetId.nodeId);
}
+std::expected<NodeId, UA_StatusCode> Client::findObjectChildByName(const
UA_NodeId& parent_node_id, std::string_view browse_name,
+ UA_UInt32 ref_type_id) {
+ UA_BrowseRequest browse_request;
+ UA_BrowseRequest_init(&browse_request);
+ browse_request.nodesToBrowse = UA_BrowseDescription_new();
+ browse_request.nodesToBrowseSize = 1;
+ UA_NodeId_copy(&parent_node_id, &browse_request.nodesToBrowse[0].nodeId);
+ browse_request.nodesToBrowse[0].referenceTypeId = UA_NODEID_NUMERIC(0,
ref_type_id);
+ browse_request.nodesToBrowse[0].includeSubtypes = true;
+ browse_request.nodesToBrowse[0].browseDirection = UA_BROWSEDIRECTION_FORWARD;
+ browse_request.nodesToBrowse[0].nodeClassMask = UA_NODECLASS_OBJECT;
+ browse_request.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_BROWSENAME;
+
+ UA_BrowseResponse browse_response = UA_Client_Service_browse(client_,
browse_request);
+ const auto guard = gsl::finally([&browse_request, &browse_response]() {
+ UA_BrowseRequest_clear(&browse_request);
+ UA_BrowseResponse_clear(&browse_response);
+ });
Review Comment:
Should we separate the two cleanup guards, to avoid possible future cases
where an error between the two initializations cause the resources of one to be
leaked?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]