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

Home » Imported messages » comp.lang.php » Why would chmod( ... ) fail with "No such file or directory"
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
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 Go to previous messageGo to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma:
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
--
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Cannot send emails
Next Topic: website developers requirements
Goto Forum:
  

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

Current Time: Fri Nov 29 23:33:53 GMT 2024

Total time taken to generate the page: 0.05805 seconds