1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
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  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBS_PROCFS_INSPECTOR_SERVER_DIRECTORY_H_
18 #define LIBS_PROCFS_INSPECTOR_SERVER_DIRECTORY_H_
19 
20 #include <dirent.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #include <memory>
26 #include <string>
27 
28 namespace procfsinspector {
29 class Directory {
30 public:
31     class Entry {
32     public:
33         explicit Entry(std::string parent = "", std::string child = "");
34 
getChild()35         const std::string& getChild() { return mChild; }
36         std::string str();
37 
38         bool isEmpty() const;
39 
40         operator bool() const {
41             return !isEmpty();
42         }
43 
44         uid_t getOwnerUserId();
45 
46     private:
47         std::string mParent;
48         std::string mChild;
49     };
50 
51     explicit Directory(const char* path);
52 
53     Entry next(unsigned char type = DT_UNKNOWN);
54 
55 private:
56     class Deleter {
57     public:
operator()58         void operator()(DIR* dir) {
59             if (dir) closedir(dir);
60         }
61     };
62     std::string mPath;
63     std::unique_ptr<DIR, Deleter> mDirectory;
64 };
65 
66 }  // namespace procfsinspector
67 
68 #endif  // LIBS_PROCFS_INSPECTOR_SERVER_DIRECTORY_H_
69