Skip to content

Commit 60a2808

Browse files
committed
feat: add GetSystemStorageDirectory function to manage shared memory storage path
1 parent 712803a commit 60a2808

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

include/libsharedmemory/libsharedmemory.hpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#undef min
2929
#undef max
3030
#undef WIN32_LEAN_AND_MEAN
31+
#include <filesystem>
3132
#endif
3233

3334
namespace lsm
@@ -92,7 +93,7 @@ class Memory
9293

9394
[[nodiscard]] std::span<std::byte> as_bytes() const noexcept
9495
{
95-
return std::span<std::byte>(static_cast<std::byte*>(_data), _size);
96+
return {static_cast<std::byte*>(_data), _size};
9697
}
9798

9899
void destroy() const;
@@ -124,6 +125,30 @@ class Memory
124125

125126
namespace lsm_windows_detail
126127
{
128+
inline std::string GetSystemStorageDirectory()
129+
{
130+
char* programData = nullptr;
131+
size_t len = 0;
132+
if (_dupenv_s(&programData, &len, "PROGRAMDATA") == 0 && programData != nullptr)
133+
{
134+
std::filesystem::path storagePath = std::filesystem::path(programData) / "shared_memory";
135+
free(programData);
136+
137+
// Create directory if it doesn't exist
138+
std::error_code ec;
139+
std::filesystem::create_directories(storagePath, ec);
140+
if (ec)
141+
{
142+
// Directory creation failed
143+
return {};
144+
}
145+
146+
return storagePath.string();
147+
}
148+
// If PROGRAMDATA is not set, return empty string
149+
return {};
150+
}
151+
127152
inline std::string sanitize_name(const std::string& name)
128153
{
129154
std::string sanitized = name;
@@ -142,7 +167,7 @@ namespace lsm_windows_detail
142167
inline std::string persistence_file_path(const std::string& name)
143168
{
144169
char buffer[MAX_PATH] = {0};
145-
const DWORD pathLength = GetTempPathA(static_cast<DWORD>(sizeof(buffer)), buffer);
170+
const DWORD pathLength = GetSystemStorageDirectory();
146171
std::string basePath(buffer, buffer + pathLength);
147172
if (!basePath.empty())
148173
{

0 commit comments

Comments
 (0)