annotate src/filelock.c @ 383:6a50c6a581a5

Added tag r21-2-6 for changeset 064ab7fed2e0
author cvs
date Mon, 13 Aug 2007 11:07:40 +0200
parents 064ab7fed2e0
children 74fd4e045ea6
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
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
37
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.
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
44
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.
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
66
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.
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
70
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
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
73
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
74 /* Here is the structure that stores information about a lock. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
76 typedef struct
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
77 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
78 char *user;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
79 char *host;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
80 unsigned long pid;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
81 } lock_info_type;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
83 /* When we read the info back, we might need this much more,
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
84 enough for decimal representation plus null. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
85 #define LOCK_PID_MAX (4 * sizeof (unsigned long))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
87 /* Free the two dynamically-allocated pieces in PTR. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
88 #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
89
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
90 /* 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
91 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
92 #define MAKE_LOCK_NAME(lock, file) \
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
93 (lock = (char *) alloca (XSTRING_LENGTH(file) + 2 + 1), \
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 fill_in_lock_file_name (lock, (file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 static void
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
97 fill_in_lock_file_name (lockfile, fn)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
98 register char *lockfile;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
99 register Lisp_Object fn;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
101 register char *p;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
102
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
103 strcpy (lockfile, XSTRING_DATA(fn));
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
104
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
105 /* Shift the nondirectory part of the file name (including the null)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
106 right two characters. Here is one of the places where we'd have to
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
107 do something to support 14-character-max file names. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
108 for (p = lockfile + strlen (lockfile); p != lockfile && *p != '/'; p--)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
109 p[2] = *p;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
110
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
111 /* Insert the `.#'. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
112 p[1] = '.';
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
113 p[2] = '#';
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
114 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
115
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
116 /* Lock the lock file named LFNAME.
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
117 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
118 Return 1 if successful, 0 if not. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
120 static int
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
121 lock_file_1 (char *lfname,int force)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
122 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
123 register int err;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
124 char *user_name;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
125 char *host_name;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
126 char *lock_info_str;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
127
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
128 if (STRINGP (Fuser_login_name (Qnil)))
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
129 user_name = (char *)XSTRING_DATA((Fuser_login_name (Qnil)));
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
130 else
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
131 user_name = "";
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
132 if (STRINGP (Fsystem_name ()))
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
133 host_name = (char *)XSTRING_DATA((Fsystem_name ()));
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
134 else
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
135 host_name = "";
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
136 lock_info_str = (char *)alloca (strlen (user_name) + strlen (host_name)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
137 + LOCK_PID_MAX + 5);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
139 sprintf (lock_info_str, "%s@%s.%lu", user_name, host_name,
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
140 (unsigned long) getpid ());
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 err = symlink (lock_info_str, lfname);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
143 if (errno == EEXIST && force)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
144 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
145 unlink (lfname);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
146 err = symlink (lock_info_str, lfname);
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
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
149 return err == 0;
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 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
153 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
154 2 if the current process owns it,
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
155 or -1 if something is wrong with the locking mechanism. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
157 static int
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
158 current_lock_owner (lock_info_type *owner, char *lfname)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
159 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
160 int o, p, len, ret;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
161 int local_owner = 0;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
162 char *at, *dot;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
163 char *lfinfo = 0;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
164 int bufsize = 50;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
165 /* Read arbitrarily-long contents of symlink. Similar code in
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
166 file-symlink-p in fileio.c. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
167 do
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
169 bufsize *= 2;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
170 lfinfo = (char *) xrealloc (lfinfo, bufsize);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
171 len = readlink (lfname, lfinfo, bufsize);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
172 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
173 while (len >= bufsize);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
174
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
175 /* If nonexistent lock file, all is well; otherwise, got strange error. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
176 if (len == -1)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
177 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
178 xfree (lfinfo);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
179 return errno == ENOENT ? 0 : -1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
182 /* Link info exists, so `len' is its length. Null terminate. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
183 lfinfo[len] = 0;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
184
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
185 /* 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
186 read it to determine return value, so allocate it. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
187 if (!owner)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
189 owner = (lock_info_type *) alloca (sizeof (lock_info_type));
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
190 local_owner = 1;
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
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
193 /* Parse USER@HOST.PID. If can't parse, return -1. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
194 /* The USER is everything before the first @. */
382
064ab7fed2e0 Import from CVS: tag r21-2-6
cvs
parents: 373
diff changeset
195 at = strchr (lfinfo, '@');
064ab7fed2e0 Import from CVS: tag r21-2-6
cvs
parents: 373
diff changeset
196 dot = strrchr (lfinfo, '.');
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
197 if (!at || !dot) {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
198 xfree (lfinfo);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
199 return -1;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
200 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
201 len = at - lfinfo;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
202 owner->user = (char *) xmalloc (len + 1);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
203 strncpy (owner->user, lfinfo, len);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
204 owner->user[len] = 0;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
205
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
206 /* The PID is everything after the last `.'. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
207 owner->pid = atoi (dot + 1);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
208
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
209 /* The host is everything in between. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
210 len = dot - at - 1;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
211 owner->host = (char *) xmalloc (len + 1);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
212 strncpy (owner->host, at + 1, len);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
213 owner->host[len] = 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
215 /* We're done looking at the link info. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
216 xfree (lfinfo);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
217
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
218 /* On current host? */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
219 if (STRINGP (Fsystem_name ())
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
220 && strcmp (owner->host, XSTRING_DATA(Fsystem_name ())) == 0)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
221 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
222 if (owner->pid == getpid ())
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
223 ret = 2; /* We own it. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
224 else if (owner->pid > 0
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
225 && (kill (owner->pid, 0) >= 0 || errno == EPERM))
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
226 ret = 1; /* An existing process on this machine owns it. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
227 /* 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
228 zap the lockfile. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
229 else if (unlink (lfname) < 0)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
230 ret = -1;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
231 else
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
232 ret = 0;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
233 }
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 { /* 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
236 here's where we'd do it. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
237 ret = 1;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
238 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
239
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
240 /* Avoid garbage. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
241 if (local_owner || ret <= 0)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
242 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
243 FREE_LOCK_INFO (*owner);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
244 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
245 return ret;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
246 }
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 /* Lock the lock named LFNAME if possible.
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
249 Return 0 in that case.
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
250 Return positive if some other process owns the lock, and info about
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
251 that process in CLASHER.
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
252 Return -1 if cannot lock for any other reason. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
253
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
254 static int
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
255 lock_if_free (lock_info_type *clasher, char *lfname)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
257 if (lock_file_1 (lfname, 0) == 0)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
258 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
259 int locker;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
261 if (errno != EEXIST)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
262 return -1;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
263
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
264 locker = current_lock_owner (clasher, lfname);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
265 if (locker == 2)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
266 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
267 FREE_LOCK_INFO (*clasher);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
268 return 0; /* We ourselves locked it. */
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 else if (locker == 1)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
271 return 1; /* Someone else has 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 return -1; /* Something's wrong. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
274 }
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
275 return 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
278 /* lock_file locks file FN,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 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
280 This should be done only when about to modify a file-visiting
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 buffer previously unmodified.
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
282 Do not (normally) call this for a buffer already modified,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 as either the file is already locked, or the user has already
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 decided to go ahead without locking.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
286 When this returns, either the lock is locked for us,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 or the user has said 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 If the file is locked by someone else, this calls
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 ask-user-about-lock (a Lisp function) with two arguments,
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
291 the file name and info about the user who did the locking.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 This function can signal an error, or return t meaning
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 take away the lock, or return nil meaning ignore the lock. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 lock_file (Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
298 /* This function can GC. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
299 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
300 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
301 you protect things right. */
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
302 /* Somebody updated the code in this function and removed the previous
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
303 comment. -slb */
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
304
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
305 register Lisp_Object attack, orig_fn;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
306 register char *lfname, *locker;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
307 lock_info_type lock_info;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
308 struct gcpro gcpro1,gcpro2;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
309 Lisp_Object subject_buf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 GCPRO2 (fn, subject_buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 orig_fn = fn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 /* Create the name of the lock-file for file fn */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 /* 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
319 visited. */
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
320 {
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
321 subject_buf = get_truename_buffer (orig_fn);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
322 if (!NILP (subject_buf)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
323 && NILP (Fverify_visited_file_modtime (subject_buf))
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
324 && !NILP (Ffile_exists_p (fn)))
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
325 call1_in_buffer (XBUFFER(subject_buf),
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
326 Qask_user_about_supersession_threat, fn);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
327 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 /* Try to lock the lock. */
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
330 if (lock_if_free (&lock_info, lfname) <= 0)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
331 /* Return now if we have locked it, or if lock creation failed */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 goto done;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 /* Else consider breaking the lock */
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
335 locker = (char *) alloca (strlen (lock_info.user) + strlen (lock_info.host)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
336 + LOCK_PID_MAX + 9);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
337 sprintf (locker, "%s@%s (pid %lu)", lock_info.user, lock_info.host,
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
338 lock_info.pid);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
339 FREE_LOCK_INFO (lock_info);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
340
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 attack = call2_in_buffer (BUFFERP (subject_buf) ? XBUFFER (subject_buf) :
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
342 current_buffer, Qask_user_about_lock , fn,
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
343 build_string (locker));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 if (!NILP (attack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 /* User says take the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
347 lock_file_1 (lfname, 1);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 goto done;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 /* User says ignore the lock */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 done:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 UNGCPRO;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 unlock_file (Lisp_Object fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
358 register char *lfname;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 fn = Fexpand_file_name (fn, Qnil);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 MAKE_LOCK_NAME (lfname, fn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
364 if (current_lock_owner (0, lfname) == 2)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 unlink (lfname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 void
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
369 unlock_all_files ()
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
371 register Lisp_Object tail;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
372 register struct buffer *b;
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
373
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
374 for (tail = Vbuffer_alist; GC_CONSP (tail); tail = XCDR (tail))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 b = XBUFFER (XCDR (XCAR (tail)));
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
377 if (STRINGP (b->file_truename) && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 unlock_file (b->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
382 DEFUN ("lock-buffer", Flock_buffer, 0, 1, 0, /*
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
383 Lock FILE, if current buffer is modified.\n\
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
384 FILE defaults to current buffer's visited file,\n\
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 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
386 */
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
387 (file))
0
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 if (NILP (file))
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
390 file = current_buffer->file_truename;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
391 CHECK_STRING (file);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
393 && !NILP (file))
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
394 lock_file (file);
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 175
diff changeset
395 return Qnil;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
398 DEFUN ("unlock-buffer", Funlock_buffer, 0, 0, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 Unlock the file visited in the current buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 if it should normally be locked.
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
401 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
402 ())
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
404 /* This function can GC */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
405 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
406 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
407 you protect things right. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
408
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 if (BUF_SAVE_MODIFF (current_buffer) < BUF_MODIFF (current_buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 && STRINGP (current_buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 unlock_file (current_buffer->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 return Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 /* Unlock the file visited in buffer BUFFER. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
417
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 unlock_buffer (struct buffer *buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 {
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
421 /* This function can GC */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
422 /* dmoore - and can destroy current_buffer and all sorts of other
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
423 mean nasty things with pointy teeth. If you call this make sure
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 100
diff changeset
424 you protect things right. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 if (BUF_SAVE_MODIFF (buffer) < BUF_MODIFF (buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 && STRINGP (buffer->file_truename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 unlock_file (buffer->file_truename);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
430 DEFUN ("file-locked-p", Ffile_locked_p, 0, 1, 0, /*
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
431 Return nil if the FILENAME is not locked,\n\
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 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
433 */
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
434 (filename))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 {
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
436 Lisp_Object ret;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
437 register char *lfname;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 int owner;
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
439 lock_info_type locker;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
441 filename = Fexpand_file_name (filename, Qnil);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
442
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
443 MAKE_LOCK_NAME (lfname, filename);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
445 owner = current_lock_owner (&locker, lfname);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 if (owner <= 0)
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
447 ret = Qnil;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
448 else if (owner == 2)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
449 ret = Qt;
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
450 else
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
451 ret = build_string (locker.user);
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 175
diff changeset
452
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
453 if (owner > 0)
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
454 FREE_LOCK_INFO (locker);
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
455
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
456 return ret;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
459
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
460 /* Initialization functions. */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
461
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 syms_of_filelock (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 /* This function can GC */
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
466 DEFSUBR (Funlock_buffer);
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
467 DEFSUBR (Flock_buffer);
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 14
diff changeset
468 DEFSUBR (Ffile_locked_p);
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 defsymbol (&Qask_user_about_supersession_threat,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 "ask-user-about-supersession-threat");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 defsymbol (&Qask_user_about_lock, "ask-user-about-lock");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 298
diff changeset
476 #endif /* CLASH_DETECTION */