]>
git.proxmox.com Git - libgit2.git/blob - tests/clar/sandbox.h
2114819e4e77493bf0cf36952dc9883c3b4fea64
2 #include <sys/syslimits.h>
5 static char _clar_path
[4096];
8 is_valid_tmp_path(const char *path
)
12 if (stat(path
, &st
) != 0)
15 if (!S_ISDIR(st
.st_mode
))
18 return (access(path
, W_OK
) == 0);
22 find_tmp_path(char *buffer
, size_t length
)
25 static const size_t var_count
= 5;
26 static const char *env_vars
[] = {
27 "CLAR_TMP", "TMPDIR", "TMP", "TEMP", "USERPROFILE"
32 for (i
= 0; i
< var_count
; ++i
) {
33 const char *env
= getenv(env_vars
[i
]);
37 if (is_valid_tmp_path(env
)) {
39 if (length
>= PATH_MAX
&& realpath(env
, buffer
) != NULL
)
42 strncpy(buffer
, env
, length
);
47 /* If the environment doesn't say anything, try to use /tmp */
48 if (is_valid_tmp_path("/tmp")) {
50 if (length
>= PATH_MAX
&& realpath("/tmp", buffer
) != NULL
)
53 strncpy(buffer
, "/tmp", length
);
58 DWORD env_len
= GetEnvironmentVariable("CLAR_TMP", buffer
, (DWORD
)length
);
59 if (env_len
> 0 && env_len
< (DWORD
)length
)
62 if (GetTempPath((DWORD
)length
, buffer
))
66 /* This system doesn't like us, try to use the current directory */
67 if (is_valid_tmp_path(".")) {
68 strncpy(buffer
, ".", length
);
75 static void clar_unsandbox(void)
77 if (_clar_path
[0] == '\0')
80 cl_must_pass(chdir(".."));
85 static int build_sandbox_path(void)
88 const char path_tail
[] = CLAR_TMPDIR
"_XXXXXX";
90 const char path_tail
[] = "clar_tmp_XXXXXX";
95 if (find_tmp_path(_clar_path
, sizeof(_clar_path
)) < 0)
98 len
= strlen(_clar_path
);
101 { /* normalize path to POSIX forward slashes */
103 for (i
= 0; i
< len
; ++i
) {
104 if (_clar_path
[i
] == '\\')
110 if (_clar_path
[len
- 1] != '/') {
111 _clar_path
[len
++] = '/';
114 strncpy(_clar_path
+ len
, path_tail
, sizeof(_clar_path
) - len
);
116 #if defined(__MINGW32__)
117 if (_mktemp(_clar_path
) == NULL
)
120 if (mkdir(_clar_path
, 0700) != 0)
122 #elif defined(_WIN32)
123 if (_mktemp_s(_clar_path
, sizeof(_clar_path
)) != 0)
126 if (mkdir(_clar_path
, 0700) != 0)
129 if (mkdtemp(_clar_path
) == NULL
)
136 static int clar_sandbox(void)
138 if (_clar_path
[0] == '\0' && build_sandbox_path() < 0)
141 if (chdir(_clar_path
) != 0)
147 const char *clar_sandbox_path(void)