blob: 792de4a6f4ee9d8ca16d7a63b515ec1649181044 [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;
77 std::vector<std::string> getBlobIds() const override;
78 bool openOrCreateBlob(const std::string& blobId, uint16_t flags) override;
79 bool deleteBlob(const std::string& blobId) override;
80 std::vector<uint8_t> read(uint32_t offset, uint32_t requestedSize) override;
Maksym Sloykoeb274112021-10-27 23:48:32 +000081 std::vector<uint8_t> readBlob(const std::string& blobId) const override;
Kun Yi64dc05c2018-12-19 13:19:03 -080082 bool write(uint32_t offset, const std::vector<uint8_t>& data) override;
83 bool commit() override;
84 bool close() override;
Kun Yi1a25e0d2020-05-11 12:28:53 -070085 bool stat(blobs::BlobMeta* meta) override;
Kun Yi64dc05c2018-12-19 13:19:03 -080086
87 /**
88 * Helper factory method to create a BinaryStore instance
89 * @param baseBlobId: base id for the created instance
Kun Yi2765b642019-01-16 11:11:24 -080090 * @param sysFile: system file object for storing binary
Kun Yi64dc05c2018-12-19 13:19:03 -080091 * @returns unique_ptr to constructed BinaryStore. Caller should take
92 * ownership of the instance.
93 */
94 static std::unique_ptr<BinaryStoreInterface>
95 createFromConfig(const std::string& baseBlobId,
Willy Tu67391d52021-12-07 19:53:49 -080096 std::unique_ptr<SysFile> file,
97 std::optional<uint32_t> maxSize = std::nullopt);
Kun Yi64dc05c2018-12-19 13:19:03 -080098
Maksym Sloykoeb274112021-10-27 23:48:32 +000099 /**
100 * Helper factory method to create a BinaryStore instance
101 * This function should be used with existing stores. It reads
102 * the baseBlobId name from the storage.
103 * @param sysFile: system file object for storing binary
104 * @param readOnly: if true, open the store in read only mode
105 * @returns unique_ptr to constructed BinaryStore.
106 */
107 static std::unique_ptr<BinaryStoreInterface>
Willy Tu67391d52021-12-07 19:53:49 -0800108 createFromFile(std::unique_ptr<SysFile> file, bool readOnly = true,
109 std::optional<uint32_t> maxSize = std::nullopt);
Maksym Sloykoeb274112021-10-27 23:48:32 +0000110
Kun Yi64dc05c2018-12-19 13:19:03 -0800111 private:
Kun Yi97be3af2019-03-05 22:43:41 -0800112 /* Load the serialized data from sysfile if commit state is dirty.
113 * Returns False if encountered error when loading */
114 bool loadSerializedData();
115
Kun Yi64dc05c2018-12-19 13:19:03 -0800116 std::string baseBlobId_;
Kun Yi0a940b92019-01-07 16:33:11 -0800117 binaryblobproto::BinaryBlobBase blob_;
Kun Yi2765b642019-01-16 11:11:24 -0800118 binaryblobproto::BinaryBlob* currentBlob_ = nullptr;
Maksym Sloykoeb274112021-10-27 23:48:32 +0000119 /* True if current blob is writable */
Kun Yi6baa7132019-01-08 21:21:02 -0800120 bool writable_ = false;
Maksym Sloykoeb274112021-10-27 23:48:32 +0000121 /* True if the entire store (not just individual blobs) is read only */
122 bool readOnly_ = false;
Kun Yi2765b642019-01-16 11:11:24 -0800123 std::unique_ptr<SysFile> file_ = nullptr;
Kun Yid297c9f2019-01-09 13:52:30 -0800124 CommitState commitState_ = CommitState::Dirty;
Willy Tu67391d52021-12-07 19:53:49 -0800125 std::optional<uint32_t> maxSize;
Kun Yi64dc05c2018-12-19 13:19:03 -0800126};
127
Kun Yi68c81142018-12-18 11:17:14 -0800128} // namespace binstore