r.v
r.v

Reputation: 4877

os.path.normpath and symbolic links

The documentation of os.path.normpath reads the following:

This string manipulation may change the meaning of a path that contains symbolic links.

What cases and examples are applicable here where the meaning would change? Where would the application of this function be unsafe and what are the workarounds?

Upvotes: 4

Views: 1167

Answers (1)

Tom Dalton
Tom Dalton

Reputation: 6190

Disclaimer: I am not 100% sure on this and don't have access to a *nix system right now to check. You'll probably want to check, or wait till commenters tell me I'm right or wrong ;-)

That said, here's my answer:

In the given example A/foo/../B -> A/B. If there are no symlinks, everything is fine.

Using the same example, lets say:

  • We are currently in the root directory /
  • A is in the root directory (/A)
  • A/foo is a symlink to the directory /X/Y
  • /X/B is a directory

So:

  1. A/foo/../B means "the directory B, which is in the parent directory of A/foo".
  2. A/foo is a symlink to /X/Y, so "the directory B, which is in the parent directory of A/foo" really means "the directory B, which is in the parent directory of /X/Y"
  3. The parent directory of /X/Y is /X, so "the directory B, which is in the parent directory of /X/Y" really means "the directory B, which is in /X".
  4. So A/foo/../B actually refers to /X/B.

In this case the normalisation of A/foo/../B to /A/B is incorrect, it should normalise to /X/B.

Upvotes: 3

Related Questions