]>
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')
73 static int build_sandbox_path(void)
75 const char path_tail
[] = "clar_tmp_XXXXXX";
78 if (find_tmp_path(_clar_path
, sizeof(_clar_path
)) < 0)
81 len
= strlen(_clar_path
);
84 { /* normalize path to POSIX forward slashes */
86 for (i
= 0; i
< len
; ++i
) {
87 if (_clar_path
[i
] == '\\')
93 if (_clar_path
[len
- 1] != '/') {
94 _clar_path
[len
++] = '/';
97 strncpy(_clar_path
+ len
, path_tail
, sizeof(_clar_path
) - len
);
99 #if defined(__MINGW32__)
100 if (_mktemp(_clar_path
) == NULL
)
103 if (mkdir(_clar_path
, 0700) != 0)
105 #elif defined(_WIN32)
106 if (_mktemp_s(_clar_path
, sizeof(_clar_path
)) != 0)
109 if (mkdir(_clar_path
, 0700) != 0)
112 if (mkdtemp(_clar_path
) == NULL
)
119 static int clar_sandbox(void)
121 if (_clar_path
[0] == '\0' && build_sandbox_path() < 0)
124 if (chdir(_clar_path
) != 0)