# File.OpenFlags: Add \`allow_directory\` and default it to true · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/d72983da44b7ebe967868ba5236a2c4b05e84510)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: d72983da44b7ebe967868ba5236a2c4b05e84510

Requested commit: d72983da44b7ebe967868ba5236a2c4b05e84510

Commit: d72983da44b7ebe967868ba5236a2c4b05e84510

Tree: 21cd22d68e90511d59519047fbb9525c711777fc

Author: Ryan Liptak

Committer: Andrew Kelley

## Message

```
File.OpenFlags: Add `allow_directory` and default it to true

This is one way of addressing/closing https://github.com/ziglang/zig/issues/16738

Previously, there was a mismatch between the default behaviors on Windows vs other platforms, where Windows was implicitly using .NON_DIRECTORY_FILE for its `openFile` implementation which caused `error.IsDir` when opening a directory, while on other platforms there is no equivalent flag for the `open` syscall. This meant that `openFile` on a path of a directory would fail on Windows but succeed on other platforms.

Adding `allow_directory` to `File.OpenFlags` serves two purposes:

1. It provides a cross-platform way to get the `.NON_DIRECTORY_FILE` behavior in the most efficient available way for the platform (on Windows, no extra syscalls are required, on other systems, an extra `fstat` is required)
2. It allows `statFile` to be implemented on top of `openFile` on Windows while still allowing `statFile` to work on directory paths. Before this commit, `statFile` on a directory path on Windows failed with `error.IsDir`

Note: The second purpose could have been addressed in different ways (bespoke call to NtCreateFile in the `statFile` implementation to avoid passing `NON_DIRECTORY_FILE`, or just never pass `NON_DIRECTORY_FILE` in the `openFile` implementation), so the first purpose is the more relevant/motivating force behind this change.

The default being `true` is intended to cut down on the number of syscalls as much as possible when using the default flags.

```

## Parents

- [1264469a41e89e72635c3b60b4cd3dca17876964](https://git.cafe/gitcafe/zig/commit/1264469a41e89e72635c3b60b4cd3dca17876964?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/d72983da44b7ebe967868ba5236a2c4b05e84510?format=markdown)
