|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "crashreporter.h" |
|
6 |
|
7 #include <dirent.h> |
|
8 #include <sys/stat.h> |
|
9 #include <errno.h> |
|
10 #include <algorithm> |
|
11 |
|
12 using namespace CrashReporter; |
|
13 using std::string; |
|
14 using std::vector; |
|
15 using std::sort; |
|
16 |
|
17 struct FileData |
|
18 { |
|
19 time_t timestamp; |
|
20 string path; |
|
21 }; |
|
22 |
|
23 static bool CompareFDTime(const FileData& fd1, const FileData& fd2) |
|
24 { |
|
25 return fd1.timestamp > fd2.timestamp; |
|
26 } |
|
27 |
|
28 void UIPruneSavedDumps(const std::string& directory) |
|
29 { |
|
30 DIR *dirfd = opendir(directory.c_str()); |
|
31 if (!dirfd) |
|
32 return; |
|
33 |
|
34 vector<FileData> dumpfiles; |
|
35 |
|
36 while (dirent *dir = readdir(dirfd)) { |
|
37 FileData fd; |
|
38 fd.path = directory + '/' + dir->d_name; |
|
39 if (fd.path.size() < 5) |
|
40 continue; |
|
41 |
|
42 if (fd.path.compare(fd.path.size() - 4, 4, ".dmp") != 0) |
|
43 continue; |
|
44 |
|
45 struct stat st; |
|
46 if (stat(fd.path.c_str(), &st)) { |
|
47 closedir(dirfd); |
|
48 return; |
|
49 } |
|
50 |
|
51 fd.timestamp = st.st_mtime; |
|
52 |
|
53 dumpfiles.push_back(fd); |
|
54 } |
|
55 |
|
56 sort(dumpfiles.begin(), dumpfiles.end(), CompareFDTime); |
|
57 |
|
58 while (dumpfiles.size() > kSaveCount) { |
|
59 // get the path of the oldest file |
|
60 string path = dumpfiles[dumpfiles.size() - 1].path; |
|
61 UIDeleteFile(path.c_str()); |
|
62 |
|
63 // s/.dmp/.extra/ |
|
64 path.replace(path.size() - 4, 4, ".extra"); |
|
65 UIDeleteFile(path.c_str()); |
|
66 |
|
67 dumpfiles.pop_back(); |
|
68 } |
|
69 } |