1 /* LBS: An LFS-inspired filesystem backup system
2 * Copyright (C) 2007 Michael Vrable
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. */
11 #include <sys/types.h>
29 Tarfile::Tarfile(const string &path, const string &segment)
33 if (tar_open(&t, (char *)path.c_str(), NULL, O_WRONLY | O_CREAT, 0600,
34 TAR_VERBOSE | TAR_GNU) == -1)
35 throw IOException("Error opening Tarfile");
42 if (tar_close(t) != 0)
43 throw IOException("Error closing Tarfile");
46 void Tarfile::write_object(int id, const char *data, size_t len)
49 sprintf(buf, "%08x", id);
50 string path = segment_name + "/" + buf;
52 internal_write_object(path, data, len);
55 void Tarfile::internal_write_object(const string &path,
56 const char *data, size_t len)
58 memset(&t->th_buf, 0, sizeof(struct tar_header));
60 th_set_type(t, S_IFREG | 0600);
65 th_set_mtime(t, time(NULL));
66 th_set_path(t, const_cast<char *>(path.c_str()));
70 throw IOException("Error writing tar header");
77 size_t blocks = (len + T_BLOCKSIZE - 1) / T_BLOCKSIZE;
78 size_t padding = blocks * T_BLOCKSIZE - len;
80 for (size_t i = 0; i < blocks - 1; i++) {
81 if (tar_block_write(t, &data[i * T_BLOCKSIZE]) == -1)
82 throw IOException("Error writing tar block");
85 char block[T_BLOCKSIZE];
86 memset(block, 0, sizeof(block));
87 memcpy(block, &data[T_BLOCKSIZE * (blocks - 1)], T_BLOCKSIZE - padding);
88 if (tar_block_write(t, block) == -1)
89 throw IOException("Error writing final tar block");
91 size += blocks * T_BLOCKSIZE;
94 static const size_t SEGMENT_SIZE = 4 * 1024 * 1024;
96 ObjectReference TarSegmentStore::write_object(const char *data, size_t len,
97 const std::string &group)
99 struct segment_info *segment;
101 // Find the segment into which the object should be written, looking up by
102 // group. If no segment exists yet, create one.
103 if (segments.find(group) == segments.end()) {
104 segment = new segment_info;
106 segment->name = generate_uuid();
108 string filename = path + "/" + segment->name + ".tar";
109 segment->file = new Tarfile(filename, segment->name);
113 segments[group] = segment;
115 segment = segments[group];
118 int id = segment->count;
120 sprintf(id_buf, "%08x", id);
122 segment->file->write_object(id, data, len);
125 ObjectReference ref(segment->name, id_buf);
127 // If this segment meets or exceeds the size target, close it so that
128 // future objects will go into a new segment.
129 if (segment->file->size_estimate() >= SEGMENT_SIZE)
130 close_segment(group);
135 void TarSegmentStore::sync()
137 while (!segments.empty())
138 close_segment(segments.begin()->first);
141 void TarSegmentStore::close_segment(const string &group)
143 struct segment_info *segment = segments[group];
145 delete segment->file;
146 segments.erase(segments.find(group));
150 string TarSegmentStore::object_reference_to_segment(const string &object)
155 LbsObject::LbsObject()
156 : group(""), data(NULL), data_len(0), written(false)
160 LbsObject::~LbsObject()
164 void LbsObject::write(TarSegmentStore *store)
166 assert(data != NULL);
169 ref = store->write_object(data, data_len, group);
173 void LbsObject::checksum()
178 hash.process(data, data_len);
179 ref.set_checksum(hash.checksum_str());