Fix app_size calculation 23/120923/1
authorjongmyeongko <jongmyeong.ko@samsung.com>
Thu, 23 Mar 2017 12:33:36 +0000 (21:33 +0900)
committerjongmyeong ko <jongmyeong.ko@samsung.com>
Fri, 24 Mar 2017 23:56:45 +0000 (16:56 -0700)
- Skip the mount point (.mmc, .pkg, tep/mount)
- Add external tep file

Change-Id: I4de49662e54c1c62e73df0135759c8d97296d962
Signed-off-by: jongmyeongko <jongmyeong.ko@samsung.com>
(cherry picked from commit 56fc473a0d0ce89006f7e128080fd2f8a64c69b6)

src/pkg_getsize.c

index 662832ba677afb00343630c205448999562f8b63..991480755899ae31e153082be8db3b01917c7e85 100644 (file)
@@ -92,32 +92,36 @@ static int __get_sdcard_path(char **sdpath)
        return 0;
 }
 
-static char *__get_external_image_path_by_handle(pkgmgrinfo_pkginfo_h handle)
+static char *__get_external_tep_path_by_handle(pkgmgrinfo_pkginfo_h handle)
 {
        int ret;
        char *result_path;
+       char *sdpath;
 
-       ret = pkgmgrinfo_pkginfo_get_external_image_path(handle, &result_path);
+       ret = pkgmgrinfo_pkginfo_get_tep_name(handle, &result_path);
        if (ret != PMINFO_R_OK)
                return NULL;
 
+       ret = __get_sdcard_path(&sdpath);
+       if (ret < 0)
+               return NULL;
+
+       if (strstr(result_path, sdpath) == NULL)
+               return NULL;
+
        return strdup(result_path);
 }
 
-static char *__get_external_image_path_by_pkgid(const char *pkgid)
+static char *__get_external_image_path_by_handle(pkgmgrinfo_pkginfo_h handle)
 {
        int ret;
        char *result_path;
-       pkgmgrinfo_pkginfo_h handle;
 
-       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
+       ret = pkgmgrinfo_pkginfo_get_external_image_path(handle, &result_path);
        if (ret != PMINFO_R_OK)
                return NULL;
 
-       result_path = __get_external_image_path_by_handle(handle);
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-
-       return result_path;
+       return strdup(result_path);
 }
 
 static long long __calculate_directory_size(int dfd, bool include_itself)
@@ -187,6 +191,60 @@ error:
        return -1;
 }
 
+static int __calculate_tep_dir_size(int dfd)
+{
+       long long size = 0;
+       struct stat st;
+       int ret;
+       DIR *dir;
+       struct dirent *dent = NULL;
+       const char *file_info;
+       char buf[1024] = {0, };
+
+       ret = fstat(dfd, &st);
+       if (ret < 0) {
+               LOGE("fstat() failed, file_info: ., errno: %d (%s)", errno,
+                       strerror_r(errno, buf, sizeof(buf)));
+               return -1;
+       }
+       size += __stat_size(&st);
+
+       dir = fdopendir(dfd);
+       if (dir == NULL) {
+               LOGE("fdopendir() failed, errno: %d (%s)", errno,
+                       strerror_r(errno, buf, sizeof(buf)));
+               return -1;
+       }
+
+       while ((dent = readdir(dir)) != NULL) {
+               file_info = dent->d_name;
+               if (file_info[0] == '.') {
+                       if (file_info[1] == '\0')
+                               continue;
+                       if ((file_info[1] == '.') && (file_info[2] == '\0'))
+                               continue;
+               }
+
+               if (strncmp(file_info, "mount", strlen("mount")) == 0)
+                       continue;
+
+               ret = fstatat(dfd, file_info, &st, AT_SYMLINK_NOFOLLOW);
+               if (ret < 0) {
+                       LOGE("fstatat() failed, file_info:%s, errno: %d(%s)",
+                               file_info, errno, strerror_r(errno, buf, sizeof(buf)));
+                       goto error;
+               }
+               size += __stat_size(&st);
+       }
+
+       closedir(dir);
+       return size;
+
+error:
+       closedir(dir);
+       return -1;
+}
+
 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)
@@ -323,8 +381,8 @@ error:
 
 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)
