xref: /aosp_15_r20/external/pigweed/pw_persistent_ram/public/pw_persistent_ram/flat_file_system_entry.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 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 "pw_file/flat_file_system.h"
18 #include "pw_persistent_ram/persistent_buffer.h"
19 
20 namespace pw::persistent_ram {
21 
22 template <size_t kMaxSizeBytes>
23 class FlatFileSystemPersistentBufferEntry final
24     : public file::FlatFileSystemService::Entry {
25  public:
FlatFileSystemPersistentBufferEntry(std::string_view file_name,file::FlatFileSystemService::Entry::Id file_id,file::FlatFileSystemService::Entry::FilePermissions permissions,PersistentBuffer<kMaxSizeBytes> & persistent_buffer)26   FlatFileSystemPersistentBufferEntry(
27       std::string_view file_name,
28       file::FlatFileSystemService::Entry::Id file_id,
29       file::FlatFileSystemService::Entry::FilePermissions permissions,
30       PersistentBuffer<kMaxSizeBytes>& persistent_buffer)
31       : file_name_(file_name),
32         file_id_(file_id),
33         permissions_(permissions),
34         persistent_buffer_(persistent_buffer) {}
35 
Name(span<char> dest)36   StatusWithSize Name(span<char> dest) final {
37     if (file_name_.empty() || !persistent_buffer_.has_value()) {
38       return StatusWithSize(Status::NotFound(), 0);
39     }
40 
41     size_t bytes_to_copy = std::min(dest.size_bytes(), file_name_.size());
42     std::memcpy(dest.data(), file_name_.data(), bytes_to_copy);
43     if (bytes_to_copy != file_name_.size()) {
44       return StatusWithSize(Status::ResourceExhausted(), bytes_to_copy);
45     }
46 
47     return StatusWithSize(OkStatus(), bytes_to_copy);
48   }
49 
SizeBytes()50   size_t SizeBytes() final { return persistent_buffer_.size(); }
51 
Delete()52   Status Delete() final {
53     persistent_buffer_.clear();
54     return pw::OkStatus();
55   }
56 
Permissions()57   file::FlatFileSystemService::Entry::FilePermissions Permissions()
58       const final {
59     return permissions_;
60   }
61 
FileId()62   file::FlatFileSystemService::Entry::Id FileId() const final {
63     return file_id_;
64   }
65 
66  private:
67   const std::string_view file_name_;
68   const file::FlatFileSystemService::Entry::Id file_id_;
69   const file::FlatFileSystemService::Entry::FilePermissions permissions_;
70   PersistentBuffer<kMaxSizeBytes>& persistent_buffer_;
71 };
72 
73 }  // namespace pw::persistent_ram
74