You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2016/01/29 03:03:00 UTC

mesos git commit: Added utility function to compute SHA512 digest.

Repository: mesos
Updated Branches:
  refs/heads/master ad058db1b -> ebc85487c


Added utility function to compute SHA512 digest.

Review: https://reviews.apache.org/r/42773/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/ebc85487
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/ebc85487
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/ebc85487

Branch: refs/heads/master
Commit: ebc85487c125d05745bffbee0bbbd0943b99210f
Parents: ad058db
Author: Jojy Varghese <jo...@mesosphere.io>
Authored: Thu Jan 28 17:36:34 2016 -0800
Committer: Jie Yu <yu...@gmail.com>
Committed: Thu Jan 28 18:02:57 2016 -0800

----------------------------------------------------------------------
 src/common/command_utils.cpp             | 28 +++++++++++++++++++++++++++
 src/common/command_utils.hpp             |  8 ++++++++
 src/tests/common/command_utils_tests.cpp | 19 ++++++++++++++++++
 3 files changed, 55 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/ebc85487/src/common/command_utils.cpp
----------------------------------------------------------------------
diff --git a/src/common/command_utils.cpp b/src/common/command_utils.cpp
index e9d7821..722d0b4 100644
--- a/src/common/command_utils.cpp
+++ b/src/common/command_utils.cpp
@@ -165,6 +165,34 @@ Future<Nothing> untar(
     .then([] () {return Nothing();});
 }
 
+
+static Future<string> shasum(const string& algorithm, const Path& input)
+{
+  vector<string> argv = {
+    "shasum",
+    "-a", algorithm,  // Shasum type.
+    input             // Input file to compute shasum.
+  };
+
+  return launch("shasum", argv)
+    .then([](const string& output) -> Future<string> {
+      vector<string> tokens = strings::tokenize(output, " ");
+      if (tokens.size() < 2) {
+        return Failure("Failed to parse '" + output + "' from shasum command");
+      }
+
+      // TODO(jojy): Check the size of tokens[0].
+
+      return tokens[0];
+    });
+}
+
+
+Future<string> sha512(const Path& input)
+{
+  return shasum("512", input);
+}
+
 } // namespace command {
 } // namespace internal {
 } // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/ebc85487/src/common/command_utils.hpp
----------------------------------------------------------------------
diff --git a/src/common/command_utils.hpp b/src/common/command_utils.hpp
index bfab345..738c309 100644
--- a/src/common/command_utils.hpp
+++ b/src/common/command_utils.hpp
@@ -61,6 +61,14 @@ process::Future<Nothing> untar(
 
 // TODO(Jojy): Add more overloads/options for untar (eg., keep existing files)
 
+
+/**
+ * Computes SHA 512 checksum of a file.
+ *
+ * @param input path of the file whose SHA 512 checksum has to be computed.
+ */
+process::Future<std::string> sha512(const Path& input);
+
 } // namespace command {
 } // namespace internal {
 } // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/ebc85487/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 938f399..9207a1b 100644
--- a/src/tests/common/command_utils_tests.cpp
+++ b/src/tests/common/command_utils_tests.cpp
@@ -222,6 +222,25 @@ TEST_F(TarTest, GZIPChangeDirectory)
   EXPECT_SOME_EQ("test", os::read(testFile));
 }
 
+
+class ShasumTest : public TemporaryDirectoryTest {};
+
+
+TEST_F(ShasumTest, SHA512SimpleFile)
+{
+  const Path testFile(path::join(os::getcwd(), "test"));
+
+  Try<Nothing> write = os::write(testFile, "hello world");
+  ASSERT_SOME(write);
+
+  Future<string> sha512 = command::sha512(testFile);
+  AWAIT_ASSERT_READY(sha512);
+
+  ASSERT_EQ(
+      sha512.get(),
+      "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f"); // NOLINT(whitespace/line_length)
+}
+
 } // namespace tests {
 } // namespace internal {
 } // namespace mesos {