blob: a33ddeaa367013ff760c360ce81d16bba14e28f4 [file] [log] [blame]
// Copyright 2022 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "vtpm/backends/cacheable_blob.h"
#include <string>
#include <trunks/tpm_generated.h>
namespace vtpm {
CacheableBlob::CacheableBlob(Blob* blob, WritableBlob* cache)
: blob_(blob), cache_(cache) {
CHECK(blob_);
CHECK(cache_);
}
trunks::TPM_RC CacheableBlob::Get(std::string& blob) {
trunks::TPM_RC rc = cache_->Get(blob);
if (rc) {
return rc;
}
// If it's empty, consider the cache is invalidated.
if (!blob.empty()) {
return trunks::TPM_RC_SUCCESS;
}
rc = blob_->Get(blob);
if (rc) {
return rc;
}
// A failed write is a failure. fort `blob_` could return the different result
// every single time, and `cache_` is supposed to be the single source of
// truth once `blob_` gives a valid output.
return cache_->Write(blob);
}
} // namespace vtpm