diff mbox

btrfs-progs: fix bug on mkfs with relative path specified

Message ID 1394533467-24967-1-git-send-email-guihc.fnst@cn.fujitsu.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Gui Hecheng March 11, 2014, 10:24 a.m. UTC
The bug accurs when exec:
	# mkfs.btrfs -r <a relative path> <device>
	(note: the path should be 'valid' correspond to your `pwd`)
error msg:
	$ scandir for <a relative path> failed: No such file...

Replace strdup() with realpath() to get the correct scan path.

Reported-by: Saul Wold <sgw@linux.intel.com>
Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
---
 mkfs.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

David Sterba March 12, 2014, 4:01 p.m. UTC | #1
On Tue, Mar 11, 2014 at 06:24:27PM +0800, Gui Hecheng wrote:
> @@ -764,7 +765,7 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
>  	/* Add list for source directory */
>  	dir_entry = malloc(sizeof(struct directory_name_entry));
>  	dir_entry->dir_name = dir_name;
> -	dir_entry->path = strdup(dir_name);
> +	dir_entry->path = realpath(dir_name, real_path);

The fix is ok, but the return value of realpath() has to be checked.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/mkfs.c b/mkfs.c
index 2dc90c2..1bd3069 100644
--- a/mkfs.c
+++ b/mkfs.c
@@ -756,6 +756,7 @@  static int traverse_directory(struct btrfs_trans_handle *trans,
 	ino_t parent_inum, cur_inum;
 	ino_t highest_inum = 0;
 	char *parent_dir_name;
+	char real_path[PATH_MAX];
 	struct btrfs_path path;
 	struct extent_buffer *leaf;
 	struct btrfs_key root_dir_key;
@@ -764,7 +765,7 @@  static int traverse_directory(struct btrfs_trans_handle *trans,
 	/* Add list for source directory */
 	dir_entry = malloc(sizeof(struct directory_name_entry));
 	dir_entry->dir_name = dir_name;
-	dir_entry->path = strdup(dir_name);
+	dir_entry->path = realpath(dir_name, real_path);
 
 	parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
 	dir_entry->inum = parent_inum;
@@ -876,7 +877,6 @@  static int traverse_directory(struct btrfs_trans_handle *trans,
 		}
 
 		free_namelist(files, count);
-		free(parent_dir_entry->path);
 		free(parent_dir_entry);
 
 		index_cnt = 2;
@@ -887,7 +887,6 @@  static int traverse_directory(struct btrfs_trans_handle *trans,
 fail:
 	free_namelist(files, count);
 fail_no_files:
-	free(parent_dir_entry->path);
 	free(parent_dir_entry);
 	return -1;
 }