blob: aa83714eab903d1d85328ac1b0bcf693721ff333 [file] [log] [blame]
Kun Yi68c81142018-12-18 11:17:14 -08001#pragma once
2
Patrick Venture15f0f942020-07-09 09:38:18 -07003#include "binarystore_interface.hpp"
Kun Yi2765b642019-01-16 11:11:24 -08004#include "sys_file.hpp"
5
Kun Yi64dc05c2018-12-19 13:19:03 -08006#include <unistd.h>
7
Kun Yi1a25e0d2020-05-11 12:28:53 -07008#include <blobs-ipmid/blobs.hpp>
Kun Yi68c81142018-12-18 11:17:14 -08009#include <cstdint>
Kun Yi64dc05c2018-12-19 13:19:03 -080010#include <memory>
Willy Tu67391d52021-12-07 19:53:49 -080011#include <optional>
Kun Yi68c81142018-12-18 11:17:14 -080012#include <string>
13#include <vector>
14
Kun Yi0a940b92019-01-07 16:33:11 -080015#include "binaryblob.pb.h"
16
Kun Yi68c81142018-12-18 11:17:14 -080017using std::size_t;
18using std::uint16_t;
19using std::uint32_t;
20using std::uint64_t;
21using std::uint8_t;
22
23namespace binstore
24{
25
26/**
Kun Yi64dc05c2018-12-19 13:19:03 -080027 * @class BinaryStore instantiates a concrete implementation of
28 * BinaryStoreInterface. The dependency on file is injected through its
29 * constructor.
30 */
31class BinaryStore : public BinaryStoreInterface
32{
33 public:
Kun Yi1a25e0d2020-05-11 12:28:53 -070034 /* |CommitState| differs slightly with |StateFlags| in blob.hpp,
35 * and thus is defined in the OEM space (bit 8 - 15). User can call stat()
36 * to query the |CommitState| of the blob path. */
37 enum CommitState
38 {
39 Dirty = (1 << 8), // In-memory data might not match persisted data
40 Clean = (1 << 9), // In-memory data matches persisted data
41 Uninitialized = (1 << 10), // Cannot find persisted data
42 CommitError = (1 << 11) // Error happened during committing
43 };
44
Kun Yi2765b642019-01-16 11:11:24 -080045 BinaryStore() = delete;
Willy Tu67391d52021-12-07 19:53:49 -080046 BinaryStore(const std::string& baseBlobId, std::unique_ptr<SysFile> file,
47 std::optional<uint32_t> maxSize = std::nullopt) :
48 baseBlobId_(baseBlobId),
49 file_(std::move(file)), maxSize(maxSize)
Kun Yi64dc05c2018-12-19 13:19:03 -080050 {
Kun Yi97be3af2019-03-05 22:43:41 -080051 blob_.set_blob_base_id(baseBlobId_);
Willy Tu67391d52021-12-07 19:53:49 -080052 if (maxSize)
53 {
54 blob_.set_max_size_bytes(*maxSize);
55 }
Kun Yi64dc05c2018-12-19 13:19:03 -080056 }
57
Willy Tu67391d52021-12-07 19:53:49 -080058 BinaryStore(std::unique_ptr<SysFile> file, bool readOnly = false,
59 std::optional<uint32_t> maxSize = std::nullopt) :
60 readOnly_{readOnly},
61 file_(std::move(file)), maxSize(maxSize)
Maksym Sloykoeb274112021-10-27 23:48:32 +000062 {
Willy Tu67391d52021-12-07 19:53:49 -080063 if (maxSize)
64 {
65 blob_.set_max_size_bytes(*maxSize);
66 }
Maksym Sloykoeb274112021-10-27 23:48:32 +000067 }
68
Kun Yi64dc05c2018-12-19 13:19:03 -080069 ~BinaryStore() = default;
Kun Yi2765b642019-01-16 11:11:24 -080070
Kun Yi64dc05c2018-12-19 13:19:03 -080071 BinaryStore(const BinaryStore&) = delete;
72 BinaryStore& operator=(const BinaryStore&) = delete;
73 BinaryStore(BinaryStore&&) = default;
74 BinaryStore& operator=(BinaryStore&&) = default;
75
76 std::string getBaseBlobId() const override;
Willy Tu7f107802023-11-06 23:05:25 -080077 bool setBaseBlobId(const std::string& baseBlobId) override;
Kun Yi64dc05c2018-12-19 13:19:03 -080078 std::vector<std::string> getBlobIds() const override;
79 bool openOrCreateBlob(const std::string& blobId, uint16_t flags) override;
80 bool deleteBlob(const std::string& blobId) override;
81 std::vector<uint8_t> read(uint32_t offset, uint32_t requestedSize) override;
Maksym Sloykoeb274112021-10-27 23:48:32 +000082 std::vector<uint8_t> readBlob(const std::string& blobId) const override;
Kun Yi64dc05c2018-12-19 13:19:03 -080083 bool write(uint32_t offset, const std::vector<uint8_t>& data) override;
84 bool commit() override;
85 bool close() override;
Kun Yi1a25e0d2020-05-11 12:28:53 -070086 bool stat(blobs::BlobMeta* meta) override;
Kun Yi64dc05c2018-12-19 13:19:03 -080087
88 /**
89 * Helper factory method to create a BinaryStore instance
90 * @param baseBlobId: base id for the created instance
Kun Yi2765b642019-01-16 11:11:24 -080091 * @param sysFile: system file object for storing binary
Kun Yi64dc05c2018-12-19 13:19:03 -080092 * @returns unique_ptr to constructed BinaryStore. Caller should take
93 * ownership of the instance.
94 */
Willy Tu7f107802023-11-06 23:05:25 -080095 static std::unique_ptr<BinaryStoreInterface> createFromConfig(
96 const std::string& baseBlobId, std::unique_ptr<SysFile> file,
97 std::optional<uint32_t> maxSize = std::nullopt,
98 std::optional<std::string> aliasBlobBaseId = std::nullopt);
Kun Yi64dc05c2018-12-19 13:19:03 -080099
Maksym Sloykoeb274112021-10-27 23:48:32 +0000100 /**
101 * Helper factory method to create a BinaryStore instance
102 * This function should be used with existing stores. It reads
103 * the baseBlobId name from the storage.
104 * @param sysFile: system file object for storing binary
105 * @param readOnly: if true, open the store in read only mode
106 * @returns unique_ptr to constructed BinaryStore.
107 */
108 static std::unique_ptr<BinaryStoreInterface>
Willy Tu67391d52021-12-07 19:53:49 -0800109 createFromFile(std::unique_ptr<SysFile> file, bool readOnly = true,
110 std::optional<uint32_t> maxSize = std::nullopt);
Maksym Sloykoeb274112021-10-27 23:48:32 +0000111
Kun Yi64dc05c2018-12-19 13:19:03 -0800112 private:
Kun Yi97be3af2019-03-05 22:43:41 -0800113 /* Load the serialized data from sysfile if commit state is dirty.
114 * Returns False if encountered error when loading */
Willy Tu7f107802023-11-06 23:05:25 -0800115 bool loadSerializedData(
116 std::optional<std::string> aliasBlobBaseId = std::nullopt);
Kun Yi97be3af2019-03-05 22:43:41 -0800117
Kun Yi64dc05c2018-12-19 13:19:03 -0800118 std::string baseBlobId_;
Kun Yi0a940b92019-01-07 16:33:11 -0800119 binaryblobproto::BinaryBlobBase blob_;
Kun Yi2765b642019-01-16 11:11:24 -0800120 binaryblobproto::BinaryBlob* currentBlob_ = nullptr;
Maksym Sloykoeb274112021-10-27 23:48:32 +0000121 /* True if current blob is writable */
Kun Yi6baa7132019-01-08 21:21:02 -0800122 bool writable_ = false;
Maksym Sloykoeb274112021-10-27 23:48:32 +0000123 /* True if the entire store (not just individual blobs) is read only */
124 bool readOnly_ = false;
Kun Yi2765b642019-01-16 11:11:24 -0800125 std::unique_ptr<SysFile> file_ = nullptr;
Kun Yid297c9f2019-01-09 13:52:30 -0800126 CommitState commitState_ = CommitState::Dirty;
Willy Tu67391d52021-12-07 19:53:49 -0800127 std::optional<uint32_t> maxSize;
Kun Yi64dc05c2018-12-19 13:19:03 -0800128};
129
Kun Yi68c81142018-12-18 11:17:14 -0800130} // namespace binstore