annotate src/filelock.c @ 408:501cfd01ee6d r21-2-34

Import from CVS: tag r21-2-34
author cvs
date Mon, 13 Aug 2007 11:18:11 +0200
parents b8cc9ab3f761
children 697ef44129c6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
1 /* Copyright (C) 1985, 86, 87, 93, 94, 96 Free Software Foundation, Inc.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
3 This file is part of GNU Emacs.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
5 GNU Emacs is free software; you can redistribute it and/or modify
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
6 it under the terms of the GNU General Public License as published by
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
8 any later version.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
10 GNU Emacs is distributed in the hope that it will be useful,
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
13 GNU General Public License for more details.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
16 along with GNU Emacs; see the file COPYING. If not, write to
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
20 /* Synced with FSF 20.2 */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 #include "lisp.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 #include "buffer.h"
159
3bb7ccffb0c0 Import from CVS: tag r20-3b6
cvs
parents: 114
diff changeset
26 #include <paths.h>
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 #include "sysfile.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 #include "sysdir.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 #include "syspwd.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 #include "syssignal.h" /* for kill */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 Lisp_Object Qask_user_about_supersession_threat;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 Lisp_Object Qask_user_about_lock;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
36 #ifdef CLASH_DETECTION
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
37
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
38 /* The strategy: to lock a file FN, create a symlink .#FN in FN's
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
39 directory, with link data `user@host.pid'. This avoids a single
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
40 mount (== failure) point for lock files.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
42 When the host in the lock data is the current host, we can check if
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
43 the pid is valid with kill.
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
44
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
45 Otherwise, we could look at a separate file that maps hostnames to
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
46 reboot times to see if the remote pid can possibly be valid, since we
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
47 don't want Emacs to have to communicate via pipes or sockets or
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
48 whatever to other processes, either locally or remotely; rms says
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
49 that's too unreliable. Hence the separate file, which could
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
50 theoretically be updated by daemons running separately -- but this
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
51 whole idea is unimplemented; in practice, at least in our
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
52 environment, it seems such stale locks arise fairly infrequently, and
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
53 Emacs' standard methods of dealing with clashes suffice.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
55 We use symlinks instead of normal files because (1) they can be
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
56 stored more efficiently on the filesystem, since the kernel knows
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
57 they will be small, and (2) all the info about the lock can be read
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
58 in a single system call (readlink). Although we could use regular
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
59 files to be useful on old systems lacking symlinks, nowadays
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
60 virtually all such systems are probably single-user anyway, so it
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
61 didn't seem worth the complication.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
63 Similarly, we don't worry about a possible 14-character limit on
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
64 file names, because those are all the same systems that don't have
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
65 symlinks.
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
66
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
67 This is compatible with the locking scheme used by Interleaf (which
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
68 has contributed this implementation for Emacs), and was designed by
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
69 Ethan Jacobson, Kimbo Mundy, and others.
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
70
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
71 --karl@cs.umb.edu/karl@hq.ileaf.com. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
73 /* Note that muleization is provided by using mule-encapsulated
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
74 versions of the system calls we use like symlink(), unlink(), etc... */
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
75
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
76
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
77 /* Here is the structure that stores information about a lock. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
79 typedef struct
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
80 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
81 char *user;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
82 char *host;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
83 unsigned long pid;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
84 } lock_info_type;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
86 /* When we read the info back, we might need this much more,
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
87 enough for decimal representation plus null. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
88 #define LOCK_PID_MAX (4 * sizeof (unsigned long))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
90 /* Free the two dynamically-allocated pieces in PTR. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
91 #define FREE_LOCK_INFO(i) do { xfree ((i).user); xfree ((i).host); } while (0)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
93 /* Write the name of the lock file for FN into LFNAME. Length will be
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
94 that of FN plus two more for the leading `.#' plus one for the null. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
95 #define MAKE_LOCK_NAME(lock, file) \
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
96 (lock = (char *) alloca (XSTRING_LENGTH (file) + 2 + 1), \
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
97 fill_in_lock_file_name ((Bufbyte *) (lock), (file)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 static void
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
100 fill_in_lock_file_name (Bufbyte *lockfile, Lisp_Object fn)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 {
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
102 Bufbyte *file_name = XSTRING_DATA (fn);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
103 Bufbyte *p;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
104 size_t dirlen;
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
105
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
106 for (p = file_name + XSTRING_LENGTH (fn) - 1;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
107 p > file_name && !IS_ANY_SEP (p[-1]);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
108 p--)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
109 ;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
110 dirlen = p - file_name;
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
111
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
112 memcpy (lockfile, file_name, dirlen);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
113 p = lockfile + dirlen;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
114 *(p++) = '.';
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
115 *(p++) = '#';
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
116 memcpy (p, file_name + dirlen, XSTRING_LENGTH (fn) - dirlen + 1);
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
117 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
118
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
119 /* Lock the lock file named LFNAME.
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
120 If FORCE is nonzero, we do so even if it is already locked.
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
121 Return 1 if successful, 0 if not. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
123 static int
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
124 lock_file_1 (char *lfname, int force)
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
125 {
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
126 int err;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
127 char *lock_info_str;
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
128 char *host_name;
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
129 char *user_name = user_login_name (NULL);
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
130
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
131 if (user_name == NULL)
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
132 user_name = "";
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
133
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
134 if (STRINGP (Vsystem_name))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
135 host_name = (char *) XSTRING_DATA (Vsystem_name);
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
136 else
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
137 host_name = "";
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
138
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
139 lock_info_str = (char *)alloca (strlen (user_name) + strlen (host_name)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
140 + LOCK_PID_MAX + 5);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
142 sprintf (lock_info_str, "%s@%s.%lu", user_name, host_name,
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
143 (unsigned long) getpid ());
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
145 err = symlink (lock_info_str, lfname);
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
146 if (err != 0 && errno == EEXIST && force)
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
147 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
148 unlink (lfname);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
149 err = symlink (lock_info_str, lfname);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
150 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
151
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
152 return err == 0;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
153 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
154
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
155 /* Return 0 if nobody owns the lock file LFNAME or the lock is obsolete,
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
156 1 if another process owns it (and set OWNER (if non-null) to info),
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
157 2 if the current process owns it,
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
158 or -1 if something is wrong with the locking mechanism. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
160 static int
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
161 current_lock_owner (lock_info_type *owner, char *lfname)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
162 {
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
163 int len, ret;
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
164 int local_owner = 0;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
165 char *at, *dot;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
166 char *lfinfo = 0;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
167 int bufsize = 50;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
168 /* Read arbitrarily-long contents of symlink. Similar code in
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
169 file-symlink-p in fileio.c. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
170 do
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
172 bufsize *= 2;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
173 lfinfo = (char *) xrealloc (lfinfo, bufsize);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
174 len = readlink (lfname, lfinfo, bufsize);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
175 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
176 while (len >= bufsize);
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
177
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
178 /* If nonexistent lock file, all is well; otherwise, got strange error. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
179 if (len == -1)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
180 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
181 xfree (lfinfo);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
182 return errno == ENOENT ? 0 : -1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
185 /* Link info exists, so `len' is its length. Null terminate. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
186 lfinfo[len] = 0;
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
187
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
188 /* Even if the caller doesn't want the owner info, we still have to
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
189 read it to determine return value, so allocate it. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
190 if (!owner)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
192 owner = (lock_info_type *) alloca (sizeof (lock_info_type));
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
193 local_owner = 1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 }
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
195
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
196 /* Parse USER@HOST.PID. If can't parse, return -1. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
197 /* The USER is everything before the first @. */
382
064ab7fed2e0 Import from CVS: tag r21-2-6
cvs
parents: 373
diff changeset
198 at = strchr (lfinfo, '@');
064ab7fed2e0 Import from CVS: tag r21-2-6
cvs
parents: 373
diff changeset
199 dot = strrchr (lfinfo, '.');
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
200 if (!at || !dot) {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
201 xfree (lfinfo);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
202 return -1;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
203 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
204 len = at - lfinfo;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
205 owner->user = (char *) xmalloc (len + 1);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
206 strncpy (owner->user, lfinfo, len);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
207 owner->user[len] = 0;
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
208
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
209 /* The PID is everything after the last `.'. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
210 owner->pid = atoi (dot + 1);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
211
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
212 /* The host is everything in between. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
213 len = dot - at - 1;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
214 owner->host = (char *) xmalloc (len + 1);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
215 strncpy (owner->host, at + 1, len);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
216 owner->host[len] = 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
218 /* We're done looking at the link info. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
219 xfree (lfinfo);
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
220
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
221 /* On current host? */
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
222 if (STRINGP (Fsystem_name ())
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
223 && strcmp (owner->host, (char *) XSTRING_DATA (Fsystem_name ())) == 0)
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
224 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
225 if (owner->pid == getpid ())
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
226 ret = 2; /* We own it. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
227 else if (owner->pid > 0
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
228 && (kill (owner->pid, 0) >= 0 || errno == EPERM))
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
229 ret = 1; /* An existing process on this machine owns it. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
230 /* The owner process is dead or has a strange pid (<=0), so try to
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
231 zap the lockfile. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
232 else if (unlink (lfname) < 0)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
233 ret = -1;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
234 else
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
235 ret = 0;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
236 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
237 else
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
238 { /* If we wanted to support the check for stale locks on remote machines,
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
239 here's where we'd do it. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
240 ret = 1;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
241 }
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
242
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
243 /* Avoid garbage. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
244 if (local_owner || ret <= 0)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
245 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
246 FREE_LOCK_INFO (*owner);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
247 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
248 return ret;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
249 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
250
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
251 /* Lock the lock named LFNAME if possible.
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
252 Return 0 in that case.
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
253 Return positive if some other process owns the lock, and info about
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
254 that process in CLASHER.
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
255 Return -1 if cannot lock for any other reason. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
256
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
257 static int
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
258 lock_if_free (lock_info_type *clasher, char *lfname)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
260 if (lock_file_1 (lfname, 0) == 0)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
261 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
262 int locker;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
264 if (errno != EEXIST)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
265 return -1;
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
266
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
267 locker = current_lock_owner (clasher, lfname);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
268 if (locker == 2)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
269 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
270 FREE_LOCK_INFO (*clasher);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
271 return 0; /* We ourselves locked it. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
272 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
273 else if (locker == 1)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
274 return 1; /* Someone else has it. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
275
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
276 return -1; /* Something's wrong. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
277 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
278 return 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
281 /* lock_file locks file FN,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 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
283 This should be done only when about to modify a file-visiting
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 buffer previously unmodified.
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
285 Do not (normally) call this for a buffer already modified,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 as either the file is already locked, or the user has already
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 decided to go ahead without locking.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
289 When this returns, either the lock is locked for us,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 or the user has said to go ahead without locking.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
292 If the file is locked by someone else, this calls
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 ask-user-about-lock (a Lisp function) with two arguments,
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
294 the file name and info about the user who did the locking.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 This function can signal an error, or return t meaning
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 take away the lock, or return nil meaning ignore the lock. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 lock_file (Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
301 /* This function can GC. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
302 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
303 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
304 you protect things right. */
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
305 /* Somebody updated the code in this function and removed the previous
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
306 comment. -slb */
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
307
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
308 register Lisp_Object attack, orig_fn;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
309 register char *lfname, *locker;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
310 lock_info_type lock_info;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
311 struct gcpro gcpro1,gcpro2;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
312 Lisp_Object subject_buf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 GCPRO2 (fn, subject_buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 orig_fn = fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 /* Create the name of the lock-file for file fn */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 /* 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
322 visited. */
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
323 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
324 subject_buf = get_truename_buffer (orig_fn);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
325 if (!NILP (subject_buf)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
326 && NILP (Fverify_visited_file_modtime (subject_buf))
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
327 && !NILP (Ffile_exists_p (fn)))
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
328 call1_in_buffer (XBUFFER(subject_buf),
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
329 Qask_user_about_supersession_threat, fn);
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
330 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 /* Try to lock the lock. */
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
333 if (lock_if_free (&lock_info, lfname) <= 0)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
334 /* Return now if we have locked it, or if lock creation failed */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 goto done;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 /* Else consider breaking the lock */
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
338 locker = (char *) alloca (strlen (lock_info.user) + strlen (lock_info.host)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
339 + LOCK_PID_MAX + 9);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
340 sprintf (locker, "%s@%s (pid %lu)", lock_info.user, lock_info.host,
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
341 lock_info.pid);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
342 FREE_LOCK_INFO (lock_info);
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
343
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 attack = call2_in_buffer (BUFFERP (subject_buf) ? XBUFFER (subject_buf) :
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
345 current_buffer, Qask_user_about_lock , fn,
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
346 build_string (locker));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 if (!NILP (attack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 /* User says take the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
350 lock_file_1 (lfname, 1);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 goto done;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 /* User says ignore the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 done:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 UNGCPRO;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 unlock_file (Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
361 register char *lfname;
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
362 struct gcpro gcpro1;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
363
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
364 GCPRO1 (fn);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
370 if (current_lock_owner (0, lfname) == 2)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 unlink (lfname);
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
372
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
373 UNGCPRO;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 void
404
2f8bb876ab1d Import from CVS: tag r21-2-32
cvs
parents: 398
diff changeset
377 unlock_all_files (void)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
379 register Lisp_Object tail;
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
380
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 382
diff changeset
381 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 {
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
383 struct buffer *b = XBUFFER (XCDR (XCAR (tail)));
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
384 if (STRINGP (b->file_truename) && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 unlock_file (b->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
389 DEFUN ("lock-buffer", Flock_buffer, 0, 1, 0, /*
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
390 Lock FILE, if current buffer is modified.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
391 FILE defaults to current buffer's visited file,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 or else nothing is done if current buffer isn't visiting a file.
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
393 */
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
394 (file))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
396 if (NILP (file))
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
397 file = current_buffer->file_truename;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
398 CHECK_STRING (file);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
400 && !NILP (file))
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
401 lock_file (file);
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 175
diff changeset
402 return Qnil;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
405 DEFUN ("unlock-buffer", Funlock_buffer, 0, 0, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 Unlock the file visited in the current buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 if it should normally be locked.
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
408 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
409 ())
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
411 /* This function can GC */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
412 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
413 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
414 you protect things right. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
415
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 && STRINGP (current_buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 unlock_file (current_buffer->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 /* Unlock the file visited in buffer BUFFER. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
424
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 unlock_buffer (struct buffer *buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
428 /* This function can GC */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
429 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
430 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
431 you protect things right. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 if (BUF_SAVE_MODIFF (buffer) < BUF_MODIFF (buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 && STRINGP (buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 unlock_file (buffer->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
437 DEFUN ("file-locked-p", Ffile_locked_p, 0, 1, 0, /*
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
438 Return nil if the FILENAME is not locked,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 t if it is locked by you, else a string of the name of the locker.
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
440 */
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
441 (filename))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
443 Lisp_Object ret;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
444 register char *lfname;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 int owner;
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
446 lock_info_type locker;
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
447 struct gcpro gcpro1;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
448
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
449 GCPRO1 (filename);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
451 filename = Fexpand_file_name (filename, Qnil);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
452
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
453 MAKE_LOCK_NAME (lfname, filename);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
455 owner = current_lock_owner (&locker, lfname);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 if (owner <= 0)
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
457 ret = Qnil;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
458 else if (owner == 2)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
459 ret = Qt;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
460 else
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
461 ret = build_string (locker.user);
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 175
diff changeset
462
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
463 if (owner > 0)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
464 FREE_LOCK_INFO (locker);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
465
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
466 UNGCPRO;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 404
diff changeset
467
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
468 return ret;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
471
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
472 /* Initialization functions. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
473
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 syms_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 /* This function can GC */
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
478 DEFSUBR (Funlock_buffer);
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
479 DEFSUBR (Flock_buffer);
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
480 DEFSUBR (Ffile_locked_p);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 defsymbol (&Qask_user_about_supersession_threat,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 "ask-user-about-supersession-threat");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 defsymbol (&Qask_user_about_lock, "ask-user-about-lock");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
488 #endif /* CLASH_DETECTION */