Copies a file larger than one block, checks the copy is there and equal, that no ".part" is left, that an existing destination is refused and left as it was, that a missing source fails without leaving anything, and that n_file_same_content() tells equal files from different ones of the same size.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
if (!ok) {
}
}
static int there(
const char* path) {
FILE* f = fopen(path, "rb");
if (!f) return 0;
fclose(f);
return 1;
}
const char* tmp = getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp";
char src[512], dst[512], part[520], other[512];
size_t copied = 0, i = 0;
FILE* f = NULL;
snprintf(src, sizeof(src), "%s/ex_file_copy_%d.src", tmp, (int)getpid());
snprintf(dst, sizeof(dst), "%s/ex_file_copy_%d.dst", tmp, (int)getpid());
snprintf(part, sizeof(part), "%s.part", dst);
snprintf(other, sizeof(other), "%s/ex_file_copy_%d.other", tmp, (int)getpid());
f = fopen(src, "wb");
if (!f) return 1;
for (i = 0; i < 800000; i++) fputc((int)((i * 7 + i / 13) & 0xff), f);
fclose(f);
f = fopen(other, "wb");
if (f) {
for (i = 0; i < 800000; i++) fputc(i == 500000 ? 0 : (int)((i * 7 + i / 13) & 0xff), f);
fclose(f);
}
remove(other);
remove(dst);
remove(src);
remove(dst);
}
static void expect_true(const char *label, int cond)
static int there(const char *path)
does a file exist
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_NOTICE
normal but significant condition
int n_file_same_content(const char *a, const char *b)
Do two files hold the same bytes.
int n_file_copy_verified(const char *from, const char *to, size_t *copied)
Copy a file, check the copy against the original, and only then give it its name.
Common headers and low-level functions & define.
Files configuration header.