Locking a file when using VI to prevent multiple-edit sessions by diff users

At the office, we often have to edit one file with VI. We are 4-6 workers doing it and sometimes can be done at the same time.

We have found a problem and want to prevent it with a file lock. Is it possible and how ?

problem :

Worker-a starts edit VI session on File-A at 1PM
Worker-b starts edit VI session on File-A at 1:05PM

worker-a changes 100 lines and exit/saves at 2:00PM
worker-b changes 1 line and exit/saves at 2:15PM

as result, changes done by worker-a are lost
we even had cases where losts of lines being edited were lost.

if we could lock the file upon editing it with VI, it woud prevent others from editing it and would have to wait for whomever is in to finish before being able to edit it.

I would do that the hard way ... recompile from the source :smiley:
Get the source, modify so to open in read-only mode (and warn user) if the lock file is present!
Not really what you're looking for i guess :rolleyes: (though the modification should be straight)

instant messaging, send a message to all other users that you are hacking around in the file so the others know to hold off until you are done.
i cannot think of an easy way with file locking to prevent your problem.
we have several dev boxes that we modify or reboot at random and we use instant messaging to broadcast a message that we are doing some changes, seems to work for us.

Isn't there a unix (AIX) command that can lock or unlock a file ?

That way we could simply execute a small script that would do :

if file not locked
lock file
vi file
unlock file
else
echo "file being edited"

A revision management system such as CVS and Subversion will eliminate all of your worries in the long run.

Do not let everybody modify the production version. Have every staff checkout a local copy, which everyone modifies on their own, commit to CVS, and at last only update the production version by the administrator after the current version has been considered okay.

Best of all, such systems can help you keep the revision history of every file managed, so you literally never lose anything!

Maybe you can either:
1.
ask the user to do a ps -ef before editing the file
cerntst1:/# ps -ef | grep test
root 478570 611346 0 15:29:18 pts/2 0:00 vi test.txt <-----
root 647012 51760 0 15:29:39 pts/3 0:00 grep test

or
2.
create a program to detect the vi process and if vi test.txt is showing up in the process, exit.

Like cbkihong said, revision control is the way to go. You could even use a built-in system like rcs.

vim (an excellent vi clone) creates a swap file when you open a file. A side-effect is that if someone else opens the file in vim it sees that there already is a swap file. It then tells the user that the file is already opened by someone else (and which user that is) and asks if you maybe want to open it read-only. Maybe switching to vim is an option?

Without revision control, vim is probably okay, but note that you can still forcefully edit the file despite its warning (probably opened by another party). And, what if the other party is opening the file with emacs? joe? or just simply the filesystem is samba mounted and the file opened with Notepad on a Windows machine? The swap file serves no purpose then. Can you control the exact editor the staff is using? It depends on whether the company is willing to bear this risk. For most companies, especially for production servers, this is most probably no.

Therefore, any editor-specific feature is unlikely to be safe by most corporate standards, if there is any.

"At the office"..."We are 4-6 workers"...
talk to each other!

Hmm. Then the office will be very noisy, I guess. :smiley:

I do have a very simple way of doing this but it involves renaming vi to something else (viedit for example) and replacing vi with a K shell script. If you are interested let me know here and I will post the script.

I'm interested : )

OK

If you follow the previous instructions and paste the following into a script named vi and situate it in the /usr/bin directory. I have tested it with no errors.

#!/bin/ksh
#
vimod=/usr/bin/viedit
#
# The above is the renamed vi executable.
#
vercnt=/tmp/vercnt
vercnt1=/tmp/vercnt1
#
# You can put the above anywhere you want.
#
PID=$$
#
# Check the number of attribs equal to zero no file name given.
#
if [[ $# -eq 0 ]]
then
echo No file name given exiting
exit 3
fi
#
vifile=$1
grep $vifile $vercnt 1>/dev/null 2>&1
if [[ $? != 0 ]]
then
echo $vifile":$PID" >> $vercnt
else
echo Sorry this file is already being edited please try later
exit 3
fi
#
# This is where vi will be called.
#
$vimod $vifile
#
# When the vi session ends the function will return here and delete the PID entry from the
# holding file.
#
cat $vercnt | grep -v $PID > $vercnt1
mv $vercnt1 $vercnt

Enjoy.

Can VIM be installed on AIX 64Bit v5.2 ?

If so, can we download a package for it and where ?