in reply to Why doesn't chmod work on this lexical filehandle?
chmod 0444 on a file handle open for writing works for me. I'm running Linux Debian 2.6.22 (lenny) and Perl 5.10. However, I seem to recall that when I was running code on Debian Etch + 5.8.8 chmod on a file open for writing would fail. That older version of Perl (or of the operating system) didn't let you mark a file as read only when it was open for writing.
As mentioned earlier, chmod on a file handle in Perl only works if your system supports chmod on file handles in general (i.e. it supports fchmod). I'm guessing your system does not support it or else you are using an older perl/os combination. Can you set any permissions on a file handle? Or is the problem limited to making files open for writing read only?
In any case, your best bet is to simply call chmod on the file name rather than the file handle. I can see from your code that you have access to both so this shouldn't be a problem. Your only risk is a small chance that someone could intercept the file and modify it between the time that you close the handle and the time that you set the permissions.
Also, as a side note, I notice you are using the two parameter open. Don't. Use the three parameter open instead. With the two parameter open there is a small chance that Perl with confuse the file name and the open instructions. With the three parameter open, this risk goes away. You may not be able to avoid the security/timing risk, but the risk of a misinterpreted is something you can eliminate entirely.
Update: - struck out words -my recollection was faulty - looking back on my testing notes from a ways back it wasn't chmod on a file handle that failed in Etch+5.8.8 but rather 0444 passed to sysopen - system open would turn 0444 into 0644. I just brought up the Debian Etch system and chmod 0444 on a file handle does work on Etch+5.8.8. Thank-you anonymous monk below for making me double check this.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Why doesn't chmod work on this lexical filehandle?
by Anonyrnous Monk (Hermit) on Feb 15, 2011 at 04:06 UTC | |
by williff (Initiate) on Feb 15, 2011 at 15:45 UTC | |
|
Re^2: Why doesn't chmod work on this lexical filehandle?
by williff (Initiate) on Feb 15, 2011 at 15:53 UTC |