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

-e file test won't follow symlinks?

by consultant1027 (Initiate)
on Jun 11, 2011 at 00:07 UTC ( #909233=perlquestion: print w/ replies, xml ) Need Help??
consultant1027 has asked for the wisdom of the Perl Monks concerning the following question:

Have a script that checks for file exists using -e. It works great but if I try to test for a file that is accessed via a symbolic link directory, won't work. I even made sure the directory and file have 777 permissions. Not sure if this is a perl limitation or a limitation of Apache/PLESK as essentially the script is running on one website and is trying to check for a file on another website on the server. I can however bring up the file in the browser (its an image) using the symlink in the URL.

Comment on -e file test won't follow symlinks?
Re: -e file test won't follow symlinks?
by Anonymous Monk on Jun 11, 2011 at 01:13 UTC
    chroot is a jail, it keeps a program from changing directories
Re: -e file test won't follow symlinks?
by jethro (Monsignor) on Jun 11, 2011 at 01:27 UTC
    > mkdir a > ln -s a b > touch a/t > perl -e ' print "yes\n" if (-e "a/t");' yes > perl -e ' print "yes\n" if (-e "b/t");' yes

    As you can see, in general it works. Are you sure you have the right path? Web address and filesystem path of any file can be totally different because of apaches ability to rewrite addresses

    Did you check if you can test whether the directory exists? Or its parent directory and so on?

Re: -e file test won't follow symlinks?
by Khen1950fx (Canon) on Jun 12, 2011 at 01:08 UTC
    I think that symlinks under Plesk have different permissions than Unix permissions. As a work-around, I experimented with Setup::File::Symlink. Instead of checking permissions, I checked for status---either 200 or 304 in this case.

    Here I used a dry run. If you want to create a symlink, comment out name and change -dry_run => 1 to 0. It requires 5.10.1+.

    #!/usr/bin/perl use Modern::Perl; use Setup::File::Symlink qw(setup_symlink); my $x = '/root/Desktop/tmp'; my $res = setup_symlink( name => "create(dry run)", symlink => "/y", target => "/$x/x", other_args => {-dry_run => 1}, status => 200, exists => 1, ); print "status: 200\n", if $res->[0] == 200; print "status: 304\n", if $res->[0] == 304;

      I assume you peeked at the test scripts on the usage of setup_symlink(). My bad for not yet providing a Synopsis, but you don't actually need some of the arguments. You only need:

      my $res = setup_symlink( symlink => "/y", target => "/$x/x", );

      And BTW, I think Setup::File::Symlink is a bit of an overkill in this case.

Re: -e file test won't follow symlinks?
by mr_mischief (Monsignor) on Jun 13, 2011 at 10:29 UTC
    Your web server is generally configured to not follow symlinks, or to not follow them unless the owner matches who is trying to follow the link. This is especially true on a shared hosting server.
Re: -e file test won't follow symlinks?
by sedusedan (Monk) on Oct 04, 2012 at 06:56 UTC

    -e follows symlinks, because it does a stat(), not an lstat(). If the symlink target does not exist or is inaccessible, -e will report false even though the symlink itself exists.

    If that is not what you want, you might want to try file_exists() in SHARYANTO::File::Util. Or you can implement the logic itself, by using -l instead of -e.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-10-26 08:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (152 votes), past polls