1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  *
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12 
13  * * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations
18  * under the License.
19  */
20 
21 #ifndef CORE__IO__OHOS_FILESYSTEM_H
22 #define CORE__IO__OHOS_FILESYSTEM_H
23 
24 #include <base/containers/string.h>
25 #include <base/containers/string_view.h>
26 #include <base/containers/vector.h>
27 #include <base/containers/unordered_map.h>
28 #include <base/namespace.h>
29 #include <core/io/intf_directory.h>
30 #include <core/io/intf_file.h>
31 #include <core/io/intf_file_system.h>
32 #include <core/namespace.h>
33 
34 #include "ohos_file.h"
35 
36 CORE_BEGIN_NAMESPACE()
37 class FileManager;
38 
39 class OhosFilesystem final : public IFilesystem {
40 public:
41     OhosFilesystem(const BASE_NS::string_view hapPath, const BASE_NS::string_view bundleName,
42         const BASE_NS::string_view moduleName);
43     OhosFilesystem() = delete;
44     OhosFilesystem(OhosFilesystem const&) = delete;
45     OhosFilesystem& operator=(OhosFilesystem const&) = delete;
46     ~OhosFilesystem() override = default;
47 
48     IDirectory::Entry GetEntry(BASE_NS::string_view path) override;
49     IFile::Ptr CreateFile(BASE_NS::string_view path) override;
50     bool DeleteFile(BASE_NS::string_view path) override;
51     IDirectory::Ptr OpenDirectory(BASE_NS::string_view path) override;
52     IDirectory::Ptr CreateDirectory(BASE_NS::string_view path) override;
53     bool DeleteDirectory(BASE_NS::string_view path) override;
54     bool Rename(BASE_NS::string_view fromPath, BASE_NS::string_view toPath) override;
55     BASE_NS::vector<BASE_NS::string> GetUriPaths(BASE_NS::string_view uri) const override;
56     IFile::Ptr OpenFile(BASE_NS::string_view path) override;
57 
58 protected:
59     BASE_NS::string ValidatePath(const BASE_NS::string_view pathIn) const;
Destroy()60     void Destroy() override
61     {
62         delete this;
63     }
64 
65     PlatformHapInfo hapInfo_;
66     BASE_NS::unordered_map<BASE_NS::string, std::weak_ptr<OhosFileStorage>> ohosFiles_;
67     BASE_NS::refcnt_ptr<OhosResMgr> resManager_ = nullptr;
68 };
69 CORE_END_NAMESPACE()
70 #endif
71