• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 <base/containers/unordered_map.h>
20 #include <core/io/intf_file_system.h>
21 #include <core/namespace.h>
22 
23 #include "memory_file.h"
24 
CORE_BEGIN_NAMESPACE()25 CORE_BEGIN_NAMESPACE()
26 
27 /** Memory protocol.
28  * Protocol implementation that wraps a buffer in memory.
29  */
30 class MemoryFilesystem final : public IFilesystem {
31 public:
32     MemoryFilesystem() = default;
33     ~MemoryFilesystem() override = default;
34     MemoryFilesystem(MemoryFilesystem const&) = delete;
35     MemoryFilesystem& operator=(MemoryFilesystem const&) = delete;
36 
37     IDirectory::Entry GetEntry(BASE_NS::string_view uri) override;
38     IFile::Ptr OpenFile(BASE_NS::string_view path) override;
39     IFile::Ptr CreateFile(BASE_NS::string_view path) override;
40     bool DeleteFile(BASE_NS::string_view path) override;
41 
42     IDirectory::Ptr OpenDirectory(BASE_NS::string_view path) override;
43     IDirectory::Ptr CreateDirectory(BASE_NS::string_view path) override;
44     bool DeleteDirectory(BASE_NS::string_view path) override;
45 
46     bool Rename(BASE_NS::string_view fromPath, BASE_NS::string_view toPath) override;
47 
48     BASE_NS::vector<BASE_NS::string> GetUriPaths(BASE_NS::string_view uri) const override;
49 
50 protected:
51     void Destroy() override
52     {
53         delete this;
54     }
55 
56 private:
57     BASE_NS::unordered_map<BASE_NS::string, std::weak_ptr<MemoryFileStorage>> memoryFiles_;
58 };
59 CORE_END_NAMESPACE()
60 
61 #endif // CORE__IO__MEMORY_FILESYSTEM_H
62