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

fs.copy only works if the source directory contains subdirectories. #154

Closed
plainenglishh opened this issue Feb 18, 2024 · 0 comments
Closed
Labels
bug Something isn't working

Comments

@plainenglishh
Copy link
Contributor

The fs.copy function fails when trying to copy a directory that doesn't contain any subdirectories.

Presumably this is because the function implementation at 'src/lune/builtin/fs/copy.rs' doesn't create the target directory and instead relies on fs::create_dir_all to create it while it populates the subdirectories.

// src/lune/builtins/fs/copy.rs:137..161
} else if is_dir {
    let contents = get_contents_at(source.to_path_buf(), options).await?;
    
    if options.overwrite {
        let (is_dir, is_file) = match fs::metadata(&target).await {
            Ok(meta) => (meta.is_dir(), meta.is_file()),
            Err(e) if e.kind() == ErrorKind::NotFound => (false, false),
            Err(e) => return Err(e.into()),
        };
        if is_dir {
            fs::remove_dir_all(target).await?;
        } else if is_file {
            fs::remove_file(target).await?;
        }
    }
    
    // FUTURE: Write dirs / files concurrently
    // to potentially speed these operations up
    for (_, dir) in &contents.dirs {
        fs::create_dir_all(target.join(dir)).await?;
        // The line above creates the target directory as a result of populating the target subdirectories. This line is never reached if the target directory doesn't contain subdirectories, however.
    }
    for (_, file) in &contents.files {
        fs::copy(source.join(file), target.join(file)).await?;
    }
}

This results in the function either silently failing (in the case of an empty directory), or throwing an error when trying to populate the files into the nonexistant target directory.

This is easily fixed by inserting fs::create_dir_all(target).await?; on line 152.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants