xapian-core  1.4.26
replicate_utils.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2010 Richard Boulton
5  * Copyright (C) 2010 Olly Betts
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <config.h>
23 
24 #include "replicate_utils.h"
25 
26 #include "xapian/error.h"
27 
28 #include "io_utils.h"
29 #include "posixy_wrapper.h"
30 
31 #include "safefcntl.h"
32 #include "safesysstat.h"
33 #include "safeunistd.h"
34 
35 #include <sys/types.h>
36 
37 #include <cerrno>
38 #include <string>
39 
40 using namespace std;
41 
42 int
43 create_changeset_file(const string & changeset_dir,
44  const string & filename,
45  string & changes_name)
46 {
47  changes_name = changeset_dir;
48  changes_name += '/';
49  changes_name += filename;
50  int changes_fd = posixy_open(changes_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0666);
51  if (changes_fd < 0) {
52  string message("Couldn't open changeset to write: ");
53  message += changes_name;
54  throw Xapian::DatabaseError(message, errno);
55  }
56  return changes_fd;
57 }
58 
59 void
60 write_and_clear_changes(int changes_fd, string & buf, size_t bytes)
61 {
62  if (changes_fd != -1) {
63  io_write(changes_fd, buf.data(), bytes);
64  }
65  buf.erase(0, bytes);
66 }
void io_write(int fd, const char *p, size_t n)
Write n bytes from block pointed to by p to file descriptor fd.
Definition: io_utils.cc:145
Provides wrappers with POSIXy semantics.
STL namespace.
#define O_CLOEXEC
Definition: safefcntl.h:90
include <sys/stat.h> with portability enhancements
Hierarchy of classes which Xapian can throw as exceptions.
int create_changeset_file(const string &changeset_dir, const string &filename, string &changes_name)
Create a new changeset file, and return an open fd for writing to it.
Utility functions for replication implementations.
Wrappers for low-level POSIX I/O routines.
#define posixy_open
<unistd.h>, but with compat.
DatabaseError indicates some sort of database related error.
Definition: error.h:367
include <fcntl.h>, but working around broken platforms.
void write_and_clear_changes(int changes_fd, string &buf, size_t bytes)
Write some changes from a buffer, and then drop them from the buffer.