Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

om init: Copy symlinks as is #443

Merged
merged 2 commits into from
Mar 17, 2025
Merged
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
43 changes: 28 additions & 15 deletions crates/omnix-common/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Filesystem utilities
use async_walkdir::WalkDir;
use async_walkdir::{DirEntry, WalkDir};
use futures_lite::stream::StreamExt;
use std::{
os::unix::fs::PermissionsExt,
Expand All @@ -11,30 +11,43 @@ use tokio::fs;
///
/// The target directory will always be user readable & writable.
pub async fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> anyhow::Result<()> {
let src = src.as_ref();
let dst = dst.as_ref();

let mut walker = WalkDir::new(src);
let mut walker = WalkDir::new(&src);

while let Some(entry) = walker.next().await {
let entry = entry?;
let path = &entry.path();
let relative = path.strip_prefix(src)?;
let target = dst.join(relative);
copy_entry(&src, entry?, &dst).await?;
}
Ok(())
}

if entry.file_type().await?.is_dir() {
fs::create_dir_all(&target).await?;
async fn copy_entry(
src: impl AsRef<Path>,
entry: DirEntry,
dst: impl AsRef<Path>,
) -> anyhow::Result<()> {
let path = &entry.path();
let relative = path.strip_prefix(src)?;
let target = dst.as_ref().join(relative);

let file_type = entry.file_type().await?;
if file_type.is_dir() {
// Handle directories
fs::create_dir_all(&target).await?;
} else {
if let Some(parent) = target.parent() {
fs::create_dir_all(parent).await?;
}
if file_type.is_symlink() {
// Handle symlinks *as is* (we expect relative symlink targets) without resolution.
let link_target = fs::read_link(path).await?;
fs::symlink(&link_target, &target).await?;
} else {
if let Some(parent) = target.parent() {
fs::create_dir_all(parent).await?;
}
// Handle regular files
fs::copy(path, &target).await?;
// Because we are copying from the Nix store, the source paths will be read-only.
// So, make the target writeable by the owner.
make_owner_writeable(&target).await?;
}
}

Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions doc/src/history.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- Extra nix handling
- Allow `--override-input` to work again (#439)
- Support `--rebuild` by disallowing it in irrelevant subcommands (`eval`, `develop`, `run`, `flake {lock,check}`) (#441)
- `om init`
- Handle symlinks *as is* (we expect relative symlink targets) without resolution (#443)

## 1.0.2 (2025-03-11) {#1.0.2}

Expand Down