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 normalizePath and tests #6587

Merged
merged 1 commit into from
Jul 7, 2018
Merged

Conversation

FedericoCeratto
Copy link
Member

Related to #6486

@yglukhov
Copy link
Member

Thats cool, but maybe its better to move normalizePath to ospaths.nim? Also please have a look at https://github.com/yglukhov/nimx/blob/master/nimx/pathutils.nim#L33. That is an inplace version to avoid allocations. Immutable version might be called normalizedPath and reuse the inplace version. Also my impl may optionally respect native path separators which proved to be quite useful.

@FedericoCeratto FedericoCeratto force-pushed the normalizePath branch 2 times, most recently from 8b2c961 to 14492f3 Compare November 19, 2017 15:01
@FedericoCeratto FedericoCeratto force-pushed the normalizePath branch 2 times, most recently from 316702c to 390ab7d Compare November 23, 2017 13:48
@jxy
Copy link
Contributor

jxy commented Dec 21, 2017

Any news on this? I would like these to be in the stdlib somewhere.

@FedericoCeratto FedericoCeratto force-pushed the normalizePath branch 2 times, most recently from 78383a6 to f3e0a2a Compare April 15, 2018 18:53
@kaushalmodi
Copy link
Contributor

Just recently, I wished such a function existed that converted relative paths like ../foo/ to absolute paths.

Can this please be added?

@dom96
Copy link
Contributor

dom96 commented May 31, 2018

This is great, but it sucks that os in general doesn't offer in-place versions of procs. Providing it for this is a bit awkward.

@FedericoCeratto
Copy link
Member Author

Rebased. Feel free to merge it as it is or modify the PR.

@dom96
Copy link
Contributor

dom96 commented Jun 1, 2018

Thanks for rebasing, but tests are failing.

@dom96
Copy link
Contributor

dom96 commented Jul 3, 2018

The tos test is failing so your new tests or implementation must be wrong.

@FedericoCeratto FedericoCeratto force-pushed the normalizePath branch 5 times, most recently from e55e30f to b237bd4 Compare July 5, 2018 11:13
@timotheecour
Copy link
Member

just sent out #8213 add os.absolutePath; fixes #8174
it allows passing in a 2nd (optional) argument (root), and works correctly with cases where path is already absolute, ignoring the root argument in that case

@Araq
Copy link
Member

Araq commented Jul 6, 2018

Is absolutePath the same as result = if isAbsolute(x): x else: getCurrentDir() / x ?

Copy link
Contributor

@dom96 dom96 left a 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>`_.
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Contributor

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.

doAssert normalizedPath("\\a\\b\\..\\..\\..\\foo", '\\') == "\\foo"

when defined(Linux) or defined(osx):
Copy link
Contributor

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: [].} =
Copy link
Contributor

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("/../../../") == "/"
Copy link
Contributor

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.

Copy link
Member Author

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

Copy link
Member

@timotheecour timotheecour Jul 7, 2018

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?

@dom96
Copy link
Contributor

dom96 commented Jul 6, 2018

I do also prefer the absolutePath implementation that @timotheecour made in #8213. (But I do still believe it should be name expandPath)

@FedericoCeratto
Copy link
Member Author

@Araq absolutePath is meant to do that (see implementation) and it's also normalizing the output by trimming away a trailing separator.

@FedericoCeratto
Copy link
Member Author

@dom96 "expansion" usually refers to expanding "~" into the home directory and so on.
absolutePath is meant to go from relative to absolute path, hence the name.

@kaushalmodi
Copy link
Contributor

@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.

@FedericoCeratto
Copy link
Member Author

See expandFilename() discussion in #6486 . Unlike the proposed absolutePath, expandFilename() is acting on existing files and directories only.

@FedericoCeratto FedericoCeratto force-pushed the normalizePath branch 2 times, most recently from 24b9eec to d7cefc8 Compare July 6, 2018 18:48
@dom96
Copy link
Contributor

dom96 commented Jul 6, 2018

Okay, absolutePath it is. Can anyone point me to where this normalize vs. normalized convention is used? I can't find it in the stdlib and I want to document it.

@FedericoCeratto FedericoCeratto changed the title Add normalizePath, absolutePath and tests Add normalizePath and tests Jul 6, 2018
@dom96 dom96 merged commit 53ce58f into nim-lang:devel Jul 7, 2018
@@ -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: [].} =
Copy link
Member

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

Copy link
Contributor

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.

Copy link
Member

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)

Copy link
Member

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

@FedericoCeratto FedericoCeratto deleted the normalizePath branch July 7, 2018 11:18
@timotheecour timotheecour added the OS (stdlib) Related to OS modules in standard library label Oct 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS (stdlib) Related to OS modules in standard library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants