• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/vector.h>
23 #include <base/namespace.h>
24 #include <core/io/intf_directory.h>
25 #include <core/io/intf_file.h>
26 #include <core/namespace.h>
27 
28 #include "memory_file.h"
29 
30 CORE_BEGIN_NAMESPACE()
31 using BASE_NS::string;
32 using BASE_NS::string_view;
33 using BASE_NS::vector;
34 
GetEntry(const string_view path)35 IDirectory::Entry MemoryFilesystem::GetEntry(const string_view path)
36 {
37     if (auto const pos = memoryFiles_.find(path); pos != memoryFiles_.end()) {
38         return { IDirectory::Entry::FILE, string(path), 0 };
39     }
40     return {};
41 }
42 
OpenFile(const string_view path,const IFile::Mode mode)43 IFile::Ptr MemoryFilesystem::OpenFile(const string_view path, const IFile::Mode mode)
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), mode) };
49         }
50     }
51     return {};
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(), IFile::Mode::READ_WRITE) };
58     }
59     auto storage = std::make_shared<MemoryFileStorage>();
60     memoryFiles_[path] = storage;
61 
62     return IFile::Ptr { new MemoryFile(BASE_NS::move(storage), IFile::Mode::READ_WRITE) };
63 }
64 
DeleteFile(const string_view path)65 bool MemoryFilesystem::DeleteFile(const string_view path)
66 {
67     return memoryFiles_.erase(path) != 0u;
68 }
69 
FileExists(const string_view path) const70 bool MemoryFilesystem::FileExists(const string_view path) const
71 {
72     return memoryFiles_.contains(path);
73 }
74 
OpenDirectory(const string_view)75 IDirectory::Ptr MemoryFilesystem::OpenDirectory(const string_view /* path */)
76 {
77     return {};
78 }
79 
CreateDirectory(const string_view)80 IDirectory::Ptr MemoryFilesystem::CreateDirectory(const string_view /* path */)
81 {
82     return {};
83 }
84 
DeleteDirectory(const string_view)85 bool MemoryFilesystem::DeleteDirectory(const string_view /* path */)
86 {
87     return false;
88 }
89 
DirectoryExists(const string_view) const90 bool MemoryFilesystem::DirectoryExists(const string_view /* path */) const
91 {
92     return false;
93 }
94 
Rename(const string_view,const string_view)95 bool MemoryFilesystem::Rename(const string_view /* fromPath */, const string_view /* toPath */)
96 {
97     return false;
98 }
99 
GetUriPaths(const string_view) const100 vector<string> MemoryFilesystem::GetUriPaths(const string_view) const
101 {
102     return {};
103 }
104 CORE_END_NAMESPACE()
105