xapian-core  2.0.0
replicatetcpserver.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2008,2010,2011 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 
23 #include "replicatetcpserver.h"
24 
25 #include <xapian/error.h>
26 #include "api/replication.h"
27 #include "remoteconnection.h"
28 
29 using namespace std;
30 
31 ReplicateTcpServer::ReplicateTcpServer(const string & host, int port,
32  const string & path_)
33  : TcpServer(host, port, false, false), path(path_)
34 {
35 }
36 
37 void
39 {
40  RemoteConnection client(socket, -1);
41  try {
42  // Read start_revision from the client.
43  string start_revision;
44  if (client.get_message(start_revision, 0.0) != 'R') {
45  throw Xapian::NetworkError("Bad replication client message");
46  }
47 
48  // Read dbname from the client.
49  string dbname;
50  if (client.get_message(dbname, 0.0) != 'D') {
51  throw Xapian::NetworkError("Bad replication client message (2)");
52  }
53  if (dbname.find("..") != string::npos) {
54  throw Xapian::NetworkError("dbname contained '..'");
55  }
56 
57  string dbpath(path);
58  dbpath += '/';
59  dbpath += dbname;
60  Xapian::DatabaseMaster master(dbpath);
61  master.write_changesets_to_fd(socket, start_revision, NULL);
62  } catch (...) {
63  // Ignore exceptions.
64  }
65 }
A RemoteConnection object provides a bidirectional connection to another RemoteConnection object on a...
int get_message(std::string &result, double end_time)
Read one message from fdin.
std::string path
The path to pass to DatabaseMaster.
ReplicateTcpServer(const std::string &host, int port, const std::string &path_)
Construct a ReplicateTcpServer and start listening for connections.
void handle_one_connection(int socket)
Handle a single connection on an already connected socket.
Access to a master database for replication.
Definition: replication.h:56
void write_changesets_to_fd(int fd, const std::string &start_revision, ReplicationInfo *info) const
Write a set of changesets for upgrading the database to a file.
Definition: replication.cc:69
Indicates a problem communicating with a remote database.
Definition: error.h:791
Hierarchy of classes which Xapian can throw as exceptions.
#define false
Definition: header.h:9
RemoteConnection class used by the remote backend.
TCP/IP replication server class.
Replication support for Xapian databases.