#include "naut/storage.h" #include "test.h" #include #include #include int main(void) { char tmpl[] = "/tmp/naut_stor_XXXXXX"; char *root = mkdtemp(tmpl); CHECK(root != NULL); /* multi-file torrent: a write/read can straddle the file boundary */ naut_file files[3] = { { (char *)"a.bin", 100 }, { (char *)"d/b.bin", 50 }, { (char *)"d/e/c.bin", 30 }, }; naut_err err; naut_storage *s = naut_storage_open(files, 3, root, &err); CHECK(s && err == NAUT_OK); CHECK_EQ(naut_storage_total(s), 180); CHECK(!naut_storage_direct_enabled(s)); /* fill the whole space with a known pattern in one straddling write */ uint8_t pattern[180]; for (int i = 0; i < 180; i++) pattern[i] = (uint8_t)(i * 3 + 1); CHECK(naut_storage_write(s, 0, pattern, 180) == NAUT_OK); /* read back across boundaries at an awkward offset */ uint8_t rb[120]; CHECK(naut_storage_read(s, 40, rb, 120) == NAUT_OK); /* spans all 3 files */ CHECK(memcmp(rb, pattern + 40, 120) == 0); /* out-of-range rejected */ CHECK(naut_storage_write(s, 170, pattern, 20) == NAUT_ERR_RANGE); CHECK(naut_storage_sync(s) == NAUT_OK); naut_storage_close(s); /* reopen and confirm persistence + nested files exist on disk */ s = naut_storage_open(files, 3, root, &err); CHECK(s && err == NAUT_OK); uint8_t all[180]; CHECK(naut_storage_read(s, 0, all, 180) == NAUT_OK); CHECK(memcmp(all, pattern, 180) == 0); naut_storage_close(s); /* Optional O_DIRECT uses aligned bulk I/O and buffered edge fallback. */ { char direct_tmpl[] = "/tmp/naut_direct_XXXXXX"; char *direct_root = mkdtemp(direct_tmpl); naut_file direct_file = { (char *)"direct.bin", 8192 }; naut_storage_opts opts = { .direct_io = true, .preallocate = true, }; naut_storage *direct = naut_storage_open_opts( &direct_file, 1, direct_root, &opts, &err); CHECK(direct && err == NAUT_OK); uint8_t *write_buf = aligned_alloc(NAUT_PAGE, 8192); uint8_t *read_buf = aligned_alloc(NAUT_PAGE, 8192); CHECK(write_buf && read_buf); memset(write_buf, 0x5a, 8192); CHECK(naut_storage_write(direct, 0, write_buf, 8192) == NAUT_OK); CHECK(naut_storage_read(direct, 0, read_buf, 8192) == NAUT_OK); CHECK(memcmp(write_buf, read_buf, 8192) == 0); free(write_buf); free(read_buf); naut_storage_close(direct); char direct_cmd[256]; snprintf(direct_cmd, sizeof direct_cmd, "rm -rf '%s'", direct_root); if (system(direct_cmd) != 0) {} } /* cleanup */ char cmd[256]; snprintf(cmd, sizeof cmd, "rm -rf '%s'", root); if (system(cmd) != 0) { /* best effort */ } TEST_MAIN_END(); }