summaryrefslogtreecommitdiff
path: root/test/src/mbgl/test/ramdisk.cpp
blob: 063c09954bffe1752a8355b592428f40c10f29c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <mbgl/test/ramdisk.hpp>

#include <stdexcept>

#include <cstring>

namespace mbgl {
namespace test {

#if TEST_HAS_RAMDISK

RamDisk::RamDisk(const size_t size) {
    char executable[64];
    snprintf(executable, sizeof executable, "test/scripts/ramdisk_darwin.sh %lu", size / 512);
    if (!(child = popen(executable, "r+"))) {
        throw std::runtime_error("Could not start child process");
    }
    write("\n");
}

void RamDisk::write(const char* command) {
    const size_t length = strlen(command);
    if (fwrite(command, 1, length, child) != length) {
        throw std::runtime_error("Could not write to child process");
    }
    // Read lines until we get a line with a single NUL byte, which serves as command completion marker.
    char line[256];
    do {
        if (!fgets(line, sizeof line, child)) {
            throw std::runtime_error("Could not read from child process");
        }
    } while (strncmp(line, "\0\n", sizeof line) != 0);
}

void RamDisk::setReadOnly() {
    write("readonly\n");
}

void RamDisk::setReadWrite() {
    write("readwrite\n");
}

void RamDisk::fillDiskExceptFor(size_t remaining) {
    char command[32];
    snprintf(command, sizeof command, "filldisk %lu\n", remaining);
    write(command);
}

void RamDisk::emptyDisk() {
    write("emptydisk\n");
}

RamDisk::~RamDisk() {
    pclose(child);
}

#endif // TEST_HAS_RAMDISK

} // namespace test
} // namespace mbgl