xref: /openbmc/hiomapd/vpnor/test/tmpd.hpp (revision 26558dbb00e439fb6ea18566361c0671ea6133f5)
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright (C) 2018 IBM Corp. */
3 
4 #include <assert.h>
5 #include <string.h>
6 #include <vector>
7 #include <fstream>
8 #include <experimental/filesystem>
9 
10 #include "config.h"
11 #include "mboxd.h"
12 #include "vpnor/pnor_partition_table.hpp"
13 
14 namespace openpower
15 {
16 namespace virtual_pnor
17 {
18 namespace test
19 {
20 
21 namespace fs = std::experimental::filesystem;
22 
23 class VpnorRoot
24 {
25   public:
26     template <std::size_t N>
27     VpnorRoot(struct mbox_context* ctx, const std::string (&toc)[N],
28               size_t blockSize)
29     {
30         char tmplt[] = "/tmp/vpnor_root.XXXXXX";
31         char* tmpdir = mkdtemp(tmplt);
32         root = fs::path{tmpdir};
33 
34         for (const auto& attr : attributes)
35         {
36             fs::create_directory(root / attr);
37         }
38 
39         fs::path tocFilePath = root / "ro" / PARTITION_TOC_FILE;
40 
41         for (const std::string& line : toc)
42         {
43             pnor_partition part;
44 
45             openpower::virtual_pnor::parseTocLine(line, blockSize, part);
46 
47             /* Populate the partition in the tree */
48             std::vector<char> zeroed(part.data.actual, 0);
49             fs::path partitionFilePath = root / "ro" / part.data.name;
50             std::ofstream(partitionFilePath)
51                 .write(zeroed.data(), zeroed.size());
52 
53             /* Update the ToC if the partition file was created */
54             std::ofstream(tocFilePath, std::ofstream::app) << line << "\n";
55         }
56 
57         strncpy(ctx->paths.ro_loc, ro().c_str(), PATH_MAX - 1);
58         ctx->paths.ro_loc[PATH_MAX - 1] = '\0';
59         strncpy(ctx->paths.rw_loc, rw().c_str(), PATH_MAX - 1);
60         ctx->paths.rw_loc[PATH_MAX - 1] = '\0';
61         strncpy(ctx->paths.prsv_loc, prsv().c_str(), PATH_MAX - 1);
62         ctx->paths.prsv_loc[PATH_MAX - 1] = '\0';
63         strncpy(ctx->paths.patch_loc, patch().c_str(), PATH_MAX - 1);
64         ctx->paths.patch_loc[PATH_MAX - 1] = '\0';
65     }
66 
67     VpnorRoot(const VpnorRoot&) = delete;
68     VpnorRoot& operator=(const VpnorRoot&) = delete;
69     VpnorRoot(VpnorRoot&&) = delete;
70     VpnorRoot& operator=(VpnorRoot&&) = delete;
71 
72     ~VpnorRoot()
73     {
74         fs::remove_all(root);
75     }
76     fs::path ro()
77     {
78         return fs::path{root} / "ro";
79     }
80     fs::path rw()
81     {
82         return fs::path{root} / "rw";
83     }
84     fs::path prsv()
85     {
86         return fs::path{root} / "prsv";
87     }
88     fs::path patch()
89     {
90         return fs::path{root} / "patch";
91     }
92     size_t write(const std::string& name, const void* data, size_t len);
93     size_t patch(const std::string& name, const void* data, size_t len);
94 
95   private:
96     fs::path root;
97     const std::string attributes[4] = {"ro", "rw", "prsv", "patch"};
98 };
99 
100 } // namespace test
101 } // namespace virtual_pnor
102 } // namespace openpower
103