From 6cecf0f6735153458bc3f6b1de6b81924a507353 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Thu, 8 Jul 2021 01:15:28 +0800 Subject: [PATCH] [file_utils] Check the file type to prevent unexpected writes by thin_repair --- base/file_utils.cc | 8 ++++++-- tests/thin_repair.rs | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/base/file_utils.cc b/base/file_utils.cc index af7ce2a..638c752 100644 --- a/base/file_utils.cc +++ b/base/file_utils.cc @@ -77,8 +77,12 @@ file_utils::check_file_exists(string const &file, bool must_be_regular_file) { throw runtime_error(msg.str()); } - if (must_be_regular_file && !S_ISREG(info.st_mode)) - throw runtime_error("Not a regular file"); + if (!S_ISREG(info.st_mode)) { + if (must_be_regular_file) + throw runtime_error("Not a regular file"); + if (!S_ISBLK(info.st_mode)) + throw runtime_error("Not a block device or regular file"); + } } file_utils::file_descriptor diff --git a/tests/thin_repair.rs b/tests/thin_repair.rs index 31ecaa8..aa2deba 100644 --- a/tests/thin_repair.rs +++ b/tests/thin_repair.rs @@ -81,6 +81,7 @@ test_accepts_version!(ThinRepair); test_rejects_bad_option!(ThinRepair); test_input_file_not_found!(ThinRepair); +test_input_cannot_be_a_directory!(ThinRepair); test_corrupted_input_data!(ThinRepair); test_missing_output_option!(ThinRepair);