16 #include <tzplatform_config.h>
18 #include "pkgmgr-info.h"
19 #include "pkgmgrinfo_debug.h"
20 #include "pkgmgrinfo_private.h"
21 #include "pkgmgr_parser.h"
22 #include "pkgmgr_parser_db.h"
24 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
26 static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid)
35 fullpath = strdup(dir);
38 subpath = dirname(fullpath);
39 if (strlen(subpath) > 1 && strcmp(subpath, fullpath) != 0) {
40 ret = _mkdir_for_user(fullpath, uid, gid);
47 ret = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
48 if (ret && errno != EEXIST) {
51 } else if (ret && errno == EEXIST) {
56 if (getuid() == ROOT_UID) {
57 fd = open(dir, O_RDONLY);
59 _LOGE("FAIL : open %s : %s", dir,
60 strerror_r(errno, buf, sizeof(buf)));
66 _LOGE("FAIL : fstat %s : %s", dir,
67 strerror_r(errno, buf, sizeof(buf)));
72 if (S_ISLNK(sb.st_mode)) {
73 _LOGE("FAIL : %s is symlink!", dir);
78 ret = fchown(fd, uid, gid);
80 _LOGE("FAIL : fchown %s %d.%d, because %s", dir, uid,
81 gid, strerror_r(errno, buf, sizeof(buf)));
94 static char *_get_db_path(uid_t uid)
99 db_path = tzplatform_getenv(TZ_SYS_DB);
100 if (db_path == NULL) {
101 _LOGE("Failed to get TZ_SYS_DB path");
105 if (uid == GLOBAL_USER || uid == ROOT_UID)
106 return strdup(db_path);
108 snprintf(path, sizeof(path), "%s/user/%d", db_path, uid);
113 int _check_create_cert_db(void)
115 return pkgmgr_parser_initialize_cert_db();
118 static gid_t _get_gid(const char *name)
125 ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
126 if (ret || ge == NULL) {
127 _LOGE("fail to get gid of %s", name);
134 API const char *getIconPath(uid_t uid, bool readonly)
136 const char *path = NULL;
137 uid_t uid_caller = getuid();
138 gid_t gid = ROOT_UID;
140 if (uid != GLOBAL_USER && uid != ROOT_UID) {
141 _LOGD("not supported target user");
146 path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
148 /* just allow certain users to create the icon directory if needed. */
149 if (path && (uid_caller == ROOT_UID ||
150 uid_caller == APPFW_UID || uid_caller == uid))
151 _mkdir_for_user(path, uid, gid);
156 API char *getUserPkgParserDBPath(void)
158 return getUserPkgParserDBPathUID(_getuid());
161 API char *getUserPkgParserDBPathUID(uid_t uid)
163 char pkgmgr_parser_db[PATH_MAX];
164 uid_t uid_caller = getuid();
165 gid_t gid = ROOT_UID;
168 db_path = _get_db_path(uid);
169 if (db_path == NULL) {
170 _LOGE("Failed to get db path %d", uid);
173 snprintf(pkgmgr_parser_db, sizeof(pkgmgr_parser_db),
174 "%s/.pkgmgr_parser.db", db_path);
175 if (access(db_path, F_OK) != 0) {
176 if (uid != GLOBAL_USER && uid != ROOT_UID) {
177 tzplatform_set_user(uid);
178 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
179 tzplatform_reset_user();
181 /* just allow certain users to create the dbspace directory if needed. */
182 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID)
183 _mkdir_for_user(db_path, uid, gid);
187 return strdup(pkgmgr_parser_db);
190 API char *getUserPkgCertDBPath(void)
193 char pkgmgr_cert_db[PATH_MAX];
195 db_path = _get_db_path(GLOBAL_USER);
196 snprintf(pkgmgr_cert_db, sizeof(pkgmgr_cert_db),
197 "%s/.pkgmgr_cert.db", db_path);
200 return strdup(pkgmgr_cert_db);
203 API const char *getUserManifestPath(uid_t uid, bool readonly)
205 const char *path = NULL;
206 uid_t uid_caller = getuid();
207 gid_t gid = ROOT_UID;
209 if (uid != GLOBAL_USER && uid != ROOT_UID) {
210 tzplatform_set_user(uid);
211 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
212 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
213 tzplatform_reset_user();
216 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
218 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
221 /* just allow certain users to create the icon directory if needed. */
222 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
223 _mkdir_for_user(path, uid, gid);
228 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
230 *i = sqlite3_column_int(stmt, idx);
233 inline void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
237 val = (const char *)sqlite3_column_text(stmt, idx);
242 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path, uid_t uid)
244 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
246 sqlite3 *pkgmgr_parser_db = NULL;
249 const char *location_str;
251 db_path = getUserPkgParserDBPathUID(uid);
252 if (db_path == NULL) {
253 _LOGE("Failed to get pkg parser db path - %d", uid);
254 return PMINFO_R_ERROR;
257 ret = __open_db(db_path, &pkgmgr_parser_db, SQLITE_OPEN_READWRITE);
258 if (ret != SQLITE_OK) {
259 _LOGE("connect db failed!");
261 return PMINFO_R_ERROR;
265 /*Begin transaction*/
266 /* Setting Manifest DB */
267 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN DEFERRED", NULL, NULL, NULL);
268 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
269 _LOGD("Transaction Begin\n");
271 if (location == INSTALL_INTERNAL)
272 location_str = "installed_internal";
273 else if (location == INSTALL_EXTERNAL)
274 location_str = "installed_external";
276 location_str = "installed_extended";
277 /* pkgcakge_info table */
278 query = sqlite3_mprintf(
279 "update package_info set installed_storage=%Q, external_path=%Q where package=%Q",
280 location_str, external_pkg_path, pkgid);
282 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
283 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
286 /* package_app_info table */
287 query = sqlite3_mprintf(
288 "update package_app_info set app_installed_storage=%Q, app_external_path=%Q where package=%Q",
289 location_str, external_pkg_path, pkgid);
291 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
292 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
294 /*Commit transaction*/
295 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
296 if (ret != SQLITE_OK) {
297 _LOGE("Failed to commit transaction. Rollback now\n");
298 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
299 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
301 _LOGD("Transaction Commit and End\n");
305 sqlite3_close_v2(pkgmgr_parser_db);
310 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path)
312 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, external_pkg_path, _getuid());