File locking (Not Flock)

B

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
 
I

it_says_BALLS_on_your forehead

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';
 
B

Bill H

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

Jürgen Exner

Bill said:
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
 
M

Mumia W.

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
 
J

Jim Cochrane

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.

--
 
M

Mumia W.

[ 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top