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 API_CORE_IO_IDIRECTORY_H
17 #define API_CORE_IO_IDIRECTORY_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/string.h>
22 #include <base/containers/unique_ptr.h>
23 #include <base/containers/vector.h>
24 #include <base/namespace.h>
25 #include <core/namespace.h>
26 
CORE_BEGIN_NAMESPACE()27 CORE_BEGIN_NAMESPACE()
28 /** @ingroup group_io_idirectory */
29 /** IThe IDirectory interface provides access to directory structures and their contents. */
30 class IDirectory {
31 public:
32     /** Entry, is description of either a file or directory */
33     struct Entry {
34         /** Enumeration for entry type */
35         enum Type {
36             /** Unknown type specification */
37             UNKNOWN,
38             /** Type specification: File */
39             FILE,
40             /** Type specification: Directory */
41             DIRECTORY
42         };
43 
44         /** Type of the entry */
45         Type type;
46         /** Name of the entry */
47         BASE_NS::string name;
48         /** timestamp, 0 if not applicable. */
49         uint64_t timestamp { 0 };
50     };
51 
52     /** Close the directory. */
53     virtual void Close() = 0;
54 
55     /** Get list of file/directory entries in this directory.
56      *  @return List of file / directory entries in this directory.
57      */
58     virtual BASE_NS::vector<Entry> GetEntries() const = 0;
59 
60     struct Deleter {
61         constexpr Deleter() noexcept = default;
62         void operator()(IDirectory* ptr) const
63         {
64             ptr->Destroy();
65         }
66     };
67     using Ptr = BASE_NS::unique_ptr<IDirectory, Deleter>;
68 
69 protected:
70     IDirectory() = default;
71     virtual ~IDirectory() = default;
72     virtual void Destroy() = 0;
73 };
74 CORE_END_NAMESPACE()
75 
76 #endif // API_CORE_IO_IDIRECTORY_H
77