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"
23 #include "manager/pkginfo_manager.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)
184 _mkdir_for_user(db_path, uid, gid);
188 return strdup(pkgmgr_parser_db);
191 API char *getUserPkgCertDBPath(void)
194 char pkgmgr_cert_db[PATH_MAX];
196 db_path = _get_db_path(GLOBAL_USER);
197 snprintf(pkgmgr_cert_db, sizeof(pkgmgr_cert_db),
198 "%s/.pkgmgr_cert.db", db_path);
201 return strdup(pkgmgr_cert_db);
204 API const char *getUserManifestPath(uid_t uid, bool readonly)
206 const char *path = NULL;
207 uid_t uid_caller = getuid();
208 gid_t gid = ROOT_UID;
210 if (uid != GLOBAL_USER && uid != ROOT_UID) {
211 tzplatform_set_user(uid);
212 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
213 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
214 tzplatform_reset_user();
217 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
219 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
222 /* just allow certain users to create the icon directory if needed. */
223 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
224 _mkdir_for_user(path, uid, gid);
229 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
231 *i = sqlite3_column_int(stmt, idx);
234 inline void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
238 val = (const char *)sqlite3_column_text(stmt, idx);
243 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path, uid_t uid)
245 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
246 return _pkginfo_set_usr_installed_storage(pkgid, location, external_pkg_path, uid);
249 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path)
251 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, external_pkg_path, _getuid());