monotone

monotone Mtn Source Tree

Root/cmd_packet.cc

1// Copyright (C) 2002 Graydon Hoare <graydon@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 <iostream>
12#include <sstream>
13
14#include "cmd.hh"
15#include "app_state.hh"
16#include "database.hh"
17#include "key_store.hh"
18#include "packet.hh"
19
20using std::cin;
21using std::cout;
22using std::istringstream;
23using std::vector;
24
25CMD(pubkey, "pubkey", "", CMD_REF(packet_io), N_("ID"),
26 N_("Prints a public key packet"),
27 "",
28 options::opts::none)
29{
30 database db(app);
31 key_store keys(app);
32
33 if (args.size() != 1)
34 throw usage(execid);
35
36 rsa_keypair_id ident(idx(args, 0)());
37 bool exists(false);
38 rsa_pub_key key;
39 if (db.database_specified() && db.public_key_exists(ident))
40 {
41 db.get_key(ident, key);
42 exists = true;
43 }
44 if (keys.key_pair_exists(ident))
45 {
46 keypair kp;
47 keys.get_key_pair(ident, kp);
48 key = kp.pub;
49 exists = true;
50 }
51 N(exists,
52 F("public key '%s' does not exist") % idx(args, 0)());
53
54 packet_writer pw(cout);
55 pw.consume_public_key(ident, key);
56}
57
58CMD(privkey, "privkey", "", CMD_REF(packet_io), N_("ID"),
59 N_("Prints a private key packet"),
60 "",
61 options::opts::none)
62{
63 key_store keys(app);
64
65 if (args.size() != 1)
66 throw usage(execid);
67
68 rsa_keypair_id ident(idx(args, 0)());
69 N(keys.key_pair_exists(ident),
70 F("public and private key '%s' do not exist in keystore")
71 % idx(args, 0)());
72
73 packet_writer pw(cout);
74 keypair kp;
75 keys.get_key_pair(ident, kp);
76 pw.consume_key_pair(ident, kp);
77}
78
79namespace
80{
81 // this writer injects packets it receives to the database
82 // and/or keystore.
83
84 struct packet_db_writer : public packet_consumer
85 {
86 database & db;
87 key_store & keys;
88
89 public:
90 packet_db_writer(database & db, key_store & keys)
91 : db(db), keys(keys)
92 {}
93 virtual ~packet_db_writer() {}
94 virtual void consume_file_data(file_id const & ident,
95 file_data const & dat)
96 {
97 transaction_guard guard(db);
98 db.put_file(ident, dat);
99 guard.commit();
100 }
101
102 virtual void consume_file_delta(file_id const & old_id,
103 file_id const & new_id,
104 file_delta const & del)
105 {
106 transaction_guard guard(db);
107 db.put_file_version(old_id, new_id, del);
108 guard.commit();
109 }
110
111 virtual void consume_revision_data(revision_id const & ident,
112 revision_data const & dat)
113 {
114 transaction_guard guard(db);
115 db.put_revision(ident, dat);
116 guard.commit();
117 }
118
119 virtual void consume_revision_cert(revision<cert> const & t)
120 {
121 transaction_guard guard(db);
122 db.put_revision_cert(t);
123 guard.commit();
124 }
125
126 virtual void consume_public_key(rsa_keypair_id const & ident,
127 rsa_pub_key const & k)
128 {
129 transaction_guard guard(db);
130 db.put_key(ident, k);
131 guard.commit();
132 }
133
134 virtual void consume_key_pair(rsa_keypair_id const & ident,
135 keypair const & kp)
136 {
137 keys.put_key_pair(ident, kp);
138 }
139
140 virtual void consume_old_private_key(rsa_keypair_id const & ident,
141 old_arc4_rsa_priv_key const & k)
142 {
143 rsa_pub_key dummy;
144 keys.migrate_old_key_pair(ident, k, dummy);
145 }
146 };
147}
148
149
150CMD(read, "read", "", CMD_REF(packet_io), "[FILE1 [FILE2 [...]]]",
151 N_("Reads packets from files"),
152 N_("If no files are provided, the standard input is used."),
153 options::opts::none)
154{
155 database db(app);
156 key_store keys(app);
157 packet_db_writer dbw(db, keys);
158 size_t count = 0;
159 if (args.empty())
160 {
161 count += read_packets(cin, dbw);
162 N(count != 0, F("no packets found on stdin"));
163 }
164 else
165 {
166 for (args_vector::const_iterator i = args.begin();
167 i != args.end(); ++i)
168 {
169 data dat;
170 read_data(system_path(*i), dat);
171 istringstream ss(dat());
172 count += read_packets(ss, dbw);
173 }
174 N(count != 0, FP("no packets found in given file",
175 "no packets found in given files",
176 args.size()));
177 }
178 P(FP("read %d packet", "read %d packets", count) % count);
179}
180
181
182// Local Variables:
183// mode: C++
184// fill-column: 76
185// c-file-style: "gnu"
186// indent-tabs-mode: nil
187// End:
188// 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