Skip to content

Commit cde36cf

Browse files
committed
File system image structure, version 2, part 1
1 parent 2f3f1fa commit cde36cf

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

include/dwarfs/fstypes.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <folly/small_vector.h>
3333

3434
#include "dwarfs/block_compressor.h" // TODO: or the other way round?
35+
#include "dwarfs/checksum.h"
3536

3637
namespace dwarfs {
3738

@@ -64,7 +65,7 @@ struct iovec_read_buf {
6465
};
6566

6667
constexpr uint8_t MAJOR_VERSION = 2;
67-
constexpr uint8_t MINOR_VERSION = 1;
68+
constexpr uint8_t MINOR_VERSION = 2;
6869

6970
enum class section_type : uint16_t {
7071
BLOCK = 0,
@@ -93,6 +94,26 @@ struct section_header {
9394
void dump(std::ostream& os) const;
9495
};
9596

97+
struct section_header_v2 {
98+
char magic[6]; // [0] "DWARFS" / file_header no longer needed
99+
uint8_t major; // [6] major version
100+
uint8_t minor; // [7] minor version
101+
uint8_t sha2_512_256[32]; // [8] SHA2-512/256 starting from next field
102+
uint64_t xxh3_64; // [40] XXH3-64 starting from next field
103+
uint32_t number; // [48] section number
104+
uint16_t type; // [52] section type
105+
uint16_t compression; // [54] compression
106+
uint64_t length; // [56] length of section
107+
108+
static_assert(checksum::digest_size(checksum::algorithm::XXH3_64) ==
109+
sizeof(xxh3_64));
110+
static_assert(checksum::digest_size(checksum::algorithm::SHA2_512_256) ==
111+
sizeof(sha2_512_256));
112+
113+
std::string to_string() const;
114+
void dump(std::ostream& os) const;
115+
};
116+
96117
std::string get_compression_name(compression_type type);
97118

98119
std::string get_section_name(section_type type);

src/dwarfs/filesystem_writer.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <folly/system/ThreadName.h>
3232

3333
#include "dwarfs/block_compressor.h"
34+
#include "dwarfs/checksum.h"
3435
#include "dwarfs/filesystem_writer.h"
3536
#include "dwarfs/fstypes.h"
3637
#include "dwarfs/logger.h"
@@ -155,7 +156,6 @@ class filesystem_writer_ : public filesystem_writer::impl {
155156
template <typename T>
156157
void write(const T& obj);
157158
void write(const std::vector<uint8_t>& data);
158-
void write_file_header();
159159
void writer_thread();
160160
size_t mem_used() const;
161161

@@ -172,6 +172,7 @@ class filesystem_writer_ : public filesystem_writer::impl {
172172
std::condition_variable cond_;
173173
volatile bool flush_;
174174
std::thread writer_thread_;
175+
uint32_t section_number_{0};
175176
};
176177

177178
template <typename LoggerPolicy>
@@ -188,9 +189,7 @@ filesystem_writer_<LoggerPolicy>::filesystem_writer_(
188189
, max_queue_size_(max_queue_size)
189190
, log_(lgr)
190191
, flush_(false)
191-
, writer_thread_(&filesystem_writer_::writer_thread, this) {
192-
write_file_header();
193-
}
192+
, writer_thread_(&filesystem_writer_::writer_thread, this) {}
194193

195194
template <typename LoggerPolicy>
196195
filesystem_writer_<LoggerPolicy>::~filesystem_writer_() noexcept {
@@ -268,24 +267,31 @@ void filesystem_writer_<LoggerPolicy>::write(const std::vector<uint8_t>& data) {
268267
write(reinterpret_cast<const char*>(&data[0]), data.size());
269268
}
270269

271-
template <typename LoggerPolicy>
272-
void filesystem_writer_<LoggerPolicy>::write_file_header() {
273-
file_header hdr;
274-
::memcpy(&hdr.magic[0], "DWARFS", 6);
275-
hdr.major = MAJOR_VERSION;
276-
hdr.minor = MINOR_VERSION;
277-
write(hdr);
278-
}
279-
280270
template <typename LoggerPolicy>
281271
void filesystem_writer_<LoggerPolicy>::write(section_type type,
282272
compression_type compression,
283273
const std::vector<uint8_t>& data) {
284-
section_header sh;
285-
sh.type = type;
286-
sh.compression = compression;
287-
sh.unused = 0;
274+
section_header_v2 sh;
275+
::memcpy(&sh.magic[0], "DWARFS", 6);
276+
sh.major = MAJOR_VERSION;
277+
sh.minor = MINOR_VERSION;
278+
sh.number = section_number_++;
279+
sh.type = static_cast<uint16_t>(type);
280+
sh.compression = static_cast<uint16_t>(compression);
288281
sh.length = data.size();
282+
283+
checksum xxh(checksum::algorithm::XXH3_64);
284+
xxh.update(&sh.number,
285+
sizeof(section_header_v2) - offsetof(section_header_v2, number));
286+
xxh.update(data.data(), data.size());
287+
DWARFS_CHECK(xxh.finalize(&sh.xxh3_64), "XXH3-64 checksum failed");
288+
289+
checksum sha(checksum::algorithm::SHA2_512_256);
290+
sha.update(&sh.xxh3_64,
291+
sizeof(section_header_v2) - offsetof(section_header_v2, xxh3_64));
292+
sha.update(data.data(), data.size());
293+
DWARFS_CHECK(sha.finalize(&sh.sha2_512_256), "SHA512/256 checksum failed");
294+
289295
write(sh);
290296
write(data);
291297

0 commit comments

Comments
 (0)