Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filesystem func to transform a path to a URI #55454

Merged
merged 1 commit into from
Sep 24, 2024
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
56 changes: 56 additions & 0 deletions base/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,59 @@ relpath(path::AbstractString, startpath::AbstractString) =
for f in (:isdirpath, :splitdir, :splitdrive, :splitext, :normpath, :abspath)
@eval $f(path::AbstractString) = $f(String(path))
end

"""
uripath(path::AbstractString)

Encode `path` as a URI as per [RFC8089: The "file" URI
Scheme](https://www.rfc-editor.org/rfc/rfc8089), [RFC3986: Uniform Resource
Identifier (URI): Generic Syntax](https://www.rfc-editor.org/rfc/rfc3986), and
the [Freedesktop File URI spec](https://www.freedesktop.org/wiki/Specifications/file-uri-spec/).

## Examples

```julia-repl
julia> uripath("/home/user/example file.jl") # On a unix machine
"file://<hostname>/home/user/example%20file.jl"

juila> uripath("C:\\Users\\user\\example file.jl") # On a windows machine
"file:///C:/Users/user/example%20file.jl"
```
"""
tecosaur marked this conversation as resolved.
Show resolved Hide resolved
function uripath end

@static if Sys.iswindows()
function uripath(path::String)
percent_escape(s) = # RFC3986 Section 2.1
'%' * join(map(b -> uppercase(string(b, base=16)), codeunits(s)), '%')
encode_uri_component(s) = # RFC3986 Section 2.3
replace(s, r"[^A-Za-z0-9\-_.~/]+" => percent_escape)
path = abspath(path)
if startswith(path, "\\\\") # UNC path, RFC8089 Appendix E.3
unixpath = join(eachsplit(path, path_separator_re, keepempty=false), '/')
string("file://", encode_uri_component(unixpath)) # RFC8089 Section 2
else
drive, localpath = splitdrive(path) # Assuming that non-UNC absolute paths on Windows always have a drive component
unixpath = join(eachsplit(localpath, path_separator_re, keepempty=false), '/')
encdrive = replace(encode_uri_component(drive), "%3A" => ':', "%7C" => '|') # RFC8089 Appendices D.2, E.2.1, and E.2.2
string("file:///", encdrive, '/', encode_uri_component(unixpath)) # RFC8089 Section 2
end
end
else
function uripath(path::String)
percent_escape(s) = # RFC3986 Section 2.1
'%' * join(map(b -> uppercase(string(b, base=16)), codeunits(s)), '%')
encode_uri_component(s) = # RFC3986 Section 2.3
replace(s, r"[^A-Za-z0-9\-_.~/]+" => percent_escape)
localpath = join(eachsplit(abspath(path), path_separator_re, keepempty=false), '/')
host = if ispath("/proc/sys/fs/binfmt_misc/WSLInterop") # WSL sigil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xref #36425

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's a relevant xref. Checking for /proc/sys/fs/binfmt_misc/WSLInterop looks like a pretty solid way of checking for WSL though, but from the page I linked it seems like it may have been introduced after the issue that's mentioned in that PR as the relevant reference.

distro = get(ENV, "WSL_DISTRO_NAME", "") # See <https://patrickwu.space/wslconf/>
"wsl\$/$distro" # See <https://github.com/microsoft/terminal/pull/14993> and <https://learn.microsoft.com/en-us/windows/wsl/filesystems>
else
gethostname() # Freedesktop File URI Spec, Hostnames section
end
string("file://", encode_uri_component(host), '/', encode_uri_component(localpath)) # RFC8089 Section 2
end
end

uripath(path::AbstractString) = uripath(String(path))
13 changes: 13 additions & 0 deletions test/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,19 @@
test_relpath()
end

@testset "uripath" begin
host = if Sys.iswindows() "" else gethostname() end
sysdrive, uridrive = if Sys.iswindows() "C:\\", "C:/" else "/", "" end
@test Base.Filesystem.uripath("$(sysdrive)some$(sep)file.txt") == "file://$host/$(uridrive)some/file.txt"
@test Base.Filesystem.uripath("$(sysdrive)another$(sep)$(sep)folder$(sep)file.md") == "file://$host/$(uridrive)another/folder/file.md"
@test Base.Filesystem.uripath("$(sysdrive)some file with ^odd% chars") == "file://$host/$(uridrive)some%20file%20with%20%5Eodd%25%20chars"
@test Base.Filesystem.uripath("$(sysdrive)weird chars like @#&()[]{}") == "file://$host/$(uridrive)weird%20chars%20like%20%40%23%26%28%29%5B%5D%7B%7D"
@test Base.Filesystem.uripath("$sysdrive") == "file://$host/$uridrive"
@test Base.Filesystem.uripath(".") == Base.Filesystem.uripath(pwd())
@test Base.Filesystem.uripath("$(sysdrive)unicode$(sep)Δεδομένα") == "file://$host/$(uridrive)unicode/%CE%94%CE%B5%CE%B4%CE%BF%CE%BC%CE%AD%CE%BD%CE%B1"
@test Base.Filesystem.uripath("$(sysdrive)unicode$(sep)🧮🐛🔨") == "file://$host/$(uridrive)unicode/%F0%9F%A7%AE%F0%9F%90%9B%F0%9F%94%A8"
end

if Sys.iswindows()
@testset "issue #23646" begin
@test lowercase(relpath("E:\\a\\b", "C:\\c")) == "e:\\a\\b"
Expand Down