Repository: mesos Updated Branches: refs/heads/master 250439f5b -> 4107f14e3
Added common compression utilities. Added support for GZIP compress and uncompress. Review: https://reviews.apache.org/r/43546/ Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/906566db Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/906566db Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/906566db Branch: refs/heads/master Commit: 906566db499ce2c66a4d8044025ddb94131ead8b Parents: 250439f Author: Jojy Varghese <[email protected]> Authored: Wed Feb 17 11:33:20 2016 -0800 Committer: Jie Yu <[email protected]> Committed: Wed Feb 17 11:33:20 2016 -0800 ---------------------------------------------------------------------- src/common/command_utils.cpp | 25 +++++++++++++++ src/common/command_utils.hpp | 18 +++++++++++ src/tests/common/command_utils_tests.cpp | 45 +++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/906566db/src/common/command_utils.cpp ---------------------------------------------------------------------- diff --git a/src/common/command_utils.cpp b/src/common/command_utils.cpp index 8c2ba35..3f1d7f3 100644 --- a/src/common/command_utils.cpp +++ b/src/common/command_utils.cpp @@ -197,6 +197,31 @@ Future<string> sha512(const Path& input) }); } + +Future<Nothing> gzip(const Path& input) +{ + vector<string> argv = { + "gzip", + input + }; + + return launch("gzip", argv) + .then([]() { return Nothing(); }); +} + + +Future<Nothing> decompress(const Path& input) +{ + vector<string> argv = { + "gzip", + "-d", // Decompress. + input + }; + + return launch("gzip", argv) + .then([]() { return Nothing(); }); +} + } // namespace command { } // namespace internal { } // namespace mesos { http://git-wip-us.apache.org/repos/asf/mesos/blob/906566db/src/common/command_utils.hpp ---------------------------------------------------------------------- diff --git a/src/common/command_utils.hpp b/src/common/command_utils.hpp index 738c309..525f9c1 100644 --- a/src/common/command_utils.hpp +++ b/src/common/command_utils.hpp @@ -69,6 +69,24 @@ process::Future<Nothing> untar( */ process::Future<std::string> sha512(const Path& input); + +/** + * Compresses the given input file in GZIP format. + * + * @param input path of the file to be compressed. + */ +process::Future<Nothing> gzip(const Path& input); + +// TODO(jojy): Add support for other compression algorithms. + +/** + * Decompresses given input file based on its compression format. + * TODO(jojy): Add support for other compression algorithms. + * + * @param input path of the compressed file. + */ +process::Future<Nothing> decompress(const Path& input); + } // namespace command { } // namespace internal { } // namespace mesos { http://git-wip-us.apache.org/repos/asf/mesos/blob/906566db/src/tests/common/command_utils_tests.cpp ---------------------------------------------------------------------- diff --git a/src/tests/common/command_utils_tests.cpp b/src/tests/common/command_utils_tests.cpp index 9207a1b..c91a7f3 100644 --- a/src/tests/common/command_utils_tests.cpp +++ b/src/tests/common/command_utils_tests.cpp @@ -241,6 +241,51 @@ TEST_F(ShasumTest, SHA512SimpleFile) "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f"); // NOLINT(whitespace/line_length) } + +class CompressionTest : public TemporaryDirectoryTest {}; + + +// Tests command::decompress API for a tar + gzip file. +TEST_F(CompressionTest, GZIPDecompressTarFile) +{ + // Create a test file. + const Path testFile("testfile"); + + Try<Nothing> write = os::write(testFile, "test"); + ASSERT_SOME(write); + + // Archive the test file. + const Path outputTarFile("test.tar"); + + AWAIT_ASSERT_READY(command::tar( + testFile, + outputTarFile, + None(), + command::Compression::GZIP)); + + ASSERT_TRUE(os::exists(outputTarFile)); + + // Remove the test file. + ASSERT_SOME(os::rm(testFile)); + ASSERT_FALSE(os::exists(testFile)); + + // Append ".gz" as gzip expects it. + const Path gzipFile(outputTarFile.value + ".gz"); + ASSERT_SOME(os::rename(outputTarFile, gzipFile)); + + AWAIT_ASSERT_READY(command::decompress(gzipFile)); + + // Tar file should exist now. + ASSERT_TRUE(os::exists(outputTarFile)); + + // Untar the tarball and verify that the original file is created. + AWAIT_ASSERT_READY(command::untar(outputTarFile)); + ASSERT_TRUE(os::exists(testFile)); + + // Verify that the content is same as original file. + EXPECT_SOME_EQ("test", os::read(testFile)); +} + } // namespace tests { } // namespace internal { } // namespace mesos {
