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__FILEMANAGER_H
17 #define CORE__IO__FILEMANAGER_H
18 
19 #include <cstdint>
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_manager.h>
29 #include <core/io/intf_file_system.h>
30 #include <core/namespace.h>
31 
32 CORE_BEGIN_NAMESPACE()
33 class ProxyFilesystem;
34 
35 class FileManager final : public IFileManager {
36 public:
37     const IInterface* GetInterface(const BASE_NS::Uid& uid) const override;
38     IInterface* GetInterface(const BASE_NS::Uid& uid) override;
39     void Ref() override;
40     void Unref() override;
41 
42     FileManager();
43     ~FileManager() override = default;
44 
45     IDirectory::Entry GetEntry(BASE_NS::string_view uri) override;
46 
47     IFile::Ptr OpenFile(BASE_NS::string_view uri) override;
48     IFile::Ptr CreateFile(BASE_NS::string_view uri) override;
49 
50     bool DeleteFile(BASE_NS::string_view uri) override;
51 
52     IDirectory::Ptr OpenDirectory(BASE_NS::string_view uri) override;
53     IDirectory::Ptr CreateDirectory(BASE_NS::string_view uri) override;
54     bool DeleteDirectory(BASE_NS::string_view uri) override;
55 
56     bool Rename(BASE_NS::string_view fromUri, BASE_NS::string_view toUri) override;
57 
58     void RegisterFilesystem(BASE_NS::string_view protocol, IFilesystem::Ptr filesystem) override;
59     void UnregisterFilesystem(BASE_NS::string_view protocol) override;
60 
61     void RegisterAssetPath(BASE_NS::string_view uri) override;
62     void UnregisterAssetPath(BASE_NS::string_view uri) override;
63 
64     BASE_NS::vector<BASE_NS::string> GetAbsolutePaths(BASE_NS::string_view uri) const;
65 
66     bool RegisterPath(BASE_NS::string_view protocol, BASE_NS::string_view uri, bool prepend) override;
67     void UnregisterPath(BASE_NS::string_view protocol, BASE_NS::string_view uri) override;
68 
69     virtual IFilesystem::Ptr CreateROFilesystem(const void* const data, uint64_t size) override;
70 
71 private:
72     // NOTE: Johannes Pystynen 2019/10/25, Faster access when protocol is known.
73     IFilesystem* GetFilesystem(BASE_NS::string_view protocol) const;
74 
75     // Fix "invalid" uris (converts relative file:// -> absolute. does not affect other protocols.)
76     BASE_NS::string FixPath(BASE_NS::string_view pathIn) const;
77 
78     // Current working directory for the "file://" protocol, used to resolve the absolute path.
79     BASE_NS::string basePath_;
80 
81     BASE_NS::unordered_map<BASE_NS::string, IFilesystem::Ptr> filesystems_;
82 
83     BASE_NS::unordered_map<BASE_NS::string, ProxyFilesystem*> proxyFilesystems_;
84 
85     uint32_t refCount_ { 0 };
86 };
87 CORE_END_NAMESPACE()
88 
89 #endif // CORE__IO__FILEMANAGER_H
90