#include "dat_paths.h" #include #include #include #ifdef _WIN32 #include #define test_pid _getpid #else #include #define test_pid getpid #endif static void write_text(const char *path, const char *text) { FILE *file = fopen(path, "wb"); assert(file); assert(fwrite(text, 1, strlen(text), file) == strlen(text)); assert(fclose(file) == 0); } int main(void) { char destination[256], temporary[256], contents[32]; FILE *file; int n; n = snprintf(destination, sizeof(destination), "build/test-paths-%ld", (long)test_pid()); assert(n > 0 && (size_t)n < sizeof(destination)); n = snprintf(temporary, sizeof(temporary), "%s.tmp", destination); assert(n > 0 && (size_t)n < sizeof(temporary)); write_text(destination, "before"); write_text(temporary, "after"); assert(dat_replace_file(temporary, destination) == 0); file = fopen(destination, "rb"); assert(file); assert(fgets(contents, sizeof(contents), file)); assert(fclose(file) == 0); assert(strcmp(contents, "after") == 0); assert(remove(destination) == 0); puts("user-state atomic replacement: PASS"); return 0; }