Solved

File Locking for editing ??

Posted on 1997-11-26
8
554 Views
Last Modified: 2013-12-27
Hello,

    I'm running on a Sun SPARCstation 5 (SunOS 5.5.1) can
someone please tell me the best way to control this situation: There are several files that several people need to change concurrently (using vi and emacs).  These 3 people
are members of a unique unix group.  How can we ensure that
we are not editing the same file at the same time? This is not source code and we don't want to use SCCS or any other
source control system - We're looking for some file locking
mechanism; e.g., John is ready to edit file "foo.doc" he
executes <unix.file.locking.command> he then edits the document; after finished with edits he unlocks the file <unix.file.locking.command -u>.

Thanks !

kcc
0
Comment
Question by:kccole
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2008024
lock="$NEWSCTL/LOCKxxx"         # modify name as appropriate
ltemp="$NEWSCTL/L.$$"
echo $$ >$ltemp
trap "rm -f $ltemp ; exit 0" 0 1 2 15
while true
do
        if ln $ltemp $lock
        then
                trap "rm -f $ltemp $lock ; exit 0" 0 1 2 15
                break
        fi
        sleep 30
done

0
 

Author Comment

by:kccole
ID: 2008025
Thanx for your reply but unfortunately I'm left with too many questions as to what each line of code does exactly and how it can be used in the example I supplied (in other words please
make it more idiot proof! with comments for code and an explanation of how to implement/execute when needing to edit the
 file.

Thanx Much!
kcc
0
 
LVL 1

Accepted Solution

by:
dhughes111797 earned 70 total points
ID: 2008026
I wrote something like this in C that works. All you have to
do is set an alias for your editor of choice (emacs, vi)
that runs this program instead. Then your editor environment variable is set to your real editor.
e.g.
setenv EDITOR vi
alias vi /usr/bin/vi-lock

Try it out and see if it works for you.

#include <stdio.h>
#include <fcntl.h>
#include <sys/file.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
 
extern int errno;
 
main(int argc, char *argv[])
{
        int fd, rc;
        char answer[2];
        char edit_string[80];
        struct flock fl;
        char *editor;
        char *user;
 
        fl.l_type = F_WRLCK;
        fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = 0;

if (argc < 2) {
perror("usage vi-lock file [ file2 [ file3 ...]]");
exit(-5);
}

if ((editor = getenv("EDITOR")) == NULL)
editor = "vi";
if ((user = getenv("USER")) == NULL)
user = "root";


while (--argc > 0) {
*argv++;        /* skip argv[0] */

fd = open(*argv, O_CREAT | O_EXCL, 0600);
if (fd >= 0) {
fprintf(stderr, "Creating new file:  %s\n", *argv);
sleep(1);
} else {
close(fd);
}

fd = open(*argv, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Couldn't open file %s for writing\n", *
argv);
exit(-2);
}
if (((rc = fcntl(fd, F_SETLK, &fl)) < 0) && (errno != EINVAL)) {
perror("Locking");
fcntl(fd, F_GETLK, &fl);
printf("File %s is currently locked by pid %d. Wait? (Y/
N) ",
*argv, fl.l_pid);
scanf("%c",answer);
if (*answer == 'Y' || *answer == 'y') {
rc = fcntl(fd, F_SETLKW, &fl);
} else {
continue;
}
                } else if (rc < 0 && errno != EINVAL) {
                        perror("Locking problem");
                        printf("errno %d fd %d\n", errno, fd);
                        exit(1);
                }
 
 
                sprintf(edit_string, "%s %s\n", editor, *argv);
                rc = system(edit_string);
                if (rc < 0) {
                        perror("couldn't exec vi");
                        exit(-3);
                }
 
                fl.l_type = F_UNLCK;
                rc = fcntl(fd, F_SETLK, &fl);
                if (rc < 0) {
                        perror("couldn't unlock file");
                        exit(-4);
                }
        }
 
}

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Expert Comment

by:ahoffmann
ID: 2008027
alias vi to vi-lock (for example)
shell script for vi-lock (not security proofed):

#! /bin/sh
file=$1
chmod g-w $file
chmod o-w $file
/bin/vi $file
chmod g+w $file
chmod o+w $file
0
 

Author Comment

by:kccole
ID: 2008028
ahoffman,

    Doesn't your script work only if the owner of the file isn't
a possible editor of the file ? e.g.,

    unix id=sammi   group id=sammi

Sammi decides to add 3 other user's (unix id's) to his group:

    group sammi ==>  sammi, franki, noel, pransor

If franki executes your script and edits a common file
having the permission of 775 (before executing it) then
what will prevent Sammi from editing the same file at the same
time, thus corrupting the changes franki has made?

thanx!
kcc
0
 

Author Comment

by:kccole
ID: 2008029
Adjusted points to 75
0
 

Author Comment

by:kccole
ID: 2008030
Hello All,
 
    I've just learned of a utility called "led" on my system
that allows the very thing I need to do.  I just thought I'd poll the readers to see if anyone else knew of its existence too and
if so why do home grown stuff when something is already there to
use??

Thanx Again.,
kcc
0
 
LVL 1

Expert Comment

by:dhughes111797
ID: 2008031
never heard of it.. Are you sure it's not a local thing? (It's not
on any of our Solaris2 boxes)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question