Expand tilde while locking and unlocking files.
[pantumic.git] / lib / lock.c
blob3624e7d38de344b5c177b7e4b872c83757aec46f
1 /* 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: file locking
26 * \author Adam Byrtek
27 * \date 2003
29 * Locking scheme 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 "lib/global.h"
56 #include "lib/vfs/mc-vfs/vfs.h"
57 #include "lib/util.h" /* tilde_expand() */
58 #include "lib/lock.h"
60 #include "src/wtools.h" /* query_dialog() */
62 #define BUF_SIZE 255
63 #define PID_BUF_SIZE 10
65 struct lock_s {
66 char *who;
67 pid_t pid;
70 /** \fn static char * lock_build_name (void)
71 * \brief builds user@host.domain.pid string (need to be freed)
72 * \return a pointer to lock filename
74 static char *
75 lock_build_name (void)
77 char host[BUF_SIZE];
78 const char *user = NULL;
79 struct passwd *pw;
81 pw = getpwuid (getuid ());
82 if (pw) user = pw->pw_name;
83 if (!user) user = getenv ("USER");
84 if (!user) user = getenv ("USERNAME");
85 if (!user) user = getenv ("LOGNAME");
86 if (!user) user = "";
88 /** \todo Use FQDN, no clean interface, so requires lot of code */
89 if (gethostname (host, BUF_SIZE - 1) == -1)
90 *host = '\0';
92 return g_strdup_printf ("%s@%s.%d", user, host, (int) getpid ());
95 static char *
96 lock_build_symlink_name (const char *fname)
98 char *fname_copy, *symlink_name;
99 char absolute_fname[PATH_MAX];
101 if (mc_realpath (fname, absolute_fname) == NULL)
102 return NULL;
104 fname = x_basename (absolute_fname);
105 fname_copy = g_strdup (fname);
106 absolute_fname[fname - absolute_fname] = '\0';
107 symlink_name = g_strconcat (absolute_fname, ".#", fname_copy, (char *) NULL);
108 g_free (fname_copy);
110 return symlink_name;
113 /* Extract pid from user@host.domain.pid string */
114 static struct lock_s *
115 lock_extract_info (const char *str)
117 size_t i, len;
118 const char *p, *s;
119 static char pid[PID_BUF_SIZE], who[BUF_SIZE];
120 static struct lock_s lock;
122 len = strlen (str);
124 for (p = str + len - 1; p >= str; p--)
125 if (*p == '.')
126 break;
128 /* Everything before last '.' is user@host */
129 i = 0;
130 for (s = str; s < p && i < BUF_SIZE; s++)
131 who[i++] = *s;
132 who[i] = '\0';
134 /* Treat text between '.' and ':' or '\0' as pid */
135 i = 0;
136 for (p = p + 1; (p < str + len) && (*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 cnt = readlink (lockfname, buf, BUF_SIZE - 1);
153 if (cnt == -1 || *buf == '\0')
154 return NULL;
155 buf[cnt] = '\0';
156 return buf;
160 /* Tries to raise file lock
161 Returns 1 on success, 0 on failure, -1 if abort
162 Warning: Might do screen refresh and lose edit->force */
164 lock_file (const char *fname)
166 char *lockfname, *newlock, *msg, *lock;
167 struct stat statbuf;
168 struct lock_s *lockinfo;
169 gboolean symlink_ok;
171 /* Just to be sure (and don't lock new file) */
172 if (fname == NULL || *fname == '\0')
173 return 0;
175 fname = tilde_expand (fname);
177 /* Locking on VFS is not supported */
178 if (!vfs_file_is_local (fname))
180 g_free (fname);
181 return 0;
184 /* Check if already locked */
185 lockfname = lock_build_symlink_name (fname);
186 g_free (fname);
187 if (lockfname == NULL)
188 return 0;
190 if (lstat (lockfname, &statbuf) == 0) {
191 lock = lock_get_info (lockfname);
192 if (lock == NULL) {
193 g_free (lockfname);
194 return 0;
196 lockinfo = lock_extract_info (lock);
198 /* Check if locking process alive, ask user if required */
199 if (lockinfo->pid == 0
200 || !(kill (lockinfo->pid, 0) == -1 && errno == ESRCH)) {
201 msg =
202 g_strdup_printf (_
203 ("File \"%s\" is already being edited.\n"
204 "User: %s\nProcess ID: %d"), x_basename (lockfname) + 2,
205 lockinfo->who, (int) lockinfo->pid);
206 /* TODO: Implement "Abort" - needs to rewind undo stack */
207 switch (query_dialog
208 (_("File locked"), msg, D_NORMAL, 2, _("&Grab lock"),
209 _("&Ignore lock"))) {
210 case 0:
211 break;
212 case 1:
213 case -1:
214 g_free (lockfname);
215 g_free (msg);
216 return 0;
218 g_free (msg);
220 unlink (lockfname);
223 /* Create lock symlink */
224 newlock = lock_build_name ();
225 symlink_ok = (symlink (newlock, lockfname) != -1);
226 g_free (newlock);
227 g_free (lockfname);
229 return symlink_ok ? 1 : 0;
232 /* Lowers file lock if possible
233 Always returns 0 */
235 unlock_file (const char *fname)
237 char *lockfname, *lock;
238 struct stat statbuf;
240 /* Just to be sure */
241 if (fname == NULL || *fname == '\0')
242 return 0;
244 fname = tilde_expand (fname);
245 lockfname = lock_build_symlink_name (fname);
246 g_free (fname);
248 if (lockfname == NULL)
249 return 0;
251 /* Check if lock exists */
252 if (lstat (lockfname, &statbuf) == -1) {
253 g_free (lockfname);
254 return 0;
257 lock = lock_get_info (lockfname);
258 if (lock != NULL) {
259 /* Don't touch if lock is not ours */
260 if (lock_extract_info (lock)->pid != getpid ()) {
261 g_free (lockfname);
262 return 0;
266 /* Remove lock */
267 unlink (lockfname);
268 g_free (lockfname);
269 return 0;