From 69a826f187cbef2acf3fda2512f28ea31c42933c Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sat, 4 Aug 2012 09:33:40 +0200 Subject: [PATCH] vipw: make vim writebackup mode work MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Some editors, such as Vim with 'writebackup' mode enabled, use "atomic save" in which the old file is deleted and a new one with the same name created in its place. The vipw tries to detect if such happen by looking hard temporary file link count, when it is zero reopen temporary file by using it's path. Reported-by: Mantas Mikulėnas References: http://www.spinics.net/lists/util-linux-ng/msg06666.html Signed-off-by: Sami Kerola --- login-utils/vipw.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/login-utils/vipw.c b/login-utils/vipw.c index ed3f43bec5..1eeeb0dd7c 100644 --- a/login-utils/vipw.c +++ b/login-utils/vipw.c @@ -275,6 +275,18 @@ static void edit_file(int is_shadow) if (fstat(fileno(tmp_fd), &end)) pw_error(tmp_file, 1, 1); + /* Some editors, such as Vim with 'writebackup' mode enabled, + * use "atomic save" in which the old file is deleted and a new + * one with the same name created in its place. */ + if (end.st_nlink == 0) { + if (close_stream(tmp_fd) != 0) + err(EXIT_FAILURE, _("write error")); + tmp_fd = fopen(tmp_file, "r"); + if (!tmp_file) + err(EXIT_FAILURE, _("cannot open %s"), tmp_file); + if (fstat(fileno(tmp_fd), &end)) + pw_error(tmp_file, 1, 1); + } if (begin.st_mtime == end.st_mtime) { warnx(_("no changes made")); pw_error((char *)NULL, 0, 0); -- 2.47.3