Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a76a39

Browse files
authored
Merge pull request #9085 from andreacorbellini/comm-no-lock
comm: hold the stdin lock for the whole duration of the program
2 parents e4f0216 + 35360f7 commit 0a76a39

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/uu/comm/src/comm.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use std::cmp::Ordering;
99
use std::ffi::OsString;
1010
use std::fs::{File, metadata};
11-
use std::io::{self, BufRead, BufReader, Read, Stdin, stdin};
11+
use std::io::{self, BufRead, BufReader, Read, StdinLock, stdin};
1212
use std::path::Path;
1313
use uucore::error::{FromIo, UResult, USimpleError};
1414
use uucore::format_usage;
@@ -55,10 +55,20 @@ struct OrderChecker {
5555
}
5656

5757
enum Input {
58-
Stdin(Stdin),
58+
Stdin(StdinLock<'static>),
5959
FileIn(BufReader<File>),
6060
}
6161

62+
impl Input {
63+
fn stdin() -> Self {
64+
Self::Stdin(stdin().lock())
65+
}
66+
67+
fn from_file(f: File) -> Self {
68+
Self::FileIn(BufReader::new(f))
69+
}
70+
}
71+
6272
struct LineReader {
6373
line_ending: LineEnding,
6474
input: Input,
@@ -73,7 +83,7 @@ impl LineReader {
7383
let line_ending = self.line_ending.into();
7484

7585
let result = match &mut self.input {
76-
Input::Stdin(r) => r.lock().read_until(line_ending, buf),
86+
Input::Stdin(r) => r.read_until(line_ending, buf),
7787
Input::FileIn(r) => r.read_until(line_ending, buf),
7888
};
7989

@@ -283,16 +293,13 @@ fn comm(a: &mut LineReader, b: &mut LineReader, delim: &str, opts: &ArgMatches)
283293

284294
fn open_file(name: &OsString, line_ending: LineEnding) -> io::Result<LineReader> {
285295
if name == "-" {
286-
Ok(LineReader::new(Input::Stdin(stdin()), line_ending))
296+
Ok(LineReader::new(Input::stdin(), line_ending))
287297
} else {
288298
if metadata(name)?.is_dir() {
289299
return Err(io::Error::other(translate!("comm-error-is-directory")));
290300
}
291301
let f = File::open(name)?;
292-
Ok(LineReader::new(
293-
Input::FileIn(BufReader::new(f)),
294-
line_ending,
295-
))
302+
Ok(LineReader::new(Input::from_file(f), line_ending))
296303
}
297304
}
298305

0 commit comments

Comments
 (0)