16 #include <tzplatform_config.h>
19 #include "pkgmgr-info.h"
20 #include "pkgmgrinfo_debug.h"
21 #include "pkgmgrinfo_private.h"
22 #include "pkgmgr_parser.h"
23 #include "pkgmgr_parser_db.h"
25 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
27 static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid)
36 fullpath = strdup(dir);
39 subpath = dirname(fullpath);
40 if (strlen(subpath) > 1 && strcmp(subpath, fullpath) != 0) {
41 ret = _mkdir_for_user(fullpath, uid, gid);
48 ret = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
49 if (ret && errno != EEXIST) {
52 } else if (ret && errno == EEXIST) {
57 if (getuid() == ROOT_UID) {
58 fd = open(dir, O_RDONLY);
60 _LOGE("FAIL : open %s : %s", dir,
61 strerror_r(errno, buf, sizeof(buf)));
67 _LOGE("FAIL : fstat %s : %s", dir,
68 strerror_r(errno, buf, sizeof(buf)));
73 if (S_ISLNK(sb.st_mode)) {
74 _LOGE("FAIL : %s is symlink!", dir);
79 ret = fchown(fd, uid, gid);
81 _LOGE("FAIL : fchown %s %d.%d, because %s", dir, uid,
82 gid, strerror_r(errno, buf, sizeof(buf)));
95 static char *_get_db_path(uid_t uid)
100 db_path = tzplatform_getenv(TZ_SYS_DB);
101 if (db_path == NULL) {
102 _LOGE("Failed to get TZ_SYS_DB path");
106 if (uid == GLOBAL_USER || uid == ROOT_UID)
107 return strdup(db_path);
109 snprintf(path, sizeof(path), "%s/user/%d", db_path, uid);
114 int _check_create_cert_db(void)
116 return pkgmgr_parser_initialize_cert_db();
119 static gid_t _get_gid(const char *name)
126 ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
127 if (ret || ge == NULL) {
128 _LOGE("fail to get gid of %s", name);
135 API const char *getIconPath(uid_t uid, bool readonly)
137 const char *path = NULL;
138 uid_t uid_caller = getuid();
139 gid_t gid = ROOT_UID;
141 if (uid != GLOBAL_USER && uid != ROOT_UID) {
142 _LOGD("not supported target user");
147 path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
149 /* just allow certain users to create the icon directory if needed. */
150 if (path && (uid_caller == ROOT_UID ||
151 uid_caller == APPFW_UID || uid_caller == uid))
152 _mkdir_for_user(path, uid, gid);
157 API char *getUserPkgParserDBPath(void)
159 return getUserPkgParserDBPathUID(_getuid());
162 API char *getUserPkgParserDBPathUID(uid_t uid)
164 char pkgmgr_parser_db[PATH_MAX];
165 uid_t uid_caller = getuid();
166 gid_t gid = ROOT_UID;
169 db_path = _get_db_path(uid);
170 if (db_path == NULL) {
171 _LOGE("Failed to get db path %d", uid);
174 snprintf(pkgmgr_parser_db, sizeof(pkgmgr_parser_db),
175 "%s/.pkgmgr_parser.db", db_path);
176 if (access(db_path, F_OK) != 0) {
177 if (uid != GLOBAL_USER && uid != ROOT_UID) {
178 tzplatform_set_user(uid);
179 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
180 tzplatform_reset_user();
182 /* just allow certain users to create the dbspace directory if needed. */
183 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID ||
185 _mkdir_for_user(db_path, uid, gid);
189 return strdup(pkgmgr_parser_db);
192 API char *getUserPkgCertDBPath(void)
194 return getUserPkgCertDBPathUID(_getuid());
197 API char *getUserPkgCertDBPathUID(uid_t uid)
199 char pkgmgr_cert_db[PATH_MAX];
200 uid_t uid_caller = getuid();
201 gid_t gid = ROOT_UID;
204 db_path = _get_db_path(uid);
205 if (db_path == NULL) {
206 _LOGE("Failed to get db path %d", uid);
209 snprintf(pkgmgr_cert_db, sizeof(pkgmgr_cert_db),
210 "%s/.pkgmgr_cert.db", db_path);
212 if (uid != GLOBAL_USER && uid != ROOT_UID) {
213 tzplatform_set_user(uid);
214 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
215 tzplatform_reset_user();
218 /* just allow certain users to create the dbspace directory if needed. */
219 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
220 _mkdir_for_user(db_path, uid, gid);
224 return strdup(pkgmgr_cert_db);
227 API const char *getUserManifestPath(uid_t uid, bool readonly)
229 const char *path = NULL;
230 uid_t uid_caller = getuid();
231 gid_t gid = ROOT_UID;
233 if (uid != GLOBAL_USER && uid != ROOT_UID) {
234 tzplatform_set_user(uid);
235 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
236 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
237 tzplatform_reset_user();
240 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
242 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
245 /* just allow certain users to create the icon directory if needed. */
246 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
247 _mkdir_for_user(path, uid, gid);
252 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
254 *i = sqlite3_column_int(stmt, idx);
257 inline void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
261 val = (const char *)sqlite3_column_text(stmt, idx);
266 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path, uid_t uid)
268 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
270 sqlite3 *pkgmgr_parser_db = NULL;
273 const char *location_str;
275 db_path = getUserPkgParserDBPathUID(uid);
276 if (db_path == NULL) {
277 _LOGE("Failed to get pkg parser db path - %d", uid);
278 return PMINFO_R_ERROR;
281 ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
282 SQLITE_OPEN_READWRITE, NULL);
283 if (ret != SQLITE_OK) {
284 _LOGE("connect db failed!");
286 return PMINFO_R_ERROR;
290 /*Begin transaction*/
291 /* Setting Manifest DB */
292 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
293 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
294 _LOGD("Transaction Begin\n");
296 if (location == INSTALL_INTERNAL)
297 location_str = "installed_internal";
298 else if (location == INSTALL_EXTERNAL)
299 location_str = "installed_external";
301 location_str = "installed_extended";
302 /* pkgcakge_info table */
303 query = sqlite3_mprintf(
304 "update package_info set installed_storage=%Q, external_path=%Q where package=%Q",
305 location_str, external_pkg_path, pkgid);
307 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
308 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
311 /* package_app_info table */
312 query = sqlite3_mprintf(
313 "update package_app_info set app_installed_storage=%Q, app_external_path=%Q where package=%Q",
314 location_str, external_pkg_path, pkgid);
316 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
317 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
319 /*Commit transaction*/
320 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
321 if (ret != SQLITE_OK) {
322 _LOGE("Failed to commit transaction. Rollback now\n");
323 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
324 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
326 _LOGD("Transaction Commit and End\n");
330 sqlite3_close(pkgmgr_parser_db);
335 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path)
337 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, external_pkg_path, _getuid());