User.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_UTIL__USER
18 #define COM_BORA_SOFTWARE__BALAU_UTIL__USER
19 
21 #include <Balau/Resource/File.hpp>
22 
23 #include <pwd.h>
24 
25 namespace Balau::Util {
26 
30 struct User final {
35  static std::string getUserName() {
36  #if BOOST_OS_UNIX
37  const uid_t uid = geteuid();
38  const passwd * pw = getpwuid(uid);
39 
40  if (pw) {
41  return std::string(pw->pw_name);
42  } else {
45  , "Failed to obtain the user name: user entry not found in passwd."
46  );
47  }
48  #elif BOOST_OS_WINDOWS
49  #error "The Windows platform is not yet implemented."
50  #else
51  #error "Platform not implemented."
52  #endif
53  }
54 
61  // Best effort approach.
62  #if BOOST_OS_UNIX
63  const char * home = std::getenv("HOME");
64 
65  if (home != nullptr && !Util::Strings::trim(home).empty()) {
66  return Resource::File(Util::Strings::trim(home));
67  }
68 
69  passwd pwd {};
70  passwd * result;
71  int s;
72  size_t bufferSize = 512;
73  std::vector<char> buffer(bufferSize);
74  bool repeat = false;
75 
76  do {
77  s = getpwuid_r(getuid(), &pwd, buffer.data(), bufferSize, &result);
78 
79  if (result == nullptr) {
80  if (s == 0) {
83  , "Failed to locate the user home directory: user entry not found in passwd."
84  );
85  } else if (errno == ERANGE) {
86  // Give up at 1MB buffer size.
87  if (bufferSize >= 1048576) {
90  , "Failed to locate the user home directory with buffer size of 1MB."
91  );
92  }
93 
94  bufferSize *= 2;
95  buffer.resize(bufferSize, 0);
96  repeat = true;
97  } else {
100  , "Failed to locate the user home directory: getpwnam_r raised an error, errno = " +
101  ::toString(errno)
102  );
103  }
104  }
105  } while (!repeat);
106 
107  if (result == nullptr || result->pw_dir == nullptr) {
110  , ::toString("Failed to locate the user home directory: getpwnam_r raised an error, errno = ", errno)
111  );
112  }
113 
114  return Resource::File(result->pw_dir);
115  #elif BOOST_OS_WINDOWS
116  #error "The Windows platform is not yet implemented."
117 
118  // TODO verify
119  const char * up = std::getenv("USERPROFILE");
120 
121  if (up != nullptr && !Util::Strings::trim(up).empty()) {
122  return Resource::File(up);
123  }
124 
125  const char * hd = std::getenv("HOMEDRIVE");
126  const char * hp = std::getenv("HOMEPATH");
127 
128  if (hd != nullptr && hp != nullptr && !Util::Strings::trim(hd).empty() && !std::string(hp).empty()) {
129  // TODO
130  return Resource::File(h);
131  }
132  #else
133  #error "Platform not implemented."
134  #endif
135  }
136 
138 
139  User() = delete;
140  User(const User &) = delete;
141  User & operator = (const User &) = delete;
142 };
143 
144 } // namespace Balau::Util
145 
146 #endif // COM_BORA_SOFTWARE__BALAU_UTIL__USER
U8String< AllocatorT > toString(const ZipEntryInfo &info)
Print the zip entry info as a UTF-8 string.
Definition: Zip.hpp:113
Utility functions.
A file on the local file system.
#define ThrowBalauException(ExceptionClass,...)
Throw a Balau style exception, with implicit file and line number, and optional stacktrace.
Definition: BalauException.hpp:45
static std::string getUserName()
Get the name of the user running the process.
Definition: User.hpp:35
static Resource::File getHomeDirectory()
Get the home directory of the user running the process.
Definition: User.hpp:60
Utilities for accessing user information.
Definition: User.hpp:30
static std::string_view trim(const std::string_view &input)
Trim whitespace from the beginning and end of the supplied UTF-8 string.
Definition: Strings.hpp:706
Thrown when a resource is not found.
Definition: ResourceExceptions.hpp:48
A file on the local file system.
Definition: File.hpp:35
Balau exceptions for resources.