/rust/registry/src/index.crates.io-1949cf8c6b5b557f/cap-primitives-3.4.5/src/fs/reopen.rs
Line | Count | Source |
1 | | //! Re-open a `fs::File` to produce an independent handle. |
2 | | |
3 | | use crate::fs::{is_file_read_write, is_same_file, reopen_impl, OpenOptions}; |
4 | | use std::{fs, io}; |
5 | | |
6 | | /// Re-open an `fs::File` to produce an independent handle. |
7 | | /// |
8 | | /// This operation isn't supported by all operating systems in all |
9 | | /// circumstances, or in some operating systems in any circumstances, |
10 | | /// so it may return an `io::ErrorKind::Other` error if the file |
11 | | /// cannot be reopened. |
12 | | #[inline] |
13 | 0 | pub fn reopen(file: &fs::File, options: &OpenOptions) -> io::Result<fs::File> { |
14 | 0 | let (read, write) = is_file_read_write(file)?; |
15 | | |
16 | | // Don't grant more rights than the original file had. And don't allow |
17 | | // it to create a file. |
18 | 0 | if options.create |
19 | 0 | || options.create_new |
20 | 0 | || (!read && options.read) |
21 | 0 | || (!write && (options.write || options.append || options.truncate)) |
22 | | { |
23 | 0 | return Err(io::Error::new( |
24 | 0 | io::ErrorKind::PermissionDenied, |
25 | 0 | "Couldn't reopen file", |
26 | 0 | )); |
27 | 0 | } |
28 | | |
29 | 0 | let new = reopen_impl(file, options)?; |
30 | | |
31 | 0 | if !is_same_file(file, &new)? { |
32 | 0 | return Err(io::Error::new(io::ErrorKind::Other, "Couldn't reopen file")); |
33 | 0 | } |
34 | | |
35 | 0 | Ok(new) |
36 | 0 | } |