File locking (Not Flock)

File locking (Not Flock)

am 28.08.2007 20:39:59 von Bill H

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

Re: File locking (Not Flock)

am 28.08.2007 20:48:05 von it_says_BALLS_on_your forehead

On Aug 28, 2:39 pm, Bill H 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';

Re: File locking (Not Flock)

am 28.08.2007 22:47:03 von Bill H

On Aug 28, 2:48 pm, it_says_BALLS_on_your forehead
wrote:
> On Aug 28, 2:39 pm, Bill H 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';

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

Re: File locking (Not Flock)

am 28.08.2007 23:00:26 von jurgenex

Bill H wrote:
> On Aug 28, 2:48 pm, it_says_BALLS_on_your forehead
> wrote:
>> On Aug 28, 2:39 pm, Bill H 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';
>
> 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 confused. 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

Re: File locking (Not Flock)

am 28.08.2007 23:47:06 von paduille.4061.mumia.w+nospam

On 08/28/2007 03:47 PM, Bill H wrote:
> On Aug 28, 2:48 pm, it_says_BALLS_on_your forehead
> wrote:
>> On Aug 28, 2:39 pm, Bill H 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';
>
> 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

Re: File locking (Not Flock)

am 29.08.2007 04:17:33 von Jim Cochrane

On 2007-08-28, Mumia W. wrote:
> On 08/28/2007 03:47 PM, Bill H wrote:
>> On Aug 28, 2:48 pm, it_says_BALLS_on_your forehead
>> wrote:
>>> On Aug 28, 2:39 pm, Bill H 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';
>>
>> 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

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.

--

Re: File locking (Not Flock)

am 29.08.2007 05:18:23 von paduille.4061.mumia.w+nospam

On 08/28/2007 09:17 PM, Jim Cochrane wrote:
> On 2007-08-28, Mumia W. wrote:
>> [ code to "lock" using chmod snipped ]
> 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.