use u64 to represent file size

This increases the maximum supported file size (previously limited to 4GB)
and avoids potential issues with u32 to u64 conversions, which are no longer
needed.

Signed-off-by: onur-ozkan <work@onurozkan.dev>
This commit is contained in:
onur-ozkan 2024-01-21 12:06:42 +03:00
parent d73bd3fb3b
commit 1afd216f76
2 changed files with 13 additions and 14 deletions

View File

@ -300,7 +300,7 @@ fn run(support_lib_count: usize, exe: String, all_args: Vec<String>) {
// Ok now it's time to read all the output. We're receiving "frames" // Ok now it's time to read all the output. We're receiving "frames"
// representing stdout/stderr, so we decode all that here. // representing stdout/stderr, so we decode all that here.
let mut header = [0; 5]; let mut header = [0; 9];
let mut stderr_done = false; let mut stderr_done = false;
let mut stdout_done = false; let mut stdout_done = false;
let mut client = t!(client.into_inner()); let mut client = t!(client.into_inner());
@ -308,10 +308,8 @@ fn run(support_lib_count: usize, exe: String, all_args: Vec<String>) {
let mut stderr = io::stderr(); let mut stderr = io::stderr();
while !stdout_done || !stderr_done { while !stdout_done || !stderr_done {
t!(client.read_exact(&mut header)); t!(client.read_exact(&mut header));
let amt = ((header[1] as u64) << 24) let amt = u64::from_be_bytes(header[1..9].try_into().unwrap());
| ((header[2] as u64) << 16)
| ((header[3] as u64) << 8)
| ((header[4] as u64) << 0);
if header[0] == 0 { if header[0] == 0 {
if amt == 0 { if amt == 0 {
stdout_done = true; stdout_done = true;
@ -349,7 +347,8 @@ fn send(path: &Path, dst: &mut dyn Write) {
t!(dst.write_all(&[0])); t!(dst.write_all(&[0]));
let mut file = t!(File::open(&path)); let mut file = t!(File::open(&path));
let amt = t!(file.metadata()).len(); let amt = t!(file.metadata()).len();
t!(dst.write_all(&[(amt >> 24) as u8, (amt >> 16) as u8, (amt >> 8) as u8, (amt >> 0) as u8,]));
t!(dst.write_all(&amt.to_be_bytes()));
t!(io::copy(&mut file, dst)); t!(io::copy(&mut file, dst));
} }

View File

@ -347,7 +347,7 @@ fn recv<B: BufRead>(dir: &Path, io: &mut B) -> PathBuf {
// the filesystem limits. // the filesystem limits.
let len = cmp::min(filename.len() - 1, 50); let len = cmp::min(filename.len() - 1, 50);
let dst = dir.join(t!(str::from_utf8(&filename[..len]))); let dst = dir.join(t!(str::from_utf8(&filename[..len])));
let amt = read_u32(io) as u64; let amt = read_u64(io);
t!(io::copy(&mut io.take(amt), &mut t!(File::create(&dst)))); t!(io::copy(&mut io.take(amt), &mut t!(File::create(&dst))));
set_permissions(&dst); set_permissions(&dst);
dst dst
@ -365,7 +365,7 @@ fn my_copy(src: &mut dyn Read, which: u8, dst: &Mutex<dyn Write>) {
loop { loop {
let n = t!(src.read(&mut b)); let n = t!(src.read(&mut b));
let mut dst = dst.lock().unwrap(); let mut dst = dst.lock().unwrap();
t!(dst.write_all(&create_header(which, n as u32))); t!(dst.write_all(&create_header(which, n as u64)));
if n > 0 { if n > 0 {
t!(dst.write_all(&b[..n])); t!(dst.write_all(&b[..n]));
} else { } else {
@ -377,7 +377,7 @@ fn my_copy(src: &mut dyn Read, which: u8, dst: &Mutex<dyn Write>) {
fn batch_copy(buf: &[u8], which: u8, dst: &Mutex<dyn Write>) { fn batch_copy(buf: &[u8], which: u8, dst: &Mutex<dyn Write>) {
let n = buf.len(); let n = buf.len();
let mut dst = dst.lock().unwrap(); let mut dst = dst.lock().unwrap();
t!(dst.write_all(&create_header(which, n as u32))); t!(dst.write_all(&create_header(which, n as u64)));
if n > 0 { if n > 0 {
t!(dst.write_all(buf)); t!(dst.write_all(buf));
// Marking buf finished // Marking buf finished
@ -385,13 +385,13 @@ fn batch_copy(buf: &[u8], which: u8, dst: &Mutex<dyn Write>) {
} }
} }
const fn create_header(which: u8, n: u32) -> [u8; 5] { const fn create_header(which: u8, n: u64) -> [u8; 9] {
let bytes = n.to_be_bytes(); let bytes = n.to_be_bytes();
[which, bytes[0], bytes[1], bytes[2], bytes[3]] [which, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]]
} }
fn read_u32(r: &mut dyn Read) -> u32 { fn read_u64(r: &mut dyn Read) -> u64 {
let mut len = [0; 4]; let mut len = [0; 8];
t!(r.read_exact(&mut len)); t!(r.read_exact(&mut len));
u32::from_be_bytes(len) u64::from_be_bytes(len)
} }