For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > August 2007 > File locking (Not Flock)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author File locking (Not Flock)
Bill H

2007-08-28, 7:07 pm

Is there a perl / linux way of locking a file? Not the FLOCK method
when you open a file, but setting the actual file as read only, or
read / write?

Bill H

it_says_BALLS_on_your forehead

2007-08-28, 7:07 pm

On Aug 28, 2:39 pm, Bill H <b...@ts1000.us> wrote:
> Is there a perl / linux way of locking a file? Not the FLOCK method
> when you open a file, but setting the actual file as read only, or
> read / write?


my $cnt = chmod 0755, 'foo', 'bar';

Bill H

2007-08-28, 7:07 pm

On Aug 28, 2:48 pm, it_says_BALLS_on_your forehead
<simon.c...@fmr.com> wrote:
> On Aug 28, 2:39 pm, Bill H <b...@ts1000.us> wrote:
>
>
> my $cnt = chmod 0755, 'foo', 'bar';


I was hoping to avoid using permissions, but I guess there isn't a
simple read only - read / write attribute in linux like in DOS. Thanks

Jürgen Exner

2007-08-28, 7:07 pm

Bill H wrote:
> On Aug 28, 2:48 pm, it_says_BALLS_on_your forehead
> <simon.c...@fmr.com> wrote:
>
> I was hoping to avoid using permissions, but I guess there isn't a
> simple read only - read / write attribute in linux like in DOS. Thanks


You seem to be . File permissions/attributes are a matter of the
file system, not the operating system.
Because DOS doesn't know any fileystem but FAT I guess you are talking about
read/write attributes for FAT.
First of all of course you can use FAT also in Linux although no sane person
would recommend to do so.
Now if you want the same functionality as FAT read/write attributes on a
unixoid file system then file permissions are the correct answer as can
easily be seen by the customary
-rwxrwxrwx
They just provide a finer granularity of options which is an absolut
requirement because of the multi-user nature of Linux.

If you want to set the file to read-only just remove the w for all three
groups:
-r-xr-xr-x

jue




Mumia W.

2007-08-28, 7:08 pm

On 08/28/2007 03:47 PM, Bill H wrote:
> On Aug 28, 2:48 pm, it_says_BALLS_on_your forehead
> <simon.c...@fmr.com> wrote:
>
> I was hoping to avoid using permissions, but I guess there isn't a
> simple read only - read / write attribute in linux like in DOS. Thanks
>


I'm not sure about this, but I think you'd do better locking the file
this way:

my $cnt = chmod 0444, 'foo', 'bar';

Test if a file is "unlocked" this way:

if (-w 'foo') {
... write to 'foo' ...
}

Read these:
perldoc -f chmod
perldoc -f -w
Jim Cochrane

2007-08-28, 10:05 pm

On 2007-08-28, Mumia W. <paduille.4061.mumia.w+nospam@earthlink.net> wrote:
> On 08/28/2007 03:47 PM, Bill H wrote:
>
> I'm not sure about this, but I think you'd do better locking the file
> this way:
>
> my $cnt = chmod 0444, 'foo', 'bar';
>
> Test if a file is "unlocked" this way:
>
> if (-w 'foo') {
> ... write to 'foo' ...
> }
>
> Read these:
> perldoc -f chmod
> perldoc -f -w


I don't believe there's any way to make the implied set of operations
atomic (unless you add something, like semaphores), so it looks like
you'll have a potential race condition.

--

Mumia W.

2007-08-29, 8:04 am

On 08/28/2007 09:17 PM, Jim Cochrane wrote:
> On 2007-08-28, Mumia W. <paduille.4061.mumia.w+nospam@earthlink.net> wrote:
> I don't believe there's any way to make the implied set of operations
> atomic (unless you add something, like semaphores), so it looks like
> you'll have a potential race condition.
>


Hmm. I didn't think about that.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com