1 //
2 // inmemory_filesystem_utils.hpp
3 //
4 // Copyright © 2024 Apple Inc. All rights reserved.
5 //
6 // Please refer to the license found in the LICENSE file in the root directory of the source tree.
7 
8 #pragma once
9 
10 #include "inmemory_filesystem.hpp"
11 
12 namespace inmemoryfs {
13 
14 /// Serializes the item at the specified path and writes it to the stream.
15 ///
16 /// The structure of the `InMemoryFileSystem` is identical to the structure of the filesystem at the
17 /// specified path.
18 ///
19 /// @param fs  The in-memory filesystem.
20 /// @param canonical_path  The path components from the root.
21 /// @param alignment  The alignment of the offset where an item is written to the stream.
22 /// @param ostream   The output stream.
23 /// @param error   On failure, error is populated with the failure reason.
24 /// @retval `true` if the serialized bytes were written to `ostream` otherwise `false`.
25 bool serialize(const InMemoryFileSystem& fs,
26                const std::vector<std::string>& canonical_path,
27                size_t alignment,
28                std::ostream& ostream,
29                std::error_code& error) noexcept;
30 
31 /// Serializes the item at the specified path and writes it to the stream.
32 ///
33 /// The structure of the `InMemoryFileSystem` is identical to the structure of the filesystem at the
34 /// specified path.
35 ///
36 /// @param fs  The in-memory filesystem.
37 /// @param canonical_path  The path components from the root.
38 /// @param alignment  The alignment of the offset where an item is written to the stream.
39 /// @param dst   The destination pointer, the buffer size must be >= the size returned by `get_buffer_size_for_serialization`.
40 /// @param error   On failure, error is populated with the failure reason.
41 /// @retval `true` if the serialized bytes were written to `ostream` otherwise `false`.
42 bool serialize(const InMemoryFileSystem& fs,
43                const std::vector<std::string>& canonical_path,
44                size_t alignment,
45                void *dst,
46                std::error_code& error) noexcept;
47 
48 /// Computes the size of the buffer that would be needed to serialized the item at the specified path.
49 ///
50 /// @param fs  The in-memory filesystem.
51 /// @param canonical_path  The path components from the root.
52 /// @param alignment  The alignment of the offset where an item is written to the stream.
53 /// @retval The size of the buffer that will be needed to write the item at the specified path.
54 size_t get_buffer_size_for_serialization(const InMemoryFileSystem& fs,
55                                          const std::vector<std::string>& canonical_path,
56                                          size_t alignment) noexcept;
57 
58 /// Constructs an `InMemoryFileSystem` instance from the buffer contents.
59 ///
60 /// @param buffer  The memory buffer.
61 /// @retval The constructed `InMemoryFileSystem` or `nullptr` if the deserialization fail
62 std::unique_ptr<InMemoryFileSystem> make_from_buffer(const std::shared_ptr<MemoryBuffer>& buffer) noexcept;
63 
64 
65 } // namespace inmemoryfs
66