Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_file_copy.c
Go to the documentation of this file.
1
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include "nilorea/n_common.h"
21#include "nilorea/n_files.h"
22#include "nilorea/n_log.h"
23
25static int failures = 0;
26
28static void expect_true(const char* what, int ok) {
29 if (!ok) {
30 n_log(LOG_ERR, "FAIL: %s", what);
31 failures++;
32 }
33}
34
36static int there(const char* path) {
37 FILE* f = fopen(path, "rb");
38
39 if (!f) return 0;
40 fclose(f);
41 return 1;
42}
43
44int main(void) {
45 const char* tmp = getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp";
46 char src[512], dst[512], part[520], other[512];
47 size_t copied = 0, i = 0;
48 FILE* f = NULL;
49
51 snprintf(src, sizeof(src), "%s/ex_file_copy_%d.src", tmp, (int)getpid());
52 snprintf(dst, sizeof(dst), "%s/ex_file_copy_%d.dst", tmp, (int)getpid());
53 snprintf(part, sizeof(part), "%s.part", dst);
54 snprintf(other, sizeof(other), "%s/ex_file_copy_%d.other", tmp, (int)getpid());
55
56 /* A little over three blocks, so the loop turns and the tail is short. */
57 f = fopen(src, "wb");
58 if (!f) return 1;
59 for (i = 0; i < 800000; i++) fputc((int)((i * 7 + i / 13) & 0xff), f);
60 fclose(f);
61
62 expect_true("a file is copied", n_file_copy_verified(src, dst, &copied) == TRUE);
63 expect_true("every byte of it", copied == 800000);
64 expect_true("the copy is equal", n_file_same_content(src, dst) == TRUE);
65 expect_true("no part file is left", !there(part));
66 expect_true("an existing copy is not replaced", n_file_copy_verified(src, dst, NULL) == FALSE);
67 expect_true("and is still equal after the refusal", n_file_same_content(src, dst) == TRUE);
68
69 /* Same size, one byte different. */
70 f = fopen(other, "wb");
71 if (f) {
72 for (i = 0; i < 800000; i++) fputc(i == 500000 ? 0 : (int)((i * 7 + i / 13) & 0xff), f); /* 29 there in the original */
73 fclose(f);
74 }
75 expect_true("one byte different is different", n_file_same_content(src, other) == FALSE);
76 remove(other);
77
78 remove(dst);
79 expect_true("a missing source fails", n_file_copy_verified(other, dst, NULL) == FALSE);
80 expect_true("and leaves nothing", !there(dst) && !there(part));
81 expect_true("a NULL is refused", n_file_copy_verified(NULL, dst, NULL) == FALSE);
82
83 remove(src);
84 remove(dst);
85 n_log(LOG_NOTICE, "ex_file_copy: %s", failures ? "FAILED" : "ok");
86 return failures ? 1 : 0;
87}
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.