From c91a645782e5d8eaa096dd24774ac3280deec631 Mon Sep 17 00:00:00 2001 From: Takeshi Nakatani Date: Wed, 12 Aug 2015 15:09:34 +0000 Subject: [PATCH] Convert rename_before_close to a shell script #229 --- test/Makefile.am | 6 --- test/integration-test-main.sh | 12 +++-- test/rename_before_close.c | 88 ----------------------------------- 3 files changed, 8 insertions(+), 98 deletions(-) delete mode 100644 test/rename_before_close.c diff --git a/test/Makefile.am b/test/Makefile.am index d539b9e..84b13fa 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -28,9 +28,3 @@ EXTRA_DIST = \ sample_ahbe.conf testdir = test - -test_PROGRAMS=rename_before_close - -rename_before_close_SOURCES = rename_before_close.c - -install: diff --git a/test/integration-test-main.sh b/test/integration-test-main.sh index 5115256..1e23997 100755 --- a/test/integration-test-main.sh +++ b/test/integration-test-main.sh @@ -286,14 +286,18 @@ rm_test_dir ########################################################## if false; then echo "Testing rename before close ..." -$CUR_DIR/rename_before_close $TEST_TEXT_FILE -if [ $? != 0 ]; then +( + echo foo + mv $TEST_TEXT_FILE ${TEST_TEXT_FILE}.new +) > $TEST_TEXT_FILE + +if ! cmp <(echo foo) ${TEST_TEXT_FILE}.new; then echo "rename before close failed" exit 1 fi -# clean up -rm_test_file +rm_test_file ${TEST_TEXT_FILE}.new +rm -f ${TEST_TEXT_FILE} fi ########################################################## diff --git a/test/rename_before_close.c b/test/rename_before_close.c deleted file mode 100644 index 7efceee..0000000 --- a/test/rename_before_close.c +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -static const char FILE_CONTENT[] = "XXXX"; -#define PROG "rename_before_close" - -static char * -filename_to_mkstemp_template(const char *file) -{ - size_t len = strlen(file); - static const char suffix[] = ".XXXXXX"; - size_t new_len = len + sizeof(suffix); - char *ret_str = calloc(1, new_len); - int ret = snprintf(ret_str, new_len, "%s%s", file, suffix); - assert(ret == new_len - 1); - assert(ret_str[new_len] == '\0'); - return ret_str; -} - -static off_t -get_file_size(const char *file) -{ - struct stat ss; - printf(PROG ": stat(%s)\n", file); - int ret = lstat(file, &ss); - assert(ret == 0); - return ss.st_size; -} - -static void -test_rename_before_close(const char *file) -{ - char *template = filename_to_mkstemp_template(file); - printf(PROG ": mkstemp(%s)\n", template); - int fd = mkstemp(template); - assert(fd >= 0); - - sleep(1); - - printf(PROG ": write(%s)\n", template); - int ret = write(fd, FILE_CONTENT, sizeof(FILE_CONTENT)); - assert(ret == sizeof(FILE_CONTENT)); - - sleep(1); - - printf(PROG ": fsync(%s)\n", template); - ret = fsync(fd); - assert(ret == 0); - - sleep(1); - - assert(get_file_size(template) == sizeof(FILE_CONTENT)); - - sleep(1); - - printf(PROG ": rename(%s, %s)\n", template, file); - ret = rename(template, file); - assert(ret == 0); - - sleep(1); - - printf(PROG ": close(%s)\n", file); - ret = close(fd); - assert(ret == 0); - - sleep(1); - - assert(get_file_size(file) == sizeof(FILE_CONTENT)); -} - -int -main(int argc, char *argv[]) -{ - setvbuf(stdout, NULL, _IONBF, 0); - - if (argc < 2) { - printf("Usage: %s ", argv[0]); - return 1; - } - - test_rename_before_close(argv[1]); - return 0; -}