Add new implementation for pkg update info
[platform/core/appfw/pkgmgr-server.git] / src / util.c
index 6a1b7b9..a480626 100644 (file)
@@ -1,12 +1,18 @@
 #include <stdlib.h>
 #include <sys/types.h>
 
+#include <gum/gum-user.h>
+#include <gum/common/gum-user-types.h>
 #include <unzip.h>
 
+#include <tzplatform_config.h>
 #include <pkgmgr-info.h>
 
 #include "pkgmgr-server.h"
 
+#define RETRY_MAX 5
+#define RETRY_WAIT_USEC (1000000 / 2) /* 0.5 sec */
+
 struct manifest_and_type {
        const char *manifest;
        const char *type;
@@ -19,6 +25,8 @@ struct manifest_and_type type_map[] = {
        { NULL, NULL }
 };
 
+static const char legacy_content_path[] = "/opt/usr/media";
+
 const char *_get_pkgtype_from_file(const char *file_path)
 {
        const char *type = NULL;
@@ -68,3 +76,58 @@ char *_get_pkgtype_from_pkgid(const char *pkgid, uid_t uid)
 
        return type;
 }
+
+const char *_get_adjusted_pkgpath(const char *org_file_path, uid_t caller_uid)
+{
+       const char *file_path;
+
+       if (caller_uid >= REGULAR_USER &&
+               strstr(org_file_path, legacy_content_path) == org_file_path) {
+               DBG("legacy media path!");
+               tzplatform_set_user(caller_uid);
+               file_path = tzplatform_mkpath(TZ_USER_CONTENT,
+                       org_file_path + strlen(legacy_content_path));
+               tzplatform_reset_user();
+       } else {
+               file_path = org_file_path;
+       }
+
+       return file_path;
+}
+
+int __is_admin_user(uid_t uid)
+{
+       GumUser *guser;
+       GumUserType ut = GUM_USERTYPE_NONE;
+       int retry_cnt = 0;
+
+       do {
+               guser = gum_user_get_sync(uid, FALSE);
+               if (guser == NULL) {
+                       ERR("cannot get user information from gumd, retry");
+                       retry_cnt++;
+                       usleep(RETRY_WAIT_USEC);
+                       continue;
+               }
+               break;
+       } while (retry_cnt <= RETRY_MAX);
+
+       if (guser == NULL) {
+               ERR("cannot get user information from gumd, failed");
+               return -1;
+       }
+
+       g_object_get(G_OBJECT(guser), "usertype", &ut, NULL);
+       if (ut == GUM_USERTYPE_NONE) {
+               ERR("cannot get user type");
+               g_object_unref(guser);
+               return -1;
+       } else if (ut != GUM_USERTYPE_ADMIN) {
+               g_object_unref(guser);
+               return 0;
+       }
+
+       g_object_unref(guser);
+
+       return 1;
+}