Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/crystal/system/dir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ module Crystal::System::Dir
# In particular we only care about the name and whether its
# a directory or not to improve the performance of Dir.glob
# by avoid having to call File.info on every directory entry.
# If dir is nil, the type is unknown.
# In the future we might change Dir's API to expose these entries
# with more info but right now it's not necessary.
struct Entry
getter name
getter? dir

def initialize(@name : String, @dir : Bool)
def initialize(@name : String, @dir : Bool?)
end
end

Expand Down
7 changes: 6 additions & 1 deletion src/crystal/system/unix/dir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ module Crystal::System::Dir
Errno.value = Errno::NONE
if entry = LibC.readdir(dir)
name = String.new(entry.value.d_name.to_unsafe)
dir = entry.value.d_type == LibC::DT_DIR

dir = case entry.value.d_type
when LibC::DT_DIR then true
when LibC::DT_UNKNOWN then nil
else false
end
Entry.new(name, dir)
elsif Errno.value != Errno::NONE
raise ::File::Error.from_errno("Error reading directory entries", file: path)
Expand Down
23 changes: 16 additions & 7 deletions src/dir/glob.cr
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ class Dir
fullpath = Path[path].join("").to_s
end

if dir_entry
yield fullpath if dir_entry.dir?
else
yield fullpath if dir?(fullpath)
if dir_entry && !dir_entry.dir?.nil?
yield fullpath
elsif dir?(fullpath)
yield fullpath
end
in EntryMatch
return if sequence[pos + 1]?.is_a?(RecursiveDirectories)
Expand All @@ -187,8 +187,12 @@ class Dir

each_child(path) do |entry|
if cmd.matches?(entry.name)
if entry.dir?
fullpath = join(path, entry.name)
is_dir = entry.dir?
fullpath = join(path, entry.name)
if is_dir.nil?
is_dir = dir?(fullpath)
end
if is_dir
path_stack << {next_pos, fullpath, entry}
end
end
Expand Down Expand Up @@ -248,7 +252,12 @@ class Dir
yield fullpath if next_cmd.matches?(entry.name)
end

if entry.dir?
is_dir = entry.dir?
if is_dir.nil?
is_dir = dir?(fullpath)
end

if is_dir
path_stack << {next_pos, fullpath, entry}

dir_path_stack.push fullpath
Expand Down
3 changes: 2 additions & 1 deletion src/lib_c/aarch64-linux-gnu/c/dirent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require "./sys/types"
lib LibC
type DIR = Void

DT_DIR = 4
DT_UNKNOWN = 0
DT_DIR = 4

struct Dirent
d_ino : InoT
Expand Down
3 changes: 2 additions & 1 deletion src/lib_c/aarch64-linux-musl/c/dirent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require "./sys/types"
lib LibC
type DIR = Void

DT_DIR = 4
DT_UNKNOWN = 0
DT_DIR = 4

struct Dirent
d_ino : InoT
Expand Down
3 changes: 2 additions & 1 deletion src/lib_c/arm-linux-gnueabihf/c/dirent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require "./sys/types"
lib LibC
type DIR = Void

DT_DIR = 4
DT_UNKNOWN = 0
DT_DIR = 4

struct Dirent
d_ino : InoT
Expand Down
3 changes: 2 additions & 1 deletion src/lib_c/i386-linux-gnu/c/dirent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require "./sys/types"
lib LibC
type DIR = Void

DT_DIR = 4
DT_UNKNOWN = 0
DT_DIR = 4

struct Dirent
d_ino : InoT
Expand Down
3 changes: 2 additions & 1 deletion src/lib_c/i386-linux-musl/c/dirent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require "./sys/types"
lib LibC
type DIR = Void

DT_DIR = 4
DT_UNKNOWN = 0
DT_DIR = 4

struct Dirent
d_ino : InoT
Expand Down
3 changes: 2 additions & 1 deletion src/lib_c/x86_64-darwin/c/dirent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require "./sys/types"
lib LibC
type DIR = Void

DT_DIR = 4
DT_UNKNOWN = 0
DT_DIR = 4

struct Dirent
d_ino : InoT
Expand Down
3 changes: 2 additions & 1 deletion src/lib_c/x86_64-dragonfly/c/dirent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require "./sys/types"
lib LibC
type DIR = Void

DT_DIR = 4
DT_UNKNOWN = 0
DT_DIR = 4

struct Dirent
d_fileno : InoT
Expand Down
3 changes: 2 additions & 1 deletion src/lib_c/x86_64-freebsd/c/dirent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require "./sys/types"
lib LibC
type DIR = Void

DT_DIR = 4
DT_UNKNOWN = 0
DT_DIR = 4

struct Dirent
{% if flag?(:freebsd11) %}
Expand Down
3 changes: 2 additions & 1 deletion src/lib_c/x86_64-linux-gnu/c/dirent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require "./sys/types"
lib LibC
type DIR = Void

DT_DIR = 4
DT_UNKNOWN = 0
DT_DIR = 4

struct Dirent
d_ino : InoT
Expand Down
3 changes: 2 additions & 1 deletion src/lib_c/x86_64-linux-musl/c/dirent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require "./sys/types"
lib LibC
type DIR = Void

DT_DIR = 4
DT_UNKNOWN = 0
DT_DIR = 4

struct Dirent
d_ino : InoT
Expand Down
3 changes: 2 additions & 1 deletion src/lib_c/x86_64-netbsd/c/dirent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ lib LibC
dd_lock : Void*
end

DT_DIR = 4
DT_UNKNOWN = 0
DT_DIR = 4

struct Dirent
d_fileno : InoT
Expand Down
3 changes: 2 additions & 1 deletion src/lib_c/x86_64-openbsd/c/dirent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require "./sys/types"
lib LibC
type DIR = Void

DT_DIR = 4
DT_UNKNOWN = 0
DT_DIR = 4

struct Dirent
d_fileno : InoT
Expand Down