Mercurial > emacs
annotate src/filelock.c @ 14220:74d7745bcaa4
sp correction
author | Simon Marshall <simon@gnu.org> |
---|---|
date | Thu, 18 Jan 1996 16:10:41 +0000 |
parents | ee40177f6c68 |
children | 24c31fcbcf97 |
rev | line source |
---|---|
7307 | 1 /* Copyright (C) 1985, 1986, 1987, 1993, 1994 Free Software Foundation, Inc. |
163 | 2 |
3 This file is part of GNU Emacs. | |
4 | |
5 GNU Emacs is free software; you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
624 | 7 the Free Software Foundation; either version 2, or (at your option) |
163 | 8 any later version. |
9 | |
10 GNU Emacs is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with GNU Emacs; see the file COPYING. If not, write to | |
14186
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14075
diff
changeset
|
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14075
diff
changeset
|
18 Boston, MA 02111-1307, USA. */ |
163 | 19 |
20 | |
21 #include <sys/types.h> | |
22 #include <sys/stat.h> | |
4696
1fc792473491
Include <config.h> instead of "config.h".
Roland McGrath <roland@gnu.org>
parents:
4680
diff
changeset
|
23 #include <config.h> |
372 | 24 |
25 #ifdef VMS | |
559 | 26 #include "vms-pwd.h" |
372 | 27 #else |
163 | 28 #include <pwd.h> |
372 | 29 #endif |
30 | |
163 | 31 #include <errno.h> |
32 #include <sys/file.h> | |
33 #ifdef USG | |
34 #include <fcntl.h> | |
35 #endif /* USG */ | |
36 | |
37 #include "lisp.h" | |
4701
05f6a91c2801
Include <paths.h>, not "paths.h".
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
38 #include <paths.h> |
163 | 39 #include "buffer.h" |
40 | |
6300
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
41 #ifdef SYSV_SYSTEM_DIR |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
42 #include <dirent.h> |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
43 #else /* not SYSV_SYSTEM_DIR */ |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
44 #ifdef NONSYSTEM_DIR_LIBRARY |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
45 #include "ndir.h" |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
46 #else /* not NONSYSTEM_DIR_LIBRARY */ |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
47 #ifdef MSDOS |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
48 #include <dirent.h> |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
49 #else |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
50 #include <sys/dir.h> |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
51 #endif |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
52 #endif /* not NONSYSTEM_DIR_LIBRARY */ |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
53 #ifndef MSDOS |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
54 extern DIR *opendir (); |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
55 #endif /* not MSDOS */ |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
56 #endif /* not SYSV_SYSTEM_DIR */ |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
57 |
163 | 58 extern int errno; |
59 | |
624 | 60 extern char *egetenv (); |
733 | 61 extern char *strcpy (); |
624 | 62 |
8776
290f584b7bb1
(getpwuid): Test just DECLARE_GETPWUID_WITH_UID_T, not __bsdi__.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
63 #ifdef DECLARE_GETPWUID_WITH_UID_T |
4618
e4a5c21eb300
(getpwuid): Declare it as in xrdb.c.
Richard M. Stallman <rms@gnu.org>
parents:
4275
diff
changeset
|
64 extern struct passwd *getpwuid (uid_t); |
e4a5c21eb300
(getpwuid): Declare it as in xrdb.c.
Richard M. Stallman <rms@gnu.org>
parents:
4275
diff
changeset
|
65 #else |
4272
41c85882768c
(getpwuid): Declare at top level, and not if __386bsd__.
Richard M. Stallman <rms@gnu.org>
parents:
3602
diff
changeset
|
66 extern struct passwd *getpwuid (); |
41c85882768c
(getpwuid): Declare at top level, and not if __386bsd__.
Richard M. Stallman <rms@gnu.org>
parents:
3602
diff
changeset
|
67 #endif |
41c85882768c
(getpwuid): Declare at top level, and not if __386bsd__.
Richard M. Stallman <rms@gnu.org>
parents:
3602
diff
changeset
|
68 |
163 | 69 #ifdef CLASH_DETECTION |
70 | |
71 /* If system does not have symbolic links, it does not have lstat. | |
72 In that case, use ordinary stat instead. */ | |
73 | |
74 #ifndef S_IFLNK | |
75 #define lstat stat | |
76 #endif | |
77 | |
624 | 78 |
79 /* The name of the directory in which we keep lock files, with a '/' | |
80 appended. */ | |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
81 char *lock_dir; |
624 | 82 |
83 /* The name of the file in the lock directory which is used to | |
84 arbitrate access to the entire directory. */ | |
85 #define SUPERLOCK_NAME "!!!SuperLock!!!" | |
86 | |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
87 /* The name of the superlock file. This is SUPERLOCK_NAME appended to |
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
88 lock_dir. */ |
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
89 char *superlock_file; |
624 | 90 |
91 /* Set LOCK to the name of the lock file for the filename FILE. | |
92 char *LOCK; Lisp_Object FILE; */ | |
3537
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
93 |
3602
f71f3f167365
* filelock.c: Test HAVE_LONG_FILE_NAMES, not SHORT_FILE_NAMES, to
Jim Blandy <jimb@redhat.com>
parents:
3537
diff
changeset
|
94 #ifndef HAVE_LONG_FILE_NAMES |
3537
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
95 |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
96 #define MAKE_LOCK_NAME(lock, file) \ |
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
97 (lock = (char *) alloca (14 + strlen (lock_dir) + 1), \ |
3537
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
98 fill_in_lock_short_file_name (lock, (file))) |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
99 |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
100 |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
101 fill_in_lock_short_file_name (lockfile, fn) |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
102 register char *lockfile; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
103 register Lisp_Object fn; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
104 { |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
105 register union |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
106 { |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
107 unsigned int word [2]; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
108 unsigned char byte [8]; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
109 } crc; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
110 register unsigned char *p, new; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
111 |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
112 /* 7-bytes cyclic code for burst correction on byte-by-byte basis. |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
113 the used polynomial is D^7 + D^6 + D^3 +1. pot@cnuce.cnr.it */ |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
114 |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
115 crc.word[0] = crc.word[1] = 0; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
116 |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
117 for (p = XSTRING (fn)->data; new = *p++; ) |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
118 { |
4680
9d56bce1534a
* filelock.c (fill_in_lock_short_file_name): corrected the crc
Francesco Potortì <pot@gnu.org>
parents:
4618
diff
changeset
|
119 new += crc.byte[6]; |
3537
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
120 crc.byte[6] = crc.byte[5] + new; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
121 crc.byte[5] = crc.byte[4]; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
122 crc.byte[4] = crc.byte[3]; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
123 crc.byte[3] = crc.byte[2] + new; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
124 crc.byte[2] = crc.byte[1]; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
125 crc.byte[1] = crc.byte[0]; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
126 crc.byte[0] = new; |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
127 } |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
128 sprintf (lockfile, "%s%.2x%.2x%.2x%.2x%.2x%.2x%.2x", lock_dir, |
3537
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
129 crc.byte[0], crc.byte[1], crc.byte[2], crc.byte[3], |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
130 crc.byte[4], crc.byte[5], crc.byte[6]); |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
131 } |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
132 |
3602
f71f3f167365
* filelock.c: Test HAVE_LONG_FILE_NAMES, not SHORT_FILE_NAMES, to
Jim Blandy <jimb@redhat.com>
parents:
3537
diff
changeset
|
133 #else /* defined HAVE_LONG_FILE_NAMES */ |
3537
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
134 |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
135 #define MAKE_LOCK_NAME(lock, file) \ |
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
136 (lock = (char *) alloca (XSTRING (file)->size + strlen (lock_dir) + 1), \ |
624 | 137 fill_in_lock_file_name (lock, (file))) |
138 | |
3537
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
139 |
624 | 140 fill_in_lock_file_name (lockfile, fn) |
141 register char *lockfile; | |
142 register Lisp_Object fn; | |
143 { | |
144 register char *p; | |
145 | |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
146 strcpy (lockfile, lock_dir); |
624 | 147 |
148 p = lockfile + strlen (lockfile); | |
149 | |
150 strcpy (p, XSTRING (fn)->data); | |
151 | |
152 for (; *p; p++) | |
153 { | |
154 if (*p == '/') | |
155 *p = '!'; | |
156 } | |
157 } | |
3602
f71f3f167365
* filelock.c: Test HAVE_LONG_FILE_NAMES, not SHORT_FILE_NAMES, to
Jim Blandy <jimb@redhat.com>
parents:
3537
diff
changeset
|
158 #endif /* !defined HAVE_LONG_FILE_NAMES */ |
624 | 159 |
163 | 160 static Lisp_Object |
161 lock_file_owner_name (lfname) | |
162 char *lfname; | |
163 { | |
164 struct stat s; | |
165 struct passwd *the_pw; | |
166 | |
167 if (lstat (lfname, &s) == 0) | |
168 the_pw = getpwuid (s.st_uid); | |
169 return (the_pw == 0 ? Qnil : build_string (the_pw->pw_name)); | |
170 } | |
171 | |
172 | |
173 /* lock_file locks file fn, | |
174 meaning it serves notice on the world that you intend to edit that file. | |
175 This should be done only when about to modify a file-visiting | |
176 buffer previously unmodified. | |
177 Do not (normally) call lock_buffer for a buffer already modified, | |
178 as either the file is already locked, or the user has already | |
179 decided to go ahead without locking. | |
180 | |
181 When lock_buffer returns, either the lock is locked for us, | |
182 or the user has said to go ahead without locking. | |
183 | |
184 If the file is locked by someone else, lock_buffer calls | |
185 ask-user-about-lock (a Lisp function) with two arguments, | |
186 the file name and the name of the user who did the locking. | |
187 This function can signal an error, or return t meaning | |
188 take away the lock, or return nil meaning ignore the lock. */ | |
189 | |
190 /* The lock file name is the file name with "/" replaced by "!" | |
191 and put in the Emacs lock directory. */ | |
192 /* (ie., /ka/king/junk.tex -> /!/!ka!king!junk.tex). */ | |
193 | |
3602
f71f3f167365
* filelock.c: Test HAVE_LONG_FILE_NAMES, not SHORT_FILE_NAMES, to
Jim Blandy <jimb@redhat.com>
parents:
3537
diff
changeset
|
194 /* If HAVE_LONG_FILE_NAMES is not defined, the lock file name is the hex |
3537
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
195 representation of a 14-bytes CRC generated from the file name |
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
196 and put in the Emacs lock directory (not very nice, but it works). |
4680
9d56bce1534a
* filelock.c (fill_in_lock_short_file_name): corrected the crc
Francesco Potortì <pot@gnu.org>
parents:
4618
diff
changeset
|
197 (ie., /ka/king/junk.tex -> /!/12a82c62f1c6da). */ |
3537
22055fd47b78
(MAKE_LOCK_PATH): If SHORT_FILE_NAMES allocates
Richard M. Stallman <rms@gnu.org>
parents:
2961
diff
changeset
|
198 |
163 | 199 void |
200 lock_file (fn) | |
201 register Lisp_Object fn; | |
202 { | |
12811
11f5ce737c57
(lock_file): Use get_truename_buffer.
Richard M. Stallman <rms@gnu.org>
parents:
12104
diff
changeset
|
203 register Lisp_Object attack, orig_fn; |
163 | 204 register char *lfname; |
205 | |
12811
11f5ce737c57
(lock_file): Use get_truename_buffer.
Richard M. Stallman <rms@gnu.org>
parents:
12104
diff
changeset
|
206 orig_fn = fn; |
12104
10197e4b3fb2
(unlock_file, lock_file): Call Fexpand_file_name.
Karl Heuer <kwzh@gnu.org>
parents:
10752
diff
changeset
|
207 fn = Fexpand_file_name (fn, Qnil); |
10197e4b3fb2
(unlock_file, lock_file): Call Fexpand_file_name.
Karl Heuer <kwzh@gnu.org>
parents:
10752
diff
changeset
|
208 |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
209 MAKE_LOCK_NAME (lfname, fn); |
163 | 210 |
624 | 211 /* See if this file is visited and has changed on disk since it was |
212 visited. */ | |
163 | 213 { |
6499
e0bef61003ae
(lock_file): Use assignment, not initialization.
Karl Heuer <kwzh@gnu.org>
parents:
6300
diff
changeset
|
214 register Lisp_Object subject_buf; |
12811
11f5ce737c57
(lock_file): Use get_truename_buffer.
Richard M. Stallman <rms@gnu.org>
parents:
12104
diff
changeset
|
215 subject_buf = get_truename_buffer (orig_fn); |
485 | 216 if (!NILP (subject_buf) |
217 && NILP (Fverify_visited_file_modtime (subject_buf)) | |
218 && !NILP (Ffile_exists_p (fn))) | |
163 | 219 call1 (intern ("ask-user-about-supersession-threat"), fn); |
220 } | |
221 | |
222 /* Try to lock the lock. */ | |
223 if (lock_if_free (lfname) <= 0) | |
224 /* Return now if we have locked it, or if lock dir does not exist */ | |
225 return; | |
226 | |
227 /* Else consider breaking the lock */ | |
228 attack = call2 (intern ("ask-user-about-lock"), fn, | |
229 lock_file_owner_name (lfname)); | |
485 | 230 if (!NILP (attack)) |
163 | 231 /* User says take the lock */ |
232 { | |
233 lock_superlock (lfname); | |
234 lock_file_1 (lfname, O_WRONLY) ; | |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
235 unlink (superlock_file); |
163 | 236 return; |
237 } | |
238 /* User says ignore the lock */ | |
239 } | |
240 | |
241 /* Lock the lock file named LFNAME. | |
242 If MODE is O_WRONLY, we do so even if it is already locked. | |
243 If MODE is O_WRONLY | O_EXCL | O_CREAT, we do so only if it is free. | |
244 Return 1 if successful, 0 if not. */ | |
245 | |
246 int | |
247 lock_file_1 (lfname, mode) | |
248 int mode; char *lfname; | |
249 { | |
250 register int fd; | |
251 char buf[20]; | |
252 | |
253 if ((fd = open (lfname, mode, 0666)) >= 0) | |
254 { | |
255 #ifdef USG | |
256 chmod (lfname, 0666); | |
257 #else | |
258 fchmod (fd, 0666); | |
259 #endif | |
260 sprintf (buf, "%d ", getpid ()); | |
261 write (fd, buf, strlen (buf)); | |
262 close (fd); | |
263 return 1; | |
264 } | |
265 else | |
266 return 0; | |
267 } | |
268 | |
269 /* Lock the lock named LFNAME if possible. | |
270 Return 0 in that case. | |
271 Return positive if lock is really locked by someone else. | |
272 Return -1 if cannot lock for any other reason. */ | |
273 | |
274 int | |
275 lock_if_free (lfname) | |
276 register char *lfname; | |
277 { | |
278 register int clasher; | |
279 | |
280 while (lock_file_1 (lfname, O_WRONLY | O_EXCL | O_CREAT) == 0) | |
281 { | |
282 if (errno != EEXIST) | |
283 return -1; | |
284 clasher = current_lock_owner (lfname); | |
285 if (clasher != 0) | |
286 if (clasher != getpid ()) | |
287 return (clasher); | |
288 else return (0); | |
289 /* Try again to lock it */ | |
290 } | |
291 return 0; | |
292 } | |
293 | |
294 /* Return the pid of the process that claims to own the lock file LFNAME, | |
295 or 0 if nobody does or the lock is obsolete, | |
296 or -1 if something is wrong with the locking mechanism. */ | |
297 | |
298 int | |
299 current_lock_owner (lfname) | |
300 char *lfname; | |
301 { | |
302 int owner = current_lock_owner_1 (lfname); | |
303 if (owner == 0 && errno == ENOENT) | |
304 return (0); | |
305 /* Is it locked by a process that exists? */ | |
306 if (owner != 0 && (kill (owner, 0) >= 0 || errno == EPERM)) | |
307 return (owner); | |
308 if (unlink (lfname) < 0) | |
309 return (-1); | |
310 return (0); | |
311 } | |
312 | |
313 int | |
314 current_lock_owner_1 (lfname) | |
315 char *lfname; | |
316 { | |
317 register int fd; | |
318 char buf[20]; | |
319 int tem; | |
320 | |
321 fd = open (lfname, O_RDONLY, 0666); | |
322 if (fd < 0) | |
323 return 0; | |
324 tem = read (fd, buf, sizeof buf); | |
325 close (fd); | |
326 return (tem <= 0 ? 0 : atoi (buf)); | |
327 } | |
328 | |
329 | |
330 void | |
331 unlock_file (fn) | |
332 register Lisp_Object fn; | |
333 { | |
334 register char *lfname; | |
335 | |
12104
10197e4b3fb2
(unlock_file, lock_file): Call Fexpand_file_name.
Karl Heuer <kwzh@gnu.org>
parents:
10752
diff
changeset
|
336 fn = Fexpand_file_name (fn, Qnil); |
10197e4b3fb2
(unlock_file, lock_file): Call Fexpand_file_name.
Karl Heuer <kwzh@gnu.org>
parents:
10752
diff
changeset
|
337 |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
338 MAKE_LOCK_NAME (lfname, fn); |
163 | 339 |
340 lock_superlock (lfname); | |
341 | |
342 if (current_lock_owner_1 (lfname) == getpid ()) | |
343 unlink (lfname); | |
344 | |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
345 unlink (superlock_file); |
163 | 346 } |
347 | |
348 lock_superlock (lfname) | |
349 char *lfname; | |
350 { | |
351 register int i, fd; | |
6300
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
352 DIR *lockdir; |
163 | 353 |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
354 for (i = -20; i < 0 && (fd = open (superlock_file, |
163 | 355 O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0; |
356 i++) | |
357 { | |
358 if (errno != EEXIST) | |
359 return; | |
6300
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
360 |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
361 /* This seems to be necessary to prevent Emacs from hanging when the |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
362 competing process has already deleted the superlock, but it's still |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
363 in the NFS cache. So we force NFS to synchronize the cache. */ |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
364 if (lockdir = opendir (lock_dir)) |
6300
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
365 closedir (lockdir); |
0aa51282fbfe
(lock_superlock): Synchronize NFS cache.
Karl Heuer <kwzh@gnu.org>
parents:
4701
diff
changeset
|
366 |
163 | 367 sleep (1); |
368 } | |
369 if (fd >= 0) | |
370 { | |
371 #ifdef USG | |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
372 chmod (superlock_file, 0666); |
163 | 373 #else |
374 fchmod (fd, 0666); | |
375 #endif | |
376 write (fd, lfname, strlen (lfname)); | |
377 close (fd); | |
378 } | |
379 } | |
380 | |
381 void | |
382 unlock_all_files () | |
383 { | |
384 register Lisp_Object tail; | |
385 register struct buffer *b; | |
386 | |
9949
e9d341f235ee
(unlock_all_files): Use the new type-test macros.
Karl Heuer <kwzh@gnu.org>
parents:
9130
diff
changeset
|
387 for (tail = Vbuffer_alist; GC_CONSP (tail); tail = XCONS (tail)->cdr) |
163 | 388 { |
389 b = XBUFFER (XCONS (XCONS (tail)->car)->cdr); | |
10752
e4fb5e01090a
(unlock_buffer, unlock_all_files, Flock_buffer)
Richard M. Stallman <rms@gnu.org>
parents:
10331
diff
changeset
|
390 if (STRINGP (b->file_truename) && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) |
e4fb5e01090a
(unlock_buffer, unlock_all_files, Flock_buffer)
Richard M. Stallman <rms@gnu.org>
parents:
10331
diff
changeset
|
391 unlock_file (b->file_truename); |
163 | 392 } |
393 } | |
394 | |
395 | |
396 DEFUN ("lock-buffer", Flock_buffer, Slock_buffer, | |
397 0, 1, 0, | |
398 "Lock FILE, if current buffer is modified.\n\ | |
399 FILE defaults to current buffer's visited file,\n\ | |
400 or else nothing is done if current buffer isn't visiting a file.") | |
14075
21a86b6274e6
(Flock_buffer, Ffile_locked_p): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
parents:
12811
diff
changeset
|
401 (file) |
21a86b6274e6
(Flock_buffer, Ffile_locked_p): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
parents:
12811
diff
changeset
|
402 Lisp_Object file; |
163 | 403 { |
14075
21a86b6274e6
(Flock_buffer, Ffile_locked_p): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
parents:
12811
diff
changeset
|
404 if (NILP (file)) |
21a86b6274e6
(Flock_buffer, Ffile_locked_p): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
parents:
12811
diff
changeset
|
405 file = current_buffer->file_truename; |
163 | 406 else |
14075
21a86b6274e6
(Flock_buffer, Ffile_locked_p): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
parents:
12811
diff
changeset
|
407 CHECK_STRING (file, 0); |
10331
2ae69efc3e8b
Use SAVE_MODIFF and BUF_SAVE_MODIFF
Richard M. Stallman <rms@gnu.org>
parents:
10017
diff
changeset
|
408 if (SAVE_MODIFF < MODIFF |
14075
21a86b6274e6
(Flock_buffer, Ffile_locked_p): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
parents:
12811
diff
changeset
|
409 && !NILP (file)) |
21a86b6274e6
(Flock_buffer, Ffile_locked_p): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
parents:
12811
diff
changeset
|
410 lock_file (file); |
163 | 411 return Qnil; |
412 } | |
413 | |
414 DEFUN ("unlock-buffer", Funlock_buffer, Sunlock_buffer, | |
415 0, 0, 0, | |
416 "Unlock the file visited in the current buffer,\n\ | |
417 if it should normally be locked.") | |
418 () | |
419 { | |
10331
2ae69efc3e8b
Use SAVE_MODIFF and BUF_SAVE_MODIFF
Richard M. Stallman <rms@gnu.org>
parents:
10017
diff
changeset
|
420 if (SAVE_MODIFF < MODIFF |
10752
e4fb5e01090a
(unlock_buffer, unlock_all_files, Flock_buffer)
Richard M. Stallman <rms@gnu.org>
parents:
10331
diff
changeset
|
421 && STRINGP (current_buffer->file_truename)) |
e4fb5e01090a
(unlock_buffer, unlock_all_files, Flock_buffer)
Richard M. Stallman <rms@gnu.org>
parents:
10331
diff
changeset
|
422 unlock_file (current_buffer->file_truename); |
163 | 423 return Qnil; |
424 } | |
425 | |
426 | |
427 /* Unlock the file visited in buffer BUFFER. */ | |
428 | |
429 unlock_buffer (buffer) | |
430 struct buffer *buffer; | |
431 { | |
10331
2ae69efc3e8b
Use SAVE_MODIFF and BUF_SAVE_MODIFF
Richard M. Stallman <rms@gnu.org>
parents:
10017
diff
changeset
|
432 if (BUF_SAVE_MODIFF (buffer) < BUF_MODIFF (buffer) |
10752
e4fb5e01090a
(unlock_buffer, unlock_all_files, Flock_buffer)
Richard M. Stallman <rms@gnu.org>
parents:
10331
diff
changeset
|
433 && STRINGP (buffer->file_truename)) |
e4fb5e01090a
(unlock_buffer, unlock_all_files, Flock_buffer)
Richard M. Stallman <rms@gnu.org>
parents:
10331
diff
changeset
|
434 unlock_file (buffer->file_truename); |
163 | 435 } |
436 | |
437 DEFUN ("file-locked-p", Ffile_locked_p, Sfile_locked_p, 0, 1, 0, | |
438 "Return nil if the FILENAME is not locked,\n\ | |
439 t if it is locked by you, else a string of the name of the locker.") | |
14075
21a86b6274e6
(Flock_buffer, Ffile_locked_p): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
parents:
12811
diff
changeset
|
440 (filename) |
21a86b6274e6
(Flock_buffer, Ffile_locked_p): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
parents:
12811
diff
changeset
|
441 Lisp_Object filename; |
163 | 442 { |
443 register char *lfname; | |
444 int owner; | |
445 | |
14075
21a86b6274e6
(Flock_buffer, Ffile_locked_p): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
parents:
12811
diff
changeset
|
446 filename = Fexpand_file_name (filename, Qnil); |
163 | 447 |
14075
21a86b6274e6
(Flock_buffer, Ffile_locked_p): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
parents:
12811
diff
changeset
|
448 MAKE_LOCK_NAME (lfname, filename); |
163 | 449 |
450 owner = current_lock_owner (lfname); | |
451 if (owner <= 0) | |
452 return (Qnil); | |
453 else if (owner == getpid ()) | |
454 return (Qt); | |
455 | |
456 return (lock_file_owner_name (lfname)); | |
457 } | |
458 | |
624 | 459 |
460 /* Initialization functions. */ | |
461 | |
462 init_filelock () | |
463 { | |
10017
6e28a6ff9a41
(init_filelock): Add missing semicolon.
Richard M. Stallman <rms@gnu.org>
parents:
9996
diff
changeset
|
464 char *new_name; |
9992
5666a2d5b2bc
(init_filelock): Always copy lock_path.
Richard M. Stallman <rms@gnu.org>
parents:
9949
diff
changeset
|
465 |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
466 lock_dir = egetenv ("EMACSLOCKDIR"); |
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
467 if (! lock_dir) |
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
468 lock_dir = PATH_LOCK; |
624 | 469 |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
470 /* Copy the name in case egetenv got it from a Lisp string. */ |
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
471 new_name = (char *) xmalloc (strlen (lock_dir) + 2); |
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
472 strcpy (new_name, lock_dir); |
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
473 lock_dir = new_name; |
9992
5666a2d5b2bc
(init_filelock): Always copy lock_path.
Richard M. Stallman <rms@gnu.org>
parents:
9949
diff
changeset
|
474 |
624 | 475 /* Make sure it ends with a slash. */ |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
476 if (lock_dir[strlen (lock_dir) - 1] != '/') |
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
477 strcat (lock_dir, "/"); |
624 | 478 |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
479 superlock_file = (char *) xmalloc ((strlen (lock_dir) |
624 | 480 + sizeof (SUPERLOCK_NAME))); |
9996
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
481 strcpy (superlock_file, lock_dir); |
478f14a61aba
(lock_dir, superlock_file, MAKE_LOCK_NAME):
Richard M. Stallman <rms@gnu.org>
parents:
9992
diff
changeset
|
482 strcat (superlock_file, SUPERLOCK_NAME); |
624 | 483 } |
484 | |
163 | 485 syms_of_filelock () |
486 { | |
487 defsubr (&Sunlock_buffer); | |
488 defsubr (&Slock_buffer); | |
489 defsubr (&Sfile_locked_p); | |
490 } | |
491 | |
492 #endif /* CLASH_DETECTION */ |