HVM
HVM copied to clipboard
Add IO function for creating new directories
Implement a new IO function in the HVM to give Bend the ability to create new directories in the file system.
1. create_directory(path, parents=False, exist_ok=False)
@spec create_directory(str, bool, bool) -> Result[None, Error]
Creates a new directory at the specified path.
Parameters
-
path
: The path where the new directory should be created -
parents
: IfTrue
, create parent directories as needed. IfFalse
, parent directories must exist -
exist_ok
: IfTrue
, don't raise an error if the directory already exists
Returns Ok(None)
if successful, or Err(reason)
if an error occurs.
Possible (but not limited to) errors:
-
FileExists
: The directory already exists andexist_ok
isFalse
-
FileNotFound
: A parent directory doesn't exist and parents isFalse
-
PermissionDenied
: The user doesn't have permission to create the directory -
OSError
: Other OS-level errors (e.g., disk full, invalid path)
Examples
# Create a new directory
result = create_directory("new_folder")
# Ok(None)
# Create a directory with non-existent parents, creating them as needed
result = create_directory("path/to/new/folder", parents=True)
# Ok(None)
# Create a directory, allowing it to exist
result = create_directory("maybe_existing_folder", exist_ok=True)
# Ok(None)
# Try to create an existing directory
result = create_directory("existing_folder")
# Err(FileExists)
# Create a directory with non-existent parents
result = create_directory("path/to/new/folder")
# Err(FileNotFound)
Considerations
- Implement proper error handling and propagation.
- Ensure thread-safety for directory creation.
- Handle path normalization (e.g., handling '...' and '.' in paths).
- Consider preserving or setting specific permissions for newly created directories.
- Be aware of and handle potential race conditions (e.g., directories being created/deleted concurrently)
- Implement appropriate logging for debugging.
Test cases to implement
- Create a new directory in an existing parent directory
- Attempt to create a directory that already exists (with and without
exist_ok
) - Create a directory with multiple levels of non-existent parent directories (with and without
parents
flag) - Attempt to create a directory without proper permissions
- Create a directory with a very long path name (test path length limits)
- Create a directory with special characters in the name
- Attempt to create a directory in a read-only file system
- Create multiple directories concurrently to test thread-safety
- Create a directory where a file with the same name already exists
- Attempt to create a directory with an invalid name (e.g., empty string, only whitespace)
- Create a directory using relative paths
- Create a directory using absolute paths
- Test behavior when disk is full