Skip to content

feat: creating and removing empty directories #1011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

wassup05
Copy link
Contributor

@wassup05 wassup05 commented Jul 1, 2025

User facing functions added are

  • make_directory (path, mode, err) (mode argument only for Unix systems)
  • remove_directory (path, err)
  1. Both of them support relative paths and on Windows, paths involving either / or \.
  2. They bind to the CRT functions mkdir, rmdir, _mkdir, _rmdir on Unix and Windows respectively.
  3. A binding to strerror has been included as a private helper to provide helpful error messages.
  4. state_type is used for error handling.

Side note:
make_directory could have a logical argument recursive which would work like mkdir -p but that would require path manipulation especially dir_name from #999

Do let me know your thoughts.

@sebastian-mutz
Copy link
Contributor

Hey @wassup05. Thanks for the PR. Great work!

  1. I added minor suggestions to the comments (just grammar).
  2. For example_remove_directory, I wonder if it makes sense to first create the directory. Ideally an example only demonstrates the use of one procedure, but without the existing directory, it can't. Just a thought and nothing major.

!!
!! ### Description
!! This function makes an empty directory according to the path provided.
!! Relative paths as well as on Windows paths involving either `/` or `\` are accepted
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add period to end of sentence.

!! ### Description
!! This function makes an empty directory according to the path provided.
!! Relative paths as well as on Windows paths involving either `/` or `\` are accepted
!! appropriate error message is returned whenever any error occur.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalise "appropriate" to be consistent. (Same down below).

@wassup05
Copy link
Contributor Author

wassup05 commented Jul 9, 2025

For example_remove_directory, I wonder if it makes sense to first create the directory. Ideally an example only demonstrates the use of one procedure, but without the existing directory, it can't. Just a thought and nothing major.

I think it's fine @sebastian-mutz, doing that would kind of clutter the example I feel, and this is how it's been handled in some other examples as well like here and here

@sebastian-mutz
Copy link
Contributor

For example_remove_directory, I wonder if it makes sense to first create the directory. Ideally an example only demonstrates the use of one procedure, but without the existing directory, it can't. Just a thought and nothing major.

I think it's fine @sebastian-mutz, doing that would kind of clutter the example I feel, and this is how it's been handled in some other examples as well like here and here

Makes sense. Then it's best to keep it consistent.

@wassup05
Copy link
Contributor Author

Concerning the mode argument which is not applicable to Windows, here is how some other languages handle it...

Python

Complete docs here: https://docs.python.org/3/library/os.html#os.mkdir
To quote some important parts:

On some systems, mode is ignored. Where it is used, the current umask value is first masked out. If bits other than the last 9 (i.e. the last 3 digits of the octal representation of the mode) are set, their meaning is platform-dependent. On some platforms, they are ignored and you should call chmod() explicitly to set them.

On Windows, a mode of 0o700 is specifically handled to apply access control to the new directory such that only the current user and administrators have access. Other values of mode are ignored.

Golang

Golang does it in a different way by creating a portable custom struct of the permission bits, to quote exactly

A FileMode represents a file's mode and permission bits. The bits have the same definition on all systems, so that information about files can be moved from one system to another portably. Not all bits apply to all systems. The only required bit is ModeDir for directories.

Reference: https://pkg.go.dev/os#FileMode, https://pkg.go.dev/os#Mkdir

Rust

Rust seems to have dropped this argument completely and uses default permissions
Reference: https://doc.rust-lang.org/std/fs/fn.create_dir.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants