From b1297c4192ab447b451375057fcd9a4a5c508b10 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 28 Feb 2024 15:52:03 -0800 Subject: [PATCH] Less usage of C stdio --- native/src/base/include/stream.hpp | 16 +-------------- native/src/base/stream.cpp | 9 -------- native/src/boot/compress.cpp | 33 ++++++++++++++++++------------ 3 files changed, 21 insertions(+), 37 deletions(-) diff --git a/native/src/base/include/stream.hpp b/native/src/base/include/stream.hpp index 027de1c1f..adad83675 100644 --- a/native/src/base/include/stream.hpp +++ b/native/src/base/include/stream.hpp @@ -60,9 +60,7 @@ struct in_stream { }; // A stream is something that is writable and readable -struct stream : public out_stream, public in_stream { - virtual ~stream() = default; -}; +struct stream : public out_stream, public in_stream {}; using stream_ptr = std::unique_ptr; @@ -122,18 +120,6 @@ private: * Bridge between stream class and C stdio * ****************************************/ -// sFILE -> stream_ptr -class fp_stream final : public file_stream { -public: - fp_stream(FILE *fp = nullptr) : fp(fp, fclose) {} - fp_stream(sFILE &&fp) : fp(std::move(fp)) {} - ssize_t read(void *buf, size_t len) override; -protected: - ssize_t do_write(const void *buf, size_t len) override; -private: - sFILE fp; -}; - // stream_ptr -> sFILE sFILE make_stream_fp(stream_ptr &&strm); diff --git a/native/src/base/stream.cpp b/native/src/base/stream.cpp index f86e10cc1..c276ff6ca 100644 --- a/native/src/base/stream.cpp +++ b/native/src/base/stream.cpp @@ -45,15 +45,6 @@ ssize_t in_stream::readFully(void *buf, size_t len) { return read_sz; } -ssize_t fp_stream::read(void *buf, size_t len) { - auto ret = fread(buf, 1, len, fp.get()); - return ret ? ret : (ferror(fp.get()) ? -1 : 0); -} - -ssize_t fp_stream::do_write(const void *buf, size_t len) { - return fwrite(buf, 1, len, fp.get()); -} - bool filter_out_stream::write(const void *buf, size_t len) { return base->write(buf, len); } diff --git a/native/src/boot/compress.cpp b/native/src/boot/compress.cpp index 2aa3f7321..49daa32e1 100644 --- a/native/src/boot/compress.cpp +++ b/native/src/boot/compress.cpp @@ -627,12 +627,13 @@ void decompress(char *infile, const char *outfile) { bool in_std = infile == "-"sv; bool rm_in = false; - FILE *in_fp = in_std ? stdin : xfopen(infile, "re"); + int in_fd = in_std ? STDIN_FILENO : xopen(infile, O_RDONLY); + int out_fd = -1; out_strm_ptr strm; char buf[4096]; size_t len; - while ((len = fread(buf, 1, sizeof(buf), in_fp))) { + while ((len = read(in_fd, buf, sizeof(buf)))) { if (!strm) { format_t type = check_fmt(buf, len); @@ -660,8 +661,10 @@ void decompress(char *infile, const char *outfile) { } } - FILE *out_fp = outfile == "-"sv ? stdout : xfopen(outfile, "we"); - strm = get_decoder(type, make_unique(out_fp)); + out_fd = outfile == "-"sv ? + STDOUT_FILENO : + xopen(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); + strm = get_decoder(type, make_unique(out_fd)); if (ext) *ext = '.'; } if (!strm->write(buf, len)) @@ -669,7 +672,8 @@ void decompress(char *infile, const char *outfile) { } strm.reset(nullptr); - fclose(in_fp); + if (in_fd != STDIN_FILENO) close(in_fd); + if (out_fd != STDOUT_FILENO) close(out_fd); if (rm_in) unlink(infile); @@ -683,36 +687,39 @@ void compress(const char *method, const char *infile, const char *outfile) { bool in_std = infile == "-"sv; bool rm_in = false; - FILE *in_fp = in_std ? stdin : xfopen(infile, "re"); - FILE *out_fp; + int in_fd = in_std ? STDIN_FILENO : xopen(infile, O_RDONLY); + int out_fd = -1; if (outfile == nullptr) { if (in_std) { - out_fp = stdout; + out_fd = STDOUT_FILENO; } else { /* If user does not provide outfile and infile is not * STDIN, output to .[ext] */ string tmp(infile); tmp += fmt2ext[fmt]; - out_fp = xfopen(tmp.data(), "we"); + out_fd = xopen(tmp.data(), O_WRONLY | O_CREAT | O_TRUNC, 0644); fprintf(stderr, "Compressing to [%s]\n", tmp.data()); rm_in = true; } } else { - out_fp = outfile == "-"sv ? stdout : xfopen(outfile, "we"); + out_fd = outfile == "-"sv ? + STDOUT_FILENO : + xopen(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); } - auto strm = get_encoder(fmt, make_unique(out_fp)); + auto strm = get_encoder(fmt, make_unique(out_fd)); char buf[4096]; size_t len; - while ((len = fread(buf, 1, sizeof(buf), in_fp))) { + while ((len = read(in_fd, buf, sizeof(buf)))) { if (!strm->write(buf, len)) LOGE("Compression error!\n"); } strm.reset(nullptr); - fclose(in_fp); + if (in_fd != STDIN_FILENO) close(in_fd); + if (out_fd != STDOUT_FILENO) close(out_fd); if (rm_in) unlink(infile);