-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Win: add Crystal::System::DirHandle #4903
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| struct Crystal::System::DirHandle | ||
| # Creates a new `DirHandle` | ||
| # def self.new(path : String) : DirHandle | ||
|
|
||
| # Reads the next entry from dir and returns it as a string. Returns `nil` at the end of the stream. | ||
| # def read | ||
|
|
||
| # Repositions this directory to the first entry. | ||
| # def rewind | ||
|
|
||
| # Closes the directory stream. | ||
| # def close | ||
|
|
||
| # Returns the current working directory. | ||
| # def self.current : String | ||
|
|
||
| # Changes the current working directory of the process to the given string. | ||
| # def self.cd(path : String) | ||
|
|
||
| # Returns `true` if the given path exists and is a directory | ||
| # def self.exists?(path : String) : Bool | ||
|
|
||
| # Creates a new directory at the given path, including any non-existing | ||
| # intermediate directories. The linux-style permission mode can be specified. | ||
| # def self.mkdir(path : String, mode) | ||
|
|
||
| # Removes the directory at the given path. | ||
| # def self.rmdir(path : String) | ||
| end | ||
|
|
||
| {% if flag?(:win32) %} | ||
| require "./windows/dir_handle" | ||
| {% else %} | ||
| require "./unix/dir_handle" | ||
| {% end %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| require "c/dirent" | ||
| require "c/unistd" | ||
| require "c/sys/stat" | ||
|
|
||
| struct Crystal::System::DirHandle | ||
| @dirhandle : LibC::DIR* | ||
|
|
||
| @closed = false | ||
|
|
||
| def initialize(path : String) | ||
| @dirhandle = LibC.opendir(path.check_no_null_byte) | ||
| unless @dirhandle | ||
| raise Errno.new("Error opening directory '#{path}'") | ||
| end | ||
| @closed = false | ||
| end | ||
|
|
||
| def read | ||
| # readdir() returns NULL for failure and sets errno or returns NULL for EOF but leaves errno as is. wtf. | ||
| Errno.value = 0 | ||
| ent = LibC.readdir(@dirhandle) | ||
| if ent | ||
| String.new(ent.value.d_name.to_unsafe) | ||
| elsif Errno.value != 0 | ||
| raise Errno.new("readdir") | ||
| else | ||
| nil | ||
| end | ||
| end | ||
|
|
||
| def rewind | ||
| LibC.rewinddir(@dirhandle) | ||
| end | ||
|
|
||
| def close | ||
| return if @closed | ||
| if LibC.closedir(@dirhandle) != 0 | ||
| raise Errno.new("closedir") | ||
| end | ||
| @closed = true | ||
| end | ||
|
|
||
| def self.current : String | ||
| if dir = LibC.getcwd(nil, 0) | ||
| String.new(dir).tap { LibC.free(dir.as(Void*)) } | ||
| else | ||
| raise Errno.new("getcwd") | ||
| end | ||
| end | ||
|
|
||
| def self.cd(path : String) | ||
| if LibC.chdir(path.check_no_null_byte) != 0 | ||
| raise Errno.new("Error while changing directory to #{path.inspect}") | ||
| end | ||
| end | ||
|
|
||
| def self.exists?(path : String) : Bool | ||
| if LibC.stat(path.check_no_null_byte, out stat) != 0 | ||
| if Errno.value == Errno::ENOENT || Errno.value == Errno::ENOTDIR | ||
| return false | ||
| else | ||
| raise Errno.new("stat") | ||
| end | ||
| end | ||
| File::Stat.new(stat).directory? | ||
| end | ||
|
|
||
| def self.mkdir(path : String, mode) | ||
| if LibC.mkdir(path.check_no_null_byte, mode) == -1 | ||
| raise Errno.new("Unable to create directory '#{path}'") | ||
| end | ||
| end | ||
|
|
||
| def self.rmdir(path : String) | ||
| if LibC.rmdir(path.check_no_null_byte) == -1 | ||
| raise Errno.new("Unable to remove directory '#{path}'") | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| require "c/winapi" | ||
| require "winerror" | ||
|
|
||
| struct Crystal::System::DirHandle | ||
| @dir_handle : LibC::Handle | ||
|
|
||
| def initialize(path : String) | ||
| if !DirHandle.exists?(path) | ||
| raise WinError.new("Error opening directory '#{path}'") | ||
| end | ||
| @dir_handle = LibC::INVALID_HANDLE_VALUE | ||
| end | ||
|
|
||
| def read | ||
| data = LibC::WIN32_FIND_DATA_A.new | ||
| if @dir_handle == LibC::INVALID_HANDLE_VALUE | ||
| @dir_handle = LibC._FindFirstFileA((path + "\\*").check_no_null_byte, pointerof(data)) | ||
| if @dir_handle == LibC::INVALID_HANDLE_VALUE | ||
| raise WinError.new("FindFirstFileA") | ||
| end | ||
| elsif LibC._FindNextFileA(@dir_handle, pointerof(data)) == 0 | ||
| error = LibC._GetLastError | ||
| if error == WinError::ERROR_NO_MORE_FILES | ||
| return nil | ||
| else | ||
| raise WinError.new("FindNextFileA", error) | ||
| end | ||
| end | ||
| String.new(data.cFileName.to_slice) | ||
| end | ||
|
|
||
| def rewind | ||
| close | ||
| end | ||
|
|
||
| def close | ||
| if @dir_handle != LibC::INVALID_HANDLE_VALUE | ||
| if LibC._FindClose(@dir_handle) == 0 | ||
| raise WinError.new("FindClose") | ||
| end | ||
| @dir_handle = LibC::INVALID_HANDLE_VALUE | ||
| end | ||
| end | ||
|
|
||
| def self.current : String | ||
| len = LibC._GetCurrentDirectoryA(0, nil) | ||
| if len == 0 | ||
| raise WinError.new("_GetCurrentDirectoryA") | ||
| end | ||
| String.new(len) do |buffer| | ||
| if LibC._GetCurrentDirectoryA(len, buffer) == 0 | ||
| raise WinError.new("_GetCurrentDirectoryA") | ||
| end | ||
| {len - 1, len - 1} # remove \0 at the end | ||
| end | ||
| end | ||
|
|
||
| def self.cd(path : String) | ||
| if LibC._SetCurrentDirectoryA(path.check_no_null_byte) == 0 | ||
| raise WinError.new("Error while changing directory to #{path.inspect}") | ||
| end | ||
| end | ||
|
|
||
| def self.exists?(path : String) : Bool | ||
| atr = LibC._GetFileAttributesA(path.check_no_null_byte) | ||
| if (atr == LibWindows::INVALID_FILE_ATTRIBUTES) | ||
| return false | ||
| end | ||
| return atr & LibC::FILE_ATTRIBUTE_DIRECTORY != 0 | ||
| end | ||
|
|
||
| def self.mkdir(path : String, mode) | ||
| if LibC._CreateDirectoryA(path.check_no_null_byte, nil) == 0 | ||
| raise WinError.new("Unable to create directory '#{path}'") | ||
| end | ||
| end | ||
|
|
||
| def self.rmdir(path : String) | ||
| if LibC._RemoveDirectoryA(path.check_no_null_byte) == 0 | ||
| raise WinError.new("Unable to remove directory '#{path}'") | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not exactly related to this pr, but why does ˋmkdirˋ & ˋrmdirˋ returns ˋ0ˋ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know, but in spec there is
Dir.mkdir(path, 0o700).should eq(0)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably they should be marked as
: Nilfor avoid leaking a useless return valueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll make another PR for it