Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rust/MANIFEST.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

200 changes: 188 additions & 12 deletions rust/generated/src__storage__storage_migration.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 58 additions & 1 deletion src/storage/storage_migration.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -694,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. */
Expand Down Expand Up @@ -721,7 +770,15 @@ 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) {
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);
goto done;
}
Expand Down
Loading
Loading