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 /* Simplified TAR header--we only need to store regular files, don't need to
49 * handle long filenames, etc. */
50 static const int TAR_BLOCK_SIZE = 512;
72 /* A simple wrapper around a single TAR file to represent a segment. Objects
73 * may only be written out all at once, since the tar header must be written
74 * first; incremental writing is not supported. */
77 Tarfile(RemoteFile *file, const std::string &segment);
80 void write_object(int id, const char *data, size_t len);
82 // Return an estimate of the size of the file.
83 size_t size_estimate();
87 std::string segment_name;
92 int real_fd, filter_fd;
95 // Write data to the tar file
96 void tar_write(const char *data, size_t size);
99 class TarSegmentStore {
101 // New segments will be stored in the given directory.
102 TarSegmentStore(RemoteStore *remote,
104 { this->remote = remote; this->db = db; }
105 ~TarSegmentStore() { sync(); }
107 // Writes an object to segment in the store, and returns the name
108 // (segment/object) to refer to it. The optional parameter group can be
109 // used to control object placement; objects with different group
110 // parameters are kept in separate segments.
111 ObjectReference write_object(const char *data, size_t len,
112 const std::string &group = "");
114 // Ensure all segments have been fully written.
117 // Dump statistics to stdout about how much data has been written
121 struct segment_info {
124 std::string name; // UUID
125 int count; // Objects written to this segment
126 int size; // Combined size of objects written
127 std::string basename; // Name of segment without directory
132 std::map<std::string, struct segment_info *> segments;
135 // Ensure that all segments in the given group have been fully written.
136 void close_segment(const std::string &group);
138 // Parse an object reference string and return just the segment name
140 std::string object_reference_to_segment(const std::string &object);
143 /* An in-memory representation of an object, which can be incrementally built
144 * before it is written out to a segment. */
150 // If an object is placed in a group, it will be written out to segments
151 // only containing other objects in the same group. A group name is simply
153 //std::string get_group() const { return group; }
154 void set_group(const std::string &g) { group = g; }
156 // Data in an object must be written all at once, and cannot be generated
157 // incrementally. Data can be an arbitrary block of binary data of any
158 // size. The pointer to the data need only remain valid until write() is
160 void set_data(const char *d, size_t len) { data = d; data_len = len; }
162 // Write an object to a segment, thus making it permanent. This function
163 // can be called at most once.
164 void write(TarSegmentStore *store);
166 // Compute the checksum of an object, and include it in the object
167 // reference. This should be called after write(), and the data specified
168 // by set_data() must remain valid through the call to checksum().
171 // An object is assigned a permanent name once it has been written to a
172 // segment. Until that time, its name cannot be determined.
173 std::string get_name() const { return ref.to_string(); }
174 ObjectReference get_ref() { return ref; }
185 /* Program through which segment data is piped before being written to file. */
186 extern const char *filter_program;
188 /* Extension which should be appended to segments written out (.tar is already
189 * included; this adds to it) */
190 extern const char *filter_extension;
192 /* Launch a process to filter data written to a file descriptor. fd_out is the
193 * file descriptor where the filtered data should be written. program is the
194 * filter program to execute (a single string which will be interpreted by
195 * /bin/sh). The return value is a file descriptor to which the data to be
196 * filtered should be written. The process ID of the filter process is stored
197 * at address filter_pid if non-NULL. */
198 int spawn_filter(int fd_out, const char *program, pid_t *filter_pid);
200 #endif // _LBS_STORE_H