Open kings177 opened 3 weeks ago
@kings177 I don't think this is a good interface.
delete_file
implies that it only removes non-directory files, which isn't the case. It'd be better to just call it delete
or even better, remove
.delete_directory
without passing the recursive flag is redundant with delete_file
and in almost all cases useless. This function really only exists for its recursive option.delete_file
to remove both directories and non-directory files while delete_directory
removes only directories. It'd be better for both functions to have similar behaviours.I think a better interface would be one of these two:
remove
for removing non-directory files and empty directories and remove_all
for removing any files recursively, including trees of directories.remove_file
for removing non-directory files, remove_dir
for removing empty directories, remove_tree
for removing trees of directories.Personally I'm favorable to the first one, but either would be fine.
The second one can still be implemented with only two functions and the recursive flag, choosing between recursive or not can be an additional layer in bend.
As always, names can still be bikeshed (remove vs remove_file, remove_dir vs rmdir, etc)
those are some pretty good points, and i agree.
remove for removing non-directory files and empty directories and remove_all for removing any files recursively, including trees of directories.
i do prefer this over the one originally written in the issue. so changes to this would be:
delete_file(path)
we would have remove(path)
delete_directory(path)
we would have remove_all(path)
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)
Deletes a single file or an empty directory at the specified path.
Returns
Ok(None)
if successful, orErr(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 existPermissionDenied
: Lack of permission to delete the file or directoryNotEmpty
: Attempted to remove a non-empty directoryOSError
: Other OS-level errors (e.g., I/O error)Examples
2.
delete_directory(path, recursive=False)
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 existPermissionDenied
: Lack of permission to delete the directory or its contentsNotADirectory
: The path is not a directoryOSError
: Other OS-level errors (e.g., I/O error)Examples
Considerations
unlink
andrmdir
on files and directories.Test cases to implement