Skip to content

Commit

Permalink
Merge pull request #141 from symmetree-labs/windows
Browse files Browse the repository at this point in the history
Enable windows builds
  • Loading branch information
rsdy committed May 14, 2022
2 parents 98306e8 + a0466f8 commit 177d4e6
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 8 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ jobs:
matrix:
name: [
linux,
macos
# - windows-latest # disabled until morale improves
macos,
windows
]

include:
Expand All @@ -52,6 +52,12 @@ jobs:
target: x86_64-apple-darwin
cross: false

- name: windows
os: windows-latest
asset_name: 0s-windows-x86_64
target: x86_64-pc-windows-msvc
cross: true

rust:
- 1.60.0 # MSRV

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ instead of a path to the repository.
0s commit mystash /path/to/movies

## Installation

Zerostash works on Linux, macOS, and Windows, and you can download
[pre-built binaries](https://github.com/rsdy/zerostash/releases)!

If you're looking for package manager integrations, though, look below.

### Installation on macOS

There is a homebrew tap you can use!
Expand Down Expand Up @@ -95,7 +101,7 @@ Place it in your `$PATH`, and then run:

0s --help

### Build from source.
### Build from source

The usual Rust incantation will also do to build the binary
yourself. Use [`rustup`](https://rustup.rs/) to get `cargo` running or
Expand Down
54 changes: 50 additions & 4 deletions zerostash-files/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ impl Entry {
path: &impl AsRef<Path>,
preserve: &PreserveMetadata,
) -> Result<Entry, EntryError> {
let path = path.as_ref();
let (unix_secs, unix_nanos) = if preserve.times {
to_unix_mtime(&metadata)?
} else {
Expand Down Expand Up @@ -217,7 +216,7 @@ impl Entry {
},

size: metadata.len(),
name: normalize_filename(&path)?,
name: normalize_filename(path)?,

chunks: Vec::new(),
})
Expand All @@ -229,18 +228,37 @@ impl Entry {
path: &impl AsRef<Path>,
preserve: &PreserveMetadata,
) -> Result<Option<fs::File>, EntryError> {
use FileType::*;

let file = match self.file_type {
Directory => {
fs::create_dir_all(path)?;
fs::File::open(path)?
}
File => {
let file = open_file(path)?;
file.set_len(self.size)?;
file
}
Symlink(ref pointed_to) => open_symlink(path, pointed_to)?,
};

file.set_len(self.size)?;

if let Some(readonly) = self.readonly {
if preserve.permissions {
let metadata = file.metadata()?;
let mut permissions = metadata.permissions();
permissions.set_readonly(readonly);
file.set_permissions(permissions);
file.set_permissions(permissions)?;
}
}

Ok(())
Ok(if self.file_type.is_file() {
Some(file)
} else {
None
})
}

#[cfg(unix)]
Expand Down Expand Up @@ -288,6 +306,34 @@ impl Entry {
}
}

#[cfg(windows)]
fn open_symlink(
path: impl AsRef<Path> + Copy,
pointed_to: impl AsRef<Path> + Copy,
) -> Result<fs::File, io::Error> {
use std::os::windows::fs::{symlink_dir, symlink_file};

let pointed_md = std::fs::metadata(pointed_to)?;
let symlink = if pointed_md.is_dir() {
symlink_dir
} else {
symlink_file
};

match symlink(pointed_to, path) {
Ok(()) => Ok(fs::OpenOptions::new().read(true).open(path)?),
Err(err) if err.kind() == io::ErrorKind::NotFound => {
if let Some(parent) = path.as_ref().parent() {
fs::create_dir_all(parent)?;
open_symlink(path, pointed_to)
} else {
Err(err)
}
}
Err(err) => Err(err),
}
}

#[cfg(unix)]
fn open_symlink(
path: impl AsRef<Path> + Copy,
Expand Down
1 change: 1 addition & 0 deletions zerostash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rprompt = "1.0.5"
serde = { version = "1", features = ["serde_derive"] }
toml = "0.5"
xdg = "2.2"
dirs = "4.0"
ignore = "0.4"

humansize = "1.1.1"
Expand Down
3 changes: 2 additions & 1 deletion zerostash/src/commands/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use abscissa_core::{
use chrono::{DateTime, Utc};
use clap::Parser;
use humansize::{file_size_opts, FileSize};
use nix::unistd::{Gid, Group, Uid, User};
use std::{io::Write, sync::Arc};
use termcolor::{Color, ColorSpec, WriteColor};
use zerostash_files::*;
Expand Down Expand Up @@ -119,6 +118,7 @@ impl Ls {

#[cfg(unix)]
fn get_uid(uid: Option<u32>) -> String {
use nix::unistd::{Uid, User};
uid.and_then(|uid| User::from_uid(Uid::from_raw(uid)).ok())
.flatten()
.map(|u| u.name)
Expand All @@ -127,6 +127,7 @@ fn get_uid(uid: Option<u32>) -> String {

#[cfg(unix)]
fn get_gid(gid: Option<u32>) -> String {
use nix::unistd::{Gid, Group};
gid.and_then(|gid| Group::from_gid(Gid::from_raw(gid)).ok())
.flatten()
.map(|g| g.name)
Expand Down
13 changes: 13 additions & 0 deletions zerostash/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,26 @@ pub enum Backend {

impl ZerostashConfig {
/// Path to the configuration directory
#[cfg(unix)]
pub fn path() -> PathBuf {
xdg::BaseDirectories::with_prefix("zerostash")
.unwrap()
.place_config_file("config.toml")
.expect("cannot create configuration directory")
}

/// Path to the configuration directory
#[cfg(windows)]
pub fn path() -> PathBuf {
let mut p = dirs::home_dir().expect("cannot find home directory");

p.push(".zerostash");
std::fs::create_dir_all(&p).expect("failed to create config dir");

p.push("config.toml");
p
}

/// Write the config file to the file system
pub fn write(&self) -> Result<()> {
unimplemented!()
Expand Down

0 comments on commit 177d4e6

Please sign in to comment.