4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
33 #include "ail_private.h"
36 #define GROUP_MENU 6010
38 #define OPT_DESKTOP_DIRECTORY "/opt/share/applications"
39 #define USR_DESKTOP_DIRECTORY "/usr/share/applications"
40 #define APP_INFO_DB_FILE "/opt/dbspace/.app_info.db"
45 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
50 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
52 static int initdb_count_app(void)
58 ret = ail_filter_new(&filter);
59 if (ret != AIL_ERROR_OK) {
63 ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
64 if (ret != AIL_ERROR_OK) {
65 ail_filter_destroy(filter);
69 ret = ail_filter_count_appinfo(filter, &total);
70 if (ret != AIL_ERROR_OK) {
71 ail_filter_destroy(filter);
75 ail_filter_destroy(filter);
82 char* _desktop_to_package(const char* desktop)
86 retv_if(!desktop, NULL);
88 package = strdup(desktop);
89 retv_if(!package, NULL);
91 tmp = strrchr(package, '.');
93 _E("(tmp == NULL) return\n");
98 if (strcmp(tmp, ".desktop")) {
99 _E("%s is not a desktop file", desktop);
111 int initdb_load_directory(const char *directory)
114 struct dirent entry, *result;
119 dir = opendir(directory);
121 if (strerror_r(errno, buf, sizeof(buf)) == 0)
122 _E("Failed to access the [%s] because %s\n", directory, buf);
123 return AIL_ERROR_FAIL;
126 len = strlen(directory) + 1;
127 _D("Loading desktop files from %s\n", directory);
129 for (ret = readdir_r(dir, &entry, &result);
130 ret == 0 && result != NULL;
131 ret = readdir_r(dir, &entry, &result)) {
134 if (entry.d_name[0] == '.') continue;
136 package = _desktop_to_package(entry.d_name);
138 _E("Failed to convert file to package[%s]\n", entry.d_name);
142 if (ail_desktop_add(package) != AIL_ERROR_OK) {
143 _E("Failed to add a package[%s]\n", package);
156 static int initdb_change_perm(const char *db_file)
159 char journal_file[BUFSZE];
163 files[0] = (char *)db_file;
164 files[1] = journal_file;
167 retv_if(!db_file, AIL_ERROR_FAIL);
169 snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
171 for (i = 0; files[i]; i++) {
172 ret = chown(files[i], OWNER_ROOT, GROUP_MENU);
174 strerror_r(errno, buf, sizeof(buf));
175 _E("FAIL : chown %s %d.%d, because %s", db_file, OWNER_ROOT, GROUP_MENU, buf);
176 return AIL_ERROR_FAIL;
179 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
181 strerror_r(errno, buf, sizeof(buf));
182 _E("FAIL : chmod %s 0664, because %s", db_file, buf);
183 return AIL_ERROR_FAIL;
191 static int __is_authorized()
193 /* ail_init db should be called by as root privilege. */
195 uid_t uid = getuid();
196 if ((uid_t) 0 == uid)
203 int main(int argc, char *argv[])
207 if (!__is_authorized()) {
208 fprintf(stderr, "You are not an authorized user!\n");
209 _D("You are not an authorized user!\n");
210 return AIL_ERROR_FAIL;
213 ret = setenv("AIL_INITDB", "1", 1);
214 _D("AIL_INITDB : %d", ret);
216 ret = initdb_count_app();
218 _D("Some Apps in the App Info DB.");
222 ret = initdb_load_directory(OPT_DESKTOP_DIRECTORY);
223 if (ret == AIL_ERROR_FAIL) {
224 _E("cannot load opt desktop directory.");
225 return AIL_ERROR_FAIL;
228 ret = initdb_load_directory(USR_DESKTOP_DIRECTORY);
229 if (ret == AIL_ERROR_FAIL) {
230 _E("cannot load usr desktop directory.");
231 return AIL_ERROR_FAIL;
234 ret = initdb_change_perm(APP_INFO_DB_FILE);
235 if (ret == AIL_ERROR_FAIL) {
237 return AIL_ERROR_FAIL;