-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some temporary directory handling changes #94
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,17 @@ | |
#include <sys/syslimits.h> | ||
#endif | ||
|
||
static char _clar_path[4096 + 1]; | ||
/* | ||
* The tempdir is the temporary directory for the entirety of the clar | ||
* process execution. The sandbox is an individual temporary directory | ||
* for the execution of an individual test. Sandboxes are deleted | ||
* entirely after test execution to avoid pollution across tests. | ||
*/ | ||
#define CLAR_PATH_MAX 4096 | ||
static char _clar_tempdir[CLAR_PATH_MAX]; | ||
size_t _clar_tempdir_len; | ||
|
||
static char _clar_sandbox[CLAR_PATH_MAX]; | ||
|
||
static int | ||
is_valid_tmp_path(const char *path) | ||
|
@@ -35,10 +45,9 @@ find_tmp_path(char *buffer, size_t length) | |
continue; | ||
|
||
if (is_valid_tmp_path(env)) { | ||
#ifdef __APPLE__ | ||
if (length >= PATH_MAX && realpath(env, buffer) != NULL) | ||
return 0; | ||
#endif | ||
if (strlen(env) + 1 > CLAR_PATH_MAX) | ||
return -1; | ||
|
||
strncpy(buffer, env, length - 1); | ||
buffer[length - 1] = '\0'; | ||
return 0; | ||
|
@@ -47,10 +56,6 @@ find_tmp_path(char *buffer, size_t length) | |
|
||
/* If the environment doesn't say anything, try to use /tmp */ | ||
if (is_valid_tmp_path("/tmp")) { | ||
#ifdef __APPLE__ | ||
if (length >= PATH_MAX && realpath("/tmp", buffer) != NULL) | ||
return 0; | ||
#endif | ||
strncpy(buffer, "/tmp", length - 1); | ||
buffer[length - 1] = '\0'; | ||
return 0; | ||
|
@@ -75,17 +80,45 @@ find_tmp_path(char *buffer, size_t length) | |
return -1; | ||
} | ||
|
||
static void clar_unsandbox(void) | ||
static int canonicalize_tmp_path(char *buffer) | ||
{ | ||
#ifdef _WIN32 | ||
char tmp[CLAR_PATH_MAX]; | ||
DWORD ret; | ||
|
||
ret = GetFullPathName(buffer, CLAR_PATH_MAX, tmp, NULL); | ||
|
||
if (ret == 0 || ret > CLAR_PATH_MAX) | ||
return -1; | ||
|
||
ret = GetLongPathName(tmp, buffer, CLAR_PATH_MAX); | ||
|
||
if (ret == 0 || ret > CLAR_PATH_MAX) | ||
return -1; | ||
|
||
return 0; | ||
#else | ||
char tmp[CLAR_PATH_MAX]; | ||
|
||
if (realpath(buffer, tmp) == NULL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I worry a bit that realpath may not be available on all systems, as it is only specified by POSIX.1-2001 and thus not part of C90 itself. And unfortunately we have folks that compile Git on systems that predate POSIX.1-2001 itself. I sometimes wish that some kind of resource existed that provides a matrix of operating system <-> availability of certain features. |
||
return -1; | ||
|
||
strcpy(buffer, tmp); | ||
return 0; | ||
#endif | ||
} | ||
|
||
static void clar_tempdir_shutdown(void) | ||
{ | ||
if (_clar_path[0] == '\0') | ||
if (_clar_tempdir[0] == '\0') | ||
return; | ||
|
||
cl_must_pass(chdir("..")); | ||
|
||
fs_rm(_clar_path); | ||
fs_rm(_clar_tempdir); | ||
} | ||
|
||
static int build_sandbox_path(void) | ||
static int build_tempdir_path(void) | ||
{ | ||
#ifdef CLAR_TMPDIR | ||
const char path_tail[] = CLAR_TMPDIR "_XXXXXX"; | ||
|
@@ -95,60 +128,111 @@ static int build_sandbox_path(void) | |
|
||
size_t len; | ||
|
||
if (find_tmp_path(_clar_path, sizeof(_clar_path)) < 0) | ||
if (find_tmp_path(_clar_tempdir, sizeof(_clar_tempdir)) < 0 || | ||
canonicalize_tmp_path(_clar_tempdir) < 0) | ||
return -1; | ||
|
||
len = strlen(_clar_path); | ||
len = strlen(_clar_tempdir); | ||
|
||
#ifdef _WIN32 | ||
{ /* normalize path to POSIX forward slashes */ | ||
size_t i; | ||
for (i = 0; i < len; ++i) { | ||
if (_clar_path[i] == '\\') | ||
_clar_path[i] = '/'; | ||
if (_clar_tempdir[i] == '\\') | ||
_clar_tempdir[i] = '/'; | ||
} | ||
} | ||
#endif | ||
|
||
if (_clar_path[len - 1] != '/') { | ||
_clar_path[len++] = '/'; | ||
if (_clar_tempdir[len - 1] != '/') { | ||
_clar_tempdir[len++] = '/'; | ||
} | ||
|
||
strncpy(_clar_path + len, path_tail, sizeof(_clar_path) - len); | ||
strncpy(_clar_tempdir + len, path_tail, sizeof(_clar_tempdir) - len); | ||
|
||
#if defined(__MINGW32__) | ||
if (_mktemp(_clar_path) == NULL) | ||
if (_mktemp(_clar_tempdir) == NULL) | ||
return -1; | ||
|
||
if (mkdir(_clar_path, 0700) != 0) | ||
if (mkdir(_clar_tempdir, 0700) != 0) | ||
return -1; | ||
#elif defined(_WIN32) | ||
if (_mktemp_s(_clar_path, sizeof(_clar_path)) != 0) | ||
if (_mktemp_s(_clar_tempdir, sizeof(_clar_tempdir)) != 0) | ||
return -1; | ||
|
||
if (mkdir(_clar_path, 0700) != 0) | ||
if (mkdir(_clar_tempdir, 0700) != 0) | ||
return -1; | ||
#else | ||
if (mkdtemp(_clar_path) == NULL) | ||
if (mkdtemp(_clar_tempdir) == NULL) | ||
return -1; | ||
#endif | ||
|
||
_clar_tempdir_len = strlen(_clar_tempdir); | ||
return 0; | ||
} | ||
|
||
static int clar_sandbox(void) | ||
static int clar_tempdir_init(void) | ||
{ | ||
if (_clar_path[0] == '\0' && build_sandbox_path() < 0) | ||
if (_clar_tempdir[0] == '\0' && build_tempdir_path() < 0) | ||
return -1; | ||
|
||
if (chdir(_clar_path) != 0) | ||
if (chdir(_clar_tempdir) != 0) | ||
return -1; | ||
|
||
srand(clock() ^ time(NULL) ^ (getpid() << 16)); | ||
|
||
return 0; | ||
} | ||
|
||
static int clar_sandbox_create(void) | ||
{ | ||
char alpha[] = "0123456789abcdef"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be |
||
int num = rand(); | ||
|
||
cl_assert(_clar_sandbox[0] == '\0'); | ||
|
||
strcpy(_clar_sandbox, _clar_tempdir); | ||
_clar_sandbox[_clar_tempdir_len] = '/'; | ||
|
||
_clar_sandbox[_clar_tempdir_len + 1] = alpha[(num & 0xf0000000) >> 28]; | ||
_clar_sandbox[_clar_tempdir_len + 2] = alpha[(num & 0x0f000000) >> 24]; | ||
_clar_sandbox[_clar_tempdir_len + 3] = alpha[(num & 0x00f00000) >> 20]; | ||
_clar_sandbox[_clar_tempdir_len + 4] = alpha[(num & 0x000f0000) >> 16]; | ||
_clar_sandbox[_clar_tempdir_len + 5] = alpha[(num & 0x0000f000) >> 12]; | ||
_clar_sandbox[_clar_tempdir_len + 6] = alpha[(num & 0x00000f00) >> 8]; | ||
_clar_sandbox[_clar_tempdir_len + 7] = alpha[(num & 0x000000f0) >> 4]; | ||
_clar_sandbox[_clar_tempdir_len + 8] = alpha[(num & 0x0000000f) >> 0]; | ||
_clar_sandbox[_clar_tempdir_len + 9] = '\0'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that we have a central directory where all test results will be written into, do we need randomness at all? We could compute statically derived paths, e.g. by appending the test name to the sandbox path. These have to be unique anyway, so the only thing that we need to care about is to prune such a directory that may be left over from a previous run. |
||
|
||
if (mkdir(_clar_sandbox, 0700) != 0) | ||
return -1; | ||
|
||
if (chdir(_clar_sandbox) != 0) | ||
return -1; | ||
|
||
return 0; | ||
} | ||
|
||
static int clar_sandbox_cleanup(void) | ||
{ | ||
cl_assert(_clar_sandbox[0] != '\0'); | ||
|
||
fs_rm(_clar_sandbox); | ||
_clar_sandbox[0] = '\0'; | ||
|
||
if (chdir(_clar_tempdir) != 0) | ||
return -1; | ||
|
||
return 0; | ||
} | ||
|
||
const char *clar_tempdir_path(void) | ||
{ | ||
return _clar_tempdir; | ||
} | ||
|
||
const char *clar_sandbox_path(void) | ||
{ | ||
return _clar_path; | ||
return _clar_sandbox; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be non-static? I couldn't spot any uses of it outside of the current scope.