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__STD_FILESYSTEM_H
17 #define CORE__IO__STD_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 
CORE_BEGIN_NAMESPACE()28 CORE_BEGIN_NAMESPACE()
29 /** File protocol.
30  * Protocol implementation that wraps standard file i/o.
31  */
32 class StdFilesystem final : public IFilesystem {
33 public:
34     // base path must be absolute, start with a "/" and on windows contain the drive letter "c:".
35     // and should end on a "/". must use a "/" as directory separator.
36     explicit StdFilesystem(BASE_NS::string_view basePath);
37     ~StdFilesystem() override = default;
38     StdFilesystem(StdFilesystem const&) = delete;
39     StdFilesystem& operator=(StdFilesystem const&) = delete;
40 
41     // All uris should be absolute, and are considered such. (should start with "/")
42     IDirectory::Entry GetEntry(BASE_NS::string_view uri) override;
43     IFile::Ptr OpenFile(BASE_NS::string_view path) override;
44     IFile::Ptr CreateFile(BASE_NS::string_view path) override;
45     bool DeleteFile(BASE_NS::string_view path) override;
46 
47     IDirectory::Ptr OpenDirectory(BASE_NS::string_view path) override;
48     IDirectory::Ptr CreateDirectory(BASE_NS::string_view path) override;
49     bool DeleteDirectory(BASE_NS::string_view path) override;
50 
51     bool Rename(BASE_NS::string_view fromPath, BASE_NS::string_view toPath) override;
52 
53     BASE_NS::vector<BASE_NS::string> GetUriPaths(BASE_NS::string_view uri) const override;
54 
55 protected:
56     BASE_NS::string ValidatePath(BASE_NS::string_view path) const;
57     void Destroy() override
58     {
59         delete this;
60     }
61     BASE_NS::string basePath_;
62 };
63 CORE_END_NAMESPACE()
64 
65 #endif // CORE__IO__STD_FILESYSTEM_H
66