don't use local-file-change context for mapreduce
[hiphop-php.git] / hphp / util / user-info.h
blob3d96af62095123e8f176b1ed7abd4892811085d0
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #pragma once
19 #include <grp.h>
20 #include <pwd.h>
21 #include <unistd.h>
22 #include <vector>
24 #include <folly/String.h>
26 #include "hphp/util/exception.h"
28 namespace HPHP {
30 struct UserInfo final {
31 explicit UserInfo(const char* name) {
32 ::passwd* retpwptr = nullptr;
33 int pwbuflen = ::sysconf(_SC_GETPW_R_SIZE_MAX);
34 if (pwbuflen < 1) {
35 throw Exception("Could not get _SC_GETPW_R_SIZE_MAX");
37 pwbuf.resize(pwbuflen);
39 if (::getpwnam_r(name, &pwd, pwbuf.data(), pwbuf.size(), &retpwptr)) {
40 throw Exception("getpwnam_r: %s", folly::errnoStr(errno).c_str());
43 if (!retpwptr) {
44 throw Exception("getpwnam_r: no such user: %s", name);
48 explicit UserInfo(uid_t uid) {
49 ::passwd* retpwptr = nullptr;
50 int pwbuflen = ::sysconf(_SC_GETPW_R_SIZE_MAX);
51 if (pwbuflen < 1) {
52 throw Exception("Could not get _SC_GETPW_R_SIZE_MAX");
54 pwbuf.resize(pwbuflen);
56 if (::getpwuid_r(uid, &pwd, pwbuf.data(), pwbuf.size(), &retpwptr)) {
57 throw Exception("getpwuid_r: %s", folly::errnoStr(errno).c_str());
60 if (!retpwptr) {
61 throw Exception("getpwuid_r: no such uid: %u", uid);
65 ::passwd pwd;
66 std::vector<char> pwbuf;
69 struct GroupInfo final {
70 explicit GroupInfo(const char* name) {
71 ::group* retgrptr = nullptr;
72 int grbuflen = ::sysconf(_SC_GETGR_R_SIZE_MAX);
73 if (grbuflen < 1) {
74 throw Exception("Could not get _SC_GETGR_R_SIZE_MAX");
76 grbuf.resize(grbuflen);
78 if (::getgrnam_r(name, &gr, grbuf.data(), grbuf.size(), &retgrptr)) {
79 throw Exception("getgrnam_r: %s", folly::errnoStr(errno).c_str());
82 if (!retgrptr) {
83 throw Exception("getgrnam_r: no such group: %s", name);
87 ::group gr;
88 std::vector<char> grbuf;
91 } // namespace HPHP