bin/xbps-rindex: better error handling for writing repodata archives

This fixes issues when writes fail (as example if the disk is full),
where xbps would create empty repodata or stagedata archives.
This commit is contained in:
Duncan Overbruck 2019-10-25 23:00:35 +02:00 committed by Juan RP
parent 04d5554ed2
commit 7b4a925302
2 changed files with 34 additions and 22 deletions

View File

@ -60,7 +60,8 @@ repodata_flush(struct xbps_handle *xhp, const char *repodir,
umask(mask);
/* Create and write our repository archive */
ar = archive_write_new();
assert(ar);
if (ar == NULL)
return false;
if (compression == NULL || strcmp(compression, "gzip") == 0) {
archive_write_add_filter_gzip(ar);
@ -83,11 +84,13 @@ repodata_flush(struct xbps_handle *xhp, const char *repodir,
}
archive_write_set_format_pax_restricted(ar);
archive_write_open_fd(ar, repofd);
if (archive_write_open_fd(ar, repofd) != ARCHIVE_OK)
return false;
/* XBPS_REPOIDX */
buf = xbps_dictionary_externalize(idx);
assert(buf);
if (buf == NULL)
return false;
rv = xbps_archive_append_buf(ar, buf, strlen(buf),
XBPS_REPOIDX, 0644, "root", "root");
free(buf);
@ -98,6 +101,8 @@ repodata_flush(struct xbps_handle *xhp, const char *repodir,
if (meta == NULL) {
/* fake entry */
buf = strdup("DEADBEEF");
if (buf == NULL)
return false;
} else {
buf = xbps_dictionary_externalize(meta);
}
@ -108,7 +113,10 @@ repodata_flush(struct xbps_handle *xhp, const char *repodir,
return false;
/* Write data to tempfile and rename */
archive_write_finish(ar);
if (archive_write_close(ar) != ARCHIVE_OK)
return false;
if (archive_write_free(ar) != ARCHIVE_OK)
return false;
#ifdef HAVE_FDATASYNC
fdatasync(repofd);
#else

View File

@ -83,7 +83,8 @@ xbps_archive_append_buf(struct archive *ar, const void *buf, const size_t buflen
assert(gname);
entry = archive_entry_new();
assert(entry);
if (entry == NULL)
return archive_errno(ar);
archive_entry_set_filetype(entry, AE_IFREG);
archive_entry_set_perm(entry, mode);
@ -100,7 +101,10 @@ xbps_archive_append_buf(struct archive *ar, const void *buf, const size_t buflen
archive_entry_free(entry);
return archive_errno(ar);
}
archive_write_finish_entry(ar);
if (archive_write_finish_entry(ar) != ARCHIVE_OK) {
archive_entry_free(entry);
return archive_errno(ar);
}
archive_entry_free(entry);
return 0;