Bend icon indicating copy to clipboard operation
Bend copied to clipboard

Add IO functions for removing files and directories

Open kings177 opened this issue 6 months ago • 0 comments

Depends on

  • [ ] https://github.com/HigherOrderCO/HVM/issues/418

Add file and directory removal capabilities to Bend by implementing remove and remove_all functions that interface with the HVM's IO system.

1. remove(path)

Removes a file or an empty directory at the specified path.

Parameters

  • path: Path of the file or empty directory to be removed

Example Usage

# Remove a file
result = remove("text.txt")
match result:
    case Ok(_):
        print("File removed successfully")
    case Err(e):
        print(f"Error removing file: {e}")
# Remove an empty directory
result = remove("empty_dir")
match result:
    case Ok(_):
        print("Directory removed successfully")
    case Err(e):
        print(f"Error removing directory: {e}")
```	

## 2. `remove_all(path)`
Removes a directory at `path`, along with all of its contents.

### Parameters
- `path`: Path of the directory to be removed

### Example Usage
```py
# Remove a directory and all its contents
result = remove_all("non_empty_dir")
match result:
    case Ok(_):
        print("Directory and its contents removed successfully")
    case Err(e):
        print(f"Error removing directory: {e}")

Implementation Details

  • remove_all should handle recursive deletion of subdirectories and files.
  • Both functions should properly handle and report errors from the HVM layer (e.g., FileNotFound, PermissionDenied, NotEmpty, NotADirectory, OSError).
  • Write clear docstrings for the remove and remove_all functions, explaining their purpose and usage.

Considerations

  • Consider the behavior when removing symbolic links.
  • Implement proper error handling for invalid paths, permissions, and other potential issues.

Testing

  1. Remove a file.
  2. Remove an empty directory.
  3. Remove a directory containing files and subdirectories with remove_all.
  4. Remove a symbolic link.
  5. Attempt to remove a non-empty directory with remove.
  6. Attempt to remove a file or directory without sufficient permissions.
  7. Attempt to remove a non-existent file or directory.
  8. Attempt to remove a file or directory with an invalid path.

kings177 avatar Aug 20 '24 20:08 kings177