1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/client/crashreporter_unix_common.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,69 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "crashreporter.h" 1.9 + 1.10 +#include <dirent.h> 1.11 +#include <sys/stat.h> 1.12 +#include <errno.h> 1.13 +#include <algorithm> 1.14 + 1.15 +using namespace CrashReporter; 1.16 +using std::string; 1.17 +using std::vector; 1.18 +using std::sort; 1.19 + 1.20 +struct FileData 1.21 +{ 1.22 + time_t timestamp; 1.23 + string path; 1.24 +}; 1.25 + 1.26 +static bool CompareFDTime(const FileData& fd1, const FileData& fd2) 1.27 +{ 1.28 + return fd1.timestamp > fd2.timestamp; 1.29 +} 1.30 + 1.31 +void UIPruneSavedDumps(const std::string& directory) 1.32 +{ 1.33 + DIR *dirfd = opendir(directory.c_str()); 1.34 + if (!dirfd) 1.35 + return; 1.36 + 1.37 + vector<FileData> dumpfiles; 1.38 + 1.39 + while (dirent *dir = readdir(dirfd)) { 1.40 + FileData fd; 1.41 + fd.path = directory + '/' + dir->d_name; 1.42 + if (fd.path.size() < 5) 1.43 + continue; 1.44 + 1.45 + if (fd.path.compare(fd.path.size() - 4, 4, ".dmp") != 0) 1.46 + continue; 1.47 + 1.48 + struct stat st; 1.49 + if (stat(fd.path.c_str(), &st)) { 1.50 + closedir(dirfd); 1.51 + return; 1.52 + } 1.53 + 1.54 + fd.timestamp = st.st_mtime; 1.55 + 1.56 + dumpfiles.push_back(fd); 1.57 + } 1.58 + 1.59 + sort(dumpfiles.begin(), dumpfiles.end(), CompareFDTime); 1.60 + 1.61 + while (dumpfiles.size() > kSaveCount) { 1.62 + // get the path of the oldest file 1.63 + string path = dumpfiles[dumpfiles.size() - 1].path; 1.64 + UIDeleteFile(path.c_str()); 1.65 + 1.66 + // s/.dmp/.extra/ 1.67 + path.replace(path.size() - 4, 4, ".extra"); 1.68 + UIDeleteFile(path.c_str()); 1.69 + 1.70 + dumpfiles.pop_back(); 1.71 + } 1.72 +}