annotate src/filelock.c @ 14:9ee227acff29 r19-15b90

Import from CVS: tag r19-15b90
author cvs
date Mon, 13 Aug 2007 08:48:42 +0200
parents ac2d302a0011
children 859a2309aef8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Copyright (C) 1985, 1986, 1987, 1992, 1993, 1994
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 /* Synched up with: FSF 19.30. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 #include "lisp.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 #include "buffer.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 #include "paths.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 #include "sysfile.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 #include "sysdir.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 #include "syspwd.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 #include "syssignal.h" /* for kill */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 #ifdef CLASH_DETECTION
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 /* FSFmacs uses char *lock_dir and char *superlock_file instead of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 the Lisp variables we use. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 /* The name of the directory in which we keep lock files, with a '/'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 appended. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 Lisp_Object Vlock_directory;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 #if 0 /* FSFmacs */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 /* Look in startup.el */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 /* The name of the file in the lock directory which is used to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 arbitrate access to the entire directory. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 #define SUPERLOCK_NAME "!!!SuperLock!!!"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 /* The name of the superlock file. This is SUPERLOCK_NAME appended to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 Vlock_directory. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 Lisp_Object Vsuperlock_file;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 Lisp_Object Qask_user_about_supersession_threat;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 Lisp_Object Qask_user_about_lock;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 static void lock_superlock (CONST char *lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 static int lock_file_1 (CONST char *lfname, int mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 static int lock_if_free (CONST char *lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 static int current_lock_owner (CONST char *);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 static int current_lock_owner_1 (CONST char *);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 /* Set LOCK to the name of the lock file for the filename FILE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 char *LOCK; Lisp_Object FILE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 MAKE_LOCK_NAME assumes you have already verified that Vlock_directory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 is a string. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 #ifndef HAVE_LONG_FILE_NAMES
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
72 #define MAKE_LOCK_NAME(lock, file) \
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
73 (lock = (char *) alloca (14 + XSTRING_LENGTH (Vlock_directory) + 1), \
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 fill_in_lock_short_file_name (lock, (file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 fill_in_lock_short_file_name (REGISTER char *lockfile, REGISTER Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 REGISTER union
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 unsigned int word [2];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 unsigned char byte [8];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 } crc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 REGISTER unsigned char *p, new;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 /* 7-bytes cyclic code for burst correction on byte-by-byte basis.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 the used polynomial is D^7 + D^6 + D^3 +1. pot@cnuce.cnr.it */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 crc.word[0] = crc.word[1] = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
93 for (p = XSTRING_DATA (fn); new = *p++; )
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 new += crc.byte[6];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 crc.byte[6] = crc.byte[5] + new;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 crc.byte[5] = crc.byte[4];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 crc.byte[4] = crc.byte[3];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 crc.byte[3] = crc.byte[2] + new;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 crc.byte[2] = crc.byte[1];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 crc.byte[1] = crc.byte[0];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 crc.byte[0] = new;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 int need_slash = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 /* in case lock-directory doesn't end in / */
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
109 if (XSTRING_BYTE (Vlock_directory,
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
110 XSTRING_LENGTH (Vlock_directory) - 1) != '/')
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 need_slash = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 sprintf (lockfile, "%s%s%.2x%.2x%.2x%.2x%.2x%.2x%.2x",
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
114 (char *) XSTRING_DATA (Vlock_directory),
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 need_slash ? "/" : "",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 crc.byte[0], crc.byte[1], crc.byte[2], crc.byte[3],
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 crc.byte[4], crc.byte[5], crc.byte[6]);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 #else /* defined HAVE_LONG_FILE_NAMES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 /* +2 for terminating null and possible extra slash */
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
124 #define MAKE_LOCK_NAME(lock, file) \
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
125 (lock = (char *) alloca (XSTRING_LENGTH (file) + \
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
126 XSTRING_LENGTH (Vlock_directory) + 2), \
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 fill_in_lock_file_name (lock, (file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 fill_in_lock_file_name (REGISTER char *lockfile, REGISTER Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 /* fn must be a Lisp_String! */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 REGISTER char *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
137 strcpy (lockfile, (char *) XSTRING_DATA (Vlock_directory));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 p = lockfile + strlen (lockfile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 if (p == lockfile /* lock-directory is empty?? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 || *(p - 1) != '/') /* in case lock-directory doesn't end in / */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 *p = '/';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 p++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
148 strcpy (p, (char *) XSTRING_DATA (fn));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 for (; *p; p++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 if (*p == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 *p = '!';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 #endif /* !defined HAVE_LONG_FILE_NAMES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 static Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 lock_file_owner_name (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 struct stat s;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 struct passwd *the_pw = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 if (lstat (lfname, &s) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 the_pw = getpwuid (s.st_uid);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 return (the_pw == 0 ? Qnil : build_string (the_pw->pw_name));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 /* lock_file locks file fn,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 meaning it serves notice on the world that you intend to edit that file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 This should be done only when about to modify a file-visiting
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 buffer previously unmodified.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 Do not (normally) call lock_buffer for a buffer already modified,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 as either the file is already locked, or the user has already
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 decided to go ahead without locking.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 When lock_buffer returns, either the lock is locked for us,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 or the user has said to go ahead without locking.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 If the file is locked by someone else, lock_buffer calls
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 ask-user-about-lock (a Lisp function) with two arguments,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 the file name and the name of the user who did the locking.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 This function can signal an error, or return t meaning
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 take away the lock, or return nil meaning ignore the lock. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 /* The lock file name is the file name with "/" replaced by "!"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 and put in the Emacs lock directory. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 /* (ie., /ka/king/junk.tex -> /!/!ka!king!junk.tex). */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 /* If HAVE_LONG_FILE_NAMES is not defined, the lock file name is the hex
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 representation of a 14-bytes CRC generated from the file name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 and put in the Emacs lock directory (not very nice, but it works).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 (ie., /ka/king/junk.tex -> /!/12a82c62f1c6da). */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 lock_file (Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 /* This function can GC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 REGISTER Lisp_Object attack, orig_fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 struct gcpro gcpro1, gcpro2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 Lisp_Object subject_buf = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 if (NILP (Vlock_directory) || NILP (Vsuperlock_file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 GCPRO2 (fn, subject_buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 orig_fn = fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 /* Create the name of the lock-file for file fn */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 /* See if this file is visited and has changed on disk since it was
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 visited. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 subject_buf = Fget_file_buffer (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 if (!NILP (subject_buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 && NILP (Fverify_visited_file_modtime (subject_buf))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 && !NILP (Ffile_exists_p (fn)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 call1_in_buffer (XBUFFER (subject_buf),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 Qask_user_about_supersession_threat, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 /* Try to lock the lock. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 if (lock_if_free (lfname) <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 /* Return now if we have locked it, or if lock dir does not exist */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 goto done;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 /* Else consider breaking the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 attack = call2_in_buffer (BUFFERP (subject_buf) ? XBUFFER (subject_buf) :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 current_buffer, Qask_user_about_lock, fn,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 lock_file_owner_name (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 if (!NILP (attack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 /* User says take the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 CHECK_STRING (Vsuperlock_file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 lock_superlock (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 lock_file_1 (lfname, O_WRONLY);
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
241 unlink ((char *) XSTRING_DATA (Vsuperlock_file));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 goto done;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 /* User says ignore the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 done:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 UNGCPRO;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 /* Lock the lock file named LFNAME.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 If MODE is O_WRONLY, we do so even if it is already locked.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 If MODE is O_WRONLY | O_EXCL | O_CREAT, we do so only if it is free.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 Return 1 if successful, 0 if not. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 lock_file_1 (CONST char *lfname, int mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 REGISTER int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 char buf[20];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 if ((fd = open (lfname, mode, 0666)) >= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 #ifdef USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 chmod (lfname, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 fchmod (fd, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 sprintf (buf, "%ld ", (long) getpid ());
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 write (fd, buf, strlen (buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 return 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 /* Lock the lock named LFNAME if possible.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 Return 0 in that case.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 Return positive if lock is really locked by someone else.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 Return -1 if cannot lock for any other reason. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 lock_if_free (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 REGISTER int clasher;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 while (lock_file_1 (lfname, O_WRONLY | O_EXCL | O_CREAT) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 if (errno != EEXIST)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 clasher = current_lock_owner (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 if (clasher != 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 if (clasher != getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 return (clasher);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 else return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 /* Try again to lock it */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 /* Return the pid of the process that claims to own the lock file LFNAME,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 or 0 if nobody does or the lock is obsolete,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 or -1 if something is wrong with the locking mechanism. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 current_lock_owner (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 int owner = current_lock_owner_1 (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 if (owner == 0 && errno == ENOENT)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 /* Is it locked by a process that exists? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 if (owner != 0 && (kill (owner, 0) >= 0 || errno == EPERM))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 return (owner);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 if (unlink (lfname) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 return (-1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 current_lock_owner_1 (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 REGISTER int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 char buf[20];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 int tem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 fd = open (lfname, O_RDONLY, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 if (fd < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 tem = read (fd, buf, sizeof buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 return (tem <= 0 ? 0 : atoi (buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 unlock_file (Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 if (NILP (Vlock_directory) || NILP (Vsuperlock_file)) return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 CHECK_STRING (Vsuperlock_file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 lock_superlock (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 if (current_lock_owner_1 (lfname) == getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 unlink (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
353 unlink ((char *) XSTRING_DATA (Vsuperlock_file));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 lock_superlock (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 REGISTER int i, fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 DIR *lockdir;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 for (i = -20; i < 0 &&
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
363 (fd = open ((char *) XSTRING_DATA (Vsuperlock_file),
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 if (errno != EEXIST)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 /* This seems to be necessary to prevent Emacs from hanging when the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 competing process has already deleted the superlock, but it's still
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 in the NFS cache. So we force NFS to synchronize the cache. */
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
373 lockdir = opendir ((char *) XSTRING_DATA (Vlock_directory));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 if (lockdir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 closedir (lockdir);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 emacs_sleep (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 if (fd >= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 #ifdef USG
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
382 chmod ((char *) XSTRING_DATA (Vsuperlock_file), 0666);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 fchmod (fd, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 write (fd, lfname, strlen (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 unlock_all_files (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 REGISTER Lisp_Object tail;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 REGISTER struct buffer *b;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 for (tail = Vbuffer_alist; GC_CONSP (tail);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 tail = XCDR (tail))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 b = XBUFFER (XCDR (XCAR (tail)));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 if (STRINGP (b->file_truename) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 BUF_SAVE_MODIFF (b) < BUF_MODIFF (b))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 unlock_file (b->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
408 DEFUN ("lock-buffer", Flock_buffer, Slock_buffer, 0, 1, 0 /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 Lock FILE, if current buffer is modified.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 FILE defaults to current buffer's visited file,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 or else nothing is done if current buffer isn't visiting a file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 */ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 (fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 Lisp_Object fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 /* This function can GC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 if (NILP (fn))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 fn = current_buffer->file_truename;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 && !NILP (fn))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 lock_file (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
426 DEFUN ("unlock-buffer", Funlock_buffer, Sunlock_buffer, 0, 0, 0 /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 Unlock the file visited in the current buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 if it should normally be locked.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 */ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 && STRINGP (current_buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 unlock_file (current_buffer->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 /* Unlock the file visited in buffer BUFFER. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 unlock_buffer (struct buffer *buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 if (BUF_SAVE_MODIFF (buffer) < BUF_MODIFF (buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 && STRINGP (buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 unlock_file (buffer->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 DEFUN ("file-locked-p", Ffile_locked_p, Sfile_locked_p, 0, 1, 0 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 Return nil if the FILENAME is not locked,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 t if it is locked by you, else a string of the name of the locker.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 */ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 (fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 Lisp_Object fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 /* This function can GC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 int owner;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 if (NILP (Vlock_directory) || NILP (Vsuperlock_file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 owner = current_lock_owner (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 if (owner <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 return (Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 else if (owner == getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 return (Qt);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 return (lock_file_owner_name (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 syms_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 /* This function can GC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 defsubr (&Sunlock_buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 defsubr (&Slock_buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 defsubr (&Sfile_locked_p);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 defsymbol (&Qask_user_about_supersession_threat,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 "ask-user-about-supersession-threat");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 defsymbol (&Qask_user_about_lock, "ask-user-about-lock");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 vars_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 DEFVAR_LISP ("lock-directory", &Vlock_directory /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 Don't change this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 */ );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 DEFVAR_LISP ("superlock-file", &Vsuperlock_file /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 Don't change this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 */ );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 complex_vars_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 #ifdef PATH_LOCK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 Vlock_directory =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 Ffile_name_as_directory (build_string (PATH_LOCK));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 Vlock_directory = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 #ifdef PATH_SUPERLOCK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 Vsuperlock_file =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 Ffile_name_as_directory (build_string (PATH_SUPERLOCK));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 Vsuperlock_file = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 /* All the rest done dynamically by startup.el */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 #endif /* CLASH_DETECTION */