mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-14 01:13:33 +08:00
Simple test for POSIX storage backend
This commit is contained in:
@@ -40,6 +40,29 @@ int main(int argc, const char** argv)
|
||||
ENFORCE(0 == std::system(("cat " + event_log_file).c_str()));
|
||||
}
|
||||
|
||||
/*
|
||||
* Storage backend test
|
||||
*/
|
||||
{
|
||||
using namespace uavcan::dynamic_node_id_server;
|
||||
|
||||
uavcan_posix::dynamic_node_id_server::FileStorageBackend backend;
|
||||
ENFORCE(0 <= backend.init("/tmp/uavcan_posix/dynamic_node_id_server/storage"));
|
||||
|
||||
auto print_key = [&](const char* key) {
|
||||
std::cout << static_cast<IStorageBackend&>(backend).get(key).c_str() << std::endl;
|
||||
};
|
||||
|
||||
print_key("foobar");
|
||||
|
||||
static_cast<IStorageBackend&>(backend).set("foobar", "0123456789abcdef0123456789abcdef");
|
||||
static_cast<IStorageBackend&>(backend).set("the_answer", "42");
|
||||
|
||||
print_key("foobar");
|
||||
print_key("the_answer");
|
||||
print_key("nonexistent");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
|
||||
+3
-1
@@ -34,6 +34,8 @@ class FileStorageBackend : public uavcan::dynamic_node_id_server::IStorageBacken
|
||||
*/
|
||||
enum { MaxPathLength = 128 };
|
||||
|
||||
enum { FilePermissions = 438 }; ///< 0o666
|
||||
|
||||
/**
|
||||
* This type is used for the path
|
||||
*/
|
||||
@@ -76,7 +78,7 @@ class FileStorageBackend : public uavcan::dynamic_node_id_server::IStorageBacken
|
||||
using namespace std;
|
||||
PathString path = base_path.c_str();
|
||||
path += key;
|
||||
int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC);
|
||||
int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, FilePermissions);
|
||||
if (fd >= 0)
|
||||
{
|
||||
write(fd, value.c_str(), value.size());
|
||||
|
||||
Reference in New Issue
Block a user