mirror of
https://github.com/kevinbentley/Descent3.git
synced 2025-01-22 11:28:56 +00:00
Implementing new ddio_GetTmpFileName()
New function uses std::filesystem::path and generates result faster than ddio_GetTempFileName() (benchmarked on Linux).
This commit is contained in:
parent
8940a5ae38
commit
511743d4b3
@ -35,3 +35,7 @@ target_include_directories(ddio PUBLIC
|
||||
${PROJECT_SOURCE_DIR}/ddio
|
||||
>
|
||||
)
|
||||
|
||||
if(BUILD_TESTING)
|
||||
add_subdirectory(tests)
|
||||
endif()
|
||||
|
11
ddio/ddio.h
11
ddio/ddio.h
@ -383,6 +383,15 @@ void ddio_DoForeachFile(const std::filesystem::path &search_path, const std::reg
|
||||
// Returns TRUE if successful, FALSE if an error
|
||||
bool ddio_GetTempFileName(const char *basedir, const char *prefix, char *filename);
|
||||
|
||||
/**
|
||||
* Generates a temporary filename based on the prefix in basedir. Function ensures that generated
|
||||
* filename does not exists in basedir directory.
|
||||
* @param basedir directory to put the files
|
||||
* @param prefix prefix for the temp filename
|
||||
* @return generated filename with ".tmp" extension in basedir directory or empty path on failure
|
||||
*/
|
||||
std::filesystem::path ddio_GetTmpFileName(const std::filesystem::path &basedir, const char *prefix);
|
||||
|
||||
/**
|
||||
* Check process existence by PID
|
||||
* @param pid PID of requested process
|
||||
@ -412,4 +421,4 @@ bool ddio_CreateLockFile(const std::filesystem::path &dir);
|
||||
*/
|
||||
bool ddio_DeleteLockFile(const std::filesystem::path &dir);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -17,13 +17,16 @@
|
||||
*/
|
||||
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <regex>
|
||||
|
||||
#include "IOOps.h"
|
||||
#include "chrono_timer.h"
|
||||
#include "ddio.h"
|
||||
#include "mem.h"
|
||||
#include "pserror.h"
|
||||
|
||||
const std::array<char, 4> LOCK_TAG = {'L', 'O', 'C', 'K'};
|
||||
@ -156,3 +159,36 @@ void ddio_DoForeachFile(const std::filesystem::path &search_path, const std::reg
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::filesystem::path ddio_GetTmpFileName(const std::filesystem::path &basedir, const char *prefix) {
|
||||
static const char alphanum[] =
|
||||
"0123456789"
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz";
|
||||
// size of random part
|
||||
const int len = 10;
|
||||
const char *ext = ".tmp";
|
||||
std::filesystem::path result;
|
||||
size_t len_result = strlen((basedir / prefix).u8string().c_str());
|
||||
char *random_name = (char *)mem_malloc(len_result + len + strlen(ext) + 1);
|
||||
strncpy(random_name, (basedir / prefix).u8string().c_str(), len_result);
|
||||
|
||||
srand(D3::ChronoTimer::GetTimeMS());
|
||||
|
||||
int tries = 20;
|
||||
while (tries > 0) {
|
||||
for (size_t i = len_result; i < len_result + len; i++) {
|
||||
random_name[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
|
||||
}
|
||||
random_name[len_result + len] = '\0';
|
||||
strcat(random_name, ext);
|
||||
if (!std::filesystem::exists(random_name)) {
|
||||
// Found unique name, break the loop
|
||||
result = random_name;
|
||||
break;
|
||||
}
|
||||
tries--;
|
||||
}
|
||||
mem_free(random_name);
|
||||
return result;
|
||||
}
|
||||
|
10
ddio/tests/CMakeLists.txt
Normal file
10
ddio/tests/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
set(CMAKE_FOLDER "tests")
|
||||
|
||||
add_executable(ddio_tests
|
||||
ddio_tests.cpp
|
||||
)
|
||||
target_link_libraries(ddio_tests PRIVATE
|
||||
GTest::gtest_main
|
||||
ddio
|
||||
)
|
||||
gtest_discover_tests(ddio_tests)
|
30
ddio/tests/ddio_tests.cpp
Normal file
30
ddio/tests/ddio_tests.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Descent 3
|
||||
* Copyright (C) 2024 Descent Developers
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <filesystem>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "ddio.h"
|
||||
|
||||
TEST(D3, DDIO_GetTmpFileName) {
|
||||
std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
|
||||
std::filesystem::path result = ddio_GetTmpFileName(temp_dir, "prefix_");
|
||||
EXPECT_FALSE(result.empty());
|
||||
EXPECT_EQ(result.extension(), ".tmp");
|
||||
EXPECT_TRUE(canonical(result.parent_path()) == canonical(temp_dir));
|
||||
}
|
Loading…
Reference in New Issue
Block a user