mirror of
https://github.com/chmln/sd
synced 2024-11-14 15:37:08 +00:00
parent
fd55fa0982
commit
c8022e292f
2 changed files with 20 additions and 5 deletions
|
@ -113,9 +113,11 @@ impl Replacer {
|
||||||
file.set_len(replaced.len() as u64)?;
|
file.set_len(replaced.len() as u64)?;
|
||||||
file.set_permissions(meta.permissions())?;
|
file.set_permissions(meta.permissions())?;
|
||||||
|
|
||||||
let mut mmap_target = unsafe { MmapMut::map_mut(&file)? };
|
if !replaced.is_empty() {
|
||||||
mmap_target.deref_mut().write_all(&replaced)?;
|
let mut mmap_target = unsafe { MmapMut::map_mut(&file)? };
|
||||||
mmap_target.flush_async()?;
|
mmap_target.deref_mut().write_all(&replaced)?;
|
||||||
|
mmap_target.flush_async()?;
|
||||||
|
}
|
||||||
|
|
||||||
drop(mmap_source);
|
drop(mmap_source);
|
||||||
drop(source);
|
drop(source);
|
||||||
|
|
17
tests/cli.rs
17
tests/cli.rs
|
@ -24,14 +24,27 @@ fn in_place() -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn in_place_with_empty_result_file() -> Result<()> {
|
||||||
|
let mut file = tempfile::NamedTempFile::new()?;
|
||||||
|
file.write(b"a7c")?;
|
||||||
|
let path = file.into_temp_path();
|
||||||
|
|
||||||
|
sd().args(&["a\\dc", "", path.to_str().unwrap()])
|
||||||
|
.assert()
|
||||||
|
.success();
|
||||||
|
assert_file(&path.to_path_buf(), "");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn replace_into_stdout() -> Result<()> {
|
fn replace_into_stdout() -> Result<()> {
|
||||||
let mut file = tempfile::NamedTempFile::new()?;
|
let mut file = tempfile::NamedTempFile::new()?;
|
||||||
file.write(b"abc123def")?;
|
file.write(b"abc123def")?;
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
sd()
|
sd().args(&["-p", "abc\\d+", "", file.path().to_str().unwrap()])
|
||||||
.args(&["-p", "abc\\d+", "", file.path().to_str().unwrap()])
|
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("def");
|
.stdout("def");
|
||||||
|
|
Loading…
Reference in a new issue