HVM icon indicating copy to clipboard operation
HVM copied to clipboard

Add IO functions for removing files and directories

Open kings177 opened this issue 6 months ago • 2 comments

Implement a file deletion functionality to give Bend the ability to delete files from the filesystem. This should include both single file deletion and recursive directory deletion.

1. delete_file(path)

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

Deletes a single file or an empty directory at the specified path.

Returns Ok(None) if successful, or Err(reason) if an error occurs.

This function attempts to remove both files and empty directories without first checking the type of the path. It tries to remove the path as a file first, and if that fails, it attempts to remove it as a directory.

Possible (but not limited to) errors:

  • FileNotFound: The file or directory does not exist
  • PermissionDenied: Lack of permission to delete the file or directory
  • NotEmpty: Attempted to remove a non-empty directory
  • OSError: Other OS-level errors (e.g., I/O error)

Examples

# Delete an existing file
result = delete_file("example.txt")
# Ok(None)
# Delete an empty directory
result = delete_file("empty_dir")
# Ok(None)
# Try to delete a non-existent file or directory
result = delete_file("nonexistent.txt")
# Err(FileNotFound)
# Try to delete a non-empty directory
result = delete_file("my_directory")
# Err(NotEmpty)

2. delete_directory(path, recursive=False)

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

Deletes a directory at the specified path. If recursive is True, it will delete the directory and all its contents.

Returns Ok(None) if successful, or Err(reason) if an error occurs.

Note: For non-recursive deletion of an empty directory, this function behaves the same as delete_file(path).

Possible (but not limited to) errors:

  • FileNotFound: The directory does not exist
  • PermissionDenied: Lack of permission to delete the directory or its contents
  • NotADirectory: The path is not a directory
  • OSError: Other OS-level errors (e.g., I/O error)

Examples

# Delete an empty directory
result = delete_directory("empty_dir")
# Ok(None)
# Try to delete a non-empty directory
result = delete_directory("non_empty_dir")
# Err(NotEmpty)
# Delete a directory and its contents
result = delete_directory("non_empty_dir", recursive=True)
# Ok(None)
# Try to delete a file using delete_directory
result = delete_directory("file.txt")
# Err(NotADirectory)

Considerations

  • Implement a single function for both file and empty directory removal, similar to go's approach. as it can be more efficient than separate functions.
  • Avoid using a separate stat call to determine if the path is a file or directory. Instead, attempt both file and directory removal operations, as this is generally more efficient.
  • Be aware of differences in error reporting, especially for operations like unlink and rmdir on files and directories.
  • Implement proper error handling and propagation.
  • Ensure that read-only files can be deleted (will probably need some additional sys calls).
  • Add appropriate logging for debugging.

Test cases to implement

  1. Delete a regular file
  2. Attempt to delete a non-existent file
  3. Delete an empty directory
  4. Attempt to delete a non-empty directory
  5. Attempt to delete a file without proper permissions
  6. Delete a non-empty directory (recursive)
  7. Delete a read-only file
  8. Attempt to delete a file that is currently in use by another process

kings177 avatar Aug 20 '24 19:08 kings177