Skip to contentSkip to conversation

Improve error handling in the btrfs command

PATCH In review Goffredo Baroncelli 2 participants 4 messages Dec 20, 2010 – Dec 21, 2010 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Improve error handling in the btrfs command

Hi Chris,

below is enclosed a trivial patch, which has the aim to improve the error reporting of the "btrfs" command.

You can pull from

http://cassiopea.homelinux.net/git/btrfs-progs-unstable.git

branch

strerror

I changed every printf("some-error") to something like:

	e = errno;
	fprintf(stderr, "ERROR: .... - %s", strerror(e));

so:

1) all the error are reported to standard error
2) At the end of the message is printed the error as returned by the system.

The change is quite simple, I replaced every printf("some-error") to the line above. I don't touched anything other.
I also integrated a missing "printf" on the basis of the Ben patch.

This patch leads the btrfs command to be more "user friendly" :-)

Regards
G.Baroncelli

 btrfs-list.c |   40 ++++++++++++++++++++++--------
 btrfs_cmds.c |   77 ++++++++++++++++++++++++++++++++++++++++-----------------
 utils.c      |    6 ++++
 3 files changed, 89 insertions(+), 34 deletions(-)
Signature · 2 lines
gpg key@ keyserver.linux.it: Goffredo Baroncelli (ghigo) <[email protected]>
Key fingerprint = 4769 7E51 5293 D36C 814E  C054 BF04 F161 3DC5 0512
btrfs-list.c +29 -11
index 93766a8..abcc2f4 100644
@@ -265,7 +265,7 @@ static int resolve_root(struct root_lookup *rl, struct root_info *ri)
 static int lookup_ino_path(int fd, struct root_info *ri)
 {
 	struct btrfs_ioctl_ino_lookup_args args;
-	int ret;
+	int ret, e;
 
 	if (ri->path)
 		return 0;
@@ -275,9 +275,11 @@ static int lookup_ino_path(int fd, struct root_info *ri)
 	args.objectid = ri->dir_id;
 
 	ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
+	e = errno;
 	if (ret) {
-		fprintf(stderr, "ERROR: Failed to lookup path for root %llu\n",
-			(unsigned long long)ri->ref_tree);
+		fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
+			(unsigned long long)ri->ref_tree,
+			strerror(e));
 		return ret;
 	}
 
@@ -320,15 +322,18 @@ static u64 find_root_gen(int fd)
 	unsigned long off = 0;
 	u64 max_found = 0;
 	int i;
+	int e;
 
 	memset(&ino_args, 0, sizeof(ino_args));
 	ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
 
 	/* this ioctl fills in ino_args->treeid */
 	ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
+	e = errno;
 	if (ret) {
-		fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu\n",
-			(unsigned long long)BTRFS_FIRST_FREE_OBJECTID);
+		fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
+			(unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
+			strerror(e));
 		return 0;
 	}
 
@@ -351,8 +356,10 @@ static u64 find_root_gen(int fd)
 
 	while (1) {
 		ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
+		e = errno;
 		if (ret < 0) {
-			fprintf(stderr, "ERROR: can't perform the search\n");
+			fprintf(stderr, "ERROR: can't perform the search - %s\n",
+				strerror(e));
 			return 0;
 		}
 		/* the ioctl returns the number of item it found in nr_items */
@@ -407,14 +414,16 @@ static char *__ino_resolve(int fd, u64 dirid)
 	struct btrfs_ioctl_ino_lookup_args args;
 	int ret;
 	char *full;
+	int e;
 
 	memset(&args, 0, sizeof(args));
 	args.objectid = dirid;
 
 	ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
+	e = errno;
 	if (ret) {
-		fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu\n",
-			(unsigned long long)dirid);
+		fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
+			(unsigned long long)dirid, strerror(e) );
 		return ERR_PTR(ret);
 	}
 
@@ -472,6 +481,7 @@ static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
 	struct btrfs_ioctl_search_header *sh;
 	unsigned long off = 0;
 	int namelen;
