-
Notifications
You must be signed in to change notification settings - Fork 431
Open
Labels
enhancementNew feature or requestNew feature or request
Milestone
Description
Implement copy functionality to give Bend the ability to copy files and directories. This should support both single file copying and recursive directory copying.
1. copy(source, destination, recursive=False, overwrite=False)
@spec copy(str, str, bool, bool) -> Result[None, Error]Copies a file or directory from the source path to the destination path.
Parameters
source: Path of the file or directory to be copieddestination: Path where the file or directory should be copied torecursive: IfTrue, copies directories and their contents recursively. Ignored for single copies.overwrite: IfTrue, overwrites the destination if it exists. IfFalse, returns an error if the destination exists.
Returns Ok(None) if successful, or Err(reason) if an error occurs.
Possible (but not limited to) errors:
FileNotFound: The source file or directory does not existPermissionDenied: Lack of permission to read the source or write to the destinationFileExists: The destination already exists and overwrite isFalseNotADirectory: The source is a directory but recursive isFalse
Examples
# Copy a file
result = copy("source.txt", "destination.txt")
# Ok(None)# Copy and overwrite an existing file
result = copy("source.txt", "existing.txt", overwrite=True)
# Ok(None)# Recursively copy a directory and its contents
result = copy("source_dir", "dest_dir", recursive=True)
# Ok(None)# Try to copy to non-existing file or directory
result = copy("nonexistent, "dest")
# Err(FileNotFound)# Try to copy to an existing destination without overwrite
result = copy("source.txt", "existing.txt")
# Err(FileExists)# Try to copy a directory (non-recursively)
result = copy("source_dir", "dest_dir")
# Err(NotADirectory)Considerations
- Handle symbolic links appropriately (whether to follow them or copy the link itself).
- Implement proper error handling and propagation.
- Consider preserving file metadata (permissions etc) during copying.
- Ensure the function can distinguish between files and directories without relying on separate stat calls.
- Be aware of potential issues with copying across different filesystems or drives.
- Implement appropriate logging for debugging.
Test cases to implement
- Copy a small text file
- Copy a large binary file
- Recursively copy a directory with subdirectories and files
- Attempt to copy a non-existent file or directory
- Attempt to copy to a read-only destination
- Copy a file or directory to an existing destination with and without overwrite flag
- Copy a symbolic link (both the link itself and following the link)
- Copy files and directories with various permission settings
- Attempt to copy a directory non-recursively when it contains files or subdirectories
- Copy a file using the recursive flag (should work the same as non-recursive)
- Attempt to copy a file or directory into itself or its subdirectory
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request