linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dan Merillat <dan.merillat@gmail.com>
To: David Sterba <dsterba@suse.cz>, BTRFS <linux-btrfs@vger.kernel.org>
Subject: [PATCH 2/3] btrfs-progs: separate the overwrite check.
Date: Thu, 23 Apr 2015 12:51:33 -0400	[thread overview]
Message-ID: <55392315.8040306@gmail.com> (raw)
In-Reply-To: <55392221.6090502@gmail.com>

Symlink restore needs this, but the cut&paste became
too complicated.  Simplify everything.

Signed-off-by: Dan Merillat <dan.merillat@gmail.com>
---
 cmds-restore.c | 53 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 19 deletions(-)

diff --git a/cmds-restore.c b/cmds-restore.c
index e877548..8869f2a 100644
--- a/cmds-restore.c
+++ b/cmds-restore.c
@@ -781,6 +781,37 @@ out:
 	return ret;
 }
 
+/* returns:
+ *  0 if the file exists and should be skipped.
+ *  1 if the file does NOT exist
+ *  2 if the file exists but is OK to overwrite
+ */
+
+static int overwrite_ok(const char * path)
+{
+	static int warn = 0;
+	struct stat st;
+	int ret;
+
+	/* don't be fooled by symlinks */
+	ret = fstatat(-1, path_name, &st, AT_SYMLINK_NOFOLLOW);
+
+	if (!ret) {
+		if (overwrite)
+			return 2;
+
+		if (verbose || !warn)
+			printf("Skipping existing file"
+				   " %s\n", path);
+		if (!warn)
+			printf("If you wish to overwrite use "
+				   "the -o option to overwrite\n");
+		warn = 1;
+		return 0;
+	}
+	return 1;
+}
+
 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
 		      const char *output_rootdir, const char *in_dir,
 		      const regex_t *mreg)
@@ -897,25 +928,9 @@ static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
 		 * files, no symlinks or anything else.
 		 */
 		if (type == BTRFS_FT_REG_FILE) {
-			if (!overwrite) {
-				static int warn = 0;
-				struct stat st;
-
-				ret = stat(path_name, &st);
-				if (!ret) {
-					loops = 0;
-					if (verbose || !warn)
-						printf("Skipping existing file"
-						       " %s\n", path_name);
-					if (warn)
-						goto next;
-					printf("If you wish to overwrite use "
-					       "the -o option to overwrite\n");
-					warn = 1;
-					goto next;
-				}
-				ret = 0;
-			}
+			if (!overwrite_ok(path_name))
+				goto next;
+
 			if (verbose)
 				printf("Restoring %s\n", path_name);
 			if (dry_run)
-- 
2.1.4


  parent reply	other threads:[~2015-04-23 16:51 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-23 16:47 [PATCH 0/3] btrfs-progs: restore symlinks Dan Merillat
2015-04-23 16:50 ` [PATCH 1/3] btrfs-progs: restore: document metadata restore Dan Merillat
2015-04-23 16:51 ` Dan Merillat [this message]
2015-04-24 15:24   ` [PATCH 2/3] btrfs-progs: separate the overwrite check David Sterba
2015-04-25 18:19     ` Dan Merillat
2015-04-29 15:38       ` David Sterba
2015-04-23 16:52 ` [PATCH 3/3] btrfs-progs: optionally restore symlinks Dan Merillat
2015-04-24  4:38 ` [PATCH 0/3] btrfs-progs: " Duncan
2015-04-25 18:16   ` Dan Merillat

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=55392315.8040306@gmail.com \
    --to=dan.merillat@gmail.com \
    --cc=dsterba@suse.cz \
    --cc=linux-btrfs@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).