1 /*
2  * Copyright (C) 2016 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 #define DEBUG false
17 #include "Log.h"
18 
19 #include "report_directory.h"
20 
21 #include <private/android_filesystem_config.h>
22 #include <utils/String8.h>
23 
24 #include <dirent.h>
25 #include <libgen.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 
30 #include <vector>
31 
32 namespace android {
33 namespace os {
34 namespace incidentd {
35 
stat_mtime_cmp(const std::pair<String8,struct stat> & a,const std::pair<String8,struct stat> & b)36 static bool stat_mtime_cmp(const std::pair<String8, struct stat>& a,
37                            const std::pair<String8, struct stat>& b) {
38     return a.second.st_mtime < b.second.st_mtime;
39 }
40 
clean_directory(const char * directory,off_t maxSize,size_t maxCount)41 void clean_directory(const char* directory, off_t maxSize, size_t maxCount) {
42     DIR* dir;
43     struct dirent* entry;
44     struct stat st;
45 
46     std::vector<std::pair<String8, struct stat>> files;
47 
48     if ((dir = opendir(directory)) == NULL) {
49         ALOGE("Couldn't open incident directory: %s", directory);
50         return;
51     }
52 
53     String8 dirbase(directory);
54     if (directory[dirbase.size() - 1] != '/') dirbase += "/";
55 
56     off_t totalSize = 0;
57     size_t totalCount = 0;
58 
59     // Enumerate, count and add up size
60     while ((entry = readdir(dir)) != NULL) {
61         if (entry->d_name[0] == '.') {
62             continue;
63         }
64         String8 filename = dirbase + entry->d_name;
65         if (stat(filename.string(), &st) != 0) {
66             ALOGE("Unable to stat file %s", filename.string());
67             continue;
68         }
69         if (!S_ISREG(st.st_mode)) {
70             continue;
71         }
72         files.push_back(std::pair<String8, struct stat>(filename, st));
73 
74         totalSize += st.st_size;
75         totalCount++;
76     }
77 
78     closedir(dir);
79 
80     // Count or size is less than max, then we're done.
81     if (totalSize < maxSize && totalCount < maxCount) {
82         return;
83     }
84 
85     // Oldest files first.
86     sort(files.begin(), files.end(), stat_mtime_cmp);
87 
88     // Remove files until we're under our limits.
89     for (std::vector<std::pair<String8, struct stat>>::iterator it = files.begin();
90          it != files.end() && totalSize >= maxSize && totalCount >= maxCount; it++) {
91         remove(it->first.string());
92         totalSize -= it->second.st_size;
93         totalCount--;
94     }
95 }
96 
97 }  // namespace incidentd
98 }  // namespace os
99 }  // namespace android
100