refactor getsize sync function 23/105823/4
authorjongmyeongko <jongmyeong.ko@samsung.com>
Mon, 19 Dec 2016 14:09:09 +0000 (23:09 +0900)
committerjongmyeongko <jongmyeong.ko@samsung.com>
Wed, 21 Dec 2016 01:24:53 +0000 (10:24 +0900)
Submit with:
https://review.tizen.org/gerrit/#/c/105817/
https://review.tizen.org/gerrit/#/c/105823/
https://review.tizen.org/gerrit/#/c/105825/

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

index 8c47a5c..64df2fe 100644 (file)
@@ -493,33 +493,34 @@ static int __get_total_pkg_size_info_cb(const pkgmgrinfo_pkginfo_h handle,
        return 0;
 }
 
-int __make_size_info_file(char *req_key, long long size)
+static int __write_size_info_fifo(char *req_key, long long size)
 {
-       FILE *file;
-       int fd = 0;
-       char buf[MAX_PKG_BUF_LEN];
-       char info_file[MAX_PKG_BUF_LEN];
+       int fd;
+       char buf[MAX_LONGLONG_LENGTH];
+       char fifo_path[PATH_MAX];
+       int ret = 0;
 
        if (req_key == NULL)
                return -1;
 
-       snprintf(info_file, sizeof(info_file), "%s/%s", PKG_SIZE_INFO_PATH,
-               req_key);
-       LOGD("File path = (%s), size = (%lld)", info_file, size);
+       snprintf(fifo_path, sizeof(fifo_path), "/tmp/pkgmgr/%s", req_key);
+       LOGD("fifo path = (%s), size = (%lld)", fifo_path, size);
 
-       file = fopen(info_file, "w");
-       if (file == NULL) {
-               LOGE("Couldn't open the file %s", info_file);
+       fd = open(fifo_path, O_WRONLY);
+       if (fd < 0) {
+               LOGE("failed to open, errno(%d)", errno);
                return -1;
        }
 
        snprintf(buf, MAX_LONGLONG_LENGTH, "%lld", size);
-       fwrite(buf, 1, strlen(buf), file);
+       ret = write(fd, buf, strlen(buf));
+       if (ret < 0) {
+               LOGE("failed to write, ret(%d), errno(%d)", ret, errno);
+               close(fd);
+               return -1;
+       }
 
-       fflush(file);
-       fd = fileno(file);
-       fsync(fd);
-       fclose(file);
+       close(fd);
 
        return 0;
 }
@@ -605,6 +606,7 @@ int main(int argc, char *argv[])
        long long size = 0;
        pkgmgr_installer *pi;
        pkg_size_info_t info = {0, };
+       int fifo_exist = 0;
 
        /* argv has bellowed meaning */
        /* argv[1] = pkgid */
@@ -612,6 +614,7 @@ int main(int argc, char *argv[])
        /* argv[3] = caller uid */
        /* argv[5] = req_key */
        /* argv[7] = target uid */
+       /* argv[8] = sync flag */
 
        if (argv[1] == NULL) {
                LOGE("pkgid is NULL");
@@ -623,9 +626,17 @@ int main(int argc, char *argv[])
        caller_uid = atoi(argv[3]);
        req_key = argv[5];
 
+       if (argv[8] && !strcmp("--sync", argv[8])) {
+               fifo_exist = 1;
+       }
+
        pi = pkgmgr_installer_new();
        if (pi == NULL) {
                LOGE("failed to create installer");
+               if (fifo_exist) {
+                       if (__write_size_info_fifo(req_key, -1) < 0)
+                               LOGE("failed to write to fifo");
+               }
                return -1;
        }
        pkgmgr_installer_receive_request(pi, argc, argv);
@@ -633,8 +644,8 @@ int main(int argc, char *argv[])
        target_uid = pkgmgr_installer_get_uid(pi);
 
        LOGD("start get size : [pkgid=%s, request type=%d, target_uid=%d, "
-                       "caller_uid=%d]",
-                       pkgid, get_type, target_uid, caller_uid);
+                       "caller_uid=%d, fifo_exist=%d]",
+                       pkgid, get_type, target_uid, caller_uid, fifo_exist);
 
        switch (get_type) {
        case PM_GET_TOTAL_SIZE:
@@ -643,7 +654,6 @@ int main(int argc, char *argv[])
                if (ret < 0)
                        LOGW("failed to get size of some path");
                size = info.app_size + info.data_size + info.cache_size;
-               ret = __make_size_info_file(req_key, size);
                break;
        case PM_GET_DATA_SIZE:
                /* send result to file */
@@ -651,7 +661,6 @@ int main(int argc, char *argv[])
                if (ret < 0)
                        LOGW("failed to get size of some path");
                size = info.data_size + info.cache_size;
-               ret = __make_size_info_file(req_key, size);
                break;
        case PM_GET_ALL_PKGS:
                /* send result to file */
@@ -661,7 +670,6 @@ int main(int argc, char *argv[])
                        LOGE("failed to get all packages");
                else
                        size = info.app_size + info.data_size + info.cache_size;
-               ret = __make_size_info_file(req_key, size);
                break;
        case PM_GET_SIZE_INFO:
                /* send each result to signal */
@@ -669,7 +677,6 @@ int main(int argc, char *argv[])
                        pi, target_uid);
                if (ret < 0)
                        LOGE("failed to get all packages");
-               ret = __make_size_info_file(req_key, 0);
                break;
        case PM_GET_PKG_SIZE_INFO:
                /* send result to signal */
@@ -680,7 +687,6 @@ int main(int argc, char *argv[])
                /* always send a result */
                ret = __send_result_to_signal(pi, req_key,
                        pkgid, &info);
-               ret = __make_size_info_file(req_key, size);
                break;
        case PM_GET_TOTAL_PKG_SIZE_INFO:
                /* send result to signal */
@@ -693,7 +699,6 @@ int main(int argc, char *argv[])
                /* always send a result */
                ret = __send_result_to_signal(pi, req_key,
                        PKG_SIZE_INFO_TOTAL, &info);
-               ret = __make_size_info_file(req_key, size);
                break;
        default:
                ret = -1;
@@ -701,6 +706,11 @@ int main(int argc, char *argv[])
                break;
        }
 
+       if (fifo_exist) {
+               if (__write_size_info_fifo(req_key, size) < 0)
+                       LOGE("failed to write to fifo");
+       }
+
        /* Only PM_GET_SIZE_INFO type needs 'end' signal,
         * because the result is sent on each package's calculation.
         * So, the callback needs to know the end of results.