monotone

monotone Mtn Source Tree

Root/cmd_netsync.cc

1#include "cmd.hh"
2
3#include "netsync.hh"
4#include "globish.hh"
5#include "keys.hh"
6#include "cert.hh"
7
8#include <fstream>
9
10using std::ifstream;
11using std::ofstream;
12using std::string;
13using std::vector;
14
15static const var_key default_server_key(var_domain("database"),
16 var_name("default-server"));
17static const var_key default_include_pattern_key(var_domain("database"),
18 var_name("default-include-pattern"));
19static const var_key default_exclude_pattern_key(var_domain("database"),
20 var_name("default-exclude-pattern"));
21
22static void
23process_netsync_args(string const & name,
24 vector<utf8> const & args,
25 utf8 & addr,
26 utf8 & include_pattern, utf8 & exclude_pattern,
27 bool use_defaults,
28 bool serve_mode,
29 app_state & app)
30{
31 // handle host argument
32 if (!serve_mode)
33 {
34 if (args.size() >= 1)
35 {
36 addr = idx(args, 0);
37 if (use_defaults
38 && (!app.db.var_exists(default_server_key) || app.set_default))
39 {
40 P(F("setting default server to %s") % addr);
41 app.db.set_var(default_server_key, var_value(addr()));
42 }
43 }
44 else
45 {
46 N(use_defaults, F("no hostname given"));
47 N(app.db.var_exists(default_server_key),
48 F("no server given and no default server set"));
49 var_value addr_value;
50 app.db.get_var(default_server_key, addr_value);
51 addr = utf8(addr_value());
52 L(FL("using default server address: %s") % addr);
53 }
54 }
55
56 // handle include/exclude args
57 if (serve_mode || (args.size() >= 2 || !app.exclude_patterns.empty()))
58 {
59 E(serve_mode || args.size() >= 2, F("no branch pattern given"));
60 int pattern_offset = (serve_mode ? 0 : 1);
61 vector<utf8> patterns(args.begin() + pattern_offset, args.end());
62 combine_and_check_globish(patterns, include_pattern);
63 combine_and_check_globish(app.exclude_patterns, exclude_pattern);
64 if (use_defaults &&
65 (!app.db.var_exists(default_include_pattern_key) || app.set_default))
66 {
67 P(F("setting default branch include pattern to '%s'") % include_pattern);
68 app.db.set_var(default_include_pattern_key, var_value(include_pattern()));
69 }
70 if (use_defaults &&
71 (!app.db.var_exists(default_exclude_pattern_key) || app.set_default))
72 {
73 P(F("setting default branch exclude pattern to '%s'") % exclude_pattern);
74 app.db.set_var(default_exclude_pattern_key, var_value(exclude_pattern()));
75 }
76 }
77 else
78 {
79 N(use_defaults, F("no branch pattern given"));
80 N(app.db.var_exists(default_include_pattern_key),
81 F("no branch pattern given and no default pattern set"));
82 var_value pattern_value;
83 app.db.get_var(default_include_pattern_key, pattern_value);
84 include_pattern = utf8(pattern_value());
85 L(FL("using default branch include pattern: '%s'") % include_pattern);
86 if (app.db.var_exists(default_exclude_pattern_key))
87 {
88 app.db.get_var(default_exclude_pattern_key, pattern_value);
89 exclude_pattern = utf8(pattern_value());
90 }
91 else
92 exclude_pattern = utf8("");
93 L(FL("excluding: %s") % exclude_pattern);
94 }
95}
96
97CMD(push, N_("network"), N_("[ADDRESS[:PORTNUMBER] [PATTERN]]"),
98 N_("push branches matching PATTERN to netsync server at ADDRESS"),
99 OPT_SET_DEFAULT % OPT_EXCLUDE % OPT_KEY_TO_PUSH)
100{
101 utf8 addr, include_pattern, exclude_pattern;
102 process_netsync_args(name, args, addr, include_pattern, exclude_pattern, true, false, app);
103
104 rsa_keypair_id key;
105 get_user_key(key, app);
106 app.signing_key = key;
107
108 run_netsync_protocol(client_voice, source_role, addr,
109 include_pattern, exclude_pattern, app);
110}
111
112CMD(pull, N_("network"), N_("[ADDRESS[:PORTNUMBER] [PATTERN]]"),
113 N_("pull branches matching PATTERN from netsync server at ADDRESS"),
114 OPT_SET_DEFAULT % OPT_EXCLUDE)
115{
116 utf8 addr, include_pattern, exclude_pattern;
117 process_netsync_args(name, args, addr, include_pattern, exclude_pattern, true, false, app);
118
119 if (app.signing_key() == "")
120 P(F("doing anonymous pull; use -kKEYNAME if you need authentication"));
121
122 run_netsync_protocol(client_voice, sink_role, addr,
123 include_pattern, exclude_pattern, app);
124}
125
126CMD(sync, N_("network"), N_("[ADDRESS[:PORTNUMBER] [PATTERN]]"),
127 N_("sync branches matching PATTERN with netsync server at ADDRESS"),
128 OPT_SET_DEFAULT % OPT_EXCLUDE % OPT_KEY_TO_PUSH)
129{
130 utf8 addr, include_pattern, exclude_pattern;
131 process_netsync_args(name, args, addr, include_pattern, exclude_pattern, true, false, app);
132
133 rsa_keypair_id key;
134 get_user_key(key, app);
135 app.signing_key = key;
136
137 run_netsync_protocol(client_voice, source_and_sink_role, addr,
138 include_pattern, exclude_pattern, app);
139}
140
141
142struct pid_file
143{
144 explicit pid_file(system_path const & p)
145 : path(p)
146 {
147 if (path.empty())
148 return;
149 require_path_is_nonexistent(path, F("pid file '%s' already exists") % path);
150 file.open(path.as_external().c_str());
151 E(file.is_open(), F("failed to create pid file '%s'") % path);
152 file << get_process_id() << "\n";
153 file.flush();
154 }
155
156 ~pid_file()
157 {
158 if (path.empty())
159 return;
160 pid_t pid;
161 ifstream(path.as_external().c_str()) >> pid;
162 if (pid == get_process_id()) {
163 file.close();
164 delete_file(path);
165 }
166 }
167
168private:
169 ofstream file;
170 system_path path;
171};
172
173CMD_NO_WORKSPACE(serve, N_("network"), N_("PATTERN ..."),
174 N_("serve the branches specified by PATTERNs to connecting clients"),
175 OPT_BIND % OPT_STDIO % OPT_NO_TRANSPORT_AUTH % OPT_PIDFILE % OPT_EXCLUDE)
176{
177 if (args.size() < 1)
178 throw usage(name);
179
180 pid_file pid(app.pidfile);
181
182 if (app.use_transport_auth)
183 {
184 rsa_keypair_id key;
185 get_user_key(key, app);
186 app.signing_key = key;
187
188 N(app.lua.hook_persist_phrase_ok(),
189F("need permission to store persistent passphrase (see hook persist_phrase_ok())"));
190 require_password(key, app);
191 }
192 else
193 {
194 E(app.bind_stdio,
195F("The --no-transport-auth option is only permitted in combination with --stdio"));
196 }
197
198 app.db.ensure_open();
199
200 utf8 dummy_addr, include_pattern, exclude_pattern;
201 process_netsync_args(name, args, dummy_addr, include_pattern, exclude_pattern, false, true, app);
202 run_netsync_protocol(server_voice, source_and_sink_role, app.bind_address,
203 include_pattern, exclude_pattern, app);
204}
205
206// Local Variables:
207// mode: C++
208// fill-column: 76
209// c-file-style: "gnu"
210// indent-tabs-mode: nil
211// End:
212// 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