diff --git a/CMakeLists.txt b/CMakeLists.txt index 9469c929..cf4ea85d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -501,6 +501,8 @@ if (NOT CROSSTOOLS) target_include_directories(pip_lua PUBLIC "${_lua_src_dir}") if (WIN32) target_compile_definitions(pip_lua PRIVATE LUA_BUILD_AS_DLL LUA_CORE) + else() + target_compile_definitions(pip_lua PRIVATE LUA_USE_POSIX) endif() list(APPEND HDR_DIRS "${PIP_3PL_DIR}/LuaBridge") list(APPEND HDRS ${_lua_src_hdr}) diff --git a/libs/main/io_devices/pifile.cpp b/libs/main/io_devices/pifile.cpp index 879f0ef1..f9ed991d 100644 --- a/libs/main/io_devices/pifile.cpp +++ b/libs/main/io_devices/pifile.cpp @@ -176,14 +176,16 @@ bool PIFile::openTemporary(PIIODevice::DeviceMode mode) { PIString tp; #ifdef WINDOWS tp = PIDir::temporary().path() + PIDir::separator + "file" + PIString::fromNumber(randomi()); -#else - char * rc = tmpnam(0); - if (!rc) return false; - tp = rc; -#endif while (isExists(tp)) { tp += PIString::fromNumber(randomi() % 10); } +#else + char template_rc[] = "/tmp/pifile_tmp_XXXXXX"; + int fd = mkstemp(template_rc); + if (fd == -1) return false; + ::close(fd); + tp = template_rc; +#endif return open(tp, mode); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3eefbfcb..c5dae9c3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -35,3 +35,4 @@ pip_test(math) pip_test(core) pip_test(piobject) pip_test(client_server pip_client_server) +pip_test(io) diff --git a/tests/io/testpifile.cpp b/tests/io/testpifile.cpp new file mode 100644 index 00000000..33944763 --- /dev/null +++ b/tests/io/testpifile.cpp @@ -0,0 +1,14 @@ +#include "pifile.h" + +#include "gtest/gtest.h" + +TEST(PIFile_Test, openTemporary) { + const auto ba = PIByteArray::fromHex("AABBCC"); + PIFile f; + ASSERT_TRUE(f.openTemporary()); + ASSERT_TRUE(f.path().isNotEmpty()); + ASSERT_EQ(ba.size(), f.write(ba)); + ASSERT_EQ(ba, f.readAll()); + ASSERT_TRUE(f.close()); +} +