Bend
Bend copied to clipboard
Add IO functions for removing files and directories
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
andremove_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
- Remove a file.
- Remove an empty directory.
- Remove a directory containing files and subdirectories with
remove_all
. - Remove a symbolic link.
- Attempt to remove a non-empty directory with
remove
. - Attempt to remove a file or directory without sufficient permissions.
- Attempt to remove a non-existent file or directory.
- Attempt to remove a file or directory with an invalid path.