2024-06-15 23:31:43 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2024-06-16 00:53:15 +00:00
|
|
|
#include <array>
|
2024-09-11 10:20:12 +00:00
|
|
|
#include <cstring>
|
2024-06-15 23:31:43 +00:00
|
|
|
#include <filesystem>
|
2024-06-16 00:53:15 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
2024-07-08 10:21:44 +00:00
|
|
|
#include <regex>
|
2024-06-15 23:31:43 +00:00
|
|
|
|
2024-06-16 00:53:15 +00:00
|
|
|
#include "IOOps.h"
|
2024-09-11 10:20:12 +00:00
|
|
|
#include "chrono_timer.h"
|
2024-06-15 23:31:43 +00:00
|
|
|
#include "ddio.h"
|
2024-09-11 10:20:12 +00:00
|
|
|
#include "mem.h"
|
2024-06-16 00:53:15 +00:00
|
|
|
#include "pserror.h"
|
2024-06-15 23:31:43 +00:00
|
|
|
|
2024-06-16 00:53:15 +00:00
|
|
|
const std::array<char, 4> LOCK_TAG = {'L', 'O', 'C', 'K'};
|
2024-06-15 23:31:43 +00:00
|
|
|
|
2024-06-16 00:53:15 +00:00
|
|
|
struct LockFileContent {
|
|
|
|
std::array<char, 4> tag = LOCK_TAG;
|
|
|
|
int32_t pid = -1;
|
|
|
|
};
|
2024-06-15 23:31:43 +00:00
|
|
|
|
2024-06-16 00:53:15 +00:00
|
|
|
std::ostream &operator<<(std::ostream &output, const LockFileContent &header) {
|
|
|
|
std::copy(header.tag.begin(), header.tag.end(), std::ostream_iterator<char>(output));
|
|
|
|
D3::bin_write(output, header.pid);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::istream &operator>>(std::istream &input, LockFileContent &header) {
|
2024-07-08 10:21:44 +00:00
|
|
|
input.read(reinterpret_cast<char *>(header.tag.data()), 4);
|
2024-06-16 00:53:15 +00:00
|
|
|
D3::bin_read(input, header.pid);
|
|
|
|
return input;
|
|
|
|
}
|
2024-06-15 23:31:43 +00:00
|
|
|
|
2024-07-08 10:21:44 +00:00
|
|
|
bool ddio_CreateLockFile(const std::filesystem::path &dir) {
|
2024-06-16 00:53:15 +00:00
|
|
|
std::filesystem::path lock_filename = dir / ".lock";
|
2024-06-15 23:31:43 +00:00
|
|
|
|
2024-07-08 10:21:44 +00:00
|
|
|
if (!std::filesystem::is_directory(lock_filename.parent_path())) {
|
2024-06-16 00:53:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LockFileContent lock_content;
|
|
|
|
int32_t curr_pid = ddio_GetPID();
|
|
|
|
|
|
|
|
if (std::filesystem::exists(lock_filename)) {
|
|
|
|
try {
|
|
|
|
std::ifstream lockfile(lock_filename, std::ios::binary);
|
|
|
|
lockfile >> lock_content;
|
|
|
|
lockfile.close();
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
return false;
|
2024-06-15 23:31:43 +00:00
|
|
|
}
|
2024-06-16 00:53:15 +00:00
|
|
|
|
|
|
|
if (lock_content.tag != LOCK_TAG) {
|
|
|
|
return false;
|
2024-06-15 23:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// it appears to be a lock file, check the pid
|
2024-06-16 00:53:15 +00:00
|
|
|
ASSERT(lock_content.pid != -1);
|
2024-06-15 23:31:43 +00:00
|
|
|
|
|
|
|
// check the file id in the file, compared to our pid
|
2024-06-16 00:53:15 +00:00
|
|
|
if (lock_content.pid == curr_pid) {
|
2024-06-15 23:31:43 +00:00
|
|
|
// lock file already exists for the current process
|
2024-06-16 00:53:15 +00:00
|
|
|
return true;
|
2024-06-15 23:31:43 +00:00
|
|
|
}
|
|
|
|
|
2024-06-16 00:53:15 +00:00
|
|
|
if (ddio_CheckProcess(lock_content.pid)) {
|
2024-06-15 23:31:43 +00:00
|
|
|
// Process exists, lock still valid
|
2024-06-16 00:53:15 +00:00
|
|
|
return false;
|
2024-06-15 23:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the process no longer exists, we can create a lock file if needed
|
|
|
|
// we'll delete the useless one now
|
2024-06-16 00:53:15 +00:00
|
|
|
// the lock file in the directory belongs to us!
|
|
|
|
std::filesystem::remove(lock_filename);
|
2024-06-15 23:31:43 +00:00
|
|
|
}
|
|
|
|
|
2024-06-16 00:53:15 +00:00
|
|
|
lock_content.pid = curr_pid;
|
2024-06-15 23:31:43 +00:00
|
|
|
|
2024-06-16 00:53:15 +00:00
|
|
|
try {
|
|
|
|
std::ofstream lockfile(lock_filename, std::ios::binary);
|
|
|
|
lockfile << lock_content;
|
|
|
|
lockfile.close();
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
return false;
|
2024-06-15 23:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// at this point result will either be 1 or 2 from checking the lock file
|
|
|
|
// either way, a lock file has been created
|
2024-06-16 00:53:15 +00:00
|
|
|
return true;
|
2024-06-15 23:31:43 +00:00
|
|
|
}
|
|
|
|
|
2024-07-08 10:21:44 +00:00
|
|
|
bool ddio_DeleteLockFile(const std::filesystem::path &dir) {
|
2024-06-16 00:53:15 +00:00
|
|
|
int32_t curr_pid = ddio_GetPID();
|
2024-06-15 23:31:43 +00:00
|
|
|
|
2024-06-16 00:53:15 +00:00
|
|
|
std::filesystem::path lock_filename = dir / ".lock";
|
|
|
|
if (!std::filesystem::exists(lock_filename)) {
|
|
|
|
return true;
|
2024-06-15 23:31:43 +00:00
|
|
|
}
|
2024-06-16 00:53:15 +00:00
|
|
|
|
|
|
|
LockFileContent lock_content;
|
|
|
|
try {
|
|
|
|
std::ifstream lockfile(lock_filename, std::ios::binary);
|
|
|
|
lockfile >> lock_content;
|
|
|
|
lockfile.close();
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
return false;
|
2024-06-15 23:31:43 +00:00
|
|
|
}
|
2024-06-16 00:53:15 +00:00
|
|
|
|
|
|
|
if (lock_content.tag != LOCK_TAG) {
|
|
|
|
return false;
|
2024-06-15 23:31:43 +00:00
|
|
|
}
|
|
|
|
|
2024-06-16 00:53:15 +00:00
|
|
|
ASSERT(lock_content.pid != -1);
|
2024-06-15 23:31:43 +00:00
|
|
|
|
|
|
|
// check the file id in the file, compared to our pid
|
2024-06-16 00:53:15 +00:00
|
|
|
if (lock_content.pid != curr_pid) {
|
2024-06-15 23:31:43 +00:00
|
|
|
// it doesn't belong to
|
2024-06-16 00:53:15 +00:00
|
|
|
return false;
|
2024-06-15 23:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code ec;
|
|
|
|
// the lock file in the directory belongs to us!
|
2024-06-16 00:53:15 +00:00
|
|
|
if (!std::filesystem::remove(lock_filename, ec)) {
|
|
|
|
return false;
|
2024-06-15 23:31:43 +00:00
|
|
|
}
|
|
|
|
|
2024-06-16 00:53:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
2024-07-08 10:21:44 +00:00
|
|
|
|
2024-08-11 00:37:42 +00:00
|
|
|
void ddio_DoForeachFile(const std::filesystem::path &search_path, const std::regex ®ex,
|
2024-07-08 10:21:44 +00:00
|
|
|
const std::function<void(std::filesystem::path)> &func) {
|
|
|
|
if (!std::filesystem::is_directory(search_path)) {
|
2024-08-11 00:37:42 +00:00
|
|
|
return;
|
2024-07-08 10:21:44 +00:00
|
|
|
}
|
|
|
|
const std::filesystem::directory_iterator end;
|
|
|
|
for (std::filesystem::directory_iterator iter{search_path}; iter != end; iter++) {
|
|
|
|
const std::string filename = iter->path().filename().string();
|
|
|
|
if (std::filesystem::is_regular_file(*iter)) {
|
|
|
|
if (std::regex_match(filename, regex)) {
|
|
|
|
func(iter->path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-11 10:20:12 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|