Multi user aware when getting pkg info handle 50/74450/2
authorKyungwook Tak <k.tak@samsung.com>
Tue, 14 Jun 2016 07:43:48 +0000 (16:43 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Tue, 14 Jun 2016 08:28:08 +0000 (17:28 +0900)
Change-Id: Ia3746304891954471d8981ed2bea2479c1967aaa
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
src/framework/service/app-deleter.cpp
src/framework/service/file-system.cpp
src/framework/service/file-system.h
src/framework/service/fs-utils.cpp
src/framework/service/fs-utils.h

index 99920c7..e3cfe8a 100644 (file)
 #include <memory>
 #include <cstring>
 
-#ifdef PLATFORM_VERSION_3
-#include <vector>
-#include <unistd.h>
-#include <sys/types.h>
-#include <pwd.h>
-#endif
-
 #include <package-manager.h>
 
 #include "common/audit/logger.h"
 #include "common/exception.h"
-
-namespace {
-
-#ifdef PLATFORM_VERSION_3
-uid_t getUid(const std::string &username)
-{
-       auto bufsize = ::sysconf(_SC_GETPW_R_SIZE_MAX);
-       bufsize = (bufsize == -1) ? 16384 : bufsize;
-
-       std::vector<char> buf(bufsize, 0);
-
-       struct passwd pwd;
-       struct passwd *result = nullptr;
-
-       auto ret = ::getpwnam_r(username.c_str(), &pwd, buf.data(), buf.size(), &result);
-
-       if (result == nullptr) {
-               if (ret == 0)
-                       ThrowExc(CSR_ERROR_SERVER, "Uid not found by username: " << username);
-               else
-                       ThrowExc(CSR_ERROR_SERVER, "Failed to getpwnam_r with errno: " << errno);
-       }
-
-       return pwd.pw_uid;
-}
-#endif
-
-} // namespace anonymous
+#include "service/fs-utils.h"
 
 namespace Csr {
 
@@ -84,7 +50,7 @@ void AppDeleter::remove(const std::string &pkgid, const std::string &username)
                                                                                nullptr, nullptr);
        else
                ret = ::pkgmgr_client_usr_uninstall(client.get(), nullptr, pkgid.c_str(), PM_QUIET,
-                                                                                       nullptr, nullptr, ::getUid(username));
+                                                                                       nullptr, nullptr, getUid(username));
 #else
        (void) username;
        ret = ::pkgmgr_client_uninstall(client.get(), nullptr, pkgid.c_str(), PM_QUIET,
index 8b29beb..9d25331 100644 (file)
@@ -49,6 +49,7 @@ std::vector<std::regex> g_regexprs{
 #ifdef PLATFORM_VERSION_3
        makeRegexpr("^(/opt/usr/apps/([^/]+))"),               // /opt/usr/apps/{pkgid}/
        makeRegexpr("^(/home/([^/]+)/apps_rw/([^/]+))"),       // /home/{user}/apps_rw/{pkgid}/
+       makeRegexpr("^(/opt/home/([^/]+)/apps_rw/([^/]+))"),   // /opt/home/{user}/apps_rw/{pkgid}/
        makeRegexpr("^(/sdcard/app2sd/([^/]+)/([^/]+))"),      // /sdcard/app2sd/{user}/{pkgid}/
        makeRegexpr("^(/sdcard/app2sd/([^/]+))"),              // /sdcard/app2sd/{pkgid}/
        makeRegexpr("^(/sdcard/apps/([^/]+)/apps_rw/([^/]+))") // /sdcard/apps/{user}/apps_rw/{pkgid}/
@@ -62,12 +63,26 @@ std::vector<std::regex> g_regexprs{
 
 } // namespace anonymous
 
-int File::getPkgTypes(const std::string &pkgid)
+int File::getPkgTypes(const std::string &user, const std::string &pkgid)
 {
        pkgmgrinfo_pkginfo_h handle;
+
+#ifdef PLATFORM_VERSION_3
+       int ret = -1;
+       if (user.empty())
+               ret = ::pkgmgrinfo_pkginfo_get_pkginfo(pkgid.c_str(), &handle);
+       else
+               ret = ::pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid.c_str(), getUid(user), &handle);
+#else
+       (void) user;
        auto ret = ::pkgmgrinfo_pkginfo_get_pkginfo(pkgid.c_str(), &handle);
-       if (ret != PMINFO_R_OK)
+#endif
+
+       if (ret != PMINFO_R_OK) {
+               INFO("Extracted pkgid[" << pkgid << "] from filepath isn't pkg id. "
+                        "It's not package.");
                return 0;
+       }
 
        auto type = static_cast<int>(Type::Package);
 
@@ -84,16 +99,19 @@ bool File::isInApp(const std::string &path)
                if (!std::regex_search(path, matched, rege))
                        continue;
 
+               std::string pkgUser;
                std::string pkgId;
 
-               if (matched.size() == 3)
+               if (matched.size() == 3) {
                        pkgId = matched[2];
-               else if (matched.size() == 4)
+               } else if (matched.size() == 4) {
+                       pkgUser = matched[2];
                        pkgId = matched[3];
-               else
+               } else {
                        continue;
+               }
 
-               return File::getPkgTypes(pkgId) != 0;
+               return File::getPkgTypes(pkgUser, pkgId) != 0;
        }
 
        return false;
@@ -108,6 +126,7 @@ std::string File::getPkgPath(const std::string &path)
                        continue;
 
                std::string pkgPath;
+               std::string pkgUser;
                std::string pkgId;
 
                if (matched.size() == 3) {
@@ -115,12 +134,13 @@ std::string File::getPkgPath(const std::string &path)
                        pkgId = matched[2];
                } else if (matched.size() == 4) {
                        pkgPath = matched[1];
+                       pkgUser = matched[2];
                        pkgId = matched[3];
                } else {
                        continue;
                }
 
-               return File::getPkgTypes(pkgId) != 0 ? pkgPath : path;
+               return File::getPkgTypes(pkgUser, pkgId) != 0 ? pkgPath : path;
        }
 
        return path;
