1import os
2
3import infra.basetest
4
5
6class TestMtools(infra.basetest.BRTest):
7    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
8        """
9        BR2_PACKAGE_MTOOLS=y
10        BR2_TARGET_ROOTFS_CPIO=y
11        # BR2_TARGET_ROOTFS_TAR is not set
12        """
13
14    def test_run(self):
15        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
16        self.emulator.boot(arch="armv5",
17                           kernel="builtin",
18                           options=["-initrd", cpio_file])
19        self.emulator.login()
20
21        dos_img = "dos-fat.img"
22        mtools_opts = f"-i {dos_img}"
23
24        self.assertRunOk("mtools --version")
25
26        # Create an empty image file to hold the FAT partition
27        self.assertRunOk(f"dd if=/dev/zero of={dos_img} bs=1M count=1")
28
29        # Any Mtools command is expected to fail on an unformated
30        # partition.
31        cmd = f"minfo {mtools_opts} ::"
32        _, exit_code = self.emulator.run(cmd)
33        self.assertNotEqual(exit_code, 0)
34
35        # Now, let's format the partition file to FAT
36        self.assertRunOk(f"mformat {mtools_opts} ::")
37
38        # Run some Mtools commands on this empty partition
39        self.assertRunOk(f"minfo {mtools_opts} ::")
40        self.assertRunOk(f"mdir {mtools_opts} ::")
41        self.assertRunOk(f"mlabel {mtools_opts} -N 12345678 ::BUILDROOT")
42
43        # Create a reference file on our Linux filesystem
44        self.assertRunOk("echo 'Hello Buildroot!' > file1.txt")
45
46        # Copy the reference file into the DOS image, then perform
47        # various file manipulations
48        self.assertRunOk(f"mcopy {mtools_opts} file1.txt ::file2.txt")
49        self.assertRunOk(f"mcopy {mtools_opts} ::file2.txt ::file3.txt")
50        self.assertRunOk(f"mdel {mtools_opts} ::file2.txt")
51        self.assertRunOk(f"mren {mtools_opts} ::file3.txt ::file4.txt")
52        self.assertRunOk(f"mmd {mtools_opts} ::dir1")
53        self.assertRunOk(f"mmove {mtools_opts} ::file4.txt ::dir1")
54        self.assertRunOk(f"mdir {mtools_opts} ::dir1")
55        self.assertRunOk(f"mdu {mtools_opts} -a ::")
56
57        # Copy back the file from the DOS image to the Linux
58        # filesystem
59        self.assertRunOk(f"mcopy {mtools_opts} ::dir1/file4.txt file5.txt")
60
61        # We expect this last copied file to have the same content as
62        # the reference one created at the beginning
63        self.assertRunOk("cmp file1.txt file5.txt")
64
65        # Delete a directory tree containing a file
66        self.assertRunOk(f"mdeltree {mtools_opts} ::dir1")
67