Skip to content

Commit

Permalink
fix FilePath.contains
Browse files Browse the repository at this point in the history
  • Loading branch information
joneugster committed Jan 30, 2025
1 parent b512f4a commit 65e830c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cache/IO.lean
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def getPackageDir (sourceFile : FilePath) : CacheM FilePath := do
let packageDir? := sp.find? (·.contains sourceFile)
match packageDir? with
| some dir => return dir
| none => throw <| IO.userError s!"Unknown package directory for {sourceFile}\n{sp.map (·.normalize.toString)}"
| none => throw <| IO.userError s!"Unknown package directory for {sourceFile}\nsearch path: {sp}"

/-- Runs a terminal command and retrieves its output, passing the lines to `processLine` -/
partial def runCurlStreaming (args : Array String) (init : α)
Expand Down
18 changes: 12 additions & 6 deletions Cache/Lean.lean
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,27 @@ namespace System.FilePath
Returns true if `target` lives inside `path`.
Does not check if the two actually exist.
The paths can contain arbitrary amounts of "." and "..",
but if one of them starts with ".." the function will always return `False`!
The paths can contain arbitrary amounts of ".", "" or "..".
However, if the paths do not contain the same amount of leading "..", it
will return `False` as the real system path is unknown to the function.
For example, `contains (".." / "myFolder") ("myFolder" / "..")` will always return `false`
even though it might be true depending on which local working directory on is in
(which `contains` does'nt know about since it's not in `IO`.)
-/
def contains (path target : FilePath) : Bool :=
go path.components target.components
where
go : List String → List String → Bool
-- must not start with ".."
| ".." :: _, _ => false
| _, ".." :: _ => false
-- ignore "." or ""
-- ignore leading "." or ""
| "." :: path, target => go path target
| path, "." :: target => go path target
| "" :: path, target => go path target
| path, "" :: target => go path target
-- must not start with ".."
| ".." :: path, ".." :: target => go path target
| ".." :: _, _ => false
| _, ".." :: _ => false
-- cancel entry with following ".."
| _ :: ".." :: path, target => go path target
| path, _ :: ".." :: target => go path target
Expand Down

0 comments on commit 65e830c

Please sign in to comment.