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

Problem description:
when using "-e" command (see example below) to check if file exists, in some cases getting incorrect answer.
While the file actually exists, the "-e" indicates that the file doesn't exists.

Environment:
The file is located in mounted area in NetApp server.
Perl version 5.10.1.

# Scenario 1, example of the problem: my $is_file_exists = 1; my $db_path = $ST_DB_PATH.$db_name; unless (-e $db_path) { print "The file doesn't exist\n"; $is_file_exists = 0; }
In some cases, the result of Scenario 1 is $is_file_exists=0
while the actually file exists.
# Scenario 2, example of workaround that solves the problem: my $is_file_exists = 1; my $db_path = $ST_DB_PATH.$db_name; # Adding cat command to solve the "-e" problem `cat $db_path > /dev/null`; unless (-e $db_path) { print "The file doesn't exist\n"; $is_file_exists = 0; }
Workaround description:
Before using "-e" command, using system command: "cat <file>".
When calling "-e" command after "cat" the "-e" works as expected.

I use the "cat" as a workaround, I can't use it in the entire code.
Does someone faced such a problem and found solution?
Do you think this problem is related to the Perl or to the native platform or to the NetApp environment?

Replies are listed 'Best First'.
Re: File Existence using "-e" not always working
by no_slogan (Deacon) on Mar 27, 2014 at 15:58 UTC
    Try opening the file for read?

      Exactly.

      Looks like "auto mount" behavior. It is common that checking for a file's existence won't trigger the automatic mounting of the remote file system but that trying to read a file will trigger that.

      So just trying to open the file, as suggested, will likely also trigger it.

      my $exists = do { open my $fh, '<', $db_path };

      - tye        

Re: File Existence using "-e" not always working
by Anonymous Monk on Mar 27, 2014 at 16:57 UTC

    Do you think this problem is related to the Perl

    No, as perl just calls the underlying OS call ... to stat ... and this code in perl has been working for decades

Re: File Existence using "-e" not always working
by jellisii2 (Hermit) on Mar 27, 2014 at 17:34 UTC
    Check the size instead of if it exists?
Re: File Existence using "-e" not always working
by Anonymous Monk on Mar 27, 2014 at 17:08 UTC