@@ -137,6 +157,7 @@ File::File(const std::string &fpath, int type) : m_path(fpath), m_type(type)
                if (matched.size() == 3) {
                        this->m_appPkgPath = matched[1];
                        this->m_appPkgId = matched[2];
+                       this->m_appUser.clear();
                } else if (matched.size() == 4) {
                        this->m_appPkgPath = matched[1];
                        this->m_appUser = matched[2];
@@ -145,7 +166,7 @@ File::File(const std::string &fpath, int type) : m_path(fpath), m_type(type)
                        continue;
                }
 
-               this->m_type |= File::getPkgTypes(this->m_appPkgId);
+               this->m_type |= File::getPkgTypes(this->m_appUser, this->m_appPkgId);
 
                break;
        }
@@ -256,6 +277,7 @@ FsVisitor::FsVisitor(const std::string &dirpath, time_t modifiedSince) :
        if (!this->m_dirptr)
                ThrowExc(CSR_ERROR_SERVER, "Failed to open dir: " << dirpath);
 
+       DEBUG("dir opened: " << dirpath);
        this->m_dirs.push((dirpath.back() == '/') ? dirpath : (dirpath + '/'));
 }
 
@@ -291,8 +313,9 @@ FilePtr FsVisitor::next()
 
                        if (this->m_dirs.empty())
                                return nullptr;
-                       else
-                               continue;
+
+                       DEBUG("dir opened: " << this->m_dirs.front());
+                       continue;
                }
 
                auto &dir = this->m_dirs.front();
@@ -308,6 +331,7 @@ FilePtr FsVisitor::next()
                        else if (name_size == 2 && ::strcmp(name, "..") == 0)
                                continue;
 
+                       DEBUG("push dir to dirs: " << (dir + name + '/'));
                        this->m_dirs.emplace(dir + name + '/');
                } else if (result->d_type == DT_REG) {
                        try {
index 88e8c1a..e679865 100644 (file)
@@ -65,7 +65,7 @@ private:
 
        static FilePtr createInternal(const std::string &fpath, time_t modifiedSince,
                                                                  bool isModifiedOnly);
-       static int getPkgTypes(const std::string &pkgid);
+       static int getPkgTypes(const std::string &user, const std::string &pkgid);
 
        explicit File(const std::string &fpath, int type);
 
index 52d0de1..ada4b08 100644 (file)
  */
 #include "service/fs-utils.h"
 
+#include <vector>
 #include <cstring>
 #include <cerrno>
 #include <unistd.h>
+#include <sys/types.h>
+#include <pwd.h>
 
+#include <csr-error.h>
+
+#include "common/exception.h"
 #include "common/audit/logger.h"
 
 namespace Csr {
 
+uid_t getUid(const std::string &username)
+{
+       auto bufsize = ::sysconf(_SC_GETPW_R_SIZE_MAX);
+       bufsize = (bufsize == -1) ? 16384 : bufsize;
+
+       std::vector<char> buf(bufsize, 0);
+
+       struct passwd pwd;
+       struct passwd *result = nullptr;
+
+       auto ret = ::getpwnam_r(username.c_str(), &pwd, buf.data(), buf.size(), &result);
+
+       if (result == nullptr) {
+               if (ret == 0)
+                       ThrowExc(CSR_ERROR_SERVER, "Uid not found by username: " << username);
+               else
+                       ThrowExc(CSR_ERROR_SERVER, "Failed to getpwnam_r with errno: " << errno);
+       }
+
+       return pwd.pw_uid;
+}
+
 std::unique_ptr<struct stat> getStat(const std::string &target)
 {
        std::unique_ptr<struct stat> statptr(new struct stat);
-       memset(statptr.get(), 0x00, sizeof(struct stat));
+       ::memset(statptr.get(), 0x00, sizeof(struct stat));
 
        if (::stat(target.c_str(), statptr.get()) != 0) {
                const int err = errno;
index 1707a99..629b1fa 100644 (file)
 
 #include <memory>
 #include <string>
+#include <sys/types.h>
 #include <sys/stat.h>
 
 namespace Csr {
 
+uid_t getUid(const std::string &username);
+
 std::unique_ptr<struct stat> getStat(const std::string &target);
 
 }