From 36b6d22ed7eebeffde75eca902cfbb33e04cb324 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Mon, 30 Nov 2026 07:25:59 -0500 Subject: [PATCH 1/5] Apply suggested fix to src/storage/storage_migration.c from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- src/storage/storage_migration.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/storage/storage_migration.c b/src/storage/storage_migration.c index f0bee2e72..b6786a35e 100644 --- a/src/storage/storage_migration.c +++ b/src/storage/storage_migration.c @@ -721,7 +721,13 @@ static int remove_verified_source(const storage_migration_job_t *job, } source = openat(parent, leaf, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); if (source < 0) { - if (errno == ENOENT) { result = 0; goto done; } + if (errno == ENOENT) { + safe_strcpy(error, + "Source missing at cleanup path; previous attempt may have staged it. " + "Manual cleanup verification required", + STORAGE_MIGRATION_ERROR_MAX, 0); + goto done; + } set_error(error, "Cannot open source", source_path); goto done; } From 60044146171eeae3d26557a1efcd71a93c2aea78 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Mon, 30 Nov 2026 07:26:01 -0500 Subject: [PATCH 2/5] Apply suggested fix to src/storage/storage_migration.c from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- src/storage/storage_migration.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/storage/storage_migration.c b/src/storage/storage_migration.c index b6786a35e..cb93137b0 100644 --- a/src/storage/storage_migration.c +++ b/src/storage/storage_migration.c @@ -617,6 +617,25 @@ static int copy_and_verify( set_error(error, "Cannot sync temporary destination", temporary_path); result = -1; } + if (result == 0) { + struct stat source_status_final; + if (fstat(source, &source_status_final) != 0) { + set_error(error, "Cannot stat source after copy", source_path); + result = -1; + } else if (source_status_final.st_dev != source_status.st_dev || + source_status_final.st_ino != source_status.st_ino || + source_status_final.st_size != source_status.st_size || + source_status_final.st_mtim.tv_sec != source_status.st_mtim.tv_sec || + source_status_final.st_mtim.tv_nsec != source_status.st_mtim.tv_nsec || + source_status_final.st_ctim.tv_sec != source_status.st_ctim.tv_sec || + source_status_final.st_ctim.tv_nsec != source_status.st_ctim.tv_nsec || + copied != (uint64_t)source_status.st_size || + copied != (uint64_t)source_status_final.st_size) { + safe_strcpy(error, "Source changed during copy", + STORAGE_MIGRATION_ERROR_MAX, 0); + result = -1; + } + } free(buffer); close(source); char source_checksum[STORAGE_MIGRATION_CHECKSUM_MAX]; From 7abcee37758c10c53fcdf637ff945725e35cac65 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Mon, 30 Nov 2026 07:26:02 -0500 Subject: [PATCH 3/5] Apply suggested fix to src/storage/storage_migration.c from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- src/storage/storage_migration.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/storage/storage_migration.c b/src/storage/storage_migration.c index cb93137b0..7ae509076 100644 --- a/src/storage/storage_migration.c +++ b/src/storage/storage_migration.c @@ -270,7 +270,8 @@ static bool same_inode(const struct stat *a, const struct stat *b) { static bool same_verified_file(const struct stat *a, const struct stat *b) { return same_inode(a, b) && S_ISREG(b->st_mode) && a->st_size == b->st_size && - a->st_mtim.tv_sec == b->st_mtim.tv_sec && a->st_mtim.tv_nsec == b->st_mtim.tv_nsec; + a->st_mtim.tv_sec == b->st_mtim.tv_sec && a->st_mtim.tv_nsec == b->st_mtim.tv_nsec && + a->st_ctim.tv_sec == b->st_ctim.tv_sec && a->st_ctim.tv_nsec == b->st_ctim.tv_nsec; } static void release_publication(migration_publication_t *publication) { From 241cfb2889d3559057a319c56395b42834b67c8f Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Mon, 30 Nov 2026 07:30:29 -0500 Subject: [PATCH 4/5] Detect staged migration sources without rejecting atomic renames --- src/storage/storage_migration.c | 43 ++++++++++++++++--- .../unit/test_storage_migration_publication.c | 32 ++++++++++++++ 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/storage/storage_migration.c b/src/storage/storage_migration.c index 7ae509076..dc281254c 100644 --- a/src/storage/storage_migration.c +++ b/src/storage/storage_migration.c @@ -270,8 +270,7 @@ static bool same_inode(const struct stat *a, const struct stat *b) { static bool same_verified_file(const struct stat *a, const struct stat *b) { return same_inode(a, b) && S_ISREG(b->st_mode) && a->st_size == b->st_size && - a->st_mtim.tv_sec == b->st_mtim.tv_sec && a->st_mtim.tv_nsec == b->st_mtim.tv_nsec && - a->st_ctim.tv_sec == b->st_ctim.tv_sec && a->st_ctim.tv_nsec == b->st_ctim.tv_nsec; + a->st_mtim.tv_sec == b->st_mtim.tv_sec && a->st_mtim.tv_nsec == b->st_mtim.tv_nsec; } static void release_publication(migration_publication_t *publication) { @@ -714,6 +713,36 @@ static int destination_retained(const storage_migration_job_t *job) { return retained; } +/* A crash after staging the source can leave its verified inode outside the + * original pathname. Distinguish that case from a source removed elsewhere. */ +static int staged_source_exists(int parent, const char *job_uuid) { + char prefix[LIGHTNVR_UUID_STRING_SIZE + 24]; + int length = snprintf(prefix, sizeof(prefix), ".migration-delete-%s-", job_uuid); + if (length < 0 || (size_t)length >= sizeof(prefix)) return -1; + int scan_fd = dup(parent); + if (scan_fd < 0) return -1; + DIR *directory = fdopendir(scan_fd); + if (!directory) { close(scan_fd); return -1; } + int result = 0; + for (;;) { + errno = 0; + struct dirent *entry = readdir(directory); + if (!entry) { if (errno) result = -1; break; } + if (strncmp(entry->d_name, prefix, (size_t)length) != 0) continue; + int private_fd = openat(parent, entry->d_name, + O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); + if (private_fd < 0) { result = -1; break; } + struct stat artifact; + int found = fstatat(private_fd, "verified", &artifact, AT_SYMLINK_NOFOLLOW); + int saved_errno = errno; + close(private_fd); + if (found == 0) { result = 1; break; } + if (saved_errno != ENOENT) { result = -1; break; } + } + closedir(directory); + return result; +} + /* Keep the verified inode open while moving its directory entry out of the * recording namespace. Only the entry inside our private directory can be * removed; a replacement at the original path is never unlinked. */ @@ -742,10 +771,12 @@ static int remove_verified_source(const storage_migration_job_t *job, source = openat(parent, leaf, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); if (source < 0) { if (errno == ENOENT) { - safe_strcpy(error, - "Source missing at cleanup path; previous attempt may have staged it. " - "Manual cleanup verification required", - STORAGE_MIGRATION_ERROR_MAX, 0); + int staged = staged_source_exists(parent, job->uuid); + if (staged == 0) { result = 0; goto done; } + safe_strcpy(error, staged > 0 ? + "Source retained in private cleanup directory; manual verification required" : + "Cannot inspect private source cleanup directories", + STORAGE_MIGRATION_ERROR_MAX, 0); goto done; } set_error(error, "Cannot open source", source_path); diff --git a/tests/unit/test_storage_migration_publication.c b/tests/unit/test_storage_migration_publication.c index 01fdcb940..18cc243a3 100644 --- a/tests/unit/test_storage_migration_publication.c +++ b/tests/unit/test_storage_migration_publication.c @@ -37,6 +37,7 @@ static bool restore_collision, commit_failure_replacement, cancel_after_publicat static bool replace_source_after_staging; static int replace_source_after_commit; static bool remove_source_after_commit; +static bool stage_source_after_commit; static int existing_bind_injection; static char private_artifact[MAX_PATH_LENGTH], active_job[LIGHTNVR_UUID_STRING_SIZE]; static char relocated_parent[256]; @@ -245,6 +246,17 @@ db_storage_migration_result_t __wrap_db_storage_migration_commit_location( remove_source_after_commit = false; TEST_ASSERT_EQUAL_INT(0, unlink(source_path)); } + if (result == DB_STORAGE_MIGRATION_OK && stage_source_after_commit) { + stage_source_after_commit = false; + char parent[MAX_PATH_LENGTH]; + int n = snprintf(parent, sizeof(parent), "%s/camera/.migration-delete-%s-fixture", + source_root, job->uuid); + TEST_ASSERT_TRUE(n > 0 && (size_t)n < sizeof(parent)); + TEST_ASSERT_EQUAL_INT(0, mkdir(parent, 0700)); + n = snprintf(private_artifact, sizeof(private_artifact), "%s/verified", parent); + TEST_ASSERT_TRUE(n > 0 && (size_t)n < sizeof(private_artifact)); + TEST_ASSERT_EQUAL_INT(0, rename(source_path, private_artifact)); + } return result; } static void assert_bytes(const char *path, const char *expected) { @@ -289,6 +301,7 @@ void setUp(void) { cancel_after_publication = false; replace_source_after_commit = false; remove_source_after_commit = false; + stage_source_after_commit = false; replace_source_after_staging = false; fail_source_unlinkat = false; existing_bind_injection = 0; private_artifact[0] = 0; @@ -402,6 +415,24 @@ static void test_source_already_removed_after_commit_completes_cleanup(void) { assert_bytes(destination_path, payload); } +static void test_staged_source_missing_at_path_remains_pending(void) { + storage_migration_job_t job; + create_job(&job, "move"); + stage_source_after_commit = true; + TEST_ASSERT_EQUAL_INT(1, storage_migration_process_one()); + TEST_ASSERT_FALSE(stage_source_after_commit); + TEST_ASSERT_EQUAL_INT(DB_STORAGE_MIGRATION_OK, db_storage_migration_get(job.uuid, &job)); + TEST_ASSERT_EQUAL_STRING("cleanup_pending", job.state); + TEST_ASSERT_NOT_EQUAL_INT(0, access(source_path, F_OK)); + assert_bytes(private_artifact, payload); + TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_exec(get_db_handle(), + "UPDATE storage_migration_jobs SET next_attempt_at=0;", NULL, NULL, NULL)); + TEST_ASSERT_EQUAL_INT(1, storage_migration_process_one()); + TEST_ASSERT_EQUAL_INT(DB_STORAGE_MIGRATION_OK, db_storage_migration_get(job.uuid, &job)); + TEST_ASSERT_EQUAL_STRING("cleanup_pending", job.state); + assert_bytes(private_artifact, payload); +} + static void test_restored_source_identity_allows_cleanup_retry(void) { storage_migration_job_t job; create_job(&job, "move"); @@ -795,6 +826,7 @@ int main(void) { RUN_TEST(test_same_bytes_source_replacement_is_retained); RUN_TEST(test_source_path_reused_during_cleanup_is_preserved); RUN_TEST(test_source_already_removed_after_commit_completes_cleanup); + RUN_TEST(test_staged_source_missing_at_path_remains_pending); RUN_TEST(test_restored_source_identity_allows_cleanup_retry); RUN_TEST(test_normal_copy_keeps_source_and_verified_replica); RUN_TEST(test_regular_destination_appearing_after_check_is_not_overwritten); From 4c02123f0db52a3b5ea4dcf886d03aa0b5d280cc Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 30 Nov 2026 12:37:57 +0000 Subject: [PATCH 5/5] lightnvr-rs: regenerate from C Generated by tools/rust-transpile/generate.py for c19b9d3. --- rust/MANIFEST.json | 4 +- .../src__storage__storage_migration.rs | 200 ++++++++++++++++-- 2 files changed, 190 insertions(+), 14 deletions(-) diff --git a/rust/MANIFEST.json b/rust/MANIFEST.json index 7cec11516..7c46ba5ec 100644 --- a/rust/MANIFEST.json +++ b/rust/MANIFEST.json @@ -605,9 +605,9 @@ }, { "c": "src/storage/storage_migration.c", - "c_sha256": "c0e14b2ac66f3891621cf08f0326e9f5053e3af706659827771c0f0f34f1222c", + "c_sha256": "52f57376111f08c64e2a954546c2fccfd10fb54fe777215768b12e53f7e84382", "rust": "generated/src__storage__storage_migration.rs", - "rust_sha256": "4ccfb3fc4e5a38547e06b9989c5995f0ac3f40c9c1a7348c629ef6e2ff872725", + "rust_sha256": "252cbe375ec3ae5c939fc2b5ddd61143e6dd8a3cab58ca65fa9087b9e10a01c7", "adapted": true }, { diff --git a/rust/generated/src__storage__storage_migration.rs b/rust/generated/src__storage__storage_migration.rs index 5a9ae4ec7..a7cf02601 100644 --- a/rust/generated/src__storage__storage_migration.rs +++ b/rust/generated/src__storage__storage_migration.rs @@ -1,12 +1,13 @@ use ::f128; use ::libc; unsafe extern "C" { + pub type __dirstream; pub type sqlite3_stmt; pub type sqlite3; - pub type __dirstream; unsafe fn __errno_location() -> *mut ::core::ffi::c_int; unsafe fn closedir(__dirp: *mut DIR) -> ::core::ffi::c_int; unsafe fn opendir(__name: *const ::core::ffi::c_char) -> *mut DIR; + unsafe fn fdopendir(__fd: ::core::ffi::c_int) -> *mut DIR; unsafe fn readdir(__dirp: *mut DIR) -> *mut dirent; unsafe fn open( __file: *const ::core::ffi::c_char, @@ -100,6 +101,7 @@ unsafe extern "C" { __buf: *const ::core::ffi::c_void, __n: size_t, ) -> ssize_t; + unsafe fn dup(__fd: ::core::ffi::c_int) -> ::core::ffi::c_int; unsafe fn geteuid() -> __uid_t; unsafe fn unlinkat( __fd: ::core::ffi::c_int, @@ -501,6 +503,16 @@ pub struct mbedtls_sha256_context { pub private_is224: ::core::ffi::c_int, } pub type uint32_t = u32; +pub type DIR = __dirstream; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct dirent { + pub d_ino: __ino_t, + pub d_off: __off_t, + pub d_reclen: ::core::ffi::c_ushort, + pub d_type: ::core::ffi::c_uchar, + pub d_name: [::core::ffi::c_char; 256], +} #[derive(Copy, Clone)] #[repr(C)] pub struct storage_target_t { @@ -567,16 +579,6 @@ pub type atomic_bool = bool; pub type sqlite3_destructor_type = Option ()>; pub type sqlite3_int64 = sqlite_int64; pub type sqlite_int64 = ::core::ffi::c_longlong; -pub type DIR = __dirstream; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct dirent { - pub d_ino: __ino_t, - pub d_off: __off_t, - pub d_reclen: ::core::ffi::c_ushort, - pub d_type: ::core::ffi::c_uchar, - pub d_name: [::core::ffi::c_char; 256], -} #[derive(Clone, Copy, PartialEq, Eq)] #[repr(transparent)] pub struct C2Rust_Unnamed_0(pub ::core::ffi::c_uint); @@ -2740,6 +2742,59 @@ unsafe extern "C" fn copy_and_verify( ); result = -1 as ::core::ffi::c_int; } + if result == 0 as ::core::ffi::c_int { + let mut source_status_final: stat = stat { + st_dev: 0, + st_ino: 0, + st_nlink: 0, + st_mode: 0, + st_uid: 0, + st_gid: 0, + __pad0: 0, + st_rdev: 0, + st_size: 0, + st_blksize: 0, + st_blocks: 0, + st_atim: timespec { + tv_sec: 0, + tv_nsec: 0, + }, + st_mtim: timespec { + tv_sec: 0, + tv_nsec: 0, + }, + st_ctim: timespec { + tv_sec: 0, + tv_nsec: 0, + }, + __glibc_reserved: [0; 3], + }; + if fstat(source, &raw mut source_status_final) != 0 as ::core::ffi::c_int { + set_error( + error, + b"Cannot stat source after copy\0".as_ptr() as *const ::core::ffi::c_char, + source_path, + ); + result = -1 as ::core::ffi::c_int; + } else if source_status_final.st_dev != source_status.st_dev + || source_status_final.st_ino != source_status.st_ino + || source_status_final.st_size != source_status.st_size + || source_status_final.st_mtim.tv_sec != source_status.st_mtim.tv_sec + || source_status_final.st_mtim.tv_nsec != source_status.st_mtim.tv_nsec + || source_status_final.st_ctim.tv_sec != source_status.st_ctim.tv_sec + || source_status_final.st_ctim.tv_nsec != source_status.st_ctim.tv_nsec + || copied != source_status.st_size as uint64_t + || copied != source_status_final.st_size as uint64_t + { + safe_strcpy( + error, + b"Source changed during copy\0".as_ptr() as *const ::core::ffi::c_char, + STORAGE_MIGRATION_ERROR_MAX as size_t, + 0 as size_t, + ); + result = -1 as ::core::ffi::c_int; + } + } free(buffer as *mut ::core::ffi::c_void); close(source); let mut source_checksum: [::core::ffi::c_char; 65] = [0; 65]; @@ -2952,6 +3007,108 @@ unsafe extern "C" fn destination_retained( pthread_mutex_unlock(mutex); return retained; } +unsafe extern "C" fn staged_source_exists( + mut parent: ::core::ffi::c_int, + mut job_uuid: *const ::core::ffi::c_char, +) -> ::core::ffi::c_int { + let mut prefix: [::core::ffi::c_char; 61] = [0; 61]; + let mut length: ::core::ffi::c_int = snprintf( + &raw mut prefix as *mut ::core::ffi::c_char, + ::core::mem::size_of::<[::core::ffi::c_char; 61]>(), + b".migration-delete-%s-\0".as_ptr() as *const ::core::ffi::c_char, + job_uuid, + ); + if length < 0 as ::core::ffi::c_int + || length as size_t >= ::core::mem::size_of::<[::core::ffi::c_char; 61]>() + { + return -1 as ::core::ffi::c_int; + } + let mut scan_fd: ::core::ffi::c_int = dup(parent); + if scan_fd < 0 as ::core::ffi::c_int { + return -1 as ::core::ffi::c_int; + } + let mut directory: *mut DIR = fdopendir(scan_fd); + if directory.is_null() { + close(scan_fd); + return -1 as ::core::ffi::c_int; + } + let mut result: ::core::ffi::c_int = 0 as ::core::ffi::c_int; + loop { + *__errno_location() = 0 as ::core::ffi::c_int; + let mut entry: *mut dirent = readdir(directory); + if entry.is_null() { + if *__errno_location() != 0 { + result = -1 as ::core::ffi::c_int; + } + break; + } else { + if strncmp( + &raw mut (*entry).d_name as *mut ::core::ffi::c_char, + &raw mut prefix as *mut ::core::ffi::c_char, + length as size_t, + ) != 0 as ::core::ffi::c_int + { + continue; + } + let mut private_fd: ::core::ffi::c_int = openat( + parent, + &raw mut (*entry).d_name as *mut ::core::ffi::c_char, + O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC, + ); + if private_fd < 0 as ::core::ffi::c_int { + result = -1 as ::core::ffi::c_int; + break; + } else { + let mut artifact: stat = stat { + st_dev: 0, + st_ino: 0, + st_nlink: 0, + st_mode: 0, + st_uid: 0, + st_gid: 0, + __pad0: 0, + st_rdev: 0, + st_size: 0, + st_blksize: 0, + st_blocks: 0, + st_atim: timespec { + tv_sec: 0, + tv_nsec: 0, + }, + st_mtim: timespec { + tv_sec: 0, + tv_nsec: 0, + }, + st_ctim: timespec { + tv_sec: 0, + tv_nsec: 0, + }, + __glibc_reserved: [0; 3], + }; + let mut found: ::core::ffi::c_int = fstatat( + private_fd, + b"verified\0".as_ptr() as *const ::core::ffi::c_char, + &raw mut artifact, + AT_SYMLINK_NOFOLLOW, + ); + let mut saved_errno: ::core::ffi::c_int = *__errno_location(); + close(private_fd); + if found == 0 as ::core::ffi::c_int { + result = 1 as ::core::ffi::c_int; + break; + } else { + if saved_errno == ENOENT { + continue; + } + result = -1 as ::core::ffi::c_int; + break; + } + } + } + } + closedir(directory); + return result; +} unsafe extern "C" fn remove_verified_source( mut job: *const storage_migration_job_t, mut source_path: *const ::core::ffi::c_char, @@ -3163,7 +3320,26 @@ unsafe extern "C" fn remove_verified_source( source = openat(parent, leaf, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); if source < 0 as ::core::ffi::c_int { if *__errno_location() == ENOENT { - result = 0 as ::core::ffi::c_int; + let mut staged: ::core::ffi::c_int = staged_source_exists( + parent, + &raw const (*job).uuid as *const ::core::ffi::c_char, + ); + if staged == 0 as ::core::ffi::c_int { + result = 0 as ::core::ffi::c_int; + } else { + safe_strcpy( + error, + if staged > 0 as ::core::ffi::c_int { + b"Source retained in private cleanup directory; manual verification required\0" + .as_ptr() as *const ::core::ffi::c_char + } else { + b"Cannot inspect private source cleanup directories\0".as_ptr() + as *const ::core::ffi::c_char + }, + STORAGE_MIGRATION_ERROR_MAX as size_t, + 0 as size_t, + ); + } } else { set_error( error,