Add convenience fn to compute a file's sha1sum
authorRobin Gareus <robin@gareus.org>
Mon, 26 Jun 2017 17:39:40 +0000 (19:39 +0200)
committerRobin Gareus <robin@gareus.org>
Mon, 26 Jun 2017 17:39:40 +0000 (19:39 +0200)
libs/ardour/ardour/utils.h
libs/ardour/utils.cc

index 1208d9ce77d175e6e3751bc49ee5d1801a6c9305..77dc8236a7feb227a9bf8c8ce1fafa819df894ae 100644 (file)
@@ -106,6 +106,8 @@ LIBARDOUR_API bool matching_unsuffixed_filename_exists_in (const std::string& di
 
 LIBARDOUR_API uint32_t how_many_dsp_threads ();
 
+LIBARDOUR_API std::string compute_sha1_of_file (std::string path);
+
 template<typename T> boost::shared_ptr<ControlList> route_list_to_control_list (boost::shared_ptr<RouteList> rl, boost::shared_ptr<T> (Stripable::*get_control)() const) {
        boost::shared_ptr<ControlList> cl (new ControlList);
        if (!rl) { return cl; }
index f73f206d29acef9e0a8a99b0a921094e760929bb..31a94702e122c4486daf6c02e7cc2914cc13f4c8 100644 (file)
@@ -52,6 +52,7 @@
 #include "pbd/stacktrace.h"
 #include "pbd/xml++.h"
 #include "pbd/basename.h"
+#include "pbd/scoped_file_descriptor.h"
 #include "pbd/strsplit.h"
 #include "pbd/replace_all.h"
 
@@ -719,6 +720,29 @@ ARDOUR::slider_position_to_gain_with_max (double g, double max_gain)
        return position_to_gain (g) * max_gain / 2.0;
 }
 
+#include "sha1.c"
+
+std::string
+ARDOUR::compute_sha1_of_file (std::string path)
+{
+       PBD::ScopedFileDescriptor fd (g_open (path.c_str(), O_RDONLY, 0444));
+       if (fd < 0) {
+               return std::string ();
+       }
+       char buf[4096];
+       ssize_t n_read;
+       char hash[41];
+       Sha1Digest s;
+       sha1_init (&s);
+
+       while ((n_read = ::read(fd, buf, sizeof(buf))) > 0) {
+               sha1_write (&s, (const uint8_t*) buf, n_read);
+       }
+
+       sha1_result_hash (&s, hash);
+       return std::string (hash);
+}
+
 extern "C" {
        void c_stacktrace() { stacktrace (cerr); }
 }