Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
dduan committed Oct 18, 2020
1 parent c028f26 commit 0bc73b6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
48 changes: 28 additions & 20 deletions Sources/Pathos/Windows/Path+Windows.swift
Expand Up @@ -450,30 +450,36 @@ extension Path {
}

public func glob() throws -> [Path] {
// try binaryPath.c { pattern -> [Path] in
// guard let globStarPosition = self.binaryPath.content.firstIndex(of: [42, 42]) else {
simpleGlobImpl()
// }

// func recursiveFNMatch(_ path: Path) throws -> [Path] {
// try path.children(recursive: true)
// .map(\.0)
// .filter {
// $0.binaryPath.c { cPath in
// fnmatch(pattern, cPath, 0) == 0
// }
// }
// }

// return try Path(BinaryString(nulTerminatedStorage: self.binaryPath.content.prefix(globStarPosition) + [0]))
// .simpleGlobImpl()
// .filter { try $0.metadata().fileType.isDirectory }
// .flatMap(recursiveFNMatch)
// }
try binaryPath.c { pattern -> [Path] in
guard let globStarPosition = self.binaryPath.content.firstIndex(of: [42, 42]) else {
return simpleGlobImpl()
}

func recursiveFNMatch(_ path: Path) throws -> [Path] {
try path.children(recursive: true)
.map(\.0)
.filter {
$0.binaryPath.c { cPath in
PathMatchSpecW(cPath, pattern)
}
}
}

return try Path(BinaryString(nulTerminatedStorage: self.binaryPath.content.prefix(globStarPosition) + [42, 0]))
.simpleGlobImpl()
.filter { try $0.metadata().fileType.isDirectory }
.flatMap(recursiveFNMatch)
}
}

func simpleGlobImpl() -> [Path] {
print(self)
var result = [Path]()
if (try? metadata().fileType.isDirectory) == true {
print("self is dir")
result.append(self)
}

var data = WIN32_FIND_DATAW()

func addResultIfNecessary(_ data: inout WIN32_FIND_DATAW) {
Expand All @@ -500,6 +506,7 @@ extension Path {
binaryPath.c { pathCString in
let handle = FindFirstFileW(pathCString, &data)
if handle == INVALID_HANDLE_VALUE {
print(">>>>", GetLastError())
return
}

Expand All @@ -514,6 +521,7 @@ extension Path {
}
}

print(result)
return result
}

Expand Down
2 changes: 2 additions & 0 deletions Tests/PathosTests/GlobTests.swift
Expand Up @@ -47,6 +47,8 @@ final class GlobTests: XCTestCase {
try file.write("")
}

try print(Array(Path(".").children(recursive: true).map(\.0)))

XCTAssertEqual(
Set(try (Path(".") + "x" + "**" + "*.swift").glob()),
[
Expand Down

0 comments on commit 0bc73b6

Please sign in to comment.