annotate src/filelock.c @ 4109:76f746324590

if [ "$os" ] --> if [ x"$os" != x"" ]
author Doug Evans <dje@gnu.org>
date Fri, 16 Jul 1993 18:24:47 +0000
parents f71f3f167365
children 41c85882768c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2961
e94a593c3952 Updated copyright years.
Jim Blandy <jimb@redhat.com>
parents: 733
diff changeset
1 /* Copyright (C) 1985, 1986, 1987, 1993 Free Software Foundation, Inc.
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3 This file is part of GNU Emacs.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 GNU Emacs is free software; you can redistribute it and/or modify
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 any later version.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10 GNU Emacs is distributed in the hope that it will be useful,
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 GNU General Public License for more details.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 along with GNU Emacs; see the file COPYING. If not, write to
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20 #include <sys/types.h>
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 #include <sys/stat.h>
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22 #include "config.h"
372
481e29c1e27e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 163
diff changeset
23
481e29c1e27e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 163
diff changeset
24 #ifdef VMS
559
65e7f842d017 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 485
diff changeset
25 #include "vms-pwd.h"
372
481e29c1e27e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 163
diff changeset
26 #else
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 #include <pwd.h>
372
481e29c1e27e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 163
diff changeset
28 #endif
481e29c1e27e *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 163
diff changeset
29
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30 #include <errno.h>
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 #include <sys/file.h>
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 #ifdef USG
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33 #include <fcntl.h>
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34 #endif /* USG */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 #include "lisp.h"
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 #include "paths.h"
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38 #include "buffer.h"
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 extern int errno;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
42 extern char *egetenv ();
733
62dd28940dc6 entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 638
diff changeset
43 extern char *strcpy ();
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
44
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 #ifdef CLASH_DETECTION
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 /* If system does not have symbolic links, it does not have lstat.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 In that case, use ordinary stat instead. */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50 #ifndef S_IFLNK
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 #define lstat stat
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 #endif
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
54
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
55 /* The name of the directory in which we keep lock files, with a '/'
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
56 appended. */
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
57 char *lock_path;
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
58
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
59 /* The name of the file in the lock directory which is used to
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
60 arbitrate access to the entire directory. */
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
61 #define SUPERLOCK_NAME "!!!SuperLock!!!"
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
62
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
63 /* The path to the superlock file. This is SUPERLOCK_NAME appended to
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
64 lock_path. */
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
65 char *superlock_path;
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
66
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
67 /* Set LOCK to the name of the lock file for the filename FILE.
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
68 char *LOCK; Lisp_Object FILE; */
3537
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
69
3602
f71f3f167365 * filelock.c: Test HAVE_LONG_FILE_NAMES, not SHORT_FILE_NAMES, to
Jim Blandy <jimb@redhat.com>
parents: 3537
diff changeset
70 #ifndef HAVE_LONG_FILE_NAMES
3537
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
71
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
72 #define MAKE_LOCK_PATH(lock, file) \
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
73 (lock = (char *) alloca (14 + strlen (lock_path) + 1), \
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
74 fill_in_lock_short_file_name (lock, (file)))
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
75
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
76
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
77 fill_in_lock_short_file_name (lockfile, fn)
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
78 register char *lockfile;
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
79 register Lisp_Object fn;
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
80 {
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
81 register union
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
82 {
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
83 unsigned int word [2];
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
84 unsigned char byte [8];
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
85 } crc;
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
86 register unsigned char *p, new;
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
87
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
88 /* 7-bytes cyclic code for burst correction on byte-by-byte basis.
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
89 the used polynomial is D^7 + D^6 + D^3 +1. pot@cnuce.cnr.it */
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
90
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
91 crc.word[0] = crc.word[1] = 0;
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
92
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
93 for (p = XSTRING (fn)->data; new = *p++; )
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
94 {
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
95 new += crc.byte[7];
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
96 crc.byte[7] = crc.byte[6];
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
97 crc.byte[6] = crc.byte[5] + new;
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
98 crc.byte[5] = crc.byte[4];
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
99 crc.byte[4] = crc.byte[3];
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
100 crc.byte[3] = crc.byte[2] + new;
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
101 crc.byte[2] = crc.byte[1];
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
102 crc.byte[1] = crc.byte[0];
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
103 crc.byte[0] = new;
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
104 }
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
105 sprintf (lockfile, "%s%.2x%.2x%.2x%.2x%.2x%.2x%.2x", lock_path,
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
106 crc.byte[0], crc.byte[1], crc.byte[2], crc.byte[3],
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
107 crc.byte[4], crc.byte[5], crc.byte[6]);
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
108 }
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
109
3602
f71f3f167365 * filelock.c: Test HAVE_LONG_FILE_NAMES, not SHORT_FILE_NAMES, to
Jim Blandy <jimb@redhat.com>
parents: 3537
diff changeset
110 #else /* defined HAVE_LONG_FILE_NAMES */
3537
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
111
638
40b255f55df3 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 624
diff changeset
112 #define MAKE_LOCK_PATH(lock, file) \
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
113 (lock = (char *) alloca (XSTRING (file)->size + strlen (lock_path) + 1), \
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
114 fill_in_lock_file_name (lock, (file)))
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
115
3537
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
116
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
117 fill_in_lock_file_name (lockfile, fn)
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
118 register char *lockfile;
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
119 register Lisp_Object fn;
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
120 {
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
121 register char *p;
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
122
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
123 strcpy (lockfile, lock_path);
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
124
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
125 p = lockfile + strlen (lockfile);
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
126
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
127 strcpy (p, XSTRING (fn)->data);
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
128
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
129 for (; *p; p++)
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
130 {
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
131 if (*p == '/')
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
132 *p = '!';
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
133 }
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
134 }
3602
f71f3f167365 * filelock.c: Test HAVE_LONG_FILE_NAMES, not SHORT_FILE_NAMES, to
Jim Blandy <jimb@redhat.com>
parents: 3537
diff changeset
135 #endif /* !defined HAVE_LONG_FILE_NAMES */
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
136
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137 static Lisp_Object
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138 lock_file_owner_name (lfname)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139 char *lfname;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 struct stat s;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 struct passwd *the_pw;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 extern struct passwd *getpwuid ();
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 if (lstat (lfname, &s) == 0)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 the_pw = getpwuid (s.st_uid);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 return (the_pw == 0 ? Qnil : build_string (the_pw->pw_name));
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 /* lock_file locks file fn,
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 meaning it serves notice on the world that you intend to edit that file.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 This should be done only when about to modify a file-visiting
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 buffer previously unmodified.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 Do not (normally) call lock_buffer for a buffer already modified,
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 as either the file is already locked, or the user has already
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 decided to go ahead without locking.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 When lock_buffer returns, either the lock is locked for us,
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 or the user has said to go ahead without locking.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 If the file is locked by someone else, lock_buffer calls
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 ask-user-about-lock (a Lisp function) with two arguments,
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 the file name and the name of the user who did the locking.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 This function can signal an error, or return t meaning
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 take away the lock, or return nil meaning ignore the lock. */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 /* The lock file name is the file name with "/" replaced by "!"
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 and put in the Emacs lock directory. */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 /* (ie., /ka/king/junk.tex -> /!/!ka!king!junk.tex). */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171
3602
f71f3f167365 * filelock.c: Test HAVE_LONG_FILE_NAMES, not SHORT_FILE_NAMES, to
Jim Blandy <jimb@redhat.com>
parents: 3537
diff changeset
172 /* If HAVE_LONG_FILE_NAMES is not defined, the lock file name is the hex
3537
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
173 representation of a 14-bytes CRC generated from the file name
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
174 and put in the Emacs lock directory (not very nice, but it works).
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
175 (ie., /ka/king/junk.tex -> /!/ec92d3ed24a8f0). */
22055fd47b78 (MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents: 2961
diff changeset
176
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 void
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178 lock_file (fn)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 register Lisp_Object fn;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 register Lisp_Object attack;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 register char *lfname;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
184 MAKE_LOCK_PATH (lfname, fn);
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
186 /* See if this file is visited and has changed on disk since it was
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
187 visited. */
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 register Lisp_Object subject_buf = Fget_file_buffer (fn);
485
8c615e453683 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 372
diff changeset
190 if (!NILP (subject_buf)
8c615e453683 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 372
diff changeset
191 && NILP (Fverify_visited_file_modtime (subject_buf))
8c615e453683 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 372
diff changeset
192 && !NILP (Ffile_exists_p (fn)))
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193 call1 (intern ("ask-user-about-supersession-threat"), fn);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 /* Try to lock the lock. */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 if (lock_if_free (lfname) <= 0)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 /* Return now if we have locked it, or if lock dir does not exist */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 return;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 /* Else consider breaking the lock */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 attack = call2 (intern ("ask-user-about-lock"), fn,
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 lock_file_owner_name (lfname));
485
8c615e453683 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 372
diff changeset
204 if (!NILP (attack))
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 /* User says take the lock */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 lock_superlock (lfname);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 lock_file_1 (lfname, O_WRONLY) ;
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
209 unlink (superlock_path);
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 return;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 /* User says ignore the lock */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 /* Lock the lock file named LFNAME.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 If MODE is O_WRONLY, we do so even if it is already locked.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 If MODE is O_WRONLY | O_EXCL | O_CREAT, we do so only if it is free.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 Return 1 if successful, 0 if not. */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220 int
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221 lock_file_1 (lfname, mode)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222 int mode; char *lfname;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224 register int fd;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 char buf[20];
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 if ((fd = open (lfname, mode, 0666)) >= 0)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 #ifdef USG
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 chmod (lfname, 0666);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 #else
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 fchmod (fd, 0666);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 #endif
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234 sprintf (buf, "%d ", getpid ());
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 write (fd, buf, strlen (buf));
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 close (fd);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 return 1;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 else
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 return 0;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 /* Lock the lock named LFNAME if possible.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 Return 0 in that case.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 Return positive if lock is really locked by someone else.
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246 Return -1 if cannot lock for any other reason. */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 int
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249 lock_if_free (lfname)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 register char *lfname;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 register int clasher;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 while (lock_file_1 (lfname, O_WRONLY | O_EXCL | O_CREAT) == 0)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 if (errno != EEXIST)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 return -1;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 clasher = current_lock_owner (lfname);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 if (clasher != 0)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260 if (clasher != getpid ())
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261 return (clasher);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262 else return (0);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263 /* Try again to lock it */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 return 0;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 /* Return the pid of the process that claims to own the lock file LFNAME,
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269 or 0 if nobody does or the lock is obsolete,
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 or -1 if something is wrong with the locking mechanism. */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272 int
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 current_lock_owner (lfname)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274 char *lfname;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 int owner = current_lock_owner_1 (lfname);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 if (owner == 0 && errno == ENOENT)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 return (0);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 /* Is it locked by a process that exists? */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280 if (owner != 0 && (kill (owner, 0) >= 0 || errno == EPERM))
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 return (owner);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282 if (unlink (lfname) < 0)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283 return (-1);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
284 return (0);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
285 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
286
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
287 int
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
288 current_lock_owner_1 (lfname)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
289 char *lfname;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
290 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291 register int fd;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
292 char buf[20];
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
293 int tem;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
294
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
295 fd = open (lfname, O_RDONLY, 0666);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
296 if (fd < 0)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
297 return 0;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
298 tem = read (fd, buf, sizeof buf);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
299 close (fd);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300 return (tem <= 0 ? 0 : atoi (buf));
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
301 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
302
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
303
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
304 void
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
305 unlock_file (fn)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
306 register Lisp_Object fn;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
307 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
308 register char *lfname;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
310 MAKE_LOCK_PATH (lfname, fn);
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
311
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312 lock_superlock (lfname);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
313
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
314 if (current_lock_owner_1 (lfname) == getpid ())
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315 unlink (lfname);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
316
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
317 unlink (superlock_path);
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
318 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
319
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
320 lock_superlock (lfname)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
321 char *lfname;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
322 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
323 register int i, fd;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
324
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
325 for (i = -20; i < 0 && (fd = open (superlock_path,
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327 i++)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
329 if (errno != EEXIST)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 return;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331 sleep (1);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
332 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333 if (fd >= 0)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
335 #ifdef USG
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
336 chmod (superlock_path, 0666);
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337 #else
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 fchmod (fd, 0666);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339 #endif
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
340 write (fd, lfname, strlen (lfname));
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341 close (fd);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
342 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
344
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
345 void
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 unlock_all_files ()
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
347 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
348 register Lisp_Object tail;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
349 register struct buffer *b;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351 for (tail = Vbuffer_alist; XGCTYPE (tail) == Lisp_Cons;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352 tail = XCONS (tail)->cdr)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
354 b = XBUFFER (XCONS (XCONS (tail)->car)->cdr);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
355 if (XTYPE (b->filename) == Lisp_String &&
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
356 b->save_modified < BUF_MODIFF (b))
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
357 unlock_file (b->filename);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
358 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
359 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 DEFUN ("lock-buffer", Flock_buffer, Slock_buffer,
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 0, 1, 0,
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364 "Lock FILE, if current buffer is modified.\n\
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365 FILE defaults to current buffer's visited file,\n\
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366 or else nothing is done if current buffer isn't visiting a file.")
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
367 (fn)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
368 Lisp_Object fn;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
369 {
485
8c615e453683 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 372
diff changeset
370 if (NILP (fn))
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
371 fn = current_buffer->filename;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
372 else
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
373 CHECK_STRING (fn, 0);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
374 if (current_buffer->save_modified < MODIFF
485
8c615e453683 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 372
diff changeset
375 && !NILP (fn))
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
376 lock_file (fn);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
377 return Qnil;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
378 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
379
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
380 DEFUN ("unlock-buffer", Funlock_buffer, Sunlock_buffer,
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
381 0, 0, 0,
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
382 "Unlock the file visited in the current buffer,\n\
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
383 if it should normally be locked.")
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
384 ()
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
385 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
386 if (current_buffer->save_modified < MODIFF &&
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
387 XTYPE (current_buffer->filename) == Lisp_String)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
388 unlock_file (current_buffer->filename);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389 return Qnil;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
390 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
392
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
393 /* Unlock the file visited in buffer BUFFER. */
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
395 unlock_buffer (buffer)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
396 struct buffer *buffer;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
397 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
398 if (buffer->save_modified < BUF_MODIFF (buffer) &&
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
399 XTYPE (buffer->filename) == Lisp_String)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400 unlock_file (buffer->filename);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
402
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
403 DEFUN ("file-locked-p", Ffile_locked_p, Sfile_locked_p, 0, 1, 0,
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404 "Return nil if the FILENAME is not locked,\n\
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
405 t if it is locked by you, else a string of the name of the locker.")
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
406 (fn)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
407 Lisp_Object fn;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
408 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
409 register char *lfname;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
410 int owner;
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
411
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
412 fn = Fexpand_file_name (fn, Qnil);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
413
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
414 MAKE_LOCK_PATH (lfname, fn);
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
415
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
416 owner = current_lock_owner (lfname);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
417 if (owner <= 0)
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
418 return (Qnil);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
419 else if (owner == getpid ())
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
420 return (Qt);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
421
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
422 return (lock_file_owner_name (lfname));
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
423 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
424
624
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
425
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
426 /* Initialization functions. */
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
427
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
428 init_filelock ()
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
429 {
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
430 lock_path = egetenv ("EMACSLOCKDIR");
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
431 if (! lock_path)
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
432 lock_path = PATH_LOCK;
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
433
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
434 /* Make sure it ends with a slash. */
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
435 if (lock_path[strlen (lock_path) - 1] != '/')
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
436 {
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
437 lock_path = strcpy ((char *) xmalloc (strlen (lock_path) + 2),
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
438 lock_path);
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
439 strcat (lock_path, "/");
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
440 }
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
441
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
442 superlock_path = (char *) xmalloc ((strlen (lock_path)
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
443 + sizeof (SUPERLOCK_NAME)));
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
444 strcpy (superlock_path, lock_path);
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
445 strcat (superlock_path, SUPERLOCK_NAME);
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
446 }
2bb7f23b7ea5 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 559
diff changeset
447
163
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
448 syms_of_filelock ()
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
449 {
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
450 defsubr (&Sunlock_buffer);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
451 defsubr (&Slock_buffer);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
452 defsubr (&Sfile_locked_p);
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
453 }
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
454
0f3996cb4ae5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
455 #endif /* CLASH_DETECTION */