Update copyright notices to use a central AUTHORS file.
[cumulus.git] / subfile.h
1 /* Cumulus: Efficient Filesystem Backup to the Cloud
2  * Copyright (C) 2008 The Cumulus Developers
3  * See the AUTHORS file for a list of contributors.
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 /* Allow for sub-file incremental backups: if only a portion of a file changes,
21  * allow the new data to be written out, and the old data to simply be
22  * referenced from the new metadata log. */
23
24 #ifndef _LBS_SUBFILE_H
25 #define _LBS_SUBFILE_H
26
27 #include <list>
28 #include <map>
29 #include <set>
30 #include <string>
31 #include <vector>
32
33 #include "localdb.h"
34 #include "ref.h"
35 #include "store.h"
36 #include "third_party/chunk.h"
37
38 class Subfile {
39 public:
40     Subfile(LocalDb *localdb);
41     ~Subfile();
42
43     // Prepare to compute a subfile incremental by loading signatures for data
44     // in the old file.
45     void load_old_blocks(const std::list<ObjectReference> &blocks);
46
47     // Break a new block of data into small chunks, and compute checksums of
48     // the chunks.  After doing so, a delta can be computed, or the signatures
49     // can be written out to the database.  The caller must not modify the
50     // buffer until all operations referring to it are finished.
51     void analyze_new_block(const char *buf, size_t len);
52
53     // Store the signatures for the most recently-analyzed block in the local
54     // database (linked to the specified object), if the block is sufficiently
55     // large.  If signatures already exist, they will be overwritten.
56     void store_analyzed_signatures(ObjectReference ref);
57
58     std::list<ObjectReference> create_incremental(TarSegmentStore *tss,
59                                                   LbsObject *o,
60                                                   double block_age);
61
62     static const int HASH_SIZE = 20;
63
64 private:
65     struct chunk_info {
66         char hash[HASH_SIZE];
67         int offset, len;
68     };
69
70     struct block_summary {
71         ObjectReference ref;
72         int num_chunks;
73         struct chunk_info *chunks;
74     };
75
76     LocalDb *db;
77     bool checksums_loaded;
78     std::set<ObjectReference> old_blocks;
79     std::vector<block_summary> block_list;
80     std::map<std::string, std::pair<int, int> > chunk_index;
81
82     bool new_block_summary_valid;
83     block_summary new_block_summary;
84
85     const char *analyzed_buf;
86     size_t analyzed_len;
87
88     void ensure_signatures_loaded();
89     void index_chunks(ObjectReference ref);
90     void free_analysis();
91     void store_block_signatures(ObjectReference ref, block_summary summary);
92
93     std::string get_algorithm() {
94         return chunk_algorithm_name() + "/sha1";
95     }
96 };
97
98 #endif // _LBS_SUBFILE_H