Skip to content

Commit 8c65cfb

Browse files
Fix Windows build: platform-compatible stat and test helpers
1 parent aa0c17d commit 8c65cfb

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

liblsl/src/lsl_security.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include <windows.h>
2828
#include <iphlpapi.h>
2929
#include <direct.h>
30+
#include <sys/types.h>
31+
#include <sys/stat.h>
3032
#pragma comment(lib, "iphlpapi.lib")
3133
#else
3234
#include <unistd.h>
@@ -57,8 +59,13 @@ static int make_dir(const char* p) {
5759

5860
// Check if path exists and is a directory
5961
static bool is_directory(const char* p) {
62+
#ifdef _WIN32
63+
struct _stat st;
64+
return _stat(p, &st) == 0 && (st.st_mode & _S_IFDIR);
65+
#else
6066
struct stat st;
6167
return stat(p, &st) == 0 && S_ISDIR(st.st_mode);
68+
#endif
6269
}
6370

6471
// Recursively create directory and all parent components with mode 0700 (owner-only).

liblsl/testing/int/security.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,51 @@
2727
#include <cstring>
2828
#include <fstream>
2929
#include <map>
30+
#include <vector>
31+
32+
#ifdef _WIN32
33+
#include <direct.h>
34+
#include <sys/types.h>
35+
#include <sys/stat.h>
36+
#include <io.h>
37+
#define stat_func _stat
38+
#define stat_struct struct _stat
39+
#else
3040
#include <sys/stat.h>
3141
#include <unistd.h>
32-
#include <vector>
42+
#define stat_func stat
43+
#define stat_struct struct stat
44+
#endif
3345

3446
using namespace lsl::security;
3547

3648
// Helper functions for test file/directory operations
3749
static void test_mkdir_p(const std::string& path) {
50+
#ifdef _WIN32
51+
std::string cmd = "mkdir \"" + path + "\" 2>nul";
52+
#else
3853
std::string cmd = "mkdir -p \"" + path + "\"";
54+
#endif
3955
std::system(cmd.c_str());
4056
}
4157

4258
static void test_rm_rf(const std::string& path) {
59+
#ifdef _WIN32
60+
std::string cmd = "rmdir /s /q \"" + path + "\" 2>nul";
61+
#else
4362
std::string cmd = "rm -rf \"" + path + "\"";
63+
#endif
4464
std::system(cmd.c_str());
4565
}
4666

4767
static bool test_file_exists(const std::string& path) {
48-
struct stat buffer;
49-
return (stat(path.c_str(), &buffer) == 0);
68+
stat_struct buffer;
69+
return (stat_func(path.c_str(), &buffer) == 0);
5070
}
5171

5272
static size_t test_file_size(const std::string& path) {
53-
struct stat buffer;
54-
if (stat(path.c_str(), &buffer) != 0) return 0;
73+
stat_struct buffer;
74+
if (stat_func(path.c_str(), &buffer) != 0) return 0;
5575
return static_cast<size_t>(buffer.st_size);
5676
}
5777

@@ -122,7 +142,14 @@ TEST_CASE("Key save creates directories and sets permissions", "[security][keyge
122142
sec.initialize();
123143

124144
// Use a unique nested path that does not pre-exist
125-
std::string test_base = "/tmp/lsl_dirtest_" +
145+
std::string tmp_dir;
146+
#ifdef _WIN32
147+
const char* tmp_env = std::getenv("TEMP");
148+
tmp_dir = tmp_env ? tmp_env : "C:\\Temp";
149+
#else
150+
tmp_dir = "/tmp";
151+
#endif
152+
std::string test_base = tmp_dir + "/lsl_dirtest_" +
126153
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
127154
std::string nested_config = test_base + "/sub/deep/lsl_api.cfg";
128155

0 commit comments

Comments
 (0)