1 : #include <iostream>
2 :
3 : #include "hash.hh"
4 : #include "shared.hh"
5 : #include "help.txt.hh"
6 :
7 :
8 : using namespace nix;
9 :
10 :
11 : void printHelp()
12 1 : {
13 1 : std::cout << string((char *) helpText, sizeof helpText);
14 : }
15 :
16 :
17 : void run(Strings args)
18 55 : {
19 55 : HashType ht = htMD5;
20 55 : bool flat = false;
21 55 : bool base32 = false;
22 55 : bool truncate = false;
23 55 : enum { opHash, opTo32, opTo16 } op = opHash;
24 :
25 1867 : Strings ss;
26 :
27 243 : for (Strings::iterator i = args.begin();
28 : i != args.end(); i++)
29 : {
30 188 : if (*i == "--flat") flat = true;
31 142 : else if (*i == "--base32") base32 = true;
32 110 : else if (*i == "--truncate") truncate = true;
33 110 : else if (*i == "--type") {
34 51 : ++i;
35 309 : if (i == args.end()) throw UsageError("`--type' requires an argument");
36 51 : ht = parseHashType(*i);
37 51 : if (ht == htUnknown)
38 0 : throw UsageError(format("unknown hash type `%1%'") % *i);
39 : }
40 59 : else if (*i == "--to-base16") op = opTo16;
41 57 : else if (*i == "--to-base32") op = opTo32;
42 55 : else ss.push_back(*i);
43 : }
44 :
45 55 : if (op == opHash) {
46 102 : for (Strings::iterator i = ss.begin(); i != ss.end(); ++i) {
47 51 : Hash h = flat ? hashFile(ht, *i) : hashPath(ht, *i);
48 51 : if (truncate && h.hashSize > 20) h = compressHash(h, 20);
49 51 : std::cout << format("%1%\n") %
50 : (base32 ? printHash32(h) : printHash(h));
51 : }
52 : }
53 :
54 : else {
55 8 : for (Strings::iterator i = ss.begin(); i != ss.end(); ++i) {
56 4 : Hash h = op == opTo16 ? parseHash32(ht, *i) : parseHash(ht, *i);
57 4 : std::cout << format("%1%\n") %
58 : (op == opTo16 ? printHash(h) : printHash32(h));
59 : }
60 : }
61 : }
62 :
63 :
64 112 : string programId = "nix-hash";
65 112 :
|