Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_file_copy.c

n_file_copy_verified() and n_file_same_content()

n_file_copy_verified() and n_file_same_content()

Author
Castagnier Mickael
Version
1.0
Date
24/09/2026

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>
#include "nilorea/n_log.h"
static int failures = 0;
static void expect_true(const char* what, int ok) {
if (!ok) {
n_log(LOG_ERR, "FAIL: %s", what);
}
}
static int there(const char* path) {
FILE* f = fopen(path, "rb");
if (!f) return 0;
fclose(f);
return 1;
}
int main(void) {
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());
/* A little over three blocks, so the loop turns and the tail is short. */
f = fopen(src, "wb");
if (!f) return 1;
for (i = 0; i < 800000; i++) fputc((int)((i * 7 + i / 13) & 0xff), f);
fclose(f);
expect_true("a file is copied", n_file_copy_verified(src, dst, &copied) == TRUE);
expect_true("every byte of it", copied == 800000);
expect_true("the copy is equal", n_file_same_content(src, dst) == TRUE);
expect_true("no part file is left", !there(part));
expect_true("an existing copy is not replaced", n_file_copy_verified(src, dst, NULL) == FALSE);
expect_true("and is still equal after the refusal", n_file_same_content(src, dst) == TRUE);
/* Same size, one byte different. */
f = fopen(other, "wb");
if (f) {
for (i = 0; i < 800000; i++) fputc(i == 500000 ? 0 : (int)((i * 7 + i / 13) & 0xff), f); /* 29 there in the original */
fclose(f);
}
expect_true("one byte different is different", n_file_same_content(src, other) == FALSE);
remove(other);
remove(dst);
expect_true("a missing source fails", n_file_copy_verified(other, dst, NULL) == FALSE);
expect_true("and leaves nothing", !there(dst) && !there(part));
expect_true("a NULL is refused", n_file_copy_verified(NULL, dst, NULL) == FALSE);
remove(src);
remove(dst);
n_log(LOG_NOTICE, "ex_file_copy: %s", failures ? "FAILED" : "ok");
return failures ? 1 : 0;
}
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void expect_true(const char *label, int cond)
Definition ex_atlas.c:53
static int there(const char *path)
does a file exist
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
int n_file_same_content(const char *a, const char *b)
Do two files hold the same bytes.
Definition n_files.c:366
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.
Definition n_files.c:381
Common headers and low-level functions & define.
Files configuration header.
Generic log system.