annotate src/filelock.c @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Copyright (C) 1985, 1986, 1987, 1992, 1993, 1994
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 /* Synched up with: FSF 19.30. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 #include "lisp.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 #include "buffer.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 #include "paths.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 #include "sysfile.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 #include "sysdir.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 #include "syspwd.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 #include "syssignal.h" /* for kill */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 #ifdef CLASH_DETECTION
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 /* FSFmacs uses char *lock_dir and char *superlock_file instead of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 the Lisp variables we use. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 /* The name of the directory in which we keep lock files, with a '/'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 appended. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 Lisp_Object Vlock_directory;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 #if 0 /* FSFmacs */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 /* Look in startup.el */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 /* The name of the file in the lock directory which is used to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 arbitrate access to the entire directory. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 #define SUPERLOCK_NAME "!!!SuperLock!!!"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 /* The name of the superlock file. This is SUPERLOCK_NAME appended to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 Vlock_directory. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 Lisp_Object Vsuperlock_file;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 Lisp_Object Qask_user_about_supersession_threat;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 Lisp_Object Qask_user_about_lock;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 static void lock_superlock (CONST char *lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 static int lock_file_1 (CONST char *lfname, int mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 static int lock_if_free (CONST char *lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 static int current_lock_owner (CONST char *);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 static int current_lock_owner_1 (CONST char *);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 /* Set LOCK to the name of the lock file for the filename FILE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 char *LOCK; Lisp_Object FILE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 MAKE_LOCK_NAME assumes you have already verified that Vlock_directory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 is a string. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 #ifndef HAVE_LONG_FILE_NAMES
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 #define MAKE_LOCK_NAME(lock, file) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (lock = (char *) alloca (14 + string_length (XSTRING (Vlock_directory)) + \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 1), \
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);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 for (p = string_data (XSTRING (fn)); new = *p++; )
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 / */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 if (string_byte (XSTRING (Vlock_directory),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 string_length (XSTRING (Vlock_directory)) - 1) != '/')
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",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (char *) string_data (XSTRING (Vlock_directory)),
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 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 #define MAKE_LOCK_NAME(lock, file) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (lock = (char *) alloca (string_length (XSTRING (file)) + \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 string_length (XSTRING (Vlock_directory)) + 2), \
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 strcpy (lockfile, (char *) string_data (XSTRING (Vlock_directory)));
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 strcpy (p, (char *) string_data (XSTRING (fn)));
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 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 /* This function can GC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 REGISTER Lisp_Object attack, orig_fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 struct gcpro gcpro1, gcpro2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 Lisp_Object subject_buf = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 if (NILP (Vlock_directory) || NILP (Vsuperlock_file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 GCPRO2 (fn, subject_buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 orig_fn = fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 /* Create the name of the lock-file for file fn */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 /* 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
219 visited. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 subject_buf = Fget_file_buffer (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 if (!NILP (subject_buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 && NILP (Fverify_visited_file_modtime (subject_buf))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 && !NILP (Ffile_exists_p (fn)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 call1_in_buffer (XBUFFER (subject_buf),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 Qask_user_about_supersession_threat, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 /* Try to lock the lock. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 if (lock_if_free (lfname) <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 /* 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
230 goto done;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 /* Else consider breaking the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 attack = call2_in_buffer (BUFFERP (subject_buf) ? XBUFFER (subject_buf) :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 current_buffer, Qask_user_about_lock, fn,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 lock_file_owner_name (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 if (!NILP (attack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 /* User says take the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 CHECK_STRING (Vsuperlock_file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 lock_superlock (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 lock_file_1 (lfname, O_WRONLY);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 unlink ((char *) string_data (XSTRING (Vsuperlock_file)));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 goto done;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 /* User says ignore the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 done:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 UNGCPRO;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 /* Lock the lock file named LFNAME.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 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
253 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
254 Return 1 if successful, 0 if not. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 lock_file_1 (CONST char *lfname, int mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 REGISTER int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 char buf[20];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 if ((fd = open (lfname, mode, 0666)) >= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 #ifdef USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 chmod (lfname, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 fchmod (fd, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 sprintf (buf, "%ld ", (long) getpid ());
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 write (fd, buf, strlen (buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 return 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 /* Lock the lock named LFNAME if possible.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 Return 0 in that case.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 Return positive if lock is really locked by someone else.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 Return -1 if cannot lock for any other reason. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 lock_if_free (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 REGISTER int clasher;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 while (lock_file_1 (lfname, O_WRONLY | O_EXCL | O_CREAT) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 if (errno != EEXIST)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 clasher = current_lock_owner (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 if (clasher != 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 if (clasher != getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 return (clasher);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 else return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 /* Try again to lock it */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 /* Return the pid of the process that claims to own the lock file LFNAME,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 or 0 if nobody does or the lock is obsolete,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 or -1 if something is wrong with the locking mechanism. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 current_lock_owner (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 int owner = current_lock_owner_1 (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 if (owner == 0 && errno == ENOENT)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 /* Is it locked by a process that exists? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 if (owner != 0 && (kill (owner, 0) >= 0 || errno == EPERM))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 return (owner);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 if (unlink (lfname) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 return (-1);
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 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 current_lock_owner_1 (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 REGISTER int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 char buf[20];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 int tem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 fd = open (lfname, O_RDONLY, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 if (fd < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 tem = read (fd, buf, sizeof buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 return (tem <= 0 ? 0 : atoi (buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 unlock_file (Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 if (NILP (Vlock_directory) || NILP (Vsuperlock_file)) return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 CHECK_STRING (Vsuperlock_file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 lock_superlock (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 if (current_lock_owner_1 (lfname) == getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 unlink (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 unlink ((char *) string_data (XSTRING (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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 lock_superlock (CONST char *lfname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 REGISTER int i, fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 DIR *lockdir;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 for (i = -20; i < 0 &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 (fd = open ((char *) string_data (XSTRING (Vsuperlock_file)),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 if (errno != EEXIST)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 /* This seems to be necessary to prevent Emacs from hanging when the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 competing process has already deleted the superlock, but it's still
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 in the NFS cache. So we force NFS to synchronize the cache. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 lockdir = opendir ((char *) string_data (XSTRING (Vlock_directory)));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 if (lockdir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 closedir (lockdir);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 emacs_sleep (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 if (fd >= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 #ifdef USG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 chmod ((char *) string_data (XSTRING (Vsuperlock_file)), 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 fchmod (fd, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 write (fd, lfname, strlen (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 unlock_all_files (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 REGISTER Lisp_Object tail;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 REGISTER struct buffer *b;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 for (tail = Vbuffer_alist; GC_CONSP (tail);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 tail = XCDR (tail))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 b = XBUFFER (XCDR (XCAR (tail)));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 if (STRINGP (b->file_truename) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 BUF_SAVE_MODIFF (b) < BUF_MODIFF (b))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 unlock_file (b->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 DEFUN ("lock-buffer", Flock_buffer, Slock_buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 0, 1, 0 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 Lock FILE, if current buffer is modified.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 FILE defaults to current buffer's visited file,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 or else nothing is done if current buffer isn't visiting a file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 */ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 (fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 Lisp_Object fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 /* This function can GC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 if (NILP (fn))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 fn = current_buffer->file_truename;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 CHECK_STRING (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 && !NILP (fn))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 lock_file (fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 DEFUN ("unlock-buffer", Funlock_buffer, Sunlock_buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 0, 0, 0 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 Unlock the file visited in the current buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 if it should normally be locked.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 */ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 && STRINGP (current_buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 unlock_file (current_buffer->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 /* Unlock the file visited in buffer BUFFER. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 unlock_buffer (struct buffer *buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 if (BUF_SAVE_MODIFF (buffer) < BUF_MODIFF (buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 && STRINGP (buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 unlock_file (buffer->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 DEFUN ("file-locked-p", Ffile_locked_p, Sfile_locked_p, 0, 1, 0 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 Return nil if the FILENAME is not locked,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 t if it is locked by you, else a string of the name of the locker.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 */ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 (fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 Lisp_Object fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 /* This function can GC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 REGISTER char *lfname;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 int owner;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 if (NILP (Vlock_directory) || NILP (Vsuperlock_file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 CHECK_STRING (Vlock_directory);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 owner = current_lock_owner (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 if (owner <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 return (Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 else if (owner == getpid ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 return (Qt);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 return (lock_file_owner_name (lfname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 syms_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 /* This function can GC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 defsubr (&Sunlock_buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 defsubr (&Slock_buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 defsubr (&Sfile_locked_p);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 defsymbol (&Qask_user_about_supersession_threat,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 "ask-user-about-supersession-threat");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 defsymbol (&Qask_user_about_lock, "ask-user-about-lock");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 vars_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 DEFVAR_LISP ("lock-directory", &Vlock_directory /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 Don't change this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 */ );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 DEFVAR_LISP ("superlock-file", &Vsuperlock_file /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 Don't change this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 */ );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 complex_vars_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 #ifdef PATH_LOCK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 Vlock_directory =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 Ffile_name_as_directory (build_string (PATH_LOCK));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 Vlock_directory = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 #ifdef PATH_SUPERLOCK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 Vsuperlock_file =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 Ffile_name_as_directory (build_string (PATH_SUPERLOCK));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 Vsuperlock_file = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 /* All the rest done dynamically by startup.el */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 #endif /* CLASH_DETECTION */