60d7fe22aeff1f576a7e0caf45323bb0bb923e7c
[dcpomatic.git] / test / disk_writer_test.cc
1 /*
2     Copyright (C) 2020-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "lib/cross.h"
23 #include "lib/ext.h"
24 #include "test.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>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <fcntl.h>
33 #include <iostream>
34 #include <future>
35 #include <regex>
36
37
38 using std::future;
39 using std::string;
40 using std::vector;
41
42
43 static
44 void
45 create_empty (boost::filesystem::path file, int size)
46 {
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);
51         close (fd);
52 }
53
54
55 vector<string>
56 ext2_ls (vector<string> arguments)
57 {
58         using namespace boost::process;
59
60         boost::asio::io_service ios;
61         future<string> data;
62         child ch (search_path("e2ls"), arguments, std_in.close(), std_out > data, ios);
63         ios.run();
64
65         auto output = data.get();
66         boost::trim (output);
67         vector<string> parts;
68         boost::split (parts, output, boost::is_any_of("\t "), boost::token_compress_on);
69         return parts;
70 }
71
72
73 /** Use the writer code to make a disk and partition and copy a file (in a directory)
74  *  to it, then check that:
75  *  - the partition has inode size 128
76  *  - the file and directory have reasonable timestamps
77  *  - the file can be copied back off the disk
78  */
79 BOOST_AUTO_TEST_CASE (disk_writer_test1)
80 {
81         using namespace boost::filesystem;
82         using namespace boost::process;
83
84         remove_all ("build/test/disk_writer_test1.disk");
85         remove_all ("build/test/disk_writer_test1.partition");
86         remove_all ("build/test/disk_writer_test1");
87
88         path disk = "build/test/disk_writer_test1.disk";
89         path partition = "build/test/disk_writer_test1.partition";
90
91         /* lwext4 has a lower limit of correct ext2 partition sizes it can make; 32Mb
92          * does not work here: fsck gives errors about an incorrect free blocks count.
93          */
94         create_empty (disk, 256 * 1024 * 1024);
95         create_empty (partition, 256 * 1024 * 1024);
96
97         path dcp = "build/test/disk_writer_test1";
98         create_directory (dcp);
99         /* Some arbitrary file size here */
100         make_random_file (dcp / "foo", 1024 * 1024 * 32 - 6128);
101
102         dcpomatic::write (dcp, disk.string(), partition.string(), 0);
103
104         BOOST_CHECK_EQUAL (system("/sbin/e2fsck -fn build/test/disk_writer_test1.partition"), 0);
105
106         {
107                 boost::asio::io_service ios;
108                 future<string> data;
109                 child ch ("/sbin/tune2fs", args({"-l", partition.string()}), std_in.close(), std_out > data, ios);
110                 ios.run();
111
112                 string output = data.get();
113                 std::smatch matches;
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");
118         }
119
120         BOOST_CHECK (ext2_ls({partition.string()}) == vector<string>({"disk_writer_test1", "lost+found"}));
121
122         string const unset_date = "1-Jan-1970";
123
124         /* Check timestamp of the directory has been set */
125         auto details = ext2_ls({"-l", partition.string()});
126         BOOST_REQUIRE (details.size() >= 6);
127         BOOST_CHECK (details[5] != unset_date);
128
129         auto const dir = partition.string() + ":disk_writer_test1";
130         BOOST_CHECK (ext2_ls({dir}) == vector<string>({"foo"}));
131
132         /* Check timestamp of foo */
133         details = ext2_ls({"-l", dir});
134         BOOST_REQUIRE (details.size() >= 6);
135         BOOST_CHECK (details[5] != unset_date);
136
137         system ("e2cp " + partition.string() + ":disk_writer_test1/foo build/test/disk_writer_test1_foo_back");
138         check_file ("build/test/disk_writer_test1/foo", "build/test/disk_writer_test1_foo_back");
139 }
140