annotate src/filelock.c @ 191:ecf6ba7b0a10 r20-3b22

Import from CVS: tag r20-3b22
author cvs
date Mon, 13 Aug 2007 09:57:38 +0200
parents 3d6bfa290dbd
children 966663fcf606
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
175
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 159
diff changeset
34 #ifndef CLASH_DETECTION
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 159
diff changeset
35 #error CLASH_DETECTION is not defined??
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 159
diff changeset
36 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 /* FSFmacs uses char *lock_dir and char *superlock_file instead of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 the Lisp variables we use. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 /* The name of the directory in which we keep lock files, with a '/'
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 175
diff changeset
42 appended. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 Lisp_Object Vlock_directory;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 #if 0 /* FSFmacs */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 /* Look in startup.el */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 /* The name of the file in the lock directory which is used to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 arbitrate access to the entire directory. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 #define SUPERLOCK_NAME "!!!SuperLock!!!"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 /* The name of the superlock file. This is SUPERLOCK_NAME appended to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 Vlock_directory. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 Lisp_Object Vsuperlock_file;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 Lisp_Object Qask_user_about_supersession_threat;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 Lisp_Object Qask_user_about_lock;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 static void lock_superlock (CONST char *lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 static int lock_file_1 (CONST char *lfname, int mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 static int lock_if_free (CONST char *lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 static int current_lock_owner (CONST char *);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 static int current_lock_owner_1 (CONST char *);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 /* Set LOCK to the name of the lock file for the filename FILE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 char *LOCK; Lisp_Object FILE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 MAKE_LOCK_NAME assumes you have already verified that Vlock_directory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 is a string. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 #ifndef HAVE_LONG_FILE_NAMES
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72
80
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
73 #define MAKE_LOCK_NAME(lock, file) \
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
74 (lock = (char *) alloca (14 + XSTRING_LENGTH (Vlock_directory) + 1), \
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 fill_in_lock_short_file_name (lock, (file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 fill_in_lock_short_file_name (REGISTER char *lockfile, REGISTER Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 REGISTER union
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 unsigned int word [2];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 unsigned char byte [8];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 } crc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 REGISTER unsigned char *p, new;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 CHECK_STRING (Vlock_directory);
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 175
diff changeset
88
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 /* 7-bytes cyclic code for burst correction on byte-by-byte basis.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 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
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 crc.word[0] = crc.word[1] = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
94 for (p = XSTRING_DATA (fn); new = *p++; )
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 new += crc.byte[6];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 crc.byte[6] = crc.byte[5] + new;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 crc.byte[5] = crc.byte[4];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 crc.byte[4] = crc.byte[3];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 crc.byte[3] = crc.byte[2] + new;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 crc.byte[2] = crc.byte[1];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 crc.byte[1] = crc.byte[0];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 crc.byte[0] = new;
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 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 int need_slash = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 /* in case lock-directory doesn't end in / */
80
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
110 if (XSTRING_BYTE (Vlock_directory,
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
111 XSTRING_LENGTH (Vlock_directory) - 1) != '/')
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 need_slash = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 sprintf (lockfile, "%s%s%.2x%.2x%.2x%.2x%.2x%.2x%.2x",
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
115 (char *) XSTRING_DATA (Vlock_directory),
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 need_slash ? "/" : "",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 crc.byte[0], crc.byte[1], crc.byte[2], crc.byte[3],
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 crc.byte[4], crc.byte[5], crc.byte[6]);
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 #else /* defined HAVE_LONG_FILE_NAMES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 /* +2 for terminating null and possible extra slash */
80
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
125 #define MAKE_LOCK_NAME(lock, file) \
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
126 (lock = (char *) alloca (XSTRING_LENGTH (file) + \
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
127 XSTRING_LENGTH (Vlock_directory) + 2), \
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 fill_in_lock_file_name (lock, (file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 fill_in_lock_file_name (REGISTER char *lockfile, REGISTER Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 /* fn must be a Lisp_String! */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 REGISTER char *p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
138 strcpy (lockfile, (char *) XSTRING_DATA (Vlock_directory));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 p = lockfile + strlen (lockfile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 if (p == lockfile /* lock-directory is empty?? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 || *(p - 1) != '/') /* in case lock-directory doesn't end in / */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 {
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 p++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
149 strcpy (p, (char *) XSTRING_DATA (fn));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 for (; *p; p++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 if (*p == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 *p = '!';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 #endif /* !defined HAVE_LONG_FILE_NAMES */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 static Lisp_Object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 lock_file_owner_name (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 struct stat s;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 struct passwd *the_pw = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 if (lstat (lfname, &s) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 the_pw = getpwuid (s.st_uid);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 return (the_pw == 0 ? Qnil : build_string (the_pw->pw_name));
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 /* lock_file locks file fn,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 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
173 This should be done only when about to modify a file-visiting
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 buffer previously unmodified.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 Do not (normally) call lock_buffer for a buffer already modified,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 as either the file is already locked, or the user has already
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 decided to go ahead without locking.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 When lock_buffer returns, either the lock is locked for us,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 or the user has said to go ahead without locking.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 If the file is locked by someone else, lock_buffer calls
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 ask-user-about-lock (a Lisp function) with two arguments,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 the file name and the name of the user who did the locking.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 This function can signal an error, or return t meaning
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 take away the lock, or return nil meaning ignore the lock. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 /* The lock file name is the file name with "/" replaced by "!"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 and put in the Emacs lock directory. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 /* (ie., /ka/king/junk.tex -> /!/!ka!king!junk.tex). */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 /* 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
193 representation of a 14-bytes CRC generated from the file name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 and put in the Emacs lock directory (not very nice, but it works).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 (ie., /ka/king/junk.tex -> /!/12a82c62f1c6da). */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 lock_file (Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
200 /* This function can GC. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
201 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
202 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
203 you protect things right. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
204
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 REGISTER Lisp_Object attack, orig_fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 struct gcpro gcpro1, gcpro2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 Lisp_Object subject_buf = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 if (NILP (Vlock_directory) || NILP (Vsuperlock_file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 GCPRO2 (fn, subject_buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 orig_fn = fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 /* Create the name of the lock-file for file fn */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 /* 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
223 visited. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 subject_buf = Fget_file_buffer (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 if (!NILP (subject_buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 && NILP (Fverify_visited_file_modtime (subject_buf))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 && !NILP (Ffile_exists_p (fn)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 call1_in_buffer (XBUFFER (subject_buf),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 Qask_user_about_supersession_threat, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 /* Try to lock the lock. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 if (lock_if_free (lfname) <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 /* 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
234 goto done;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 /* Else consider breaking the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 attack = call2_in_buffer (BUFFERP (subject_buf) ? XBUFFER (subject_buf) :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 current_buffer, Qask_user_about_lock, fn,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 lock_file_owner_name (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 if (!NILP (attack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 /* User says take the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 CHECK_STRING (Vsuperlock_file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 lock_superlock (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 lock_file_1 (lfname, O_WRONLY);
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
246 unlink ((char *) XSTRING_DATA (Vsuperlock_file));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 goto done;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 /* User says ignore the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 done:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 UNGCPRO;
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 /* Lock the lock file named LFNAME.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 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
257 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
258 Return 1 if successful, 0 if not. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 lock_file_1 (CONST char *lfname, int mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 REGISTER int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 char buf[20];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 if ((fd = open (lfname, mode, 0666)) >= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 {
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents: 80
diff changeset
268 #if defined(WINDOWSNT)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents: 80
diff changeset
269 chmod(lfname, _S_IREAD|_S_IWRITE);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents: 80
diff changeset
270 #elif defined(USG)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 chmod (lfname, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 fchmod (fd, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 sprintf (buf, "%ld ", (long) getpid ());
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 write (fd, buf, strlen (buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 return 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 /* Lock the lock named LFNAME if possible.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 Return 0 in that case.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 Return positive if lock is really locked by someone else.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 Return -1 if cannot lock for any other reason. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 lock_if_free (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 REGISTER int clasher;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 while (lock_file_1 (lfname, O_WRONLY | O_EXCL | O_CREAT) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 if (errno != EEXIST)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 clasher = current_lock_owner (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 if (clasher != 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 if (clasher != getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 return (clasher);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 else return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 /* Try again to lock it */
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 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 /* 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
309 or 0 if nobody does or the lock is obsolete,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 or -1 if something is wrong with the locking mechanism. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 current_lock_owner (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 int owner = current_lock_owner_1 (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 if (owner == 0 && errno == ENOENT)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 /* Is it locked by a process that exists? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 if (owner != 0 && (kill (owner, 0) >= 0 || errno == EPERM))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 return (owner);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 if (unlink (lfname) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 return (-1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 current_lock_owner_1 (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 REGISTER int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 char buf[20];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 int tem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 fd = open (lfname, O_RDONLY, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 if (fd < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 tem = read (fd, buf, sizeof buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 return (tem <= 0 ? 0 : atoi (buf));
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 unlock_file (Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
345 /* This function can GC. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
346 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
347 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
348 you protect things right. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
349
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 if (NILP (Vlock_directory) || NILP (Vsuperlock_file)) return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 CHECK_STRING (Vsuperlock_file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 lock_superlock (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 if (current_lock_owner_1 (lfname) == getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 unlink (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
365 unlink ((char *) XSTRING_DATA (Vsuperlock_file));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 lock_superlock (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 REGISTER int i, fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 DIR *lockdir;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 for (i = -20; i < 0 &&
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
375 (fd = open ((char *) XSTRING_DATA (Vsuperlock_file),
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 if (errno != EEXIST)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 /* This seems to be necessary to prevent Emacs from hanging when the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 competing process has already deleted the superlock, but it's still
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 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
385 lockdir = opendir ((char *) XSTRING_DATA (Vlock_directory));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 if (lockdir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 closedir (lockdir);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 emacs_sleep (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 if (fd >= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 {
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents: 80
diff changeset
393 #if defined(WINDOWSNT)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents: 80
diff changeset
394 chmod(lfname, _S_IREAD|_S_IWRITE);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents: 80
diff changeset
395 #elif defined(USG)
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents: 2
diff changeset
396 chmod ((char *) XSTRING_DATA (Vsuperlock_file), 0666);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 fchmod (fd, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 write (fd, lfname, strlen (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 close (fd);
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 unlock_all_files (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
408 /* This function can GC. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
409
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
410 Lisp_Object tail;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 REGISTER struct buffer *b;
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
412 struct gcpro gcpro1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
414 GCPRO1 (tail);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 for (tail = Vbuffer_alist; GC_CONSP (tail);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 tail = XCDR (tail))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 b = XBUFFER (XCDR (XCAR (tail)));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 if (STRINGP (b->file_truename) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 BUF_SAVE_MODIFF (b) < BUF_MODIFF (b))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 unlock_file (b->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 }
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
423 UNGCPRO;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
427 DEFUN ("lock-buffer", Flock_buffer, 0, 1, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 Lock FILE, if current buffer is modified.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 FILE defaults to current buffer's visited file,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 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
431 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
432 (fn))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 /* This function can GC */
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
435 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
436 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
437 you protect things right. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
438
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 if (NILP (fn))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 fn = current_buffer->file_truename;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 && !NILP (fn))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 lock_file (fn);
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 175
diff changeset
445 return Qnil;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
448 DEFUN ("unlock-buffer", Funlock_buffer, 0, 0, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 Unlock the file visited in the current buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 if it should normally be locked.
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
451 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
452 ())
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
454 /* This function can GC */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
455 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
456 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
457 you protect things right. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
458
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 && STRINGP (current_buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 unlock_file (current_buffer->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 return Qnil;
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 /* Unlock the file visited in buffer BUFFER. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 unlock_buffer (struct buffer *buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
471 /* This function can GC */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
472 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
473 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
474 you protect things right. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 if (BUF_SAVE_MODIFF (buffer) < BUF_MODIFF (buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 && STRINGP (buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 unlock_file (buffer->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
480 DEFUN ("file-locked-p", Ffile_locked_p, 0, 1, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 Return nil if the FILENAME is not locked,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 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
483 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
484 (fn))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 /* This function can GC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 int owner;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 if (NILP (Vlock_directory) || NILP (Vsuperlock_file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 owner = current_lock_owner (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 if (owner <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 return (Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 else if (owner == getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 return (Qt);
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 175
diff changeset
503
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 return (lock_file_owner_name (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 syms_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 /* This function can GC */
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
511 DEFSUBR (Funlock_buffer);
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
512 DEFSUBR (Flock_buffer);
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
513 DEFSUBR (Ffile_locked_p);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 defsymbol (&Qask_user_about_supersession_threat,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 "ask-user-about-supersession-threat");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 defsymbol (&Qask_user_about_lock, "ask-user-about-lock");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 vars_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 DEFVAR_LISP ("lock-directory", &Vlock_directory /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 Don't change this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 */ );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 DEFVAR_LISP ("superlock-file", &Vsuperlock_file /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 Don't change this
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 complex_vars_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 #ifdef PATH_LOCK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 Vlock_directory =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 Ffile_name_as_directory (build_string (PATH_LOCK));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 Vlock_directory = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 #ifdef PATH_SUPERLOCK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 Vsuperlock_file =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 Ffile_name_as_directory (build_string (PATH_SUPERLOCK));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 Vsuperlock_file = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 /* All the rest done dynamically by startup.el */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 }