]> git.sesse.net Git - plocate/blob - database-builder.cpp
439e25a59e88980ef6a2f8f7bb9d38c43f7134ec
[plocate] / database-builder.cpp
1 #include "database-builder.h"
2
3 #include "dprintf.h"
4 #include "turbopfor-encode.h"
5
6 #include <algorithm>
7 #include <assert.h>
8 #include <fcntl.h>
9 #include <string.h>
10 #include <string_view>
11 #include <sys/stat.h>
12 #include <sys/time.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15 #include <zdict.h>
16 #include <zstd.h>
17
18 #define P4NENC_BOUND(n) ((n + 127) / 128 + (n + 32) * sizeof(uint32_t))
19
20 #define NUM_TRIGRAMS 16777216
21
22 using namespace std;
23 using namespace std::chrono;
24
25 constexpr unsigned num_overflow_slots = 16;
26
27 string zstd_compress(const string &src, ZSTD_CDict *cdict, string *tempbuf);
28
29 static inline uint32_t read_unigram(const string_view s, size_t idx)
30 {
31         if (idx < s.size()) {
32                 return (unsigned char)s[idx];
33         } else {
34                 return 0;
35         }
36 }
37
38 static inline uint32_t read_trigram(const string_view s, size_t start)
39 {
40         return read_unigram(s, start) |
41                 (read_unigram(s, start + 1) << 8) |
42                 (read_unigram(s, start + 2) << 16);
43 }
44
45 class PostingListBuilder {
46 public:
47         inline void add_docid(uint32_t docid);
48         inline void add_first_docid(uint32_t docid);
49         void finish();
50
51         string encoded;
52         size_t num_docids = 0;
53
54 private:
55         void write_header(uint32_t docid);
56         void append_block();
57
58         vector<uint32_t> pending_deltas;
59
60         uint32_t last_docid = -1;
61 };
62
63 void PostingListBuilder::add_docid(uint32_t docid)
64 {
65         // Deduplicate against the last inserted value, if any.
66         if (docid == last_docid) {
67                 return;
68         }
69
70         assert(num_docids != 0);
71
72         pending_deltas.push_back(docid - last_docid - 1);
73         last_docid = docid;
74         if (pending_deltas.size() == 128) {
75                 append_block();
76                 pending_deltas.clear();
77         }
78         ++num_docids;
79 }
80
81 void PostingListBuilder::add_first_docid(uint32_t docid)
82 {
83         write_header(docid);
84         ++num_docids;
85         last_docid = docid;
86 }
87
88 void PostingListBuilder::finish()
89 {
90         if (pending_deltas.empty()) {
91                 return;
92         }
93
94         assert(!encoded.empty());  // write_header() should already have run.
95
96         // No interleaving for partial blocks.
97         unsigned char buf[P4NENC_BOUND(128)];
98         unsigned char *end = encode_pfor_single_block<128>(pending_deltas.data(), pending_deltas.size(), /*interleaved=*/false, buf);
99         encoded.append(reinterpret_cast<char *>(buf), reinterpret_cast<char *>(end));
100 }
101
102 void PostingListBuilder::append_block()
103 {
104         unsigned char buf[P4NENC_BOUND(128)];
105         assert(pending_deltas.size() == 128);
106         unsigned char *end = encode_pfor_single_block<128>(pending_deltas.data(), 128, /*interleaved=*/true, buf);
107         encoded.append(reinterpret_cast<char *>(buf), reinterpret_cast<char *>(end));
108 }
109
110 void PostingListBuilder::write_header(uint32_t docid)
111 {
112         unsigned char buf[P4NENC_BOUND(1)];
113         unsigned char *end = write_baseval(docid, buf);
114         encoded.append(reinterpret_cast<char *>(buf), end - buf);
115 }
116
117 void DictionaryBuilder::add_file(string filename, dir_time)
118 {
119         if (keep_current_block) {  // Only bother saving the filenames if we're actually keeping the block.
120                 if (!current_block.empty()) {
121                         current_block.push_back('\0');
122                 }
123                 current_block += filename;
124         }
125         if (++num_files_in_block == block_size) {
126                 flush_block();
127         }
128 }
129
130 void DictionaryBuilder::flush_block()
131 {
132         if (keep_current_block) {
133                 if (slot_for_current_block == -1) {
134                         lengths.push_back(current_block.size());
135                         sampled_blocks.push_back(move(current_block));
136                 } else {
137                         lengths[slot_for_current_block] = current_block.size();
138                         sampled_blocks[slot_for_current_block] = move(current_block);
139                 }
140         }
141         current_block.clear();
142         num_files_in_block = 0;
143         ++block_num;
144
145         if (block_num < blocks_to_keep) {
146                 keep_current_block = true;
147                 slot_for_current_block = -1;
148         } else {
149                 // Keep every block with equal probability (reservoir sampling).
150                 uint64_t idx = uniform_int_distribution<uint64_t>(0, block_num)(reservoir_rand);
151                 keep_current_block = (idx < blocks_to_keep);
152                 slot_for_current_block = idx;
153         }
154 }
155
156 string DictionaryBuilder::train(size_t buf_size)
157 {
158         string dictionary_buf;
159         sort(sampled_blocks.begin(), sampled_blocks.end());  // Seemingly important for decompression speed.
160         for (const string &block : sampled_blocks) {
161                 dictionary_buf += block;
162         }
163
164         string buf;
165         buf.resize(buf_size);
166         size_t ret = ZDICT_trainFromBuffer(&buf[0], buf_size, dictionary_buf.data(), lengths.data(), lengths.size());
167         if (ZDICT_isError(ret)) {
168                 return "";
169         }
170         dprintf("Sampled %zu bytes in %zu blocks, built a dictionary of size %zu\n", dictionary_buf.size(), lengths.size(), ret);
171         buf.resize(ret);
172
173         sampled_blocks.clear();
174         lengths.clear();
175
176         return buf;
177 }
178
179 class EncodingCorpus : public DatabaseReceiver {
180 public:
181         EncodingCorpus(FILE *outfp, size_t block_size, ZSTD_CDict *cdict, bool store_dir_times);
182         ~EncodingCorpus();
183
184         void add_file(std::string filename, dir_time dt) override;
185         void flush_block() override;
186         void finish() override;
187
188         std::vector<uint64_t> filename_blocks;
189         size_t num_files = 0, num_files_in_block = 0, num_blocks = 0;
190         bool seen_trigram(uint32_t trgm)
191         {
192                 return invindex[trgm] != nullptr;
193         }
194         size_t num_files_seen() const override { return num_files; }
195         PostingListBuilder &get_pl_builder(uint32_t trgm)
196         {
197                 return *invindex[trgm];
198         }
199
200         void add_docid(uint32_t trgm, uint32_t docid)
201         {
202                 if (invindex[trgm] == nullptr) {
203                         invindex[trgm] = new PostingListBuilder;
204                         invindex[trgm]->add_first_docid(docid);
205                 } else {
206                         invindex[trgm]->add_docid(docid);
207                 }
208         }
209
210         size_t num_trigrams() const;
211         std::string get_compressed_dir_times();
212
213 private:
214         void compress_dir_times(size_t allowed_slop);
215
216         std::unique_ptr<PostingListBuilder *[]> invindex;
217         FILE *outfp;
218         std::string current_block;
219         std::string tempbuf;
220         const size_t block_size;
221         const bool store_dir_times;
222         ZSTD_CDict *cdict;
223
224         ZSTD_CStream *dir_time_ctx = nullptr;
225         std::string dir_times;  // Buffer of still-uncompressed data.
226         std::string dir_times_compressed;
227 };
228
229
230 EncodingCorpus::EncodingCorpus(FILE *outfp, size_t block_size, ZSTD_CDict *cdict, bool store_dir_times)
231         : invindex(new PostingListBuilder *[NUM_TRIGRAMS]), outfp(outfp), block_size(block_size), store_dir_times(store_dir_times), cdict(cdict)
232 {
233         fill(invindex.get(), invindex.get() + NUM_TRIGRAMS, nullptr);
234         if (store_dir_times) {
235                 dir_time_ctx = ZSTD_createCStream();
236                 ZSTD_initCStream(dir_time_ctx, /*level=*/6);
237         }
238 }
239
240 EncodingCorpus::~EncodingCorpus()
241 {
242         for (unsigned i = 0; i < NUM_TRIGRAMS; ++i) {
243                 delete invindex[i];
244         }
245 }
246
247 void EncodingCorpus::add_file(string filename, dir_time dt)
248 {
249         ++num_files;
250         if (!current_block.empty()) {
251                 current_block.push_back('\0');
252         }
253         current_block += filename;
254         if (++num_files_in_block == block_size) {
255                 flush_block();
256         }
257
258         if (store_dir_times) {
259                 if (dt.sec == -1) {
260                         // Not a directory.
261                         dir_times.push_back('\0');
262                 } else {
263                         dir_times.push_back('\1');
264                         dir_times.append(reinterpret_cast<char *>(&dt.sec), sizeof(dt.sec));
265                         dir_times.append(reinterpret_cast<char *>(&dt.nsec), sizeof(dt.nsec));
266                 }
267                 compress_dir_times(/*allowed_slop=*/4096);
268         }
269 }
270
271 void EncodingCorpus::compress_dir_times(size_t allowed_slop)
272 {
273         while (dir_times.size() >= allowed_slop) {
274                 size_t old_size = dir_times_compressed.size();
275                 dir_times_compressed.resize(old_size + 4096);
276
277                 ZSTD_outBuffer outbuf;
278                 outbuf.dst = dir_times_compressed.data() + old_size;
279                 outbuf.size = 4096;
280                 outbuf.pos = 0;
281
282                 ZSTD_inBuffer inbuf;
283                 inbuf.src = dir_times.data();
284                 inbuf.size = dir_times.size();
285                 inbuf.pos = 0;
286
287                 int ret = ZSTD_compressStream(dir_time_ctx, &outbuf, &inbuf);
288                 if (ret < 0) {
289                         fprintf(stderr, "ZSTD_compressStream() failed\n");
290                         exit(1);
291                 }
292
293                 dir_times_compressed.resize(old_size + outbuf.pos);
294                 dir_times.erase(dir_times.begin(), dir_times.begin() + inbuf.pos);
295
296                 if (outbuf.pos == 0 && inbuf.pos == 0) {
297                         // Nothing happened (not enough data?), try again later.
298                         return;
299                 }
300         }
301 }
302
303 void EncodingCorpus::flush_block()
304 {
305         if (current_block.empty()) {
306                 return;
307         }
308
309         uint32_t docid = num_blocks;
310
311         // Create trigrams.
312         const char *ptr = current_block.c_str();
313         while (ptr < current_block.c_str() + current_block.size()) {
314                 string_view s(ptr);
315                 if (s.size() >= 3) {
316                         for (size_t j = 0; j < s.size() - 2; ++j) {
317                                 uint32_t trgm = read_trigram(s, j);
318                                 add_docid(trgm, docid);
319                         }
320                 }
321                 ptr += s.size() + 1;
322         }
323
324         // Compress and add the filename block.
325         filename_blocks.push_back(ftell(outfp));
326         string compressed = zstd_compress(current_block, cdict, &tempbuf);
327         if (fwrite(compressed.data(), compressed.size(), 1, outfp) != 1) {
328                 perror("fwrite()");
329                 exit(1);
330         }
331
332         current_block.clear();
333         num_files_in_block = 0;
334         ++num_blocks;
335 }
336
337 void EncodingCorpus::finish()
338 {
339         flush_block();
340 }
341
342 size_t EncodingCorpus::num_trigrams() const
343 {
344         size_t num = 0;
345         for (unsigned trgm = 0; trgm < NUM_TRIGRAMS; ++trgm) {
346                 if (invindex[trgm] != nullptr) {
347                         ++num;
348                 }
349         }
350         return num;
351 }
352
353 string EncodingCorpus::get_compressed_dir_times()
354 {
355         if (!store_dir_times) {
356                 return "";
357         }
358         compress_dir_times(/*allowed_slop=*/0);
359         assert(dir_times.empty());
360
361         for (;;) {
362                 size_t old_size = dir_times_compressed.size();
363                 dir_times_compressed.resize(old_size + 4096);
364
365                 ZSTD_outBuffer outbuf;
366                 outbuf.dst = dir_times_compressed.data() + old_size;
367                 outbuf.size = 4096;
368                 outbuf.pos = 0;
369
370                 int ret = ZSTD_endStream(dir_time_ctx, &outbuf);
371                 if (ret < 0) {
372                         fprintf(stderr, "ZSTD_compressStream() failed\n");
373                         exit(1);
374                 }
375
376                 dir_times_compressed.resize(old_size + outbuf.pos);
377
378                 if (ret == 0) {
379                         // All done.
380                         break;
381                 }
382         }
383
384         return dir_times_compressed;
385 }
386
387 string zstd_compress(const string &src, ZSTD_CDict *cdict, string *tempbuf)
388 {
389         static ZSTD_CCtx *ctx = nullptr;
390         if (ctx == nullptr) {
391                 ctx = ZSTD_createCCtx();
392         }
393
394         size_t max_size = ZSTD_compressBound(src.size());
395         if (tempbuf->size() < max_size) {
396                 tempbuf->resize(max_size);
397         }
398         size_t size;
399         if (cdict == nullptr) {
400                 size = ZSTD_compressCCtx(ctx, &(*tempbuf)[0], max_size, src.data(), src.size(), /*level=*/6);
401         } else {
402                 size = ZSTD_compress_usingCDict(ctx, &(*tempbuf)[0], max_size, src.data(), src.size(), cdict);
403         }
404         return string(tempbuf->data(), size);
405 }
406
407 bool is_prime(uint32_t x)
408 {
409         if ((x % 2) == 0 || (x % 3) == 0) {
410                 return false;
411         }
412         uint32_t limit = ceil(sqrt(x));
413         for (uint32_t factor = 5; factor <= limit; ++factor) {
414                 if ((x % factor) == 0) {
415                         return false;
416                 }
417         }
418         return true;
419 }
420
421 uint32_t next_prime(uint32_t x)
422 {
423         if ((x % 2) == 0) {
424                 ++x;
425         }
426         while (!is_prime(x)) {
427                 x += 2;
428         }
429         return x;
430 }
431
432 unique_ptr<Trigram[]> create_hashtable(EncodingCorpus &corpus, const vector<uint32_t> &all_trigrams, uint32_t ht_size, uint32_t num_overflow_slots)
433 {
434         unique_ptr<Trigram[]> ht(new Trigram[ht_size + num_overflow_slots + 1]);  // 1 for the sentinel element at the end.
435         for (unsigned i = 0; i < ht_size + num_overflow_slots + 1; ++i) {
436                 ht[i].trgm = uint32_t(-1);
437                 ht[i].num_docids = 0;
438                 ht[i].offset = 0;
439         }
440         for (uint32_t trgm : all_trigrams) {
441                 // We don't know offset yet, so set it to zero.
442                 Trigram to_insert{ trgm, uint32_t(corpus.get_pl_builder(trgm).num_docids), 0 };
443
444                 uint32_t bucket = hash_trigram(trgm, ht_size);
445                 unsigned distance = 0;
446                 while (ht[bucket].num_docids != 0) {
447                         // Robin Hood hashing; reduces the longest distance by a lot.
448                         unsigned other_distance = bucket - hash_trigram(ht[bucket].trgm, ht_size);
449                         if (distance > other_distance) {
450                                 swap(to_insert, ht[bucket]);
451                                 distance = other_distance;
452                         }
453
454                         ++bucket, ++distance;
455                         if (distance > num_overflow_slots) {
456                                 return nullptr;
457                         }
458                 }
459                 ht[bucket] = to_insert;
460         }
461         return ht;
462 }
463
464 DatabaseBuilder::DatabaseBuilder(const char *outfile, gid_t owner, int block_size, string dictionary, bool check_visibility)
465         : outfile(outfile), block_size(block_size)
466 {
467         umask(0027);
468
469         string path = outfile;
470         path.resize(path.find_last_of('/') + 1);
471         if (path.empty()) {
472                 path = ".";
473         }
474 #ifdef O_TMPFILE
475         int fd = open(path.c_str(), O_WRONLY | O_TMPFILE, 0640);
476         if (fd == -1) {
477                 perror(path.c_str());
478                 exit(1);
479         }
480 #else
481         temp_filename = string(outfile) + ".XXXXXX";
482         int fd = mkstemp(&temp_filename[0]);
483         if (fd == -1) {
484                 perror(temp_filename.c_str());
485                 exit(1);
486         }
487         if (fchmod(fd, 0640) == -1) {
488                 perror("fchmod");
489                 exit(1);
490         }
491 #endif
492
493         if (owner != (gid_t)-1) {
494                 if (fchown(fd, (uid_t)-1, owner) == -1) {
495                         perror("fchown");
496                         exit(1);
497                 }
498         }
499
500         outfp = fdopen(fd, "wb");
501         if (outfp == nullptr) {
502                 perror(outfile);
503                 exit(1);
504         }
505
506         // Write the header.
507         memcpy(hdr.magic, "\0plocate", 8);
508         hdr.version = -1;  // Mark as broken.
509         hdr.hashtable_size = 0;  // Not known yet.
510         hdr.extra_ht_slots = num_overflow_slots;
511         hdr.num_docids = 0;
512         hdr.hash_table_offset_bytes = -1;  // We don't know these offsets yet.
513         hdr.max_version = 2;
514         hdr.filename_index_offset_bytes = -1;
515         hdr.zstd_dictionary_length_bytes = -1;
516         hdr.check_visibility = check_visibility;
517         fwrite(&hdr, sizeof(hdr), 1, outfp);
518
519         if (dictionary.empty()) {
520                 hdr.zstd_dictionary_offset_bytes = 0;
521                 hdr.zstd_dictionary_length_bytes = 0;
522         } else {
523                 hdr.zstd_dictionary_offset_bytes = ftell(outfp);
524                 fwrite(dictionary.data(), dictionary.size(), 1, outfp);
525                 hdr.zstd_dictionary_length_bytes = dictionary.size();
526                 cdict = ZSTD_createCDict(dictionary.data(), dictionary.size(), /*level=*/6);
527         }
528
529         hdr.directory_data_length_bytes = 0;
530         hdr.directory_data_offset_bytes = 0;
531         hdr.next_zstd_dictionary_length_bytes = 0;
532         hdr.next_zstd_dictionary_offset_bytes = 0;
533         hdr.conf_block_length_bytes = 0;
534         hdr.conf_block_offset_bytes = 0;
535 }
536
537 DatabaseReceiver *DatabaseBuilder::start_corpus(bool store_dir_times)
538 {
539         corpus_start = steady_clock::now();
540         corpus = new EncodingCorpus(outfp, block_size, cdict, store_dir_times);
541         return corpus;
542 }
543
544 void DatabaseBuilder::set_next_dictionary(std::string next_dictionary)
545 {
546         this->next_dictionary = move(next_dictionary);
547 }
548
549 void DatabaseBuilder::set_conf_block(std::string conf_block)
550 {
551         this->conf_block = move(conf_block);
552 }
553
554 void DatabaseBuilder::finish_corpus()
555 {
556         corpus->finish();
557         hdr.num_docids = corpus->filename_blocks.size();
558
559         // Stick an empty block at the end as sentinel.
560         corpus->filename_blocks.push_back(ftell(outfp));
561         const size_t bytes_for_filenames = corpus->filename_blocks.back() - corpus->filename_blocks.front();
562
563         // Write the offsets to the filenames.
564         hdr.filename_index_offset_bytes = ftell(outfp);
565         const size_t bytes_for_filename_index = corpus->filename_blocks.size() * sizeof(uint64_t);
566         fwrite(corpus->filename_blocks.data(), corpus->filename_blocks.size(), sizeof(uint64_t), outfp);
567         corpus->filename_blocks.clear();
568         corpus->filename_blocks.shrink_to_fit();
569
570         // Finish up encoding the posting lists.
571         size_t trigrams = 0, longest_posting_list = 0;
572         size_t bytes_for_posting_lists = 0;
573         for (unsigned trgm = 0; trgm < NUM_TRIGRAMS; ++trgm) {
574                 if (!corpus->seen_trigram(trgm))
575                         continue;
576                 PostingListBuilder &pl_builder = corpus->get_pl_builder(trgm);
577                 pl_builder.finish();
578                 longest_posting_list = max(longest_posting_list, pl_builder.num_docids);
579                 trigrams += pl_builder.num_docids;
580                 bytes_for_posting_lists += pl_builder.encoded.size();
581         }
582         size_t num_trigrams = corpus->num_trigrams();
583         dprintf("%zu files, %zu different trigrams, %zu entries, avg len %.2f, longest %zu\n",
584                 corpus->num_files, num_trigrams, trigrams, double(trigrams) / num_trigrams, longest_posting_list);
585         dprintf("%zu bytes used for posting lists (%.2f bits/entry)\n", bytes_for_posting_lists, 8 * bytes_for_posting_lists / double(trigrams));
586
587         dprintf("Building posting lists took %.1f ms.\n\n", 1e3 * duration<float>(steady_clock::now() - corpus_start).count());
588
589         // Find the used trigrams.
590         vector<uint32_t> all_trigrams;
591         for (unsigned trgm = 0; trgm < NUM_TRIGRAMS; ++trgm) {
592                 if (corpus->seen_trigram(trgm)) {
593                         all_trigrams.push_back(trgm);
594                 }
595         }
596
597         // Create the hash table.
598         unique_ptr<Trigram[]> hashtable;
599         uint32_t ht_size = next_prime(all_trigrams.size());
600         for (;;) {
601                 hashtable = create_hashtable(*corpus, all_trigrams, ht_size, num_overflow_slots);
602                 if (hashtable == nullptr) {
603                         dprintf("Failed creating hash table of size %u, increasing by 5%% and trying again.\n", ht_size);
604                         ht_size = next_prime(ht_size * 1.05);
605                 } else {
606                         dprintf("Created hash table of size %u.\n\n", ht_size);
607                         break;
608                 }
609         }
610
611         // Find the offsets for each posting list.
612         size_t bytes_for_hashtable = (ht_size + num_overflow_slots + 1) * sizeof(Trigram);
613         uint64_t offset = ftell(outfp) + bytes_for_hashtable;
614         for (unsigned i = 0; i < ht_size + num_overflow_slots + 1; ++i) {
615                 hashtable[i].offset = offset;  // Needs to be there even for empty slots.
616                 if (hashtable[i].num_docids == 0) {
617                         continue;
618                 }
619
620                 const string &encoded = corpus->get_pl_builder(hashtable[i].trgm).encoded;
621                 offset += encoded.size();
622         }
623
624         // Write the hash table.
625         hdr.hash_table_offset_bytes = ftell(outfp);
626         hdr.hashtable_size = ht_size;
627         fwrite(hashtable.get(), ht_size + num_overflow_slots + 1, sizeof(Trigram), outfp);
628
629         // Write the actual posting lists.
630         for (unsigned i = 0; i < ht_size + num_overflow_slots + 1; ++i) {
631                 if (hashtable[i].num_docids == 0) {
632                         continue;
633                 }
634                 const string &encoded = corpus->get_pl_builder(hashtable[i].trgm).encoded;
635                 fwrite(encoded.data(), encoded.size(), 1, outfp);
636         }
637
638         // Finally, write the directory times (for updatedb).
639         string compressed_dir_times = corpus->get_compressed_dir_times();
640         size_t bytes_for_compressed_dir_times = 0;
641         if (!compressed_dir_times.empty()) {
642                 hdr.directory_data_offset_bytes = ftell(outfp);
643                 hdr.directory_data_length_bytes = compressed_dir_times.size();
644                 fwrite(compressed_dir_times.data(), compressed_dir_times.size(), 1, outfp);
645                 bytes_for_compressed_dir_times = compressed_dir_times.size();
646                 compressed_dir_times.clear();
647         }
648
649         // Write the recommended dictionary for next update.
650         if (!next_dictionary.empty()) {
651                 hdr.next_zstd_dictionary_offset_bytes = ftell(outfp);
652                 hdr.next_zstd_dictionary_length_bytes = next_dictionary.size();
653                 fwrite(next_dictionary.data(), next_dictionary.size(), 1, outfp);
654         }
655
656         // And the configuration block.
657         if (!conf_block.empty()) {
658                 hdr.conf_block_offset_bytes = ftell(outfp);
659                 hdr.conf_block_length_bytes = conf_block.size();
660                 fwrite(conf_block.data(), conf_block.size(), 1, outfp);
661         }
662
663         // Rewind, and write the updated header.
664         hdr.version = 1;
665         fseek(outfp, 0, SEEK_SET);
666         fwrite(&hdr, sizeof(hdr), 1, outfp);
667
668 #ifdef O_TMPFILE
669         // Give the file a proper name, making it visible in the file system.
670         // TODO: It would be nice to be able to do this atomically, like with rename.
671         unlink(outfile.c_str());
672         char procpath[256];
673         snprintf(procpath, sizeof(procpath), "/proc/self/fd/%d", fileno(outfp));
674         if (linkat(AT_FDCWD, procpath, AT_FDCWD, outfile.c_str(), AT_SYMLINK_FOLLOW) == -1) {
675                 perror("linkat");
676                 exit(1);
677         }
678 #else
679         if (rename(temp_filename.c_str(), outfile.c_str()) == -1) {
680                 perror("rename");
681                 exit(1);
682         }
683 #endif
684
685         fclose(outfp);
686
687         size_t total_bytes = (bytes_for_hashtable + bytes_for_posting_lists + bytes_for_filename_index + bytes_for_filenames + bytes_for_compressed_dir_times);
688
689         dprintf("Block size:     %7d files\n", block_size);
690         dprintf("Dictionary:     %'7.1f MB\n", hdr.zstd_dictionary_length_bytes / 1048576.0);
691         dprintf("Hash table:     %'7.1f MB\n", bytes_for_hashtable / 1048576.0);
692         dprintf("Posting lists:  %'7.1f MB\n", bytes_for_posting_lists / 1048576.0);
693         dprintf("Filename index: %'7.1f MB\n", bytes_for_filename_index / 1048576.0);
694         dprintf("Filenames:      %'7.1f MB\n", bytes_for_filenames / 1048576.0);
695         if (bytes_for_compressed_dir_times != 0) {
696                 dprintf("Modify times:   %'7.1f MB\n", bytes_for_compressed_dir_times / 1048576.0);
697         }
698         dprintf("Total:          %'7.1f MB\n", total_bytes / 1048576.0);
699         dprintf("\n");
700 }