annotate src/filelock.c @ 68:0b7f3b634cf2 r19-16-pre9

Import from CVS: tag r19-16-pre9
author cvs
date Mon, 13 Aug 2007 09:00:03 +0200
parents 7e54bd776075
children 131b0175ea99
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 {
40
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
199 /* This function can GC. */
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
200 /* dmoore - and can destroy current_buffer and all sorts of other
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
201 mean nasty things with pointy teeth. If you call this make sure
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
202 you protect things right. */
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
203
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 REGISTER Lisp_Object attack, orig_fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 struct gcpro gcpro1, gcpro2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 Lisp_Object subject_buf = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 if (NILP (Vlock_directory) || NILP (Vsuperlock_file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 GCPRO2 (fn, subject_buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 orig_fn = fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 /* Create the name of the lock-file for file fn */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 /* 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
222 visited. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 subject_buf = Fget_file_buffer (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 if (!NILP (subject_buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 && NILP (Fverify_visited_file_modtime (subject_buf))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 && !NILP (Ffile_exists_p (fn)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 call1_in_buffer (XBUFFER (subject_buf),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 Qask_user_about_supersession_threat, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 /* Try to lock the lock. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 if (lock_if_free (lfname) <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 /* 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
233 goto done;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 /* Else consider breaking the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 attack = call2_in_buffer (BUFFERP (subject_buf) ? XBUFFER (subject_buf) :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 current_buffer, Qask_user_about_lock, fn,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 lock_file_owner_name (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 if (!NILP (attack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 /* User says take the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 CHECK_STRING (Vsuperlock_file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 lock_superlock (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 lock_file_1 (lfname, O_WRONLY);
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
245 unlink ((char *) XSTRING_DATA (Vsuperlock_file));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 goto done;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 /* User says ignore the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 done:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 UNGCPRO;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 /* Lock the lock file named LFNAME.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 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
256 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
257 Return 1 if successful, 0 if not. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 lock_file_1 (CONST char *lfname, int mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 REGISTER int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 char buf[20];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 if ((fd = open (lfname, mode, 0666)) >= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 #ifdef USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 chmod (lfname, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 fchmod (fd, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 sprintf (buf, "%ld ", (long) getpid ());
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 write (fd, buf, strlen (buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 return 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 /* Lock the lock named LFNAME if possible.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 Return 0 in that case.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 Return positive if lock is really locked by someone else.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 Return -1 if cannot lock for any other reason. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 lock_if_free (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 REGISTER int clasher;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 while (lock_file_1 (lfname, O_WRONLY | O_EXCL | O_CREAT) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 if (errno != EEXIST)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 clasher = current_lock_owner (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 if (clasher != 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 if (clasher != getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 return (clasher);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 else return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 /* Try again to lock it */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 /* 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
306 or 0 if nobody does or the lock is obsolete,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 or -1 if something is wrong with the locking mechanism. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 current_lock_owner (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 int owner = current_lock_owner_1 (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 if (owner == 0 && errno == ENOENT)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 /* Is it locked by a process that exists? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 if (owner != 0 && (kill (owner, 0) >= 0 || errno == EPERM))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 return (owner);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 if (unlink (lfname) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 return (-1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 current_lock_owner_1 (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 REGISTER int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 char buf[20];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 int tem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 fd = open (lfname, O_RDONLY, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 if (fd < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 tem = read (fd, buf, sizeof buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 return (tem <= 0 ? 0 : atoi (buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 unlock_file (Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 {
40
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
342 /* This function can GC. */
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
343 /* dmoore - and can destroy current_buffer and all sorts of other
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
344 mean nasty things with pointy teeth. If you call this make sure
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
345 you protect things right. */
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
346
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 if (NILP (Vlock_directory) || NILP (Vsuperlock_file)) return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 CHECK_STRING (Vsuperlock_file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 lock_superlock (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 if (current_lock_owner_1 (lfname) == getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 unlink (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
362 unlink ((char *) XSTRING_DATA (Vsuperlock_file));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 lock_superlock (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 REGISTER int i, fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 DIR *lockdir;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 for (i = -20; i < 0 &&
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
372 (fd = open ((char *) XSTRING_DATA (Vsuperlock_file),
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 if (errno != EEXIST)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 /* This seems to be necessary to prevent Emacs from hanging when the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 competing process has already deleted the superlock, but it's still
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 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
382 lockdir = opendir ((char *) XSTRING_DATA (Vlock_directory));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 if (lockdir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 closedir (lockdir);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 emacs_sleep (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 if (fd >= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 #ifdef USG
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
391 chmod ((char *) XSTRING_DATA (Vsuperlock_file), 0666);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 fchmod (fd, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 write (fd, lfname, strlen (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 unlock_all_files (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 {
40
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
403 /* This function can GC. */
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
404
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
405 Lisp_Object tail;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 REGISTER struct buffer *b;
40
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
407 struct gcpro gcpro1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408
40
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
409 GCPRO1 (tail);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 for (tail = Vbuffer_alist; GC_CONSP (tail);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 tail = XCDR (tail))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 b = XBUFFER (XCDR (XCAR (tail)));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 if (STRINGP (b->file_truename) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 BUF_SAVE_MODIFF (b) < BUF_MODIFF (b))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 unlock_file (b->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 }
40
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
418 UNGCPRO;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
422 DEFUN ("lock-buffer", Flock_buffer, 0, 1, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 Lock FILE, if current buffer is modified.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 FILE defaults to current buffer's visited file,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 or else nothing is done if current buffer isn't visiting a file.
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
426 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
427 (fn))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 /* This function can GC */
40
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
430 /* dmoore - and can destroy current_buffer and all sorts of other
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
431 mean nasty things with pointy teeth. If you call this make sure
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
432 you protect things right. */
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
433
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 if (NILP (fn))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 fn = current_buffer->file_truename;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 && !NILP (fn))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 lock_file (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
443 DEFUN ("unlock-buffer", Funlock_buffer, 0, 0, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 Unlock the file visited in the current buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 if it should normally be locked.
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
446 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
447 ())
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 {
40
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
449 /* This function can GC */
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
450 /* dmoore - and can destroy current_buffer and all sorts of other
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
451 mean nasty things with pointy teeth. If you call this make sure
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
452 you protect things right. */
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
453
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 && STRINGP (current_buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 unlock_file (current_buffer->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 /* Unlock the file visited in buffer BUFFER. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 unlock_buffer (struct buffer *buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 {
40
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
466 /* This function can GC */
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
467 /* dmoore - and can destroy current_buffer and all sorts of other
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
468 mean nasty things with pointy teeth. If you call this make sure
7e54bd776075 Import from CVS: tag r19-15b103
cvs
parents: 20
diff changeset
469 you protect things right. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 if (BUF_SAVE_MODIFF (buffer) < BUF_MODIFF (buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 && STRINGP (buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 unlock_file (buffer->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
475 DEFUN ("file-locked-p", Ffile_locked_p, 0, 1, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 Return nil if the FILENAME is not locked,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 t if it is locked by you, else a string of the name of the locker.
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
478 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
479 (fn))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 /* This function can GC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 int owner;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 if (NILP (Vlock_directory) || NILP (Vsuperlock_file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 owner = current_lock_owner (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 if (owner <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 return (Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 else if (owner == getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 return (Qt);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 return (lock_file_owner_name (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 syms_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 /* This function can GC */
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
506 DEFSUBR (Funlock_buffer);
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
507 DEFSUBR (Flock_buffer);
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
508 DEFSUBR (Ffile_locked_p);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 defsymbol (&Qask_user_about_supersession_threat,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 "ask-user-about-supersession-threat");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 defsymbol (&Qask_user_about_lock, "ask-user-about-lock");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 vars_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 DEFVAR_LISP ("lock-directory", &Vlock_directory /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 Don't change this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 */ );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 DEFVAR_LISP ("superlock-file", &Vsuperlock_file /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 Don't change this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 */ );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 complex_vars_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 #ifdef PATH_LOCK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 Vlock_directory =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 Ffile_name_as_directory (build_string (PATH_LOCK));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 Vlock_directory = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 #ifdef PATH_SUPERLOCK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 Vsuperlock_file =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 Ffile_name_as_directory (build_string (PATH_SUPERLOCK));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 Vsuperlock_file = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 /* All the rest done dynamically by startup.el */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 #endif /* CLASH_DETECTION */