1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://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,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef CORE__IO__MEMORY_FILESYSTEM_H
17 #define CORE__IO__MEMORY_FILESYSTEM_H
18 
19 #include <memory>
20 
21 #include <base/containers/string.h>
22 #include <base/containers/string_view.h>
23 #include <base/containers/unordered_map.h>
24 #include <base/containers/vector.h>
25 #include <base/namespace.h>
26 #include <core/io/intf_directory.h>
27 #include <core/io/intf_file.h>
28 #include <core/io/intf_file_system.h>
29 #include <core/namespace.h>
30 
31 CORE_BEGIN_NAMESPACE()
32 class MemoryFileStorage;
33 
34 /** Memory protocol.
35  * Protocol implementation that wraps a buffer in memory.
36  */
37 class MemoryFilesystem final : public IFilesystem {
38 public:
39     MemoryFilesystem() = default;
40     ~MemoryFilesystem() override = default;
41     MemoryFilesystem(MemoryFilesystem const&) = delete;
42     MemoryFilesystem& operator=(MemoryFilesystem const&) = delete;
43 
44     IDirectory::Entry GetEntry(BASE_NS::string_view path) override;
45     IFile::Ptr OpenFile(BASE_NS::string_view path) override;
46     IFile::Ptr CreateFile(BASE_NS::string_view path) override;
47     bool DeleteFile(BASE_NS::string_view path) override;
48 
49     IDirectory::Ptr OpenDirectory(BASE_NS::string_view path) override;
50     IDirectory::Ptr CreateDirectory(BASE_NS::string_view path) override;
51     bool DeleteDirectory(BASE_NS::string_view path) override;
52 
53     bool Rename(BASE_NS::string_view fromPath, BASE_NS::string_view toPath) override;
54 
55     BASE_NS::vector<BASE_NS::string> GetUriPaths(BASE_NS::string_view uri) const override;
56 
57 protected:
Destroy()58     void Destroy() override
59     {
60         delete this;
61     }
62 
63 private:
64     BASE_NS::unordered_map<BASE_NS::string, std::weak_ptr<MemoryFileStorage>> memoryFiles_;
65 };
66 CORE_END_NAMESPACE()
67 
68 #endif // CORE__IO__MEMORY_FILESYSTEM_H
69