Add app2sd file as external app_size 85/120285/12
authorjongmyeongko <jongmyeong.ko@samsung.com>
Wed, 22 Mar 2017 06:05:39 +0000 (15:05 +0900)
committerjongmyeongko <jongmyeong.ko@samsung.com>
Wed, 22 Mar 2017 10:25:41 +0000 (19:25 +0900)
Requires:
https://review.tizen.org/gerrit/120280

Change-Id: Idd2fd98c0f08377e34509a77181f6543e1d6129d
Signed-off-by: jongmyeongko <jongmyeong.ko@samsung.com>
src/pkg_getsize.c

index 1a1d7578591aedd669a409497b0ff9d9bee126e5..662832ba677afb00343630c205448999562f8b63 100644 (file)
@@ -92,6 +92,34 @@ static int __get_sdcard_path(char **sdpath)
        return 0;
 }
 
+static char *__get_external_image_path_by_handle(pkgmgrinfo_pkginfo_h handle)
+{
+       int ret;
+       char *result_path;
+
+       ret = pkgmgrinfo_pkginfo_get_external_image_path(handle, &result_path);
+       if (ret != PMINFO_R_OK)
+               return NULL;
+
+       return strdup(result_path);
+}
+
+static char *__get_external_image_path_by_pkgid(const char *pkgid)
+{
+       int ret;
+       char *result_path;
+       pkgmgrinfo_pkginfo_h handle;
+
+       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
+       if (ret != PMINFO_R_OK)
+               return NULL;
+
+       result_path = __get_external_image_path_by_handle(handle);
+       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+
+       return result_path;
+}
+
 static long long __calculate_directory_size(int dfd, bool include_itself)
 {
        long long size = 0;
@@ -159,7 +187,7 @@ error:
        return -1;
 }
 
