Skip to content

Add IO function for creating new directories #420

@kings177

Description

@kings177

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: If True, create parent directories as needed. If False, parent directories must exist
  • exist_ok: If True, 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 and exist_ok is False
  • FileNotFound: A parent directory doesn't exist and parents is False
  • 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

  1. Create a new directory in an existing parent directory
  2. Attempt to create a directory that already exists (with and without exist_ok)
  3. Create a directory with multiple levels of non-existent parent directories (with and without parents flag)
  4. Attempt to create a directory without proper permissions
  5. Create a directory with a very long path name (test path length limits)
  6. Create a directory with special characters in the name
  7. Attempt to create a directory in a read-only file system
  8. Create multiple directories concurrently to test thread-safety
  9. Create a directory where a file with the same name already exists
  10. Attempt to create a directory with an invalid name (e.g., empty string, only whitespace)
  11. Create a directory using relative paths
  12. Create a directory using absolute paths
  13. Test behavior when disk is full

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions