Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

File Locking for editing ??

Posted on 1997-11-26
8
Medium Priority
?
560 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 280 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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!

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…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

636 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