#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 {
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,
#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}/
} // 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);
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;
continue;
std::string pkgPath;
+ std::string pkgUser;
std::string pkgId;
if (matched.size() == 3) {
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;
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];
continue;
}
- this->m_type |= File::getPkgTypes(this->m_appPkgId);
+ this->m_type |= File::getPkgTypes(this->m_appUser, this->m_appPkgId);
break;
}
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 + '/'));
}
if (this->m_dirs.empty())
return nullptr;
- else
- continue;
+
+ DEBUG("dir opened: " << this->m_dirs.front());
+ continue;
}
auto &dir = this->m_dirs.front();
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 {
*/
#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;