Skip to content

Commit

Permalink
Merge #7
Browse files Browse the repository at this point in the history
7: cpt r=matklad a=matklad

- mktemp_d and tests for cp
- Publish 0.1.7


Co-authored-by: Aleksey Kladov <[email protected]>
  • Loading branch information
bors[bot] and matklad authored Oct 28, 2020
2 parents c4d641c + 975888b commit 9c45881
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.1.7

- `mktemp_d` creates an (insecure, world readable) temporary directory.
- `cp(foo, bar)` copies `foo` _into_ `bar`, if `bar` is an existing directory.

## 0.1.6

- `.read()` chomps `\r\n` on Windows.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "xshell"
description = "Utilities for quick shell scripting in Rust"
categories = ["development-tools::build-utils", "filesystem"]
version = "0.1.6"
version = "0.1.7"
license = "MIT OR Apache-2.0"
repository = "https://github.com/matklad/xshell"
authors = ["Aleksey Kladov <[email protected]>"]
Expand Down
44 changes: 43 additions & 1 deletion src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::{Path, PathBuf};
use std::{
path::{Path, PathBuf},
sync::atomic::{AtomicUsize, Ordering},
};

use crate::{error::fs_err, gsl, Result};

Expand Down Expand Up @@ -66,6 +69,29 @@ pub fn cwd() -> Result<PathBuf> {
with_path(&Path::new("."), std::env::current_dir())
}

pub fn mktemp_d() -> Result<TempDir> {
let _guard = gsl::read();
let base = std::env::temp_dir();
let pid = std::process::id();
mkdir_p(&base)?;

static CNT: AtomicUsize = AtomicUsize::new(0);

let mut n_try = 0u32;
loop {
n_try += 1;
let cnt = CNT.fetch_add(1, Ordering::Relaxed);
let path = base.join(format!("{}_{}", pid, cnt));
if let Err(io_err) = std::fs::create_dir(&path) {
if n_try == 1024 {
return Err(fs_err(path, io_err));
}
continue;
}
return Ok(TempDir { path });
}
}

fn with_path<T>(path: &Path, res: Result<T, std::io::Error>) -> Result<T> {
res.map_err(|io_err| fs_err(path.to_path_buf(), io_err))
}
Expand Down Expand Up @@ -95,3 +121,19 @@ fn read_dir_aux(path: &Path) -> std::io::Result<Vec<PathBuf>> {
res.sort();
Ok(res)
}

pub struct TempDir {
path: PathBuf,
}

impl TempDir {
pub fn path(&self) -> &Path {
&self.path
}
}

impl Drop for TempDir {
fn drop(&mut self) {
rm_rf(&self.path).unwrap()
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub use xshell_macros::__cmd;
pub use crate::{
env::{pushd, pushenv, Pushd, Pushenv},
error::{Error, Result},
fs::{cp, cwd, mkdir_p, read_dir, read_file, rm_rf, write_file},
fs::{cp, cwd, mkdir_p, mktemp_d, read_dir, read_file, rm_rf, write_file, TempDir},
};

#[macro_export]
Expand Down
24 changes: 23 additions & 1 deletion tests/it.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{ffi::OsStr, thread, time::Duration, time::Instant};

use xshell::{cmd, cwd, mkdir_p, pushd, pushenv, read_file, rm_rf, write_file};
use xshell::{cmd, cp, cwd, mkdir_p, mktemp_d, pushd, pushenv, read_file, rm_rf, write_file};

#[test]
fn smoke() {
Expand Down Expand Up @@ -208,6 +208,28 @@ fn test_pushenv_lock() {
t2.join().unwrap();
}

#[test]
fn test_cp() {
let path;
{
let tempdir = mktemp_d().unwrap();
path = tempdir.path().to_path_buf();
let foo = tempdir.path().join("foo.txt");
let bar = tempdir.path().join("bar.txt");
let dir = tempdir.path().join("dir");
write_file(&foo, "hello world").unwrap();
mkdir_p(&dir).unwrap();

cp(&foo, &bar).unwrap();
assert_eq!(read_file(&bar).unwrap(), "hello world");

cp(&foo, &dir).unwrap();
assert_eq!(read_file(&dir.join("foo.txt")).unwrap(), "hello world");
assert!(path.exists());
}
assert!(!path.exists());
}

fn check_failure(code: &str, err_msg: &str) {
mkdir_p("./target/cf").unwrap();
let _p = pushd("./target/cf").unwrap();
Expand Down

0 comments on commit 9c45881

Please sign in to comment.