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

Fix overwrite option for copying directories #133

Merged
merged 2 commits into from
Dec 29, 2023
Merged

Conversation

bloeo
Copy link
Contributor

@bloeo bloeo commented Dec 6, 2023

Currently, the overwrite option when copying directories only works if the target already exists and is a directory. fs::remove_dir_all(target) errors if the target does not exist, and currently the overwriting code does not account for that whatever is overwritten might be a file rather than a directory.

fs = require("@lune/fs")

fs.writeDir("example")
fs.copy("example", "example2", {
    overwrite = true, -- If true, will error if a directory named "example2" doesn't already exist
})

Copy link
Collaborator

@filiptibell filiptibell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - thank you for the fix! One small comment and this should be good to go.

Path::exists uses sync filesystem API under the hood, so we end up doing two filesystem reads one of which unexpectedly blocks the thread.
One call to fs::metadata should be enough and we can match on the NotFound error instead and ignore that. Similar to how we do here:

let (is_dir, is_file) = match fs::metadata(&source).await {
Ok(meta) => (meta.is_dir(), meta.is_file()),
Err(e) if e.kind() == ErrorKind::NotFound => {
return Err(LuaError::RuntimeError(format!(
"No file or directory exists at the path '{}'",
source.display()
)))
}
Err(e) => return Err(e.into()),
};

@bloeo bloeo requested a review from filiptibell December 29, 2023 14:58
Copy link
Collaborator

@filiptibell filiptibell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@filiptibell filiptibell merged commit 39d9557 into lune-org:main Dec 29, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants