1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#![deny(
warnings,
missing_docs,
clippy::pedantic,
clippy::dbg_macro,
clippy::semicolon_if_nothing_returned
)]
#![forbid(unsafe_code)]
#![allow(clippy::inline_always)]
#![doc(test(attr(deny(warnings), forbid(unsafe_code))))]
use sealed::Sealed;
use std::fs::OpenOptions;
#[cfg(unix)]
use std::os::unix::fs::OpenOptionsExt as FsOpenOptionsExt;
#[cfg(windows)]
use std::os::windows::fs::OpenOptionsExt as FsOpenOptionsExt;
mod sealed {
pub trait Sealed {}
}
pub trait OpenOptionsExt: Sealed {
fn no_follow_with(&mut self, #[cfg(unix)] flags: i32, #[cfg(windows)] flags: u32) -> &mut Self;
#[inline(always)]
fn no_follow(&mut self) -> &mut Self {
self.no_follow_with(0)
}
}
impl Sealed for OpenOptions {}
impl OpenOptionsExt for OpenOptions {
#[cfg(unix)]
#[inline(always)]
fn no_follow_with(&mut self, flags: i32) -> &mut Self {
self.custom_flags(flags | libc::O_NOFOLLOW)
}
#[cfg(windows)]
#[inline(always)]
fn no_follow_with(&mut self, flags: u32) -> &mut Self {
const OPEN_REPARSE_POINT: u32 = 0x20_0000;
self.custom_flags(flags | OPEN_REPARSE_POINT)
}
}
#[cfg(test)]
#[allow(clippy::blacklisted_name)]
mod tests {
use super::OpenOptionsExt;
#[cfg(unix)]
use std::os::unix::fs::symlink;
#[cfg(windows)]
use std::os::windows::fs::symlink_file as symlink;
use std::{
fs::{File, OpenOptions},
io::prelude::*,
path::Path,
};
use tempfile::tempdir;
fn dont_open_symlink(path: impl AsRef<Path>) -> File {
let file = OpenOptions::new()
.read(true)
.no_follow()
.open(path)
.unwrap();
assert!(
!file.metadata().unwrap().file_type().is_symlink(),
"file is a symbolic link"
);
file
}
#[test]
fn fine_with_normal_file() {
let mut content = String::new();
dont_open_symlink(file!())
.read_to_string(&mut content)
.unwrap();
assert_eq!(content, include_str!(concat!("../", file!())));
}
#[test]
#[should_panic = "ymbolic link"]
fn fail_on_symlink() {
let dir = tempdir().unwrap();
let foo = dir.path().join("foo");
File::create(&foo).unwrap();
let bar = dir.path().join("bar");
symlink(foo, &bar).unwrap();
dont_open_symlink(bar);
}
}