xref: /aosp_15_r20/external/pigweed/pw_blob_store/public/pw_blob_store/flat_file_system_entry.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #pragma once
16 
17 #include <cstddef>
18 
19 #include "pw_blob_store/blob_store.h"
20 #include "pw_file/flat_file_system.h"
21 #include "pw_span/span.h"
22 #include "pw_status/status.h"
23 #include "pw_status/status_with_size.h"
24 #include "pw_sync/lock_annotations.h"
25 #include "pw_sync/virtual_basic_lockable.h"
26 
27 namespace pw::blob_store {
28 
29 class FlatFileSystemBlobStoreEntry final
30     : public file::FlatFileSystemService::Entry {
31  public:
32   using file::FlatFileSystemService::Entry::FilePermissions;
33   using file::FlatFileSystemService::Entry::Id;
34 
35   // File IDs must be globally unique, and map to a pw_transfer TransferService
36   // read/write handler ID.
37   //
38   // TODO(pwbug/492): When BlobStore access is thread-safe, the mutex can be
39   // dropped.
FlatFileSystemBlobStoreEntry(Id file_id,FilePermissions permissions,BlobStore & blob_store,sync::VirtualBasicLockable & blob_store_lock)40   FlatFileSystemBlobStoreEntry(Id file_id,
41                                FilePermissions permissions,
42                                BlobStore& blob_store,
43                                sync::VirtualBasicLockable& blob_store_lock)
44       : file_id_(file_id),
45         permissions_(permissions),
46         initialized_(false),
47         blob_store_(blob_store),
48         blob_store_lock_(blob_store_lock) {}
49 
50   // Initializes the underlying BlobStore. Calling this before use is optional,
51   // as this class will also lazy-init
52   Status Init();
53 
54   StatusWithSize Name(span<char> dest) final;
55 
56   size_t SizeBytes() final;
57 
Permissions()58   FilePermissions Permissions() const final { return permissions_; }
59 
60   Status Delete() final;
61 
FileId()62   Id FileId() const override { return file_id_; }
63 
64  private:
65   // Initializes the BlobStore if uninitialized, and CHECK()s initialization
66   // to ensure it succeeded.
67   void EnsureInitialized();
68 
69   const Id file_id_;
70   const FilePermissions permissions_;
71   bool initialized_ PW_GUARDED_BY(blob_store_lock_);
72   blob_store::BlobStore& blob_store_ PW_GUARDED_BY(blob_store_lock_);
73   sync::VirtualBasicLockable& blob_store_lock_;
74 };
75 
76 }  // namespace pw::blob_store
77