2c2f523a9f01108fe451a6ae57d3cdceab9551b6
[cumulus.git] / tarstore.cc
1 /* LBS: An LFS-inspired filesystem backup system
2  * Copyright (C) 2006  Michael Vrable
3  *
4  * Backup data is stored in a collection of objects, which are grouped together
5  * into segments for storage purposes.  This implementation of the object store
6  * is built on top of libtar, and represents segments as TAR files and objects
7  * as files within them. */
8
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <time.h>
15 #include <uuid/uuid.h>
16
17 #include <string>
18 #include <iostream>
19
20 #include "tarstore.h"
21
22 using std::string;
23
24 Tarfile::Tarfile(const string &path, const string &segment)
25     : segment_name(segment)
26 {
27     if (tar_open(&t, (char *)path.c_str(), NULL, O_WRONLY | O_CREAT, 0600,
28                  TAR_VERBOSE | TAR_GNU) == -1)
29         throw IOException("Error opening Tarfile");
30 }
31
32 Tarfile::~Tarfile()
33 {
34     string checksum_list = checksums.str();
35     internal_write_object(segment_name + "/checksums",
36                           checksum_list.data(), checksum_list.size());
37     tar_append_eof(t);
38
39     if (tar_close(t) != 0)
40         throw IOException("Error closing Tarfile");
41 }
42
43 void Tarfile::write_object(int id, const char *data, size_t len)
44 {
45     char buf[64];
46     sprintf(buf, "%08x", id);
47     string path = segment_name + "/" + buf;
48
49     internal_write_object(path, data, len);
50
51     // Compute a checksum for the data block, which will be stored at the end
52     // of the TAR file.
53     SHA1Checksum hash;
54     hash.process(data, len);
55     sprintf(buf, "%08x", id);
56     checksums << buf << " " << hash.checksum_str() << "\n";
57 }
58
59 void Tarfile::internal_write_object(const string &path,
60                                     const char *data, size_t len)
61 {
62     memset(&t->th_buf, 0, sizeof(struct tar_header));
63
64     th_set_type(t, S_IFREG | 0600);
65     th_set_user(t, 0);
66     th_set_group(t, 0);
67     th_set_mode(t, 0600);
68     th_set_size(t, len);
69     th_set_mtime(t, time(NULL));
70     th_set_path(t, const_cast<char *>(path.c_str()));
71     th_finish(t);
72
73     if (th_write(t) != 0)
74         throw IOException("Error writing tar header");
75
76     if (len == 0)
77         return;
78
79     size_t blocks = (len + T_BLOCKSIZE - 1) / T_BLOCKSIZE;
80     size_t padding = blocks * T_BLOCKSIZE - len;
81
82     for (size_t i = 0; i < blocks - 1; i++) {
83         if (tar_block_write(t, &data[i * T_BLOCKSIZE]) == -1)
84             throw IOException("Error writing tar block");
85     }
86
87     char block[T_BLOCKSIZE];
88     memset(block, 0, sizeof(block));
89     memcpy(block, &data[T_BLOCKSIZE * (blocks - 1)], T_BLOCKSIZE - padding);
90     if (tar_block_write(t, block) == -1)
91         throw IOException("Error writing final tar block");
92 }
93
94 string TarSegmentStore::write_object(const char *data, size_t len, const
95                                      std::string &group)
96 {
97     struct segment_info *segment;
98
99     // Find the segment into which the object should be written, looking up by
100     // group.  If no segment exists yet, create one.
101     if (segments.find(group) == segments.end()) {
102         segment = new segment_info;
103
104         uint8_t uuid[16];
105         char uuid_buf[40];
106         uuid_generate(uuid);
107         uuid_unparse_lower(uuid, uuid_buf);
108         segment->name = uuid_buf;
109
110         string filename = path + "/" + segment->name + ".tar";
111         segment->file = new Tarfile(filename, segment->name);
112
113         segment->count = 0;
114
115         segments[group] = segment;
116     } else {
117         segment = segments[group];
118     }
119
120     int id = segment->count;
121     char id_buf[64];
122     sprintf(id_buf, "%08x", id);
123
124     segment->file->write_object(id, data, len);
125     segment->count++;
126
127     return segment->name + "/" + id_buf;
128 }
129
130 void TarSegmentStore::sync()
131 {
132     while (!segments.empty()) {
133         const string &name = segments.begin()->first;
134         struct segment_info *segment = segments[name];
135
136         fprintf(stderr, "Closing segment group %s (%s)\n",
137                 name.c_str(), segment->name.c_str());
138
139         delete segment->file;
140         segments.erase(segments.begin());
141         delete segment;
142     }
143 }