Line data Source code
1 : /*
2 : +----------------------------------------------------------------------+
3 : | LSParrot PHP LSP Extension |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) LSParrot GitHub Organization |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to the 0BSD license that is |
8 : | bundled with this package in the file LICENSE. |
9 : +----------------------------------------------------------------------+
10 : | Author: Go Kudo <zeriyoshi@gmail.com> |
11 : +----------------------------------------------------------------------+
12 : */
13 :
14 : #ifndef LSP_PORTABILITY_H
15 : # define LSP_PORTABILITY_H
16 :
17 : # ifdef HAVE_CONFIG_H
18 : # include <config.h>
19 : # endif
20 :
21 : # include <php.h>
22 :
23 : # include <ctype.h>
24 : # include <errno.h>
25 : # include <stdio.h>
26 : # include <stdint.h>
27 : # include <stdbool.h>
28 : # include <stdlib.h>
29 : # include <string.h>
30 : # include <sys/stat.h>
31 : # include <time.h>
32 :
33 : # if defined(_WIN32)
34 : # include <fcntl.h>
35 : # include <io.h>
36 : # include <process.h>
37 : # include <windows.h>
38 : typedef intptr_t lsp_process_id;
39 : typedef intptr_t lsp_pipe_handle;
40 : # define LSP_HAVE_POSIX_PROCESS 0
41 : # define LSP_INVALID_PROCESS_ID ((lsp_process_id) 0)
42 : # define LSP_INVALID_PIPE_HANDLE ((lsp_pipe_handle) 0)
43 : # define strcasecmp _stricmp
44 : # define strncasecmp _strnicmp
45 : # else
46 : # include <fcntl.h>
47 : # include <poll.h>
48 : # include <signal.h>
49 : # include <spawn.h>
50 : # include <strings.h>
51 : # include <sys/select.h>
52 : # include <sys/time.h>
53 : # include <sys/wait.h>
54 : # include <unistd.h>
55 : typedef pid_t lsp_process_id;
56 : typedef intptr_t lsp_pipe_handle;
57 : # define LSP_HAVE_POSIX_PROCESS 1
58 : # define LSP_INVALID_PROCESS_ID ((lsp_process_id) 0)
59 : # define LSP_INVALID_PIPE_HANDLE ((lsp_pipe_handle) -1)
60 : # endif
61 :
62 : # include <Zend/zend_virtual_cwd.h>
63 :
64 966428 : static inline bool lsp_is_path_separator(char c)
65 : {
66 : #if defined(_WIN32)
67 : return c == '/' || c == '\\';
68 : #else
69 966428 : return c == '/';
70 : #endif
71 : }
72 :
73 16828 : static inline size_t lsp_path_root_length(const char *value, size_t length)
74 : {
75 : #if defined(_WIN32)
76 : size_t server_end, share_end;
77 :
78 : if (length >= 3 && isalpha((unsigned char) value[0]) && value[1] == ':' && lsp_is_path_separator(value[2])) {
79 : return 3;
80 : }
81 : if (length >= 2 && isalpha((unsigned char) value[0]) && value[1] == ':') {
82 : return 2;
83 : }
84 : if (length >= 2 && lsp_is_path_separator(value[0]) && lsp_is_path_separator(value[1])) {
85 : server_end = 2;
86 : while (server_end < length && !lsp_is_path_separator(value[server_end])) {
87 : server_end++;
88 : }
89 : if (server_end >= length) {
90 : return length;
91 : }
92 :
93 : share_end = server_end + 1;
94 : while (share_end < length && !lsp_is_path_separator(value[share_end])) {
95 : share_end++;
96 : }
97 :
98 : return share_end < length ? share_end + 1 : share_end;
99 : }
100 : #endif
101 :
102 16828 : return length > 0 && lsp_is_path_separator(value[0]) ? 1 : 0;
103 : }
104 :
105 2214 : static inline bool lsp_path_is_absolute(zend_string *path)
106 : {
107 : const char *value;
108 : size_t length;
109 :
110 2214 : value = ZSTR_VAL(path);
111 2214 : length = ZSTR_LEN(path);
112 :
113 : #if defined(_WIN32)
114 : if (length >= 3 && isalpha((unsigned char) value[0]) && value[1] == ':' && lsp_is_path_separator(value[2])) {
115 : return true;
116 : }
117 : if (length >= 2 && lsp_is_path_separator(value[0]) && lsp_is_path_separator(value[1])) {
118 : return true;
119 : }
120 : #endif
121 :
122 2214 : return length > 0 && lsp_is_path_separator(value[0]);
123 : }
124 :
125 205032 : static inline bool lsp_path_has_trailing_separator(zend_string *path)
126 : {
127 205032 : return ZSTR_LEN(path) > 0 && lsp_is_path_separator(ZSTR_VAL(path)[ZSTR_LEN(path) - 1]);
128 : }
129 :
130 17750 : static inline char *lsp_last_path_separator(char *path)
131 : {
132 : char *forward;
133 : #if defined(_WIN32)
134 : char *backward;
135 : #endif
136 :
137 17750 : forward = strrchr(path, '/');
138 : #if defined(_WIN32)
139 : backward = strrchr(path, '\\');
140 : if (forward && backward) {
141 : return forward > backward ? forward : backward;
142 : }
143 : if (backward) {
144 : return backward;
145 : }
146 : #endif
147 :
148 17750 : return forward;
149 : }
150 :
151 88 : static inline const char *lsp_last_path_separator_const(const char *path)
152 : {
153 : const char *forward;
154 : #if defined(_WIN32)
155 : const char *backward;
156 : #endif
157 :
158 88 : forward = strrchr(path, '/');
159 : #if defined(_WIN32)
160 : backward = strrchr(path, '\\');
161 : if (forward && backward) {
162 : return forward > backward ? forward : backward;
163 : }
164 : if (backward) {
165 : return backward;
166 : }
167 : #endif
168 :
169 88 : return forward;
170 : }
171 :
172 56 : static inline const char *lsp_path_basename(zend_string *path)
173 : {
174 : const char *slash;
175 :
176 56 : slash = lsp_last_path_separator_const(ZSTR_VAL(path));
177 :
178 56 : return slash ? slash + 1 : ZSTR_VAL(path);
179 : }
180 :
181 23982 : static inline int lsp_path_compare(const char *left, const char *right, size_t length)
182 : {
183 : #if defined(_WIN32)
184 : return strncasecmp(left, right, length);
185 : #else
186 23982 : return strncmp(left, right, length);
187 : #endif
188 : }
189 :
190 11044 : static inline bool lsp_path_value_contains_segment(const char *path, size_t path_length, const char *segment)
191 : {
192 : size_t segment_length, i;
193 : bool start_ok, end_ok;
194 :
195 11044 : segment_length = strlen(segment);
196 11044 : if (segment_length == 0 || path_length < segment_length) {
197 1000 : return false;
198 : }
199 :
200 351338 : for (i = 0; i + segment_length <= path_length; i++) {
201 341322 : start_ok = i == 0 || lsp_is_path_separator(path[i - 1]);
202 341322 : end_ok = i + segment_length == path_length || lsp_is_path_separator(path[i + segment_length]);
203 341322 : if (start_ok && end_ok && lsp_path_compare(path + i, segment, segment_length) == 0) {
204 28 : return true;
205 : }
206 : }
207 :
208 10016 : return false;
209 : }
210 :
211 15696 : static inline bool lsp_path_is_same_or_under(zend_string *path, zend_string *root)
212 : {
213 : size_t root_length, root_min_length;
214 :
215 15696 : root_length = ZSTR_LEN(root);
216 15696 : root_min_length = lsp_path_root_length(ZSTR_VAL(root), root_length);
217 16316 : while (root_length > root_min_length && lsp_is_path_separator(ZSTR_VAL(root)[root_length - 1])) {
218 620 : root_length--;
219 : }
220 :
221 15696 : if (root_length == 0 || ZSTR_LEN(path) < root_length) {
222 1436 : return false;
223 : }
224 :
225 14260 : if (root_length == 1 && lsp_is_path_separator(ZSTR_VAL(root)[0])) {
226 0 : return ZSTR_LEN(path) > 0 && lsp_is_path_separator(ZSTR_VAL(path)[0]);
227 : }
228 :
229 14260 : if (lsp_path_compare(ZSTR_VAL(path), ZSTR_VAL(root), root_length) != 0) {
230 4160 : return false;
231 : }
232 :
233 10100 : if (root_length == root_min_length && root_length > 0 && lsp_is_path_separator(ZSTR_VAL(root)[root_length - 1])) {
234 0 : return true;
235 : }
236 :
237 10100 : return ZSTR_LEN(path) == root_length || lsp_is_path_separator(ZSTR_VAL(path)[root_length]);
238 : }
239 :
240 936 : static inline char lsp_path_list_separator(void)
241 : {
242 : #if defined(_WIN32)
243 : return ';';
244 : #else
245 936 : return ':';
246 : #endif
247 : }
248 :
249 11000 : static inline uint32_t lsp_executable_extension_count(void)
250 : {
251 : #if defined(_WIN32)
252 : return 4u;
253 : #else
254 11000 : return 1u;
255 : #endif
256 : }
257 :
258 5500 : static inline const char *lsp_executable_extension(uint32_t index)
259 : {
260 : #if defined(_WIN32)
261 : static const char *extensions[] = {"", ".bat", ".cmd", ".exe"};
262 :
263 : if (index < sizeof(extensions) / sizeof(extensions[0])) {
264 : return extensions[index];
265 : }
266 : #else
267 : (void) index;
268 : #endif
269 :
270 5500 : return "";
271 : }
272 :
273 : static inline bool lsp_string_ends_with_ci(zend_string *value, const char *suffix)
274 : {
275 : size_t suffix_length;
276 :
277 : suffix_length = strlen(suffix);
278 : if (ZSTR_LEN(value) < suffix_length) {
279 : return false;
280 : }
281 :
282 : return strncasecmp(ZSTR_VAL(value) + ZSTR_LEN(value) - suffix_length, suffix, suffix_length) == 0;
283 : }
284 :
285 5500 : static inline bool lsp_path_is_windows_batch_file(zend_string *path)
286 : {
287 : #if defined(_WIN32)
288 : return lsp_string_ends_with_ci(path, ".bat") || lsp_string_ends_with_ci(path, ".cmd");
289 : #else
290 : (void) path;
291 :
292 5500 : return false;
293 : #endif
294 : }
295 :
296 36434 : static inline bool lsp_is_regular_file(zend_string *path)
297 : {
298 : zend_stat_t st;
299 :
300 36434 : return VCWD_STAT(ZSTR_VAL(path), &st) == 0 && S_ISREG(st.st_mode);
301 : }
302 :
303 5500 : static inline bool lsp_path_is_executable_file(zend_string *path)
304 : {
305 : #if defined(_WIN32)
306 : return lsp_is_regular_file(path);
307 : #else
308 5500 : return lsp_is_regular_file(path) && access(ZSTR_VAL(path), X_OK) == 0;
309 : #endif
310 : }
311 :
312 156 : static inline const char *lsp_php_binary(void)
313 : {
314 : static char binary[MAXPATHLEN];
315 :
316 156 : if (binary[0] == '\0') {
317 : #if defined(_WIN32)
318 : snprintf(binary, sizeof(binary), "%s/php.exe", PHP_BINDIR);
319 : #else
320 120 : snprintf(binary, sizeof(binary), "%s/php", PHP_BINDIR);
321 : #endif
322 : }
323 :
324 156 : return binary;
325 : }
326 :
327 548 : static inline void lsp_set_lsp_stdio_binary(void)
328 : {
329 : #if defined(_WIN32)
330 : _setmode(_fileno(stdin), _O_BINARY);
331 : _setmode(_fileno(stdout), _O_BINARY);
332 : #endif
333 548 : }
334 :
335 720 : static inline long lsp_current_process_id(void)
336 : {
337 : #if defined(_WIN32)
338 : return (long) _getpid();
339 : #else
340 720 : return (long) getpid();
341 : #endif
342 : }
343 :
344 8007 : static inline bool lsp_process_id_valid(lsp_process_id process)
345 : {
346 : #if LSP_HAVE_POSIX_PROCESS
347 : /* Reject negative ids too: fork()/spawn failures yield -1, and
348 : * kill(-1, sig) would signal every process the user owns. */
349 8007 : return process > 0;
350 : #else
351 : return process != LSP_INVALID_PROCESS_ID;
352 : #endif
353 : }
354 :
355 4118 : static inline bool lsp_pipe_handle_valid(lsp_pipe_handle pipe)
356 : {
357 4118 : return pipe != LSP_INVALID_PIPE_HANDLE;
358 : }
359 :
360 14654 : static inline zend_long lsp_stat_mtime_nsec(zend_stat_t *st)
361 : {
362 : # if defined(__APPLE__)
363 : return (zend_long) st->st_mtimespec.tv_nsec;
364 : # elif defined(__linux__)
365 14654 : return (zend_long) st->st_mtim.tv_nsec;
366 : # else
367 : return 0;
368 : # endif
369 : }
370 :
371 1132 : static inline void lsp_mkdir_p(zend_string *path)
372 : {
373 : char *buffer, *p;
374 : size_t root_length;
375 :
376 1132 : if (ZSTR_LEN(path) == 0) {
377 0 : return;
378 : }
379 :
380 1132 : buffer = estrndup(ZSTR_VAL(path), ZSTR_LEN(path));
381 1132 : root_length = lsp_path_root_length(buffer, ZSTR_LEN(path));
382 1132 : p = buffer + (root_length > 0 ? root_length : 1);
383 52740 : for (; *p; p++) {
384 51608 : if (lsp_is_path_separator(*p)) {
385 3052 : *p = '\0';
386 3052 : VCWD_MKDIR(buffer, 0777);
387 3052 : *p = ZSTR_VAL(path)[p - buffer];
388 : }
389 : }
390 :
391 1132 : VCWD_MKDIR(buffer, 0777);
392 1132 : efree(buffer);
393 : }
394 :
395 188 : static inline bool lsp_write_string_file(zend_string *path, zend_string *contents)
396 : {
397 : FILE *handle;
398 : size_t written;
399 :
400 188 : handle = fopen(ZSTR_VAL(path), "wb");
401 188 : if (!handle) {
402 0 : return false;
403 : }
404 :
405 188 : written = fwrite(ZSTR_VAL(contents), 1, ZSTR_LEN(contents), handle);
406 188 : fclose(handle);
407 :
408 188 : return written == ZSTR_LEN(contents);
409 : }
410 :
411 2167 : static inline void lsp_sleep_milliseconds(uint32_t milliseconds)
412 : {
413 : #if defined(_WIN32)
414 : Sleep((DWORD) milliseconds);
415 : #else
416 2167 : usleep((unsigned int) milliseconds * 1000u);
417 : #endif
418 2167 : }
419 :
420 : /* Flush gcov counters before _exit(2) in forked workers so coverage runs
421 : * observe code executed in child processes. LSPARROT_COVERAGE is defined by
422 : * the pskel coverage build; everywhere else this is a no-op. A strong
423 : * reference is required because linkers do not extract libgcov.a members
424 : * for weak references. */
425 : #if defined(LSPARROT_COVERAGE)
426 : extern void __gcov_dump(void);
427 : #endif
428 :
429 568 : static inline void lsp_coverage_flush(void)
430 : {
431 : #if defined(LSPARROT_COVERAGE)
432 568 : __gcov_dump();
433 : #endif
434 0 : }
435 :
436 : #endif /* LSP_PORTABILITY_H */
|