Session checkpoint on webui-plugin: - engine dump (nautctl dump) + engine endgame integration - per-file move locations persistence; torrent-level "Set location" with reset/keep-relative/leave-separate handling + residual prune - Lua: naut.get_labels, define_settings/get_setting (script_host struct) - daemon-owned labels (category+tags) + taxonomy persistence; webui write-through - fix: pausing a completed/seeding torrent now sticks (stop wins over result) - automation tab responsive layout; anime_sort label gating + settings Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
132 lines
5.2 KiB
C
132 lines
5.2 KiB
C
/* Proves the "move files as they finish" feature at the engine level:
|
|
* - per-file completion fires the moment a file's last piece verifies,
|
|
* BEFORE the whole torrent is done;
|
|
* - a completed file can be relocated mid-download while later pieces (for
|
|
* other files) keep arriving, with no corruption. */
|
|
#include "naut/piece.h"
|
|
#include "naut/metainfo.h"
|
|
#include "naut/storage.h"
|
|
#include "test.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#ifndef NAUT_FIXTURES
|
|
#define NAUT_FIXTURES "tests/fixtures"
|
|
#endif
|
|
|
|
static uint8_t *slurp(const char *path, size_t *len) {
|
|
FILE *f = fopen(path, "rb"); if (!f) return NULL;
|
|
fseek(f, 0, SEEK_END); long n = ftell(f); fseek(f, 0, SEEK_SET);
|
|
uint8_t *b = malloc(n);
|
|
if (fread(b, 1, n, f) != (size_t)n) { fclose(f); free(b); return NULL; }
|
|
fclose(f); *len = (size_t)n; return b;
|
|
}
|
|
|
|
struct cbctx {
|
|
naut_download *d;
|
|
naut_storage *st;
|
|
char destdir[256];
|
|
int order[8], norder;
|
|
bool moved0;
|
|
bool file0_done_while_incomplete;
|
|
};
|
|
|
|
static void on_file(void *ctx, uint32_t fidx, const char *path) {
|
|
struct cbctx *c = ctx;
|
|
c->order[c->norder++] = (int)fidx;
|
|
if (fidx == 0 && !c->moved0) {
|
|
c->file0_done_while_incomplete = !naut_download_complete(c->d);
|
|
char dest[512]; snprintf(dest, sizeof dest, "%s/done_a.txt", c->destdir);
|
|
/* relocate file 0 RIGHT NOW, mid-download */
|
|
CHECK(naut_storage_relocate(c->st, 0, dest) == NAUT_OK);
|
|
c->moved0 = true;
|
|
(void)path;
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
size_t tlen;
|
|
uint8_t *tor = slurp(NAUT_FIXTURES "/multi_v1.torrent", &tlen);
|
|
CHECK(tor != NULL); if (!tor) return 1;
|
|
naut_metainfo mi;
|
|
CHECK(naut_metainfo_parse(tor, tlen, &mi) == NAUT_OK);
|
|
CHECK_EQ(mi.num_files, 2);
|
|
|
|
/* build the flat byte space from the original files, in torrent order */
|
|
uint8_t *global = malloc(mi.total_length);
|
|
uint64_t goff = 0;
|
|
for (size_t i = 0; i < mi.num_files; i++) {
|
|
char p[512]; snprintf(p, sizeof p, "%s/data/%s", NAUT_FIXTURES, mi.files[i].path);
|
|
size_t fl; uint8_t *fb = slurp(p, &fl);
|
|
CHECK(fb && fl == (size_t)mi.files[i].length);
|
|
memcpy(global + goff, fb, fl); goff += fl; free(fb);
|
|
}
|
|
|
|
char tmpl[] = "/tmp/naut_fm_XXXXXX"; char *root = mkdtemp(tmpl);
|
|
char destdir[256]; snprintf(destdir, sizeof destdir, "%s_moved", root);
|
|
|
|
naut_err err;
|
|
naut_storage *st = naut_storage_open(mi.files, mi.num_files, root, &err);
|
|
CHECK(st && err == NAUT_OK);
|
|
naut_download *d = naut_download_create(&mi, st);
|
|
CHECK(d != NULL);
|
|
|
|
struct cbctx ctx; memset(&ctx, 0, sizeof ctx);
|
|
ctx.d = d; ctx.st = st; snprintf(ctx.destdir, sizeof ctx.destdir, "%s", destdir);
|
|
naut_download_set_file_cb(d, on_file, &ctx);
|
|
|
|
uint32_t idx, begin, len;
|
|
while (naut_download_next_request(d, &idx, &begin, &len)) {
|
|
uint64_t g = (uint64_t)idx * (uint64_t)mi.piece_length + begin;
|
|
bool done = false;
|
|
CHECK(naut_download_on_block(d, idx, begin, global + g, len, &done) == NAUT_OK);
|
|
}
|
|
CHECK(naut_download_complete(d));
|
|
|
|
/* file 0 finished and was moved while the torrent was still incomplete */
|
|
CHECK_EQ(ctx.norder, 2);
|
|
CHECK_EQ(ctx.order[0], 0); /* file 0 completed first */
|
|
CHECK_EQ(ctx.order[1], 1);
|
|
CHECK(ctx.file0_done_while_incomplete); /* fired before whole-torrent done */
|
|
CHECK(ctx.moved0);
|
|
CHECK(naut_download_file_complete(d, 0) && naut_download_file_complete(d, 1));
|
|
|
|
naut_storage_sync(st);
|
|
naut_storage_close(st);
|
|
|
|
/* relocated file 0 is at the destination, byte-correct */
|
|
char dest[512]; snprintf(dest, sizeof dest, "%s/done_a.txt", destdir);
|
|
size_t dl; uint8_t *got0 = slurp(dest, &dl);
|
|
CHECK(got0 && dl == (size_t)mi.files[0].length && memcmp(got0, global, dl) == 0);
|
|
free(got0);
|
|
|
|
/* file 1 stayed put and is byte-correct (pieces after the move landed fine) */
|
|
char p1[512]; snprintf(p1, sizeof p1, "%s/%s", root, mi.files[1].path);
|
|
size_t l1; uint8_t *got1 = slurp(p1, &l1);
|
|
CHECK(got1 && l1 == (size_t)mi.files[1].length &&
|
|
memcmp(got1, global + mi.files[0].length, l1) == 0);
|
|
free(got1);
|
|
|
|
/* Simulate a daemon restart: file 0 now lives at `dest`, not under `root`.
|
|
* Reopening with its saved location as an override must pick it up in place
|
|
* so resume verifies every piece -- if the override were ignored, file 0
|
|
* would open as an empty placeholder under root and resume would fail. */
|
|
const char *overrides[2] = { dest, NULL };
|
|
naut_storage_opts ropts = { .preallocate = true, .overrides = overrides };
|
|
naut_storage *st2 =
|
|
naut_storage_open_opts(mi.files, mi.num_files, root, &ropts, &err);
|
|
CHECK(st2 && err == NAUT_OK);
|
|
naut_download *d2 = naut_download_create(&mi, st2);
|
|
CHECK(d2 != NULL);
|
|
CHECK(naut_download_resume(d2) == NAUT_OK);
|
|
CHECK(naut_download_complete(d2)); /* both files verified from their homes */
|
|
naut_download_destroy(d2);
|
|
naut_storage_close(st2);
|
|
|
|
naut_download_destroy(d);
|
|
free(global); free(tor); naut_metainfo_free(&mi);
|
|
char cmd[600]; snprintf(cmd, sizeof cmd, "rm -rf '%s' '%s'", root, destdir);
|
|
if (system(cmd)) {}
|
|
TEST_MAIN_END();
|
|
}
|