-static long long __calculate_shared_dir_size(int dfd, const char *app_root_dir,
+static int __calculate_shared_dir_size(int dfd, const char *app_root_dir,
                long long *data_size, long long *app_size,
                long long *cache_size)
 {
@@ -192,6 +220,13 @@ static long long __calculate_shared_dir_size(int dfd, const char *app_root_dir,
 
        subfd = openat(fd, "data", O_RDONLY | O_DIRECTORY);
        if (subfd >= 0) {
+               ret = fstat(subfd, &st);
+               if (ret < 0) {
+                       LOGE("fstat() failed, errno: %d (%s)", errno,
+                               strerror_r(errno, buf, sizeof(buf)));
+                       goto error;
+               }
+               *app_size += __stat_size(&st); /* shared/data directory */
                size = __calculate_directory_size(subfd, false);
                if (size < 0) {
                        LOGE("Calculating shared/data directory failed.");
@@ -210,6 +245,13 @@ static long long __calculate_shared_dir_size(int dfd, const char *app_root_dir,
 
        subfd = openat(fd, "trusted", O_RDONLY | O_DIRECTORY);
        if (subfd >= 0) {
+               ret = fstat(subfd, &st);
+               if (ret < 0) {
+                       LOGE("fstat() failed, errno: %d (%s)", errno,
+                               strerror_r(errno, buf, sizeof(buf)));
+                       goto error;
+               }
+               *app_size += __stat_size(&st); /* shared/trusted directory */
                size = __calculate_directory_size(subfd, false);
                if (size < 0) {
                        LOGE("Calculating shared/trusted directory failed.");
@@ -246,6 +288,13 @@ static long long __calculate_shared_dir_size(int dfd, const char *app_root_dir,
 
        subfd = openat(fd, "cache", O_RDONLY | O_DIRECTORY);
        if (subfd >= 0) {
+               ret = fstat(subfd, &st);
+               if (ret < 0) {
+                       LOGE("fstat() failed, errno: %d (%s)", errno,
+                               strerror_r(errno, buf, sizeof(buf)));
+                       goto error;
+               }
+               *app_size += __stat_size(&st); /* shared/cache directory */
                size = __calculate_directory_size(subfd, false);
                if (size < 0) {
                        LOGE("Calculating shared/cache directory failed.");
@@ -272,7 +321,8 @@ error:
        return -1;
 }
 
-static int __calculate_pkg_size_info(STORAGE_TYPE type, const char *pkgid,
+static int __calculate_pkg_size_info(STORAGE_TYPE type,
+               const char *pkgid, const char *ext_image_path,
                long long *data_size, long long *cache_size,
                long long *app_size)
 {
@@ -310,6 +360,29 @@ static int __calculate_pkg_size_info(STORAGE_TYPE type, const char *pkgid,
                        tzplatform_mkpath3(TZ_USER_NAME, "apps_rw", pkgid));
                free(sdpath);
                tzplatform_reset_user();
+               if (ext_image_path) {
+                       subfd = open(ext_image_path, O_RDONLY);
+                       if (subfd < 0) {
+                               LOGE("open() failed, path: %s, errno: %d (%s)",
+                                       ext_image_path, errno,
+                                       strerror_r(errno, buf, sizeof(buf)));
+                               return -1;
+                       } else {
+                               ret = fstat(subfd, &st);
+                               if (ret < 0) {
+                                       LOGE("fstat() failed, path: %s, "
+                                               "errno: %d (%s)",
+                                               ext_image_path, errno,
+                                               strerror_r(errno, buf,
+                                                       sizeof(buf)));
+                                       close(subfd);
+                                       return -1;
+                               }
+                               /* external image file */
+                               *app_size += __stat_size(&st);
+                               close(subfd);
+                       }
+               }
        } else {
                LOGE("Invalid STORAGE_TYPE");
                return -1;
@@ -358,6 +431,13 @@ static int __calculate_pkg_size_info(STORAGE_TYPE type, const char *pkgid,
                }
                if (strncmp(name, "data", strlen("data")) == 0) {
                        LOGD("traverse path: %s/%s", app_root_dir, name);
+                       ret = fstat(subfd, &st);
+                       if (ret < 0) {
+                               LOGE("fstat() failed, errno: %d (%s)", errno,
+                                       strerror_r(errno, buf, sizeof(buf)));
+                               goto error;
+                       }
+                       *app_size += __stat_size(&st); /* data directory */
                        size = __calculate_directory_size(subfd, false);
                        if (size < 0) {
                                LOGE("Calculating data directory failed.");
@@ -367,6 +447,13 @@ static int __calculate_pkg_size_info(STORAGE_TYPE type, const char *pkgid,
                        LOGD("data_size: %lld", *data_size);
                } else if (strncmp(name, "cache", strlen("cache")) == 0) {
                        LOGD("traverse path: %s/%s", app_root_dir, name);
+                       ret = fstat(subfd, &st);
+                       if (ret < 0) {
+                               LOGE("fstat() failed, errno: %d (%s)", errno,
+                                       strerror_r(errno, buf, sizeof(buf)));
+                               goto error;
+                       }
+                       *app_size += __stat_size(&st); /* cache directory */
                        size = __calculate_directory_size(subfd, false);
                        if (size < 0) {
                                LOGE("Calculating cache directory failed.");
@@ -375,13 +462,12 @@ static int __calculate_pkg_size_info(STORAGE_TYPE type, const char *pkgid,
                        *cache_size += size;
                        LOGD("cache_size: %lld", *cache_size);
                } else if (strncmp(name, "shared", strlen("shared")) == 0) {
-                       size = __calculate_shared_dir_size(dfd, app_root_dir,
+                       ret = __calculate_shared_dir_size(dfd, app_root_dir,
                                        data_size, app_size, cache_size);
-                       if (size < 0) {
+                       if (ret < 0) {
                                LOGE("Calculating shared directory failed.");
                                goto error;
                        }
-                       *app_size += size;
                        LOGD("app_size: %lld", *app_size);
                } else {
                        LOGD("traverse path: %s/%s", app_root_dir, name);
@@ -441,13 +527,13 @@ static char *__get_pkg_size_info_str(const pkg_size_info_t* pkg_size_info)
        return size_info_str;
 }
 
-static int __get_pkg_size_info(const char *pkgid,
+static int __get_pkg_size_info(const char *pkgid, const char *ext_image_path,
                pkg_size_info_t *pkg_size_info)
 {
        int ret;
 
        ret = __calculate_pkg_size_info(STORAGE_TYPE_INTERNAL_GLOBAL_PATH,
-               pkgid, &pkg_size_info->data_size,
+               pkgid, ext_image_path, &pkg_size_info->data_size,
                &pkg_size_info->cache_size, &pkg_size_info->app_size);
        if (ret < 0) {
                LOGE("failed to calculate interal(global) size " \
@@ -459,7 +545,7 @@ static int __get_pkg_size_info(const char *pkgid,
        }
 
        ret = __calculate_pkg_size_info(STORAGE_TYPE_INTERNAL_USER_PATH,
-               pkgid, &pkg_size_info->data_size,
+               pkgid, ext_image_path, &pkg_size_info->data_size,
                &pkg_size_info->cache_size, &pkg_size_info->app_size);
        if (ret < 0) {
                LOGE("failed to calculate interal(user) size " \
@@ -471,7 +557,7 @@ static int __get_pkg_size_info(const char *pkgid,
        }
 
        ret = __calculate_pkg_size_info(STORAGE_TYPE_EXTERNAL_USER_PATH,
-               pkgid, &pkg_size_info->ext_data_size,
+               pkgid, ext_image_path, &pkg_size_info->ext_data_size,
                &pkg_size_info->ext_cache_size, &pkg_size_info->ext_app_size);
        if (ret < 0) {
                LOGE("failed to calculate external(user) size " \
@@ -491,6 +577,7 @@ static int __get_total_pkg_size_info_cb(const pkgmgrinfo_pkginfo_h handle,
 {
        int ret;
        char *pkgid;
+       char *ext_image_path;
        pkg_size_info_t temp_pkg_size_info = {0,};
        pkg_size_info_t *pkg_size_info = (void *)user_data;
 
@@ -500,10 +587,13 @@ static int __get_total_pkg_size_info_cb(const pkgmgrinfo_pkginfo_h handle,
                return -1;
        }
 
-       ret = __get_pkg_size_info(pkgid, &temp_pkg_size_info);
+       ext_image_path = __get_external_image_path_by_handle(handle);
+       ret = __get_pkg_size_info(pkgid, ext_image_path, &temp_pkg_size_info);
        if (ret < 0)
                LOGW("failed to get size of some path");
                /* even if it's an error, store all the valid result */
+       if (ext_image_path)
+               free(ext_image_path);
 
        pkg_size_info->app_size += temp_pkg_size_info.app_size;
        pkg_size_info->data_size += temp_pkg_size_info.data_size;
@@ -552,6 +642,7 @@ static int __send_sizeinfo_cb(const pkgmgrinfo_pkginfo_h handle,
 {
        int ret;
        char *pkgid;
+       char *ext_image_path;
        int data_size = 0;
        int total_size = 0;
        char total_buf[MAX_PKG_BUF_LEN];
@@ -566,11 +657,15 @@ static int __send_sizeinfo_cb(const pkgmgrinfo_pkginfo_h handle,
                return -1;
        }
 
-       ret = __get_pkg_size_info(pkgid, &temp_pkg_size_info);
+       ext_image_path = __get_external_image_path_by_handle(handle);
+       ret = __get_pkg_size_info(pkgid, ext_image_path, &temp_pkg_size_info);
        if (ret < 0)
                LOGW("failed to get size of some path");
                /* even if it's an error, store all the valid result */
 
+       if (ext_image_path)
+               free(ext_image_path);
+
        total_size = temp_pkg_size_info.app_size +
                temp_pkg_size_info.data_size + temp_pkg_size_info.cache_size;
        data_size = temp_pkg_size_info.data_size +
@@ -629,6 +724,7 @@ int main(int argc, char *argv[])
        pkgmgr_installer *pi;
        pkg_size_info_t info = {0, };
        int fifo_exist = 0;
+       char *ext_image_path = NULL;
 
        /* argv has bellowed meaning */
        /* argv[1] = pkgid */
@@ -671,14 +767,16 @@ int main(int argc, char *argv[])
        switch (get_type) {
        case PM_GET_TOTAL_SIZE:
                /* send result to file */
-               ret = __get_pkg_size_info(pkgid, &info);
+               ext_image_path = __get_external_image_path_by_pkgid(pkgid);
+               ret = __get_pkg_size_info(pkgid, ext_image_path, &info);
                if (ret < 0)
                        LOGW("failed to get size of some path");
                size = info.app_size + info.data_size + info.cache_size;
                break;
        case PM_GET_DATA_SIZE:
                /* send result to file */
-               ret = __get_pkg_size_info(pkgid, &info);
+               ext_image_path = __get_external_image_path_by_pkgid(pkgid);
+               ret = __get_pkg_size_info(pkgid, ext_image_path, &info);
                if (ret < 0)
                        LOGW("failed to get size of some path");
                size = info.data_size + info.cache_size;
@@ -701,7 +799,8 @@ int main(int argc, char *argv[])
                break;
        case PM_GET_PKG_SIZE_INFO:
                /* send result to signal */
-               ret = __get_pkg_size_info(pkgid, &info);
+               ext_image_path = __get_external_image_path_by_pkgid(pkgid);
+               ret = __get_pkg_size_info(pkgid, ext_image_path, &info);
                if (ret < 0)
                        LOGW("failed to get size of some path");
                size = info.app_size + info.data_size + info.cache_size;
@@ -753,6 +852,9 @@ int main(int argc, char *argv[])
                        LOGE("failed to send finished signal");
        }
 
+       if (ext_image_path)
+               free(ext_image_path);
+
        LOGD("get size result = %d", ret);
        pkgmgr_installer_free(pi);