cap_primitives/fs/
reopen.rs1use crate::fs::{is_file_read_write, is_same_file, reopen_impl, OpenOptions};
4use std::{fs, io};
5
6#[inline]
13pub fn reopen(file: &fs::File, options: &OpenOptions) -> io::Result<fs::File> {
14 let (read, write) = is_file_read_write(file)?;
15
16 if options.create
19 || options.create_new
20 || (!read && options.read)
21 || (!write && (options.write || options.append || options.truncate))
22 {
23 return Err(io::Error::new(
24 io::ErrorKind::PermissionDenied,
25 "Couldn't reopen file",
26 ));
27 }
28
29 let new = reopen_impl(file, options)?;
30
31 if !is_same_file(file, &new)? {
32 return Err(io::Error::new(io::ErrorKind::Other, "Couldn't reopen file"));
33 }
34
35 Ok(new)
36}