Why would chmod( ... ) fail with "No such file or directory" [message #173159] |
Fri, 25 March 2011 09:35 |
Simon
Messages: 29 Registered: February 2011
Karma: 0
|
Junior Member |
|
|
Hi,
I have some code that creates a temp file, writes to it and then calls
chmod(...) to make sure that the file has 0644 permissions.
// -------------
// pseudo code
//
$tmpfname = 'xyx'; // come from another function.
if (!($fd = @fopen($tmpfname, 'wb')))
{
return false;
}
fwrite($fd, $data ); // the data comes from another function has well.
no warnings given.
fclose($fd); // no warnings given.
chmod( $tmpfname, 0644 ); <!-- error here with "No such file or directory"
// -------------
The issue I have is that it works _sometimes_, in fact, 99% of the time
it works.
But from time to time the log reports that the file does not exist.
But how it is possible, if the file opened successfully with fopen( ... )
I wonder if the problem is partly because I am using @fopen(...) rather
than fopen(...), (I don't need to suppress the warnings).
But in that case fwrite(...) and fclose(...) would also report
errors/warnings.
Any suggestions as to why chmod(...), (and a subsequent rename(...) )
would sometimes not work.
Thanks
Simon
|
|
|
Re: Why would chmod( ... ) fail with "No such file or directory" [message #173160 is a reply to message #173159] |
Fri, 25 March 2011 10:31 |
alvaro.NOSPAMTHANX
Messages: 277 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
El 25/03/2011 10:35, Simon escribió/wrote:
> Hi,
>
> I have some code that creates a temp file, writes to it and then calls
> chmod(...) to make sure that the file has 0644 permissions.
>
> // -------------
> // pseudo code
> //
> $tmpfname = 'xyx'; // come from another function.
>
> if (!($fd = @fopen($tmpfname, 'wb')))
> {
> return false;
> }
>
> fwrite($fd, $data ); // the data comes from another function has well.
> no warnings given.
fwrite() returns FALSE if there was an error, you don't need to rely on
getting warnings.
> fclose($fd); // no warnings given.
Same with fclose().
> chmod( $tmpfname, 0644 ); <!-- error here with "No such file or directory"
> // -------------
>
> The issue I have is that it works _sometimes_, in fact, 99% of the time
> it works.
> But from time to time the log reports that the file does not exist.
>
> But how it is possible, if the file opened successfully with fopen( ... )
Perhaps that $tmpfname string that comes from another function is not
always unique. If the file was created by another process, you may not
be its owner thus you don't have permission to change permissions, or
the other process may have removed it while you were writing it. There
are a number of tools that can prevent access to a file for security
reasons (from PHP's safe mode to SELInux) and you don't always get an
accurate error message.
--
-- 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: Why would chmod( ... ) fail with "No such file or directory" [message #173161 is a reply to message #173160] |
Fri, 25 March 2011 12:37 |
Simon
Messages: 29 Registered: February 2011
Karma: 0
|
Junior Member |
|
|
On 3/25/2011 12:31 PM, "Álvaro G. Vicario" wrote:
> El 25/03/2011 10:35, Simon escribió/wrote:
>> Hi,
>>
>> I have some code that creates a temp file, writes to it and then calls
>> chmod(...) to make sure that the file has 0644 permissions.
>>
>> // -------------
>> // pseudo code
>> //
>> $tmpfname = 'xyx'; // come from another function.
>>
>> if (!($fd = @fopen($tmpfname, 'wb')))
>> {
>> return false;
>> }
>>
>> fwrite($fd, $data ); // the data comes from another function has well.
>> no warnings given.
>
> fwrite() returns FALSE if there was an error, you don't need to rely on
> getting warnings.
>
>> fclose($fd); // no warnings given.
>
> Same with fclose().
>
>> chmod( $tmpfname, 0644 ); <!-- error here with "No such file or
>> directory"
>> // -------------
>>
>> The issue I have is that it works _sometimes_, in fact, 99% of the time
>> it works.
>> But from time to time the log reports that the file does not exist.
>>
>> But how it is possible, if the file opened successfully with fopen( ... )
>
> Perhaps that $tmpfname string that comes from another function is not
> always unique. If the file was created by another process, you may not
> be its owner thus you don't have permission to change permissions, or
> the other process may have removed it while you were writing it. There
> are a number of tools that can prevent access to a file for security
> reasons (from PHP's safe mode to SELInux) and you don't always get an
> accurate error message.
>
It make a lot of sense, and yes, it is very possible that the name is
not unique, but I wonder how I could prevent the error/warning message
in the first place.
I cannot add a file_exists(...) just before chmod(...) because between
the call to file_exists(...) and chmod(...) I could loose ownership of
the file.
Maybe I could/should use a temp name, open that temp file, write to it,
close it, chmod(...) it and then just rename it.
That might be a little better, maybe.
Simon
|
|
|
Re: Why would chmod( ... ) fail with "No such file or directory" [message #173162 is a reply to message #173161] |
Fri, 25 March 2011 12:52 |
alvaro.NOSPAMTHANX
Messages: 277 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
El 25/03/2011 13:37, Simon escribió/wrote:
> On 3/25/2011 12:31 PM, "Álvaro G. Vicario" wrote:
>> El 25/03/2011 10:35, Simon escribió/wrote:
>>> Hi,
>>>
>>> I have some code that creates a temp file, writes to it and then calls
>>> chmod(...) to make sure that the file has 0644 permissions.
>>>
>>> // -------------
>>> // pseudo code
>>> //
>>> $tmpfname = 'xyx'; // come from another function.
>>>
>>> if (!($fd = @fopen($tmpfname, 'wb')))
>>> {
>>> return false;
>>> }
>>>
>>> fwrite($fd, $data ); // the data comes from another function has well.
>>> no warnings given.
>>
>> fwrite() returns FALSE if there was an error, you don't need to rely on
>> getting warnings.
>>
>>> fclose($fd); // no warnings given.
>>
>> Same with fclose().
>>
>>> chmod( $tmpfname, 0644 ); <!-- error here with "No such file or
>>> directory"
>>> // -------------
>>>
>>> The issue I have is that it works _sometimes_, in fact, 99% of the time
>>> it works.
>>> But from time to time the log reports that the file does not exist.
>>>
>>> But how it is possible, if the file opened successfully with fopen(
>>> ... )
>>
>> Perhaps that $tmpfname string that comes from another function is not
>> always unique. If the file was created by another process, you may not
>> be its owner thus you don't have permission to change permissions, or
>> the other process may have removed it while you were writing it. There
>> are a number of tools that can prevent access to a file for security
>> reasons (from PHP's safe mode to SELInux) and you don't always get an
>> accurate error message.
>>
>
> It make a lot of sense, and yes, it is very possible that the name is
> not unique, but I wonder how I could prevent the error/warning message
> in the first place.
>
> I cannot add a file_exists(...) just before chmod(...) because between
> the call to file_exists(...) and chmod(...) I could loose ownership of
> the file.
>
> Maybe I could/should use a temp name, open that temp file, write to it,
> close it, chmod(...) it and then just rename it.
>
> That might be a little better, maybe.
You can flock() the file but I believe it won't work properly unless the
other process is also using flock() from PHP. You know your setup better
than anyone :)
--
-- 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: Why would chmod( ... ) fail with "No such file or directory" [message #173163 is a reply to message #173161] |
Fri, 25 March 2011 13:29 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 3/25/2011 8:37 AM, Simon wrote:
> On 3/25/2011 12:31 PM, "Álvaro G. Vicario" wrote:
>> El 25/03/2011 10:35, Simon escribió/wrote:
>>> Hi,
>>>
>>> I have some code that creates a temp file, writes to it and then calls
>>> chmod(...) to make sure that the file has 0644 permissions.
>>>
>>> // -------------
>>> // pseudo code
>>> //
>>> $tmpfname = 'xyx'; // come from another function.
>>>
>>> if (!($fd = @fopen($tmpfname, 'wb')))
>>> {
>>> return false;
>>> }
>>>
>>> fwrite($fd, $data ); // the data comes from another function has well.
>>> no warnings given.
>>
>> fwrite() returns FALSE if there was an error, you don't need to rely on
>> getting warnings.
>>
>>> fclose($fd); // no warnings given.
>>
>> Same with fclose().
>>
>>> chmod( $tmpfname, 0644 ); <!-- error here with "No such file or
>>> directory"
>>> // -------------
>>>
>>> The issue I have is that it works _sometimes_, in fact, 99% of the time
>>> it works.
>>> But from time to time the log reports that the file does not exist.
>>>
>>> But how it is possible, if the file opened successfully with fopen(
>>> ... )
>>
>> Perhaps that $tmpfname string that comes from another function is not
>> always unique. If the file was created by another process, you may not
>> be its owner thus you don't have permission to change permissions, or
>> the other process may have removed it while you were writing it. There
>> are a number of tools that can prevent access to a file for security
>> reasons (from PHP's safe mode to SELInux) and you don't always get an
>> accurate error message.
>>
>
> It make a lot of sense, and yes, it is very possible that the name is
> not unique, but I wonder how I could prevent the error/warning message
> in the first place.
>
> I cannot add a file_exists(...) just before chmod(...) because between
> the call to file_exists(...) and chmod(...) I could loose ownership of
> the file.
>
> Maybe I could/should use a temp name, open that temp file, write to it,
> close it, chmod(...) it and then just rename it.
>
> That might be a little better, maybe.
>
> Simon
>
This is always a problem when you use temporary files for storage.
But here's a different question - what is it you're trying to accomplish?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: Why would chmod( ... ) fail with "No such file or directory" [message #173164 is a reply to message #173159] |
Fri, 25 March 2011 14:00 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
Simon wrote:
> Hi,
>
> I have some code that creates a temp file, writes to it and then calls
> chmod(...) to make sure that the file has 0644 permissions.
>
> // -------------
> // pseudo code
> //
> $tmpfname = 'xyx'; // come from another function.
>
> if (!($fd = @fopen($tmpfname, 'wb')))
> {
> return false;
> }
>
> fwrite($fd, $data ); // the data comes from another function has well.
> no warnings given.
> fclose($fd); // no warnings given.
> chmod( $tmpfname, 0644 ); <!-- error here with "No such file or directory"
> // -------------
>
> The issue I have is that it works _sometimes_, in fact, 99% of the time
> it works.
> But from time to time the log reports that the file does not exist.
>
> But how it is possible, if the file opened successfully with fopen( ... )
>
> I wonder if the problem is partly because I am using @fopen(...) rather
> than fopen(...), (I don't need to suppress the warnings).
>
> But in that case fwrite(...) and fclose(...) would also report
> errors/warnings.
>
> Any suggestions as to why chmod(...), (and a subsequent rename(...) )
> would sometimes not work.
>
I ran into some very strange issues like this a year back..my conclusion
was that PHP caches file info and doesn't (always )flush to the OS until
the session ends, if it can. So system calls within php referring to
that file may fail.
In my case it was a total failure of the mysql LOAD FILE command to find
a PHP uploaded file until I had copy()-ed it to another filename...
> Thanks
>
> Simon
>
|
|
|
|
Re: Why would chmod( ... ) fail with "No such file or directory" [message #173166 is a reply to message #173164] |
Fri, 25 March 2011 17:52 |
crankypuss
Messages: 147 Registered: March 2011
Karma: 0
|
Senior Member |
|
|
The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:
> Simon wrote:
>> Hi,
>>
>> I have some code that creates a temp file, writes to it and then calls
>> chmod(...) to make sure that the file has 0644 permissions.
>>
>> // -------------
>> // pseudo code
>> //
>> $tmpfname = 'xyx'; // come from another function.
>>
>> if (!($fd = @fopen($tmpfname, 'wb')))
>> {
>> return false;
>> }
>>
>> fwrite($fd, $data ); // the data comes from another function has well.
>> no warnings given.
>> fclose($fd); // no warnings given.
>> chmod( $tmpfname, 0644 ); <!-- error here with "No such file or directory"
>> // -------------
>>
>> The issue I have is that it works _sometimes_, in fact, 99% of the time
>> it works.
>> But from time to time the log reports that the file does not exist.
>>
>> But how it is possible, if the file opened successfully with fopen( ... )
>>
>> I wonder if the problem is partly because I am using @fopen(...) rather
>> than fopen(...), (I don't need to suppress the warnings).
>>
>> But in that case fwrite(...) and fclose(...) would also report
>> errors/warnings.
>>
>> Any suggestions as to why chmod(...), (and a subsequent rename(...) )
>> would sometimes not work.
>>
>
> I ran into some very strange issues like this a year back..my conclusion
> was that PHP caches file info and doesn't (always )flush to the OS until
> the session ends, if it can. So system calls within php referring to
> that file may fail.
>
> In my case it was a total failure of the mysql LOAD FILE command to find
> a PHP uploaded file until I had copy()-ed it to another filename...
The following might be useful background info:
http://www.php.net/manual/en/function.clearstatcache.php
--
no aluminum siding offers today
|
|
|
Re: Why would chmod( ... ) fail with "No such file or directory" [message #173172 is a reply to message #173159] |
Fri, 25 March 2011 23:20 |
Twayne
Messages: 135 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
In news:8v35ufF6r4U1(at)mid(dot)individual(dot)net,
Simon <bad(at)example(dot)com> typed:
> Hi,
>
> I have some code that creates a temp file, writes to it
> and then calls chmod(...) to make sure that the file has
> 0644 permissions.
> // -------------
> // pseudo code
> //
> $tmpfname = 'xyx'; // come from another function.
>
> if (!($fd = @fopen($tmpfname, 'wb')))
> {
> return false;
> }
>
> fwrite($fd, $data ); // the data comes from another
> function has well. no warnings given.
> fclose($fd); // no warnings given.
> chmod( $tmpfname, 0644 ); <!-- error here with "No such
> file or directory" // -------------
>
> The issue I have is that it works _sometimes_, in fact,
> 99% of the time it works.
> But from time to time the log reports that the file does
> not exist.
> But how it is possible, if the file opened successfully
> with fopen( ... )
> I wonder if the problem is partly because I am using
> @fopen(...) rather than fopen(...), (I don't need to
> suppress the warnings).
> But in that case fwrite(...) and fclose(...) would also
> report errors/warnings.
>
> Any suggestions as to why chmod(...), (and a subsequent
> rename(...) ) would sometimes not work.
>
> Thanks
>
> Simon
It's possible you're trying to chmod while the last chmod process is stiill
running in memory (hasn't released the file yet). Try adding say a 1 Second
delay to start with with & see if that stops the problem. Or, get a monitor
to watch the processes.
HTH,
Twayne`
|
|
|