• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "directory.h"
18 
19 #include <android-base/stringprintf.h>
20 
Directory(const char * path)21 procfsinspector::Directory::Directory(const char* path) {
22     if (path && path[0]) {
23         mPath.assign(path);
24         mDirectory.reset(opendir(path));
25     }
26 }
27 
isEmpty() const28 bool procfsinspector::Directory::Entry::isEmpty() const {
29     return mParent.empty() && mChild.empty();
30 }
31 
Entry(std::string parent,std::string child)32 procfsinspector::Directory::Entry::Entry(std::string parent, std::string child) :
33     mParent(parent), mChild(child) {
34     if (!isEmpty()) {
35         if (mParent.back() != '/') {
36             mParent += '/';
37         }
38     }
39 }
40 
str()41 std::string procfsinspector::Directory::Entry::str() {
42     return mParent + mChild;
43 }
44 
getOwnerUserId()45 uid_t procfsinspector::Directory::Entry::getOwnerUserId() {
46     if (isEmpty()) {
47         return -1;
48     }
49     struct stat buf;
50     // fill in stat info for this entry, or return invalid UID on failure
51     if (stat(str().c_str(), &buf)) {
52         return -1;
53     }
54     return buf.st_uid;
55 }
56 
next(unsigned char type)57 procfsinspector::Directory::Entry procfsinspector::Directory::next(unsigned char type) {
58     if (auto dir = mDirectory.get()) {
59         dirent *entry = readdir(dir);
60         if (entry) {
61             // only return entries of the right type (regular file, directory, ...)
62             // but always return UNKNOWN entries as it is an allowed wildcard entry
63             if (entry->d_type == DT_UNKNOWN ||
64                 type == DT_UNKNOWN ||
65                 entry->d_type == type) {
66                 return Entry(mPath, entry->d_name);
67             }
68         }
69     }
70 
71     return Entry();
72 }
73