1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : adsocket.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include <unistd.h> |
---|
12 | |
---|
13 | #include <climits> |
---|
14 | #include <cstdarg> |
---|
15 | #include <cctype> |
---|
16 | |
---|
17 | #include <netdb.h> |
---|
18 | #include <netinet/tcp.h> |
---|
19 | #include <signal.h> |
---|
20 | #include <sys/mman.h> |
---|
21 | #include <sys/socket.h> |
---|
22 | #include <sys/stat.h> |
---|
23 | #include <sys/time.h> |
---|
24 | #include <sys/un.h> |
---|
25 | |
---|
26 | #if defined(DARWIN) |
---|
27 | # include <sys/sysctl.h> |
---|
28 | #endif // DARWIN |
---|
29 | |
---|
30 | #include <arb_cs.h> |
---|
31 | #include <arb_str.h> |
---|
32 | #include <arb_strbuf.h> |
---|
33 | #include <arb_file.h> |
---|
34 | #include <arb_sleep.h> |
---|
35 | #include <arb_pathlen.h> |
---|
36 | |
---|
37 | #include "gb_comm.h" |
---|
38 | #include "gb_data.h" |
---|
39 | #include "gb_localdata.h" |
---|
40 | |
---|
41 | #include <SigHandler.h> |
---|
42 | |
---|
43 | #include <algorithm> |
---|
44 | #include <arb_misc.h> |
---|
45 | #include <arb_defs.h> |
---|
46 | |
---|
47 | // ------------------------------------------------ |
---|
48 | // private read and write socket functions |
---|
49 | |
---|
50 | void gbcm_read_flush() { |
---|
51 | gb_local->write_ptr = gb_local->write_buffer; |
---|
52 | gb_local->write_free = gb_local->write_bufsize; |
---|
53 | } |
---|
54 | |
---|
55 | static long gbcm_read_buffered(int socket, char *ptr, long size) { |
---|
56 | /* write_ptr ptr to not read data |
---|
57 | write_free = write_bufsize-size of non read data; |
---|
58 | */ |
---|
59 | long holding; |
---|
60 | holding = gb_local->write_bufsize - gb_local->write_free; |
---|
61 | if (holding <= 0) { |
---|
62 | holding = read(socket, gb_local->write_buffer, (size_t)gb_local->write_bufsize); |
---|
63 | |
---|
64 | if (holding < 0) |
---|
65 | { |
---|
66 | fprintf(stderr, "Cannot read data from client: len=%li (%s, errno %i)\n", |
---|
67 | holding, strerror(errno), errno); |
---|
68 | return 0; |
---|
69 | } |
---|
70 | gbcm_read_flush(); |
---|
71 | gb_local->write_free-=holding; |
---|
72 | } |
---|
73 | if (size>holding) size = holding; |
---|
74 | memcpy(ptr, gb_local->write_ptr, (int)size); |
---|
75 | gb_local->write_ptr += size; |
---|
76 | gb_local->write_free += size; |
---|
77 | return size; |
---|
78 | } |
---|
79 | |
---|
80 | long gbcm_read(int socket, char *ptr, long size) { |
---|
81 | long leftsize = size; |
---|
82 | while (leftsize) { |
---|
83 | long readsize = gbcm_read_buffered(socket, ptr, leftsize); |
---|
84 | if (readsize<=0) return 0; |
---|
85 | ptr += readsize; |
---|
86 | leftsize -= readsize; |
---|
87 | } |
---|
88 | |
---|
89 | return size; |
---|
90 | } |
---|
91 | |
---|
92 | GBCM_ServerResult gbcm_write_flush(int socket) { |
---|
93 | long leftsize = gb_local->write_ptr - gb_local->write_buffer; |
---|
94 | ssize_t writesize; |
---|
95 | char *ptr = gb_local->write_buffer; |
---|
96 | |
---|
97 | // once we're done, the buffer will be free |
---|
98 | gb_local->write_free = gb_local->write_bufsize; |
---|
99 | gb_local->write_ptr = gb_local->write_buffer; |
---|
100 | |
---|
101 | while (leftsize) { |
---|
102 | #ifdef MSG_NOSIGNAL |
---|
103 | // Linux has MSG_NOSIGNAL, but not SO_NOSIGPIPE |
---|
104 | // prevent SIGPIPE here |
---|
105 | writesize = send(socket, ptr, leftsize, MSG_NOSIGNAL); |
---|
106 | #else |
---|
107 | writesize = write(socket, ptr, leftsize); |
---|
108 | #endif |
---|
109 | |
---|
110 | if (writesize<0) { |
---|
111 | if (gb_local->iamclient) { |
---|
112 | fprintf(stderr, |
---|
113 | "Client (pid=%i) terminating after failure to contact database (%s).", |
---|
114 | getpid(), strerror(errno)); |
---|
115 | exit(EXIT_SUCCESS); |
---|
116 | } |
---|
117 | else { |
---|
118 | fprintf(stderr, "Error sending data to client (%s).", strerror(errno)); |
---|
119 | return GBCM_SERVER_FAULT; |
---|
120 | } |
---|
121 | } |
---|
122 | ptr += writesize; |
---|
123 | leftsize -= writesize; |
---|
124 | } |
---|
125 | |
---|
126 | return GBCM_SERVER_OK; |
---|
127 | } |
---|
128 | |
---|
129 | GBCM_ServerResult gbcm_write(int socket, const char *ptr, long size) { |
---|
130 | while (size >= gb_local->write_free) { |
---|
131 | memcpy(gb_local->write_ptr, ptr, (int)gb_local->write_free); |
---|
132 | gb_local->write_ptr += gb_local->write_free; |
---|
133 | size -= gb_local->write_free; |
---|
134 | ptr += gb_local->write_free; |
---|
135 | |
---|
136 | gb_local->write_free = 0; |
---|
137 | if (gbcm_write_flush(socket)) return GBCM_SERVER_FAULT; |
---|
138 | } |
---|
139 | memcpy(gb_local->write_ptr, ptr, (int)size); |
---|
140 | gb_local->write_ptr += size; |
---|
141 | gb_local->write_free -= size; |
---|
142 | return GBCM_SERVER_OK; |
---|
143 | } |
---|
144 | |
---|
145 | GB_ERROR gbcm_open_socket(const char *path, bool do_connect, int *psocket, char **unix_name) { |
---|
146 | if (path && strcmp(path, ":") == 0) { |
---|
147 | path = GBS_read_arb_tcp("ARB_DB_SERVER"); |
---|
148 | if (!path) { |
---|
149 | return GB_await_error(); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | return arb_open_socket(path, do_connect, psocket, unix_name); |
---|
154 | } |
---|
155 | |
---|
156 | #if defined(WARN_TODO) |
---|
157 | #warning gbcms_close is unused |
---|
158 | #endif |
---|
159 | long gbcms_close(gbcmc_comm *link) { |
---|
160 | if (link->socket) { |
---|
161 | close(link->socket); |
---|
162 | link->socket = 0; |
---|
163 | if (link->unix_name) { |
---|
164 | unlink(link->unix_name); |
---|
165 | } |
---|
166 | } |
---|
167 | return 0; |
---|
168 | } |
---|
169 | |
---|
170 | gbcmc_comm *gbcmc_open(const char *path) { |
---|
171 | gbcmc_comm *link = ARB_calloc<gbcmc_comm>(1); |
---|
172 | GB_ERROR err = gbcm_open_socket(path, true, &link->socket, &link->unix_name); |
---|
173 | |
---|
174 | if (err) { |
---|
175 | if (link->unix_name) free(link->unix_name); // @@@ |
---|
176 | free(link); |
---|
177 | if (*err) { |
---|
178 | GB_internal_errorf("ARB_DB_CLIENT_OPEN\n(Reason: %s)", err); |
---|
179 | } |
---|
180 | return 0; |
---|
181 | } |
---|
182 | gb_local->iamclient = true; |
---|
183 | return link; |
---|
184 | } |
---|
185 | |
---|
186 | long gbcm_write_two(int socket, long a, long c) { |
---|
187 | long ia[3]; |
---|
188 | ia[0] = a; |
---|
189 | ia[1] = 3; |
---|
190 | ia[2] = c; |
---|
191 | if (!socket) return 1; |
---|
192 | return gbcm_write(socket, (const char *)ia, sizeof(long)*3); |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | GBCM_ServerResult gbcm_read_two(int socket, long a, long *b, long *c) { |
---|
197 | /*! read two values: length and any user long |
---|
198 | * |
---|
199 | * if data is send by gbcm_write_two() then @param b should be zero |
---|
200 | * and is not used! |
---|
201 | */ |
---|
202 | |
---|
203 | long ia[3]; |
---|
204 | long size; |
---|
205 | size = gbcm_read(socket, (char *)&(ia[0]), sizeof(long)*3); |
---|
206 | if (size != sizeof(long) * 3) { |
---|
207 | GB_internal_errorf("receive failed: %zu bytes expected, %li got, keyword %lX", |
---|
208 | sizeof(long) * 3, size, a); |
---|
209 | return GBCM_SERVER_FAULT; |
---|
210 | } |
---|
211 | if (ia[0] != a) { |
---|
212 | GB_internal_errorf("received keyword failed %lx != %lx\n", ia[0], a); |
---|
213 | return GBCM_SERVER_FAULT; |
---|
214 | } |
---|
215 | if (b) { |
---|
216 | *b = ia[1]; |
---|
217 | } |
---|
218 | else { |
---|
219 | if (ia[1]!=3) { |
---|
220 | GB_internal_error("receive failed: size not 3\n"); |
---|
221 | return GBCM_SERVER_FAULT; |
---|
222 | } |
---|
223 | } |
---|
224 | *c = ia[2]; |
---|
225 | return GBCM_SERVER_OK; |
---|
226 | } |
---|
227 | |
---|
228 | GBCM_ServerResult gbcm_write_string(int socket, const char *key) { |
---|
229 | if (key) { |
---|
230 | size_t len = strlen(key); |
---|
231 | gbcm_write_long(socket, len); |
---|
232 | if (len) gbcm_write(socket, key, len); |
---|
233 | } |
---|
234 | else { |
---|
235 | gbcm_write_long(socket, -1); |
---|
236 | } |
---|
237 | return GBCM_SERVER_OK; |
---|
238 | } |
---|
239 | |
---|
240 | char *gbcm_read_string(int socket) |
---|
241 | { |
---|
242 | char *key; |
---|
243 | long len = gbcm_read_long(socket); |
---|
244 | |
---|
245 | if (len) { |
---|
246 | if (len>0) { |
---|
247 | ARB_calloc(key, len+1); |
---|
248 | gbcm_read(socket, key, len); |
---|
249 | } |
---|
250 | else { |
---|
251 | key = 0; |
---|
252 | } |
---|
253 | } |
---|
254 | else { |
---|
255 | key = ARB_strdup(""); |
---|
256 | } |
---|
257 | |
---|
258 | return key; |
---|
259 | } |
---|
260 | |
---|
261 | GBCM_ServerResult gbcm_write_long(int socket, long data) { |
---|
262 | gbcm_write(socket, (char*)&data, sizeof(data)); |
---|
263 | return GBCM_SERVER_OK; |
---|
264 | } |
---|
265 | |
---|
266 | long gbcm_read_long(int socket) { |
---|
267 | long data; |
---|
268 | gbcm_read(socket, (char*)&data, sizeof(data)); |
---|
269 | return data; |
---|
270 | } |
---|
271 | |
---|
272 | char *GB_read_fp(FILE *in) { |
---|
273 | /*! like GB_read_file(), but works on already open file |
---|
274 | * (useful together with GB_fopen_tempfile()) |
---|
275 | * |
---|
276 | * Note: File should be opened in text-mode (e.g. "rt") |
---|
277 | */ |
---|
278 | |
---|
279 | GBS_strstruct *buf = GBS_stropen(4096); |
---|
280 | int c; |
---|
281 | |
---|
282 | while (EOF != (c = getc(in))) { |
---|
283 | GBS_chrcat(buf, c); |
---|
284 | } |
---|
285 | return GBS_strclose(buf); |
---|
286 | } |
---|
287 | |
---|
288 | char *GB_read_file(const char *path) { // consider using class FileContent instead |
---|
289 | /*! read content of file 'path' into string (heap-copy) |
---|
290 | * |
---|
291 | * if path is '-', read from STDIN |
---|
292 | * |
---|
293 | * @return NULL in case of error (use GB_await_error() to get the message) |
---|
294 | */ |
---|
295 | char *result = 0; |
---|
296 | |
---|
297 | if (strcmp(path, "-") == 0) { |
---|
298 | result = GB_read_fp(stdin); |
---|
299 | } |
---|
300 | else { |
---|
301 | char *epath = GBS_eval_env(path); |
---|
302 | |
---|
303 | if (epath) { |
---|
304 | FILE *in = fopen(epath, "rt"); |
---|
305 | |
---|
306 | if (!in) GB_export_error(GB_IO_error("reading", epath)); |
---|
307 | else { |
---|
308 | long data_size = GB_size_of_file(epath); |
---|
309 | |
---|
310 | if (data_size >= 0) { |
---|
311 | result = ARB_alloc<char>(data_size+1); |
---|
312 | |
---|
313 | data_size = fread(result, 1, data_size, in); |
---|
314 | result[data_size] = 0; |
---|
315 | } |
---|
316 | fclose(in); |
---|
317 | } |
---|
318 | } |
---|
319 | free(epath); |
---|
320 | } |
---|
321 | return result; |
---|
322 | } |
---|
323 | |
---|
324 | char *GB_map_FILE(FILE *in, int writeable) { |
---|
325 | int fi = fileno(in); |
---|
326 | size_t size = GB_size_of_FILE(in); |
---|
327 | char *buffer; |
---|
328 | if (size<=0) { |
---|
329 | GB_export_error("GB_map_file: sorry file not found"); |
---|
330 | return NULL; |
---|
331 | } |
---|
332 | if (writeable) { |
---|
333 | buffer = (char*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fi, 0); |
---|
334 | } |
---|
335 | else { |
---|
336 | buffer = (char*)mmap(NULL, size, PROT_READ, MAP_SHARED, fi, 0); |
---|
337 | } |
---|
338 | if (buffer == MAP_FAILED) { |
---|
339 | GB_export_errorf("GB_map_file: Error: Out of Memory: mmap failed (errno: %i)", errno); |
---|
340 | return NULL; |
---|
341 | } |
---|
342 | return buffer; |
---|
343 | } |
---|
344 | |
---|
345 | char *GB_map_file(const char *path, int writeable) { |
---|
346 | FILE *in; |
---|
347 | char *buffer; |
---|
348 | in = fopen(path, "r"); |
---|
349 | if (!in) { |
---|
350 | GB_export_errorf("GB_map_file: sorry file '%s' not readable", path); |
---|
351 | return NULL; |
---|
352 | } |
---|
353 | buffer = GB_map_FILE(in, writeable); |
---|
354 | fclose(in); |
---|
355 | return buffer; |
---|
356 | } |
---|
357 | |
---|
358 | GB_ULONG GB_time_of_day() { |
---|
359 | timeval tp; |
---|
360 | if (gettimeofday(&tp, 0)) return 0; |
---|
361 | return tp.tv_sec; |
---|
362 | } |
---|
363 | |
---|
364 | GB_ERROR GB_textprint(const char *path) { |
---|
365 | // goes to header: __ATTR__USERESULT |
---|
366 | char *fpath = GBS_eval_env(path); |
---|
367 | char *quoted_fpath = GBK_singlequote(fpath); |
---|
368 | const char *command = GBS_global_string("arb_textprint %s &", quoted_fpath); |
---|
369 | GB_ERROR error = GBK_system(command); |
---|
370 | error = GB_failedTo_error("print textfile", fpath, error); |
---|
371 | free(quoted_fpath); |
---|
372 | free(fpath); |
---|
373 | return error; |
---|
374 | } |
---|
375 | |
---|
376 | // -------------------------------------------------------------------------------- |
---|
377 | |
---|
378 | static GB_CSTR GB_getenvPATH() { |
---|
379 | static const char *path = 0; |
---|
380 | if (!path) { |
---|
381 | path = ARB_getenv_ignore_empty("PATH"); |
---|
382 | if (!path) { |
---|
383 | path = GBS_eval_env("/bin:/usr/bin:$(ARBHOME)/bin"); |
---|
384 | GB_informationf("Your PATH variable is empty - using '%s' as search path.", path); |
---|
385 | } |
---|
386 | else { |
---|
387 | char *arbbin = GBS_eval_env("$(ARBHOME)/bin"); |
---|
388 | if (strstr(path, arbbin) == 0) { |
---|
389 | GB_warningf("Your PATH variable does not contain '%s'. Things may not work as expected.", arbbin); |
---|
390 | } |
---|
391 | free(arbbin); |
---|
392 | } |
---|
393 | } |
---|
394 | return path; |
---|
395 | } |
---|
396 | |
---|
397 | // -------------------------------------------------------------------------------- |
---|
398 | // Functions to find an executable |
---|
399 | |
---|
400 | static char *GB_find_executable(GB_CSTR description_of_executable, ...) { |
---|
401 | // goes to header: __ATTR__SENTINEL |
---|
402 | /* search the path for an executable with any of the given names (...) |
---|
403 | * if any is found, it's full path is returned |
---|
404 | * if none is found, a warning call is returned (which can be executed without harm) |
---|
405 | */ |
---|
406 | |
---|
407 | GB_CSTR name; |
---|
408 | char *found = 0; |
---|
409 | va_list args; |
---|
410 | |
---|
411 | va_start(args, description_of_executable); |
---|
412 | while (!found && (name = va_arg(args, GB_CSTR)) != 0) found = ARB_executable(name, GB_getenvPATH()); |
---|
413 | va_end(args); |
---|
414 | |
---|
415 | if (!found) { // none of the executables has been found |
---|
416 | char *looked_for; |
---|
417 | char *msg; |
---|
418 | { |
---|
419 | GBS_strstruct *buf = GBS_stropen(100); |
---|
420 | int first = 1; |
---|
421 | |
---|
422 | va_start(args, description_of_executable); |
---|
423 | while ((name = va_arg(args, GB_CSTR)) != 0) { |
---|
424 | if (!first) GBS_strcat(buf, ", "); |
---|
425 | first = 0; |
---|
426 | GBS_strcat(buf, name); |
---|
427 | } |
---|
428 | va_end(args); |
---|
429 | looked_for = GBS_strclose(buf); |
---|
430 | } |
---|
431 | |
---|
432 | msg = GBS_global_string_copy("Could not find a %s (looked for: %s)", description_of_executable, looked_for); |
---|
433 | GB_warning(msg); |
---|
434 | found = GBS_global_string_copy("echo \"%s\" ; arb_ign Parameters", msg); |
---|
435 | free(msg); |
---|
436 | free(looked_for); |
---|
437 | } |
---|
438 | else { |
---|
439 | GB_informationf("Using %s '%s' ('%s')", description_of_executable, name, found); |
---|
440 | } |
---|
441 | return found; |
---|
442 | } |
---|
443 | |
---|
444 | // -------------------------------------------------------------------------------- |
---|
445 | // Functions to access the environment variables used by ARB: |
---|
446 | |
---|
447 | static char *getenv_executable(GB_CSTR envvar) { |
---|
448 | // get full path of executable defined by 'envvar' |
---|
449 | // returns 0 if |
---|
450 | // - envvar not defined or |
---|
451 | // - not defining an executable (warns about that) |
---|
452 | |
---|
453 | char *result = 0; |
---|
454 | const char *exe_name = ARB_getenv_ignore_empty(envvar); |
---|
455 | |
---|
456 | if (exe_name) { |
---|
457 | result = ARB_executable(exe_name, GB_getenvPATH()); |
---|
458 | if (!result) { |
---|
459 | GB_warningf("Environment variable '%s' contains '%s' (which is not an executable)", envvar, exe_name); |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | return result; |
---|
464 | } |
---|
465 | |
---|
466 | static char *getenv_existing_directory(GB_CSTR envvar) { |
---|
467 | // get full path of directory defined by 'envvar' |
---|
468 | // return 0 if |
---|
469 | // - envvar is not defined or |
---|
470 | // - does not point to a directory (warns about that) |
---|
471 | |
---|
472 | char *result = 0; |
---|
473 | const char *dir_name = ARB_getenv_ignore_empty(envvar); |
---|
474 | |
---|
475 | if (dir_name) { |
---|
476 | if (GB_is_directory(dir_name)) { |
---|
477 | result = ARB_strdup(dir_name); |
---|
478 | } |
---|
479 | else { |
---|
480 | GB_warningf("Environment variable '%s' should contain the path of an existing directory.\n" |
---|
481 | "(current content '%s' has been ignored.)", envvar, dir_name); |
---|
482 | } |
---|
483 | } |
---|
484 | return result; |
---|
485 | } |
---|
486 | |
---|
487 | static void GB_setenv(const char *var, const char *value) { |
---|
488 | if (setenv(var, value, 1) != 0) { |
---|
489 | GB_warningf("Could not set environment variable '%s'. This might cause problems in subprocesses.\n" |
---|
490 | "(Reason: %s)", var, strerror(errno)); |
---|
491 | } |
---|
492 | } |
---|
493 | |
---|
494 | GB_CSTR GB_getenvARB_XTERM() { |
---|
495 | static const char *xterm = 0; |
---|
496 | if (!xterm) { |
---|
497 | xterm = ARB_getenv_ignore_empty("ARB_XTERM"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_XTERM |
---|
498 | if (!xterm) xterm = "xterm -sl 1000 -sb -geometry 150x60"; |
---|
499 | } |
---|
500 | return xterm; |
---|
501 | } |
---|
502 | |
---|
503 | static GB_CSTR GB_getenvARB_XCMD() { |
---|
504 | static const char *xcmd = 0; |
---|
505 | if (!xcmd) { |
---|
506 | xcmd = ARB_getenv_ignore_empty("ARB_XCMD"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_XCMD |
---|
507 | if (!xcmd) { |
---|
508 | const char *xterm = GB_getenvARB_XTERM(); |
---|
509 | gb_assert(xterm); |
---|
510 | xcmd = GBS_global_string_copy("%s -e", xterm); |
---|
511 | } |
---|
512 | } |
---|
513 | return xcmd; |
---|
514 | } |
---|
515 | |
---|
516 | GB_CSTR GB_getenvUSER() { |
---|
517 | static const char *user = 0; |
---|
518 | if (!user) { |
---|
519 | user = ARB_getenv_ignore_empty("USER"); |
---|
520 | if (!user) user = ARB_getenv_ignore_empty("LOGNAME"); |
---|
521 | if (!user) { |
---|
522 | user = ARB_getenv_ignore_empty("HOME"); |
---|
523 | if (user && strrchr(user, '/')) user = strrchr(user, '/')+1; |
---|
524 | } |
---|
525 | if (!user) { |
---|
526 | fprintf(stderr, "WARNING: Cannot identify user: environment variables USER, LOGNAME and HOME not set\n"); |
---|
527 | user = "UnknownUser"; |
---|
528 | } |
---|
529 | } |
---|
530 | return user; |
---|
531 | } |
---|
532 | |
---|
533 | |
---|
534 | static GB_CSTR GB_getenvHOME() { |
---|
535 | static SmartCharPtr Home; |
---|
536 | if (Home.isNull()) { |
---|
537 | char *home = getenv_existing_directory("HOME"); |
---|
538 | if (!home) { |
---|
539 | home = nulldup(GB_getcwd()); |
---|
540 | if (!home) home = ARB_strdup("."); |
---|
541 | fprintf(stderr, "WARNING: Cannot identify user's home directory: environment variable HOME not set\n" |
---|
542 | "Using current directory (%s) as home.\n", home); |
---|
543 | } |
---|
544 | gb_assert(home); |
---|
545 | Home = home; |
---|
546 | } |
---|
547 | return &*Home; |
---|
548 | } |
---|
549 | |
---|
550 | GB_CSTR GB_getenvARBHOME() { |
---|
551 | static SmartCharPtr Arbhome; |
---|
552 | if (Arbhome.isNull()) { |
---|
553 | char *arbhome = getenv_existing_directory("ARBHOME"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARBHOME |
---|
554 | if (!arbhome) { |
---|
555 | fprintf(stderr, "Fatal ERROR: Environment Variable ARBHOME not found !!!\n" |
---|
556 | " Please set 'ARBHOME' to the installation path of ARB\n"); |
---|
557 | exit(EXIT_FAILURE); |
---|
558 | } |
---|
559 | Arbhome = arbhome; |
---|
560 | } |
---|
561 | return &*Arbhome; |
---|
562 | } |
---|
563 | |
---|
564 | GB_CSTR GB_getenvARBMACRO() { |
---|
565 | static const char *am = 0; |
---|
566 | if (!am) { |
---|
567 | am = getenv_existing_directory("ARBMACRO"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARBMACRO |
---|
568 | if (!am) am = ARB_strdup(GB_path_in_ARBLIB("macros")); |
---|
569 | } |
---|
570 | return am; |
---|
571 | } |
---|
572 | |
---|
573 | static char *getenv_autodirectory(const char *envvar, const char *defaultDirectory) { |
---|
574 | // if environment variable 'envvar' contains an existing directory -> use that |
---|
575 | // otherwise fallback to 'defaultDirectory' (create if not existing) |
---|
576 | // return heap-copy of full directory name |
---|
577 | char *dir = getenv_existing_directory(envvar); |
---|
578 | if (!dir) { |
---|
579 | dir = GBS_eval_env(defaultDirectory); |
---|
580 | if (!GB_is_directory(dir)) { |
---|
581 | GB_ERROR error = GB_create_directory(dir); |
---|
582 | if (error) GB_warning(error); |
---|
583 | } |
---|
584 | } |
---|
585 | return dir; |
---|
586 | } |
---|
587 | |
---|
588 | GB_CSTR GB_getenvARB_PROP() { |
---|
589 | static SmartCharPtr ArbProps; |
---|
590 | if (ArbProps.isNull()) ArbProps = getenv_autodirectory("ARB_PROP", GB_path_in_HOME(".arb_prop")); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_PROP |
---|
591 | return &*ArbProps; |
---|
592 | } |
---|
593 | |
---|
594 | GB_CSTR GB_getenvARBMACROHOME() { |
---|
595 | static SmartCharPtr ArbMacroHome; |
---|
596 | if (ArbMacroHome.isNull()) ArbMacroHome = getenv_autodirectory("ARBMACROHOME", GB_path_in_arbprop("macros")); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARBMACROHOME |
---|
597 | return &*ArbMacroHome; |
---|
598 | } |
---|
599 | |
---|
600 | GB_CSTR GB_getenvARBCONFIG() { |
---|
601 | static SmartCharPtr ArbConfig; |
---|
602 | if (ArbConfig.isNull()) ArbConfig = getenv_autodirectory("ARBCONFIG", GB_path_in_arbprop("cfgSave")); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARBCONFIG |
---|
603 | return &*ArbConfig; |
---|
604 | } |
---|
605 | |
---|
606 | GB_CSTR GB_getenvARB_GS() { |
---|
607 | static const char *gs = 0; |
---|
608 | if (!gs) { |
---|
609 | gs = getenv_executable("ARB_GS"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_GS |
---|
610 | if (!gs) gs = GB_find_executable("Postscript viewer", "gv", "ghostview", NULL); |
---|
611 | } |
---|
612 | return gs; |
---|
613 | } |
---|
614 | |
---|
615 | GB_CSTR GB_getenvARB_PDFVIEW() { |
---|
616 | static const char *pdfview = 0; |
---|
617 | if (!pdfview) { |
---|
618 | pdfview = getenv_executable("ARB_PDFVIEW"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_PDFVIEW |
---|
619 | if (!pdfview) pdfview = GB_find_executable("PDF viewer", "epdfview", "xpdf", "kpdf", "acroread", "gv", NULL); |
---|
620 | } |
---|
621 | return pdfview; |
---|
622 | } |
---|
623 | |
---|
624 | GB_CSTR GB_getenvDOCPATH() { |
---|
625 | static const char *dp = 0; |
---|
626 | if (!dp) { |
---|
627 | char *res = getenv_existing_directory("ARB_DOC"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_DOC |
---|
628 | if (res) dp = res; |
---|
629 | else dp = ARB_strdup(GB_path_in_ARBLIB("help")); |
---|
630 | } |
---|
631 | return dp; |
---|
632 | } |
---|
633 | |
---|
634 | GB_CSTR GB_getenvHTMLDOCPATH() { |
---|
635 | static const char *dp = 0; |
---|
636 | if (!dp) { |
---|
637 | char *res = getenv_existing_directory("ARB_HTMLDOC"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_HTMLDOC |
---|
638 | if (res) dp = res; |
---|
639 | else dp = ARB_strdup(GB_path_in_ARBLIB("help_html")); |
---|
640 | } |
---|
641 | return dp; |
---|
642 | |
---|
643 | } |
---|
644 | |
---|
645 | static gb_getenv_hook getenv_hook = NULL; |
---|
646 | |
---|
647 | NOT4PERL gb_getenv_hook GB_install_getenv_hook(gb_getenv_hook hook) { |
---|
648 | // Install 'hook' to be called by GB_getenv(). |
---|
649 | // If the 'hook' returns NULL, normal expansion takes place. |
---|
650 | // Otherwise GB_getenv() returns result from 'hook' |
---|
651 | |
---|
652 | gb_getenv_hook oldHook = getenv_hook; |
---|
653 | getenv_hook = hook; |
---|
654 | return oldHook; |
---|
655 | } |
---|
656 | |
---|
657 | GB_CSTR GB_getenv(const char *env) { |
---|
658 | if (getenv_hook) { |
---|
659 | const char *result = getenv_hook(env); |
---|
660 | if (result) return result; |
---|
661 | } |
---|
662 | if (strncmp(env, "ARB", 3) == 0) { |
---|
663 | // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp |
---|
664 | |
---|
665 | if (strcmp(env, "ARBHOME") == 0) return GB_getenvARBHOME(); |
---|
666 | if (strcmp(env, "ARB_PROP") == 0) return GB_getenvARB_PROP(); |
---|
667 | if (strcmp(env, "ARBCONFIG") == 0) return GB_getenvARBCONFIG(); |
---|
668 | if (strcmp(env, "ARBMACROHOME") == 0) return GB_getenvARBMACROHOME(); |
---|
669 | if (strcmp(env, "ARBMACRO") == 0) return GB_getenvARBMACRO(); |
---|
670 | |
---|
671 | if (strcmp(env, "ARB_GS") == 0) return GB_getenvARB_GS(); |
---|
672 | if (strcmp(env, "ARB_PDFVIEW") == 0) return GB_getenvARB_PDFVIEW(); |
---|
673 | if (strcmp(env, "ARB_DOC") == 0) return GB_getenvDOCPATH(); |
---|
674 | if (strcmp(env, "ARB_XTERM") == 0) return GB_getenvARB_XTERM(); |
---|
675 | if (strcmp(env, "ARB_XCMD") == 0) return GB_getenvARB_XCMD(); |
---|
676 | } |
---|
677 | else { |
---|
678 | if (strcmp(env, "HOME") == 0) return GB_getenvHOME(); |
---|
679 | if (strcmp(env, "USER") == 0) return GB_getenvUSER(); |
---|
680 | } |
---|
681 | |
---|
682 | return ARB_getenv_ignore_empty(env); |
---|
683 | } |
---|
684 | |
---|
685 | struct export_environment { |
---|
686 | export_environment() { |
---|
687 | // set all variables needed in ARB subprocesses |
---|
688 | GB_setenv("ARB_XCMD", GB_getenvARB_XCMD()); |
---|
689 | } |
---|
690 | }; |
---|
691 | |
---|
692 | static export_environment expenv; |
---|
693 | |
---|
694 | bool GB_host_is_local(const char *hostname) { |
---|
695 | // returns true if host is local |
---|
696 | |
---|
697 | arb_assert(hostname); |
---|
698 | arb_assert(hostname[0]); |
---|
699 | |
---|
700 | return |
---|
701 | ARB_stricmp(hostname, "localhost") == 0 || |
---|
702 | ARB_strBeginsWith(hostname, "127.0.0.") || |
---|
703 | ARB_stricmp(hostname, arb_gethostname()) == 0; |
---|
704 | } |
---|
705 | |
---|
706 | static GB_ULONG get_physical_memory() { |
---|
707 | // Returns the physical available memory size in k available for one process |
---|
708 | static GB_ULONG physical_memsize = 0; |
---|
709 | if (!physical_memsize) { |
---|
710 | GB_ULONG memsize; // real existing memory in k |
---|
711 | #if defined(LINUX) |
---|
712 | { |
---|
713 | long pagesize = sysconf(_SC_PAGESIZE); |
---|
714 | long pages = sysconf(_SC_PHYS_PAGES); |
---|
715 | |
---|
716 | memsize = (pagesize/1024) * pages; |
---|
717 | } |
---|
718 | #elif defined(DARWIN) |
---|
719 | #warning memsize detection needs to be tested for Darwin |
---|
720 | { |
---|
721 | int mib[2]; |
---|
722 | uint64_t bytes; |
---|
723 | size_t len; |
---|
724 | |
---|
725 | mib[0] = CTL_HW; |
---|
726 | mib[1] = HW_MEMSIZE; // uint64_t: physical ram size |
---|
727 | len = sizeof(bytes); |
---|
728 | sysctl(mib, 2, &bytes, &len, NULL, 0); |
---|
729 | |
---|
730 | memsize = bytes/1024; |
---|
731 | } |
---|
732 | #else |
---|
733 | memsize = 1024*1024; // assume 1 Gb |
---|
734 | printf("\n" |
---|
735 | "Warning: ARB is not prepared to detect the memory size on your system!\n" |
---|
736 | " (it assumes you have %ul Mb, but does not use more)\n\n", memsize/1024); |
---|
737 | #endif |
---|
738 | |
---|
739 | GB_ULONG net_memsize = memsize - 10240; // reduce by 10Mb |
---|
740 | |
---|
741 | // detect max allocateable memory by ... allocating |
---|
742 | GB_ULONG max_malloc_try = net_memsize*1024; |
---|
743 | GB_ULONG max_malloc = 0; |
---|
744 | { |
---|
745 | GB_ULONG step_size = 4096; |
---|
746 | void *head = 0; |
---|
747 | |
---|
748 | do { |
---|
749 | void **tmp; |
---|
750 | while ((tmp=(void**)malloc(step_size))) { // do NOT use ARB_alloc here! |
---|
751 | *tmp = head; |
---|
752 | head = tmp; |
---|
753 | max_malloc += step_size; |
---|
754 | if (max_malloc >= max_malloc_try) break; |
---|
755 | step_size *= 2; |
---|
756 | } |
---|
757 | } while ((step_size=step_size/2) > sizeof(void*)); |
---|
758 | |
---|
759 | while (head) freeset(head, *(void**)head); |
---|
760 | max_malloc /= 1024; |
---|
761 | } |
---|
762 | |
---|
763 | physical_memsize = std::min(net_memsize, max_malloc); |
---|
764 | |
---|
765 | #if defined(DEBUG) && 0 |
---|
766 | printf("- memsize(real) = %20lu k\n", memsize); |
---|
767 | printf("- memsize(net) = %20lu k\n", net_memsize); |
---|
768 | printf("- memsize(max_malloc) = %20lu k\n", max_malloc); |
---|
769 | #endif // DEBUG |
---|
770 | |
---|
771 | GB_informationf("Visible memory: %s", GBS_readable_size(physical_memsize*1024, "b")); |
---|
772 | } |
---|
773 | |
---|
774 | arb_assert(physical_memsize>0); |
---|
775 | return physical_memsize; |
---|
776 | } |
---|
777 | |
---|
778 | static GB_ULONG parse_env_mem_definition(const char *env_override, GB_ERROR& error) { |
---|
779 | const char *end; |
---|
780 | GB_ULONG num = strtoul(env_override, const_cast<char**>(&end), 10); |
---|
781 | |
---|
782 | error = NULL; |
---|
783 | |
---|
784 | bool valid = num>0 || env_override[0] == '0'; |
---|
785 | if (valid) { |
---|
786 | const char *formatSpec = end; |
---|
787 | double factor = 1; |
---|
788 | |
---|
789 | switch (tolower(formatSpec[0])) { |
---|
790 | case 0: |
---|
791 | num = GB_ULONG(num/1024.0+0.5); // byte->kb |
---|
792 | break; // no format given |
---|
793 | |
---|
794 | case 'g': factor *= 1024; |
---|
795 | case 'm': factor *= 1024; |
---|
796 | case 'k': break; |
---|
797 | |
---|
798 | case '%': |
---|
799 | factor = num/100.0; |
---|
800 | num = get_physical_memory(); |
---|
801 | break; |
---|
802 | |
---|
803 | default: valid = false; break; |
---|
804 | } |
---|
805 | |
---|
806 | if (valid) return GB_ULONG(num*factor+0.5); |
---|
807 | } |
---|
808 | |
---|
809 | error = "expected digits (optionally followed by k, M, G or %)"; |
---|
810 | return 0; |
---|
811 | } |
---|
812 | |
---|
813 | GB_ULONG GB_get_usable_memory() { |
---|
814 | // memory allowed to be used by a single ARB process (in kbyte) |
---|
815 | |
---|
816 | static GB_ULONG useable_memory = 0; |
---|
817 | if (!useable_memory) { |
---|
818 | bool allow_fallback = true; |
---|
819 | const char *env_override = GB_getenv("ARB_MEMORY"); |
---|
820 | const char *via_whom; |
---|
821 | if (env_override) { |
---|
822 | via_whom = "via envar ARB_MEMORY"; |
---|
823 | } |
---|
824 | else { |
---|
825 | FALLBACK: |
---|
826 | env_override = "90%"; // ARB processes do not use more than 90% of physical memory |
---|
827 | via_whom = "by internal default"; |
---|
828 | allow_fallback = false; |
---|
829 | } |
---|
830 | |
---|
831 | gb_assert(env_override); |
---|
832 | |
---|
833 | GB_ERROR env_error; |
---|
834 | GB_ULONG env_memory = parse_env_mem_definition(env_override, env_error); |
---|
835 | if (env_error) { |
---|
836 | GB_warningf("Ignoring invalid setting '%s' %s (%s)", env_override, via_whom, env_error); |
---|
837 | if (allow_fallback) goto FALLBACK; |
---|
838 | GBK_terminate("failed to detect usable memory"); |
---|
839 | } |
---|
840 | |
---|
841 | GB_informationf("Restricting used memory (%s '%s') to %s", via_whom, env_override, GBS_readable_size(env_memory*1024, "b")); |
---|
842 | if (!allow_fallback) { |
---|
843 | GB_informationf("Note: Setting envar ARB_MEMORY will override that restriction (percentage or absolute memsize)"); |
---|
844 | } |
---|
845 | useable_memory = env_memory; |
---|
846 | gb_assert(useable_memory>0 && useable_memory<get_physical_memory()); |
---|
847 | } |
---|
848 | return useable_memory; |
---|
849 | } |
---|
850 | |
---|
851 | // --------------------------- |
---|
852 | // external commands |
---|
853 | |
---|
854 | NOT4PERL GB_ERROR GB_xcmd(const char *cmd, XCMD_TYPE exectype) { |
---|
855 | // goes to header: __ATTR__USERESULT_TODO |
---|
856 | |
---|
857 | // runs a command in an xterm |
---|
858 | |
---|
859 | bool background = exectype & _XCMD__ASYNC; // true -> run asynchronous |
---|
860 | bool wait_only_if_error = !(exectype & _XCMD__WAITKEY); // true -> asynchronous does wait for keypress only if cmd fails |
---|
861 | |
---|
862 | gb_assert(exectype != XCMD_SYNC_WAITKEY); // @@@ previously unused; check how it works and whether that makes sense; fix! |
---|
863 | |
---|
864 | const int BUFSIZE = 1024; |
---|
865 | GBS_strstruct system_call(BUFSIZE); |
---|
866 | |
---|
867 | const char *xcmd = GB_getenvARB_XCMD(); |
---|
868 | |
---|
869 | system_call.put('('); |
---|
870 | system_call.cat(xcmd); |
---|
871 | |
---|
872 | { |
---|
873 | GBS_strstruct bash_command(BUFSIZE); |
---|
874 | |
---|
875 | bash_command.cat("LD_LIBRARY_PATH="); |
---|
876 | { |
---|
877 | char *dquoted_library_path = GBK_doublequote(GB_getenv("LD_LIBRARY_PATH")); |
---|
878 | bash_command.cat(dquoted_library_path); |
---|
879 | free(dquoted_library_path); |
---|
880 | } |
---|
881 | bash_command.cat(";export LD_LIBRARY_PATH; ("); |
---|
882 | bash_command.cat(cmd); |
---|
883 | |
---|
884 | const char *wait_commands = "echo; echo Press ENTER to close this window; read a"; |
---|
885 | if (wait_only_if_error) { |
---|
886 | bash_command.cat(") || ("); |
---|
887 | bash_command.cat(wait_commands); |
---|
888 | } |
---|
889 | else if (background) { |
---|
890 | bash_command.cat("; "); |
---|
891 | bash_command.cat(wait_commands); |
---|
892 | } |
---|
893 | bash_command.put(')'); |
---|
894 | |
---|
895 | system_call.cat(" bash -c "); |
---|
896 | char *squoted_bash_command = GBK_singlequote(bash_command.get_data()); |
---|
897 | system_call.cat(squoted_bash_command); |
---|
898 | free(squoted_bash_command); |
---|
899 | } |
---|
900 | system_call.cat(" )"); |
---|
901 | if (background) system_call.cat(" &"); |
---|
902 | |
---|
903 | return GBK_system(system_call.get_data()); |
---|
904 | } |
---|
905 | |
---|
906 | // --------------------------------------------- |
---|
907 | // path completion (parts former located in AWT) |
---|
908 | // @@@ whole section (+ corresponding tests) should move to adfile.cxx |
---|
909 | |
---|
910 | static int path_toggle = 0; |
---|
911 | static char path_buf[2][ARB_PATH_MAX]; |
---|
912 | |
---|
913 | static char *use_other_path_buf() { |
---|
914 | path_toggle = 1-path_toggle; |
---|
915 | return path_buf[path_toggle]; |
---|
916 | } |
---|
917 | |
---|
918 | GB_CSTR GB_append_suffix(const char *name, const char *suffix) { |
---|
919 | // if suffix != NULL -> append .suffix |
---|
920 | // (automatically removes duplicated '.'s) |
---|
921 | |
---|
922 | GB_CSTR result = name; |
---|
923 | if (suffix) { |
---|
924 | while (suffix[0] == '.') suffix++; |
---|
925 | if (suffix[0]) { |
---|
926 | result = GBS_global_string_to_buffer(use_other_path_buf(), ARB_PATH_MAX, "%s.%s", name, suffix); |
---|
927 | } |
---|
928 | } |
---|
929 | return result; |
---|
930 | } |
---|
931 | |
---|
932 | GB_CSTR GB_canonical_path(const char *anypath) { |
---|
933 | // expands '~' '..' symbolic links etc in 'anypath'. |
---|
934 | // |
---|
935 | // Never returns NULL (if called correctly) |
---|
936 | // Instead might return non-canonical path (when a directory |
---|
937 | // in 'anypath' does not exist) |
---|
938 | |
---|
939 | GB_CSTR result = NULL; |
---|
940 | if (!anypath) { |
---|
941 | GB_export_error("NULL path (internal error)"); |
---|
942 | } |
---|
943 | else if (!anypath[0]) { |
---|
944 | result = "/"; |
---|
945 | } |
---|
946 | else if (strlen(anypath) >= ARB_PATH_MAX) { |
---|
947 | GB_export_errorf("Path too long (> %i chars)", ARB_PATH_MAX-1); |
---|
948 | } |
---|
949 | else { |
---|
950 | if (anypath[0] == '~' && (!anypath[1] || anypath[1] == '/')) { |
---|
951 | GB_CSTR home = GB_getenvHOME(); |
---|
952 | GB_CSTR homeexp = GBS_global_string("%s%s", home, anypath+1); |
---|
953 | result = GB_canonical_path(homeexp); |
---|
954 | GBS_reuse_buffer(homeexp); |
---|
955 | } |
---|
956 | else { |
---|
957 | result = realpath(anypath, path_buf[1-path_toggle]); |
---|
958 | if (result) { |
---|
959 | path_toggle = 1-path_toggle; |
---|
960 | } |
---|
961 | else { // realpath failed (happens e.g. when using a non-existing path, e.g. if user entered the name of a new file) |
---|
962 | // => content of path_buf[path_toggle] is UNDEFINED! |
---|
963 | char *dir, *fullname; |
---|
964 | GB_split_full_path(anypath, &dir, &fullname, NULL, NULL); |
---|
965 | |
---|
966 | const char *canonical_dir = NULL; |
---|
967 | if (!dir) { |
---|
968 | gb_assert(!strchr(anypath, '/')); |
---|
969 | canonical_dir = GB_canonical_path("."); // use working directory |
---|
970 | } |
---|
971 | else { |
---|
972 | gb_assert(strcmp(dir, anypath) != 0); // avoid deadlock |
---|
973 | canonical_dir = GB_canonical_path(dir); |
---|
974 | } |
---|
975 | gb_assert(canonical_dir); |
---|
976 | |
---|
977 | // manually resolve '.' and '..' in non-existing parent directories |
---|
978 | if (strcmp(fullname, "..") == 0) { |
---|
979 | char *parent; |
---|
980 | GB_split_full_path(canonical_dir, &parent, NULL, NULL, NULL); |
---|
981 | if (parent) { |
---|
982 | result = strcpy(use_other_path_buf(), parent); |
---|
983 | free(parent); |
---|
984 | } |
---|
985 | } |
---|
986 | else if (strcmp(fullname, ".") == 0) { |
---|
987 | result = canonical_dir; |
---|
988 | } |
---|
989 | |
---|
990 | if (!result) result = GB_concat_path(canonical_dir, fullname); |
---|
991 | |
---|
992 | free(dir); |
---|
993 | free(fullname); |
---|
994 | } |
---|
995 | } |
---|
996 | gb_assert(result); |
---|
997 | } |
---|
998 | return result; |
---|
999 | } |
---|
1000 | |
---|
1001 | GB_CSTR GB_concat_path(GB_CSTR anypath_left, GB_CSTR anypath_right) { |
---|
1002 | // concats left and right part of a path. |
---|
1003 | // '/' is inserted in-between |
---|
1004 | // |
---|
1005 | // if one of the arguments is NULL => returns the other argument |
---|
1006 | // if both arguments are NULL => return NULL (@@@ maybe forbid?) |
---|
1007 | |
---|
1008 | GB_CSTR result = NULL; |
---|
1009 | |
---|
1010 | if (anypath_right) { |
---|
1011 | if (anypath_right[0] == '/') { |
---|
1012 | result = GB_concat_path(anypath_left, anypath_right+1); |
---|
1013 | } |
---|
1014 | else if (anypath_left && anypath_left[0]) { |
---|
1015 | if (anypath_left[strlen(anypath_left)-1] == '/') { |
---|
1016 | result = GBS_global_string_to_buffer(use_other_path_buf(), sizeof(path_buf[0]), "%s%s", anypath_left, anypath_right); |
---|
1017 | } |
---|
1018 | else { |
---|
1019 | result = GBS_global_string_to_buffer(use_other_path_buf(), sizeof(path_buf[0]), "%s/%s", anypath_left, anypath_right); |
---|
1020 | } |
---|
1021 | } |
---|
1022 | else { |
---|
1023 | result = anypath_right; |
---|
1024 | } |
---|
1025 | } |
---|
1026 | else { |
---|
1027 | result = anypath_left; |
---|
1028 | } |
---|
1029 | |
---|
1030 | return result; |
---|
1031 | } |
---|
1032 | |
---|
1033 | GB_CSTR GB_concat_full_path(const char *anypath_left, const char *anypath_right) { |
---|
1034 | // like GB_concat_path(), but returns the canonical path |
---|
1035 | GB_CSTR result = GB_concat_path(anypath_left, anypath_right); |
---|
1036 | |
---|
1037 | gb_assert(result != anypath_left); // consider using GB_canonical_path() directly |
---|
1038 | gb_assert(result != anypath_right); |
---|
1039 | |
---|
1040 | if (result) result = GB_canonical_path(result); |
---|
1041 | return result; |
---|
1042 | } |
---|
1043 | |
---|
1044 | inline bool is_absolute_path(const char *path) { return path[0] == '/' || path[0] == '~'; } |
---|
1045 | inline bool is_name_of_envvar(const char *name) { |
---|
1046 | for (int i = 0; name[i]; ++i) { |
---|
1047 | if (isalnum(name[i]) || name[i] == '_') continue; |
---|
1048 | return false; |
---|
1049 | } |
---|
1050 | return true; |
---|
1051 | } |
---|
1052 | |
---|
1053 | GB_CSTR GB_unfold_in_directory(const char *relative_directory, const char *path) { |
---|
1054 | // If 'path' is an absolute path, return canonical path. |
---|
1055 | // |
---|
1056 | // Otherwise unfolds relative 'path' using 'relative_directory' as start directory. |
---|
1057 | |
---|
1058 | if (is_absolute_path(path)) return GB_canonical_path(path); |
---|
1059 | return GB_concat_full_path(relative_directory, path); |
---|
1060 | } |
---|
1061 | |
---|
1062 | GB_CSTR GB_unfold_path(const char *pwd_envar, const char *path) { |
---|
1063 | // If 'path' is an absolute path, return canonical path. |
---|
1064 | // |
---|
1065 | // Otherwise unfolds relative 'path' using content of environment |
---|
1066 | // variable 'pwd_envar' as start directory. |
---|
1067 | // If environment variable is not defined, fall-back to current directory |
---|
1068 | |
---|
1069 | gb_assert(is_name_of_envvar(pwd_envar)); |
---|
1070 | if (is_absolute_path(path)) { |
---|
1071 | return GB_canonical_path(path); |
---|
1072 | } |
---|
1073 | |
---|
1074 | const char *pwd = GB_getenv(pwd_envar); |
---|
1075 | if (!pwd) pwd = GB_getcwd(); // @@@ really wanted ? |
---|
1076 | return GB_concat_full_path(pwd, path); |
---|
1077 | } |
---|
1078 | |
---|
1079 | static GB_CSTR GB_path_in_ARBHOME(const char *relative_path_left, const char *anypath_right) { |
---|
1080 | return GB_path_in_ARBHOME(GB_concat_path(relative_path_left, anypath_right)); |
---|
1081 | } |
---|
1082 | |
---|
1083 | GB_CSTR GB_path_in_ARBHOME(const char *relative_path) { |
---|
1084 | return GB_unfold_path("ARBHOME", relative_path); |
---|
1085 | } |
---|
1086 | GB_CSTR GB_path_in_ARBLIB(const char *relative_path) { |
---|
1087 | return GB_path_in_ARBHOME("lib", relative_path); |
---|
1088 | } |
---|
1089 | GB_CSTR GB_path_in_HOME(const char *relative_path) { |
---|
1090 | return GB_unfold_path("HOME", relative_path); |
---|
1091 | } |
---|
1092 | GB_CSTR GB_path_in_arbprop(const char *relative_path) { |
---|
1093 | return GB_unfold_path("ARB_PROP", relative_path); |
---|
1094 | } |
---|
1095 | GB_CSTR GB_path_in_ARBLIB(const char *relative_path_left, const char *anypath_right) { |
---|
1096 | return GB_path_in_ARBLIB(GB_concat_path(relative_path_left, anypath_right)); |
---|
1097 | } |
---|
1098 | GB_CSTR GB_path_in_arb_temp(const char *relative_path) { |
---|
1099 | return GB_path_in_HOME(GB_concat_path(".arb_tmp", relative_path)); |
---|
1100 | } |
---|
1101 | |
---|
1102 | #define GB_PATH_TMP GB_path_in_arb_temp("tmp") // = "~/.arb_tmp/tmp" (used wherever '/tmp' was used in the past) |
---|
1103 | |
---|
1104 | FILE *GB_fopen_tempfile(const char *filename, const char *fmode, char **res_fullname) { |
---|
1105 | // fopens a tempfile |
---|
1106 | // |
---|
1107 | // Returns |
---|
1108 | // - NULL in case of error (which is exported then) |
---|
1109 | // - otherwise returns open filehandle |
---|
1110 | // |
---|
1111 | // Always sets |
---|
1112 | // - heap-copy of used filename in 'res_fullname' (if res_fullname != NULL) |
---|
1113 | // (even if fopen failed) |
---|
1114 | |
---|
1115 | char *file = ARB_strdup(GB_concat_path(GB_PATH_TMP, filename)); |
---|
1116 | GB_ERROR error = GB_create_parent_directory(file); |
---|
1117 | FILE *fp = NULL; |
---|
1118 | |
---|
1119 | if (!error) { |
---|
1120 | bool write = strpbrk(fmode, "wa") != 0; |
---|
1121 | |
---|
1122 | fp = fopen(file, fmode); |
---|
1123 | if (fp) { |
---|
1124 | // make file private |
---|
1125 | if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) != 0) { |
---|
1126 | error = GB_IO_error("changing permissions of", file); |
---|
1127 | } |
---|
1128 | } |
---|
1129 | else { |
---|
1130 | error = GB_IO_error(GBS_global_string("opening(%s) tempfile", write ? "write" : "read"), file); |
---|
1131 | } |
---|
1132 | |
---|
1133 | if (res_fullname) { |
---|
1134 | *res_fullname = file ? ARB_strdup(file) : 0; |
---|
1135 | } |
---|
1136 | } |
---|
1137 | |
---|
1138 | if (error) { |
---|
1139 | // don't care if anything fails here.. |
---|
1140 | if (fp) { fclose(fp); fp = 0; } |
---|
1141 | if (file) unlink(file); |
---|
1142 | GB_export_error(error); |
---|
1143 | } |
---|
1144 | |
---|
1145 | free(file); |
---|
1146 | |
---|
1147 | return fp; |
---|
1148 | } |
---|
1149 | |
---|
1150 | char *GB_create_tempfile(const char *name) { |
---|
1151 | // creates a tempfile and returns full name of created file |
---|
1152 | // returns NULL in case of error (which is exported then) |
---|
1153 | |
---|
1154 | char *fullname; |
---|
1155 | FILE *out = GB_fopen_tempfile(name, "wt", &fullname); |
---|
1156 | |
---|
1157 | if (out) fclose(out); |
---|
1158 | return fullname; |
---|
1159 | } |
---|
1160 | |
---|
1161 | char *GB_unique_filename(const char *name_prefix, const char *suffix) { |
---|
1162 | // generates a unique (enough) filename |
---|
1163 | // |
---|
1164 | // scheme: name_prefix_USER_PID_COUNT.suffix |
---|
1165 | |
---|
1166 | static int counter = 0; |
---|
1167 | return GBS_global_string_copy("%s_%s_%i_%i.%s", |
---|
1168 | name_prefix, |
---|
1169 | GB_getenvUSER(), getpid(), counter++, |
---|
1170 | suffix); |
---|
1171 | } |
---|
1172 | |
---|
1173 | static GB_HASH *files_to_remove_on_exit = 0; |
---|
1174 | static long exit_remove_file(const char *file, long, void *) { |
---|
1175 | if (unlink(file) != 0) { |
---|
1176 | fprintf(stderr, "Warning: %s\n", GB_IO_error("removing", file)); |
---|
1177 | } |
---|
1178 | return 0; |
---|
1179 | } |
---|
1180 | static void exit_removal() { |
---|
1181 | if (files_to_remove_on_exit) { |
---|
1182 | GBS_hash_do_loop(files_to_remove_on_exit, exit_remove_file, NULL); |
---|
1183 | GBS_free_hash(files_to_remove_on_exit); |
---|
1184 | files_to_remove_on_exit = NULL; |
---|
1185 | } |
---|
1186 | } |
---|
1187 | void GB_remove_on_exit(const char *filename) { |
---|
1188 | // mark a file for removal on exit |
---|
1189 | |
---|
1190 | if (!files_to_remove_on_exit) { |
---|
1191 | files_to_remove_on_exit = GBS_create_hash(20, GB_MIND_CASE); |
---|
1192 | GB_atexit(exit_removal); |
---|
1193 | } |
---|
1194 | GBS_write_hash(files_to_remove_on_exit, filename, 1); |
---|
1195 | } |
---|
1196 | |
---|
1197 | void GB_split_full_path(const char *fullpath, char **res_dir, char **res_fullname, char **res_name_only, char **res_suffix) { |
---|
1198 | // Takes a file (or directory) name and splits it into "path/name.suffix". |
---|
1199 | // If result pointers (res_*) are non-NULL, they are assigned heap-copies of the split parts. |
---|
1200 | // If parts are not valid (e.g. cause 'fullpath' doesn't have a .suffix) the corresponding result pointer |
---|
1201 | // is set to NULL. |
---|
1202 | // |
---|
1203 | // The '/' and '.' characters at the split-positions will be removed (not included in the results-strings). |
---|
1204 | // Exceptions: |
---|
1205 | // - the '.' in 'res_fullname' |
---|
1206 | // - the '/' if directory part is the rootdir |
---|
1207 | // |
---|
1208 | // Note: |
---|
1209 | // - if the filename starts with '.' (and that is the only '.' in the filename, an empty filename is returned: "") |
---|
1210 | |
---|
1211 | if (fullpath && fullpath[0]) { |
---|
1212 | const char *lslash = strrchr(fullpath, '/'); |
---|
1213 | const char *name_start = lslash ? lslash+1 : fullpath; |
---|
1214 | const char *ldot = strrchr(lslash ? lslash : fullpath, '.'); |
---|
1215 | const char *terminal = strchr(name_start, 0); |
---|
1216 | |
---|
1217 | gb_assert(terminal); |
---|
1218 | gb_assert(name_start); |
---|
1219 | gb_assert(terminal > fullpath); // ensure (terminal-1) is a valid character position in path |
---|
1220 | |
---|
1221 | if (!lslash && fullpath[0] == '.' && (fullpath[1] == 0 || (fullpath[1] == '.' && fullpath[2] == 0))) { // '.' and '..' |
---|
1222 | if (res_dir) *res_dir = ARB_strdup(fullpath); |
---|
1223 | if (res_fullname) *res_fullname = NULL; |
---|
1224 | if (res_name_only) *res_name_only = NULL; |
---|
1225 | if (res_suffix) *res_suffix = NULL; |
---|
1226 | } |
---|
1227 | else { |
---|
1228 | if (res_dir) *res_dir = lslash ? ARB_strpartdup(fullpath, lslash == fullpath ? lslash : lslash-1) : NULL; |
---|
1229 | if (res_fullname) *res_fullname = ARB_strpartdup(name_start, terminal-1); |
---|
1230 | if (res_name_only) *res_name_only = ARB_strpartdup(name_start, ldot ? ldot-1 : terminal-1); |
---|
1231 | if (res_suffix) *res_suffix = ldot ? ARB_strpartdup(ldot+1, terminal-1) : NULL; |
---|
1232 | } |
---|
1233 | } |
---|
1234 | else { |
---|
1235 | if (res_dir) *res_dir = NULL; |
---|
1236 | if (res_fullname) *res_fullname = NULL; |
---|
1237 | if (res_name_only) *res_name_only = NULL; |
---|
1238 | if (res_suffix) *res_suffix = NULL; |
---|
1239 | } |
---|
1240 | } |
---|
1241 | |
---|
1242 | |
---|
1243 | // -------------------------------------------------------------------------------- |
---|
1244 | |
---|
1245 | #ifdef UNIT_TESTS |
---|
1246 | |
---|
1247 | #include <test_unit.h> |
---|
1248 | |
---|
1249 | #define TEST_EXPECT_IS_CANONICAL(file) \ |
---|
1250 | do { \ |
---|
1251 | char *dup = ARB_strdup(file); \ |
---|
1252 | TEST_EXPECT_EQUAL(GB_canonical_path(dup), dup); \ |
---|
1253 | free(dup); \ |
---|
1254 | } while(0) |
---|
1255 | |
---|
1256 | #define TEST_EXPECT_CANONICAL_TO(not_cano,cano) \ |
---|
1257 | do { \ |
---|
1258 | char *arb_not_cano = ARB_strdup(GB_concat_path(arbhome, not_cano)); \ |
---|
1259 | char *arb_cano = ARB_strdup(GB_concat_path(arbhome, cano)); \ |
---|
1260 | TEST_EXPECT_EQUAL(GB_canonical_path(arb_not_cano), arb_cano); \ |
---|
1261 | free(arb_cano); \ |
---|
1262 | free(arb_not_cano); \ |
---|
1263 | } while (0) |
---|
1264 | |
---|
1265 | static arb_test::match_expectation path_splits_into(const char *path, const char *Edir, const char *Enameext, const char *Ename, const char *Eext) { |
---|
1266 | using namespace arb_test; |
---|
1267 | expectation_group expected; |
---|
1268 | |
---|
1269 | char *Sdir,*Snameext,*Sname,*Sext; |
---|
1270 | GB_split_full_path(path, &Sdir, &Snameext, &Sname, &Sext); |
---|
1271 | |
---|
1272 | expected.add(that(Sdir).is_equal_to(Edir)); |
---|
1273 | expected.add(that(Snameext).is_equal_to(Enameext)); |
---|
1274 | expected.add(that(Sname).is_equal_to(Ename)); |
---|
1275 | expected.add(that(Sext).is_equal_to(Eext)); |
---|
1276 | |
---|
1277 | free(Sdir); |
---|
1278 | free(Snameext); |
---|
1279 | free(Sname); |
---|
1280 | free(Sext); |
---|
1281 | |
---|
1282 | return all().ofgroup(expected); |
---|
1283 | } |
---|
1284 | |
---|
1285 | #define TEST_EXPECT_PATH_SPLITS_INTO(path,dir,nameext,name,ext) TEST_EXPECTATION(path_splits_into(path,dir,nameext,name,ext)) |
---|
1286 | #define TEST_EXPECT_PATH_SPLITS_INTO__BROKEN(path,dir,nameext,name,ext) TEST_EXPECTATION__BROKEN(path_splits_into(path,dir,nameext,name,ext)) |
---|
1287 | |
---|
1288 | static arb_test::match_expectation path_splits_reversible(const char *path) { |
---|
1289 | using namespace arb_test; |
---|
1290 | expectation_group expected; |
---|
1291 | |
---|
1292 | char *Sdir,*Snameext,*Sname,*Sext; |
---|
1293 | GB_split_full_path(path, &Sdir, &Snameext, &Sname, &Sext); |
---|
1294 | |
---|
1295 | expected.add(that(GB_append_suffix(Sname, Sext)).is_equal_to(Snameext)); // GB_append_suffix should reverse name.ext-split |
---|
1296 | expected.add(that(GB_concat_path(Sdir, Snameext)).is_equal_to(path)); // GB_concat_path should reverse dir/file-split |
---|
1297 | |
---|
1298 | free(Sdir); |
---|
1299 | free(Snameext); |
---|
1300 | free(Sname); |
---|
1301 | free(Sext); |
---|
1302 | |
---|
1303 | return all().ofgroup(expected); |
---|
1304 | } |
---|
1305 | |
---|
1306 | #define TEST_SPLIT_REVERSIBILITY(path) TEST_EXPECTATION(path_splits_reversible(path)) |
---|
1307 | #define TEST_SPLIT_REVERSIBILITY__BROKEN(path) TEST_EXPECTATION__BROKEN(path_splits_reversible(path)) |
---|
1308 | |
---|
1309 | void TEST_paths() { |
---|
1310 | // test GB_concat_path |
---|
1311 | TEST_EXPECT_EQUAL(GB_concat_path("a", NULL), "a"); |
---|
1312 | TEST_EXPECT_EQUAL(GB_concat_path(NULL, "b"), "b"); |
---|
1313 | TEST_EXPECT_EQUAL(GB_concat_path("a", "b"), "a/b"); |
---|
1314 | |
---|
1315 | TEST_EXPECT_EQUAL(GB_concat_path("/", "test.fig"), "/test.fig"); |
---|
1316 | |
---|
1317 | // test GB_split_full_path |
---|
1318 | TEST_EXPECT_PATH_SPLITS_INTO("dir/sub/.ext", "dir/sub", ".ext", "", "ext"); |
---|
1319 | TEST_EXPECT_PATH_SPLITS_INTO("/root/sub/file.notext.ext", "/root/sub", "file.notext.ext", "file.notext", "ext"); |
---|
1320 | |
---|
1321 | TEST_EXPECT_PATH_SPLITS_INTO("./file.ext", ".", "file.ext", "file", "ext"); |
---|
1322 | TEST_EXPECT_PATH_SPLITS_INTO("/file", "/", "file", "file", NULL); |
---|
1323 | TEST_EXPECT_PATH_SPLITS_INTO(".", ".", NULL, NULL, NULL); |
---|
1324 | |
---|
1325 | // test reversibility of GB_split_full_path and GB_concat_path/GB_append_suffix |
---|
1326 | { |
---|
1327 | const char *prefix[] = { |
---|
1328 | "", |
---|
1329 | "dir/", |
---|
1330 | "dir/sub/", |
---|
1331 | "/dir/", |
---|
1332 | "/dir/sub/", |
---|
1333 | "/", |
---|
1334 | "./", |
---|
1335 | "../", |
---|
1336 | }; |
---|
1337 | |
---|
1338 | for (size_t d = 0; d<ARRAY_ELEMS(prefix); ++d) { |
---|
1339 | TEST_ANNOTATE(GBS_global_string("prefix='%s'", prefix[d])); |
---|
1340 | |
---|
1341 | TEST_SPLIT_REVERSIBILITY(GBS_global_string("%sfile.ext", prefix[d])); |
---|
1342 | TEST_SPLIT_REVERSIBILITY(GBS_global_string("%sfile", prefix[d])); |
---|
1343 | TEST_SPLIT_REVERSIBILITY(GBS_global_string("%s.ext", prefix[d])); |
---|
1344 | if (prefix[d][0]) { // empty string "" reverts to NULL |
---|
1345 | TEST_SPLIT_REVERSIBILITY(prefix[d]); |
---|
1346 | } |
---|
1347 | } |
---|
1348 | } |
---|
1349 | |
---|
1350 | // GB_canonical_path basics |
---|
1351 | TEST_EXPECT_CONTAINS(GB_canonical_path("./bla"), "UNIT_TESTER/run/bla"); |
---|
1352 | TEST_EXPECT_CONTAINS(GB_canonical_path("bla"), "UNIT_TESTER/run/bla"); |
---|
1353 | |
---|
1354 | { |
---|
1355 | char *arbhome = ARB_strdup(GB_getenvARBHOME()); |
---|
1356 | const char* nosuchfile = "nosuchfile"; |
---|
1357 | const char* somefile = "arb_README.txt"; |
---|
1358 | |
---|
1359 | char *somefile_in_arbhome = ARB_strdup(GB_concat_path(arbhome, somefile)); |
---|
1360 | char *nosuchfile_in_arbhome = ARB_strdup(GB_concat_path(arbhome, nosuchfile)); |
---|
1361 | char *nosuchpath_in_arbhome = ARB_strdup(GB_concat_path(arbhome, "nosuchpath")); |
---|
1362 | char *somepath_in_arbhome = ARB_strdup(GB_concat_path(arbhome, "lib")); |
---|
1363 | char *file_in_nosuchpath = ARB_strdup(GB_concat_path(nosuchpath_in_arbhome, "whatever")); |
---|
1364 | |
---|
1365 | TEST_REJECT(GB_is_directory(nosuchpath_in_arbhome)); |
---|
1366 | |
---|
1367 | // test GB_get_full_path |
---|
1368 | TEST_EXPECT_IS_CANONICAL(somefile_in_arbhome); |
---|
1369 | TEST_EXPECT_IS_CANONICAL(nosuchpath_in_arbhome); |
---|
1370 | TEST_EXPECT_IS_CANONICAL(file_in_nosuchpath); |
---|
1371 | |
---|
1372 | TEST_EXPECT_IS_CANONICAL("/sbin"); // existing (most likely) |
---|
1373 | #if !defined(DARWIN) |
---|
1374 | // TEST_DISABLED_OSX: fails for darwin on jenkins (/tmp seems to be a symbolic link there) |
---|
1375 | TEST_EXPECT_IS_CANONICAL("/tmp/arbtest.fig"); |
---|
1376 | #endif |
---|
1377 | TEST_EXPECT_IS_CANONICAL("/arbtest.fig"); // not existing (most likely) |
---|
1378 | |
---|
1379 | TEST_EXPECT_CANONICAL_TO("./PARSIMONY/./../ARBDB/./arbdb.h", "ARBDB/arbdb.h"); // test parent-path |
---|
1380 | TEST_EXPECT_CANONICAL_TO("INCLUDE/arbdb.h", "ARBDB/arbdb.h"); // test symbolic link to file |
---|
1381 | TEST_EXPECT_CANONICAL_TO("NAMES_COM/AISC/aisc.pa", "AISC_COM/AISC/aisc.pa"); // test symbolic link to directory |
---|
1382 | TEST_EXPECT_CANONICAL_TO("./NAMES_COM/AISC/..", "AISC_COM"); // test parent-path through links |
---|
1383 | |
---|
1384 | TEST_EXPECT_CANONICAL_TO("./PARSIMONY/./../ARBDB/../nosuchpath", "nosuchpath"); // nosuchpath does not exist, but involved parent dirs do |
---|
1385 | // test resolving of non-existent parent dirs: |
---|
1386 | TEST_EXPECT_CANONICAL_TO("./PARSIMONY/./../nosuchpath/../ARBDB", "ARBDB"); |
---|
1387 | TEST_EXPECT_CANONICAL_TO("./nosuchpath/./../ARBDB", "ARBDB"); |
---|
1388 | |
---|
1389 | // test GB_unfold_path |
---|
1390 | TEST_EXPECT_EQUAL(GB_unfold_path("ARBHOME", somefile), somefile_in_arbhome); |
---|
1391 | TEST_EXPECT_EQUAL(GB_unfold_path("ARBHOME", nosuchfile), nosuchfile_in_arbhome); |
---|
1392 | |
---|
1393 | char *inhome = ARB_strdup(GB_unfold_path("HOME", "whatever")); |
---|
1394 | TEST_EXPECT_EQUAL(inhome, GB_canonical_path("~/whatever")); |
---|
1395 | free(inhome); |
---|
1396 | |
---|
1397 | // test GB_unfold_in_directory |
---|
1398 | TEST_EXPECT_EQUAL(GB_unfold_in_directory(arbhome, somefile), somefile_in_arbhome); |
---|
1399 | TEST_EXPECT_EQUAL(GB_unfold_in_directory(nosuchpath_in_arbhome, somefile_in_arbhome), somefile_in_arbhome); |
---|
1400 | TEST_EXPECT_EQUAL(GB_unfold_in_directory(arbhome, nosuchfile), nosuchfile_in_arbhome); |
---|
1401 | TEST_EXPECT_EQUAL(GB_unfold_in_directory(nosuchpath_in_arbhome, "whatever"), file_in_nosuchpath); |
---|
1402 | TEST_EXPECT_EQUAL(GB_unfold_in_directory(somepath_in_arbhome, "../nosuchfile"), nosuchfile_in_arbhome); |
---|
1403 | |
---|
1404 | // test unfolding absolute paths (HOME is ignored) |
---|
1405 | TEST_EXPECT_EQUAL(GB_unfold_path("HOME", arbhome), arbhome); |
---|
1406 | TEST_EXPECT_EQUAL(GB_unfold_path("HOME", somefile_in_arbhome), somefile_in_arbhome); |
---|
1407 | TEST_EXPECT_EQUAL(GB_unfold_path("HOME", nosuchfile_in_arbhome), nosuchfile_in_arbhome); |
---|
1408 | |
---|
1409 | // test GB_path_in_ARBHOME |
---|
1410 | TEST_EXPECT_EQUAL(GB_path_in_ARBHOME(somefile), somefile_in_arbhome); |
---|
1411 | TEST_EXPECT_EQUAL(GB_path_in_ARBHOME(nosuchfile), nosuchfile_in_arbhome); |
---|
1412 | |
---|
1413 | free(file_in_nosuchpath); |
---|
1414 | free(somepath_in_arbhome); |
---|
1415 | free(nosuchpath_in_arbhome); |
---|
1416 | free(nosuchfile_in_arbhome); |
---|
1417 | free(somefile_in_arbhome); |
---|
1418 | free(arbhome); |
---|
1419 | } |
---|
1420 | |
---|
1421 | TEST_EXPECT_EQUAL(GB_path_in_ARBLIB("help"), GB_path_in_ARBHOME("lib", "help")); |
---|
1422 | |
---|
1423 | } |
---|
1424 | |
---|
1425 | // ---------------------------------------- |
---|
1426 | |
---|
1427 | class TestFile : virtual Noncopyable { |
---|
1428 | const char *name; |
---|
1429 | bool open(const char *mode) { |
---|
1430 | FILE *out = fopen(name, mode); |
---|
1431 | if (out) fclose(out); |
---|
1432 | return out; |
---|
1433 | } |
---|
1434 | void create() { ASSERT_RESULT(bool, true, open("w")); } |
---|
1435 | void unlink() { ::unlink(name); } |
---|
1436 | public: |
---|
1437 | TestFile(const char *name_) : name(name_) { create(); } |
---|
1438 | ~TestFile() { if (exists()) unlink(); } |
---|
1439 | const char *get_name() const { return name; } |
---|
1440 | bool exists() { return open("r"); } |
---|
1441 | }; |
---|
1442 | |
---|
1443 | void TEST_GB_remove_on_exit() { |
---|
1444 | { |
---|
1445 | // first test class TestFile |
---|
1446 | TestFile file("test1"); |
---|
1447 | TEST_EXPECT(file.exists()); |
---|
1448 | TEST_EXPECT(TestFile(file.get_name()).exists()); // removes the file |
---|
1449 | TEST_REJECT(file.exists()); |
---|
1450 | } |
---|
1451 | |
---|
1452 | TestFile t("test1"); |
---|
1453 | { |
---|
1454 | GB_shell shell; |
---|
1455 | GBDATA *gb_main = GB_open("no.arb", "c"); |
---|
1456 | |
---|
1457 | GB_remove_on_exit(t.get_name()); |
---|
1458 | GB_close(gb_main); |
---|
1459 | } |
---|
1460 | TEST_REJECT(t.exists()); |
---|
1461 | } |
---|
1462 | |
---|
1463 | void TEST_some_paths() { |
---|
1464 | gb_getenv_hook old = GB_install_getenv_hook(arb_test::fakeenv); |
---|
1465 | { |
---|
1466 | // ../UNIT_TESTER/run/homefake |
---|
1467 | |
---|
1468 | TEST_EXPECT_CONTAINS__BROKEN(GB_getenvHOME(), "/UNIT_TESTER/run/homefake"); // GB_getenvHOME() ignores the hook |
---|
1469 | // @@@ this is a general problem - unit tested code cannot use GB_getenvHOME() w/o problems |
---|
1470 | |
---|
1471 | TEST_EXPECT_CONTAINS(GB_getenvARB_PROP(), "/UNIT_TESTER/run/homefake/.arb_prop"); |
---|
1472 | TEST_EXPECT_CONTAINS(GB_getenvARBMACRO(), "/lib/macros"); |
---|
1473 | |
---|
1474 | TEST_EXPECT_CONTAINS(GB_getenvARBCONFIG(), "/UNIT_TESTER/run/homefake/.arb_prop/cfgSave"); |
---|
1475 | TEST_EXPECT_CONTAINS(GB_getenvARBMACROHOME(), "/UNIT_TESTER/run/homefake/.arb_prop/macros"); // works in [11068] |
---|
1476 | } |
---|
1477 | TEST_EXPECT_EQUAL((void*)arb_test::fakeenv, (void*)GB_install_getenv_hook(old)); |
---|
1478 | } |
---|
1479 | |
---|
1480 | #endif // UNIT_TESTS |
---|
1481 | |
---|