Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:59

0001 //===-- UserIDResolver.h ----------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_UTILITY_USERIDRESOLVER_H
0010 #define LLDB_UTILITY_USERIDRESOLVER_H
0011 
0012 #include "llvm/ADT/DenseMap.h"
0013 #include "llvm/ADT/StringRef.h"
0014 #include <mutex>
0015 #include <optional>
0016 
0017 namespace lldb_private {
0018 
0019 /// An abstract interface for things that know how to map numeric user/group IDs
0020 /// into names. It caches the resolved names to avoid repeating expensive
0021 /// queries. The cache is internally protected by a mutex, so concurrent queries
0022 /// are safe.
0023 class UserIDResolver {
0024 public:
0025   typedef uint32_t id_t;
0026   virtual ~UserIDResolver(); // anchor
0027 
0028   std::optional<llvm::StringRef> GetUserName(id_t uid) {
0029     return Get(uid, m_uid_cache, &UserIDResolver::DoGetUserName);
0030   }
0031   std::optional<llvm::StringRef> GetGroupName(id_t gid) {
0032     return Get(gid, m_gid_cache, &UserIDResolver::DoGetGroupName);
0033   }
0034 
0035   /// Returns a resolver which returns a failure value for each query. Useful as
0036   /// a fallback value for the case when we know all lookups will fail.
0037   static UserIDResolver &GetNoopResolver();
0038 
0039 protected:
0040   virtual std::optional<std::string> DoGetUserName(id_t uid) = 0;
0041   virtual std::optional<std::string> DoGetGroupName(id_t gid) = 0;
0042 
0043 private:
0044   using Map = llvm::DenseMap<id_t, std::optional<std::string>>;
0045 
0046   std::optional<llvm::StringRef>
0047   Get(id_t id, Map &cache,
0048       std::optional<std::string> (UserIDResolver::*do_get)(id_t));
0049 
0050   std::mutex m_mutex;
0051   Map m_uid_cache;
0052   Map m_gid_cache;
0053 };
0054 
0055 } // namespace lldb_private
0056 
0057 #endif // LLDB_UTILITY_USERIDRESOLVER_H