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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
extern crate errno;
extern crate libc;
use libc::{c_int, mkfifo, mode_t, EACCES, EEXIST, ENOENT};
use std::ffi::CString;
use std::fs::{File, OpenOptions};
use std::io;
use std::os::unix::fs::OpenOptionsExt;
use std::path::Path;
mod ext;
pub use self::ext::*;
pub fn create<P: AsRef<Path>>(path: P, mode: Option<u32>) -> io::Result<()> {
let path = CString::new(path.as_ref().to_str().unwrap())?;
let mode = mode.unwrap_or(0o644);
let result: c_int = unsafe { mkfifo(path.as_ptr(), mode as mode_t) };
let result: i32 = result.into();
if result == 0 {
return Ok(());
}
let error = errno::errno();
match error.0 {
EACCES => {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
format!("could not open {:?}: {}", path, error),
));
}
EEXIST => {
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
format!("could not open {:?}: {}", path, error),
));
}
ENOENT => {
return Err(io::Error::new(
io::ErrorKind::NotFound,
format!("could not open {:?}: {}", path, error),
));
}
_ => {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("could not open {:?}: {}", path, error),
));
}
}
}
pub fn open_read<P: AsRef<Path>>(path: P) -> io::Result<File> {
OpenOptions::new()
.read(true)
.custom_flags(libc::O_NONBLOCK)
.open(path)
}
pub fn open_write<P: AsRef<Path>>(path: P) -> io::Result<File> {
OpenOptions::new()
.write(true)
.append(true)
.custom_flags(libc::O_NONBLOCK)
.open(path)
}
#[cfg(test)]
mod tests {
extern crate fs2;
use super::*;
use fs2::FileExt;
use std::fs;
use std::io::{self, Error, ErrorKind, Read, Write};
fn lock_active_test() -> io::Result<fs::File> {
let file = File::create("/tmp/unix-named-pipe_tests.lock")?;
file.lock_exclusive()?;
Ok(file)
}
#[test]
fn create_new_pipe() {
let lock = lock_active_test().unwrap();
let filename = "/tmp/pipe";
let _ = create(filename, None).expect("could not create pipe");
fs::remove_file(filename).expect("could not remove test pipe");
lock.unlock().unwrap();
}
#[test]
fn create_pipe_eexists() {
let lock = lock_active_test().unwrap();
let filename = "/tmp/pipe";
fs::write(filename, "").expect("could not write test file");
let pipe = create(filename, None);
assert_eq!(pipe.is_err(), true);
let err: Error = pipe.unwrap_err();
assert_eq!(err.kind(), ErrorKind::AlreadyExists);
fs::remove_file(filename).expect("could not remove test file");
lock.unlock().unwrap();
}
#[test]
fn create_pipe_enoent() {
let filename = "/notadir/pipe";
let pipe = create(filename, None);
assert_eq!(pipe.is_err(), true);
let err: Error = pipe.unwrap_err();
assert_eq!(err.kind(), ErrorKind::NotFound);
}
#[test]
fn open_pipe_read() {
let lock = lock_active_test().unwrap();
let filename = "/tmp/test.pipe";
let _ = create(filename, None).expect("could not make test pipe");
let contents: [u8; 4] = [0xca, 0xfe, 0xba, 0xbe];
let mut actual: [u8; 4] = [0; 4];
let mut read_file = open_read(filename).expect("could not open test pipe for reading");
{
let mut write_file =
open_write(filename).expect("could not open test pipe for writing");
write_file
.write(&contents)
.expect("could not write test data to pipe");
write_file.flush().expect("could not flush test pipe");
}
read_file
.read_exact(&mut actual)
.expect("could not read test data from pipe");
assert_eq!(contents, actual);
fs::remove_file(filename).expect("could not remove test file");
lock.unlock().unwrap();
}
}