Skip to content
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

Merged
merged 4 commits into from
Jan 22, 2025
Merged

Conversation

ethomson
Copy link
Member

Some changes to temporary directory handling — general quality-of-life changes with an eye toward eventually supporting parallel execution (presumably with separate tempdirs per thread).

  • canonicalize temp sandbox directory everywhere
    We currently only canonicalize the temp sandbox directory on macOS,
    which is critical since /tmp is really /private/tmp. However, we
    should do it everywhere, so that tests can actually expect a consistent
    outcome by looking at clar_sandbox_path().

  • tempdir and sandboxes are now separate entities
    clar now has the concept of a temporary directory, which is the
    temporary directory for the entirety of a clar invocation (a set of
    test runs) and a sandbox, which is the temporary directory for a single
    test invocation.

    This allows us to ensure that a well-written test (that only writes into
    its sandbox) doesn't poison the well for future test invocations if it
    fails to clean up its sandbox.

@ethomson ethomson requested a review from pks-t October 21, 2024 09:43
clar/sandbox.h Outdated
@@ -2,7 +2,8 @@
#include <sys/syslimits.h>
#endif

static char _clar_path[4096 + 1];
#define CLAR_PATH_MAX 4096
static char _clar_path[CLAR_PATH_MAX];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that we lose the 4097th byte here?

#else
char tmp[CLAR_PATH_MAX];

if (realpath(buffer, tmp) == NULL)
Copy link
Member

Choose a reason for hiding this comment

The 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.

clar/sandbox.h Outdated
#define CLAR_PATH_MAX 4096
static char _clar_path[CLAR_PATH_MAX];
static char _clar_tempdir[CLAR_PATH_MAX];
size_t _clar_tempdir_len;
Copy link
Member

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.

clar/sandbox.h Outdated
return 0;
}

static int clar_sandbox_create(void)
{
char alpha[] = "0123456789abcdef";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be const, right?

clar/sandbox.h Outdated
_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';
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really excellent suggestion, which I have implemented. Test names are much more readable than random digits. 🙏

@ethomson ethomson force-pushed the ethomson/tempdirs branch 2 times, most recently from 0d03c04 to 97a3772 Compare January 20, 2025 16:10
@ethomson ethomson force-pushed the ethomson/tempdirs branch 2 times, most recently from 4b50259 to 0761710 Compare January 20, 2025 21:55
@ethomson
Copy link
Member Author

I refreshed this because in libgit2, we tried to update to the latest clar which failed, because we rely on understanding what the sandbox dir is on Windows. Since there's concern about the realpath function, I made it optional on non-macOS.

We previously canonicalized the temporary directory on macOS (since
`/tmp` is always `/private/tmp`). Expand this to Windows and -
optionally - anybody who has `realpath`. This ensures that tests who
want to understand where their sandbox is can accurately do so.
clar now has the concept of a _temporary directory_, which is the
temporary directory for the entirety of a `clar` invocation (a set of
test runs) and a sandbox, which is the temporary directory for a single
test invocation.

This allows us to ensure that a well-written test (that only writes into
its sandbox) doesn't poison the well for future test invocations if it
fails to clean up its sandbox.
Move out of the sandbox directory before trying to remove it; this is
required on Windows, where you cannot remove a directory that you're
chdir'd into.
@ethomson ethomson merged commit fac823c into main Jan 22, 2025
6 checks passed
@ethomson ethomson deleted the ethomson/tempdirs branch January 22, 2025 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants