libredirect: add support for mkstemp family of functions
This commit is contained in:
parent
b3a7dc22d1
commit
2185a70fa4
|
@ -57,7 +57,9 @@ else stdenv.mkDerivation rec {
|
||||||
''}
|
''}
|
||||||
|
|
||||||
if [ -n "$doInstallCheck" ]; then
|
if [ -n "$doInstallCheck" ]; then
|
||||||
$CC -Wall -std=c99 -D_POSIX_C_SOURCE=200809L -O3 test.c -o test
|
$CC -Wall -std=c99 \
|
||||||
|
${lib.optionalString (!stdenv.isDarwin) "-D_GNU_SOURCE"} \
|
||||||
|
-O3 test.c -o test
|
||||||
fi
|
fi
|
||||||
|
|
||||||
runHook postBuild
|
runHook postBuild
|
||||||
|
|
|
@ -84,6 +84,13 @@ static const char * rewrite(const char * path, char * buf)
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char * rewrite_non_const(char * path, char * buf)
|
||||||
|
{
|
||||||
|
// as long as the argument `path` is non-const, we can consider discarding
|
||||||
|
// the const qualifier of the return value to be safe.
|
||||||
|
return (char *)rewrite(path, buf);
|
||||||
|
}
|
||||||
|
|
||||||
static int open_needs_mode(int flags)
|
static int open_needs_mode(int flags)
|
||||||
{
|
{
|
||||||
#ifdef O_TMPFILE
|
#ifdef O_TMPFILE
|
||||||
|
@ -378,3 +385,61 @@ WRAPPER(int, rmdir)(const char *path)
|
||||||
return rmdir_real(rewrite(path, buf));
|
return rmdir_real(rewrite(path, buf));
|
||||||
}
|
}
|
||||||
WRAPPER_DEF(rmdir)
|
WRAPPER_DEF(rmdir)
|
||||||
|
|
||||||
|
static void copy_temp_wildcard(char * dest, char * src, int suffixlen) {
|
||||||
|
int dest_len = strnlen(dest, PATH_MAX);
|
||||||
|
int src_len = strnlen(src, PATH_MAX);
|
||||||
|
memcpy(dest + dest_len - (6 + suffixlen), src + src_len - (6 + suffixlen), 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
WRAPPER(int, mkstemp)(char *template)
|
||||||
|
{
|
||||||
|
int (*mkstemp_real) (char *template) = LOOKUP_REAL(mkstemp);
|
||||||
|
char buf[PATH_MAX];
|
||||||
|
char * rewritten = rewrite_non_const(template, buf);
|
||||||
|
int retval = mkstemp_real(rewritten);
|
||||||
|
if (retval >= 0 && rewritten != template) {
|
||||||
|
copy_temp_wildcard(template, rewritten, 0);
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
WRAPPER_DEF(mkstemp)
|
||||||
|
|
||||||
|
WRAPPER(int, mkostemp)(char *template, int flags)
|
||||||
|
{
|
||||||
|
int (*mkostemp_real) (char *template, int flags) = LOOKUP_REAL(mkostemp);
|
||||||
|
char buf[PATH_MAX];
|
||||||
|
char * rewritten = rewrite_non_const(template, buf);
|
||||||
|
int retval = mkostemp_real(rewritten, flags);
|
||||||
|
if (retval >= 0 && rewritten != template) {
|
||||||
|
copy_temp_wildcard(template, rewritten, 0);
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
WRAPPER_DEF(mkostemp)
|
||||||
|
|
||||||
|
WRAPPER(int, mkstemps)(char *template, int suffixlen)
|
||||||
|
{
|
||||||
|
int (*mkstemps_real) (char *template, int suffixlen) = LOOKUP_REAL(mkstemps);
|
||||||
|
char buf[PATH_MAX];
|
||||||
|
char * rewritten = rewrite_non_const(template, buf);
|
||||||
|
int retval = mkstemps_real(rewritten, suffixlen);
|
||||||
|
if (retval >= 0 && rewritten != template) {
|
||||||
|
copy_temp_wildcard(template, rewritten, suffixlen);
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
WRAPPER_DEF(mkstemps)
|
||||||
|
|
||||||
|
WRAPPER(int, mkostemps)(char *template, int suffixlen, int flags)
|
||||||
|
{
|
||||||
|
int (*mkostemps_real) (char *template, int suffixlen, int flags) = LOOKUP_REAL(mkostemps);
|
||||||
|
char buf[PATH_MAX];
|
||||||
|
char * rewritten = rewrite_non_const(template, buf);
|
||||||
|
int retval = mkostemps_real(rewritten, suffixlen, flags);
|
||||||
|
if (retval >= 0 && rewritten != template) {
|
||||||
|
copy_temp_wildcard(template, rewritten, suffixlen);
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
WRAPPER_DEF(mkostemps)
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <spawn.h>
|
#include <spawn.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
@ -43,11 +45,25 @@ void test_subprocess(void) {
|
||||||
assert(system(SUBTEST) == 0);
|
assert(system(SUBTEST) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void assert_mktemp_path(
|
||||||
|
const char * orig_prefix,
|
||||||
|
const char * orig_suffix,
|
||||||
|
const char * updated
|
||||||
|
) {
|
||||||
|
// prefix unchanged
|
||||||
|
assert(strncmp(updated, orig_prefix, strlen(orig_prefix)) == 0);
|
||||||
|
// wildcards replaced
|
||||||
|
assert(strcmp(updated + strlen(orig_prefix), "XXXXXX") != 0);
|
||||||
|
// suffix unchanged
|
||||||
|
assert(strcmp(updated + strlen(orig_prefix) + 6, orig_suffix) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
FILE *testfp;
|
FILE *testfp;
|
||||||
int testfd;
|
int testfd;
|
||||||
struct stat testsb;
|
struct stat testsb;
|
||||||
|
char buf[PATH_MAX];
|
||||||
|
|
||||||
testfp = fopen(TESTPATH, "r");
|
testfp = fopen(TESTPATH, "r");
|
||||||
assert(testfp != NULL);
|
assert(testfp != NULL);
|
||||||
|
@ -77,6 +93,30 @@ int main(int argc, char *argv[])
|
||||||
#endif
|
#endif
|
||||||
assert(unlinkat(123, TESTDIR "/dir-mkdirat", AT_REMOVEDIR) == 0);
|
assert(unlinkat(123, TESTDIR "/dir-mkdirat", AT_REMOVEDIR) == 0);
|
||||||
|
|
||||||
|
strncpy(buf, TESTDIR "/tempXXXXXX", PATH_MAX);
|
||||||
|
testfd = mkstemp(buf);
|
||||||
|
assert(testfd > 0);
|
||||||
|
assert_mktemp_path(TESTDIR "/temp", "", buf);
|
||||||
|
close(testfd);
|
||||||
|
|
||||||
|
strncpy(buf, TESTDIR "/tempXXXXXX", PATH_MAX);
|
||||||
|
testfd = mkostemp(buf, 0);
|
||||||
|
assert(testfd > 0);
|
||||||
|
assert_mktemp_path(TESTDIR "/temp", "", buf);
|
||||||
|
close(testfd);
|
||||||
|
|
||||||
|
strncpy(buf, TESTDIR "/tempXXXXXX.test", PATH_MAX);
|
||||||
|
testfd = mkstemps(buf, strlen(".test"));
|
||||||
|
assert(testfd > 0);
|
||||||
|
assert_mktemp_path(TESTDIR "/temp", ".test", buf);
|
||||||
|
close(testfd);
|
||||||
|
|
||||||
|
strncpy(buf, TESTDIR "/tempXXXXXX.test", PATH_MAX);
|
||||||
|
testfd = mkostemps(buf, strlen(".test"), 0);
|
||||||
|
assert(testfd > 0);
|
||||||
|
assert_mktemp_path(TESTDIR "/temp", ".test", buf);
|
||||||
|
close(testfd);
|
||||||
|
|
||||||
test_spawn();
|
test_spawn();
|
||||||
test_system();
|
test_system();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue