-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 normalizePath and tests #6587
Conversation
635017b
to
b3165fb
Compare
Thats cool, but maybe its better to move |
8b2c961
to
14492f3
Compare
316702c
to
390ab7d
Compare
Any news on this? I would like these to be in the stdlib somewhere. |
78383a6
to
f3e0a2a
Compare
Just recently, I wished such a function existed that converted relative paths like Can this please be added? |
This is great, but it sucks that |
f3e0a2a
to
6aa7a6a
Compare
Rebased. Feel free to merge it as it is or modify the PR. |
Thanks for rebasing, but tests are failing. |
6aa7a6a
to
0523159
Compare
The |
e55e30f
to
b237bd4
Compare
Is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a general note. These functions should all be moved to ospaths
.
lib/pure/os.nim
Outdated
## raises OSError in case of an error. Follows symlinks. | ||
## | ||
## To create absolute paths from any path (existing or not) see | ||
## `<#absolutePath>`_. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think expandPath
would make more sense as a name here. absolutePath
makes me wonder how it differs from normalizePath
.
lib/pure/os.nim
Outdated
## | ||
## Warning: URL-encoded and Unicode attempts at directory traversal are not detected. | ||
## Triple dot is not handled. | ||
let is_abs = path[0] == separator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not a big deal, but convention in stdlib is camelCase
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, on Windows this is wrong, no? You should use the isAbsolute
procedure.
tests/stdlib/tos.nim
Outdated
doAssert normalizedPath("\\a\\b\\..\\..\\..\\foo", '\\') == "\\foo" | ||
|
||
when defined(Linux) or defined(osx): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's cheating. You can't add something to os
and only test it on Linux/Mac.
lib/pure/os.nim
Outdated
else: | ||
path = "." | ||
|
||
proc normalizedPath*(path: string, separator=DirSep): string {.rtl, extern: "nos$1", tags: [].} = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remember where in the stdlib currently this convention is in use? It's about time this is documented but I can't find any other module that uses it.
doAssert normalizedPath("/../..") == "/" | ||
doAssert normalizedPath("/../../") == "/" | ||
doAssert normalizedPath("/../../../") == "/" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it would make more sense to raise an error for these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the point of normalization. If you are looking for a way to detect and prevent directory traversal I think that should go into joinPath
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @dom96 , throwing seems much saner behavior.
Anecdotically, D's version also doesn't throw but that behavior isn't documented (just filed https://issues.dlang.org/show_bug.cgi?id=19069 to see if good arguments come up)
EDIT likewise with python:
os.path.normpath('/../..')
Out[6]: '/'
even though i don't see that being documented...
what's a use case where silently accepting is preferable to throwing?
I do also prefer the |
@Araq absolutePath is meant to do that (see implementation) and it's also normalizing the output by trimming away a trailing separator. |
@dom96 "expansion" usually refers to expanding "~" into the home directory and so on. |
@dom96 I agree on the "absolutePath" naming. I don't see any confusion when one sees this name. I infact ended up on this issue thread when I couldn't find anything named "absolutePath" in the stdlib. |
See expandFilename() discussion in #6486 . Unlike the proposed absolutePath, expandFilename() is acting on existing files and directories only. |
24b9eec
to
d7cefc8
Compare
Okay, |
d7cefc8
to
d65429d
Compare
@@ -338,6 +338,47 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1", | |||
result = $r | |||
c_free(cast[pointer](r)) | |||
|
|||
proc normalizePath*(path: var string) {.rtl, extern: "nos$1", tags: [].} = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be in ospaths, no? nothing in normalizePath
accesses file system
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh yeah, I forgot about this. But @FedericoCeratto has waited long enough with this PR so we can fix this in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of course; I much prefer faster PR turnaround than waiting forever for PR's to be so-called "perfect" (this slow turnaround really hurts D ecosystem IMO, Nim seems better in that regard)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
=> kept track of that in #8177 meta-issue so we don't forget
Related to #6486