monotone

monotone Mtn Source Tree

Root/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 <string>
11#include <iterator>
12#include <sstream>
13#include <algorithm>
14#include <iterator>
15
16#include "app_state.hh"
17#include "inodeprint.hh"
18#include "sanity.hh"
19#include "platform.hh"
20#include "transforms.hh"
21#include "constants.hh"
22#include "basic_io.hh"
23
24#include "botan/botan.h"
25#include "botan/sha160.h"
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 }
84 I(src.lookahead == EOF);
85}
86
87void
88write_inodeprint_map(inodeprint_map const & ipm,
89 data & dat)
90{
91 basic_io::printer pr;
92 {
93 basic_io::stanza st;
94 st.push_str_pair(syms::format_version, "1");
95 pr.print_stanza(st);
96 }
97 for (inodeprint_map::const_iterator i = ipm.begin();
98 i != ipm.end(); ++i)
99 {
100 basic_io::stanza st;
101 st.push_file_pair(syms::file, i->first);
102 st.push_hex_pair(syms::print, hexenc<id>(i->second()));
103 pr.print_stanza(st);
104 }
105 dat = data(pr.buf);
106}
107
108class my_iprint_calc : public inodeprint_calculator
109{
110 std::string res;
111 Botan::SHA_160 hash;
112 bool too_close;
113 void add_item(void *dat, size_t size)
114 {
115 hash.update(reinterpret_cast<Botan::byte const *>(&size),
116 sizeof(size));
117 hash.update(reinterpret_cast<Botan::byte const *>(dat),
118 size);
119 }
120public:
121 my_iprint_calc() : too_close(false)
122 {}
123 std::string str()
124 {
125 char digest[constants::sha1_digest_length];
126 hash.final(reinterpret_cast<Botan::byte *>(digest));
127 return std::string(digest, constants::sha1_digest_length);
128 }
129 void note_nowish(bool n)
130 {
131 too_close = n;
132 }
133 void note_future(bool f)
134 {
135 inodeprint_calculator::add_item(f);
136 }
137 bool ok()
138 {
139 return !too_close;
140 }
141};
142
143bool inodeprint_file(file_path const & file, hexenc<inodeprint> & ip)
144{
145 my_iprint_calc calc;
146 bool ret = inodeprint_file(file.as_external(), calc);
147 inodeprint ip_raw(calc.str());
148 if (!ret)
149 ip_raw = inodeprint("");
150 encode_hexenc(ip_raw, ip);
151 return ret && calc.ok();
152}
153
154// Local Variables:
155// mode: C++
156// fill-column: 76
157// c-file-style: "gnu"
158// indent-tabs-mode: nil
159// End:
160// 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