about summary refs log tree commit diff
path: root/users/sterni/blërg/posix-path.bqn
blob: 07c332d453871efa8f5f223a6f739f1800189db8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# SPDX-FileCopyrightText: Copyright © 2026 sterni
# SPDX-License-Identifier: GPL-3.0-only
#
# Incomplete set of utilities for dealing with POSIX paths.
#
# A word of warning: Path handling is tricky to get right, even with the best of
# libraries. This one isn't battle tested yet and only used to deal with trusted
# paths.
#
# TODO(sterni): POSIX paths are bytestrings

⟨SplitChar⟩ ← •Import "string.bqn"

# TODO(sterni): replace with •file.Parts when available
SplitExt ⇐ {2↑𝕩⊔˜∨` ∧˝((∧´¬) ∨` ⊢)⌾⊏ (∊⌾⌽∧⊢)˘ "/."=⌜𝕩}

ParsePath ⇐ {
  "/"≡1↑𝕩? ⟨"/"⟩∾𝕊 '/' ((∨`∘≠)/⊢) 𝕩;
  # TODO(sterni): SplitChar which only splits once for consecutive chars
  0 (≠⟜(≠¨)/⊢) '/'⊸SplitChar 𝕩
}

RenderPath ⇐ ""⊸(•file.At´)

# does not canonicalize, but checks that the input is already canonical
# note that (e.g. due to symlinks) it may be useful to accept paths that aren't
ParseCanonicalPath ⇐ {
  ps ← ParsePath 𝕩
  ("ParseCanonicalPath: "∾𝕩∾" may not contain . or ..")!¬∨´⥊".."‿"."≡⌜ps
  ps
}

IsRelative ⇐ '/'⊸≠∘⊑∘⊑