annotate src/filelock.c @ 159:3bb7ccffb0c0 r20-3b6

Import from CVS: tag r20-3b6
author cvs
date Mon, 13 Aug 2007 09:41:43 +0200
parents 8619ce7e4c50
children 2d532a89d707
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"
159
3bb7ccffb0c0 Import from CVS: tag r20-3b6
cvs
parents: 114
diff changeset
27 #include <paths.h>
0
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
80
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
72 #define MAKE_LOCK_NAME(lock, file) \
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
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 / */
80
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
109 if (XSTRING_BYTE (Vlock_directory,
14
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 */
80
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
124 #define MAKE_LOCK_NAME(lock, file) \
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
125 (lock = (char *) alloca (XSTRING_LENGTH (file) + \
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
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 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
199 /* This function can GC. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
200 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
201 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
202 you protect things right. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
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 {
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents: 80
diff changeset
267 #if defined(WINDOWSNT)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents: 80
diff changeset
268 chmod(lfname, _S_IREAD|_S_IWRITE);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents: 80
diff changeset
269 #elif defined(USG)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 chmod (lfname, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 fchmod (fd, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 sprintf (buf, "%ld ", (long) getpid ());
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 write (fd, buf, strlen (buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 return 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 /* Lock the lock named LFNAME if possible.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 Return 0 in that case.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 Return positive if lock is really locked by someone else.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 Return -1 if cannot lock for any other reason. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 lock_if_free (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 REGISTER int clasher;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 while (lock_file_1 (lfname, O_WRONLY | O_EXCL | O_CREAT) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 if (errno != EEXIST)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 clasher = current_lock_owner (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 if (clasher != 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 if (clasher != getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 return (clasher);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 else return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 /* Try again to lock it */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 /* 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
308 or 0 if nobody does or the lock is obsolete,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 or -1 if something is wrong with the locking mechanism. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 current_lock_owner (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 int owner = current_lock_owner_1 (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 if (owner == 0 && errno == ENOENT)
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 /* Is it locked by a process that exists? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 if (owner != 0 && (kill (owner, 0) >= 0 || errno == EPERM))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 return (owner);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 if (unlink (lfname) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 return (-1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 current_lock_owner_1 (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 REGISTER int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 char buf[20];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 int tem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 fd = open (lfname, O_RDONLY, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 if (fd < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 tem = read (fd, buf, sizeof buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 return (tem <= 0 ? 0 : atoi (buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 unlock_file (Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
344 /* This function can GC. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
345 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
346 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
347 you protect things right. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
348
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 if (NILP (Vlock_directory) || NILP (Vsuperlock_file)) return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 CHECK_STRING (Vsuperlock_file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 lock_superlock (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 if (current_lock_owner_1 (lfname) == getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 unlink (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
364 unlink ((char *) XSTRING_DATA (Vsuperlock_file));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 lock_superlock (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 REGISTER int i, fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 DIR *lockdir;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 for (i = -20; i < 0 &&
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
374 (fd = open ((char *) XSTRING_DATA (Vsuperlock_file),
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 if (errno != EEXIST)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 /* This seems to be necessary to prevent Emacs from hanging when the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 competing process has already deleted the superlock, but it's still
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 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
384 lockdir = opendir ((char *) XSTRING_DATA (Vlock_directory));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 if (lockdir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 closedir (lockdir);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 emacs_sleep (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 if (fd >= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 {
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents: 80
diff changeset
392 #if defined(WINDOWSNT)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents: 80
diff changeset
393 chmod(lfname, _S_IREAD|_S_IWRITE);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents: 80
diff changeset
394 #elif defined(USG)
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
395 chmod ((char *) XSTRING_DATA (Vsuperlock_file), 0666);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 fchmod (fd, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 write (fd, lfname, strlen (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 unlock_all_files (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
407 /* This function can GC. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
408
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
409 Lisp_Object tail;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 REGISTER struct buffer *b;
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
411 struct gcpro gcpro1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
413 GCPRO1 (tail);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 for (tail = Vbuffer_alist; GC_CONSP (tail);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 tail = XCDR (tail))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 b = XBUFFER (XCDR (XCAR (tail)));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 if (STRINGP (b->file_truename) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 BUF_SAVE_MODIFF (b) < BUF_MODIFF (b))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 unlock_file (b->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 }
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
422 UNGCPRO;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
426 DEFUN ("lock-buffer", Flock_buffer, 0, 1, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 Lock FILE, if current buffer is modified.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 FILE defaults to current buffer's visited file,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 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
430 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
431 (fn))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 /* This function can GC */
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
434 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
435 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
436 you protect things right. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
437
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 if (NILP (fn))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 fn = current_buffer->file_truename;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 && !NILP (fn))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 lock_file (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
447 DEFUN ("unlock-buffer", Funlock_buffer, 0, 0, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 Unlock the file visited in the current buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 if it should normally be locked.
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
450 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
451 ())
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
453 /* This function can GC */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
454 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
455 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
456 you protect things right. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
457
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 && STRINGP (current_buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 unlock_file (current_buffer->file_truename);
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 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 /* Unlock the file visited in buffer BUFFER. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 unlock_buffer (struct buffer *buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
470 /* This function can GC */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
471 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
472 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
473 you protect things right. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 if (BUF_SAVE_MODIFF (buffer) < BUF_MODIFF (buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 && STRINGP (buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 unlock_file (buffer->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
479 DEFUN ("file-locked-p", Ffile_locked_p, 0, 1, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 Return nil if the FILENAME is not locked,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 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
482 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
483 (fn))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 /* This function can GC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 int owner;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 if (NILP (Vlock_directory) || NILP (Vsuperlock_file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 owner = current_lock_owner (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 if (owner <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 return (Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 else if (owner == getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 return (Qt);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 return (lock_file_owner_name (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 syms_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 /* This function can GC */
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
510 DEFSUBR (Funlock_buffer);
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
511 DEFSUBR (Flock_buffer);
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
512 DEFSUBR (Ffile_locked_p);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 defsymbol (&Qask_user_about_supersession_threat,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 "ask-user-about-supersession-threat");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 defsymbol (&Qask_user_about_lock, "ask-user-about-lock");
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 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 vars_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 DEFVAR_LISP ("lock-directory", &Vlock_directory /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 Don't change this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 */ );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 DEFVAR_LISP ("superlock-file", &Vsuperlock_file /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 Don't change this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 */ );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 complex_vars_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 #ifdef PATH_LOCK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 Vlock_directory =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 Ffile_name_as_directory (build_string (PATH_LOCK));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 Vlock_directory = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 #ifdef PATH_SUPERLOCK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 Vsuperlock_file =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 Ffile_name_as_directory (build_string (PATH_SUPERLOCK));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 Vsuperlock_file = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 /* All the rest done dynamically by startup.el */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 #endif /* CLASH_DETECTION */