feat: persist I2P torrent list to EDN state file across restarts
- 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:
1 parent
b803e79199
commit
dd5b132b5a
17 files changed
+1105
-39
No files matched your search
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user