cumulus-util: In list-snapshot-sizes output, display backup intent values.
[cumulus.git] / store.h
1 /* Cumulus: Smart Filesystem Backup to Dumb Servers
2  *
3  * Copyright (C) 2006-2008  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
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.
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 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.
19  */
20
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. */
24
25 #ifndef _LBS_STORE_H
26 #define _LBS_STORE_H
27
28 #include <stdint.h>
29
30 #include <list>
31 #include <map>
32 #include <set>
33 #include <string>
34 #include <iostream>
35 #include <sstream>
36
37 #include "localdb.h"
38 #include "remote.h"
39 #include "sha1.h"
40 #include "ref.h"
41
42 class LbsObject;
43
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;
47
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 {
53 private:
54     std::string error;
55 public:
56     explicit IOException(const std::string &err) { error = err; }
57     virtual ~IOException() throw () { }
58     std::string getError() const { return error; }
59 };
60
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;
64
65 struct tar_header
66 {
67     char name[100];
68     char mode[8];
69     char uid[8];
70     char gid[8];
71     char size[12];
72     char mtime[12];
73     char chksum[8];
74     char typeflag;
75     char linkname[100];
76     char magic[8];
77     char uname[32];
78     char gname[32];
79     char devmajor[8];
80     char devminor[8];
81     char prefix[155];
82     char padding[12];
83 };
84
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. */
88 class Tarfile {
89 public:
90     Tarfile(RemoteFile *file, const std::string &segment);
91     ~Tarfile();
92
93     void write_object(int id, const char *data, size_t len);
94
95     // Return an estimate of the size of the file.
96     size_t size_estimate();
97
98 private:
99     size_t size;
100     std::string segment_name;
101
102     RemoteFile *file;
103
104     /* Filter support. */
105     int real_fd, filter_fd;
106     pid_t filter_pid;
107
108     // Write data to the tar file
109     void tar_write(const char *data, size_t size);
110 };
111
112 class TarSegmentStore {
113 public:
114     // New segments will be stored in the given directory.
115     TarSegmentStore(RemoteStore *remote,
116                     LocalDb *db = NULL)
117         { this->remote = remote; this->db = db; }
118     ~TarSegmentStore() { sync(); }
119
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 = "");
126
127     // Ensure all segments have been fully written.
128     void sync();
129
130     // Dump statistics to stdout about how much data has been written
131     void dump_stats();
132
133 private:
134     struct segment_info {
135         Tarfile *file;
136         std::string group;
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
141         RemoteFile *rf;
142     };
143
144     RemoteStore *remote;
145     std::map<std::string, struct segment_info *> segments;
146     LocalDb *db;
147
148     // Ensure that all segments in the given group have been fully written.
149     void close_segment(const std::string &group);
150
151     // Parse an object reference string and return just the segment name
152     // portion.
153     std::string object_reference_to_segment(const std::string &object);
154 };
155
156 /* An in-memory representation of an object, which can be incrementally built
157  * before it is written out to a segment. */
158 class LbsObject {
159 public:
160     LbsObject();
161     ~LbsObject();
162
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
165     // a string.
166     //std::string get_group() const { return group; }
167     void set_group(const std::string &g) { group = g; }
168
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
172     // called.
173     void set_data(const char *d, size_t len) { data = d; data_len = len; }
174
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);
178
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().
182     void checksum();
183
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; }
188
189 private:
190     std::string group;
191     const char *data;
192     size_t data_len;
193
194     bool written;
195     ObjectReference ref;
196 };
197
198 /* Program through which segment data is piped before being written to file. */
199 extern const char *filter_program;
200
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;
204
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);
212
213 #endif // _LBS_STORE_H