Tizen 2.0 Release
[platform/core/appfw/ail.git] / initdb / src / initdb.c
1 /*
2  * ail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <dirent.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <errno.h>
31
32 #include "ail.h"
33 #include "ail_private.h"
34
35 #define OWNER_ROOT 0
36 #define GROUP_MENU 6010
37 #define BUFSZE 1024
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"
41
42 #ifdef _E
43 #undef _E
44 #endif
45 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
46
47 #ifdef _D
48 #undef _D
49 #endif
50 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
51
52 static int initdb_count_app(void)
53 {
54         ail_filter_h filter;
55         ail_error_e ret;
56         int total = 0;
57
58         ret = ail_filter_new(&filter);
59         if (ret != AIL_ERROR_OK) {
60                 return -1;
61         }
62
63         ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
64         if (ret != AIL_ERROR_OK) {
65                 ail_filter_destroy(filter);
66                 return -1;
67         }
68
69         ret = ail_filter_count_appinfo(filter, &total);
70         if (ret != AIL_ERROR_OK) {
71                 ail_filter_destroy(filter);
72                 return -1;
73         }
74
75         ail_filter_destroy(filter);
76
77         return total;
78 }
79
80
81
82 char* _desktop_to_package(const char* desktop)
83 {
84         char *package, *tmp;
85
86         retv_if(!desktop, NULL);
87
88         package = strdup(desktop);
89         retv_if(!package, NULL);
90
91         tmp = strrchr(package, '.');
92         if(tmp == NULL) {
93                 _E("(tmp == NULL) return\n");
94                 free(package);
95                 return NULL;
96         }
97
98         if (strcmp(tmp, ".desktop")) {
99                 _E("%s is not a desktop file", desktop);
100                 free(package);
101                 return NULL;
102         }
103
104         *tmp = '\0';
105
106         return package;
107 }
108
109
110
111 int initdb_load_directory(const char *directory)
112 {
113         DIR *dir;
114         struct dirent entry, *result;
115         int len, ret;
116         char buf[BUFSZE];
117
118         // desktop file
119         dir = opendir(directory);
120         if (!dir) {
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;
124         }
125
126         len = strlen(directory) + 1;
127         _D("Loading desktop files from %s\n", directory);
128
129         for (ret = readdir_r(dir, &entry, &result);
130                         ret == 0 && result != NULL;
131                         ret = readdir_r(dir, &entry, &result)) {
132                 char *package;
133
134                 if (entry.d_name[0] == '.') continue;
135
136                 package = _desktop_to_package(entry.d_name);
137                 if (!package) {
138                         _E("Failed to convert file to package[%s]\n", entry.d_name);
139                         continue;
140                 }
141
142                 if (ail_desktop_add(package) != AIL_ERROR_OK) {
143                         _E("Failed to add a package[%s]\n", package);
144                 }
145
146                 free(package);
147         }
148
149         closedir(dir);
150
151         return AIL_ERROR_OK;
152 }
153
154
155
156 static int initdb_change_perm(const char *db_file)
157 {
158         char buf[BUFSZE];
159         char journal_file[BUFSZE];
160         char *files[3];
161         int ret, i;
162
163         files[0] = (char *)db_file;
164         files[1] = journal_file;
165         files[2] = NULL;
166
167         retv_if(!db_file, AIL_ERROR_FAIL);
168
169         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
170
171         for (i = 0; files[i]; i++) {
172                 ret = chown(files[i], OWNER_ROOT, GROUP_MENU);
173                 if (ret == -1) {
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;
177                 }
178
179                 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
180                 if (ret == -1) {
181                         strerror_r(errno, buf, sizeof(buf));
182                         _E("FAIL : chmod %s 0664, because %s", db_file, buf);
183                         return AIL_ERROR_FAIL;
184                 }
185         }
186
187         return AIL_ERROR_OK;
188 }
189
190
191 static int __is_authorized()
192 {
193         /* ail_init db should be called by as root privilege. */
194
195         uid_t uid = getuid();
196         if ((uid_t) 0 == uid)
197                 return 1;
198         else
199                 return 0;
200 }
201
202
203 int main(int argc, char *argv[])
204 {
205         int ret;
206
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;
211         }
212
213         ret = setenv("AIL_INITDB", "1", 1);
214         _D("AIL_INITDB : %d", ret);
215
216         ret = initdb_count_app();
217         if (ret > 0) {
218                 _D("Some Apps in the App Info DB.");
219                 return AIL_ERROR_OK;
220         }
221
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;
226         }
227
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;
232         }
233
234         ret = initdb_change_perm(APP_INFO_DB_FILE);
235         if (ret == AIL_ERROR_FAIL) {
236                 _E("cannot chown.");
237                 return AIL_ERROR_FAIL;
238         }
239
240         return AIL_ERROR_OK;
241 }
242
243
244
245 // END