_ check

G

George Mpouras

first I check if a file exists ( -f $file ) and then doing several tests
using the _ for being fast.

I have serious problem because the _ checks e.g -z _ sometimes is giving
wrong results. the code is multi thread . What is going wrong ?
 
G

George Mpouras

Στις 20/3/2014 23:12, ο/η Jürgen Exner έγÏαψε:
^^^^^^^^^^^^

Are you sure you are not running into race conditions?

jue

do not know, I am bunging my head with this. It is an apache mod perl.
Maybe it have to do with apache cache.
I do not know if _ is saying always the truth. I mean if between the -f
and _ an other thread ask for -f what will happen
 
R

Rainer Weikusat

George Mpouras said:
Στις 20/3/2014 23:12, ο/η Jürgen Exner έγÏαψε:

do not know, I am bunging my head with this. It is an apache mod
perl. Maybe it have to do with apache cache.
I do not know if _ is saying always the truth. I mean if between the
-f
and _ an other thread ask for -f what will happen

As a somewhat simplified statement: Perl doesn't really support
multi-threading, all it can do is have multiple perl processes :):=
interpreters) run in a shared address space (thus, elegantly combining
all disadvantages of either model with each other in order to provide
none of the advantages). For your case, this means that each thread has
its own 'stat buffer'.

What do you mean by "-z _ sometimes is giving wrong results"? All uses
of stat are prone to TOCTOU-races, meaning, -z file being true by the
time the test was made doesn't even imply that it's still true by the
time your code sees the result.
 
G

gamo

El 20/03/14 21:43, George Mpouras escribió:
first I check if a file exists ( -f $file ) and then doing several tests
using the _ for being fast.

I have serious problem because the _ checks e.g -z _ sometimes is giving
wrong results. the code is multi thread . What is going wrong ?


The special filehandle '_' is different from using a normal variable.
So, if something is not working try a normal variable.

If that don't works, try something like this

$can_perhaps_read = -r "file"; # use the mode bits
{
use filetest 'access'; # intuit harder
$can_really_read = -r "file";
}
$can_perhaps_read = -r "file"; # use the mode bits again

Which is copyed from 'man filetest'
 
R

Rainer Weikusat

gamo said:
El 20/03/14 21:43, George Mpouras escribió:


The special filehandle '_' is different from using a normal variable.
So, if something is not working try a normal variable.

This depends on your definition of 'normal variable': perl keeps the
result of the last stat call in a 'normal variable' named PL_statbuf
(actually, this is a macro expanding to the 'normal variable' but that's
not really relevant here) and doing a filetest on _ examines the struct
stat available as/ via PL_statbuf.

But this doesn't really matter: The state of 'filesystem objects' can
generally change at any time and this means the result of each stat call
is potentially outdated as soon as it becomes available. And the same is
true about checks done via access(2), as filetest does: No matter what
the call returned, it is potentially not correct anymore by the time the
result is available.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top