Skip to content

Instantly share code, notes, and snippets.

@KodrAus
Last active March 19, 2017 03:28
Show Gist options
  • Select an option

  • Save KodrAus/69a405497676536e8b494cc151633e03 to your computer and use it in GitHub Desktop.

Select an option

Save KodrAus/69a405497676536e8b494cc151633e03 to your computer and use it in GitHub Desktop.
Test deleting an mmapped file
extern crate memmap;
extern crate tempdir;
use std::path::PathBuf;
use std::fs::{File, remove_file};
use std::io::Write;
use std::str;
use tempdir::TempDir;
use memmap::{Mmap, Protection};
fn mkdir(dir: &TempDir) -> PathBuf {
let path = dir.path().join("file");
let mut f = File::create(&path).unwrap();
f.write_all(b"all the bits").unwrap();
f.sync_all().unwrap();
path.into()
}
fn main() {
let dir = TempDir::new("memmap_test").unwrap();
let filepath = mkdir(&dir);
let mapped = Mmap::open_path(&filepath, Protection::Read).unwrap();
// Windows: OsError: Access is Denied
remove_file(&filepath).unwrap();
println!("{}", str::from_utf8(unsafe { mapped.as_slice() }).unwrap());
}
@KodrAus

KodrAus commented Mar 19, 2017

Copy link
Copy Markdown
Author

On Windows:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { repr: Os { code: 5, message: "Access is denied." } }'

@KodrAus

KodrAus commented Mar 19, 2017

Copy link
Copy Markdown
Author

Linux works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment