Ticket #1396: Fixed logic of processing '--with-search-engine' configure option.
[midnight-commander.git] / edit / editlock.c
blobb1fa61dcb646db28cbae9035fa25234cb4014810
1 /* editor file locking.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 Authors: 2003 Adam Byrtek
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.
24 /** \file
25 * \brief Source: editor file locking
26 * \author Adam Byrtek
27 * \date 2003
29 * Locking scheme used in mcedit is based on a documentation found
30 * in JED editor sources. Abstract from lock.c file (by John E. Davis):
32 * The basic idea here is quite simple. Whenever a buffer is attached to
33 * a file, and that buffer is modified, then attempt to lock the
34 * file. Moreover, before writing to a file for any reason, lock the
35 * file. The lock is really a protocol respected and not a real lock.
36 * The protocol is this: If in the directory of the file is a
37 * symbolic link with name ".#FILE", the FILE is considered to be locked
38 * by the process specified by the link.
41 #include <config.h>
43 #include <signal.h> /* kill() */
44 #include <stdio.h>
45 #include <stdarg.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <ctype.h>
50 #include <errno.h>
51 #include <sys/stat.h>
52 #include <pwd.h>
53 #include <stdlib.h>
55 #include "../src/global.h"
57 #include "edit-impl.h"
58 #include "editlock.h"
60 #include "../src/wtools.h" /* edit_query_dialog () */
61 #include "../src/strutil.h" /* utf string functions */
63 #define BUF_SIZE 255
64 #define PID_BUF_SIZE 10
66 struct lock_s {
67 char *who;
68 pid_t pid;
71 /** \fn static char * lock_build_name (void)
72 * \brief builds user@host.domain.pid string (need to be freed)
73 * \return a pointer to lock filename
75 static char *
76 lock_build_name (void)
78 char host[BUF_SIZE];
79 const char *user = NULL;
80 struct passwd *pw;
82 pw = getpwuid (getuid ());
83 if (pw) user = pw->pw_name;
84 if (!user) user = getenv ("USER");
85 if (!user) user = getenv ("USERNAME");
86 if (!user) user = getenv ("LOGNAME");
87 if (!user) user = "";
89 /** \todo Use FQDN, no clean interface, so requires lot of code */
90 if (gethostname (host, BUF_SIZE - 1) == -1)
91 *host = '\0';
93 return g_strdup_printf ("%s@%s.%d", user, host, (int) getpid ());
96 static char *
97 lock_build_symlink_name (const char *fname)
99 char *fname_copy, *symlink_name;
100 char absolute_fname[PATH_MAX];
102 if (mc_realpath (fname, absolute_fname) == NULL)
103 return NULL;
105 fname = x_basename (absolute_fname);
106 fname_copy = g_strdup (fname);
107 absolute_fname[fname - absolute_fname] = '\0';
108 symlink_name = g_strconcat (absolute_fname, ".#", fname_copy, (char *) NULL);
109 g_free (fname_copy);
111 return symlink_name;
114 /* Extract pid from user@host.domain.pid string */
115 static struct lock_s *
116 lock_extract_info (const char *str)
118 int i;
119 const char *p, *s;
120 static char pid[PID_BUF_SIZE], who[BUF_SIZE];
121 static struct lock_s lock;
123 for (p = str + str_term_width1 (str) - 1; p >= str; p--)
124 if (*p == '.')
125 break;
127 /* Everything before last '.' is user@host */
128 i = 0;
129 for (s = str; s < p && i < BUF_SIZE; s++)
130 who[i++] = *s;
131 who[i] = '\0';
133 /* Treat text between '.' and ':' or '\0' as pid */
134 i = 0;
135 for (p = p + 1;
136 p < str + str_term_width1 (str) && *p != ':' && i < PID_BUF_SIZE; p++)
137 pid[i++] = *p;
138 pid[i] = '\0';
140 lock.pid = (pid_t) atol (pid);
141 lock.who = who;
142 return &lock;
145 /* Extract user@host.domain.pid from lock file (static string) */
146 static char *
147 lock_get_info (const char *lockfname)
149 int cnt;
150 static char buf[BUF_SIZE];
152 if ((cnt = readlink (lockfname, buf, BUF_SIZE - 1)) == -1 || !*buf)
153 return NULL;
154 buf[cnt] = '\0';
155 return buf;
159 /* Tries to raise file lock
160 Returns 1 on success, 0 on failure, -1 if abort
161 Warning: Might do screen refresh and lose edit->force */
163 edit_lock_file (const char *fname)
165 char *lockfname, *newlock, *msg, *lock;
166 struct stat statbuf;
167 struct lock_s *lockinfo;
169 /* Just to be sure (and don't lock new file) */
170 if (!fname || !*fname)
171 return 0;
173 /* Locking on VFS is not supported */
174 if (!vfs_file_is_local (fname))
175 return 0;
177 /* Check if already locked */
178 lockfname = lock_build_symlink_name (fname);
179 if (lockfname == NULL)
180 return 0;
181 if (lstat (lockfname, &statbuf) == 0) {
182 lock = lock_get_info (lockfname);
183 if (!lock) {
184 g_free (lockfname);
185 return 0;
187 lockinfo = lock_extract_info (lock);
189 /* Check if locking process alive, ask user if required */
190 if (!lockinfo->pid
191 || !(kill (lockinfo->pid, 0) == -1 && errno == ESRCH)) {
192 msg =
193 g_strdup_printf (_
194 ("File \"%s\" is already being edited\n"
195 "User: %s\nProcess ID: %d"), x_basename (lockfname) + 2,
196 lockinfo->who, (int) lockinfo->pid);
197 /* TODO: Implement "Abort" - needs to rewind undo stack */
198 switch (edit_query_dialog2
199 (_("File locked"), msg, _("&Grab lock"),
200 _("&Ignore lock"))) {
201 case 0:
202 break;
203 case 1:
204 case -1:
205 g_free (lockfname);
206 g_free (msg);
207 return 0;
209 g_free (msg);
211 unlink (lockfname);
214 /* Create lock symlink */
215 newlock = lock_build_name ();
216 if (symlink (newlock, lockfname) == -1) {
217 g_free (lockfname);
218 g_free (newlock);
219 return 0;
222 g_free (lockfname);
223 g_free (newlock);
224 return 1;
227 /* Lowers file lock if possible
228 Always returns 0 to make 'lock = edit_unlock_file (f)' possible */
230 edit_unlock_file (const char *fname)
232 char *lockfname, *lock;
233 struct stat statbuf;
235 /* Just to be sure */
236 if (!fname || !*fname)
237 return 0;
239 lockfname = lock_build_symlink_name (fname);
240 if (lockfname == NULL)
241 return 0;
243 /* Check if lock exists */
244 if (lstat (lockfname, &statbuf) == -1) {
245 g_free (lockfname);
246 return 0;
249 lock = lock_get_info (lockfname);
250 if (lock) {
251 /* Don't touch if lock is not ours */
252 if (lock_extract_info (lock)->pid != getpid ()) {
253 g_free (lockfname);
254 return 0;
258 /* Remove lock */
259 unlink (lockfname);
260 g_free (lockfname);
261 return 0;