cancel
Showing results for 
Search instead for 
Did you mean: 

How to lock a file ?

SOLVED
Go to solution
Jdamian
Respected Contributor

How to lock a file ?

Hi

We want to prevent that two or more people edit a text file and overwrite the text typed by others. I thought in file locking.

Is it possible to force a file to be locked when it is gonna be changed ?

Thanx in advance
9 REPLIES
Dennis Handly
Acclaimed Contributor
Solution

Re: How to lock a file ?

You might want to look at RCS, (co(1) and ci(1)), if you only need something simple.

Normally locking on HP-UX is advisory only. Which means your editor must play along.
Ralph Grothe
Honored Contributor

Re: How to lock a file ?

If it's only for change management of files to be edited by users then probably a revision control system applies.
The most simple to use would be the RCS commands that already are installed with hpux.
Your users can set locks on the files they check out for editing them (e.g. co -l file).
But all users would be required to abide by RCS.
Then they would prevent each other from modifying locked files.
See manpages rcs, rlog, ci, co, ...
If you need a revision control at a larger (concurrent) scale you could use cvs or subversion.

As for programmatic file locking you can use the fcntl() syscall.
But this also would require the programmes that access or modify files to all abide to this locking mechanism (so called advisory locking).
See man fcntl.
Madness, thy name is system administration
Peter Godron
Honored Contributor

Re: How to lock a file ?

Hi,
what sort of application ? Or are you talking about OS level lock ?

Please see:
http://www.ecst.csuchico.edu/~beej/guide/ipc/flock.html

Jdamian
Respected Contributor

Re: How to lock a file ?

I'm looking in a user level, not in programmatic level.
Peter Godron
Honored Contributor

Re: How to lock a file ?

Oscar,
from what you describe I don't think there is an actual user controlled locking mechanism available.
If user B for example uses vi, user B will see a message about user A using the file.
But I don't think you can inforce a lock, unless you use the chmod to change ownership.
Tor-Arne Nostdal
Trusted Contributor

Re: How to lock a file ?

Hi Oscar
Since you anyway intend to write a script/program to do this, you could just create a separate lockfile.

I do this for ensuring exclusive editing of some files.
I use:
who am i > $LOCKFILE
or
who -Rm > $LOCKFILE
So that I can show who is holding the lock and when it was created, to anyone who try to edit while lock exist.

You should also trap signals so you make sure to remove the file when editing finish.

To check if someone is bypassing your program you might want to use a ps command, though this might not be feasible if you're on a shared filesystem.

Another idea is to keep a couple of generations of the file in case you want to check differences.

/Tor-Arne
I'm trying to become President of the state I'm in...
Tor-Arne Nostdal
Trusted Contributor

Re: How to lock a file ?

... and for sure ;-) check for the presence of the lockfile...

[[ -e "$LOCKFILE" ]] && {
echo "File is being edited by:"
cat $LOCKFILE
}

/2r
I'm trying to become President of the state I'm in...
Wei Jung
Trusted Contributor

Re: How to lock a file ?

can't you just use chgroup and have those 2 poor guys in another user group? :)
Don't grant that group write permissions with chown.
Jdamian
Respected Contributor

Re: How to lock a file ?

.