#include "userinfocache.h" #include #include namespace Fm { UserInfoCache* UserInfoCache::globalInstance_ = nullptr; std::mutex UserInfoCache::mutex_; UserInfoCache::UserInfoCache() : QObject() { } const std::shared_ptr& UserInfoCache::userFromId(uid_t uid) { std::lock_guard lock{mutex_}; auto it = users_.find(uid); if(it != users_.end()) return it->second; std::shared_ptr user; auto pw = getpwuid(uid); if(pw) { user = std::make_shared(uid, pw->pw_name, pw->pw_gecos); } return (users_[uid] = user); } const std::shared_ptr& UserInfoCache::groupFromId(gid_t gid) { std::lock_guard lock{mutex_}; auto it = groups_.find(gid); if(it != groups_.end()) return it->second; std::shared_ptr group; auto gr = getgrgid(gid); if(gr) { group = std::make_shared(gid, gr->gr_name); } return (groups_[gid] = group); } // static UserInfoCache* UserInfoCache::globalInstance() { std::lock_guard lock{mutex_}; if(!globalInstance_) globalInstance_ = new UserInfoCache(); return globalInstance_; } } // namespace Fm