2 Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "lib/cross.h"
25 #include <boost/algorithm/string.hpp>
26 #include <boost/asio.hpp>
27 #include <boost/filesystem.hpp>
28 #include <boost/process.hpp>
29 #include <boost/test/unit_test.hpp>
31 #include <sys/types.h>
45 create_empty (boost::filesystem::path file, int size)
47 auto fd = open (file.string().c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
48 BOOST_REQUIRE (fd != -1);
49 auto const r = posix_fallocate (fd, 0, size);
50 BOOST_REQUIRE_EQUAL (r, 0);
58 using namespace boost::process;
60 boost::asio::io_service ios;
62 /* e2ls is from 'e2tools */
63 child ch (search_path("e2ls"), path, std_in.close(), std_out > data, ios);
66 auto output = data.get();
69 boost::split (parts, output, boost::is_any_of("\t "), boost::token_compress_on);
74 /** Use the writer code to make a disk and partition and copy a file to it, then check
75 * that the partition has inode size 128 and that the file can be copied back off using
78 BOOST_AUTO_TEST_CASE (disk_writer_test1)
80 using namespace boost::filesystem;
81 using namespace boost::process;
83 remove_all ("build/test/disk_writer_test1.disk");
84 remove_all ("build/test/disk_writer_test1.partition");
85 remove_all ("build/test/disk_writer_test1");
87 path disk = "build/test/disk_writer_test1.disk";
88 path partition = "build/test/disk_writer_test1.partition";
90 /* lwext4 has a lower limit of correct ext2 partition sizes it can make; 32Mb
91 * does not work here: fsck gives errors about an incorrect free blocks count.
93 create_empty (disk, 256 * 1024 * 1024);
94 create_empty (partition, 256 * 1024 * 1024);
96 path dcp = "build/test/disk_writer_test1";
97 create_directory (dcp);
98 /* Some arbitrary file size here */
99 make_random_file (dcp / "foo", 1024 * 1024 * 32 - 6128);
101 PrivilegeEscalator::test = true;
102 dcpomatic::write (dcp, disk.string(), partition.string(), 0);
104 BOOST_CHECK_EQUAL (system("/sbin/e2fsck -fn build/test/disk_writer_test1.partition"), 0);
107 boost::asio::io_service ios;
109 child ch ("/sbin/tune2fs", args({"-l", partition.string()}), std_in.close(), std_out > data, ios);
112 string output = data.get();
114 std::regex reg("Inode size:\\s*(.*)");
115 BOOST_REQUIRE (std::regex_search(output, matches, reg));
116 BOOST_REQUIRE (matches.size() == 2);
117 BOOST_CHECK_EQUAL (matches[1].str(), "128");
120 BOOST_CHECK (ext2_ls(partition.string()) == vector<string>({"disk_writer_test1", "lost+found"}));
121 BOOST_CHECK (ext2_ls(partition.string() + ":disk_writer_test1") == vector<string>({"foo"}));
123 system ("e2cp " + partition.string() + ":disk_writer_test1/foo build/test/disk_writer_test1_foo_back");
124 check_file ("build/test/disk_writer_test1/foo", "build/test/disk_writer_test1_foo_back");