monotone

monotone Mtn Source Tree

Root/src/inodeprint.cc

1// Copyright (C) 2005 Nathaniel Smith <njs@pobox.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#include "base.hh"
11#include <iterator>
12#include <sstream>
13#include <algorithm>
14#include <iterator>
15
16#include <botan/botan.h>
17#include <botan/sha160.h>
18
19#include "inodeprint.hh"
20#include "sanity.hh"
21#include "platform.hh"
22#include "transforms.hh"
23#include "constants.hh"
24#include "basic_io.hh"
25#include "vocab_cast.hh"
26
27using std::ostream;
28using std::ostringstream;
29using std::string;
30
31// this file defines the inodeprint_map structure, and some operations on it.
32
33namespace
34{
35 namespace syms
36 {
37 // roster symbols
38 symbol const format_version("format_version");
39 symbol const file("file");
40 symbol const print("print");
41 }
42}
43// reading inodeprint_maps
44
45void
46read_inodeprint_map(data const & dat,
47 inodeprint_map & ipm)
48{
49 // don't bomb out if it's just an old-style inodeprints file
50 string start = dat().substr(0, syms::format_version().size());
51 if (start != syms::format_version())
52 {
53 L(FL("inodeprints file format is wrong, skipping it"));
54 return;
55 }
56
57 basic_io::input_source src(dat(), "inodeprint");
58 basic_io::tokenizer tok(src);
59 basic_io::parser pa(tok);
60
61 {
62 pa.esym(syms::format_version);
63 string vers;
64 pa.str(vers);
65 if (vers != "1")
66 {
67 L(FL("inodeprints file version is unknown, skipping it"));
68 return;
69 }
70 }
71
72 while(pa.symp())
73 {
74 string path, print;
75
76 pa.esym(syms::file);
77 pa.str(path);
78 pa.esym(syms::print);
79 pa.hex(print);
80
81 ipm.insert(inodeprint_entry(file_path_internal(path),
82 hexenc<inodeprint>(print,
83 origin::workspace)));
84 }
85 I(src.lookahead == EOF);
86}
87
88void
89write_inodeprint_map(inodeprint_map const & ipm,
90 data & dat)
91{
92 basic_io::printer pr;
93 {
94 basic_io::stanza st;
95 st.push_str_pair(syms::format_version, "1");
96 pr.print_stanza(st);
97 }
98 for (inodeprint_map::const_iterator i = ipm.begin();
99 i != ipm.end(); ++i)
100 {
101 basic_io::stanza st;
102 st.push_file_pair(syms::file, i->first);
103 st.push_hex_pair(syms::print, typecast_vocab<hexenc<id> >(i->second));
104 pr.print_stanza(st);
105 }
106 dat = data(pr.buf, origin::internal);
107}
108
109class my_iprint_calc : public inodeprint_calculator
110{
111 std::string res;
112 Botan::SHA_160 hash;
113 bool too_close;
114 void add_item(void *dat, size_t size)
115 {
116 hash.update(reinterpret_cast<Botan::byte const *>(&size),
117 sizeof(size));
118 hash.update(reinterpret_cast<Botan::byte const *>(dat),
119 size);
120 }
121public:
122 my_iprint_calc() : too_close(false)
123 {}
124 std::string str()
125 {
126 char digest[constants::sha1_digest_length];
127 hash.final(reinterpret_cast<Botan::byte *>(digest));
128 return std::string(digest, constants::sha1_digest_length);
129 }
130 void note_nowish(bool n)
131 {
132 too_close = n;
133 }
134 void note_future(bool f)
135 {
136 inodeprint_calculator::add_item(f);
137 }
138 bool ok()
139 {
140 return !too_close;
141 }
142};
143
144bool inodeprint_file(file_path const & file, hexenc<inodeprint> & ip)
145{
146 my_iprint_calc calc;
147 bool ret = inodeprint_file(file.as_external(), calc);
148 inodeprint ip_raw(calc.str(), origin::internal);
149 if (!ret)
150 ip_raw = inodeprint("");
151 encode_hexenc(ip_raw, ip);
152 return ret && calc.ok();
153}
154
155// Local Variables:
156// mode: C++
157// fill-column: 76
158// c-file-style: "gnu"
159// indent-tabs-mode: nil
160// End:
161// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status