//------------------------------------------------------------------------------ //! @file dircleaner.hh //! @author Andreas-Joachim Peters CERN //! @brief class keeping dir contents at a given level //------------------------------------------------------------------------------ /************************************************************************ * EOS - the CERN Disk Storage System * * Copyright (C) 2016 CERN/Switzerland * * * * 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 3 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, see .* ************************************************************************/ #ifndef FUSE_DIRCLEANER_HH_ #define FUSE_DIRCLEANER_HH_ #include #include #include "common/AssistedThread.hh" #include "common/Logging.hh" #include #include #include #include #include #include #include #include class dircleaner { public: typedef struct fileinfo { std::string path; time_t mtime; size_t size; } file_info_t; typedef std::multimap tree_map_t; typedef struct tree_info { tree_info() { totalsize = 0; totalfiles = 0; } tree_map_t treemap; int64_t totalsize; int64_t totalfiles; std::string path; void Print(std::string& out); XrdSysMutex Locker; // thread safe change size, files void change(int64_t size, int64_t files) { eos_static_info("size=%ld files=%ld", size, files); XrdSysMutexHelper mLock(Locker); totalsize += size; totalfiles += files; if (totalsize < 0) { totalsize = 0; } if (totalfiles < 0) { totalfiles = 0; } } // safe reset function void reset() { XrdSysMutexHelper mLock(Locker); totalsize = 0; totalfiles = 0; } // thread safe get size int64_t get_size() { XrdSysMutexHelper mLock(Locker); return totalsize; } // thread safe get files int64_t get_files() { XrdSysMutexHelper mLock(Locker); return totalfiles; } } tree_info_t; dircleaner(const std::string _path = "/tmp/", const std::string _name = "none", int64_t _maxsize = 0, int64_t _maxfiles = 0, float _clean_threshold = 85.0); virtual ~dircleaner(); bool has_suffix(const std::string& str, const std::string& suffix); tree_info_t& get_external_tree() { return externaltreeinfo; } void set_trim_suffix(const std::string& sfx) { trim_suffix = sfx; } int cleanall(std::string matchsuffix = ""); int scanall(std::string matchsuffix = ""); int trim(bool force); void leveler(ThreadAssistant& assistant); private: std::recursive_mutex cleaningMutex; std::string path; int64_t max_files; int64_t max_size; float clean_threshold; tree_info_t treeinfo; tree_info_t externaltreeinfo; AssistedThread tLeveler; std::string trim_suffix; std::string name; }; #endif