Debian Codegraph @

src/system.cc

raw symbols SCIP
1 /**
2 * \file src/system.cc
3 * \ingroup system
4 *
5 * Provide a high-level interface to system-related features, like filesystem manipulations.
6 *
7 * Ideally, all OS-specific features should be grouped here.
8 *
9 * This modules shoumd not depend on any other opustags module.
10 */
11
12 #include <opustags.h>cxx . . $ `<file>/src/opustags.h`/
13
14 #include <errno.h>
15 #include <fstream>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/stat.h>
19 #include <sys/wait.h>
20 #include <unistd.h>
21
22 otcxx . . $ ot/::byte_stringcxx . . $ ot/byte_string# operatorcxx . . $ `operator""_bs`(5eb9a56dda65ba65).""_bs(const char* data, size_t size)
23 {
24 return otcxx . . $ std/__cxx11/basic_string#basic_string(6f2e210728d16ac0).::byte_stringcxx . . $ ot/byte_string#(reinterpret_cast<const uint8_t*>(data), size);
25 }
26
27 otcxx . . $ ot/::byte_string_viewcxx . . $ ot/byte_string_view# operatorcxx . . $ `operator""_bsv`(630326791a3f0367).""_bsv(const char* data, size_t size)
28 {
29 return otcxx . . $ std/basic_string_view#basic_string_view(f8b699e64a75d442).::byte_string_viewcxx . . $ ot/byte_string_view#(reinterpret_cast<const uint8_t*>(data), size);
30 }
31
32 void otcxx . . $ ot/::partial_filecxx . . $ ot/partial_file#::opencxx . . $ ot/partial_file#open(85c52e162fed56f9).(const char* destination)
33 {
34 final_namecxx . . $ ot/partial_file#final_name. =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). destination;
35 temporary_namecxx . . $ ot/partial_file#temporary_name. =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). final_namecxx . . $ ot/partial_file#final_name. +cxx . . $ std/operator+(40fc1db9226233c6). ".XXXXXX.part";
36 int fd = mkstemps(const_cast<char*>(temporary_namecxx . . $ ot/partial_file#temporary_name..datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).()), 5);
37 if (fd == -1)
38 throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error.,
39 "Could not create a partial file for '" +cxx . . $ std/operator+(42c652dfebe55ad7). final_namecxx . . $ ot/partial_file#final_name. +cxx . . $ std/operator+(900ec9581c410ad9). "': " +cxx . . $ std/operator+(900ec9581c410ad9).
40 strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)};
41 filecxx . . $ ot/partial_file#file. =cxx . . $ ot/file#`operator=`(8da8a9ac207569ce). fdopencxx . . $ ot/file#file(8d4262d45fc644ab).(fd, "w");
42 if (filecxx . . $ ot/partial_file#file. ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr)
43 throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error.,
44 "Could not get the partial file handle to '" +cxx . . $ std/operator+(42c652dfebe55ad7). temporary_namecxx . . $ ot/partial_file#temporary_name. +cxx . . $ std/operator+(900ec9581c410ad9). "': " +cxx . . $ std/operator+(900ec9581c410ad9).
45 strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)};
46 }
47
48 static mode_t get_umaskcxx . . $ get_umask(db52760e79bf9054).()
49 {
50 // libc doesn’t seem to provide a way to get umask without changing it, so we need this workaround.
51 // https://www.gnu.org/software/libc/manual/html_node/Setting-Permissions.html
52 mode_t mask = umask(0);
53 umask(mask);
54 return mask;
55 }
56
57 /**
58 * Try reproducing the file permissions of file `source` onto file `dest`. If
59 * this fails for whatever reason, print a warning and leave the current
60 * permissions. When the source doesn’t exist, use the default file creation
61 * permissions according to umask.
62 */
63 static void copy_permissionscxx . . $ copy_permissions(2fa786934fe80e24).(const char* source, const char* dest)
64 {
65 mode_t target_mode;
66 struct stat source_stat;
67 if (stat(source, &source_stat) == 0) {
68 // We could technically preserve a bit more than that but who
69 // would ever need S_ISUID and friends on an Opus file?
70 target_mode = source_stat.st_mode & 0777;
71 } else if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) {
72 target_mode = 0666 & ~get_umaskcxx . . $ get_umask(db52760e79bf9054).();
73 } else {
74 fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "warning: Could not read mode of %s: %s\n", source, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
75 return;
76 }
77 if (chmod(dest, target_mode) == -1)
78 fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "warning: Could not set mode of %s: %s\n", dest, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
79 }
80
81 void otcxx . . $ ot/::partial_filecxx . . $ ot/partial_file#::commitcxx . . $ ot/partial_file#commit(49f6e7a06ebc5aa8).()
82 {
83 if (filecxx . . $ ot/partial_file#file. ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr)
84 return;
85 filecxx . . $ ot/partial_file#file..resetcxx . . $ std/unique_ptr#reset(c8c3025d73122d7d).();
86 copy_permissionscxx . . $ copy_permissions(2fa786934fe80e24).(final_namecxx . . $ ot/partial_file#final_name..c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), temporary_namecxx . . $ ot/partial_file#temporary_name..c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
87 if (rename(temporary_namecxx . . $ ot/partial_file#temporary_name..c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), final_namecxx . . $ ot/partial_file#final_name..c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()) == -1)
88 throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error.,
89 "Could not move the result file '" +cxx . . $ std/operator+(42c652dfebe55ad7). temporary_namecxx . . $ ot/partial_file#temporary_name. +cxx . . $ std/operator+(900ec9581c410ad9). "' to '" +cxx . . $ std/operator+(db5dc2ea912c6495).
90 final_namecxx . . $ ot/partial_file#final_name. +cxx . . $ std/operator+(900ec9581c410ad9). "': " +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) +cxx . . $ std/operator+(900ec9581c410ad9). "."};
91 }
92
93 void otcxx . . $ ot/::partial_filecxx . . $ ot/partial_file#::abortcxx . . $ ot/partial_file#abort(49f6e7a06ebc5aa8).()
94 {
95 if (filecxx . . $ ot/partial_file#file. ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr)
96 return;
97 filecxx . . $ ot/partial_file#file..resetcxx . . $ std/unique_ptr#reset(c8c3025d73122d7d).();
98 remove(temporary_namecxx . . $ ot/partial_file#temporary_name..c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
99 }
100
101 /**
102 * Determine the file size, in bytes, of the given file. Return -1 on for streams.
103 */
104 static long get_file_sizecxx . . $ get_file_size(1dcafcd8ef4c6e6c).(FILE* f)
105 {
106 if (fseek(f, 0L, SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!) != 0) {
107 clearerr(f); // Recover.
108 return -1;
109 }
110 long file_size = ftell(f);
111 rewind(f);
112 return file_size;
113 }
114
115 otcxx . . $ ot/::byte_stringcxx . . $ ot/byte_string# otcxx . . $ ot/::slurp_binary_filecxx . . $ ot/slurp_binary_file(568fd295d03a71e1).(const char* filename)
116 {
117 filecxx . . $ ot/file# f = strcmpcxx . . $ ot/file#file(8d4262d45fc644ab).(filename, "-") == 0 ? freopen(nullptr, "rb", stdincxx . . $ `<external>/232824719582829767/stdio.h:153:9`!)
118 : fopen(filename, "rb");
119 if (f ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr)
120 throw statuscxx . . $ ot/status#status(15c445588664906). { stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error.,
121 "Could not open '"scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78). +cxx . . $ std/operator+(900ec9581c410ad9). filename +cxx . . $ std/operator+(900ec9581c410ad9). "': " +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) +cxx . . $ std/operator+(900ec9581c410ad9). "." };
122
123 byte_stringcxx . . $ ot/byte_string# contentcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).;
124 long file_size = get_file_sizecxx . . $ get_file_size(1dcafcd8ef4c6e6c).(f.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).());
125 if (file_size == -1) {
126 // Read the input stream block by block and resize the output byte string as needed.
127 uint8_t buffer[4096];
128 while (!feof(f.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())) {
129 size_t read_len = fread(buffer, 1, sizeof(buffer), f.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).());
130 content.appendcxx . . $ std/__cxx11/basic_string#append(8f1e29ffd2791417).(buffer, read_len);
131 if (ferror(f.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()))
132 throw statuscxx . . $ ot/status#status(15c445588664906). { stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error.,
133 "Could not read '"scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78). +cxx . . $ std/operator+(900ec9581c410ad9). filename +cxx . . $ std/operator+(900ec9581c410ad9). "': " +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) +cxx . . $ std/operator+(900ec9581c410ad9). "." };
134 }
135 } else {
136 // Lucky! We know the file size, so let’s slurp it at once.
137 content.resizecxx . . $ std/__cxx11/basic_string#resize(a21092e7312342c).(file_size);
138 if (fread(content.datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).(), 1, file_size, f.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()) < file_size)
139 throw statuscxx . . $ ot/status#status(15c445588664906). { stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error.,
140 "Could not read '"scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78). +cxx . . $ std/operator+(900ec9581c410ad9). filename +cxx . . $ std/operator+(900ec9581c410ad9). "': " +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) +cxx . . $ std/operator+(900ec9581c410ad9). "." };
141 }
142
143 return contentcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
144 }
145
146 /** C++ wrapper for iconv. */
147 class encoding_convertercxx . . $ encoding_converter# {
148 public:
149 /**
150 * Allocate the iconv conversion state, initializing the given source and destination
151 * character encodings. If it's okay to have some information lost, make sure `to` ends with
152 * "//TRANSLIT", otherwise the conversion will fail when a character cannot be represented
153 * in the target encoding. See the documentation of iconv_open for details.
154 */
155 encoding_convertercxx . . $ encoding_converter#encoding_converter(2fa786934fe80e24).(const char* from, const char* to);
156 ~cxx . . $ encoding_converter#`~encoding_converter`(ced63f7c635d850d).encoding_convertercxx . . $ encoding_converter#();
157 /**
158 * Convert text using iconv. If the input sequence is invalid, return #st::badly_encoded and
159 * abort the processing, leaving out in an undefined state.
160 */
161 template<class InChar, class OutChar>
162 stdcxx . . $ std/::basic_stringcxx . . $ std/__cxx11/basic_string#<OutChar> convertcxx . . $ encoding_converter#convert(3ca86034a87a7aaf).(stdcxx . . $ std/::basic_string_viewcxx . . $ std/basic_string_view#<InChar>);
163 private:
164 iconv_t cdcxx . . $ encoding_converter#cd.; /**< conversion descriptor */
165 };
166
167 encoding_convertercxx . . $ encoding_converter#::encoding_convertercxx . . $ encoding_converter#encoding_converter(2fa786934fe80e24).(const char* from, const char* to)
168 {
169 cdcxx . . $ encoding_converter#cd. = iconv_open(to, from);
170 if (cdcxx . . $ encoding_converter#cd. == (iconv_t) -1)
171 throw stdcxx . . $ std/bad_alloc#bad_alloc(ced63f7c635d850d).::bad_alloccxx . . $ std/bad_alloc#();
172 }
173
174 encoding_convertercxx . . $ encoding_converter#::~cxx . . $ encoding_converter#`~encoding_converter`(ced63f7c635d850d).encoding_convertercxx . . $ encoding_converter#()
175 {
176 iconv_close(cdcxx . . $ encoding_converter#cd.);
177 }
178
179 template<class InChar, class OutChar>
180 stdcxx . . $ std/::basic_stringcxx . . $ std/__cxx11/basic_string#<OutChar> encoding_convertercxx . . $ encoding_converter#::convertcxx . . $ encoding_converter#convert(3ca86034a87a7aaf).(stdcxx . . $ std/::basic_string_viewcxx . . $ std/basic_string_view#<InChar> in)
181 {
182 iconv(cdcxx . . $ encoding_converter#cd., nullptr, nullptr, nullptr, nullptr);
183 stdcxx . . $ std/::basic_stringcxx . . $ std/__cxx11/basic_string#<OutChar> out;
184 out.reservecxx . . $ std/__cxx11/basic_string#reserve(a21092e7312342c).(in.sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).());
185 const char* in_data = reinterpret_cast<const char*>(in.datacxx . . $ std/basic_string_view#data(8e614c7de0b1573e).());
186 char* in_cursor = const_cast<char*>(in_data);
187 size_t in_left = in.sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).();
188 constexpr size_t chunk_size = 1024;
189 char chunk[chunk_size];
190 for (;;) {
191 char *out_cursor = chunk;
192 size_t out_left = chunk_size;
193 size_t rc = iconv(cdcxx . . $ encoding_converter#cd., &in_cursor, &in_left, &out_cursor, &out_left);
194
195 if (rc == (size_t) -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == E2BIGcxx . . $ `<external>/8987895939074593727/errno-base.h:11:9`!) {
196 // Loop normally.
197 } else if (rc == (size_t) -1) {
198 throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::badly_encodedcxx . . $ ot/st#badly_encoded., strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) +cxx . . $ std/operator+(39ed2c165380f78e). "."scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78).};
199 } else if (rc != 0) {
200 throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::badly_encodedcxx . . $ ot/st#badly_encoded.,
201 "Some characters could not be converted into the target encoding."};
202 }
203
204 out.appendcxx . . $ std/__cxx11/basic_string#append(c0b47be1fddc0e75).(reinterpret_cast<OutChar*>(chunk), out_cursor - chunk);
205 if (in_cursor == nullptr)
206 break;
207 else if (in_left == 0)
208 in_cursor = nullptr;
209 }
210 return out;
211 }
212
213 stdcxx . . $ std/::u8stringcxx . . $ std/u8string# otcxx . . $ ot/::encode_utf8cxx . . $ ot/encode_utf8(4bb652cfea9d987a).(stdcxx . . $ std/::string_viewcxx . . $ std/string_view# in)
214 {
215 static encoding_convertercxx . . $ encoding_converter# to_utf8_cvtcxx . . $ encoding_converter#encoding_converter(2fa786934fe80e24).("", "UTF-8");
216 return to_utf8_cvt.convertcxx . . $ encoding_converter#convert(3ca86034a87a7aaf).<char, char8_t>(incxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).);
217 }
218
219 stdcxx . . $ std/::stringcxx . . $ std/string# otcxx . . $ ot/::decode_utf8cxx . . $ ot/decode_utf8(249f514546b71f2d).(stdcxx . . $ std/::u8string_viewcxx . . $ std/u8string_view# in)
220 {
221 static encoding_convertercxx . . $ encoding_converter# from_utf8_cvtcxx . . $ encoding_converter#encoding_converter(2fa786934fe80e24).("UTF-8", "");
222 return from_utf8_cvt.convertcxx . . $ encoding_converter#convert(3ca86034a87a7aaf).<char8_t, char>(incxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).);
223 }
224
225 stdcxx . . $ std/::stringcxx . . $ std/string# otcxx . . $ ot/::shell_escapecxx . . $ ot/shell_escape(6ccff4770ff7ddc3).(stdcxx . . $ std/::string_viewcxx . . $ std/string_view# word)
226 {
227 stdcxx . . $ std/::stringcxx . . $ std/string# escaped_wordcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).;
228 // Pre-allocate the result, assuming most of the time enclosing it in single quotes is enough.
229 escaped_word.reservecxx . . $ std/__cxx11/basic_string#reserve(a21092e7312342c).(2 + word.sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).());
230
231 escaped_word +=cxx . . $ std/__cxx11/basic_string#`operator+=`(63cc1ae870623930). '\'';
232 for (char c : word) {
233 if (c == '\'')
234 escaped_word +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). "'\\''";
235 else if (c == '!')
236 escaped_word +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). "'\\!'";
237 else
238 escaped_word +=cxx . . $ std/__cxx11/basic_string#`operator+=`(63cc1ae870623930). c;
239 }
240 escaped_word +=cxx . . $ std/__cxx11/basic_string#`operator+=`(63cc1ae870623930). '\'';
241
242 return escaped_wordcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
243 }
244
245 void otcxx . . $ ot/::run_editorcxx . . $ ot/run_editor(9ebb09e227cac344).(stdcxx . . $ std/::string_viewcxx . . $ std/string_view# editor, stdcxx . . $ std/::string_viewcxx . . $ std/string_view# path)
246 {
247 stdcxx . . $ std/::stringcxx . . $ std/string# command = stdcxx . . $ std/::stringcxx . . $ std/string#(editor) +cxx . . $ std/operator+(900ec9581c410ad9). " " +cxx . . $ std/operator+(aa1c00e85b87da6b). shell_escapecxx . . $ ot/shell_escape(6ccff4770ff7ddc3).(pathcxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).);
248 int status = systemcxx . . $ system(5c4306d8553b3696).(command.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
249
250 if (status == -1)
251 throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error., "waitpid error: "scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78). +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)};
252 else if (!WIFEXITEDcxx . . $ `<external>/10696550936717951914/stdlib.h:47:10`!(status))
253 throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {stcxx . . $ ot/st#::child_process_failedcxx . . $ ot/st#child_process_failed.,
254 "Child process did not terminate normally: "scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78). +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)};
255 else if (WEXITSTATUScxx . . $ `<external>/10696550936717951914/stdlib.h:44:10`!(status) != 0)
256 throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {stcxx . . $ ot/st#::child_process_failedcxx . . $ ot/st#child_process_failed.,
257 "Child process exited with " +cxx . . $ std/operator+(39ed2c165380f78e). stdcxx . . $ std/::to_stringcxx . . $ std/__cxx11/to_string(3d81e17e7a87eecc).(WEXITSTATUScxx . . $ `<external>/10696550936717951914/stdlib.h:44:10`!(status))};
258 }
259
260 timespec otcxx . . $ ot/::get_file_timestampcxx . . $ ot/get_file_timestamp(afc89d0ecac88f66).(const char* path)
261 {
262 timespec mtime;
263 struct stat st;
264 if (stat(path, &st) == -1)
265 throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error., path +cxx . . $ std/operator+(39ed2c165380f78e). ": stat error: "scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78). +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)};
266 #if defined(HAVE_STAT_ST_MTIMcxx . . $ `build/config.h:6:9`!)
267 mtime = st.st_mtim;
268 #elif defined(HAVE_STAT_ST_MTIMESPEC)
269 mtime = st.st_mtimespec;
270 #else
271 mtime.tv_sec = st.st_mtime;
272 mtime.tv_nsec = st.st_mtimensec;
273 #endif
274 return mtime;
275 }