btrfs-progs: add stat check in open_ctree_fs_info
authorAustin S. Hemmelgarn <ahferroin7@gmail.com>
Fri, 18 Mar 2016 14:03:42 +0000 (10:03 -0400)
committerDavid Sterba <dsterba@suse.com>
Fri, 18 Mar 2016 15:38:14 +0000 (16:38 +0100)
commit3519f83574c29a49e7e3e493f1bf4bb8dd5b0e33
tree65e11502cc23e289b4776af5700b859c97d7298e
parentf7fb93d558e4844930a1c74683e9a4725c058192
btrfs-progs: add stat check in open_ctree_fs_info

Currently, open_ctree_fs_info will open whatever path you pass it and try
to interpret it as a BTRFS filesystem.  While this is not nessecarily
dangerous (except possibly if done on a character device), it does
result in some rather cryptic and non-sensical error messages when
trying to run certain commands in ways they weren't intended to be run.
Add a check using stat(2) to verify that the path we've been passed is
in fact a regular file or a block device, or a symlink pointing to a
regular file or block device.

This causes the following commands to provide a helpful error message
when run on a FIFO, directory, character device, or socket:
    * btrfs check
    * btrfs restore
    * btrfs-image
    * btrfs-find-root
    * btrfs inspect-internal dump-tree

stat(2) is used instead of lstat(2), as stat(2) follows symlinks just
like open(2) does, which means we check the same inode that open(2)
opens, and thus don't need special handling for symlinks.

Signed-off-by: Austin S. Hemmelgarn <ahferroin7@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
disk-io.c