Permalink
Browse files

Added is_regular_file and is_empty.

  • Loading branch information...
1 parent e5b5bf9 commit c38909993aac90b9ccef8a781d89ac4e8eb14955 @alamaison committed Jan 31, 2016
Showing with 91 additions and 2 deletions.
  1. +47 −2 ssh/filesystem.hpp
  2. +44 −0 test/ssh/filesystem_test.cpp
View
@@ -1329,9 +1329,36 @@ inline file_status status(sftp_filesystem& filesystem, const path& file)
return filesystem.status(file);
}
+/**
+ * Is the give status from a regular file?
+ */
+inline bool is_regular_file(const file_status& status)
+{
+ return status.type() == file_type::regular;
+}
+
+/**
+ * Is the given path a regular file?
+ */
+inline bool is_regular_file(sftp_filesystem& filesystem, const path& file)
+{
+ return is_regular_file(status(filesystem, file));
+}
+
+/**
+ * Is the give status from a directory?
+ */
+inline bool is_directory(const file_status& status)
+{
+ return status.type() == file_type::directory;
+}
+
+/**
+ * Is the given path a directory?
+ */
inline bool is_directory(sftp_filesystem& filesystem, const path& file)
{
- return status(filesystem, file).type() == file_type::directory;
+ return is_directory(status(filesystem, file));
}
/**
@@ -1368,7 +1395,25 @@ inline std::time_t last_write_time(sftp_filesystem& filesystem,
return status(filesystem, file).last_write_time();
}
-// Needs directory_iterator implementation so outside sftp_filesystem class body
+/**
+ * Determine whether the given file or directory is empty.
+ */
+inline bool is_empty(sftp_filesystem& filesystem, const path& file)
+{
+ file_status s = status(filesystem, file);
+ if (is_directory(s))
+ {
+ return filesystem.directory_iterator(file) ==
+ filesystem.directory_iterator();
+ }
+ else
+ {
+ return s.file_size() == 0;
+ }
+}
+
+// Needs directory_iterator implementation so outside sftp_filesystem class
+// body
inline boost::uintmax_t sftp_filesystem::remove_directory(const path& root)
{
boost::uintmax_t count = 0U;
@@ -625,6 +625,25 @@ BOOST_AUTO_TEST_CASE(is_directory_returns_false_for_non_existent_path)
BOOST_CHECK(!is_directory(filesystem(), "i do not exist"));
}
+BOOST_AUTO_TEST_CASE(is_regular_file_returns_false_for_directories)
+{
+ path target = new_directory_in_sandbox();
+
+ BOOST_CHECK(!is_regular_file(filesystem(), target));
+}
+
+BOOST_AUTO_TEST_CASE(is_regular_file_returns_false_for_files)
+{
+ path target = new_file_in_sandbox();
+
+ BOOST_CHECK(is_regular_file(filesystem(), target));
+}
+
+BOOST_AUTO_TEST_CASE(is_regular_file_returns_false_for_non_existent_path)
+{
+ BOOST_CHECK(!is_regular_file(filesystem(), "i do not exist"));
+}
+
BOOST_AUTO_TEST_CASE(new_directory)
{
path target = new_directory_in_sandbox();
@@ -755,4 +774,29 @@ BOOST_AUTO_TEST_CASE(last_write_time_of_non_file_throws_error)
system_error);
}
+BOOST_AUTO_TEST_CASE(empty_file_is_empty)
+{
+ path target = new_file_in_sandbox();
+ BOOST_CHECK(is_empty(filesystem(), target));
+}
+
+BOOST_AUTO_TEST_CASE(non_empty_file_is_not_empty)
+{
+ string data = "mary had a little lamb";
+ path target = new_file_in_sandbox_containing_data(data);
+ BOOST_CHECK(!is_empty(filesystem(), target));
+}
+
+BOOST_AUTO_TEST_CASE(empty_directory_is_empty)
+{
+ path target = new_directory_in_sandbox();
+ BOOST_CHECK(is_empty(filesystem(), target));
+}
+
+BOOST_AUTO_TEST_CASE(non_empty_directory_is_not_empty)
+{
+ new_file_in_sandbox();
+ BOOST_CHECK(!is_empty(filesystem(), sandbox()));
+}
+
BOOST_AUTO_TEST_SUITE_END();

0 comments on commit c389099

Please sign in to comment.