Support compressing during cpio backup

This commit is contained in:
LoveSy
2023-12-08 23:30:55 +08:00
committed by topjohnwu
parent d73c2daf6d
commit 8b7fae278b
9 changed files with 145 additions and 11 deletions
+15
View File
@@ -78,6 +78,21 @@ private:
void resize(size_t new_sz, bool zero = false);
};
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);
};
class file_channel : public channel {
public:
bool write(const void *buf, size_t len) final;
+5
View File
@@ -44,6 +44,7 @@ pub mod ffi {
fn set_log_level_state_cxx(level: LogLevelCxx, enabled: bool);
fn exit_on_error(b: bool);
fn cmdline_logging();
fn resize_vec(vec: &mut Vec<u8>, size: usize);
}
#[namespace = "rust"]
@@ -63,3 +64,7 @@ fn set_log_level_state_cxx(level: ffi::LogLevelCxx, enabled: bool) {
set_log_level_state(level, enabled)
}
}
fn resize_vec(vec: &mut Vec<u8>, size: usize) {
vec.resize(size, 0);
}
+41
View File
@@ -180,6 +180,47 @@ void byte_channel::resize(size_t new_sz, bool zero) {
}
}
ssize_t rust_vec_channel::read(void *buf, size_t len) {
len = std::min<size_t>(len, _data.size() - _pos);
memcpy(buf, _data.data() + _pos, len);
_pos += len;
return len;
}
bool rust_vec_channel::write(const void *buf, size_t len) {
ensure_size(_pos + len);
memcpy(_data.data() + _pos, buf, len);
_pos += len;
return true;
}
off_t rust_vec_channel::seek(off_t off, int whence) {
off_t np;
switch (whence) {
case SEEK_CUR:
np = _pos + off;
break;
case SEEK_END:
np = _data.size() + off;
break;
case SEEK_SET:
np = off;
break;
default:
return -1;
}
ensure_size(np);
_pos = np;
return np;
}
void rust_vec_channel::ensure_size(size_t sz) {
size_t old_sz = _data.size();
if (sz > old_sz) {
resize_vec(_data, sz);
}
}
ssize_t fd_channel::read(void *buf, size_t len) {
return ::read(fd, buf, len);
}