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