+               const char *ext_tep_path, long long *data_size,
+               long long *cache_size, long long *app_size)
 {
        char app_root_dir[MAX_PATH_LENGTH] = {0, };
        char buf[1024] = {0, };
@@ -380,6 +438,31 @@ static int __calculate_pkg_size_info(STORAGE_TYPE type,
                                }
                                /* external image file */
                                *app_size += __stat_size(&st);
+                               LOGD("app_size: %lld", *app_size);
+                               close(subfd);
+                       }
+               }
+               if (ext_tep_path) {
+                       subfd = open(ext_tep_path, O_RDONLY);
+                       if (subfd < 0) {
+                               LOGE("open() failed, path: %s, errno: %d (%s)",
+                                       ext_tep_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_tep_path, errno,
+                                               strerror_r(errno, buf,
+                                                       sizeof(buf)));
+                                       close(subfd);
+                                       return -1;
+                               }
+                               /* external tep file */
+                               *app_size += __stat_size(&st);
+                               LOGD("app_size: %lld", *app_size);
                                close(subfd);
                        }
                }
@@ -420,6 +503,10 @@ static int __calculate_pkg_size_info(STORAGE_TYPE type,
                if (dent->d_type != DT_DIR)
                        continue;
 
+               if (strncmp(name, ".pkg", strlen(".pkg")) == 0 ||
+                       strncmp(name, ".mmc", strlen(".mmc")) == 0)
+                       continue;
+
                subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
                if (subfd < 0) {
                        if (errno != ENOENT) {
@@ -469,6 +556,15 @@ static int __calculate_pkg_size_info(STORAGE_TYPE type,
                                goto error;
                        }
                        LOGD("app_size: %lld", *app_size);
+               } else if (strncmp(name, "tep", strlen("tep")) == 0) {
+                       LOGD("traverse path: %s/%s", app_root_dir, name);
+                       size = __calculate_tep_dir_size(subfd);
+                       if (size < 0) {
+                               LOGE("Calculating tep directory failed.");
+                               goto error;
+                       }
+                       *app_size += size;
+                       LOGD("app_size: %lld", *app_size);
                } else {
                        LOGD("traverse path: %s/%s", app_root_dir, name);
                        size = __calculate_directory_size(subfd, true);
@@ -528,12 +624,12 @@ static char *__get_pkg_size_info_str(const pkg_size_info_t* pkg_size_info)
 }
 
 static int __get_pkg_size_info(const char *pkgid, const char *ext_image_path,
-               pkg_size_info_t *pkg_size_info)
+               const char *ext_tep_path, pkg_size_info_t *pkg_size_info)
 {
        int ret;
 
        ret = __calculate_pkg_size_info(STORAGE_TYPE_INTERNAL_GLOBAL_PATH,
-               pkgid, ext_image_path, &pkg_size_info->data_size,
+               pkgid, ext_image_path, ext_tep_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 " \
@@ -545,7 +641,7 @@ static int __get_pkg_size_info(const char *pkgid, const char *ext_image_path,
        }
 
        ret = __calculate_pkg_size_info(STORAGE_TYPE_INTERNAL_USER_PATH,
-               pkgid, ext_image_path, &pkg_size_info->data_size,
+               pkgid, ext_image_path, ext_tep_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 " \
@@ -557,8 +653,9 @@ static int __get_pkg_size_info(const char *pkgid, const char *ext_image_path,
        }
 
        ret = __calculate_pkg_size_info(STORAGE_TYPE_EXTERNAL_USER_PATH,
-               pkgid, ext_image_path, &pkg_size_info->ext_data_size,
-               &pkg_size_info->ext_cache_size, &pkg_size_info->ext_app_size);
+               pkgid, ext_image_path, ext_tep_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 " \
                        "for pkgid(%s)", pkgid);
@@ -578,6 +675,7 @@ static int __get_total_pkg_size_info_cb(const pkgmgrinfo_pkginfo_h handle,
        int ret;
        char *pkgid;
        char *ext_image_path;
+       char *ext_tep_path;
        pkg_size_info_t temp_pkg_size_info = {0,};
        pkg_size_info_t *pkg_size_info = (void *)user_data;
 
@@ -588,12 +686,16 @@ static int __get_total_pkg_size_info_cb(const pkgmgrinfo_pkginfo_h handle,
        }
 
        ext_image_path = __get_external_image_path_by_handle(handle);
-       ret = __get_pkg_size_info(pkgid, ext_image_path, &temp_pkg_size_info);
+       ext_tep_path = __get_external_tep_path_by_handle(handle);
+       ret = __get_pkg_size_info(pkgid, ext_image_path, ext_tep_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);
+       if (ext_tep_path)
+               free(ext_tep_path);
 
        pkg_size_info->app_size += temp_pkg_size_info.app_size;
        pkg_size_info->data_size += temp_pkg_size_info.data_size;
@@ -643,6 +745,7 @@ static int __send_sizeinfo_cb(const pkgmgrinfo_pkginfo_h handle,
        int ret;
        char *pkgid;
        char *ext_image_path;
+       char *ext_tep_path;
        int data_size = 0;
        int total_size = 0;
        char total_buf[MAX_PKG_BUF_LEN];
@@ -658,13 +761,17 @@ static int __send_sizeinfo_cb(const pkgmgrinfo_pkginfo_h handle,
        }
 
        ext_image_path = __get_external_image_path_by_handle(handle);
-       ret = __get_pkg_size_info(pkgid, ext_image_path, &temp_pkg_size_info);
+       ext_tep_path = __get_external_tep_path_by_handle(handle);
+       ret = __get_pkg_size_info(pkgid, ext_image_path, ext_tep_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);
+       if (ext_tep_path)
+               free(ext_tep_path);
 
        total_size = temp_pkg_size_info.app_size +
                temp_pkg_size_info.data_size + temp_pkg_size_info.cache_size;
@@ -725,6 +832,8 @@ int main(int argc, char *argv[])
        pkg_size_info_t info = {0, };
        int fifo_exist = 0;
        char *ext_image_path = NULL;
+       char *ext_tep_path = NULL;
+       pkgmgrinfo_pkginfo_h handle;
 
        /* argv has bellowed meaning */
        /* argv[1] = pkgid */
@@ -764,19 +873,32 @@ int main(int argc, char *argv[])
                        "caller_uid=%d, fifo_exist=%d]",
                        pkgid, get_type, target_uid, caller_uid, fifo_exist);
 
+       if (get_type == PM_GET_TOTAL_SIZE ||
+               get_type == PM_GET_DATA_SIZE ||
+               get_type == PM_GET_PKG_SIZE_INFO) {
+               ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
+               if (ret == PMINFO_R_OK) {
+                       ext_image_path =
+                               __get_external_image_path_by_handle(handle);
+                       ext_tep_path =
+                               __get_external_tep_path_by_handle(handle);
+                       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+               }
+       }
+
        switch (get_type) {
        case PM_GET_TOTAL_SIZE:
                /* send result to file */
-               ext_image_path = __get_external_image_path_by_pkgid(pkgid);
-               ret = __get_pkg_size_info(pkgid, ext_image_path, &info);
+               ret = __get_pkg_size_info(pkgid, ext_image_path, ext_tep_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 */
-               ext_image_path = __get_external_image_path_by_pkgid(pkgid);
-               ret = __get_pkg_size_info(pkgid, ext_image_path, &info);
+               ret = __get_pkg_size_info(pkgid, ext_image_path, ext_tep_path,
+                                       &info);
                if (ret < 0)
                        LOGW("failed to get size of some path");
                size = info.data_size + info.cache_size;
@@ -799,8 +921,8 @@ int main(int argc, char *argv[])
                break;
        case PM_GET_PKG_SIZE_INFO:
                /* send result to signal */
-               ext_image_path = __get_external_image_path_by_pkgid(pkgid);
-               ret = __get_pkg_size_info(pkgid, ext_image_path, &info);
+               ret = __get_pkg_size_info(pkgid, ext_image_path, ext_tep_path,
+                                       &info);
                if (ret < 0)
                        LOGW("failed to get size of some path");
                size = info.app_size + info.data_size + info.cache_size;
@@ -854,6 +976,8 @@ int main(int argc, char *argv[])
 
        if (ext_image_path)
                free(ext_image_path);
+       if (ext_tep_path)
+               free(ext_tep_path);
 
        LOGD("get size result = %d", ret);
        pkgmgr_installer_free(pi);