Reference ownership exercise again
Oct. 12th, 2019 12:02 amDid you know you don't own the file descriptor here:
This quits with a mysterious error: Error: Custom { kind: Other, error: ErrInvalidFd }
Spent a week trying to figure out whether that's a library that works only on Linux (there are a bunch of mmap libraries, and this one boasts it is not trying to be OS-generic), I am not passing some MapOption, or what.
Apparently, the above is not the same as:
This moves us one step further: Segmentation fault 11
But now it's clear what's wrong. :-)
use mmap::*;
use crc::{crc32, Hasher32};
use std::cmp::min;
use std::fs::File;
use std::fs::metadata;
use std::os::unix::io::AsRawFd;
use std::slice::from_raw_parts;
fn main() -> std::io::Result<()> {
let fd = File::open("doh.txt")?.as_raw_fd();
let fs = metadata("doh.txt")?;
let sz = fs.len() as usize;
let mut offset: usize = 0;
let mut c32 = crc32::Digest::new(crc32::IEEE);
while offset < sz {
let rem = sz - offset;
let rem = min(rem, 1024 * 1024);
let buf = MemoryMap::new(rem, &[MapOption::MapFd(fd),
MapOption::MapReadable,
MapOption::MapOffset(offset)]).
map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?.data();
c32.write(unsafe { from_raw_parts(buf, rem) });
offset += rem;
}
println!("CRC32: {:08x}", c32.sum32());
Ok(())
}
This quits with a mysterious error: Error: Custom { kind: Other, error: ErrInvalidFd }
Spent a week trying to figure out whether that's a library that works only on Linux (there are a bunch of mmap libraries, and this one boasts it is not trying to be OS-generic), I am not passing some MapOption, or what.
Apparently, the above is not the same as:
...
let file = File::open("doh.txt")?;
let fd = file.as_raw_fd();
...This moves us one step further: Segmentation fault 11
But now it's clear what's wrong. :-)
...
let map = MemoryMap::new(rem, &[MapOption::MapFd(fd),
MapOption::MapReadable,
MapOption::MapOffset(offset)]).
map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
let buf = map.data();
...
no subject
Date: 2019-10-11 11:23 pm (UTC)no subject
Date: 2019-10-11 11:41 pm (UTC)