Add manifest scheme of package dependency
[platform/core/appfw/pkgmgr-info.git] / tool / pkg-db-creator.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Contact: Junghyun Yeon <jungh.yeon@samsung.com>,
5  * Sangyoon Jang <s89.jang@samsung.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #define _GNU_SOURCE
22
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include <dlog.h>
28 #include <tzplatform_config.h>
29
30 #include "pkgmgr-info.h"
31 #include "pkgmgr_parser_db.h"
32
33 #ifdef LOG_TAG
34 #undef LOG_TAG
35 #endif
36
37 #define LOG_TAG "PKG_DB_CREATOR"
38
39 #ifndef OWNER_ROOT
40 #define OWNER_ROOT 0
41 #endif
42 #ifndef GLOBAL_USER
43 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
44 #endif
45
46 static int _remove_file(const char *path)
47 {
48         if (!path)
49                 return 0;
50
51         if (access(path, F_OK) != 0)
52                 return 0;
53
54         return remove(path);
55 }
56
57 static int _remove_db(uid_t uid)
58 {
59         char *cert_db;
60         char *parser_db;
61         char journal_path[PATH_MAX];
62
63         parser_db = getUserPkgParserDBPathUID(uid);
64         if (!parser_db) {
65                 LOGE("Failed to get parser db path");
66                 return -1;
67         }
68
69         if (_remove_file(parser_db) != 0) {
70                 LOGE("Failed to remove parser db[%s]", parser_db);
71                 free(parser_db);
72                 return -1;
73         }
74
75         snprintf(journal_path, sizeof(journal_path), "%s-journal", parser_db);
76         free(parser_db);
77         if (_remove_file(journal_path) != 0) {
78                 LOGE("Failed to remove journal[%s]", journal_path);
79                 return -1;
80         }
81
82         if (uid != OWNER_ROOT && uid != GLOBAL_USER)
83                 return 0;
84
85         cert_db = getUserPkgCertDBPath();
86         if (!cert_db) {
87                 LOGE("Failed to get cet db path");
88                 return -1;
89         }
90
91         if (_remove_file(cert_db) != 0) {
92                 LOGE("Failed to remove parser db[%s]", cert_db);
93                 free(cert_db);
94                 return -1;
95         }
96
97         snprintf(journal_path, sizeof(journal_path), "%s-journal", cert_db);
98         free(cert_db);
99         if (_remove_file(journal_path) != 0) {
100                 LOGE("Failed to remove journal[%s]", journal_path);
101                 return -1;
102         }
103
104         return 0;
105 }
106
107 int main(int argc, char *argv[])
108 {
109         int uid;
110         int ret = 0;
111
112         if ((int)getuid() > OWNER_ROOT) {
113                 LOGE("This cmd is not allowed for regular user");
114                 return -1;
115         }
116
117         if (argc == 1) {
118                 LOGE("argument should be provided");
119                 return -1;
120         }
121
122         uid = atoi(argv[1]);
123         ret = _remove_db((uid_t)uid);
124         if (ret != 0)
125                 LOGE("failed to remove database for uid[%d]", uid);
126
127         ret = pkgmgr_parser_initialize_parser_db((uid_t)uid);
128         if (ret != 0) {
129                 LOGE("failed to create parser db for uid [%d], err[%d]", uid, ret);
130                 return -1;
131
132         }
133
134         if (uid == 0) {
135                 ret = pkgmgr_parser_initialize_cert_db();
136                 if (ret != 0) {
137                         LOGE("failed to create cert db for uid [%d], err[%d]", uid, ret);
138                         return -1;
139                 }
140         }
141
142         return 0;
143 }