Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions 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 <aleksey.kladov@gmail.com>"]
Expand All @@ -13,4 +13,4 @@ exclude = [".github/", "bors.toml", "rustfmt.toml", "cbench", "mock_bin/"]
[workspace]

[dependencies]
xshell-macros = { version = "0.1.6", path = "./xshell-macros"}
xshell-macros = { version = "0.1.7", path = "./xshell-macros"}
41 changes: 40 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,26 @@ 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();

mkdir_p(&base)?;

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

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

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 +118,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
Loading