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