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__PROXY_FILESYSTEM_H
17#define CORE__IO__PROXY_FILESYSTEM_H
18
19#include <base/containers/string.h>
20#include <base/containers/string_view.h>
21#include <base/containers/vector.h>
22#include <base/namespace.h>
23#include <core/io/intf_directory.h>
24#include <core/io/intf_file.h>
25#include <core/io/intf_file_system.h>
26#include <core/namespace.h>
27
28CORE_BEGIN_NAMESPACE()
29class FileManager;
30
31/** File protocol.
32 * Protocol implementation that uses given protocol as proxy to destination uri, for example app:// to point in to
33 * application working directory.
34 */
35class ProxyFilesystem final : public IFilesystem {
36public:
37    ProxyFilesystem(FileManager& fileManager, BASE_NS::string_view destination);
38
39    ProxyFilesystem() = delete;
40    ProxyFilesystem(ProxyFilesystem const&) = delete;
41    ProxyFilesystem& operator=(ProxyFilesystem const&) = delete;
42
43    ~ProxyFilesystem() override = default;
44
45    void RemoveSearchPath(BASE_NS::string_view destination);
46
47    IDirectory::Entry GetEntry(BASE_NS::string_view path) override;
48    IFile::Ptr OpenFile(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
52    IDirectory::Ptr OpenDirectory(BASE_NS::string_view path) override;
53    IDirectory::Ptr CreateDirectory(BASE_NS::string_view path) override;
54    bool DeleteDirectory(BASE_NS::string_view path) override;
55
56    bool Rename(BASE_NS::string_view fromPath, BASE_NS::string_view toPath) override;
57
58    BASE_NS::vector<BASE_NS::string> GetUriPaths(BASE_NS::string_view uri) const override;
59
60    void AppendSearchPath(BASE_NS::string_view path);
61    void PrependSearchPath(BASE_NS::string_view path);
62
63protected:
64    void Destroy() override
65    {
66        delete this;
67    }
68
69private:
70    FileManager& fileManager_;
71    BASE_NS::vector<BASE_NS::string> destinations_;
72};
73CORE_END_NAMESPACE()
74
75#endif // CORE__IO__PROXY_FILESYSTEM_H
76