sync with master, change mkfs path
[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
31 #define APP2EXT_SD_PLUGIN_PATH  LIBPREFIX "/libapp2sd.so"
32
33 app2ext_handle *app2ext_init(int storage_type)
34 {
35         /*Validate the function parameter recieved */
36         if (storage_type < APP2EXT_INTERNAL_MEM ||
37                 storage_type > APP2EXT_CLOUD) {
38                 app2ext_print("App2Ext Error : Invalid function arguments\n");
39                 return NULL;
40         }
41         if (storage_type != APP2EXT_SD_CARD ) {
42                 app2ext_print("App2Ext Error : Storage type currently not supported\n");
43                 return NULL;
44         }
45
46         /* allocate memory for app2ext handle*/
47         app2ext_handle *handle = (app2ext_handle *)calloc(1, sizeof(app2ext_handle));
48         if (handle == NULL) {
49                 app2ext_print("App2Ext Error : Memory allocation failure\n");
50                 return NULL;
51         }
52         void *dl_handle = NULL;
53         int (*dl_on_load)(app2ext_interface *)=NULL;
54
55         /* Load SD plugin*/
56         handle->type = APP2EXT_SD_CARD;
57         dl_handle = dlopen(APP2EXT_SD_PLUGIN_PATH, RTLD_LAZY|RTLD_GLOBAL);
58         if (NULL == dl_handle)
59         {
60                 app2ext_print("App2Ext Error : dlopen(%s) failed.\n", APP2EXT_SD_PLUGIN_PATH);
61                 free(handle);
62                 return NULL;
63         }
64         handle->plugin_handle = dl_handle;
65         dl_on_load = dlsym(dl_handle, "app2ext_on_load");
66         if (NULL == dl_on_load)
67         {
68                 app2ext_print("App2Ext Error : Cannot find app2ext_on_load symbol in %s.", APP2EXT_SD_PLUGIN_PATH);
69                 dlclose(dl_handle);
70                 free(handle);
71                 return NULL;
72         }
73
74         /*Initialize the SD plugin*/
75         if(!dl_on_load(&(handle->interface)))
76         {
77                 app2ext_print("App2Ext Error : [%s] app2ext_on_load() failed.", APP2EXT_SD_PLUGIN_PATH);
78                 dlclose(dl_handle);
79                 free(handle);
80                 return NULL;
81         }
82
83         app2ext_print("App2Ext: %s plugin loaded\n", APP2EXT_SD_PLUGIN_PATH);
84
85         return handle;
86 }
87
88 int app2ext_deinit(app2ext_handle *handle)
89 {
90         /*Validate the function parameter recieved */
91         if (handle == NULL || handle->plugin_handle == NULL){
92                 app2ext_print("App2Ext Error : Invalid function arguments\n");
93                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
94         }
95
96         /* Close the plugin handle*/
97         dlclose(handle->plugin_handle);
98
99         /* Free allocated memory during installtion*/
100         free(handle);
101         return APP2EXT_SUCCESS;
102 }
103
104 int app2ext_get_app_location(const char *appname)
105 {
106         /*Validate the function parameter received */
107         if (appname == NULL) {
108                 app2ext_print("invalid func parameters\n");
109                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
110         }
111         FILE *fp = NULL;
112         char app_mmc_path[FILENAME_MAX] = { 0, };
113         char app_dir_path[FILENAME_MAX] = { 0, };
114         char app_mmc_internal_path[FILENAME_MAX] = { 0, };
115         snprintf(app_dir_path, FILENAME_MAX,
116         "%s%s", APP_INSTALLATION_PATH, appname);
117         snprintf(app_mmc_path, FILENAME_MAX,
118         "%s%s", APP2SD_PATH, appname);
119         snprintf(app_mmc_internal_path, FILENAME_MAX,
120         "%s%s/.mmc", APP_INSTALLATION_PATH, appname);
121
122
123         /*check whether application is in external memory or not */
124         fp = fopen(app_mmc_path, "r");
125         if (fp == NULL) {
126                 app2ext_print
127                 (" app path in external memory not accesible\n");
128         } else {
129                 fclose(fp);
130                 fp = NULL;
131                 return APP2EXT_SD_CARD;
132         }
133
134         /*check whether application is in internal or not */
135         fp = fopen(app_dir_path, "r");
136         if (fp == NULL) {
137                 app2ext_print
138                 (" app path in internal memory not accesible\n");
139                 return APP2EXT_NOT_INSTALLED;
140         } else {
141                 fclose(fp);
142                 /*check whether the application is installed in SD card
143                         but SD card is not present*/
144                 fp = fopen(app_mmc_internal_path, "r");
145                 if (fp == NULL) {
146                         return APP2EXT_INTERNAL_MEM;
147                 } else {
148                         fclose(fp);
149                         return APP2EXT_ERROR_MMC_STATUS;
150                 }
151         }
152 }
153
154 int app2ext_enable_external_pkg(const char *pkgid)
155 {
156         /*Validate the function parameter received */
157         if (pkgid == NULL) {
158                 app2ext_print("invalid func parameters\n");
159                 return -1;
160         }
161         FILE *fp = NULL;
162         app2ext_handle *app2_handle = NULL;
163         char app_mmc_path[FILENAME_MAX] = { 0, };
164         snprintf(app_mmc_path, FILENAME_MAX, "%s%s", APP2SD_PATH, pkgid);
165
166         /*check whether application is in external memory or not */
167         fp = fopen(app_mmc_path, "r");
168         if (fp != NULL) {
169                 fclose(fp);
170                 fp = NULL;
171
172                 app2_handle = app2ext_init(APP2EXT_SD_CARD);
173                 if (app2_handle == NULL) {
174                         app2ext_print("app2_handle : app2ext init failed\n");
175                         return -2;
176                 }
177
178                 app2_handle->interface.enable(pkgid);
179                 app2ext_deinit(app2_handle);
180         }
181         return 0;
182 }
183
184 int app2ext_disable_external_pkg(const char *pkgid)
185 {
186         /*Validate the function parameter received */
187         if (pkgid == NULL) {
188                 app2ext_print("invalid func parameters\n");
189                 return -1;
190         }
191         FILE *fp = NULL;
192         app2ext_handle *app2_handle = NULL;
193         char app_mmc_path[FILENAME_MAX] = { 0, };
194         snprintf(app_mmc_path, FILENAME_MAX, "%s%s", APP2SD_PATH, pkgid);
195
196         /*check whether application is in external memory or not */
197         fp = fopen(app_mmc_path, "r");
198         if (fp != NULL) {
199                 fclose(fp);
200                 fp = NULL;
201
202                 app2_handle = app2ext_init(APP2EXT_SD_CARD);
203                 if (app2_handle == NULL) {
204                         app2ext_print("app2_handle : app2ext init failed\n");
205                         return -2;
206                 }
207
208                 app2_handle->interface.disable(pkgid);
209                 app2ext_deinit(app2_handle);
210         }
211         return 0;
212 }