GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/publish/cmd_hash.cc
Date: 2024-04-21 02:33:16
Exec Total Coverage
Lines: 0 20 0.0%
Branches: 0 70 0.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "cvmfs_config.h"
6 #include "cmd_hash.h"
7
8 #ifndef __STDC_FORMAT_MACROS
9 #define __STDC_FORMAT_MACROS
10 #endif
11
12 #include <inttypes.h>
13 #include <stdint.h>
14
15 #include <cstdio>
16 #include <cstring>
17
18 #include "crypto/hash.h"
19 #include "publish/except.h"
20 #include "util/logging.h"
21
22 int publish::CmdHash::Main(const Options &options) {
23 std::string algorithm = options.GetString("algorithm");
24 shash::Any hash(shash::ParseHashAlgorithm(algorithm));
25 // MD5 is not a content hash algorithm but we deal with it in this utility
26 // nevertheless
27 if (algorithm == "md5")
28 hash.algorithm = shash::kMd5;
29 if (hash.algorithm == shash::kAny)
30 throw EPublish("unknown hash algorithm: " + algorithm);
31
32 if (options.Has("input")) {
33 shash::HashString(options.GetString("input"), &hash);
34 } else {
35 shash::HashFd(fileno(stdin), &hash);
36 }
37 LogCvmfs(kLogCvmfs, kLogStdout | kLogNoLinebreak, "%s",
38 options.Has("fingerprint") ? hash.ToFingerprint().c_str()
39 : hash.ToString().c_str());
40 if (options.Has("split")) {
41 if (hash.algorithm != shash::kMd5)
42 throw EPublish("split int representation only supported for MD5");
43
44 uint64_t high, low;
45 shash::Md5 md5_hash;
46 memcpy(md5_hash.digest, hash.digest, shash::kDigestSizes[shash::kMd5]);
47 md5_hash.ToIntPair(&high, &low);
48 // SQLite uses int64, not uint64
49 LogCvmfs(kLogCvmfs, kLogStdout | kLogNoLinebreak,
50 " [%" PRId64 " %" PRId64 "]",
51 static_cast<int64_t>(high), static_cast<int64_t>(low));
52 }
53 LogCvmfs(kLogCvmfs, kLogStdout, "");
54
55 return 0;
56 }
57