1 //===-- UserIDResolver.h ----------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLDB_UTILITY_USERIDRESOLVER_H 10 #define LLDB_UTILITY_USERIDRESOLVER_H 11 12 #include "llvm/ADT/DenseMap.h" 13 #include "llvm/ADT/StringRef.h" 14 #include <mutex> 15 #include <optional> 16 17 namespace lldb_private { 18 19 /// An abstract interface for things that know how to map numeric user/group IDs 20 /// into names. It caches the resolved names to avoid repeating expensive 21 /// queries. The cache is internally protected by a mutex, so concurrent queries 22 /// are safe. 23 class UserIDResolver { 24 public: 25 typedef uint32_t id_t; 26 virtual ~UserIDResolver(); // anchor 27 GetUserName(id_t uid)28 std::optional<llvm::StringRef> GetUserName(id_t uid) { 29 return Get(uid, m_uid_cache, &UserIDResolver::DoGetUserName); 30 } GetGroupName(id_t gid)31 std::optional<llvm::StringRef> GetGroupName(id_t gid) { 32 return Get(gid, m_gid_cache, &UserIDResolver::DoGetGroupName); 33 } 34 35 /// Returns a resolver which returns a failure value for each query. Useful as 36 /// a fallback value for the case when we know all lookups will fail. 37 static UserIDResolver &GetNoopResolver(); 38 39 protected: 40 virtual std::optional<std::string> DoGetUserName(id_t uid) = 0; 41 virtual std::optional<std::string> DoGetGroupName(id_t gid) = 0; 42 43 private: 44 using Map = llvm::DenseMap<id_t, std::optional<std::string>>; 45 46 std::optional<llvm::StringRef> 47 Get(id_t id, Map &cache, 48 std::optional<std::string> (UserIDResolver::*do_get)(id_t)); 49 50 std::mutex m_mutex; 51 Map m_uid_cache; 52 Map m_gid_cache; 53 }; 54 55 } // namespace lldb_private 56 57 #endif // LLDB_UTILITY_USERIDRESOLVER_H 58