diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-11-26 12:04:13 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-11-26 12:04:13 +0100 |
commit | 0db8836cb63f60799ac7b4c5222ba9e12ca5659c (patch) | |
tree | da5189263bae7a4ed6de98ee513cd5be4631b59e /lib | |
parent | ec91a4cc4decd1ffd8ba2f3d0139239b0a0dd022 (diff) | |
download | abrt-0db8836cb63f60799ac7b4c5222ba9e12ca5659c.tar.gz abrt-0db8836cb63f60799ac7b4c5222ba9e12ca5659c.tar.xz abrt-0db8836cb63f60799ac7b4c5222ba9e12ca5659c.zip |
hookCCpp: check total dump dir size and delete the largest/oldest one
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Utils/DebugDump.cpp | 47 | ||||
-rw-r--r-- | lib/Utils/DebugDump.h | 8 | ||||
-rw-r--r-- | lib/Utils/Makefile.am | 8 | ||||
-rw-r--r-- | lib/Utils/dirsize.cpp | 100 |
4 files changed, 142 insertions, 21 deletions
diff --git a/lib/Utils/DebugDump.cpp b/lib/Utils/DebugDump.cpp index fff46957..3e226f9a 100644 --- a/lib/Utils/DebugDump.cpp +++ b/lib/Utils/DebugDump.cpp @@ -46,7 +46,19 @@ static std::string RemoveBackSlashes(const char *pDir) return std::string(pDir, len); } -static bool ExistFileDir(const char* pPath); +static bool ExistFileDir(const char *pPath) +{ + struct stat buf; + if (stat(pPath, &buf) == 0) + { + if (S_ISDIR(buf.st_mode) || S_ISREG(buf.st_mode)) + { + return true; + } + } + return false; +} + static void LoadTextFile(const char *pPath, std::string& pData); CDebugDump::CDebugDump() : @@ -60,12 +72,12 @@ void CDebugDump::Open(const char *pDir) { if (m_bOpened) { - throw CABRTException(EXCEP_ERROR, "CDebugDump::CDebugDump(): DebugDump is already opened."); + throw CABRTException(EXCEP_ERROR, "CDebugDump is already opened"); } m_sDebugDumpDir = RemoveBackSlashes(pDir); if (!ExistFileDir(m_sDebugDumpDir.c_str())) { - throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::CDebugDump(): " + m_sDebugDumpDir + " does not exist."); + throw CABRTException(EXCEP_DD_OPEN, m_sDebugDumpDir + " does not exist"); } Lock(); m_bOpened = true; @@ -77,20 +89,6 @@ bool CDebugDump::Exist(const char* pPath) return ExistFileDir(fullPath.c_str()); } - -static bool ExistFileDir(const char* pPath) -{ - struct stat buf; - if (stat(pPath, &buf) == 0) - { - if (S_ISDIR(buf.st_mode) || S_ISREG(buf.st_mode)) - { - return true; - } - } - return false; -} - static bool GetAndSetLock(const char* pLockFile, const char* pPID) { while (symlink(pPID, pLockFile) != 0) @@ -429,3 +427,18 @@ bool CDebugDump::GetNextFile(std::string *short_name, std::string *full_name) m_pGetNextFileDir = NULL; return false; } + +/* Utility function */ +void delete_debug_dump_dir(const char *pDebugDumpDir) +{ + try + { + CDebugDump dd; + dd.Open(pDebugDumpDir); + dd.Delete(); + } + catch (CABRTException& e) + { + /* Ignoring "directory already deleted" and such */ + } +} diff --git a/lib/Utils/DebugDump.h b/lib/Utils/DebugDump.h index d7533534..7dfe61ea 100644 --- a/lib/Utils/DebugDump.h +++ b/lib/Utils/DebugDump.h @@ -76,4 +76,10 @@ class CDebugDump bool GetNextFile(std::string *short_name, std::string *full_name); }; -#endif /*DEBUGDUMP_H_*/ +/** + * Deletes particular debugdump directory. + * @param pDebugDumpDir A debugdump directory. + */ +void delete_debug_dump_dir(const char *pDebugDumpDir); + +#endif diff --git a/lib/Utils/Makefile.am b/lib/Utils/Makefile.am index f0b44810..427e437c 100644 --- a/lib/Utils/Makefile.am +++ b/lib/Utils/Makefile.am @@ -1,5 +1,5 @@ # ABRTUtils has small set of deps. This reduces deps of smaller abrt binaries -# ABRTdUtils has more. It is used by daemon and plugins only +# ABRTdUtils has much more. It is used by daemon and plugins only lib_LTLIBRARIES = libABRTUtils.la libABRTdUtils.la # Not used just yet: @@ -8,7 +8,6 @@ lib_LTLIBRARIES = libABRTUtils.la libABRTdUtils.la # removed: CrashTypesSocket.cpp libABRTUtils_la_SOURCES = \ - stringops.cpp \ xfuncs.cpp \ encbase64.cpp \ read_write.cpp \ @@ -16,6 +15,8 @@ libABRTUtils_la_SOURCES = \ copyfd.cpp \ skip_whitespace.cpp \ popen_and_save_output.cpp \ + stringops.cpp \ + dirsize.cpp \ DebugDump.h DebugDump.cpp libABRTUtils_la_CPPFLAGS = \ -Wall -Werror \ @@ -35,10 +36,11 @@ libABRTUtils_la_LIBADD = libABRTdUtils_la_SOURCES = \ parse_release.cpp \ + make_descr.cpp \ CommLayerInner.h CommLayerInner.cpp \ abrt_dbus.h abrt_dbus.cpp \ abrt_xmlrpc.h abrt_xmlrpc.cpp \ - Plugin.h Plugin.cpp make_descr.cpp \ + Plugin.h Plugin.cpp \ Polkit.h Polkit.cpp \ Action.h Database.h Reporter.h Analyzer.h \ Observer.h \ diff --git a/lib/Utils/dirsize.cpp b/lib/Utils/dirsize.cpp new file mode 100644 index 00000000..739b6b73 --- /dev/null +++ b/lib/Utils/dirsize.cpp @@ -0,0 +1,100 @@ +/* + Copyright (C) 2009 Jiri Moskovcak (jmoskovc@redhat.com) + Copyright (C) 2009 RedHat inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ +#include "abrtlib.h" + +using namespace std; + +double get_dirsize(const char *pPath) +{ + DIR *dp = opendir(pPath); + if (dp == NULL) + return 0; + + struct dirent *ep; + struct stat statbuf; + double size = 0; + while ((ep = readdir(dp)) != NULL) + { + if (dot_or_dotdot(ep->d_name)) + continue; + string dname = concat_path_file(pPath, ep->d_name); + if (lstat(dname.c_str(), &statbuf) != 0) + continue; + if (S_ISDIR(statbuf.st_mode)) + { + size += get_dirsize(dname.c_str()); + } + else if (S_ISREG(statbuf.st_mode)) + { + size += statbuf.st_size; + } + } + closedir(dp); + return size; +} + +double get_dirsize_find_largest_dir( + const char *pPath, + string *worst_dir, + const char *excluded) +{ + DIR *dp = opendir(pPath); + if (dp == NULL) + return 0; + + struct dirent *ep; + struct stat statbuf; + double size = 0; + double maxsz = 0; + while ((ep = readdir(dp)) != NULL) + { + if (dot_or_dotdot(ep->d_name)) + continue; + string dname = concat_path_file(pPath, ep->d_name); + if (lstat(dname.c_str(), &statbuf) != 0) + continue; + if (S_ISDIR(statbuf.st_mode)) + { + double sz = get_dirsize(dname.c_str()); + size += sz; + + if (worst_dir && (!excluded || strcmp(excluded, ep->d_name) != 0)) + { + /* Calculate "weighted" size and age + * w = sz_kbytes * age_mins */ + sz /= 1024; + long age = (time(NULL) - statbuf.st_mtime) / 60; + if (age > 0) + sz *= age; + + if (sz > maxsz) + { + maxsz = sz; + *worst_dir = ep->d_name; + } + } + } + else if (S_ISREG(statbuf.st_mode)) + { + size += statbuf.st_size; + } + } + closedir(dp); + return size; +} |