From 255fd81d79c49f6e5dbdb0df371d8ec7de600917 Mon Sep 17 00:00:00 2001 From: Timo Teräs Date: Tue, 10 Mar 2015 13:15:58 +0200 Subject: rework error handling for write streams --- src/database.c | 28 +++++++++------------------- src/index.c | 1 + src/info.c | 8 +++++--- src/io.c | 11 ++++------- 4 files changed, 19 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/database.c b/src/database.c index bbf5a9d..44d0b20 100644 --- a/src/database.c +++ b/src/database.c @@ -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; } diff --git a/src/index.c b/src/index.c index 2fe8cb4..ccb4713 100644 --- a/src/index.c +++ b/src/index.c @@ -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; diff --git a/src/info.c b/src/info.c index 43ced63..b163aa5 100644 --- a/src/info.c +++ b/src/info.c @@ -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); } diff --git a/src/io.c b/src/io.c index f183c2a..6821c54 100644 --- a/src/io.c +++ b/src/io.c @@ -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 = -- cgit v1.2.3-60-g2f50