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 CAR_PROCFS_DIRECTORY
18 #define CAR_PROCFS_DIRECTORY
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 
30 class Directory {
31 public:
32     class Entry {
33     public:
34         Entry(std::string parent = "", std::string child = "");
35 
getChild()36         const std::string& getChild() { return mChild; }
37         std::string str();
38 
39         bool isEmpty() const;
40 
41         operator bool() const {
42             return !isEmpty();
43         }
44 
45         uid_t getOwnerUserId();
46 
47     private:
48         std::string mParent;
49         std::string mChild;
50     };
51 
52     Directory(const char* path);
53 
54     Entry next(unsigned char type = DT_UNKNOWN);
55 
56 private:
57     class Deleter {
58     public:
operator()59         void operator()(DIR* dir) {
60             if (dir) closedir(dir);
61         }
62     };
63     std::string mPath;
64     std::unique_ptr<DIR, Deleter> mDirectory;
65 };
66 
67 }
68 
69 #endif // CAR_PROCFS_DIRECTORY