1 /* Cumulus: Efficient Filesystem Backup to the Cloud
2 * Copyright (C) 2006-2008 The Cumulus Developers
3 * See the AUTHORS file for a list of contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 /* Backup data is stored in a collection of objects, which are grouped together
21 * into segments for storage purposes. This implementation of the object store
22 * represents segments as TAR files and objects as files within them. */
39 #include "third_party/sha1.h"
43 /* In memory datatype to represent key/value pairs of information, such as file
44 * metadata. Currently implemented as map<string, string>. */
45 typedef std::map<std::string, std::string> dictionary;
47 /* Simplified TAR header--we only need to store regular files, don't need to
48 * handle long filenames, etc. */
49 static const int TAR_BLOCK_SIZE = 512;
71 /* A simple wrapper around a single TAR file to represent a segment. Objects
72 * may only be written out all at once, since the tar header must be written
73 * first; incremental writing is not supported. */
76 Tarfile(RemoteFile *file, const std::string &segment);
79 void write_object(int id, const char *data, size_t len);
81 // Return an estimate of the size of the file.
82 size_t size_estimate();
86 std::string segment_name;
91 int real_fd, filter_fd;
94 // Write data to the tar file
95 void tar_write(const char *data, size_t size);
98 class TarSegmentStore {
100 // New segments will be stored in the given directory.
101 TarSegmentStore(RemoteStore *remote,
103 { this->remote = remote; this->db = db; }
104 ~TarSegmentStore() { sync(); }
106 // Writes an object to segment in the store, and returns the name
107 // (segment/object) to refer to it. The optional parameter group can be
108 // used to control object placement; objects with different group
109 // parameters are kept in separate segments.
110 ObjectReference write_object(const char *data, size_t len,
111 const std::string &group = "");
113 // Ensure all segments have been fully written.
116 // Dump statistics to stdout about how much data has been written
120 struct segment_info {
123 std::string name; // UUID
124 int count; // Objects written to this segment
125 int size; // Combined size of objects written
126 std::string basename; // Name of segment without directory
131 std::map<std::string, struct segment_info *> segments;
134 // Ensure that all segments in the given group have been fully written.
135 void close_segment(const std::string &group);
137 // Parse an object reference string and return just the segment name
139 std::string object_reference_to_segment(const std::string &object);
142 /* An in-memory representation of an object, which can be incrementally built
143 * before it is written out to a segment. */
149 // If an object is placed in a group, it will be written out to segments
150 // only containing other objects in the same group. A group name is simply
152 //std::string get_group() const { return group; }
153 void set_group(const std::string &g) { group = g; }
155 // Data in an object must be written all at once, and cannot be generated
156 // incrementally. Data can be an arbitrary block of binary data of any
157 // size. The pointer to the data need only remain valid until write() is
159 void set_data(const char *d, size_t len) { data = d; data_len = len; }
161 // Write an object to a segment, thus making it permanent. This function
162 // can be called at most once.
163 void write(TarSegmentStore *store);
165 // Compute the checksum of an object, and include it in the object
166 // reference. This should be called after write(), and the data specified
167 // by set_data() must remain valid through the call to checksum().
170 // An object is assigned a permanent name once it has been written to a
171 // segment. Until that time, its name cannot be determined.
172 std::string get_name() const { return ref.to_string(); }
173 ObjectReference get_ref() { return ref; }
184 /* Program through which segment data is piped before being written to file. */
185 extern const char *filter_program;
187 /* Extension which should be appended to segments written out (.tar is already
188 * included; this adds to it) */
189 extern const char *filter_extension;
191 /* Launch a process to filter data written to a file descriptor. fd_out is the
192 * file descriptor where the filtered data should be written. program is the
193 * filter program to execute (a single string which will be interpreted by
194 * /bin/sh). The return value is a file descriptor to which the data to be
195 * filtered should be written. The process ID of the filter process is stored
196 * at address filter_pid if non-NULL. */
197 int spawn_filter(int fd_out, const char *program, pid_t *filter_pid);
199 #endif // _LBS_STORE_H