1 #pragma once
2 
3 #include "libpldm/pldm_types.h"
4 
5 #include <stdint.h>
6 
7 #include <nlohmann/json.hpp>
8 
9 #include <filesystem>
10 #include <vector>
11 
12 namespace pldm
13 {
14 
15 namespace filetable
16 {
17 
18 namespace fs = std::filesystem;
19 using Handle = uint32_t;
20 using Json = nlohmann::json;
21 using Table = std::vector<uint8_t>;
22 
23 /** @struct FileEntry
24  *
25  *  Data structure for storing information regarding the files supported by
26  *  PLDM File I/O. The file handle is used to uniquely identify the file. The
27  *  traits provide information whether the file is Read only, Read/Write and
28  *  preserved across firmware upgrades.
29  */
30 struct FileEntry
31 {
32     Handle handle;       //!< File handle
33     fs::path fsPath;     //!< File path
34     bitfield32_t traits; //!< File traits
35 };
36 
37 /** @class FileTable
38  *
39  *  FileTable class encapsulates the data related to files supported by PLDM
40  *  File I/O and provides interfaces to lookup files information based on the
41  *  file handle and extract the file attribute table. The file attribute table
42  *  comprises of metadata for files. Metadata includes the file handle, file
43  *  name, current file size and file traits.
44  */
45 class FileTable
46 {
47   public:
48     /** @brief The file table is initialised by parsing the config file
49      *         containing information about the files.
50      *
51      * @param[in] fileTableConfigPath - path to the json file containing
52      *                                  information
53      */
54     FileTable(const std::string& fileTableConfigPath);
55     FileTable() = default;
56     ~FileTable() = default;
57     FileTable(const FileTable&) = default;
58     FileTable& operator=(const FileTable&) = default;
59     FileTable(FileTable&&) = default;
60     FileTable& operator=(FileTable&&) = default;
61 
62     /** @brief Get the file attribute table
63      *
64      * @return Table- contents of the file attribute table
65      */
66     Table operator()() const;
67 
68     /** @brief Get the FileEntry at the file handle
69      *
70      * @param[in] handle - file handle
71      *
72      * @return FileEntry - file entry at the handle
73      */
74     FileEntry at(Handle handle) const
75     {
76         return tableEntries.at(handle);
77     }
78 
79     /** @brief Check is file attribute table is empty
80      *
81      * @return bool - true if file attribute table is empty, false otherwise.
82      */
83     bool isEmpty() const
84     {
85         return fileTable.empty();
86     }
87 
88     /** @brief Clear the file table contents
89      *
90      */
91     void clear()
92     {
93         tableEntries.clear();
94         fileTable.clear();
95         padCount = 0;
96         checkSum = 0;
97     }
98 
99   private:
100     /** @brief handle to FileEntry mappings for lookups based on file handle */
101     std::unordered_map<Handle, FileEntry> tableEntries;
102 
103     /** @brief file attribute table including the pad bytes, except the checksum
104      */
105     std::vector<uint8_t> fileTable;
106 
107     /** @brief the pad count of the file attribute table, the number of pad
108      * bytes is between 0 and 3 */
109     uint8_t padCount = 0;
110 
111     /** @brief the checksum of the file attribute table */
112     uint32_t checkSum = 0;
113 };
114 
115 /** @brief Build the file attribute table if not already built using the
116  *         file table config.
117  *
118  *  @param[in] fileTablePath - path of the file table config
119  *
120  *  @return FileTable& - Reference to instance of file table
121  */
122 
123 FileTable& buildFileTable(const std::string& fileTablePath);
124 
125 } // namespace filetable
126 } // namespace pldm
127