summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimo Teräs <timo.teras@iki.fi>2023-04-10 20:17:25 +0300
committerTimo Teräs <timo.teras@iki.fi>2023-04-11 20:55:13 +0300
commitb3c4636ee213d8e37f21ecaae2748876b1063076 (patch)
treefb5fdbfad04cdc1e0935b66478c03169dfa7bae1
parent9176a977d9f0970e3251493fcd71f3c1be0834ae (diff)
downloadapk-tools-b3c4636ee213d8e37f21ecaae2748876b1063076.tar.gz
apk-tools-b3c4636ee213d8e37f21ecaae2748876b1063076.tar.bz2
apk-tools-b3c4636ee213d8e37f21ecaae2748876b1063076.tar.xz
apk-tools-b3c4636ee213d8e37f21ecaae2748876b1063076.zip
io: make apk_blob_from_* return error
-rw-r--r--src/apk_io.h4
-rw-r--r--src/database.c17
-rw-r--r--src/io.c43
-rw-r--r--src/package.c34
4 files changed, 49 insertions, 49 deletions
diff --git a/src/apk_io.h b/src/apk_io.h
index 09e537d..8f789e4 100644
--- a/src/apk_io.h
+++ b/src/apk_io.h
@@ -158,8 +158,8 @@ static inline int apk_ostream_close(struct apk_ostream *os)
return os->ops->close(os);
}
-apk_blob_t apk_blob_from_istream(struct apk_istream *istream, size_t size);
-apk_blob_t apk_blob_from_file(int atfd, const char *file);
+int apk_blob_from_istream(struct apk_istream *is, size_t size, apk_blob_t *b);
+int apk_blob_from_file(int atfd, const char *file, apk_blob_t *b);
#define APK_BTF_ADD_EOL 0x00000001
int apk_blob_to_file(int atfd, const char *file, apk_blob_t b, unsigned int flags);
diff --git a/src/database.c b/src/database.c
index 3b5bc38..9cad108 100644
--- a/src/database.c
+++ b/src/database.c
@@ -1180,9 +1180,9 @@ static int apk_db_read_state(struct apk_database *db, int flags)
* 4. scripts db
*/
if (!(flags & APK_OPENF_NO_WORLD)) {
- blob = world = apk_blob_from_file(db->root_fd, apk_world_file);
- if (APK_BLOB_IS_NULL(blob)) return -ENOENT;
- blob = apk_blob_trim(blob);
+ r = apk_blob_from_file(db->root_fd, apk_world_file, &world);
+ if (r) return r;
+ blob = apk_blob_trim(world);
apk_blob_pull_deps(&blob, db, &db->world);
free(world.ptr);
}
@@ -1336,8 +1336,7 @@ static int add_protected_paths_from_file(void *ctx, int dirfd, const char *file)
if (!file_ends_with_dot_list(file))
return 0;
- blob = apk_blob_from_file(dirfd, file);
- if (APK_BLOB_IS_NULL(blob))
+ if (apk_blob_from_file(dirfd, file, &blob))
return 0;
apk_blob_for_each_segment(blob, "\n", add_protected_path, db);
@@ -1431,8 +1430,7 @@ static int add_repos_from_file(void *ctx, int dirfd, const char *file)
return 0;
}
- blob = apk_blob_from_file(dirfd, file);
- if (APK_BLOB_IS_NULL(blob)) {
+ if (apk_blob_from_file(dirfd, file, &blob)) {
if (dirfd != AT_FDCWD) return 0;
apk_error("failed to read repositories: %s", file);
apk_message("NOTE: --repositories-file is relative to the startup directory since apk 2.12.0_rc2");
@@ -1591,8 +1589,7 @@ int apk_db_open(struct apk_database *db, struct apk_db_options *dbopts)
write_arch = TRUE;
} else {
apk_blob_t arch;
- arch = apk_blob_from_file(db->root_fd, apk_arch_file);
- if (!APK_BLOB_IS_NULL(arch)) {
+ if (!apk_blob_from_file(db->root_fd, apk_arch_file, &arch)) {
db->arch = apk_atomize_dup(&db->atoms, apk_blob_trim(arch));
free(arch.ptr);
} else {
@@ -2233,7 +2230,7 @@ static int load_apkindex(void *sctx, const struct apk_file_info *fi,
repo = &ctx->db->repos[ctx->repo];
if (strcmp(fi->name, "DESCRIPTION") == 0) {
- repo->description = apk_blob_from_istream(is, fi->size);
+ r = apk_blob_from_istream(is, fi->size, &repo->description);
} else if (strcmp(fi->name, "APKINDEX") == 0) {
ctx->found = 1;
r = apk_db_index_read(ctx->db, is, ctx->repo);
diff --git a/src/io.c b/src/io.c
index 65dee6a..4aa847d 100644
--- a/src/io.c
+++ b/src/io.c
@@ -586,55 +586,62 @@ err:
return r;
}
-apk_blob_t apk_blob_from_istream(struct apk_istream *is, size_t size)
+int apk_blob_from_istream(struct apk_istream *is, size_t size, apk_blob_t *b)
{
void *ptr;
ssize_t rsize;
+ *b = APK_BLOB_NULL;
+
ptr = malloc(size);
- if (ptr == NULL)
- return APK_BLOB_NULL;
+ if (!ptr) return -ENOMEM;
rsize = apk_istream_read(is, ptr, size);
if (rsize < 0) {
free(ptr);
- return APK_BLOB_NULL;
+ return rsize;
}
if (rsize != size)
ptr = realloc(ptr, rsize);
- return APK_BLOB_PTR_LEN(ptr, rsize);
+ *b = APK_BLOB_PTR_LEN(ptr, rsize);
+ return 0;
}
-apk_blob_t apk_blob_from_file(int atfd, const char *file)
+int apk_blob_from_file(int atfd, const char *file, apk_blob_t *b)
{
- int fd;
struct stat st;
char *buf;
+ ssize_t n;
+ int fd;
- if (atfd_error(atfd)) return APK_BLOB_NULL;
+ *b = APK_BLOB_NULL;
- fd = openat(atfd, file, O_RDONLY | O_CLOEXEC);
- if (fd < 0)
- return APK_BLOB_NULL;
+ if (atfd_error(atfd)) return atfd;
- if (fstat(fd, &st) < 0)
- goto err_fd;
+ fd = openat(atfd, file, O_RDONLY | O_CLOEXEC);
+ if (fd < 0) goto err;
+ if (fstat(fd, &st) < 0) goto err_fd;
buf = malloc(st.st_size);
- if (buf == NULL)
- goto err_fd;
+ if (!buf) goto err_fd;
- if (read(fd, buf, st.st_size) != st.st_size)
+ n = read(fd, buf, st.st_size);
+ if (n != st.st_size) {
+ if (n >= 0) errno = EIO;
goto err_read;
+ }
close(fd);
- return APK_BLOB_PTR_LEN(buf, st.st_size);
+ *b = APK_BLOB_PTR_LEN(buf, st.st_size);
+ return 0;
+
err_read:
free(buf);
err_fd:
close(fd);
- return APK_BLOB_NULL;
+err:
+ return -errno;
}
int apk_blob_to_file(int atfd, const char *file, apk_blob_t b, unsigned int flags)
diff --git a/src/package.c b/src/package.c
index f91f62f..6596694 100644
--- a/src/package.c
+++ b/src/package.c
@@ -599,7 +599,7 @@ int apk_sign_ctx_process_file(struct apk_sign_ctx *ctx,
ctx->signature.pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
if (ctx->signature.pkey != NULL) {
ctx->md = md;
- ctx->signature.data = apk_blob_from_istream(is, fi->size);
+ apk_blob_from_istream(is, fi->size, &ctx->signature.data);
}
BIO_free(bio);
@@ -974,28 +974,24 @@ void apk_pkg_free(struct apk_package *pkg)
free(pkg);
}
-int apk_ipkg_add_script(struct apk_installed_package *ipkg,
- struct apk_istream *is,
- unsigned int type, unsigned int size)
+static int apk_ipkg_assign_script(struct apk_installed_package *ipkg, unsigned int type, apk_blob_t b)
{
- void *ptr;
- int r;
-
- if (type >= APK_SCRIPT_MAX)
+ if (type >= APK_SCRIPT_MAX) {
+ free(b.ptr);
return -1;
-
- ptr = malloc(size);
- r = apk_istream_read(is, ptr, size);
- if (r < 0) {
- free(ptr);
- return r;
}
-
- if (ipkg->script[type].ptr)
- free(ipkg->script[type].ptr);
- ipkg->script[type].ptr = ptr;
- ipkg->script[type].len = size;
+ if (ipkg->script[type].ptr) free(ipkg->script[type].ptr);
+ ipkg->script[type] = b;
return 0;
+ }
+
+int apk_ipkg_add_script(struct apk_installed_package *ipkg,
+ struct apk_istream *is,
+ unsigned int type, unsigned int size)
+{
+ apk_blob_t b;
+ apk_blob_from_istream(is, size, &b);
+ return apk_ipkg_assign_script(ipkg, type, b);
}
static inline int make_dirs(int root_fd, const char *dirname, mode_t dirmode, mode_t parentmode)