Enable -Wunused-but-set-variable flag by default.
[midnight-commander.git] / lib / lock.c
blob3a9fc9d78dc48a09369f995ba0990786a54a7cbc
1 /*
2 File locking
4 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Adam Byrtek, 2003
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /** \file
27 * \brief Source: file locking
28 * \author Adam Byrtek
29 * \date 2003
31 * Locking scheme is based on a documentation found
32 * in JED editor sources. Abstract from lock.c file (by John E. Davis):
34 * The basic idea here is quite simple. Whenever a buffer is attached to
35 * a file, and that buffer is modified, then attempt to lock the
36 * file. Moreover, before writing to a file for any reason, lock the
37 * file. The lock is really a protocol respected and not a real lock.
38 * The protocol is this: If in the directory of the file is a
39 * symbolic link with name ".#FILE", the FILE is considered to be locked
40 * by the process specified by the link.
43 #include <config.h>
45 #include <signal.h> /* kill() */
46 #include <stdio.h>
47 #include <stdarg.h>
48 #include <sys/types.h>
49 #include <unistd.h>
50 #include <string.h>
51 #include <ctype.h>
52 #include <errno.h>
53 #include <sys/stat.h>
54 #include <pwd.h>
55 #include <stdlib.h>
57 #include "lib/global.h"
58 #include "lib/vfs/vfs.h"
59 #include "lib/util.h" /* tilde_expand() */
60 #include "lib/lock.h"
61 #include "lib/widget.h" /* query_dialog() */
63 /*** global variables ****************************************************************************/
65 /*** file scope macro definitions ****************************************************************/
67 #define BUF_SIZE 255
68 #define PID_BUF_SIZE 10
70 /*** file scope type declarations ****************************************************************/
72 struct lock_s
74 char *who;
75 pid_t pid;
78 /*** file scope variables ************************************************************************/
80 /*** file scope functions ************************************************************************/
81 /* --------------------------------------------------------------------------------------------- */
82 /** \fn static char * lock_build_name (void)
83 * \brief builds user@host.domain.pid string (need to be freed)
84 * \return a pointer to lock filename
87 static char *
88 lock_build_name (void)
90 char host[BUF_SIZE];
91 const char *user = NULL;
92 struct passwd *pw;
94 pw = getpwuid (getuid ());
95 if (pw)
96 user = pw->pw_name;
97 if (!user)
98 user = getenv ("USER");
99 if (!user)
100 user = getenv ("USERNAME");
101 if (!user)
102 user = getenv ("LOGNAME");
103 if (!user)
104 user = "";
106 /** \todo Use FQDN, no clean interface, so requires lot of code */
107 if (gethostname (host, BUF_SIZE - 1) == -1)
108 *host = '\0';
110 return g_strdup_printf ("%s@%s.%d", user, host, (int) getpid ());
113 /* --------------------------------------------------------------------------------------------- */
115 static char *
116 lock_build_symlink_name (const vfs_path_t * fname_vpath)
118 const char *elpath;
119 char *str_filename, *str_dirname, *symlink_name;
121 /* get first path piece */
122 elpath = vfs_path_get_by_index (fname_vpath, 0)->path;
124 str_filename = g_path_get_basename (elpath);
125 str_dirname = g_path_get_dirname (elpath);
126 symlink_name = g_strconcat (str_dirname, PATH_SEP_STR ".#", str_filename, (char *) NULL);
127 g_free (str_dirname);
128 g_free (str_filename);
130 return symlink_name;
133 /* --------------------------------------------------------------------------------------------- */
135 * Extract pid from user@host.domain.pid string
138 static struct lock_s *
139 lock_extract_info (const char *str)
141 size_t i, len;
142 const char *p, *s;
143 static char pid[PID_BUF_SIZE], who[BUF_SIZE];
144 static struct lock_s lock;
146 len = strlen (str);
148 for (p = str + len - 1; p >= str; p--)
149 if (*p == '.')
150 break;
152 /* Everything before last '.' is user@host */
153 i = 0;
154 for (s = str; s < p && i < BUF_SIZE; s++)
155 who[i++] = *s;
156 who[i] = '\0';
158 /* Treat text between '.' and ':' or '\0' as pid */
159 i = 0;
160 for (p = p + 1; (p < str + len) && (*p != ':') && (i < PID_BUF_SIZE); p++)
161 pid[i++] = *p;
162 pid[i] = '\0';
164 lock.pid = (pid_t) atol (pid);
165 lock.who = who;
166 return &lock;
169 /* --------------------------------------------------------------------------------------------- */
171 * Extract user@host.domain.pid from lock file (static string)
174 static char *
175 lock_get_info (const char *lockfname)
177 int cnt;
178 static char buf[BUF_SIZE];
180 cnt = readlink (lockfname, buf, BUF_SIZE - 1);
181 if (cnt == -1 || *buf == '\0')
182 return NULL;
183 buf[cnt] = '\0';
184 return buf;
187 /* --------------------------------------------------------------------------------------------- */
188 /*** public functions ****************************************************************************/
189 /* --------------------------------------------------------------------------------------------- */
191 /* Tries to raise file lock
192 Returns 1 on success, 0 on failure, -1 if abort
193 Warning: Might do screen refresh and lose edit->force */
196 lock_file (const vfs_path_t * fname_vpath)
198 char *lockfname = NULL, *newlock, *msg, *lock;
199 struct stat statbuf;
200 struct lock_s *lockinfo;
201 gboolean is_local;
202 gboolean symlink_ok = FALSE;
203 const char *elpath;
205 if (fname_vpath == NULL)
206 return 0;
208 elpath = vfs_path_get_by_index (fname_vpath, 0)->path;
209 /* Just to be sure (and don't lock new file) */
210 if (*elpath == '\0')
211 return 0;
213 /* Locking on VFS is not supported */
214 is_local = vfs_file_is_local (fname_vpath);
215 if (is_local)
217 /* Check if already locked */
218 lockfname = lock_build_symlink_name (fname_vpath);
221 if (!is_local || lockfname == NULL)
222 return 0;
224 if (lstat (lockfname, &statbuf) == 0)
226 lock = lock_get_info (lockfname);
227 if (lock == NULL)
228 goto ret;
229 lockinfo = lock_extract_info (lock);
231 /* Check if locking process alive, ask user if required */
232 if (lockinfo->pid == 0 || !(kill (lockinfo->pid, 0) == -1 && errno == ESRCH))
234 msg =
235 g_strdup_printf (_
236 ("File \"%s\" is already being edited.\n"
237 "User: %s\nProcess ID: %d"), x_basename (lockfname) + 2,
238 lockinfo->who, (int) lockinfo->pid);
239 /* TODO: Implement "Abort" - needs to rewind undo stack */
240 switch (query_dialog
241 (_("File locked"), msg, D_NORMAL, 2, _("&Grab lock"), _("&Ignore lock")))
243 case 0:
244 break;
245 case 1:
246 case -1:
247 g_free (msg);
248 goto ret;
249 break; /* FIXME: unneeded? */
251 g_free (msg);
253 unlink (lockfname);
256 /* Create lock symlink */
257 newlock = lock_build_name ();
258 symlink_ok = (symlink (newlock, lockfname) != -1);
259 g_free (newlock);
261 ret:
262 g_free (lockfname);
263 return symlink_ok ? 1 : 0;
266 /* --------------------------------------------------------------------------------------------- */
268 * Lowers file lock if possible
269 * @return Always 0
273 unlock_file (const vfs_path_t * fname_vpath)
275 char *lockfname, *lock;
276 struct stat statbuf;
277 const char *elpath;
279 if (fname_vpath == NULL)
280 return 0;
282 elpath = vfs_path_get_by_index (fname_vpath, 0)->path;
283 /* Just to be sure (and don't lock new file) */
284 if (*elpath == '\0')
285 return 0;
287 lockfname = lock_build_symlink_name (fname_vpath);
289 if (lockfname == NULL)
290 return 0;
292 /* Check if lock exists */
293 if (lstat (lockfname, &statbuf) == -1)
294 goto ret;
296 lock = lock_get_info (lockfname);
297 if (lock != NULL)
299 /* Don't touch if lock is not ours */
300 if (lock_extract_info (lock)->pid != getpid ())
301 goto ret;
304 /* Remove lock */
305 unlink (lockfname);
307 ret:
308 g_free (lockfname);
309 return 0;
312 /* --------------------------------------------------------------------------------------------- */