HVM icon indicating copy to clipboard operation
HVM copied to clipboard

Add IO functions for moving files and directories

Open kings177 opened this issue 6 months ago • 0 comments

Implement moving functionality to give Bend the ability to move files and directories. This should support both single file moving and recursive directory moving.

1. move(source, destination, overwrite=False)

@spec move(str, str, bool) -> Result[None, Error]

Moves a file or directory from the source path to the destination path.

Parameters

  • source: Path of the file or directory to be moved
  • destination: Path where the file or directory should be moved to
  • overwrite: If True, overwrites the destination if it exists. If False, 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 exist
  • PermissionDenied: Lack of permission to read the source or write to the destination
  • FileExists: The destination already exists and overwrite is False

Examples

# Move a file
result = move("source.txt", "destination.txt")
# Ok(None)
# Move and overwrite an existing file
result = move("source.txt", "destination.txt", overwrite=True)
# Ok(None)
# Move a directory
result = move("source_dir", "dest_dir")
# Ok(None)
# Try to move a non-existent file or directory
result = move("nonexistent", "dest")
# Err(FileNotFound)
# Try to move to an existing destination without overwrite
result = move("source.txt", "existing.txt")
# Err(FileExists)

Considerations

  • Handle moving across different filesystems
  • Consider preserving file metadata (e.g. permissions, timestamps) during moving.
  • Handle moving directories with large numbers of files efficiently.
  • Handle symbolic links appropriately (whether to follow them or move the link itself).
  • Be aware of potential issues with moving files or directories that are currently in use.
  • Implement proper error handling and propagation.

Test cases to implement

  1. Move a small text file
  2. Move a large binary file
  3. Move an empty directory
  4. Move a directory with multiple files and subdirectories
  5. Attempt to move a non-existent file or directory
  6. Attempt to move to a read-only destination
  7. Move a file or directory to an existing destination with and without overwrite flag
  8. Move a file or directory across different filesystems
  9. Move a file or directory with special characters in the name
  10. Move a symbolic link (both following and not following the link)
  11. Move files and directories with various permission settings
  12. Attempt to move a file or directory into itself or its subdirectory
  13. Move a file or directory that is currently open or in use

kings177 avatar Aug 20 '24 19:08 kings177