tizen 2.4 release
[framework/appfw/app2sd.git] / src / app2ext_interface.c
1 /*
2  * app2ext
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jyotsna Dhumale <jyotsna.a@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 #include <app2ext_interface.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <dlfcn.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <dirent.h>
31
32 #define APP2EXT_SD_PLUGIN_PATH  LIBPREFIX "/libapp2sd.so"
33
34 app2ext_handle *app2ext_init(int storage_type)
35 {
36         /*Validate the function parameter recieved */
37         if (storage_type < APP2EXT_INTERNAL_MEM ||
38                 storage_type > APP2EXT_CLOUD) {
39                 app2ext_print("App2Ext Error : Invalid function arguments\n");
40                 return NULL;
41         }
42         if (storage_type != APP2EXT_SD_CARD ) {
43                 app2ext_print("App2Ext Error : Storage type currently not supported\n");
44                 return NULL;
45         }
46
47         /* allocate memory for app2ext handle*/
48         app2ext_handle *handle = (app2ext_handle *)calloc(1, sizeof(app2ext_handle));
49         if (handle == NULL) {
50                 app2ext_print("App2Ext Error : Memory allocation failure\n");
51                 return NULL;
52         }
53         void *dl_handle = NULL;
54         int (*dl_on_load)(app2ext_interface *)=NULL;
55
56         /* Load SD plugin*/
57         handle->type = APP2EXT_SD_CARD;
58         dl_handle = dlopen(APP2EXT_SD_PLUGIN_PATH, RTLD_LAZY|RTLD_GLOBAL);
59         if (NULL == dl_handle)
60         {
61                 app2ext_print("App2Ext Error : dlopen(%s) failed.\n", APP2EXT_SD_PLUGIN_PATH);
62                 free(handle);
63                 return NULL;
64         }
65         handle->plugin_handle = dl_handle;
66         dl_on_load = dlsym(dl_handle, "app2ext_on_load");
67         if (NULL == dl_on_load)
68         {
69                 app2ext_print("App2Ext Error : Cannot find app2ext_on_load symbol in %s.", APP2EXT_SD_PLUGIN_PATH);
70                 dlclose(dl_handle);
71                 free(handle);
72                 return NULL;
73         }
74
75         /*Initialize the SD plugin*/
76         if(!dl_on_load(&(handle->interface)))
77         {
78                 app2ext_print("App2Ext Error : [%s] app2ext_on_load() failed.", APP2EXT_SD_PLUGIN_PATH);
79                 dlclose(dl_handle);
80                 free(handle);
81                 return NULL;
82         }
83
84         app2ext_print("App2Ext: %s plugin loaded\n", APP2EXT_SD_PLUGIN_PATH);
85
86         return handle;
87 }
88
89 int app2ext_deinit(app2ext_handle *handle)
90 {
91         /*Validate the function parameter recieved */
92         if (handle == NULL || handle->plugin_handle == NULL){
93                 app2ext_print("App2Ext Error : Invalid function arguments\n");
94                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
95         }
96
97         /* Close the plugin handle*/
98         dlclose(handle->plugin_handle);
99
100         /* Free allocated memory during installtion*/
101         free(handle);
102         return APP2EXT_SUCCESS;
103 }
104
105 int app2ext_get_app_location(const char *appname)
106 {
107         /*Validate the function parameter received */
108         if (appname == NULL) {
109                 app2ext_print("invalid func parameters\n");
110                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
111         }
112         FILE *fp = NULL;
113         char app_mmc_path[FILENAME_MAX] = { 0, };
114         char app_dir_path[FILENAME_MAX] = { 0, };
115         char app_mmc_internal_path[FILENAME_MAX] = { 0, };
116         snprintf(app_dir_path, FILENAME_MAX,
117         "%s%s", APP_INSTALLATION_PATH, appname);
118         snprintf(app_mmc_path, FILENAME_MAX,
119         "%s%s", APP2SD_PATH, appname);
120         snprintf(app_mmc_internal_path, FILENAME_MAX,
121         "%s%s/.mmc", APP_INSTALLATION_PATH, appname);
122
123
124         /*check whether application is in external memory or not */
125         fp = fopen(app_mmc_path, "r");
126         if (fp != NULL) {
127                 fclose(fp);
128                 fp = NULL;
129                 return APP2EXT_SD_CARD;
130         }
131
132         /*check whether application is in internal or not */
133         fp = fopen(app_dir_path, "r");
134         if (fp == NULL) {
135                 return APP2EXT_NOT_INSTALLED;
136         } else {
137                 fclose(fp);
138                 /*check whether the application is installed in SD card
139                         but SD card is not present*/
140                 fp = fopen(app_mmc_internal_path, "r");
141                 if (fp == NULL) {
142                         return APP2EXT_INTERNAL_MEM;
143                 } else {
144                         fclose(fp);
145                         return APP2EXT_ERROR_MMC_STATUS;
146                 }
147         }
148 }
149
150 int app2ext_enable_external_pkg(const char *pkgid)
151 {
152         /*Validate the function parameter received */
153         if (pkgid == NULL) {
154                 app2ext_print("invalid func parameters\n");
155                 return -1;
156         }
157         FILE *fp = NULL;
158         app2ext_handle *app2_handle = NULL;
159         char app_mmc_path[FILENAME_MAX] = { 0, };
160         snprintf(app_mmc_path, FILENAME_MAX, "%s%s", APP2SD_PATH, pkgid);
161
162         /*check whether application is in external memory or not */
163         fp = fopen(app_mmc_path, "r");
164         if (fp != NULL) {
165                 fclose(fp);
166                 fp = NULL;
167
168                 app2_handle = app2ext_init(APP2EXT_SD_CARD);
169                 if (app2_handle == NULL) {
170                         app2ext_print("app2_handle : app2ext init failed\n");
171                         return -2;
172                 }
173
174                 app2_handle->interface.enable(pkgid);
175                 app2ext_deinit(app2_handle);
176                 app2ext_print("App2Ext enable_external_pkg [%s]\n", pkgid);
177         }
178         return 0;
179 }
180
181 int app2ext_disable_external_pkg(const char *pkgid)
182 {
183         /*Validate the function parameter received */
184         if (pkgid == NULL) {
185                 app2ext_print("invalid func parameters\n");
186                 return -1;
187         }
188         FILE *fp = NULL;
189         app2ext_handle *app2_handle = NULL;
190         char app_mmc_path[FILENAME_MAX] = { 0, };
191         snprintf(app_mmc_path, FILENAME_MAX, "%s%s", APP2SD_PATH, pkgid);
192
193         /*check whether application is in external memory or not */
194         fp = fopen(app_mmc_path, "r");
195         if (fp != NULL) {
196                 fclose(fp);
197                 fp = NULL;
198
199                 app2_handle = app2ext_init(APP2EXT_SD_CARD);
200                 if (app2_handle == NULL) {
201                         app2ext_print("app2_handle : app2ext init failed\n");
202                         return -2;
203                 }
204
205                 app2_handle->interface.disable(pkgid);
206                 app2ext_deinit(app2_handle);
207                 app2ext_print("App2Ext disable_external_pkg [%s]\n", pkgid);
208         }
209         return 0;
210 }
211
212 int app2ext_enable_external_dir(void)
213 {
214         int ret = 0;
215         DIR *dir = NULL;
216         char buf[FILENAME_MAX] = { 0, };
217         struct dirent entry, *result = NULL;
218
219         dir = opendir(APP2SD_PATH);
220         if (!dir) {
221                 if (strerror_r(errno, buf, sizeof(buf)) == 0)
222                         app2ext_print("App2Ext Error :Failed to access the because %s", buf);
223                 return -1;
224         }
225
226         for (ret = readdir_r(dir, &entry, &result); ret == 0 && result != NULL; ret = readdir_r(dir, &entry, &result)) {
227                 ret = app2ext_enable_external_pkg(entry.d_name);
228                 if (ret != 0)
229                         app2ext_print("App2Ext Error : fail enable pkg[%s]\n", entry.d_name);
230         }
231
232         closedir(dir);
233         return 0;
234 }
235
236 int app2ext_disable_external_dir(void)
237 {
238         int ret = 0;
239         DIR *dir = NULL;
240         char buf[FILENAME_MAX] = { 0, };
241         struct dirent entry, *result = NULL;
242
243         dir = opendir(APP2SD_PATH);
244         if (!dir) {
245                 if (strerror_r(errno, buf, sizeof(buf)) == 0)
246                         app2ext_print("App2Ext Error :Failed to access the because %s", buf);
247                 return -1;
248         }
249
250         for (ret = readdir_r(dir, &entry, &result); ret == 0 && result != NULL; ret = readdir_r(dir, &entry, &result)) {
251                 ret = app2ext_disable_external_pkg(entry.d_name);
252                 if (ret != 0)
253                         app2ext_print("App2Ext Error : fail disable pkg[%s]\n", entry.d_name);
254         }
255
256         closedir(dir);
257         return 0;
258 }
259
260 int app2ext_force_clean_pkg(const char *pkgid)
261 {
262         /*Validate the function parameter received */
263         if (pkgid == NULL) {
264                 app2ext_print("invalid func parameters\n");
265                 return 0;
266         }
267
268         FILE *fp = NULL;
269         app2ext_handle *app2_handle = NULL;
270         char app_mmc_path[FILENAME_MAX] = { 0, };
271         snprintf(app_mmc_path, FILENAME_MAX, "%s%s/.mmc", APP_INSTALLATION_PATH, pkgid);
272
273         fp = fopen(app_mmc_path, "r");
274         if (fp == NULL) {
275                 return 0;
276         } else {
277                 fclose(fp);
278         }
279
280         app2_handle = app2ext_init(APP2EXT_SD_CARD);
281         if (app2_handle == NULL) {
282                 app2ext_print("app2_handle : app2ext init failed\n");
283                 return 0;
284         }
285
286         app2_handle->interface.force_clean(pkgid);
287         app2ext_deinit(app2_handle);
288         app2ext_print("App2Ext force_clean_pkg [%s]\n", pkgid);
289         return 0;
290 }
291