1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <sys/stat.h>
#include <fcntl.h>
#include "caformat.h"
#include "casync.h"
#include "rm-rf.h"
#include "util.h"
int main(int argc, char *argv[]) {
const char *d;
char *teststore, *testindex, *testtree;
CaSync *s;
int r, base_fd;
CaChunkID digest;
char t[CA_CHUNK_ID_FORMAT_MAX];
uint64_t flags;
assert(var_tmp_dir(&d) >= 0);
r = asprintf(&teststore, "%s/teststore.%" PRIx64, d, random_u64());
assert_se(r >= 0);
r = asprintf(&testindex, "%s/testindex.%" PRIx64, d, random_u64());
assert_se(r >= 0);
r = asprintf(&testtree, "%s/testtree.%" PRIx64, d, random_u64());
assert_se(r >= 0);
assert_se(s = ca_sync_new_encode());
flags = CA_FORMAT_DEFAULT;
if (geteuid() != 0)
flags &= ~CA_FORMAT_WITH_PRIVILEGED;
assert_se(ca_sync_set_feature_flags(s, flags) >= 0);
base_fd = open(".", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
assert_se(base_fd >= 0);
assert_se(ca_sync_set_base_fd(s, base_fd) >= 0);
assert_se(ca_sync_enable_archive_digest(s, true) >= 0);
assert_se(ca_sync_set_store_path(s, teststore) >= 0);
assert_se(ca_sync_set_index_path(s, testindex) >= 0);
for (;;) {
r = ca_sync_step(s);
assert_se(r >= 0);
switch (r) {
case CA_SYNC_FINISHED: {
assert_se(ca_sync_get_archive_digest(s, &digest) >= 0);
printf("%s\n", ca_chunk_id_format(&digest, t));
goto step2;
}
case CA_SYNC_STEP:
case CA_SYNC_PAYLOAD:
case CA_SYNC_NEXT_FILE:
case CA_SYNC_DONE_FILE:
break;
default:
assert_se(false);
}
}
step2:
ca_sync_unref(s);
assert_se(s = ca_sync_new_decode());
(void) mkdir(testtree, 0777);
base_fd = open(testtree, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
assert_se(base_fd >= 0);
assert_se(ca_sync_set_base_fd(s, base_fd) >= 0);
assert_se(ca_sync_enable_archive_digest(s, true) >= 0);
assert_se(ca_sync_set_store_path(s, teststore) >= 0);
assert_se(ca_sync_set_index_path(s, testindex) >= 0);
for (;;) {
r = ca_sync_step(s);
assert_se(r >= 0);
switch (r) {
case CA_SYNC_FINISHED: {
assert_se(ca_sync_get_archive_digest(s, &digest) >= 0);
printf("%s\n", ca_chunk_id_format(&digest, t));
goto finish;
}
case CA_SYNC_STEP:
case CA_SYNC_PAYLOAD:
case CA_SYNC_NEXT_FILE:
case CA_SYNC_DONE_FILE:
break;
default:
assert_se(false);
}
}
finish:
ca_sync_unref(s);
assert_se(unlink(testindex) == 0);
assert_se(rm_rf(teststore, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
assert_se(rm_rf(testtree, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
free(teststore);
free(testindex);
free(testtree);
return 0;
}
|