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