Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Verifying Current Working Directory: getcwd() and absolute path

by hackdaddy (Hermit)
on Jun 24, 2002 at 18:18 UTC ( [id://176875]=perlquestion: print w/replies, xml ) Need Help??

hackdaddy has asked for the wisdom of the Perl Monks concerning the following question:


I have a program that needs to verify that it has changed directory to a specified directory. My verification uses the Cwd module and the getcwd() function.

However, my verification fails because the customer has used a symbolic link for the path and getcwd() returns the absolute path.

How can I avoid this type of problem when verifying the current working directory?

  • Comment on Verifying Current Working Directory: getcwd() and absolute path

Replies are listed 'Best First'.
Re: Verifying Current Working Directory: getcwd() and absolute path
by jsprat (Curate) on Jun 24, 2002 at 18:57 UTC
    Cwd also exports (on request) a method called abs_path, which will convert a symlink to the real path. You could use this for your comparison, ie:

    use Cwd qw(abs_path getcwd); if (abs_path('/home') eq getcwd) { print "Correct directory\n"; # do whatever needs to be done }

    Of course, substitute your symlinked path for /home in the above

Re: Verifying Current Working Directory: getcwd() and absolute path
by valdez (Monsignor) on Jun 25, 2002 at 13:32 UTC

    If there are symlinks and you followed them, I think that you cannot know how you reached a file; You can test yourself:

    mkdir -p real/subdir ln -s real link

    #!/usr/bin/perl use Cwd; for $dir ('real/subdir', '../link/subdir') { print "changing to $dir\n"; chdir $dir; where(); } exit; sub where { print "pwd : ", `pwd`; print "cwd : ", Cwd::cwd, "\n"; print "getcwd : ", Cwd::getcwd, "\n"; print "fastcwd: ", Cwd::fastcwd, "\n"; }

    This is true for Linux, I don't know for other file systems. Software like Midnight Commander or Bash shell keep track of the followed path to solve this problem.

    Ciao, Valerio

Re: Verifying Current Working Directory: getcwd() and absolute path
by robobunny (Friar) on Jun 24, 2002 at 18:48 UTC
    you could test every element of the path to see if it is a symbolic link, and then check to see where it points. that seems like an awfully lot of work though. i'm not sure why you need to verify. chdir() will return false if it fails.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-24 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found