Merge "extracted helper functions to a separate header" into tizen
authorAdrian Szyndela <adrian.s@samsung.com>
Tue, 28 May 2019 08:53:14 +0000 (08:53 +0000)
committerGerrit Code Review <gerrit@review.ap-northeast-2.compute.internal>
Tue, 28 May 2019 08:53:14 +0000 (08:53 +0000)
src/internal/uid_gid_helpers.hpp [new file with mode: 0644]
src/internal/xml_parser.cpp

diff --git a/src/internal/uid_gid_helpers.hpp b/src/internal/uid_gid_helpers.hpp
new file mode 100644 (file)
index 0000000..0f67126
--- /dev/null
@@ -0,0 +1,36 @@
+#pragma once
+#include <grp.h>
+#include <pwd.h>
+#include <string>
+#include <sys/types.h>
+
+static uid_t convertToUid(const char* user) {
+       errno = 0;
+       long val = std::strtol(user, nullptr, 10);
+       if ((!errno && val != 0) || (val == 0 && user[0] == '0' && user[1] == '\0'))
+           return (uid_t) val;
+
+       struct passwd pwent;
+       struct passwd *pwd;
+       char buf[1024];
+       if (getpwnam_r(user, &pwent, buf, sizeof(buf), &pwd) || !pwd)
+               return (uid_t) -1;
+
+       return pwd->pw_uid;
+}
+
+static gid_t convertToGid(const char* group) {
+       errno = 0;
+       long val = std::strtol(group, nullptr, 10);
+       if ((!errno && val != 0) || (val == 0 && group[0] == '0' && group[1] == '\0'))
+           return (gid_t) val;
+
+       struct group grent;
+       struct group *gg;
+       char buf[1024];
+       if (getgrnam_r(group, &grent, buf, sizeof(buf), &gg) || !gg)
+               return (gid_t) -1;
+
+       return gg->gr_gid;
+}
+
index b188d0a..98ea5cd 100644 (file)
  */
 #include "libdbuspolicy1-private.h"
 #include "tslog.hpp"
+#include "uid_gid_helpers.hpp"
 #include "xml_parser.hpp"
 #include <boost/algorithm/string/predicate.hpp>
 #include <cstring>
 #include <dirent.h>
 #include <expat.h>
-#include <grp.h>
 #include <libgen.h>
 #include <memory>
-#include <pwd.h>
 #include <unistd.h>
 
 using namespace ldp_xml_parser;
@@ -311,44 +310,6 @@ void XmlParser::parsePolicyTag(const char **attr)
        }
 }
 
-namespace {
-static uid_t convertToUid(const char* user) {
-       long val = -1;
-       errno = 0;
-       val = std::strtol(user, NULL, 10);
-       if (!errno && (val != 0 ))
-               return (uid_t)val;
-       if ((val == 0 && user[0] == '0' && user[1] == 0))
-               return (uid_t)val;
-
-       struct passwd pwent;
-       struct passwd *pwd;
-       char buf[1024];
-       if (getpwnam_r(user, &pwent, buf, sizeof(buf), &pwd) || !pwd)
-               return (uid_t)-1;
-
-       return pwd->pw_uid;
-}
-
-static gid_t convertToGid(const char* group) {
-       long val = -1;
-       errno = 0;
-       val = std::strtol(group, NULL, 10);
-       if (!errno && (val != 0))
-               return (gid_t)val;
-       if ((val == 0 && group[0] == '0' && group[1] == 0))
-               return (gid_t)val;
-
-       struct group grent;
-       struct group *gg;
-       char buf[1024];
-       if (getgrnam_r(group, &grent, buf, sizeof(buf), &gg) || !gg)
-               return (gid_t)-1;
-
-       return gg->gr_gid;
-}
-}
-
 void XmlParser::parsePolicyAttribute(const char* name, const char* value)
 {
        //possible values (from dbus-daemon man):