Add to remove file command 99/283399/7
authorChanggyu Choi <changyu.choi@samsung.com>
Wed, 26 Oct 2022 02:36:46 +0000 (11:36 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Tue, 1 Nov 2022 04:43:25 +0000 (13:43 +0900)
Change-Id: I047fe640e1b9b1bc8f4f5553c56251171e888698
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
src/pkg_cleardata/pkg_cleardata.c

index ae91ff0..52a0888 100644 (file)
@@ -384,6 +384,72 @@ static int __clear_all_cache_dir(void)
        return 0;
 }
 
+static int __remove_file(const char *pkgid, const char *file_path)
+{
+       int ret;
+       char filename[PATH_MAX];
+       const char *rootpath;
+       pkgmgrinfo_pkginfo_h pkg_handle = NULL;
+       char *pkg_type = NULL;
+       struct stat st_file_info;
+       char buf[1024] = {0, };
+
+       if (pkgid == NULL || file_path == NULL) {
+               LOGE("parameter is NULL");
+               return -1;
+       }
+
+       ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkg_handle);
+       if (ret != PMINFO_R_OK) {
+               LOGE("Failed to get pkginfo");
+               return -1;
+       }
+
+       ret = pkgmgrinfo_pkginfo_get_type(pkg_handle, &pkg_type);
+       if (ret != PMINFO_R_OK) {
+               LOGE("Failed to get type");
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               return -1;
+       }
+
+       tzplatform_set_user(uid);
+       rootpath = tzplatform_getenv(TZ_USER_APP);
+
+       __send_signal(pkgid, pkg_type,
+                       PKGMGR_INSTALLER_START_KEY_STR,
+                       PKGMGR_INSTALLER_CLEAR_EVENT_STR);
+
+       snprintf(filename, sizeof(filename), "%s/%s/data/%s",
+                       rootpath, pkgid, file_path);
+       if (lstat(filename, &st_file_info) < 0)
+               perror(filename);
+
+       if (S_ISDIR(st_file_info.st_mode)) {
+               ret = __clear_dir(file_path);
+               if (ret != 0) {
+                       LOGE("Couldn't remove the directory. errno : %d (%s)",
+                               errno, strerror_r(errno, buf, sizeof(buf)));
+               }
+       }
+
+       ret = remove(filename);
+       if (ret != 0) {
+               LOGE("Couldn't remove the directory. errno : %d (%s)", errno,
+                       strerror_r(errno, buf, sizeof(buf)));
+               __send_signal(pkgid, pkg_type,
+                               PKGMGR_INSTALLER_END_KEY_STR,
+                               PKGMGR_INSTALLER_FAIL_EVENT_STR);
+       } else {
+               __send_signal(pkgid, pkg_type, PKGMGR_INSTALLER_END_KEY_STR,
+                               PKGMGR_INSTALLER_OK_EVENT_STR);
+
+       }
+
+       tzplatform_reset_user();
+       pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+       return ret;
+}
+
 static void print_usage(void)
 {
        printf("pkg data/cache clear tool\n");
@@ -391,6 +457,7 @@ static void print_usage(void)
        printf("\n");
        printf("-c, --cache           clear pkg's cache\n");
        printf("-d, --data            clear pkg's data\n");
+       printf("-f, --file            remove a pkg's file or directory\n");
        printf("-h, --help            print this help\n");
        printf("-n, --pkgid           package id\n");
        printf("-u, --uid             user id\n");
@@ -400,6 +467,7 @@ static void print_usage(void)
        printf("pkg_cleardata -c -n <pkgid> -u <uid>\n");
        printf("pkg_cleardata -d -n <pkgid> -u <uid>\n");
        printf("pkg_cleardata -cd -n <pkgid> -u <uid>\n");
+       printf("pkg_cleardata -n <pkgid> -u <uid> -f <file_path>\n");
        printf("\n");
 }
 
@@ -409,15 +477,17 @@ int main(int argc, char *argv[])
        int c = -1;
        int opt_idx = 0;
        char pkgid[MAX_PKG_NAME_LEN] = { 0, };
+       char file_path[MAX_PKG_NAME_LEN] = { 0, };
        bool clear_cache = false;
        bool clear_data = false;
-       const char short_options[] = "cdhn:u:";
+       const char short_options[] = "cdhn:u:f:";
        const struct option long_options[] = {
                {"cache", 0, NULL, 'c'},
                {"data", 0, NULL, 'd'},
                {"help", 0, NULL, 'h'},
                {"pkgid", 1, NULL, 'n'},
                {"uid", 1, NULL, 'u'},
+               {"file", 1, NULL, 'f'},
                {0, 0, 0, 0}            /* sentinel */
        };
 
@@ -462,12 +532,22 @@ int main(int argc, char *argv[])
                                print_usage();
                                return -1;
                        }
+                       break;
+               case 'f':
+                       if (optarg) {
+                               snprintf(file_path, sizeof(file_path), "%s", optarg);
+                       } else {
+                               print_usage();
+                               return -1;
+                       }
+                       break;
                default:
                        break;
                }
        }
 
-       if ((!clear_cache && !clear_data) || (pkgid[0] == '\0') || uid == 0) {
+       if ((file_path[0] == '\0' && !clear_cache && !clear_data) ||
+                       (pkgid[0] == '\0') || uid == 0) {
                print_usage();
                return -1;
        }
@@ -511,6 +591,11 @@ int main(int argc, char *argv[])
                ret = __clear_data_dir(pkgid);
        }
 
+       if (!clear_data && !clear_cache && file_path[0] != '\0') {
+               pkgmgr_installer_set_request_type(pi, PKGMGR_REQ_CLEAR);
+               ret = __remove_file(pkgid, file_path);
+       }
+
        pkgmgr_installer_free(pi);
 
        return ret;