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 #include "memory_filesystem.h"
17 
18 #include <memory>
19 
20 #include <base/containers/string.h>
21 #include <base/containers/string_view.h>
22 #include <base/containers/unordered_map.h>
23 #include <base/containers/vector.h>
24 #include <base/namespace.h>
25 #include <core/io/intf_directory.h>
26 #include <core/io/intf_file.h>
27 #include <core/namespace.h>
28 
29 #include "memory_file.h"
30 
31 CORE_BEGIN_NAMESPACE()
32 using BASE_NS::string;
33 using BASE_NS::string_view;
34 using BASE_NS::vector;
35 
GetEntry(const string_view path)36 IDirectory::Entry MemoryFilesystem::GetEntry(const string_view path)
37 {
38     if (auto const pos = memoryFiles_.find(path); pos != memoryFiles_.end()) {
39         return { IDirectory::Entry::FILE, string(path), 0 };
40     }
41     return {};
42 }
OpenFile(const string_view path)43 IFile::Ptr MemoryFilesystem::OpenFile(const string_view path)
44 {
45     if (auto const pos = memoryFiles_.find(path); pos != memoryFiles_.end()) {
46         auto storage = pos->second.lock();
47         if (storage) {
48             return IFile::Ptr { new MemoryFile(BASE_NS::move(storage)) };
49         }
50     }
51     return IFile::Ptr();
52 }
53 
CreateFile(const string_view path)54 IFile::Ptr MemoryFilesystem::CreateFile(const string_view path)
55 {
56     if (auto const pos = memoryFiles_.find(path); pos != memoryFiles_.end()) {
57         return IFile::Ptr { new MemoryFile(pos->second.lock()) };
58     }
59     auto storage = std::make_shared<MemoryFileStorage>();
60     memoryFiles_[path] = storage;
61 
62     return IFile::Ptr { new MemoryFile(BASE_NS::move(storage)) };
63 }
64 
DeleteFile(const string_view path)65 bool MemoryFilesystem::DeleteFile(const string_view path)
66 {
67     return memoryFiles_.erase(path) != 0u;
68 }
69 
OpenDirectory(const string_view)70 IDirectory::Ptr MemoryFilesystem::OpenDirectory(const string_view /* path */)
71 {
72     return IDirectory::Ptr();
73 }
74 
CreateDirectory(const string_view)75 IDirectory::Ptr MemoryFilesystem::CreateDirectory(const string_view /* path */)
76 {
77     return IDirectory::Ptr();
78 }
79 
DeleteDirectory(const string_view)80 bool MemoryFilesystem::DeleteDirectory(const string_view /* path */)
81 {
82     return false;
83 }
84 
Rename(const string_view,const string_view)85 bool MemoryFilesystem::Rename(const string_view /* fromPath */, const string_view /* toPath */)
86 {
87     return false;
88 }
89 
GetUriPaths(const string_view) const90 vector<string> MemoryFilesystem::GetUriPaths(const string_view) const
91 {
92     return {};
93 }
94 CORE_END_NAMESPACE()
95