Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Surprising behavior of Cwd module on Unix with symlinks

by ELISHEVA (Prior)
on Feb 08, 2011 at 06:15 UTC ( [id://886890]=note: print w/replies, xml ) Need Help??


in reply to Surprising behavior of Cwd module on Unix with symlinks

The most portable solution would be to avoid the use of relative paths (and pwd/cwd) altogether. If you really, truly need to go forward with a particular path, you can avoid the "auto-resolving link" problem altogether by constructing the fully qualified path and using that in your program.

In general, littering your code with relative paths and dependencies on the current working directory is not a good idea. There are several problems that can result:

  • The current working directory might not be what you think it is, not just because the real path is substituted for the symbolic path, but also because cd can fail if you try to switch to a directory for which your effective user (EUID) doesn't have permission.
  • In some cases, particularly with mounted drives, you can get stuck in no-mans land. If you are going to use cwd, you should avoid fastcwd for that reason: see http://rt.cpan.org/Public/Bug/Display.html?id=13851 (ignore the fact that this is a bug report - the discussion about it is the interesting part as regards portability issues)
  • Your code is unlikely to work in taint mode which does not like relative paths.
  • And in general, your code will not be portable to any web interface because the "current" directory in that context is essentially undefined - it could be anything on the face of the planet (well, web server).

See File::Spec for a portable way to attach relative paths to a fully qualified starting path.

About the only situation where I can see a genuine use for cwd is (a) start-up situations (b) incoming information (but here lie security risks which is why taint mode doesn't let you use them) and (c) an occasional piece of software that actually requires itself to be run with a particular directory or kind of directory as "current". Most applications do accept fully qualified paths even if the documentation examples only illustrate relative path parameters for convenience

In start-up/incoming information situations you may want to find certain files based on the starting directory of a user. However, in my own code, I immediately convert that to a fully qualified path and from there on in use fully qualified paths in the code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://886890]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found