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