michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "crashreporter.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: using namespace CrashReporter; michael@0: using std::string; michael@0: using std::vector; michael@0: using std::sort; michael@0: michael@0: struct FileData michael@0: { michael@0: time_t timestamp; michael@0: string path; michael@0: }; michael@0: michael@0: static bool CompareFDTime(const FileData& fd1, const FileData& fd2) michael@0: { michael@0: return fd1.timestamp > fd2.timestamp; michael@0: } michael@0: michael@0: void UIPruneSavedDumps(const std::string& directory) michael@0: { michael@0: DIR *dirfd = opendir(directory.c_str()); michael@0: if (!dirfd) michael@0: return; michael@0: michael@0: vector dumpfiles; michael@0: michael@0: while (dirent *dir = readdir(dirfd)) { michael@0: FileData fd; michael@0: fd.path = directory + '/' + dir->d_name; michael@0: if (fd.path.size() < 5) michael@0: continue; michael@0: michael@0: if (fd.path.compare(fd.path.size() - 4, 4, ".dmp") != 0) michael@0: continue; michael@0: michael@0: struct stat st; michael@0: if (stat(fd.path.c_str(), &st)) { michael@0: closedir(dirfd); michael@0: return; michael@0: } michael@0: michael@0: fd.timestamp = st.st_mtime; michael@0: michael@0: dumpfiles.push_back(fd); michael@0: } michael@0: michael@0: sort(dumpfiles.begin(), dumpfiles.end(), CompareFDTime); michael@0: michael@0: while (dumpfiles.size() > kSaveCount) { michael@0: // get the path of the oldest file michael@0: string path = dumpfiles[dumpfiles.size() - 1].path; michael@0: UIDeleteFile(path.c_str()); michael@0: michael@0: // s/.dmp/.extra/ michael@0: path.replace(path.size() - 4, 4, ".extra"); michael@0: UIDeleteFile(path.c_str()); michael@0: michael@0: dumpfiles.pop_back(); michael@0: } michael@0: }