MCPcopy Create free account
hub / github.com/DescentDevelopers/Descent3 / ddio_CreateLockFile

Function ddio_CreateLockFile

ddio/file.cpp:47–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45}
46
47bool ddio_CreateLockFile(const std::filesystem::path& dir) {
48 std::filesystem::path lock_filename = dir / ".lock";
49
50 if(!std::filesystem::is_directory(lock_filename.parent_path())) {
51 return false;
52 }
53
54 LockFileContent lock_content;
55 int32_t curr_pid = ddio_GetPID();
56
57 if (std::filesystem::exists(lock_filename)) {
58 try {
59 std::ifstream lockfile(lock_filename, std::ios::binary);
60 lockfile >> lock_content;
61 lockfile.close();
62 } catch (std::exception &e) {
63 return false;
64 }
65
66 if (lock_content.tag != LOCK_TAG) {
67 return false;
68 }
69
70 // it appears to be a lock file, check the pid
71 ASSERT(lock_content.pid != -1);
72
73 // check the file id in the file, compared to our pid
74 if (lock_content.pid == curr_pid) {
75 // lock file already exists for the current process
76 return true;
77 }
78
79 if (ddio_CheckProcess(lock_content.pid)) {
80 // Process exists, lock still valid
81 return false;
82 }
83
84 // the process no longer exists, we can create a lock file if needed
85 // we'll delete the useless one now
86 // the lock file in the directory belongs to us!
87 std::filesystem::remove(lock_filename);
88 }
89
90 lock_content.pid = curr_pid;
91
92 try {
93 std::ofstream lockfile(lock_filename, std::ios::binary);
94 lockfile << lock_content;
95 lockfile.close();
96 } catch (std::exception &e) {
97 return false;
98 }
99
100 // at this point result will either be 1 or 2 from checking the lock file
101 // either way, a lock file has been created
102 return true;
103}
104

Callers 1

SetupTempDirectoryFunction · 0.85

Calls 2

ddio_GetPIDFunction · 0.70
ddio_CheckProcessFunction · 0.70

Tested by

no test coverage detected