]>
git.proxmox.com Git - libgit2.git/blob - tests/clar/sandbox.h
1 static char _clar_path
[4096];
4 is_valid_tmp_path(const char *path
)
8 if (stat(path
, &st
) != 0)
11 if (!S_ISDIR(st
.st_mode
))
14 return (access(path
, W_OK
) == 0);
18 find_tmp_path(char *buffer
, size_t length
)
21 static const size_t var_count
= 5;
22 static const char *env_vars
[] = {
23 "CLAR_TMP", "TMPDIR", "TMP", "TEMP", "USERPROFILE"
28 for (i
= 0; i
< var_count
; ++i
) {
29 const char *env
= getenv(env_vars
[i
]);
33 if (is_valid_tmp_path(env
)) {
34 strncpy(buffer
, env
, length
);
39 /* If the environment doesn't say anything, try to use /tmp */
40 if (is_valid_tmp_path("/tmp")) {
41 strncpy(buffer
, "/tmp", length
);
46 DWORD env_len
= GetEnvironmentVariable("CLAR_TMP", buffer
, (DWORD
)length
);
47 if (env_len
> 0 && env_len
< (DWORD
)length
)
50 if (GetTempPath((DWORD
)length
, buffer
))
54 /* This system doesn't like us, try to use the current directory */
55 if (is_valid_tmp_path(".")) {
56 strncpy(buffer
, ".", length
);
63 static void clar_unsandbox(void)
65 if (_clar_path
[0] == '\0')
68 cl_must_pass(chdir(".."));
73 static int build_sandbox_path(void)
76 const char path_tail
[] = CLAR_TMPDIR
"_XXXXXX";
78 const char path_tail
[] = "clar_tmp_XXXXXX";
83 if (find_tmp_path(_clar_path
, sizeof(_clar_path
)) < 0)
86 len
= strlen(_clar_path
);
89 { /* normalize path to POSIX forward slashes */
91 for (i
= 0; i
< len
; ++i
) {
92 if (_clar_path
[i
] == '\\')
98 if (_clar_path
[len
- 1] != '/') {
99 _clar_path
[len
++] = '/';
102 strncpy(_clar_path
+ len
, path_tail
, sizeof(_clar_path
) - len
);
104 #if defined(__MINGW32__)
105 if (_mktemp(_clar_path
) == NULL
)
108 if (mkdir(_clar_path
, 0700) != 0)
110 #elif defined(_WIN32)
111 if (_mktemp_s(_clar_path
, sizeof(_clar_path
)) != 0)
114 if (mkdir(_clar_path
, 0700) != 0)
117 if (mkdtemp(_clar_path
) == NULL
)
124 static int clar_sandbox(void)
126 if (_clar_path
[0] == '\0' && build_sandbox_path() < 0)
129 if (chdir(_clar_path
) != 0)
135 const char *clar_sandbox_path(void)