| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Descent Developers |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <array> |
| 20 | #include <filesystem> |
| 21 | #include <fstream> |
| 22 | #include <iterator> |
| 23 | |
| 24 | #include "IOOps.h" |
| 25 | #include "ddio.h" |
| 26 | #include "pserror.h" |
| 27 | |
| 28 | const std::array<char, 4> LOCK_TAG = {'L', 'O', 'C', 'K'}; |
| 29 | |
| 30 | struct LockFileContent { |
| 31 | std::array<char, 4> tag = LOCK_TAG; |
| 32 | int32_t pid = -1; |
| 33 | }; |
| 34 | |
| 35 | std::ostream &operator<<(std::ostream &output, const LockFileContent &header) { |
| 36 | std::copy(header.tag.begin(), header.tag.end(), std::ostream_iterator<char>(output)); |
| 37 | D3::bin_write(output, header.pid); |
| 38 | return output; |
| 39 | } |
| 40 | |
| 41 | std::istream &operator>>(std::istream &input, LockFileContent &header) { |
| 42 | input.read(reinterpret_cast<char*>(header.tag.data()), 4); |
| 43 | D3::bin_read(input, header.pid); |
| 44 | return input; |
| 45 | } |
| 46 | |
| 47 | bool 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 | |
| 105 | bool ddio_DeleteLockFile(const std::filesystem::path& dir) { |
| 106 | int32_t curr_pid = ddio_GetPID(); |
| 107 | |
| 108 | std::filesystem::path lock_filename = dir / ".lock"; |
| 109 | if (!std::filesystem::exists(lock_filename)) { |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | LockFileContent lock_content; |
| 114 | try { |
| 115 | std::ifstream lockfile(lock_filename, std::ios::binary); |
| 116 | lockfile >> lock_content; |
| 117 | lockfile.close(); |
| 118 | } catch (std::exception &e) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | if (lock_content.tag != LOCK_TAG) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | ASSERT(lock_content.pid != -1); |
| 127 | |
| 128 | // check the file id in the file, compared to our pid |
| 129 | if (lock_content.pid != curr_pid) { |
| 130 | // it doesn't belong to |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | std::error_code ec; |
| 135 | // the lock file in the directory belongs to us! |
| 136 | if (!std::filesystem::remove(lock_filename, ec)) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | return true; |
| 141 | } |