]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Add util::likely_size_on_disk
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 28 Feb 2023 20:46:11 +0000 (21:46 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 4 Mar 2023 09:10:20 +0000 (10:10 +0100)
src/util/file.hpp
unittest/test_util_file.cpp

index bb0c8b226aec7fb00fde7317b22b50581ef4fcd6..c74b58dc54ba72d4e3027ec54440460a029a243f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -39,6 +39,9 @@ enum class InPlace { yes, no };
 
 void create_cachedir_tag(const std::string& dir);
 
+// Return how much a file of `size` bytes likely would take on disk.
+uint64_t likely_size_on_disk(uint64_t size);
+
 // Read data from `fd` until end of file and call `data_receiver` with the read
 // data. Returns an error if the underlying read(2) call returned -1.
 nonstd::expected<void, std::string> read_fd(int fd, DataReceiver data_receiver);
@@ -87,4 +90,12 @@ nonstd::expected<void, std::string> write_file(const std::string& path,
                                                nonstd::span<const uint8_t> data,
                                                InPlace in_place = InPlace::no);
 
+// --- Inline implementations ---
+
+inline uint64_t
+likely_size_on_disk(uint64_t size)
+{
+  return (size + 4095) & ~4095;
+}
+
 } // namespace util
index b1222b439fec16bcb9872dba6ccde9a53cdfa733..3cf09281d2e0b4b28069b2dc4278743889646194 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Joel Rosdahl and other contributors
+// Copyright (C) 2022-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
 
 using TestUtil::TestContext;
 
+TEST_CASE("util::likely_size_on_disk")
+{
+  CHECK(util::likely_size_on_disk(0) == 0);
+  CHECK(util::likely_size_on_disk(1) == 4096);
+  CHECK(util::likely_size_on_disk(4095) == 4096);
+  CHECK(util::likely_size_on_disk(4096) == 4096);
+  CHECK(util::likely_size_on_disk(4097) == 8192);
+}
+
 TEST_CASE("util::read_file and util::write_file, text data")
 {
   TestContext test_context;