UserIDResolver.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #ifndef LLDB_UTILITY_USERIDRESOLVER_H
  9. #define LLDB_UTILITY_USERIDRESOLVER_H
  10. #include "llvm/ADT/DenseMap.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include <mutex>
  13. namespace lldb_private {
  14. /// An abstract interface for things that know how to map numeric user/group IDs
  15. /// into names. It caches the resolved names to avoid repeating expensive
  16. /// queries. The cache is internally protected by a mutex, so concurrent queries
  17. /// are safe.
  18. class UserIDResolver {
  19. public:
  20. typedef uint32_t id_t;
  21. virtual ~UserIDResolver(); // anchor
  22. llvm::Optional<llvm::StringRef> GetUserName(id_t uid) {
  23. return Get(uid, m_uid_cache, &UserIDResolver::DoGetUserName);
  24. }
  25. llvm::Optional<llvm::StringRef> GetGroupName(id_t gid) {
  26. return Get(gid, m_gid_cache, &UserIDResolver::DoGetGroupName);
  27. }
  28. /// Returns a resolver which returns a failure value for each query. Useful as
  29. /// a fallback value for the case when we know all lookups will fail.
  30. static UserIDResolver &GetNoopResolver();
  31. protected:
  32. virtual llvm::Optional<std::string> DoGetUserName(id_t uid) = 0;
  33. virtual llvm::Optional<std::string> DoGetGroupName(id_t gid) = 0;
  34. private:
  35. using Map = llvm::DenseMap<id_t, llvm::Optional<std::string>>;
  36. llvm::Optional<llvm::StringRef>
  37. Get(id_t id, Map &cache,
  38. llvm::Optional<std::string> (UserIDResolver::*do_get)(id_t));
  39. std::mutex m_mutex;
  40. Map m_uid_cache;
  41. Map m_gid_cache;
  42. };
  43. } // namespace lldb_private
  44. #endif // LLDB_UTILITY_USERIDRESOLVER_H