Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "mboxd_pnor_partition_table.h" |
| 4 | #include <fcntl.h> |
| 5 | #include <string> |
| 6 | #include <unistd.h> |
| 7 | #include <experimental/filesystem> |
| 8 | |
| 9 | namespace openpower |
| 10 | { |
| 11 | namespace file |
| 12 | { |
| 13 | |
| 14 | class Descriptor |
| 15 | { |
| 16 | private: |
| 17 | /** default value */ |
| 18 | int fd = -1; |
| 19 | |
| 20 | public: |
| 21 | Descriptor() = default; |
| 22 | Descriptor(const Descriptor&) = delete; |
| 23 | Descriptor& operator=(const Descriptor&) = delete; |
| 24 | Descriptor(Descriptor&&) = delete; |
| 25 | Descriptor& operator=(Descriptor &&) = delete; |
| 26 | |
| 27 | Descriptor(int fd) : fd(fd) {} |
| 28 | |
| 29 | ~Descriptor() |
| 30 | { |
| 31 | if (fd >= 0) |
| 32 | { |
| 33 | close(fd); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | int operator()() const |
| 38 | { |
| 39 | return fd; |
| 40 | } |
| 41 | |
| 42 | void set(int descriptor) |
| 43 | { |
| 44 | fd = descriptor; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | }// namespace file |
| 49 | |
| 50 | namespace virtual_pnor |
| 51 | { |
| 52 | |
| 53 | namespace fs = std::experimental::filesystem; |
| 54 | |
| 55 | enum class ReturnCode : uint8_t |
| 56 | { |
| 57 | FILE_NOT_FOUND = 0, |
| 58 | PARTITION_NOT_FOUND = 1, |
| 59 | PARTITION_READ_ONLY = 2, |
| 60 | FILE_OPEN_FAILURE = 3, |
| 61 | SUCCESS = 4, |
| 62 | }; |
| 63 | |
| 64 | class Request |
| 65 | { |
| 66 | public: |
| 67 | |
| 68 | Request() = default; |
| 69 | Request(const Request&) = delete; |
| 70 | Request& operator=(const Request&) = delete; |
| 71 | Request(Request&&) = default; |
| 72 | Request& operator=(Request&&) = default; |
| 73 | ~Request() = default; |
| 74 | |
| 75 | openpower::file::Descriptor fd; |
| 76 | |
| 77 | protected: |
| 78 | /** @brief opens the partition file |
| 79 | * |
| 80 | * @param[in] filePath - Absolute file path. |
| 81 | * @param[in] mode - File open mode. |
| 82 | */ |
| 83 | ReturnCode open(const std::string& filePath, int mode); |
| 84 | |
| 85 | /** @brief returns the partition file path associated with the offset. |
| 86 | * |
| 87 | * @param[in] context - The mbox context pointer. |
| 88 | * @param[in] offset - The pnor offset(bytes). |
| 89 | */ |
| 90 | |
| 91 | std::string getPartitionFilePath(struct mbox_context* context, |
| 92 | uint32_t offset); |
| 93 | |
| 94 | const pnor_partition* partition = nullptr; |
| 95 | }; |
| 96 | |
| 97 | /** @class RORequest |
| 98 | * @brief Represent the read request of the partition. |
| 99 | * Stores the partition meta data. |
| 100 | */ |
| 101 | class RORequest : public Request |
| 102 | { |
| 103 | public: |
| 104 | RORequest() = default; |
| 105 | RORequest(const RORequest&) = delete; |
| 106 | RORequest& operator=(const RORequest&) = delete; |
| 107 | RORequest(RORequest&&) = default; |
| 108 | RORequest& operator=(RORequest&&) = default; |
| 109 | ~RORequest(){}; |
| 110 | |
| 111 | /** @brief opens the partition file associated with the offset |
| 112 | * in read only mode and gets the partition details. |
| 113 | * |
| 114 | * 1. Depending on the partition type,tries to open the file |
| 115 | * from the associated partition(RW/PRSV/RO). |
| 116 | * 1a. if file not found in the corresponding |
| 117 | * partition(RW/PRSV/RO) then tries to read the file from |
| 118 | * the read only partition. |
| 119 | * 1b. if the file not found in the read only partition then |
| 120 | * throw exception. |
| 121 | * |
| 122 | * @param[in] context - The mbox context pointer. |
| 123 | * @param[in] offset - The pnor offset(bytes). |
| 124 | */ |
| 125 | const pnor_partition* getPartitionInfo(struct mbox_context* context, |
| 126 | uint32_t offset); |
| 127 | }; |
| 128 | |
| 129 | /** @class RWRequest |
| 130 | * @brief Represent the write request of the partition. |
| 131 | * Stores the partition meta data. |
| 132 | */ |
| 133 | class RWRequest : public Request |
| 134 | { |
| 135 | public: |
| 136 | |
| 137 | RWRequest() = default; |
| 138 | RWRequest(const RWRequest&) = delete; |
| 139 | RWRequest& operator=(const RWRequest&) = delete; |
| 140 | RWRequest(RWRequest&&) = default; |
| 141 | RWRequest& operator=(RWRequest&&) = default; |
| 142 | ~RWRequest() {}; |
| 143 | |
| 144 | /** @brief opens the partition file associated with the offset |
| 145 | * in write mode and gets the parttition details. |
| 146 | * |
| 147 | * 1. Depending on the partition type tries to open the file |
| 148 | * from the associated partition. |
| 149 | * 1a. if file not found in the corresponding partition(RW/PRSV) |
| 150 | * then copy the file from the read only partition to the (RW/PRSV) |
| 151 | * partition depending on the partition type. |
| 152 | * 1b. if the file not found in the read only partition then throw exception. |
| 153 | * |
| 154 | * @param[in] context - The mbox context pointer. |
| 155 | * @param[in] offset - The pnor offset(bytes). |
| 156 | */ |
| 157 | const pnor_partition* getPartitionInfo(struct mbox_context* context, |
| 158 | uint32_t offset); |
| 159 | }; |
| 160 | |
| 161 | }// namespace virtual_pnor |
| 162 | }// namespace openpower |