From d21c5dc9c53b02620fce916ffc1a2695e9d3f698 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 9 Nov 2021 12:46:43 +0100 Subject: Separate the binary and library This uses the workspace feature of cargo, with the benefit that 1) We can more cleanly group the binary (user facing) code from the library 2) We can have dependencies that apply to the binary only The first point could've been achieved without workspaces (Cargo supports both binaries and libraries in a crate), but the second point is what really makes this approach a lot better. --- src/util.rs | 62 ------------------------------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 src/util.rs (limited to 'src/util.rs') diff --git a/src/util.rs b/src/util.rs deleted file mode 100644 index ea401ba..0000000 --- a/src/util.rs +++ /dev/null @@ -1,62 +0,0 @@ -//! Utility functions. -use std::{fs, io, path::Path}; - -#[cfg(unix)] -use nix::unistd::{self, Gid, Uid}; - -use super::error::Result; - -#[cfg(not(unix))] -pub mod nix { - //! Stub mod on non-unix systems. - pub mod unistd { - pub enum Uid {} - pub enum Gid {} - } -} - -/// Recursively copy the *contents* of the given directory to the given path. -pub fn copy_recursive, D: AsRef>(source: S, destination: D) -> io::Result<()> { - copy_inner(source.as_ref(), destination.as_ref()) -} - -fn copy_inner(source: &Path, destination: &Path) -> io::Result<()> { - for item in fs::read_dir(source)? { - let item = item?; - let metadata = item.metadata()?; - let item_destination = destination.join(item.file_name()); - if metadata.is_file() { - fs::copy(&item.path(), &item_destination)?; - } else if metadata.is_dir() { - fs::create_dir(&item_destination)?; - copy_inner(&item.path(), &item_destination)?; - } - } - Ok(()) -} - -/// Recursively change the owner of the given path to the given ones. -/// -/// Note that this function only works on Unix systems. **It will panic on other systems!** -pub fn chown_recursive>(path: P, uid: Option, gid: Option) -> Result<()> { - chown_inner(path.as_ref(), uid, gid) -} - -#[cfg(unix)] -fn chown_inner(path: &Path, uid: Option, gid: Option) -> Result<()> { - unistd::chown(path, uid, gid)?; - - let metadata = fs::metadata(path)?; - if metadata.is_dir() { - for item in fs::read_dir(path)? { - let item = item?; - chown_inner(&item.path(), uid, gid)?; - } - } - Ok(()) -} - -#[cfg(not(unix))] -fn chown_inner(_: &Path, _: Option, _: Option) -> Result<()> { - panic!("chown() is not available on non-Unix systems!"); -} -- cgit v1.2.3