Commit 255fd81d authored by Timo Teräs's avatar Timo Teräs

rework error handling for write streams

parent 2a6896b2
......@@ -1187,8 +1187,7 @@ static int apk_db_index_write_nr_cache(struct apk_database *db)
"installed",
"installed.new",
0644);
if (os == NULL)
return -EIO;
if (IS_ERR_OR_NULL(os)) return PTR_ERR(os);
ctx.os = os;
list_for_each_entry(ipkg, &db->installed.packages, installed_pkgs_list) {
......@@ -1648,36 +1647,29 @@ int apk_db_write_config(struct apk_database *db)
apk_world_file,
apk_world_file_tmp,
0644);
if (os == NULL)
return -1;
if (IS_ERR_OR_NULL(os)) return PTR_ERR(os);
apk_deps_write(db, db->world, os, APK_BLOB_PTR_LEN("\n", 1));
os->write(os, "\n", 1);
r = os->close(os);
if (r < 0)
return r;
if (r < 0) return r;
os = apk_ostream_to_file(db->root_fd,
apk_installed_file,
apk_installed_file_tmp,
0644);
if (os == NULL)
return -1;
if (IS_ERR_OR_NULL(os)) return PTR_ERR(os);
apk_db_write_fdb(db, os);
r = os->close(os);
if (r < 0)
return r;
if (r < 0) return r;
os = apk_ostream_to_file(db->root_fd,
apk_scripts_file,
apk_scripts_file_tmp,
0644);
if (os == NULL)
return -1;
if (IS_ERR_OR_NULL(os)) return PTR_ERR(os);
apk_db_scriptdb_write(db, os);
r = os->close(os);
if (r < 0)
return r;
if (r < 0) return r;
apk_db_index_write_nr_cache(db);
......@@ -1685,12 +1677,10 @@ int apk_db_write_config(struct apk_database *db)
apk_triggers_file,
apk_triggers_file_tmp,
0644);
if (os == NULL)
return -1;
if (IS_ERR_OR_NULL(os)) return PTR_ERR(os);
apk_db_triggers_write(db, os);
r = os->close(os);
if (r < 0)
return r;
if (r < 0) return r;
return 0;
}
......
......@@ -205,6 +205,7 @@ static int index_main(void *ctx, struct apk_database *db, struct apk_string_arra
os = apk_ostream_to_file(AT_FDCWD, ictx->output, NULL, 0644);
else
os = apk_ostream_to_fd(STDOUT_FILENO);
if (IS_ERR_OR_NULL(os)) return -1;
if (ictx->method == APK_SIGN_GENERATE) {
struct apk_ostream *counter;
......
......@@ -130,9 +130,11 @@ static void info_who_owns(struct info_ctx *ctx, struct apk_database *db,
}
if (apk_verbosity < 1 && deps->num != 0) {
os = apk_ostream_to_fd(STDOUT_FILENO);
apk_deps_write(db, deps, os, APK_BLOB_PTR_LEN(" ", 1));
os->write(os, "\n", 1);
os->close(os);
if (!IS_ERR_OR_NULL(os)) {
apk_deps_write(db, deps, os, APK_BLOB_PTR_LEN(" ", 1));
os->write(os, "\n", 1);
os->close(os);
}
}
apk_dependency_array_free(&deps);
}
......
......@@ -715,13 +715,12 @@ struct apk_ostream *apk_ostream_to_fd(int fd)
{
struct apk_fd_ostream *fos;
if (fd < 0)
return NULL;
if (fd < 0) return ERR_PTR(-EBADF);
fos = malloc(sizeof(struct apk_fd_ostream));
if (fos == NULL) {
close(fd);
return NULL;
return ERR_PTR(-ENOMEM);
}
*fos = (struct apk_fd_ostream) {
......@@ -742,14 +741,12 @@ struct apk_ostream *apk_ostream_to_file(int atfd,
int fd;
fd = openat(atfd, tmpfile ?: file, O_CREAT | O_RDWR | O_TRUNC | O_CLOEXEC, mode);
if (fd < 0)
return NULL;
if (fd < 0) return ERR_PTR(-errno);
fcntl(fd, F_SETFD, FD_CLOEXEC);
os = apk_ostream_to_fd(fd);
if (os == NULL)
return NULL;
if (IS_ERR_OR_NULL(os)) return ERR_CAST(os);
if (tmpfile != NULL) {
struct apk_fd_ostream *fos =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment