feat: persist I2P torrent list to EDN state file across restarts
Rolling Release / Test (MoonBit, Extension, Scripts) (push) Successful in 21s
Rolling Release / Build & Publish Rolling Release (push) Successful in 2m20s

- Save active and paused torrents to torrents.edn on add, remove, pause, resume, and metadata fetch, then restore them on router startup.
- Default path is $MOON_I2P_DATA_DIR, then $XDG_DATA_HOME, then ~/.local/share/moon-i2p-browser. Add --torrent-state <file> to override, or --no-torrent-state to disable.
- Write the state file atomically (temp file, fsync, rename). A corrupt or unsupported file logs a warning and does not block startup.
- Verify restored pieces incrementally in the background for large torrents; these show as state "checking" in /api/torrents.
- Expose state_file in /api/status and the startup banner.
- Add standard RFC 4648 Base64 encode/decode helpers, used for the info-b64 field.
- Add the moonedn dependency and pass --torrent-state from the Android service.
This commit is contained in:
nandi committed 2026-11-29 02:46:23 -08:00
1 parent b803e79199
commit dd5b132b5a
17 files changed
+1105 -39

No files matched your search

+57
View File
@@ -741,6 +741,63 @@ int32_t moon_i2p_net_read_file_at(
return total;
}
MOONBIT_FFI_EXPORT
int32_t moon_i2p_net_write_file_atomic(
moonbit_bytes_t path_bytes,
int32_t path_len,
moonbit_bytes_t data,
int32_t data_len) {
if (path_len <= 0 || path_len >= 960 || data_len < 0) {
net_set_error("invalid write_file_atomic arguments");
return -1;
}
char path[1024];
memcpy(path, path_bytes, (size_t)path_len);
path[path_len] = '\0';
char tmp_path[1024];
int tn = snprintf(tmp_path, sizeof(tmp_path), "%s.tmp.%ld", path, (long)getpid());
if (tn <= 0 || (size_t)tn >= sizeof(tmp_path)) {
net_set_error("atomic temp path too long");
return -1;
}
int fd = open(tmp_path, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd < 0) {
net_set_error(strerror(errno));
return -1;
}
int32_t written = 0;
while (written < data_len) {
ssize_t n = write(fd, data + written, (size_t)(data_len - written));
if (n < 0) {
if (errno == EINTR) continue;
net_set_error(strerror(errno));
close(fd);
unlink(tmp_path);
return -1;
}
written += (int32_t)n;
}
while (fsync(fd) < 0) {
if (errno == EINTR) continue;
if (errno == EINVAL) break;
net_set_error(strerror(errno));
close(fd);
unlink(tmp_path);
return -1;
}
if (close(fd) < 0) {
net_set_error(strerror(errno));
unlink(tmp_path);
return -1;
}
if (rename(tmp_path, path) < 0) {
net_set_error(strerror(errno));
unlink(tmp_path);
return -1;
}
return 0;
}
MOONBIT_FFI_EXPORT
int32_t moon_i2p_net_home_dir(moonbit_bytes_t dst, int32_t max_len) {
const char *home = getenv("HOME");
+18
View File
@@ -194,3 +194,21 @@ test "PendingNtcp2Dial/Accept stay non-blocking and Ntcp2Conn::send_blocks_nb tr
bob_conn.close()
listener.close()
}
///|
test "write_file_atomic and file_exists replace longer files cleanly" {
let rand_hex = @encoding.hex_encode(@ntcp2_net.random_bytes(4))
let dir = "/tmp/moon_i2p_atomic_test_\{rand_hex}"
@ntcp2_net.mkdir_p(dir)
let path = "\{dir}/torrents.edn"
assert_true(!@ntcp2_net.file_exists(path))
let long_bytes = b"0123456789abcdef0123456789abcdef-long-payload"
@ntcp2_net.write_file_atomic(path, long_bytes)
assert_true(@ntcp2_net.file_exists(path))
assert_eq(@ntcp2_net.read_file_bytes(path), long_bytes)
let short_bytes = b"{:version 1}\n"
@ntcp2_net.write_file_atomic(path, short_bytes)
assert_eq(@ntcp2_net.read_file_bytes(path), short_bytes)
}
+28
View File
@@ -170,6 +170,15 @@ extern "C" fn c_read_file_at(
max_len : Int32,
) -> Int32 = "moon_i2p_net_read_file_at"
///|
#borrow(path, data)
extern "C" fn c_write_file_atomic(
path : Bytes,
path_len : Int32,
data : Bytes,
data_len : Int32,
) -> Int32 = "moon_i2p_net_write_file_atomic"
///|
#borrow(dst)
extern "C" fn c_home_dir(dst : FixedArray[Byte], max_len : Int32) -> Int32 = "moon_i2p_net_home_dir"
@@ -320,6 +329,25 @@ pub fn read_file_at(
Bytes::from_array(buf)[:got].to_owned()
}
///|
/// Atomically replaces `path` with `data` via temp-file write + `fsync` + `rename`.
pub fn write_file_atomic(
path : String,
data : Bytes,
) -> Unit raise Ntcp2NetError {
let pb = @encoding.string_to_utf8_bytes(path)
if c_write_file_atomic(pb, pb.length(), data, data.length()) < 0 {
raise IoError("write_file_atomic(\{path}) failed: \{last_os_error()}")
}
}
///|
/// Returns `true` if `path` exists and can be `stat`ed.
pub fn file_exists(path : String) -> Bool {
let pb = @encoding.string_to_utf8_bytes(path)
c_file_size(pb, pb.length()) >= 0
}
///|
/// Returns the `HOME` directory path, or empty string if unset.
pub fn get_home_dir() -> String {