mirror of
https://github.com/kdave/btrfs-progs
synced 2025-03-05 18:28:24 +00:00
Btrfs-progs: make sure we are opening a file or dir with open_file_or_dir()
Previously, open_file_or_dir() will open block device successfully, however, we should enhance such checks to make sure we are really opening a file or dir. Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <clm@fb.com>
This commit is contained in:
parent
d9d9704510
commit
80d5bf799e
13
utils.c
13
utils.c
@ -1571,13 +1571,20 @@ int open_file_or_dir(const char *fname, DIR **dirstream)
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
*dirstream = opendir(fname);
|
||||
if (!*dirstream)
|
||||
return -2;
|
||||
return -1;
|
||||
fd = dirfd(*dirstream);
|
||||
} else {
|
||||
} else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
|
||||
fd = open(fname, O_RDWR);
|
||||
} else {
|
||||
/*
|
||||
* we set this on purpose, in case the caller output
|
||||
* strerror(errno) as success
|
||||
*/
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (fd < 0) {
|
||||
fd = -3;
|
||||
fd = -1;
|
||||
if (*dirstream)
|
||||
closedir(*dirstream);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user