-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathunix.rs
More file actions
186 lines (177 loc) · 6.23 KB
/
unix.rs
File metadata and controls
186 lines (177 loc) · 6.23 KB
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
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use std::env;
use std::ffi::OsStr;
use std::io::{BufWriter, Error, Result};
use std::io::{ErrorKind, Write};
use std::path::Path;
use std::process::{Child, Command, Stdio};
use uucore::error::USimpleError;
use uucore::fs;
use uucore::fs::FileInformation;
use uucore::show;
use uucore::translate;
/// A writer that writes to a `shell_process`' stdin
///
/// We use a shell process (not directly calling a sub-process) so we can forward the name of the
/// corresponding output file (xaa, xab, xac… ). This is the way it was implemented in GNU split.
struct FilterWriter {
/// Running shell process
shell_process: Child,
}
impl Write for FilterWriter {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.shell_process
.stdin
.as_mut()
.expect("failed to get shell stdin")
.write(buf)
}
fn flush(&mut self) -> Result<()> {
self.shell_process
.stdin
.as_mut()
.expect("failed to get shell stdin")
.flush()
}
}
/// Have an environment variable set at a value during this lifetime
struct WithEnvVarSet {
/// Env var key
previous_var_key: String,
/// Previous value set to this key
previous_var_value: std::result::Result<String, env::VarError>,
}
impl WithEnvVarSet {
/// Save previous value assigned to key, set key=value
fn new(key: &str, value: &str) -> Self {
let previous_env_value = env::var(key);
unsafe {
env::set_var(key, value);
}
Self {
previous_var_key: String::from(key),
previous_var_value: previous_env_value,
}
}
}
impl Drop for WithEnvVarSet {
/// Restore previous value now that this is being dropped by context
fn drop(&mut self) {
if let Ok(ref prev_value) = self.previous_var_value {
unsafe {
env::set_var(&self.previous_var_key, prev_value);
}
} else {
unsafe {
env::remove_var(&self.previous_var_key);
}
}
}
}
impl FilterWriter {
/// Create a new filter running a command with $FILE pointing at the output name
///
/// #Arguments
///
/// * `command` - The shell command to execute
/// * `filepath` - Path of the output file (forwarded to command as $FILE)
fn new(command: &str, filepath: &str) -> Result<Self> {
// set $FILE, save previous value (if there was one)
let _with_env_var_set = WithEnvVarSet::new("FILE", filepath);
let shell_process =
Command::new(env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_owned()))
.arg("-c")
.arg(command)
.stdin(Stdio::piped())
.spawn()?;
Ok(Self { shell_process })
}
}
impl Drop for FilterWriter {
/// flush stdin, close it and wait on `shell_process` before dropping self
fn drop(&mut self) {
{
// close stdin by dropping it
let _stdin = self.shell_process.stdin.as_mut();
}
let exit_status = self
.shell_process
.wait()
.expect("Couldn't wait for child process");
if let Some(return_code) = exit_status.code() {
if return_code != 0 {
show!(USimpleError::new(
1,
translate!("split-error-shell-process-returned", "code" => return_code)
));
}
} else {
show!(USimpleError::new(
1,
translate!("split-error-shell-process-terminated")
));
}
}
}
/// Instantiate either a file writer or a "write to shell process's stdin" writer
pub fn instantiate_current_writer(
filter: Option<&str>,
filename: &str,
is_new: bool,
) -> Result<BufWriter<Box<dyn Write>>> {
match filter {
None => {
let file = if is_new {
// create new file
std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(Path::new(&filename))
.map_err(|e| match e.kind() {
ErrorKind::IsADirectory => Error::other(
translate!("split-error-is-a-directory", "dir" => filename),
),
ErrorKind::PermissionDenied => Error::other(
translate!("split-error-unable-to-open-file", "file" => filename),
),
_ => Error::other(format!(
"split: {filename}: {}",
uucore::error::strip_errno(&e)
)),
})?
} else {
// re-open file that we previously created to append to it
std::fs::OpenOptions::new()
.append(true)
.open(Path::new(&filename))
.map_err(|e| match e.kind() {
ErrorKind::PermissionDenied => Error::other(
translate!("split-error-unable-to-reopen-file", "file" => filename),
),
_ => Error::other(format!(
"split: {filename}: {}",
uucore::error::strip_errno(&e)
)),
})?
};
Ok(BufWriter::new(Box::new(file) as Box<dyn Write>))
}
Some(filter_command) => Ok(BufWriter::new(Box::new(
// spawn a shell command and write to it
FilterWriter::new(filter_command, filename)?,
) as Box<dyn Write>)),
}
}
pub fn paths_refer_to_same_file(p1: &OsStr, p2: &OsStr) -> bool {
// We have to take symlinks and relative paths into account.
let p1 = if p1 == "-" {
FileInformation::from_file(&std::io::stdin())
} else {
FileInformation::from_path(Path::new(p1), true)
};
fs::infos_refer_to_same_file(p1, FileInformation::from_path(Path::new(p2), true))
}