blob: c86934ebee9ff6860b8b81b40a35d8366ee6eae2 [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>
Kun Yi68c81142018-12-18 11:17:14 -080011#include <string>
12#include <vector>
13
Kun Yi0a940b92019-01-07 16:33:11 -080014#include "binaryblob.pb.h"
15
Kun Yi68c81142018-12-18 11:17:14 -080016using std::size_t;
17using std::uint16_t;
18using std::uint32_t;
19using std::uint64_t;
20using std::uint8_t;
21
22namespace binstore
23{
24
25/**
Kun Yi64dc05c2018-12-19 13:19:03 -080026 * @class BinaryStore instantiates a concrete implementation of
27 * BinaryStoreInterface. The dependency on file is injected through its
28 * constructor.
29 */
30class BinaryStore : public BinaryStoreInterface
31{
32 public:
Kun Yi1a25e0d2020-05-11 12:28:53 -070033 /* |CommitState| differs slightly with |StateFlags| in blob.hpp,
34 * and thus is defined in the OEM space (bit 8 - 15). User can call stat()
35 * to query the |CommitState| of the blob path. */
36 enum CommitState
37 {
38 Dirty = (1 << 8), // In-memory data might not match persisted data
39 Clean = (1 << 9), // In-memory data matches persisted data
40 Uninitialized = (1 << 10), // Cannot find persisted data
41 CommitError = (1 << 11) // Error happened during committing
42 };
43
Kun Yi2765b642019-01-16 11:11:24 -080044 BinaryStore() = delete;
Patrick Venturee496b2b2020-07-09 13:49:05 -070045 BinaryStore(const std::string& baseBlobId, std::unique_ptr<SysFile> file) :
46 baseBlobId_(baseBlobId), file_(std::move(file))
Kun Yi64dc05c2018-12-19 13:19:03 -080047 {
Kun Yi97be3af2019-03-05 22:43:41 -080048 blob_.set_blob_base_id(baseBlobId_);
Kun Yi64dc05c2018-12-19 13:19:03 -080049 }
50
Kun Yi64dc05c2018-12-19 13:19:03 -080051 ~BinaryStore() = default;
Kun Yi2765b642019-01-16 11:11:24 -080052
Kun Yi64dc05c2018-12-19 13:19:03 -080053 BinaryStore(const BinaryStore&) = delete;
54 BinaryStore& operator=(const BinaryStore&) = delete;
55 BinaryStore(BinaryStore&&) = default;
56 BinaryStore& operator=(BinaryStore&&) = default;
57
58 std::string getBaseBlobId() const override;
59 std::vector<std::string> getBlobIds() const override;
60 bool openOrCreateBlob(const std::string& blobId, uint16_t flags) override;
61 bool deleteBlob(const std::string& blobId) override;
62 std::vector<uint8_t> read(uint32_t offset, uint32_t requestedSize) override;
63 bool write(uint32_t offset, const std::vector<uint8_t>& data) override;
64 bool commit() override;
65 bool close() override;
Kun Yi1a25e0d2020-05-11 12:28:53 -070066 bool stat(blobs::BlobMeta* meta) override;
Kun Yi64dc05c2018-12-19 13:19:03 -080067
68 /**
69 * Helper factory method to create a BinaryStore instance
70 * @param baseBlobId: base id for the created instance
Kun Yi2765b642019-01-16 11:11:24 -080071 * @param sysFile: system file object for storing binary
Kun Yi64dc05c2018-12-19 13:19:03 -080072 * @returns unique_ptr to constructed BinaryStore. Caller should take
73 * ownership of the instance.
74 */
75 static std::unique_ptr<BinaryStoreInterface>
76 createFromConfig(const std::string& baseBlobId,
Patrick Venturee496b2b2020-07-09 13:49:05 -070077 std::unique_ptr<SysFile> file);
Kun Yi64dc05c2018-12-19 13:19:03 -080078
79 private:
Kun Yi97be3af2019-03-05 22:43:41 -080080 /* Load the serialized data from sysfile if commit state is dirty.
81 * Returns False if encountered error when loading */
82 bool loadSerializedData();
83
Kun Yi64dc05c2018-12-19 13:19:03 -080084 std::string baseBlobId_;
Kun Yi0a940b92019-01-07 16:33:11 -080085 binaryblobproto::BinaryBlobBase blob_;
Kun Yi2765b642019-01-16 11:11:24 -080086 binaryblobproto::BinaryBlob* currentBlob_ = nullptr;
Kun Yi6baa7132019-01-08 21:21:02 -080087 bool writable_ = false;
Kun Yi2765b642019-01-16 11:11:24 -080088 std::unique_ptr<SysFile> file_ = nullptr;
Kun Yid297c9f2019-01-09 13:52:30 -080089 CommitState commitState_ = CommitState::Dirty;
Kun Yi64dc05c2018-12-19 13:19:03 -080090};
91
Kun Yi68c81142018-12-18 11:17:14 -080092} // namespace binstore