From 6e93ff6ac5b514d1b8dd9ccb2d6372576a52761a Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Sun, 5 Dec 2021 01:14:52 +0100 Subject: [PATCH] Set up {m,c,a}times on copied files (#2145). --- src/lib/ext.cc | 16 ++++++++++++++++ test/disk_writer_test.cc | 31 +++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/lib/ext.cc b/src/lib/ext.cc index feba68c1f..ec34f1472 100644 --- a/src/lib/ext.cc +++ b/src/lib/ext.cc @@ -57,6 +57,7 @@ extern "C" { #include } #include +#include #include @@ -90,6 +91,18 @@ count (boost::filesystem::path dir, uint64_t& total_bytes) } } + +static +void +set_timestamps_to_now (boost::filesystem::path path) +{ + auto const now = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + ext4_mtime_set (path.generic_string().c_str(), now); + ext4_ctime_set (path.generic_string().c_str(), now); + ext4_atime_set (path.generic_string().c_str(), now); +} + + static string write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_remaining, uint64_t total, Nanomsg* nanomsg) @@ -151,6 +164,8 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total ext4_fclose (&out); delete[] buffer; + set_timestamps_to_now (to); + return digester.get (); } @@ -231,6 +246,7 @@ copy (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_ if (r != EOK) { throw CopyError (String::compose("Failed to create directory %1", cr.generic_string()), r); } + set_timestamps_to_now (cr); for (directory_iterator i = directory_iterator(from); i != directory_iterator(); ++i) { copy (i->path(), cr, total_remaining, total, copied_files, nanomsg); diff --git a/test/disk_writer_test.cc b/test/disk_writer_test.cc index 217032d75..60d7fe22a 100644 --- a/test/disk_writer_test.cc +++ b/test/disk_writer_test.cc @@ -53,14 +53,13 @@ create_empty (boost::filesystem::path file, int size) vector -ext2_ls (string path) +ext2_ls (vector arguments) { using namespace boost::process; boost::asio::io_service ios; future data; - /* e2ls is from 'e2tools */ - child ch (search_path("e2ls"), path, std_in.close(), std_out > data, ios); + child ch (search_path("e2ls"), arguments, std_in.close(), std_out > data, ios); ios.run(); auto output = data.get(); @@ -71,9 +70,11 @@ ext2_ls (string path) } -/** Use the writer code to make a disk and partition and copy a file to it, then check - * that the partition has inode size 128 and that the file can be copied back off using - * e2tools. +/** Use the writer code to make a disk and partition and copy a file (in a directory) + * to it, then check that: + * - the partition has inode size 128 + * - the file and directory have reasonable timestamps + * - the file can be copied back off the disk */ BOOST_AUTO_TEST_CASE (disk_writer_test1) { @@ -116,8 +117,22 @@ BOOST_AUTO_TEST_CASE (disk_writer_test1) BOOST_CHECK_EQUAL (matches[1].str(), "128"); } - BOOST_CHECK (ext2_ls(partition.string()) == vector({"disk_writer_test1", "lost+found"})); - BOOST_CHECK (ext2_ls(partition.string() + ":disk_writer_test1") == vector({"foo"})); + BOOST_CHECK (ext2_ls({partition.string()}) == vector({"disk_writer_test1", "lost+found"})); + + string const unset_date = "1-Jan-1970"; + + /* Check timestamp of the directory has been set */ + auto details = ext2_ls({"-l", partition.string()}); + BOOST_REQUIRE (details.size() >= 6); + BOOST_CHECK (details[5] != unset_date); + + auto const dir = partition.string() + ":disk_writer_test1"; + BOOST_CHECK (ext2_ls({dir}) == vector({"foo"})); + + /* Check timestamp of foo */ + details = ext2_ls({"-l", dir}); + BOOST_REQUIRE (details.size() >= 6); + BOOST_CHECK (details[5] != unset_date); system ("e2cp " + partition.string() + ":disk_writer_test1/foo build/test/disk_writer_test1_foo_back"); check_file ("build/test/disk_writer_test1/foo", "build/test/disk_writer_test1_foo_back"); -- 2.30.2