summaryrefslogtreecommitdiff
path: root/src/io.c
diff options
context:
space:
mode:
authorTimo Teräs <timo.teras@iki.fi>2021-07-16 19:01:41 +0300
committerTimo Teräs <timo.teras@iki.fi>2021-07-22 15:30:08 +0300
commit3b00c0dc808f4b6c3809c91a67709ca2d7bf8865 (patch)
tree2700fe81689ae7210516196b8db58cf987536fba /src/io.c
parent69bcdd23b94dab9e0ef67bd480e7f875b06dc71d (diff)
downloadapk-tools-3b00c0dc808f4b6c3809c91a67709ca2d7bf8865.tar.gz
apk-tools-3b00c0dc808f4b6c3809c91a67709ca2d7bf8865.tar.bz2
apk-tools-3b00c0dc808f4b6c3809c91a67709ca2d7bf8865.tar.xz
apk-tools-3b00c0dc808f4b6c3809c91a67709ca2d7bf8865.zip
adb: unify various interfaces to adb_m_process
Removes code duplication, and puts important checks in one place. Support seamless decompression in adbdump.
Diffstat (limited to 'src/io.c')
-rw-r--r--src/io.c53
1 files changed, 45 insertions, 8 deletions
diff --git a/src/io.c b/src/io.c
index 0ec278b..ccd2ec0 100644
--- a/src/io.c
+++ b/src/io.c
@@ -70,6 +70,13 @@ void apk_file_meta_to_fd(int fd, struct apk_file_meta *meta)
futimens(fd, times);
}
+apk_blob_t apk_istream_mmap(struct apk_istream *is)
+{
+ if (is->flags & APK_ISTREAM_SINGLE_READ)
+ return APK_BLOB_PTR_LEN((char*)is->buf, is->buf_size);
+ return APK_BLOB_NULL;
+}
+
ssize_t apk_istream_read(struct apk_istream *is, void *ptr, size_t size)
{
ssize_t left = size, r = 0;
@@ -160,7 +167,7 @@ apk_blob_t apk_istream_get_max(struct apk_istream *is, size_t max)
if (is->ptr != is->end) {
apk_blob_t ret = APK_BLOB_PTR_LEN((char*)is->ptr, min((size_t)(is->end - is->ptr), max));
- is->ptr = is->end = 0;
+ is->ptr += ret.len;
return ret;
}
@@ -193,6 +200,41 @@ apk_blob_t apk_istream_get_delim(struct apk_istream *is, apk_blob_t token)
return (struct apk_blob) { .len = is->err < 0 ? is->err : 0 };
}
+static void blob_get_meta(struct apk_istream *is, struct apk_file_meta *meta)
+{
+ *meta = (struct apk_file_meta) { };
+}
+
+static ssize_t blob_read(struct apk_istream *is, void *ptr, size_t size)
+{
+ return 0;
+}
+
+static int blob_close(struct apk_istream *is)
+{
+ return is->err < 0 ? is->err : 0;
+}
+
+static const struct apk_istream_ops blob_istream_ops = {
+ .get_meta = blob_get_meta,
+ .read = blob_read,
+ .close = blob_close,
+};
+
+struct apk_istream *apk_istream_from_blob(struct apk_istream *is, apk_blob_t blob)
+{
+ *is = (struct apk_istream) {
+ .ops = &blob_istream_ops,
+ .buf = (uint8_t*) blob.ptr,
+ .buf_size = blob.len,
+ .ptr = (uint8_t*) blob.ptr,
+ .end = (uint8_t*) blob.ptr + blob.len,
+ .flags = APK_ISTREAM_SINGLE_READ,
+ .err = 1,
+ };
+ return is;
+}
+
static void segment_get_meta(struct apk_istream *is, struct apk_file_meta *meta)
{
struct apk_segment_istream *sis = container_of(is, struct apk_segment_istream, is);
@@ -493,14 +535,14 @@ struct apk_istream *apk_istream_from_fd(int fd)
return &fis->is;
}
-struct apk_istream *apk_istream_from_file(int atfd, const char *file)
+struct apk_istream *__apk_istream_from_file(int atfd, const char *file, int try_mmap)
{
int fd;
fd = openat(atfd, file, O_RDONLY | O_CLOEXEC);
if (fd < 0) return ERR_PTR(-errno);
- if (0) {
+ if (try_mmap) {
struct apk_istream *is = apk_mmap_istream_from_fd(fd);
if (!IS_ERR_OR_NULL(is)) return is;
}
@@ -834,11 +876,6 @@ int apk_dir_foreach_file(int dirfd, apk_dir_file_cb cb, void *ctx)
return ret;
}
-struct apk_istream *apk_istream_from_file_gz(int atfd, const char *file)
-{
- return apk_istream_gunzip(apk_istream_from_file(atfd, file));
-}
-
struct apk_fd_ostream {
struct apk_ostream os;
int fd;