FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Prevent unlink(...) warning
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Prevent unlink(...) warning [message #176027] Thu, 17 November 2011 15:04 Go to next message
Simon is currently offline  Simon
Messages: 29
Registered: February 2011
Karma: 0
Junior Member
Hi,

I am having some race condition issues on my server when trying to
delete a file.

//
function my_unlink( $filename )
{
// remove the old file
clearstatcache(TRUE, $filename);
if (file_exists($filename))
{
@unlink($filename);
}
else
{
return true;
}

clearstatcache(TRUE, $filename);
return (!file_exists($filename)); // check it was deleted.
}
//

In some cases I get

unlink(/home/xxx/public_html/temp/yyy.zz) [function.unlink]: No such
file or directory

How can I prevent the warning?

Thanks

Simon
Re: Prevent unlink(...) warning [message #176028 is a reply to message #176027] Thu, 17 November 2011 15:18 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Simon wrote:
> Hi,
>
> I am having some race condition issues on my server when trying to
> delete a file.
>

what makes you think its a race condition?

> //
> function my_unlink( $filename )
> {
> // remove the old file
> clearstatcache(TRUE, $filename);
> if (file_exists($filename))
> {
> @unlink($filename);
> }
> else
> {
> return true;
> }
>
> clearstatcache(TRUE, $filename);
> return (!file_exists($filename)); // check it was deleted.
> }
> //
>

Why do you care about the stat cache?

> In some cases I get
>
> unlink(/home/xxx/public_html/temp/yyy.zz) [function.unlink]: No such
> file or directory
>
> How can I prevent the warning?
>
Why prevent it at all?

As long as the file is not there when you are finished - why not just
unlink it anyway?

> Thanks
>
> Simon
>
Re: Prevent unlink(...) warning [message #176029 is a reply to message #176028] Thu, 17 November 2011 17:21 Go to previous messageGo to next message
Simon is currently offline  Simon
Messages: 29
Registered: February 2011
Karma: 0
Junior Member
On 2011/11/17 05:18 PM, The Natural Philosopher wrote:
> Simon wrote:
>> Hi,
>>
>> I am having some race condition issues on my server when trying to
>> delete a file.
>>
>
> what makes you think its a race condition?

A guess really,

If I do:
....
if (file_exists($filename))
{

and followed by
....
@unlink($filename);
}

And that gives me a

unlink(/home/xxx/public_html/temp/yyy.zz) [function.unlink]: No such
file or directory

The something between file_exists(...) and @unlink(...) must have
removed the file.
Either that or one of the two functions is not working as expected.

>
> Why do you care about the stat cache?
>

As per the doc, http://php.net/manual/en/function.clearstatcache.php, to
make sure that file_exists($filename) does return a cached value.

>>
>> How can I prevent the warning?
>>
> Why prevent it at all?
>
> As long as the file is not there when you are finished - why not just
> unlink it anyway?
>

Because the warnings fill my logs and I aim for no warnings at all.

Simon
Re: Prevent unlink(...) warning [message #176030 is a reply to message #176027] Thu, 17 November 2011 17:42 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 17/11/2011 16:04, Simon escribió/wrote:
> I am having some race condition issues on my server when trying to
> delete a file.
>
> //
> function my_unlink( $filename )
> {
> // remove the old file
> clearstatcache(TRUE, $filename);
> if (file_exists($filename))
> {
> @unlink($filename);
> }
> else
> {
> return true;
> }
>
> clearstatcache(TRUE, $filename);
> return (!file_exists($filename)); // check it was deleted.
> }
> //
>
> In some cases I get
>
> unlink(/home/xxx/public_html/temp/yyy.zz) [function.unlink]: No such
> file or directory
>
> How can I prevent the warning?


I can think of three possible approaches:

1. Try out flock() and find out whether it prevent other processes from
removing the file and whether it allows _you_ to remove it.

2. Write a custom error handler and see if you can convert the warning
into ErrorException. If you succeed, you can then try...catch around the
unlink() call.

3. Keep the @ and live with it. I hate the error suppression operator as
much as anyone but there're situations where trying to do without it is
just not worth the effort.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: Prevent unlink(...) warning [message #176031 is a reply to message #176029] Thu, 17 November 2011 18:29 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Simon wrote:
> On 2011/11/17 05:18 PM, The Natural Philosopher wrote:
>> Simon wrote:
>>> Hi,
>>>
>>> I am having some race condition issues on my server when trying to
>>> delete a file.
>>>
>>
>> what makes you think its a race condition?
>
> A guess really,
>
> If I do:
> ...
> if (file_exists($filename))
> {
>
> and followed by
> ...
> @unlink($filename);
> }
>
> And that gives me a
>
> unlink(/home/xxx/public_html/temp/yyy.zz) [function.unlink]: No such
> file or directory
>
> The something between file_exists(...) and @unlink(...) must have
> removed the file.
> Either that or one of the two functions is not working as expected.
>

Hmm..what else CAN be deleting that file (unless you are doing the same
thing twice)?

>>
>> Why do you care about the stat cache?
>>
>
> As per the doc, http://php.net/manual/en/function.clearstatcache.php, to
> make sure that file_exists($filename) does return a cached value.
>
>>>
>>> How can I prevent the warning?
>>>
>> Why prevent it at all?
>>
>> As long as the file is not there when you are finished - why not just
>> unlink it anyway?
>>
>
> Because the warnings fill my logs and I aim for no warnings at all.
>

Well, that's reasonable..;-)
> Simon
Re: Prevent unlink(...) warning [message #176032 is a reply to message #176027] Thu, 17 November 2011 18:54 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 11/17/2011 10:04 AM, Simon wrote:
> Hi,
>
> I am having some race condition issues on my server when trying to
> delete a file.
>
> //
> function my_unlink( $filename )
> {
> // remove the old file
> clearstatcache(TRUE, $filename);
> if (file_exists($filename))
> {
> @unlink($filename);
> }
> else
> {
> return true;
> }
>
> clearstatcache(TRUE, $filename);
> return (!file_exists($filename)); // check it was deleted.
> }
> //
>
> In some cases I get
>
> unlink(/home/xxx/public_html/temp/yyy.zz) [function.unlink]: No such
> file or directory
>
> How can I prevent the warning?
>
> Thanks
>
> Simon
>

In addition to what Álvaro said, if you're doing a lot of this in your
scripts, I would suspect a database may be a better solution.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Embedding HTML Within a PHP Statement
Next Topic: Re: comp.lang.php forum
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 10 14:59:09 GMT 2024

Total time taken to generate the page: 0.02339 seconds