Debian Codegraph @

ddio/file.cpp

raw symbols SCIP
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"cxx . . $ `<file>/lib/IOOps.h`/
25 #include "ddio.h"cxx . . $ `<file>/ddio/ddio.h`/
26 #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/
27
28 const std::arraycxx . . $ std/array#<char, 4> LOCK_TAGcxx . . $ LOCK_TAG. = {'L', 'O', 'C', 'K'};
29
30 struct LockFileContentcxx . . $ LockFileContent# {
31 std::arraycxx . . $ std/array#<char, 4> tagcxx . . $ LockFileContent#tag. = LOCK_TAGcxx . . $ LOCK_TAG.;
32 int32_t pidcxx . . $ LockFileContent#pid. = -1;
33 };
34
35 std::ostreamcxx . . $ std/ostream# &operatorcxx . . $ `operator<<`(ea796d7543f67c6).<<(std::ostreamcxx . . $ std/ostream# &output, const LockFileContentcxx . . $ LockFileContent# &header) {
36 std::copycxx . . $ std/copy(88304b602929f6f4).(header.tagcxx . . $ LockFileContent#tag..begincxx . . $ std/array#begin(8e614c7de0b1573e).(), header.tagcxx . . $ LockFileContent#tag..endcxx . . $ std/array#end(8e614c7de0b1573e).(), stdcxx . . $ std/ostream_iterator#ostream_iterator(693068b3c10adaac).::ostream_iteratorcxx . . $ std/ostream_iterator#<char>(output));
37 D3cxx . . $ D3/::bin_writecxx . . $ D3/bin_write(b9bf9de5c7808980).(output, header.pidcxx . . $ LockFileContent#pid.);
38 return output;
39 }
40
41 std::istreamcxx . . $ std/istream# &operatorcxx . . $ `operator>>`(906de88874806234).>>(std::istreamcxx . . $ std/istream# &input, LockFileContentcxx . . $ LockFileContent# &header) {
42 input.readcxx . . $ std/basic_istream#read(ec7d6f01d31dcb96).(reinterpret_cast<char*>(header.tagcxx . . $ LockFileContent#tag..datacxx . . $ std/array#data(258ee1c9e0a18a5c).()), 4);
43 D3cxx . . $ D3/::bin_readcxx . . $ D3/bin_read(66ff81923e1a8aa0).(input, header.pidcxx . . $ LockFileContent#pid.);
44 return input;
45 }
46
47 bool ddio_CreateLockFilecxx . . $ ddio_CreateLockFile(c53a6ef3278968b1).(const std::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#& dir) {
48 std::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# lock_filename = dir /cxx . . $ std/filesystem/__cxx11/`operator/`(578e0438ad5f0e7e). ".lock"cxx . . $ std/filesystem/__cxx11/path#path(835bc3a16b770d4d).;
49
50 if(!std::filesystemcxx . . $ std/filesystem/::is_directorycxx . . $ std/filesystem/is_directory(c53a6ef3278968b1).(lock_filename.parent_pathcxx . . $ std/filesystem/__cxx11/path#parent_path(159787a050b29eb0).())) {
51 return false;
52 }
53
54 LockFileContentcxx . . $ LockFileContent# lock_content;
55 int32_t curr_pid = ddio_GetPIDcxx . . $ ddio_GetPID(b126dc7c1de90089).();
56
57 if (std::filesystemcxx . . $ std/filesystem/::existscxx . . $ std/filesystem/exists(c53a6ef3278968b1).(lock_filename)) {
58 try {
59 std::ifstreamcxx . . $ std/ifstream# lockfilecxx . . $ std/basic_ifstream#basic_ifstream(fbe7c9ddfb647a62).(lock_filename, std::ioscxx . . $ std/ios#::binarycxx . . $ std/ios_base#binary.);
60 lockfile >>cxx . . $ `operator>>`(906de88874806234). lock_content;
61 lockfile.closecxx . . $ std/basic_ifstream#close(49f6e7a06ebc5aa8).();
62 } catch (std::exception &e) {
63 return false;
64 }
65
66 if (lock_content.tagcxx . . $ LockFileContent#tag. !=cxx . . $ std/`operator!=`(a474572a1f22b964). LOCK_TAGcxx . . $ LOCK_TAG.) {
67 return false;
68 }
69
70 // it appears to be a lock file, check the pid
71 ASSERTcxx . . $ `misc/pserror.h:246:9`!(lock_content.pid != -1);
72
73 // check the file id in the file, compared to our pid
74 if (lock_content.pidcxx . . $ LockFileContent#pid. == curr_pid) {
75 // lock file already exists for the current process
76 return true;
77 }
78
79 if (ddio_CheckProcesscxx . . $ ddio_CheckProcess(83c267b20bac841a).(lock_content.pidcxx . . $ LockFileContent#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::filesystemcxx . . $ std/filesystem/::removecxx . . $ std/filesystem/remove(c53a6ef3278968b1).(lock_filename);
88 }
89
90 lock_content.pidcxx . . $ LockFileContent#pid. = curr_pid;
91
92 try {
93 std::ofstreamcxx . . $ std/ofstream# lockfilecxx . . $ std/basic_ofstream#basic_ofstream(fbe7c9ddfb647a62).(lock_filename, std::ioscxx . . $ std/ios#::binarycxx . . $ std/ios_base#binary.);
94 lockfile <<cxx . . $ `operator<<`(ea796d7543f67c6). lock_content;
95 lockfile.closecxx . . $ std/basic_ofstream#close(49f6e7a06ebc5aa8).();
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_DeleteLockFilecxx . . $ ddio_DeleteLockFile(c53a6ef3278968b1).(const std::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#& dir) {
106 int32_t curr_pid = ddio_GetPIDcxx . . $ ddio_GetPID(b126dc7c1de90089).();
107
108 std::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# lock_filename = dir /cxx . . $ std/filesystem/__cxx11/`operator/`(578e0438ad5f0e7e). ".lock"cxx . . $ std/filesystem/__cxx11/path#path(835bc3a16b770d4d).;
109 if (!std::filesystemcxx . . $ std/filesystem/::existscxx . . $ std/filesystem/exists(c53a6ef3278968b1).(lock_filename)) {
110 return true;
111 }
112
113 LockFileContentcxx . . $ LockFileContent# lock_content;
114 try {
115 std::ifstreamcxx . . $ std/ifstream# lockfilecxx . . $ std/basic_ifstream#basic_ifstream(fbe7c9ddfb647a62).(lock_filename, std::ioscxx . . $ std/ios#::binarycxx . . $ std/ios_base#binary.);
116 lockfile >>cxx . . $ `operator>>`(906de88874806234). lock_content;
117 lockfile.closecxx . . $ std/basic_ifstream#close(49f6e7a06ebc5aa8).();
118 } catch (std::exception &e) {
119 return false;
120 }
121
122 if (lock_content.tagcxx . . $ LockFileContent#tag. !=cxx . . $ std/`operator!=`(a474572a1f22b964). LOCK_TAGcxx . . $ LOCK_TAG.) {
123 return false;
124 }
125
126 ASSERTcxx . . $ `misc/pserror.h:246:9`!(lock_content.pid != -1);
127
128 // check the file id in the file, compared to our pid
129 if (lock_content.pidcxx . . $ LockFileContent#pid. != curr_pid) {
130 // it doesn't belong to
131 return false;
132 }
133
134 std::error_codecxx . . $ std/error_code# eccxx . . $ std/error_code#error_code(ced63f7c635d850d).;
135 // the lock file in the directory belongs to us!
136 if (!std::filesystemcxx . . $ std/filesystem/::removecxx . . $ std/filesystem/remove(c59b83c1f15b4fe7).(lock_filename, ec)) {
137 return false;
138 }
139
140 return true;
141 }