diff --git a/base/file_utils.cc b/base/file_utils.cc index e6095f7..af7ce2a 100644 --- a/base/file_utils.cc +++ b/base/file_utils.cc @@ -71,8 +71,11 @@ void file_utils::check_file_exists(string const &file, bool must_be_regular_file) { struct stat info; int r = ::stat(file.c_str(), &info); - if (r) - throw runtime_error("Couldn't stat file"); + if (r) { + ostringstream msg; + msg << file << ": " << base::error_string(errno); + throw runtime_error(msg.str()); + } if (must_be_regular_file && !S_ISREG(info.st_mode)) throw runtime_error("Not a regular file"); @@ -116,8 +119,11 @@ file_utils::get_file_length(string const &file) { uint64_t nr_bytes; int r = ::stat(file.c_str(), &info); - if (r) - throw runtime_error("Couldn't stat path"); + if (r) { + ostringstream msg; + msg << file << ": " << base::error_string(errno); + throw runtime_error(msg.str()); + } if (S_ISREG(info.st_mode)) // It's okay to cast st_size to a uint64_t value. @@ -136,9 +142,11 @@ file_utils::get_file_length(string const &file) { throw runtime_error("ioctl BLKGETSIZE64 failed"); } ::close(fd); - } else - // FIXME: needs a better message - throw runtime_error("bad path"); + } else { + ostringstream msg; + msg << file << ": " << "Not a block device or regular file"; + throw runtime_error(msg.str()); + } return nr_bytes; } diff --git a/functional-tests/cache-functional-tests.scm b/functional-tests/cache-functional-tests.scm index 13dcaee..b8e2f41 100644 --- a/functional-tests/cache-functional-tests.scm +++ b/functional-tests/cache-functional-tests.scm @@ -82,9 +82,12 @@ (define-scenario (cache-restore missing-input-file) "the input file can't be found" (with-empty-metadata (md) - (run-fail-rcv (_ stderr) (cache-restore "-i no-such-file -o" md) - (assert-superblock-all-zeroes md) - (assert-starts-with "Couldn't stat file" stderr)))) + (let ((bad-path "no-such-file")) + (run-fail-rcv (_ stderr) (cache-restore "-i" bad-path "-o" md) + (assert-superblock-all-zeroes md) + (assert-starts-with + (string-append bad-path ": No such file or directory") + stderr))))) (define-scenario (cache-restore garbage-input-file) "the input file is just zeroes" @@ -264,9 +267,12 @@ (define-scenario (cache-repair missing-input-file) "the input file can't be found" (with-empty-metadata (md) - (run-fail-rcv (_ stderr) (cache-repair "-i no-such-file -o" md) - (assert-superblock-all-zeroes md) - (assert-starts-with "Couldn't stat path" stderr)))) + (let ((bad-path "no-such-file")) + (run-fail-rcv (_ stderr) (cache-repair "-i no-such-file -o" md) + (assert-superblock-all-zeroes md) + (assert-starts-with + (string-append bad-path ": No such file or directory") + stderr))))) (define-scenario (cache-repair garbage-input-file) "the input file is just zeroes" diff --git a/functional-tests/era-functional-tests.scm b/functional-tests/era-functional-tests.scm index 890f0ff..a81b41b 100644 --- a/functional-tests/era-functional-tests.scm +++ b/functional-tests/era-functional-tests.scm @@ -152,9 +152,12 @@ (define-scenario (era-restore missing-input-file) "the input file can't be found" (with-empty-metadata (md) - (run-fail-rcv (_ stderr) (era-restore "-i no-such-file -o" md) - (assert-superblock-all-zeroes md) - (assert-starts-with "Couldn't stat file" stderr)))) + (let ((bad-path "no-such-file")) + (run-fail-rcv (_ stderr) (era-restore "-i no-such-file -o" md) + (assert-superblock-all-zeroes md) + (assert-starts-with + (string-append bad-path ": No such file or directory") + stderr))))) (define-scenario (era-restore garbage-input-file) "the input file is just zeroes" @@ -197,7 +200,9 @@ (with-empty-metadata (md) (run-fail-rcv (stdout stderr) (era-restore "--quiet" "-i" bad-xml "-o" md) (assert-eof stdout) - (assert-starts-with "Couldn't stat file" stderr))))) + (assert-starts-with + (string-append bad-xml ": No such file or directory") + stderr))))) (define-scenario (era-restore q-fail) "No output with --q(failing)" @@ -205,7 +210,9 @@ (with-empty-metadata (md) (run-fail-rcv (stdout stderr) (era-restore "-q" "-i" bad-xml "-o" md) (assert-eof stdout) - (assert-starts-with "Couldn't stat file" stderr))))) + (assert-starts-with + (string-append bad-xml ": No such file or directory") + stderr))))) ;;;----------------------------------------------------------- ;;; era_dump scenarios diff --git a/tests/cache_check.rs b/tests/cache_check.rs index 8cf716c..f9ec093 100644 --- a/tests/cache_check.rs +++ b/tests/cache_check.rs @@ -40,16 +40,16 @@ fn accepts_help() -> Result<()> { } #[test] -fn missing_metadata() -> Result<()> { +fn missing_input_arg() -> Result<()> { let stderr = run_fail(cache_check!())?; assert!(stderr.contains(msg::MISSING_INPUT_ARG)); Ok(()) } #[test] -fn no_such_metadata() -> Result<()> { +fn input_file_not_found() -> Result<()> { let stderr = run_fail(cache_check!("/arbitrary/filename"))?; - assert!(stderr.contains("No such file or directory")); + assert!(stderr.contains(msg::FILE_NOT_FOUND)); Ok(()) } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index f47347f..9dd69f6 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -20,7 +20,7 @@ use test_dir::TestDir; #[cfg(not(feature = "rust_tests"))] pub mod msg { - pub const FILE_NOT_FOUND: &str = "Couldn't stat file"; + pub const FILE_NOT_FOUND: &str = "No such file or directory"; pub const MISSING_INPUT_ARG: &str = "No input file provided"; pub const MISSING_OUTPUT_ARG: &str = "No output file provided"; } diff --git a/tests/thin_repair.rs b/tests/thin_repair.rs index e0b6529..3dcc506 100644 --- a/tests/thin_repair.rs +++ b/tests/thin_repair.rs @@ -48,13 +48,13 @@ fn dont_repair_xml() -> Result<()> { } #[test] -fn missing_input_file() -> Result<()> { +fn input_file_not_found() -> Result<()> { let mut td = TestDir::new()?; let md = mk_zeroed_md(&mut td)?; let stderr = run_fail(thin_repair!("-i", "no-such-file", "-o", &md))?; assert!(superblock_all_zeroes(&md)?); // TODO: replace with msg::FILE_NOT_FOUND once the rust version is ready - assert!(stderr.contains("Couldn't stat file")); + assert!(stderr.contains("No such file or directory")); Ok(()) } @@ -69,7 +69,7 @@ fn garbage_input_file() -> Result<()> { } #[test] -fn missing_output_file() -> Result<()> { +fn missing_output_arg() -> Result<()> { let mut td = TestDir::new()?; let md = mk_valid_md(&mut td)?; let stderr = run_fail(thin_repair!("-i", &md))?; diff --git a/tests/thin_restore.rs b/tests/thin_restore.rs index b5aa434..0fc23bf 100644 --- a/tests/thin_restore.rs +++ b/tests/thin_restore.rs @@ -40,7 +40,7 @@ fn accepts_help() -> Result<()> { } #[test] -fn no_input_file() -> Result<()> { +fn missing_input_arg() -> Result<()> { let mut td = TestDir::new()?; let md = mk_zeroed_md(&mut td)?; let stderr = run_fail(thin_restore!("-o", &md))?; @@ -49,7 +49,7 @@ fn no_input_file() -> Result<()> { } #[test] -fn missing_input_file() -> Result<()> { +fn input_file_not_found() -> Result<()> { let mut td = TestDir::new()?; let md = mk_zeroed_md(&mut td)?; let stderr = run_fail(thin_restore!("-i", "no-such-file", "-o", &md))?; @@ -69,7 +69,7 @@ fn garbage_input_file() -> Result<()> { } #[test] -fn no_output_file() -> Result<()> { +fn missing_output_arg() -> Result<()> { let mut td = TestDir::new()?; let xml = mk_valid_xml(&mut td)?; let stderr = run_fail(thin_restore!("-i", &xml))?;