From 63bc750fecb00fc5878ca889204fc65510893778 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 11 Jun 2023 23:27:16 +0200 Subject: libio: remove single line wrapper functions Signed-off-by: David Oberhollenzer --- bin/gensquashfs/src/fstree_from_file.c | 2 +- bin/gensquashfs/src/sort_by_file.c | 2 +- bin/rdsquashfs/src/fill_files.c | 2 +- bin/sqfs2tar/src/sqfs2tar.c | 4 +- bin/sqfsdiff/src/extract.c | 2 +- bin/tar2sqfs/src/process_tarball.c | 2 +- include/io/istream.h | 99 ++++++++++++++-------------------- include/io/ostream.h | 78 ++++++++++----------------- lib/common/src/data_reader_dump.c | 4 +- lib/io/src/get_line.c | 4 +- lib/io/src/istream.c | 14 ++--- lib/io/src/xfrm/istream.c | 6 +-- lib/io/src/xfrm/ostream.c | 4 +- lib/io/test/istream_mem.c | 6 +-- lib/io/test/sparse_fb.c | 2 +- lib/io/test/xfrm.c | 4 +- lib/tar/src/iterator.c | 11 ++-- lib/tar/src/write_header.c | 6 +-- lib/tar/test/tar_write_simple.c | 10 ++-- 19 files changed, 110 insertions(+), 152 deletions(-) diff --git a/bin/gensquashfs/src/fstree_from_file.c b/bin/gensquashfs/src/fstree_from_file.c index dc7181f..325bce2 100644 --- a/bin/gensquashfs/src/fstree_from_file.c +++ b/bin/gensquashfs/src/fstree_from_file.c @@ -280,7 +280,7 @@ int fstree_from_file_stream(fstree_t *fs, istream_t *fp, const char *basepath) char *line; int ret; - filename = istream_get_filename(fp); + filename = fp->get_filename(fp); for (;;) { ret = istream_get_line(fp, &line, &line_num, diff --git a/bin/gensquashfs/src/sort_by_file.c b/bin/gensquashfs/src/sort_by_file.c index 4a1be49..e2b6338 100644 --- a/bin/gensquashfs/src/sort_by_file.c +++ b/bin/gensquashfs/src/sort_by_file.c @@ -244,7 +244,7 @@ int fstree_sort_files(fstree_t *fs, istream_t *sortfile) node->flags &= ~FLAG_FILE_ALREADY_MATCHED; } - filename = istream_get_filename(sortfile); + filename = sortfile->get_filename(sortfile); for (;;) { bool do_glob, path_glob, have_match; diff --git a/bin/rdsquashfs/src/fill_files.c b/bin/rdsquashfs/src/fill_files.c index 08e11f5..afc790e 100644 --- a/bin/rdsquashfs/src/fill_files.c +++ b/bin/rdsquashfs/src/fill_files.c @@ -155,7 +155,7 @@ static int fill_files(sqfs_data_reader_t *data, int flags) ret = sqfs_data_reader_dump(files[i].path, data, files[i].inode, fp, block_size); if (ret == 0) - ret = ostream_flush(fp); + ret = fp->flush(fp); sqfs_drop(fp); if (ret) diff --git a/bin/sqfs2tar/src/sqfs2tar.c b/bin/sqfs2tar/src/sqfs2tar.c index 43f9e78..fd8d5fa 100644 --- a/bin/sqfs2tar/src/sqfs2tar.c +++ b/bin/sqfs2tar/src/sqfs2tar.c @@ -63,7 +63,7 @@ static int terminate_archive(void) memset(buffer, '\0', sizeof(buffer)); - return ostream_append(out_file, buffer, sizeof(buffer)); + return out_file->append(out_file, buffer, sizeof(buffer)); } static sqfs_tree_node_t *tree_merge(sqfs_tree_node_t *lhs, @@ -253,7 +253,7 @@ int main(int argc, char **argv) if (terminate_archive()) goto out; - if (ostream_flush(out_file)) + if (out_file->flush(out_file)) goto out; status = EXIT_SUCCESS; diff --git a/bin/sqfsdiff/src/extract.c b/bin/sqfsdiff/src/extract.c index f2072d4..7e745cf 100644 --- a/bin/sqfsdiff/src/extract.c +++ b/bin/sqfsdiff/src/extract.c @@ -33,7 +33,7 @@ static int extract(sqfs_data_reader_t *data, const sqfs_inode_generic_t *inode, return -1; } - ostream_flush(fp); + fp->flush(fp); sqfs_drop(fp); return 0; } diff --git a/bin/tar2sqfs/src/process_tarball.c b/bin/tar2sqfs/src/process_tarball.c index 91572ec..69d430d 100644 --- a/bin/tar2sqfs/src/process_tarball.c +++ b/bin/tar2sqfs/src/process_tarball.c @@ -31,7 +31,7 @@ static int write_file(sqfs_writer_t *sqfs, dir_iterator_t *it, ret = istream_splice(in, out, cfg.block_size); } while (ret > 0); - ostream_flush(out); + out->flush(out); sqfs_drop(out); sqfs_drop(in); return ret; diff --git a/include/io/istream.h b/include/io/istream.h index 8e86891..90d6e0d 100644 --- a/include/io/istream.h +++ b/include/io/istream.h @@ -20,11 +20,50 @@ typedef struct istream_t { sqfs_object_t base; + /** + * @brief Peek into the data buffered in an istream + * + * If the internal buffer is empty, the function tries to fetch more, + * which can block. It returns a positive return code if there is no + * more data to be read, a negative error code if reading failed. Since + * this and other functions can alter the buffer pointer and contents, + * do not store the pointers returned here across function calls. + * + * Higher level functions like @ref istream_read (providing a + * Unix read() style API) are built on top of this primitive. + * + * @param strm A pointer to an istream_t implementation. + * @param out Returns a pointer into an internal buffer on success. + * @param size Returns the number of bytes available in the buffer. + * @param want A number of bytes that the reader would like to have. + * If there is less than this available, the implementation + * can choose to do a blocking precache. + * + * @return Zero on success, a negative error code on failure, + * a postive number on EOF. + */ int (*get_buffered_data)(struct istream_t *strm, const sqfs_u8 **out, size_t *size, size_t want); + /** + * @brief Mark a section of the internal buffer of an istream as used + * + * This marks the first `count` bytes of the internal buffer as used, + * forcing get_buffered_data to return data afterwards and potentially + * try to load more data. + * + * @param strm A pointer to an istream_t implementation. + * @param count The number of bytes used up. + */ void (*advance_buffer)(struct istream_t *strm, size_t count); + /** + * @brief Get the underlying filename of an input stream. + * + * @param strm The input stream to get the filename from. + * + * @return A string holding the underlying filename. + */ const char *(*get_filename)(struct istream_t *strm); } istream_t; @@ -81,66 +120,6 @@ SQFS_INTERNAL int istream_get_line(istream_t *strm, char **out, */ SQFS_INTERNAL sqfs_s32 istream_read(istream_t *strm, void *data, size_t size); -/** - * @brief Get the underlying filename of an input stream. - * - * @memberof istream_t - * - * @param strm The input stream to get the filename from. - * - * @return A string holding the underlying filename. - */ -SQFS_INLINE const char *istream_get_filename(istream_t *strm) -{ - return strm->get_filename(strm); -} - -/** - * @brief Peek into the data buffered in an istream - * - * @memberof istream_t - * - * If the internal buffer is empty, the function tries to fetch more, which can - * block, and returns a positive return code if there is no more data to be - * read. Since this and other functions can alter the buffer pointer and - * contents, do not store the pointers returned here across function calls. - * - * Higher level functions like @ref istream_read, providing a Unix read() style - * API are built on top of this primitive. - * - * @param strm A pointer to an istream_t implementation. - * @param out Returns a pointer into an internal buffer on success. - * @param size Returns the number of bytes available in the buffer. - * @param want A number of bytes that the reader would like to have. If there is - * less than this available, the implementation can choose to do a - * blocking precache. - * - * @return Zero on success, a negative error code on failure, - * a postive number on EOF. - */ -SQFS_INLINE int istream_get_buffered_data(istream_t *strm, const sqfs_u8 **out, - size_t *size, size_t want) -{ - return strm->get_buffered_data(strm, out, size, want); -} - -/** - * @brief Mark a section of the internal buffer of an istream as used - * - * @memberof istream_t - * - * This marks the first `count` bytes of the internal buffer as used, - * forcing @ref istream_get_buffered_data to return data afterwards - * and potentially try to load more data. - * - * @param strm A pointer to an istream_t implementation. - * @param count The number of bytes used up. - */ -SQFS_INLINE void istream_advance_buffer(istream_t *strm, size_t count) -{ - strm->advance_buffer(strm, count); -} - /** * @brief Skip over a number of bytes in an input stream. * diff --git a/include/io/ostream.h b/include/io/ostream.h index 9cc37ad..8fc8cef 100644 --- a/include/io/ostream.h +++ b/include/io/ostream.h @@ -19,12 +19,40 @@ typedef struct ostream_t { sqfs_object_t base; + /** + * @brief Append a block of data to an output stream. + * + * @param strm A pointer to an output stream. + * @param data A pointer to the data block to append. + * @param size The number of bytes to append. + * + * @return Zero on success, -1 on failure. + */ int (*append)(struct ostream_t *strm, const void *data, size_t size); int (*append_sparse)(struct ostream_t *strm, size_t size); + /** + * @brief Process all pending, buffered data and flush it to disk. + * + * If the stream performs some kind of transformation (e.g. transparent + * data compression), flushing caues the wrapped format to insert a + * termination token. Only call this function when you are absolutely + * DONE appending data, shortly before destroying the stream. + * + * @param strm A pointer to an output stream. + * + * @return Zero on success, -1 on failure. + */ int (*flush)(struct ostream_t *strm); + /** + * @brief Get the underlying filename of a output stream. + * + * @param strm The output stream to get the filename from. + * + * @return A string holding the underlying filename. + */ const char *(*get_filename)(struct ostream_t *strm); } ostream_t; @@ -37,23 +65,6 @@ enum { extern "C" { #endif -/** - * @brief Append a block of data to an output stream. - * - * @memberof ostream_t - * - * @param strm A pointer to an output stream. - * @param data A pointer to the data block to append. - * @param size The number of bytes to append. - * - * @return Zero on success, -1 on failure. - */ -SQFS_INLINE int ostream_append(ostream_t *strm, const void *data, - size_t size) -{ - return strm->append(strm, data, size); -} - /** * @brief Append a number of zero bytes to an output stream. * @@ -70,39 +81,6 @@ SQFS_INLINE int ostream_append(ostream_t *strm, const void *data, */ SQFS_INTERNAL int ostream_append_sparse(ostream_t *strm, size_t size); -/** - * @brief Process all pending, buffered data and flush it to disk. - * - * @memberof ostream_t - * - * If the stream performs some kind of transformation (e.g. transparent data - * compression), flushing caues the wrapped format to insert a termination - * token. Only call this function when you are absolutely DONE appending data, - * shortly before destroying the stream. - * - * @param strm A pointer to an output stream. - * - * @return Zero on success, -1 on failure. - */ -SQFS_INLINE int ostream_flush(ostream_t *strm) -{ - return strm->flush(strm); -} - -/** - * @brief Get the underlying filename of a output stream. - * - * @memberof ostream_t - * - * @param strm The output stream to get the filename from. - * - * @return A string holding the underlying filename. - */ -SQFS_INLINE const char *ostream_get_filename(ostream_t *strm) -{ - return strm->get_filename(strm); -} - #ifdef __cplusplus } #endif diff --git a/lib/common/src/data_reader_dump.c b/lib/common/src/data_reader_dump.c index 7902c25..29043f8 100644 --- a/lib/common/src/data_reader_dump.c +++ b/lib/common/src/data_reader_dump.c @@ -36,7 +36,7 @@ int sqfs_data_reader_dump(const char *name, sqfs_data_reader_t *data, return -1; } - err = ostream_append(fp, chunk, chunk_size); + err = fp->append(fp, chunk, chunk_size); free(chunk); if (err) @@ -54,7 +54,7 @@ int sqfs_data_reader_dump(const char *name, sqfs_data_reader_t *data, return -1; } - err = ostream_append(fp, chunk, chunk_size); + err = fp->append(fp, chunk, chunk_size); free(chunk); if (err) diff --git a/lib/io/src/get_line.c b/lib/io/src/get_line.c index 4dc221a..7ebce0e 100644 --- a/lib/io/src/get_line.c +++ b/lib/io/src/get_line.c @@ -50,7 +50,7 @@ int istream_get_line(istream_t *strm, char **out, const sqfs_u8 *ptr; int ret; - ret = istream_get_buffered_data(strm, &ptr, &avail, 0); + ret = strm->get_buffered_data(strm, &ptr, &avail, 0); if (ret < 0) goto fail_free; if (ret > 0) { @@ -85,7 +85,7 @@ int istream_get_line(istream_t *strm, char **out, line_len += count; line[line_len] = '\0'; - istream_advance_buffer(strm, i); + strm->advance_buffer(strm, i); if (have_line) { if (line_len > 0 && line[line_len - 1] == '\r') diff --git a/lib/io/src/istream.c b/lib/io/src/istream.c index 9340dd6..473f1af 100644 --- a/lib/io/src/istream.c +++ b/lib/io/src/istream.c @@ -19,7 +19,7 @@ sqfs_s32 istream_read(istream_t *strm, void *data, size_t size) size_t diff; int ret; - ret = istream_get_buffered_data(strm, &ptr, &diff, size); + ret = strm->get_buffered_data(strm, &ptr, &diff, size); if (ret > 0) break; if (ret < 0) @@ -29,7 +29,7 @@ sqfs_s32 istream_read(istream_t *strm, void *data, size_t size) diff = size; memcpy(data, ptr, diff); - istream_advance_buffer(strm, diff); + strm->advance_buffer(strm, diff); data = (char *)data + diff; size -= diff; total += diff; @@ -45,7 +45,7 @@ int istream_skip(istream_t *strm, sqfs_u64 size) size_t diff; int ret; - ret = istream_get_buffered_data(strm, &ptr, &diff, size); + ret = strm->get_buffered_data(strm, &ptr, &diff, size); if (ret < 0) return ret; if (ret > 0) { @@ -58,7 +58,7 @@ int istream_skip(istream_t *strm, sqfs_u64 size) diff = size; size -= diff; - istream_advance_buffer(strm, diff); + strm->advance_buffer(strm, diff); } return 0; @@ -76,7 +76,7 @@ sqfs_s32 istream_splice(istream_t *in, ostream_t *out, sqfs_u32 size) size_t diff; int ret; - ret = istream_get_buffered_data(in, &ptr, &diff, size); + ret = in->get_buffered_data(in, &ptr, &diff, size); if (ret < 0) return ret; if (ret > 0) @@ -85,12 +85,12 @@ sqfs_s32 istream_splice(istream_t *in, ostream_t *out, sqfs_u32 size) if (diff > size) diff = size; - if (ostream_append(out, ptr, diff)) + if (out->append(out, ptr, diff)) return -1; total += diff; size -= diff; - istream_advance_buffer(in, diff); + in->advance_buffer(in, diff); } return total; diff --git a/lib/io/src/xfrm/istream.c b/lib/io/src/xfrm/istream.c index c9bc41a..632409c 100644 --- a/lib/io/src/xfrm/istream.c +++ b/lib/io/src/xfrm/istream.c @@ -38,8 +38,8 @@ static int precache(istream_t *base) const sqfs_u8 *ptr; size_t avail; - ret = istream_get_buffered_data(xfrm->wrapped, &ptr, &avail, - sizeof(xfrm->uncompressed)); + ret = xfrm->wrapped->get_buffered_data(xfrm->wrapped, &ptr, + &avail, BUFSZ); if (ret < 0) return ret; if (ret > 0) @@ -58,7 +58,7 @@ static int precache(istream_t *base) } xfrm->buffer_used = out_off; - istream_advance_buffer(xfrm->wrapped, in_off); + xfrm->wrapped->advance_buffer(xfrm->wrapped, in_off); if (ret == XFRM_STREAM_BUFFER_FULL || out_off >= BUFSZ) break; diff --git a/lib/io/src/xfrm/ostream.c b/lib/io/src/xfrm/ostream.c index bd94515..79ed49a 100644 --- a/lib/io/src/xfrm/ostream.c +++ b/lib/io/src/xfrm/ostream.c @@ -42,7 +42,7 @@ static int flush_inbuf(ostream_xfrm_t *xfrm, bool finish) return -1; } - if (ostream_append(xfrm->wrapped, xfrm->outbuf, off_out)) + if (xfrm->wrapped->append(xfrm->wrapped, xfrm->outbuf, off_out)) return -1; off_out = 0; @@ -52,7 +52,7 @@ static int flush_inbuf(ostream_xfrm_t *xfrm, bool finish) } if (off_out > 0) { - if (ostream_append(xfrm->wrapped, xfrm->outbuf, off_out)) + if (xfrm->wrapped->append(xfrm->wrapped, xfrm->outbuf, off_out)) return -1; } diff --git a/lib/io/test/istream_mem.c b/lib/io/test/istream_mem.c index a486899..ca5146c 100644 --- a/lib/io/test/istream_mem.c +++ b/lib/io/test/istream_mem.c @@ -45,10 +45,10 @@ int main(int argc, char **argv) TEST_NOT_NULL(in); TEST_EQUAL_UI(((sqfs_object_t *)in)->refcount, 1); - TEST_STR_EQUAL(istream_get_filename(in), "memstream.txt"); + TEST_STR_EQUAL(in->get_filename(in), "memstream.txt"); for (i = 0; i < end2; i += diff) { - ret = istream_get_buffered_data(in, &ptr, &size, 61); + ret = in->get_buffered_data(in, &ptr, &size, 61); TEST_EQUAL_I(ret, 0); if ((end2 - i) >= 61) { @@ -65,7 +65,7 @@ int main(int argc, char **argv) diff = eat_all ? size : (size / 2); eat_all = !eat_all; - istream_advance_buffer(in, diff); + in->advance_buffer(in, diff); } sqfs_drop(in); diff --git a/lib/io/test/sparse_fb.c b/lib/io/test/sparse_fb.c index fa4b840..5ee6004 100644 --- a/lib/io/test/sparse_fb.c +++ b/lib/io/test/sparse_fb.c @@ -58,7 +58,7 @@ int main(int argc, char **argv) ret = ostream_append_sparse(&dummy, ref); TEST_EQUAL_I(ret, 0); - ret = ostream_flush(&dummy); + ret = dummy.flush(&dummy); TEST_EQUAL_I(ret, 0); TEST_EQUAL_UI(ref, total); diff --git a/lib/io/test/xfrm.c b/lib/io/test/xfrm.c index 323987c..c2e450c 100644 --- a/lib/io/test/xfrm.c +++ b/lib/io/test/xfrm.c @@ -469,11 +469,11 @@ static void run_pack_test(void) TEST_EQUAL_UI(((sqfs_object_t *)&mem_ostream)->refcount, 2); for (i = 0; i < (sizeof(orig) - 1); ++i) { - ret = ostream_append(ostream, orig + i, 1); + ret = ostream->append(ostream, orig + i, 1); TEST_EQUAL_I(ret, 0); } - ret = ostream_flush(ostream); + ret = ostream->flush(ostream); TEST_EQUAL_I(ret, 0); TEST_ASSERT(mo_flushed); diff --git a/lib/tar/src/iterator.c b/lib/tar/src/iterator.c index 5d44c28..1f26e76 100644 --- a/lib/tar/src/iterator.c +++ b/lib/tar/src/iterator.c @@ -118,8 +118,9 @@ static int strm_get_buffered_data(istream_t *strm, const sqfs_u8 **out, *size = (diff <= sizeof(tar->buffer)) ? diff : sizeof(tar->buffer); } else { - ret = istream_get_buffered_data(tar->parent->stream, - out, size, diff); + ret = tar->parent->stream-> + get_buffered_data(tar->parent->stream, + out, size, diff); if (ret > 0) goto fail_borked; if (ret < 0) @@ -146,7 +147,7 @@ static void strm_advance_buffer(istream_t *strm, size_t count) tar_istream_t *tar = (tar_istream_t *)strm; if (!tar->parent->last_sparse) { - istream_advance_buffer(tar->parent->stream, count); + tar->parent->stream->advance_buffer(tar->parent->stream, count); tar->parent->record_size -= count; } @@ -371,8 +372,8 @@ dir_iterator_t *tar_open_stream(istream_t *strm) it->read_xattr = it_read_xattr; /* proble if the stream is compressed */ - ret = istream_get_buffered_data(strm, &ptr, &size, - sizeof(tar_header_t)); + ret = strm->get_buffered_data(strm, &ptr, &size, + sizeof(tar_header_t)); if (ret != 0) goto out_strm; diff --git a/lib/tar/src/write_header.c b/lib/tar/src/write_header.c index 039d1fe..726b15d 100644 --- a/lib/tar/src/write_header.c +++ b/lib/tar/src/write_header.c @@ -98,7 +98,7 @@ static int write_header(ostream_t *fp, const struct stat *sb, const char *name, update_checksum(&hdr); - return ostream_append(fp, &hdr, sizeof(hdr)); + return fp->append(fp, &hdr, sizeof(hdr)); } static int write_ext_header(ostream_t *fp, const struct stat *orig, @@ -114,7 +114,7 @@ static int write_ext_header(ostream_t *fp, const struct stat *orig, if (write_header(fp, &sb, name, NULL, type)) return -1; - if (ostream_append(fp, payload, payload_len)) + if (fp->append(fp, payload, payload_len)) return -1; return padd_file(fp, payload_len); @@ -288,5 +288,5 @@ int write_hard_link(ostream_t *fp, const struct stat *sb, const char *name, write_number(hdr.devminor, 0, sizeof(hdr.devminor)); update_checksum(&hdr); - return ostream_append(fp, &hdr, sizeof(hdr)); + return fp->append(fp, &hdr, sizeof(hdr)); } diff --git a/lib/tar/test/tar_write_simple.c b/lib/tar/test/tar_write_simple.c index 8a41c8f..2302307 100644 --- a/lib/tar/test/tar_write_simple.c +++ b/lib/tar/test/tar_write_simple.c @@ -138,7 +138,7 @@ int main(int argc, char **argv) NULL, NULL, 9); TEST_EQUAL_I(ret, 0); - ret = ostream_append(&mem_stream, "Hello, World!\n", 14); + ret = mem_stream.append(&mem_stream, "Hello, World!\n", 14); TEST_EQUAL_I(ret, 0); ret = padd_file(&mem_stream, 14); TEST_EQUAL_I(ret, 0); @@ -157,7 +157,7 @@ int main(int argc, char **argv) TEST_EQUAL_I(ret, 0); sqfs_xattr_list_free(xattr); - ret = ostream_append(&mem_stream, ":-)\n", 4); + ret = mem_stream.append(&mem_stream, ":-)\n", 4); TEST_EQUAL_I(ret, 0); ret = padd_file(&mem_stream, 4); TEST_EQUAL_I(ret, 0); @@ -174,9 +174,9 @@ int main(int argc, char **argv) NULL, NULL, 12); TEST_EQUAL_I(ret, 0); - ret = ostream_append(&mem_stream, - "Annoy people with really long file paths!\n", - 42); + ret = mem_stream.append(&mem_stream, + "Annoy people with really long file paths!\n", + 42); TEST_EQUAL_I(ret, 0); ret = padd_file(&mem_stream, 42); TEST_EQUAL_I(ret, 0); -- cgit v1.2.3