browsing: elangley-scraps
7684972a
/* Copyright (C) CJ Affiliate
*
* You may use, distribute and modify this code under the
* terms of the GNU General Public License version 2 or
* later.
*
* You should have received a copy of the license with this
* file. If not, you will find a copy in the "LICENSE" file
* at https://github.com/cjdev/dual-control.
*/
717259b5
#include <memory>
d8adc881
#include <vector>
717259b5
#include "user.h"
d8adc881
#include "sys_unistd.h"
#include "sys_pwd.h"
55259c47
189f062b
namespace
{
0e218820
class user_impl : public user_ifc
{
private:
std::string home_directory_;
std::string user_name_;
8b49c701
uid_t uid_;
0e218820
public:
user_impl (const passwd user_info) :
home_directory_ (std::string (user_info.pw_dir)),
8b49c701
user_name_ (std::string (user_info.pw_name)),
uid_ (user_info.pw_uid) {}
uid_t uid() const override
{
return uid_;
}
af3a4cd4
std::string home_directory() const override
0e218820
{
return home_directory_;
}
bef74c28
};
189f062b
class directory_impl : public directory_ifc
{
private:
unistd unistd_;
pwd pwd_;
public:
directory_impl (unistd &unistd, pwd &pwd) : unistd_ (unistd), pwd_ (pwd) {}
bf756dea
std::vector<user> find_user (const std::string &user_name) const override
189f062b
{
std::vector<char> buffer (unistd_.sysconf (_SC_GETPW_R_SIZE_MAX));
passwd sys_passwd;
passwd *found_passwd (0);
int result = pwd_.getpwnam_r (user_name.c_str(), &sys_passwd,
buffer.data(), buffer.size(), &found_passwd);
std::vector<user> return_value;
if (!result && found_passwd) {
43794015
return_value.push_back (user::delegate (new user_impl (sys_passwd)));
189f062b
}
return return_value;
}
b8155a93
8c62e61c
std::vector<user> get_current_user () const override
{
b8155a93
const char *c_user_name = unistd_.getlogin();
if (c_user_name == nullptr) {
return {};
}
std::string user_name = c_user_name;
auto found_user = find_user (user_name);
if (found_user.empty()) {
return {};
}
return {found_user[0]};
}
189f062b
};
55259c47
}
189f062b
directory directory::create (unistd &unistd, pwd &pwd)
{
return directory (delegate (new directory_impl (unistd, pwd)));
d8adc881
}
0d8b9a17