util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 5/5] vipw: make vim writebackup mode work
Date: Sat,  4 Aug 2012 09:33:40 +0200	[thread overview]
Message-ID: <1344065620-17480-6-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1344065620-17480-1-git-send-email-kerolasa@iki.fi>

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 <grawity@gmail.com>
References: http://www.spinics.net/lists/util-linux-ng/msg06666.html
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 login-utils/vipw.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/login-utils/vipw.c b/login-utils/vipw.c
index ed3f43b..1eeeb0d 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);
-- 
1.7.11.4


  parent reply	other threads:[~2012-08-04  7:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-04  7:33 [PATCH 0/5] pull: release preparations, umount.8 and vipw fix Sami Kerola
2012-08-04  7:33 ` [PATCH 1/5] docs: ftp server does no longer support util-linux-ng Sami Kerola
2012-08-04  7:33 ` [PATCH 2/5] docs: update TODO Sami Kerola
2012-08-04  7:33 ` [PATCH 3/5] docs: add sys-utils/umount.8 manual page Sami Kerola
2012-08-04  7:33 ` [PATCH 4/5] build-sys: add files make check generates in gitignore Sami Kerola
2012-08-04  7:33 ` Sami Kerola [this message]
2012-08-06  6:14   ` [PATCH 5/5] vipw: make vim writebackup mode work Bernhard Voelker
2012-08-06  7:30     ` Sami Kerola
2012-08-06  8:28       ` Bernhard Voelker
2012-08-06 13:40 ` [PATCH 0/5] pull: release preparations, umount.8 and vipw fix Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1344065620-17480-6-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).