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