14 #include <tzplatform_config.h>
16 #include "pkgmgr-info.h"
17 #include "pkgmgrinfo_debug.h"
18 #include "pkgmgrinfo_private.h"
19 #include "pkgmgr_parser.h"
20 #include "pkgmgr_parser_db.h"
21 #include "manager/pkginfo_manager.h"
23 typedef int (*sqlite_query_callback)(void *data, int ncols,
24 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",
82 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 static gid_t _get_gid(const char *name)
121 ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
122 if (ret || ge == NULL) {
123 _LOGE("fail to get gid of %s", name);
130 API const char *getIconPath(uid_t uid, bool readonly)
132 const char *path = NULL;
133 uid_t uid_caller = getuid();
134 gid_t gid = ROOT_UID;
136 if (uid != GLOBAL_USER && uid != ROOT_UID) {
137 _LOGD("not supported target user");
142 path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
144 /* just allow certain users to create the icon directory if needed. */
145 if (path && (uid_caller == ROOT_UID ||
146 uid_caller == APPFW_UID || uid_caller == uid))
147 _mkdir_for_user(path, uid, gid);
152 API char *getUserPkgParserDBPath(void)
154 return getUserPkgParserDBPathUID(_getuid());
157 API char *getUserPkgParserDBPathUID(uid_t uid)
159 char pkgmgr_parser_db[PATH_MAX];
160 uid_t uid_caller = getuid();
161 gid_t gid = ROOT_UID;
164 db_path = _get_db_path(uid);
165 if (db_path == NULL) {
166 _LOGE("Failed to get db path %d", uid);
169 snprintf(pkgmgr_parser_db, sizeof(pkgmgr_parser_db),
170 "%s/.pkgmgr_parser.db", db_path);
171 if (access(db_path, F_OK) != 0) {
172 if (uid != GLOBAL_USER && uid != ROOT_UID) {
173 tzplatform_set_user(uid);
174 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
175 tzplatform_reset_user();
177 /* just allow certain users to create the dbspace directory if needed. */
178 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID)
179 _mkdir_for_user(db_path, uid, gid);
183 return strdup(pkgmgr_parser_db);
186 API char *getUserPkgCertDBPath(void)
189 char pkgmgr_cert_db[PATH_MAX];
191 db_path = _get_db_path(GLOBAL_USER);
192 snprintf(pkgmgr_cert_db, sizeof(pkgmgr_cert_db),
193 "%s/.pkgmgr_cert.db", db_path);
196 return strdup(pkgmgr_cert_db);
199 API const char *getUserManifestPath(uid_t uid, bool readonly)
201 const char *path = NULL;
202 uid_t uid_caller = getuid();
203 gid_t gid = ROOT_UID;
205 if (uid != GLOBAL_USER && uid != ROOT_UID) {
206 tzplatform_set_user(uid);
207 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
208 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
209 tzplatform_reset_user();
212 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
214 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
217 /* just allow certain users to create the icon directory if needed. */
218 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID ||
220 _mkdir_for_user(path, uid, gid);
225 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid,
226 INSTALL_LOCATION location, const char *external_pkg_path,
229 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
230 return _pkginfo_set_usr_installed_storage(pkgid,
231 location, external_pkg_path, uid);
234 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid,
235 INSTALL_LOCATION location, const char *external_pkg_path)
237 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
238 location, external_pkg_path, _getuid());