Skip to contentSkip to conversation

btrfs-progs: add always option to restore's looping prompt

PATCH Superseded Justin Maggard 2 participants 2 messages Jul 29, 2014 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs-progs: add always option to restore's looping prompt

If you are using btrfs restore to try to recover a very large or fragmented file, you may encounter _lots_ of prompts requiring you to press 'y' to continue because we are looping a lot.

Add the option to press 'a', to supress these prompts for the rest of the file.

---
 cmds-restore.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
cmds-restore.c +11 -6
index 96b97e1..ae95d5a 100644
@@ -410,17 +410,19 @@ static int ask_to_continue(const char *file)
 	char *ret;
 
 	printf("We seem to be looping a lot on %s, do you want to keep going "
-	       "on ? (y/N): ", file);
+	       "on ? (y/N/a): ", file);
 again:
 	ret = fgets(buf, 2, stdin);
 	if (*ret == '\n' || tolower(*ret) == 'n')
-		return 1;
+		return 0;
+	if (tolower(*ret) == 'a')
+		return 2;
 	if (tolower(*ret) != 'y') {
 		printf("Please enter either 'y' or 'n': ");
 		goto again;
 	}
 
-	return 0;
+	return 1;
 }
 
 
@@ -588,11 +590,14 @@ static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
 	}
 
 	while (1) {
-		if (loops++ >= 1024) {
+		if (loops >= 0 && loops++ >= 1024) {
 			ret = ask_to_continue(file);
-			if (ret)
+			if (ret == 0)
 				break;
-			loops = 0;
+			else if (ret == 1)
+				loops = 0;
+			else if (ret == 2)
+				loops = -1;
 		}
 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
 			do {
Signature · 1 line
1.9.1
#1 lore raw

Hi Justin,

Thanks for the patch first, comment inlined below.
-------- Original Message --------
Subject: [PATCH] btrfs-progs: add always option to restore's looping prompt
From: Justin Maggard <[email protected]>
To: <[email protected]>
Date: 2014年07月29日 08:03

Quoted 19 lines

If you are using btrfs restore to try to recover a very large or fragmented file, you may encounter _lots_ of prompts requiring you to press 'y' to continue because we are looping a lot.

Add the option to press 'a', to supress these prompts for the rest of the file.

---
  cmds-restore.c | 17 +++++++++++------
  1 file changed, 11 insertions(+), 6 deletions(-)
cmds-restore.c +0 -1
index 96b97e1..ae95d5a 100644
@@ -410,17 +410,19 @@ static int ask_to_continue(const char *file)
  	char *ret;
 
  	printf("We seem to be looping a lot on %s, do you want to keep going "
-	       "on ? (y/N): ", file);

cmds-restore.c static int ask_to_continue(const char *file) · line 421

Quoted 10 lines
cmds-restore.c +4 -1
+	       "on ? (y/N/a): ", file);
  again:
  	ret = fgets(buf, 2, stdin);
  	if (*ret == '\n' || tolower(*ret) == 'n')
-		return 1;
+		return 0;
+	if (tolower(*ret) == 'a')
+		return 2;
  	if (tolower(*ret) != 'y') {
  		printf("Please enter either 'y' or 'n': ");

Since option 'a' is added, it would be better to change the prompt and add 'a' to it.

cmds-restore.c static int ask_to_continue(const char *file) · line 426

Quoted 6 lines
cmds-restore.c +1 -1
  		goto again;
  	}
 
-	return 0;
+	return 1;
  }

If add any comment or change the return value from immediate number to more meaningful defined number, it would be much more easier to understand and expend.
Especially when the return value has difference meaning with original one.

Quoted 10 lines
cmds-restore.c +1 -2
@@ -588,11 +590,14 @@ static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
  	}
 
  	while (1) {
-		if (loops++ >= 1024) {
+		if (loops >= 0 && loops++ >= 1024) {
  			ret = ask_to_continue(file);
-			if (ret)

cmds-restore.c static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key, · line 603

Quoted 10 lines
cmds-restore.c +5 -1
+			if (ret == 0)
  				break;
-			loops = 0;
+			else if (ret == 1)
+				loops = 0;
+			else if (ret == 2)
+				loops = -1;
  		}
  		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  			do {

Also, just a suggestion and you can ignore it without any problem,
utils.c provide ask_to_continue() function to provide similar behavior, what about improve the one in utils.c and reuse the ask_to_continue() function?

Thanks,
Qu