+	int e;
 
 	memset(&args, 0, sizeof(args));
 
@@ -490,8 +500,10 @@ static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
 	sk->nr_items = 1;
 
 	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
+	e = errno;
 	if (ret < 0) {
-		fprintf(stderr, "ERROR: can't perform the search\n");
+		fprintf(stderr, "ERROR: can't perform the search - %s\n",
+			strerror(e));
 		return NULL;
 	}
 	/* the ioctl returns the number of item it found in nr_items */
@@ -550,6 +562,7 @@ int list_subvols(int fd)
 	char *name;
 	u64 dir_id;
 	int i;
+	int e;
 
 	root_lookup_init(&root_lookup);
 
@@ -578,8 +591,10 @@ int list_subvols(int fd)
 
 	while(1) {
 		ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
+		e = errno;
 		if (ret < 0) {
-			fprintf(stderr, "ERROR: can't perform the search\n");
+			fprintf(stderr, "ERROR: can't perform the search - %s\n",
+				strerror(e));
 			return ret;
 		}
 		/* the ioctl returns the number of item it found in nr_items */
@@ -747,6 +762,7 @@ int find_updated_files(int fd, u64 root_id, u64 oldest_gen)
 	u64 found_gen;
 	u64 max_found = 0;
 	int i;
+	int e;
 	u64 cache_dirid = 0;
 	u64 cache_ino = 0;
 	char *cache_dir_name = NULL;
@@ -773,8 +789,10 @@ int find_updated_files(int fd, u64 root_id, u64 oldest_gen)
 	max_found = find_root_gen(fd);
 	while(1) {
 		ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
+		e = errno;
 		if (ret < 0) {
-			fprintf(stderr, "ERROR: can't perform the search\n");
+			fprintf(stderr, "ERROR: can't perform the search- %s\n",
+				strerror(e));
 			return ret;
 		}
 		/* the ioctl returns the number of item it found in nr_items */
btrfs_cmds.c +54 -23

265 lines not on this page · raw message

utils.c +6 -0
index fd894f3..3e22403 100644
@@ -821,6 +821,7 @@ void btrfs_register_one_device(char *fname)
 	struct btrfs_ioctl_vol_args args;
 	int fd;
 	int ret;
+	int e;
 
 	fd = open("/dev/btrfs-control", O_RDONLY);
 	if (fd < 0) {
@@ -830,6 +831,11 @@ void btrfs_register_one_device(char *fname)
 	}
 	strcpy(args.name, fname);
 	ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
+	e = errno;
+	if(ret<0){
+		fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
+			fname, strerror(e));
+	}
 	close(fd);
 }
 
#1 lore raw
Quoted 2 lines · Goffredo Baroncelli

below is enclosed a trivial patch, which has the aim to improve the error reporting of the "btrfs" command.

Any reason to not just use perror() ?

Signature · 1 line
 Chris Samuel  :  http://www.csamuel.org/  :  Melbourne, VIC
#2 lore raw
Quoted 6 lines · you (Chris Samuel)
Quoted 2 lines · Goffredo Baroncelli

below is enclosed a trivial patch, which has the aim to improve the error reporting of the "btrfs" command.

Any reason to not just use perror() ?

Some time I needed to add other info, so perror(3) may not be sufficient..

Signature · 2 lines
gpg key@ keyserver.linux.it: Goffredo Baroncelli (ghigo) <[email protected]>
Key fingerprint = 4769 7E51 5293 D36C 814E  C054 BF04 F161 3DC5 0512
#3 lore raw
Quoted 1 line · Goffredo Baroncelli

Some time I needed to add other info, so perror(3) may not be sufficient..

Ah, of course, and you cannot rely on safely snprintf()'ing something into the string would get passed to perror() because that could easily change errno if something went wrong internally.

All the best,
Chris

Signature · 1 line
 Chris Samuel  :  http://www.csamuel.org/  :  Melbourne, VIC