2019-02-23 04:15:54 -05:00
|
|
|
#pragma once
|
|
|
|
|
2021-08-13 02:08:56 -07:00
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <cstdio>
|
2019-02-23 04:15:54 -05:00
|
|
|
#include <memory>
|
|
|
|
|
2020-03-09 01:50:30 -07:00
|
|
|
#include "../files.hpp"
|
2019-11-23 04:57:52 -05:00
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
struct out_stream {
|
|
|
|
virtual bool write(const void *buf, size_t len) = 0;
|
2021-08-13 02:08:56 -07:00
|
|
|
virtual ssize_t writev(const iovec *iov, int iovcnt);
|
2023-05-19 04:19:43 -07:00
|
|
|
virtual ~out_stream() = default;
|
2019-11-20 21:48:49 -05:00
|
|
|
};
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
using out_strm_ptr = std::unique_ptr<out_stream>;
|
2019-12-13 00:37:06 -05:00
|
|
|
|
|
|
|
// Delegates all operations to base stream
|
2023-05-19 04:19:43 -07:00
|
|
|
class filter_out_stream : public out_stream {
|
2019-11-20 21:48:49 -05:00
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
filter_out_stream(out_strm_ptr &&base) : base(std::move(base)) {}
|
2021-11-21 04:38:22 -08:00
|
|
|
bool write(const void *buf, size_t len) override;
|
2019-11-20 21:48:49 -05:00
|
|
|
protected:
|
2023-05-19 04:19:43 -07:00
|
|
|
out_strm_ptr base;
|
2019-11-20 21:48:49 -05:00
|
|
|
};
|
|
|
|
|
2021-11-21 02:27:52 -08:00
|
|
|
// Buffered output stream, writing in chunks
|
2023-05-19 04:19:43 -07:00
|
|
|
class chunk_out_stream : public filter_out_stream {
|
2021-11-21 02:27:52 -08:00
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
chunk_out_stream(out_strm_ptr &&base, size_t buf_sz, size_t chunk_sz)
|
2023-05-20 14:19:40 -07:00
|
|
|
: filter_out_stream(std::move(base)), chunk_sz(chunk_sz), data(buf_sz) {}
|
2021-11-21 02:27:52 -08:00
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
chunk_out_stream(out_strm_ptr &&base, size_t buf_sz = 4096)
|
2021-11-21 02:27:52 -08:00
|
|
|
: chunk_out_stream(std::move(base), buf_sz, buf_sz) {}
|
|
|
|
|
2021-11-21 04:38:22 -08:00
|
|
|
bool write(const void *buf, size_t len) final;
|
2021-11-21 02:27:52 -08:00
|
|
|
|
|
|
|
protected:
|
2021-11-23 16:50:08 -08:00
|
|
|
// Classes inheriting this class has to call finalize() in its destructor
|
|
|
|
void finalize();
|
2023-05-20 14:19:40 -07:00
|
|
|
virtual bool write_chunk(const void *buf, size_t len, bool final);
|
2021-11-21 02:27:52 -08:00
|
|
|
|
|
|
|
size_t chunk_sz;
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t buf_off = 0;
|
2023-05-20 14:19:40 -07:00
|
|
|
heap_data data;
|
2021-11-21 02:27:52 -08:00
|
|
|
};
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
struct in_stream {
|
|
|
|
virtual ssize_t read(void *buf, size_t len) = 0;
|
|
|
|
virtual ssize_t readFully(void *buf, size_t len);
|
|
|
|
virtual ssize_t readv(const iovec *iov, int iovcnt);
|
|
|
|
virtual ~in_stream() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A channel is something that is writable, readable, and seekable
|
2023-05-20 01:28:10 -07:00
|
|
|
struct channel : public out_stream, public in_stream {
|
2023-05-19 04:19:43 -07:00
|
|
|
virtual off_t seek(off_t off, int whence) = 0;
|
|
|
|
virtual ~channel() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
using channel_ptr = std::unique_ptr<channel>;
|
|
|
|
|
|
|
|
// Byte channel that dynamically allocates memory
|
|
|
|
class byte_channel : public channel {
|
2019-11-20 21:48:49 -05:00
|
|
|
public:
|
2023-05-20 14:19:40 -07:00
|
|
|
byte_channel(heap_data &data) : _data(data) {}
|
2021-08-13 02:08:56 -07:00
|
|
|
|
|
|
|
ssize_t read(void *buf, size_t len) override;
|
2021-11-21 04:38:22 -08:00
|
|
|
bool write(const void *buf, size_t len) override;
|
2020-12-30 22:11:24 -08:00
|
|
|
off_t seek(off_t off, int whence) override;
|
2019-11-20 21:48:49 -05:00
|
|
|
|
|
|
|
private:
|
2023-05-20 14:19:40 -07:00
|
|
|
heap_data &_data;
|
2020-12-30 22:11:24 -08:00
|
|
|
size_t _pos = 0;
|
|
|
|
size_t _cap = 0;
|
2019-11-20 21:48:49 -05:00
|
|
|
|
2023-05-20 14:19:40 -07:00
|
|
|
void resize(size_t new_sz, bool zero = false);
|
2019-11-20 21:48:49 -05:00
|
|
|
};
|
|
|
|
|
2023-12-08 23:30:55 +08:00
|
|
|
class rust_vec_channel : public channel {
|
|
|
|
public:
|
|
|
|
rust_vec_channel(rust::Vec<uint8_t> &data) : _data(data) {}
|
|
|
|
|
|
|
|
ssize_t read(void *buf, size_t len) override;
|
|
|
|
bool write(const void *buf, size_t len) override;
|
|
|
|
off_t seek(off_t off, int whence) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
rust::Vec<uint8_t> &_data;
|
|
|
|
size_t _pos = 0;
|
|
|
|
|
|
|
|
void ensure_size(size_t sz);
|
|
|
|
};
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
class file_channel : public channel {
|
2021-11-21 04:38:22 -08:00
|
|
|
public:
|
|
|
|
bool write(const void *buf, size_t len) final;
|
|
|
|
protected:
|
|
|
|
virtual ssize_t do_write(const void *buf, size_t len) = 0;
|
|
|
|
};
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
// File channel but does not close the file descriptor at any time
|
|
|
|
class fd_channel : public file_channel {
|
2019-11-20 21:48:49 -05:00
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
fd_channel(int fd) : fd(fd) {}
|
2021-08-13 02:08:56 -07:00
|
|
|
ssize_t read(void *buf, size_t len) override;
|
|
|
|
ssize_t readv(const iovec *iov, int iovcnt) override;
|
|
|
|
ssize_t writev(const iovec *iov, int iovcnt) override;
|
2020-12-30 22:11:24 -08:00
|
|
|
off_t seek(off_t off, int whence) override;
|
2021-11-21 04:38:22 -08:00
|
|
|
protected:
|
|
|
|
ssize_t do_write(const void *buf, size_t len) override;
|
2019-11-20 21:48:49 -05:00
|
|
|
private:
|
2020-12-30 22:11:24 -08:00
|
|
|
int fd;
|
2019-11-20 21:48:49 -05:00
|
|
|
};
|
2019-12-13 00:37:06 -05:00
|
|
|
|
|
|
|
/* ****************************************
|
2023-05-19 04:19:43 -07:00
|
|
|
* Bridge between channel class and C stdio
|
2019-12-13 00:37:06 -05:00
|
|
|
* ****************************************/
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
// sFILE -> channel_ptr
|
|
|
|
class fp_channel final : public file_channel {
|
2019-12-13 00:37:06 -05:00
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
fp_channel(FILE *fp = nullptr) : fp(fp, fclose) {}
|
|
|
|
fp_channel(sFILE &&fp) : fp(std::move(fp)) {}
|
2021-08-13 02:08:56 -07:00
|
|
|
ssize_t read(void *buf, size_t len) override;
|
2020-12-30 22:11:24 -08:00
|
|
|
off_t seek(off_t off, int whence) override;
|
2021-11-21 04:38:22 -08:00
|
|
|
protected:
|
|
|
|
ssize_t do_write(const void *buf, size_t len) override;
|
2019-12-13 00:37:06 -05:00
|
|
|
private:
|
2020-12-30 22:11:24 -08:00
|
|
|
sFILE fp;
|
2019-12-13 00:37:06 -05:00
|
|
|
};
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
// channel_ptr -> sFILE
|
|
|
|
sFILE make_channel_fp(channel_ptr &&strm);
|
2019-12-13 00:37:06 -05:00
|
|
|
|
|
|
|
template <class T, class... Args>
|
2023-05-19 04:19:43 -07:00
|
|
|
sFILE make_channel_fp(Args &&... args) {
|
|
|
|
return make_channel_fp(channel_ptr(new T(std::forward<Args>(args)...)));
|
2019-12-13 00:37:06 -05:00
|
|
|
}
|