1 /* Cumulus: Smart Filesystem Backup to Dumb Servers
3 * Copyright (C) 2006-2008 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 /* Backup data is stored in a collection of objects, which are grouped together
22 * into segments for storage purposes. This implementation of the object store
23 * represents segments as TAR files and objects as files within them. */
44 /* In memory datatype to represent key/value pairs of information, such as file
45 * metadata. Currently implemented as map<string, string>. */
46 typedef std::map<std::string, std::string> dictionary;
48 /* IOException will be thrown if an error occurs while reading or writing in
49 * one of the I/O wrappers. Depending upon the context; this may be fatal or
50 * not--typically, errors reading/writing the store will be serious, but errors
51 * reading an individual file are less so. */
52 class IOException : public std::exception {
56 explicit IOException(const std::string &err) { error = err; }
57 virtual ~IOException() throw () { }
58 std::string getError() const { return error; }
61 /* Simplified TAR header--we only need to store regular files, don't need to
62 * handle long filenames, etc. */
63 static const int TAR_BLOCK_SIZE = 512;
85 /* A simple wrapper around a single TAR file to represent a segment. Objects
86 * may only be written out all at once, since the tar header must be written
87 * first; incremental writing is not supported. */
90 Tarfile(RemoteFile *file, const std::string &segment);
93 void write_object(int id, const char *data, size_t len);
95 // Return an estimate of the size of the file.
96 size_t size_estimate();
100 std::string segment_name;
104 /* Filter support. */
105 int real_fd, filter_fd;
108 // Write data to the tar file
109 void tar_write(const char *data, size_t size);
112 class TarSegmentStore {
114 // New segments will be stored in the given directory.
115 TarSegmentStore(RemoteStore *remote,
117 { this->remote = remote; this->db = db; }
118 ~TarSegmentStore() { sync(); }
120 // Writes an object to segment in the store, and returns the name
121 // (segment/object) to refer to it. The optional parameter group can be
122 // used to control object placement; objects with different group
123 // parameters are kept in separate segments.
124 ObjectReference write_object(const char *data, size_t len,
125 const std::string &group = "");
127 // Ensure all segments have been fully written.
130 // Dump statistics to stdout about how much data has been written
134 struct segment_info {
137 std::string name; // UUID
138 int count; // Objects written to this segment
139 int size; // Combined size of objects written
140 std::string basename; // Name of segment without directory
145 std::map<std::string, struct segment_info *> segments;
148 // Ensure that all segments in the given group have been fully written.
149 void close_segment(const std::string &group);
151 // Parse an object reference string and return just the segment name
153 std::string object_reference_to_segment(const std::string &object);
156 /* An in-memory representation of an object, which can be incrementally built
157 * before it is written out to a segment. */
163 // If an object is placed in a group, it will be written out to segments
164 // only containing other objects in the same group. A group name is simply
166 //std::string get_group() const { return group; }
167 void set_group(const std::string &g) { group = g; }
169 // Data in an object must be written all at once, and cannot be generated
170 // incrementally. Data can be an arbitrary block of binary data of any
171 // size. The pointer to the data need only remain valid until write() is
173 void set_data(const char *d, size_t len) { data = d; data_len = len; }
175 // Write an object to a segment, thus making it permanent. This function
176 // can be called at most once.
177 void write(TarSegmentStore *store);
179 // Compute the checksum of an object, and include it in the object
180 // reference. This should be called after write(), and the data specified
181 // by set_data() must remain valid through the call to checksum().
184 // An object is assigned a permanent name once it has been written to a
185 // segment. Until that time, its name cannot be determined.
186 std::string get_name() const { return ref.to_string(); }
187 ObjectReference get_ref() { return ref; }
198 /* Program through which segment data is piped before being written to file. */
199 extern const char *filter_program;
201 /* Extension which should be appended to segments written out (.tar is already
202 * included; this adds to it) */
203 extern const char *filter_extension;
205 /* Launch a process to filter data written to a file descriptor. fd_out is the
206 * file descriptor where the filtered data should be written. program is the
207 * filter program to execute (a single string which will be interpreted by
208 * /bin/sh). The return value is a file descriptor to which the data to be
209 * filtered should be written. The process ID of the filter process is stored
210 * at address filter_pid if non-NULL. */
211 int spawn_filter(int fd_out, const char *program, pid_t *filter_pid);
213 #endif // _LBS_STORE_H