[personal profile] sassa_nf
Did you know you don't own the file descriptor here:
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();
...

Date: 2019-10-11 11:23 pm (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi
Totally unobvious.

Profile

sassa_nf

January 2026

S M T W T F S
    123
45678910
111213141516 17
18192021222324
25262728293031

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 23rd, 2026 12:29 am
Powered by Dreamwidth Studios