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 "manager/pkginfo_manager.h"
22 typedef int (*sqlite_query_callback)(void *data, int ncols,
23 char **coltxt, char **colname);
25 static int _mkdir_for_user(const char *dir, uid_t uid, gid_t gid)
34 fullpath = strdup(dir);
37 subpath = dirname(fullpath);
38 if (strlen(subpath) > 1 && strcmp(subpath, fullpath) != 0) {
39 ret = _mkdir_for_user(fullpath, uid, gid);
46 ret = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
47 if (ret && errno != EEXIST) {
50 } else if (ret && errno == EEXIST) {
55 if (getuid() == ROOT_UID) {
56 fd = open(dir, O_RDONLY);
58 _LOGE("FAIL : open %s : %s", dir,
59 strerror_r(errno, buf, sizeof(buf)));
65 _LOGE("FAIL : fstat %s : %s", dir,
66 strerror_r(errno, buf, sizeof(buf)));
71 if (S_ISLNK(sb.st_mode)) {
72 _LOGE("FAIL : %s is symlink!", dir);
77 ret = fchown(fd, uid, gid);
79 _LOGE("FAIL : fchown %s %d.%d, because %s",
81 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 static gid_t _get_gid(const char *name)
120 ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
121 if (ret || ge == NULL) {
122 _LOGE("fail to get gid of %s", name);
129 API const char *getIconPath(uid_t uid, bool readonly)
131 const char *path = NULL;
132 uid_t uid_caller = getuid();
133 gid_t gid = ROOT_UID;
135 if (uid != GLOBAL_USER && uid != ROOT_UID) {
136 _LOGD("not supported target user");
141 path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
143 /* just allow certain users to create the icon directory if needed. */
144 if (path && (uid_caller == ROOT_UID ||
145 uid_caller == APPFW_UID || uid_caller == uid))
146 _mkdir_for_user(path, uid, gid);
151 API char *getUserPkgParserDBPath(void)
153 return getUserPkgParserDBPathUID(_getuid());
156 API char *getUserPkgParserDBPathUID(uid_t uid)
158 char pkgmgr_parser_db[PATH_MAX];
159 uid_t uid_caller = getuid();
160 gid_t gid = ROOT_UID;
163 db_path = _get_db_path(uid);
164 if (db_path == NULL) {
165 _LOGE("Failed to get db path %d", uid);
168 snprintf(pkgmgr_parser_db, sizeof(pkgmgr_parser_db),
169 "%s/.pkgmgr_parser.db", db_path);
170 if (access(db_path, F_OK) != 0) {
171 if (uid != GLOBAL_USER && uid != ROOT_UID) {
172 tzplatform_set_user(uid);
173 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
174 tzplatform_reset_user();
176 /* just allow certain users to create the dbspace directory if needed. */
177 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID)
178 _mkdir_for_user(db_path, uid, gid);
182 return strdup(pkgmgr_parser_db);
185 API char *getUserPkgCertDBPath(void)
188 char pkgmgr_cert_db[PATH_MAX];
190 db_path = _get_db_path(GLOBAL_USER);
191 snprintf(pkgmgr_cert_db, sizeof(pkgmgr_cert_db),
192 "%s/.pkgmgr_cert.db", db_path);
195 return strdup(pkgmgr_cert_db);
198 API const char *getUserManifestPath(uid_t uid, bool readonly)
200 const char *path = NULL;
201 uid_t uid_caller = getuid();
202 gid_t gid = ROOT_UID;
204 if (uid != GLOBAL_USER && uid != ROOT_UID) {
205 tzplatform_set_user(uid);
206 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
207 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
208 tzplatform_reset_user();
211 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
213 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
216 /* just allow certain users to create the icon directory if needed. */
217 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID ||
219 _mkdir_for_user(path, uid, gid);
224 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid,
225 INSTALL_LOCATION location, const char *external_pkg_path,
228 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
229 return _pkginfo_set_usr_installed_storage(pkgid,
230 location, external_pkg_path, uid);
233 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid,
234 INSTALL_LOCATION location, const char *external_pkg_path)
236 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
237 location, external_pkg_path, _getuid());