Merge pull request #2208 from def-/walkdir
Speed up walkDir significantly
This commit is contained in:
commit
0ff4ed0ff3
2 changed files with 23 additions and 1 deletions
|
|
@ -70,6 +70,16 @@ const
|
||||||
STDIN_FILENO* = 0 ## File number of stdin;
|
STDIN_FILENO* = 0 ## File number of stdin;
|
||||||
STDOUT_FILENO* = 1 ## File number of stdout;
|
STDOUT_FILENO* = 1 ## File number of stdout;
|
||||||
|
|
||||||
|
DT_UNKNOWN* = 0 ## Unknown file type.
|
||||||
|
DT_FIFO* = 1 ## Named pipe, or FIFO.
|
||||||
|
DT_CHR* = 2 ## Character device.
|
||||||
|
DT_DIR* = 4 ## Directory.
|
||||||
|
DT_BLK* = 6 ## Block device.
|
||||||
|
DT_REG* = 8 ## Regular file.
|
||||||
|
DT_LNK* = 10 ## Symbolic link.
|
||||||
|
DT_SOCK* = 12 ## UNIX domain socket.
|
||||||
|
DT_WHT* = 14
|
||||||
|
|
||||||
type
|
type
|
||||||
TDIR* {.importc: "DIR", header: "<dirent.h>",
|
TDIR* {.importc: "DIR", header: "<dirent.h>",
|
||||||
incompleteStruct.} = object
|
incompleteStruct.} = object
|
||||||
|
|
@ -84,6 +94,10 @@ type
|
||||||
Tdirent* {.importc: "struct dirent",
|
Tdirent* {.importc: "struct dirent",
|
||||||
header: "<dirent.h>", final, pure.} = object ## dirent_t struct
|
header: "<dirent.h>", final, pure.} = object ## dirent_t struct
|
||||||
d_ino*: Tino ## File serial number.
|
d_ino*: Tino ## File serial number.
|
||||||
|
d_off*: TOff ## Not an offset. Value that ``telldir()`` would return.
|
||||||
|
d_reclen*: cshort ## Length of this record. (not POSIX)
|
||||||
|
d_type*: int8 ## Type of file; not supported by all filesystem types.
|
||||||
|
## (not POSIX)
|
||||||
d_name*: array [0..255, char] ## Name of entry.
|
d_name*: array [0..255, char] ## Name of entry.
|
||||||
|
|
||||||
Tflock* {.importc: "struct flock", final, pure,
|
Tflock* {.importc: "struct flock", final, pure,
|
||||||
|
|
|
||||||
|
|
@ -1299,8 +1299,16 @@ iterator walkDir*(dir: string): tuple[kind: PathComponent, path: string] {.
|
||||||
if y != "." and y != "..":
|
if y != "." and y != "..":
|
||||||
var s: TStat
|
var s: TStat
|
||||||
y = dir / y
|
y = dir / y
|
||||||
if lstat(y, s) < 0'i32: break
|
|
||||||
var k = pcFile
|
var k = pcFile
|
||||||
|
|
||||||
|
when defined(linux) or defined(macosx) or defined(bsd):
|
||||||
|
if x.d_type != DT_UNKNOWN:
|
||||||
|
if x.d_type == DT_DIR: k = pcDir
|
||||||
|
if x.d_type == DT_LNK: k = succ(k)
|
||||||
|
yield (k, y)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if lstat(y, s) < 0'i32: break
|
||||||
if S_ISDIR(s.st_mode): k = pcDir
|
if S_ISDIR(s.st_mode): k = pcDir
|
||||||
if S_ISLNK(s.st_mode): k = succ(k)
|
if S_ISLNK(s.st_mode): k = succ(k)
|
||||||
yield (k, y)
|
yield (k, y)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue