diff options
author | Robert Scott <code@humanleg.org.uk> | 2022-01-16 15:42:07 +0000 |
---|---|---|
committer | Robert Scott <code@humanleg.org.uk> | 2022-01-18 20:20:27 +0000 |
commit | b3a7dc22d12e8c7007a71e965ee24f6b609fe212 (patch) | |
tree | 87ec9d797e3c5ad6997c79f917c507098588ff11 /pkgs/build-support/libredirect | |
parent | 8fd298a93a28aed4dfcea6d59714c8b50452d310 (diff) |
libredirect: add support for unlink, unlinkat, rmdir
add coverage of these and mkdir functions in tests
Diffstat (limited to 'pkgs/build-support/libredirect')
-rw-r--r-- | pkgs/build-support/libredirect/default.nix | 4 | ||||
-rw-r--r-- | pkgs/build-support/libredirect/libredirect.c | 24 | ||||
-rw-r--r-- | pkgs/build-support/libredirect/test.c | 18 |
3 files changed, 44 insertions, 2 deletions
diff --git a/pkgs/build-support/libredirect/default.nix b/pkgs/build-support/libredirect/default.nix index 5545c93c19e1e..9a35db97c9ff4 100644 --- a/pkgs/build-support/libredirect/default.nix +++ b/pkgs/build-support/libredirect/default.nix @@ -57,7 +57,7 @@ else stdenv.mkDerivation rec { ''} if [ -n "$doInstallCheck" ]; then - $CC -Wall -std=c99 -O3 test.c -o test + $CC -Wall -std=c99 -D_POSIX_C_SOURCE=200809L -O3 test.c -o test fi runHook postBuild @@ -91,7 +91,7 @@ else stdenv.mkDerivation rec { installCheckPhase = '' ( source "$hook/nix-support/setup-hook" - NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true" ./test + NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true:/bar/baz=$(mktemp -d)" ./test ) ''; diff --git a/pkgs/build-support/libredirect/libredirect.c b/pkgs/build-support/libredirect/libredirect.c index 0dc7feb382c37..e33199402c4bd 100644 --- a/pkgs/build-support/libredirect/libredirect.c +++ b/pkgs/build-support/libredirect/libredirect.c @@ -354,3 +354,27 @@ WRAPPER(int, mkdirat)(int dirfd, const char *path, mode_t mode) return mkdirat_real(dirfd, rewrite(path, buf), mode); } WRAPPER_DEF(mkdirat) + +WRAPPER(int, unlink)(const char *path) +{ + int (*unlink_real) (const char *path) = LOOKUP_REAL(unlink); + char buf[PATH_MAX]; + return unlink_real(rewrite(path, buf)); +} +WRAPPER_DEF(unlink) + +WRAPPER(int, unlinkat)(int dirfd, const char *path, int flags) +{ + int (*unlinkat_real) (int dirfd, const char *path, int flags) = LOOKUP_REAL(unlinkat); + char buf[PATH_MAX]; + return unlinkat_real(dirfd, rewrite(path, buf), flags); +} +WRAPPER_DEF(unlinkat) + +WRAPPER(int, rmdir)(const char *path) +{ + int (*rmdir_real) (const char *path) = LOOKUP_REAL(rmdir); + char buf[PATH_MAX]; + return rmdir_real(rewrite(path, buf)); +} +WRAPPER_DEF(rmdir) diff --git a/pkgs/build-support/libredirect/test.c b/pkgs/build-support/libredirect/test.c index 853f26bb52092..c546a8958288e 100644 --- a/pkgs/build-support/libredirect/test.c +++ b/pkgs/build-support/libredirect/test.c @@ -1,4 +1,5 @@ #include <assert.h> +#include <errno.h> #include <fcntl.h> #include <spawn.h> #include <stdio.h> @@ -9,6 +10,7 @@ #include <sys/types.h> #include <sys/wait.h> +#define TESTDIR "/bar/baz" #define TESTPATH "/foo/bar/test" #define SUBTEST "./test sub" @@ -59,6 +61,22 @@ int main(int argc, char *argv[]) assert(stat(TESTPATH, &testsb) != -1); + assert(mkdir(TESTDIR "/dir-mkdir", 0777) == 0); + assert(unlink(TESTDIR "/dir-mkdir") == -1); // it's a directory! +#ifndef __APPLE__ + assert(errno == EISDIR); +#endif + assert(rmdir(TESTDIR "/dir-mkdir") == 0); + assert(unlink(TESTDIR "/dir-mkdir") == -1); + assert(errno == ENOENT); + + assert(mkdirat(123, TESTDIR "/dir-mkdirat", 0777) == 0); + assert(unlinkat(123, TESTDIR "/dir-mkdirat", 0) == -1); // it's a directory! +#ifndef __APPLE__ + assert(errno == EISDIR); +#endif + assert(unlinkat(123, TESTDIR "/dir-mkdirat", AT_REMOVEDIR) == 0); + test_spawn(); test_system(); |