sync with private master
[platform/core/appfw/app2sd.git] / plugin / app2sd / src / app2sd_interface.c
1 /*
2  * app2ext
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Garima Shrivastava<garima.s@samsung.com>
7  *      Jyotsna Dhumale <jyotsna.a@samsung.com>
8  *      Venkatesha Sarpangala <sarpangala.v@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <app2sd_internals.h>
25 #include <app2sd_interface.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <pkgmgr-info.h>
30 #include <vconf.h>
31
32 #define MAX_BUF_LEN     1024
33 #define APP2SD_TMP_PATH "/opt/usr/apps/tmp"
34
35 int app2sd_pre_app_install(const char *pkgid, GList* dir_list,
36                                 int size)
37 {
38         int ret = 0;
39         int free_mmc_mem = 0;
40         char *device_node = NULL;
41         char *devi = NULL;
42         char *result = NULL;
43
44         /*Validate the function parameter recieved */
45         if (pkgid == NULL || dir_list == NULL || size <= 0) {
46                 app2ext_print("App2Sd Error : Invalid function arguments\n");
47                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
48         }
49         /*Check whether MMC is present or not */
50         ret = _app2sd_check_mmc_status();
51         if (ret) {
52                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
53                         ret);
54                 return APP2EXT_ERROR_MMC_STATUS;
55         }
56         /*Find available free memory in the MMC card */
57         ret = _app2sd_get_available_free_memory(MMC_PATH,
58                                                 &free_mmc_mem);
59         if (ret) {
60                 app2ext_print("App2Sd Error : Unable to get available free memory in MMC %d\n", ret);
61                 return APP2EXT_ERROR_MMC_STATUS;
62         }
63         /*If avaialalbe free memory in MMC is less than required size + 5MB , return error */
64         if ((size + PKG_BUF_SIZE + MEM_BUF_SIZE) > free_mmc_mem) {
65                 app2ext_print("Insufficient memory in MMC for application installation %d\n", ret);
66                 return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
67         }
68         /*Create a loopback device */
69         ret = _app2sd_create_loopback_device(pkgid, (size+PKG_BUF_SIZE));
70         if (ret) {
71                 app2ext_print("App2Sd Error : Package already present\n");
72                 char buf_dir[FILENAME_MAX] = { 0, };
73                 memset((void *)&buf_dir, '\0', FILENAME_MAX);
74                 snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
75                 ret = _app2sd_delete_directory(buf_dir);
76                 if (ret) {
77                         app2ext_print
78                                 ("App2Sd Error : Unable to delete the directory %s\n",
79                                  buf_dir);
80                 }
81         }
82         /*Perform Loopback encryption setup */
83         device_node = _app2sd_do_loopback_encryption_setup(pkgid);
84         if (!device_node) {
85                 app2ext_print("App2Sd Error : Loopback encryption setup failed\n");
86                 _app2sd_delete_loopback_device(pkgid);
87                 return APP2EXT_ERROR_DO_LOSETUP;
88         }
89         /*Check whether loopback device is associated with device node or not */
90         devi = _app2sd_find_associated_device_node(pkgid);
91         if (devi == NULL) {
92                 app2ext_print("App2Sd Error : finding associated device node failed\n");
93                 ret = APP2EXT_ERROR_DO_LOSETUP;
94                 goto FINISH_OFF;
95         }
96
97         /*Format the loopback file system */
98         ret = _app2sd_create_file_system(device_node);
99         if (ret) {
100                 app2ext_print("App2Sd Error : creating FS failed failed\n");
101                 ret = APP2EXT_ERROR_CREATE_FS;
102                 goto FINISH_OFF;
103         }
104
105         /*Mount the loopback encrypted pseudo device on application installation path as with Read Write permission */
106         ret =_app2sd_mount_app_content(pkgid, device_node, MOUNT_TYPE_RW,
107                                         dir_list, APP2SD_PRE_INSTALL);
108         if (ret) {
109                 app2ext_print("App2Sd Error : mounting dev path to app install path failed\n");
110                 ret = APP2EXT_ERROR_MOUNT_PATH;
111                 goto FINISH_OFF;
112         }
113
114         /*Success */
115         ret = APP2EXT_SUCCESS;
116         goto END;
117
118 FINISH_OFF:
119
120         if (device_node) {
121                 result = _app2sd_detach_loop_device(device_node);
122                 if (result) {
123                         free(result);
124                         result = NULL;
125                 }
126                 _app2sd_delete_loopback_device(pkgid);
127         }
128 END:
129         if (device_node) {
130                 free(device_node);
131                 device_node = NULL;
132         }
133         if (devi) {
134                 free(devi);
135                 devi = NULL;
136         }
137         return ret;
138 }
139
140 int app2sd_post_app_install(const char *pkgid,
141                         app2ext_status install_status)
142 {
143         char *device_name = NULL;
144         char buf_dir[FILENAME_MAX] = { 0, };
145         int ret = APP2EXT_SUCCESS;
146         /*Validate the function parameter recieved */
147         if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
148                 || install_status > APP2EXT_STATUS_SUCCESS) {
149                 app2ext_print("Invalid func parameters\n");
150                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
151         }
152
153         /*Check whether MMC is present or not */
154         ret = _app2sd_check_mmc_status();
155         if (ret) {
156                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
157                              ret);
158                 return APP2EXT_ERROR_MMC_STATUS;
159         }
160         sync(); //2
161         /*Get the associated device node for SD card applicationer */
162         device_name = _app2sd_find_associated_device_node(pkgid);
163         if (NULL == device_name) {
164                 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
165         }
166         ret = _app2sd_unmount_app_content(pkgid);
167         if (ret) {
168                 if (device_name) {
169                         free(device_name);
170                         device_name = NULL;
171                 }
172                 app2ext_print("Unable to unmount the app content %d\n", ret);
173                 return APP2EXT_ERROR_UNMOUNT;
174         }
175         ret = _app2sd_remove_loopback_encryption_setup(pkgid);
176         if (ret) {
177                 if (device_name) {
178                         free(device_name);
179                         device_name = NULL;
180                 }
181                 app2ext_print
182                     ("Unable to Detach the loopback encryption setup for the application");
183                 return APP2EXT_ERROR_UNMOUNT;
184         }
185         if (device_name) {
186                 free(device_name);
187                 device_name = NULL;
188         }
189
190         /*Take appropriate action based on installation
191         status of application package */
192         if (install_status == APP2EXT_STATUS_FAILED) {
193                 /*Delete the loopback device from the SD card */
194                 ret = _app2sd_delete_loopback_device(pkgid);
195                 if (ret) {
196                         app2ext_print
197                             ("App2Sd Error : Unable to delete the loopback device from the SD Card\n");
198                         return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
199                 }
200                 ret = _app2sd_remove_password_from_db(pkgid);
201
202                 if (ret) {
203                         app2ext_print
204                             ("App2Sd Error : Unable to delete the password\n");
205                 }
206
207                 snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
208
209                 ret = _app2sd_delete_directory(buf_dir);
210
211                 if (ret) {
212                         app2ext_print
213                             ("App2Sd Error : Unable to delete the directory %s\n",
214                              buf_dir);
215                 }
216
217         } else {
218                 /*If  the status is success, then update installed storage to pkgmgr_parser db*/
219                 int rt = 0;
220                 pkgmgrinfo_pkgdbinfo_h handle = NULL;
221                 rt = pkgmgrinfo_create_pkgdbinfo(pkgid, &handle);
222                 if (rt < 0) {
223                         app2ext_print("pkgmgrinfo_create_pkgdbinfo[%s] fail.. \n", pkgid);
224                 }
225                 rt = pkgmgrinfo_set_installed_storage_to_pkgdbinfo(handle, INSTALL_EXTERNAL);
226                 if (rt < 0) {
227                         app2ext_print("fail to update installed location to db[%s, %s]\n", pkgid, INSTALL_EXTERNAL);
228                 }
229                 rt =pkgmgrinfo_save_pkgdbinfo(handle);
230                 if (rt < 0) {
231                         app2ext_print("pkgmgrinfo_save_pkgdbinfo[%s] failed\n", pkgid);
232                 }
233                 rt =pkgmgrinfo_destroy_pkgdbinfo(handle);
234                 if (rt < 0) {
235                         app2ext_print("pkgmgrinfo_destroy_pkgdbinfo[%s] failed\n", pkgid);
236                 }
237         }
238         return ret;
239 }
240
241 int app2sd_on_demand_setup_init(const char *pkgid)
242 {
243         int ret = APP2EXT_SUCCESS;
244         char app_path[FILENAME_MAX] = { 0, };
245         char *device_node = NULL;
246         char *result = NULL;
247         FILE *fp = NULL;
248
249         /*Validate the function parameter recieved */
250         if (pkgid == NULL) {
251                 app2ext_print
252                     ("App2Sd Error : Invalid function arguments to app launch setup\n");
253                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
254         }
255
256         /*Check whether MMC is present or not */
257         ret = _app2sd_check_mmc_status();
258         if (ret) {
259                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
260                              ret);
261                 return APP2EXT_ERROR_MMC_STATUS;
262         }
263
264         /*check app entry is there in sd card or not. */
265         snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH,
266                  pkgid);
267         fp = fopen(app_path, "r+");
268         if (fp == NULL) {
269                 app2ext_print
270                     ("App2SD Error: App Entry is not present in SD Card\n");
271                 return APP2EXT_ERROR_INVALID_PACKAGE;
272         }
273         fclose(fp);
274         result = (char *)_app2sd_find_associated_device(app_path);
275         /*process the string */
276         if ((result!=NULL) && strstr(result, "/dev") != NULL) {
277                 app2ext_print("App2SD Error! Already associated\n");
278                 free(result);
279                 result = NULL;
280                 return APP2EXT_ERROR_ALREADY_MOUNTED;
281         }
282
283         /*Do loopback setup */
284         device_node = _app2sd_do_loopback_encryption_setup(pkgid);
285         if (device_node == NULL) {
286                 app2ext_print
287                     ("App2Sd Error : loopback encryption setup failed\n");
288                 return APP2EXT_ERROR_DO_LOSETUP;
289         }
290
291         /*Do  mounting */
292         ret =
293             _app2sd_mount_app_content(pkgid, device_node, MOUNT_TYPE_RD,
294                                 NULL, APP2SD_APP_LAUNCH);
295         if (ret) {
296                 app2ext_print("App2Sd Error : Re-mount failed\n");
297                 if (device_node) {
298                         free(device_node);
299                         device_node = NULL;
300                 }
301                 return APP2EXT_ERROR_MOUNT_PATH;
302         }
303         if (device_node) {
304                 free(device_node);
305                 device_node = NULL;
306         }
307         return ret;
308 }
309
310 int app2sd_on_demand_setup_exit(const char *pkgid)
311 {
312         int ret = APP2EXT_SUCCESS;
313         char app_path[FILENAME_MAX] = { 0, };
314         FILE *fp = NULL;
315
316         /*Validate the function parameter recieved */
317         if (pkgid == NULL) {
318                 app2ext_print
319                     ("App2Sd Error : Invalid function arguments to app launch setup\n");
320                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
321         }
322
323         /*Check whether MMC is present or not */
324         ret = _app2sd_check_mmc_status();
325         if (ret) {
326                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
327                              ret);
328                 return APP2EXT_ERROR_MMC_STATUS;
329         }
330         /*check app entry is there in sd card or not. */
331         snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH,
332                  pkgid);
333         fp = fopen(app_path, "r+");
334         if (fp == NULL) {
335                 app2ext_print
336                     ("App2SD Error: App Entry is not present in SD Card\n");
337                 return APP2EXT_ERROR_INVALID_PACKAGE;
338         }
339         fclose(fp);
340         ret = _app2sd_unmount_app_content(pkgid);
341         if (ret) {
342                 app2ext_print
343                     ("App2SD Error: Unable to unmount the SD application\n");
344                 return APP2EXT_ERROR_UNMOUNT;
345         }
346         ret = _app2sd_remove_loopback_encryption_setup(pkgid);
347         if (ret) {
348                 app2ext_print("App2SD Error: Unable to remove loopback setup\n");
349                 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
350         }
351         return ret;
352 }
353
354 int app2sd_pre_app_uninstall(const char *pkgid)
355 {
356         int ret = APP2EXT_SUCCESS;
357         char app_path[FILENAME_MAX] = { 0, };
358         char *device_node = NULL;
359         FILE*fp = NULL;
360
361         /*Validate the function parameter recieved */
362         if (pkgid == NULL) {
363                 app2ext_print
364                     ("App2Sd Error : Invalid function arguments to app launch setup\n");
365                 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
366                 goto END;
367         }
368         /*Check whether MMC is present or not */
369         ret = _app2sd_check_mmc_status();
370         if (ret) {
371                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
372                              ret);
373                 ret = APP2EXT_ERROR_MMC_STATUS;
374                 goto END;
375         }
376         /*check app entry is there in sd card or not. */
377         snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH, pkgid);
378         fp = fopen(app_path, "r+");
379         if (fp == NULL) {
380                 app2ext_print
381                     ("App2SD Error: App Entry is not present in SD Card\n");
382                 ret = APP2EXT_ERROR_INVALID_PACKAGE;
383                 goto END;
384         }
385         fclose(fp);
386
387         /*Get the associated device node for SD card applicationer */
388         device_node = _app2sd_find_associated_device_node(pkgid);
389         if (NULL == device_node) {
390                 /*Do loopback setup */
391                 device_node = _app2sd_do_loopback_encryption_setup(pkgid);
392
393                 if (device_node == NULL) {
394                         app2ext_print
395                             ("App2Sd Error : loopback encryption setup failed\n");
396                         ret = APP2EXT_ERROR_DO_LOSETUP;
397                         goto END;
398                 }
399                 /*Do  mounting */
400                 ret =
401                     _app2sd_mount_app_content(pkgid, device_node,
402                                         MOUNT_TYPE_RW, NULL,
403                                         APP2SD_PRE_UNINSTALL);
404
405                 if (ret) {
406                         app2ext_print("App2Sd Error : RW-mount failed\n");
407                         if (device_node) {
408                                 free(device_node);
409                                 device_node = NULL;
410                         }
411                         ret = APP2EXT_ERROR_MOUNT_PATH;
412                         goto END;
413                 }
414         } else {
415                 /*Do  re-mounting */
416                 ret =
417                     _app2sd_mount_app_content(pkgid, device_node,
418                                         MOUNT_TYPE_RW_REMOUNT, NULL,
419                                         APP2SD_PRE_UNINSTALL);
420
421                 if (ret) {
422                         app2ext_print("App2Sd Error : Re-mount failed\n");
423                         if (device_node) {
424                                 free(device_node);
425                                 device_node = NULL;
426                         }
427                         ret = APP2EXT_ERROR_MOUNT_PATH;
428                         goto END;
429                 }
430         }
431         if (device_node) {
432                 free(device_node);
433                 device_node = NULL;
434         }
435
436 END:
437         if (ret != APP2EXT_SUCCESS)
438                 app2ext_print("App2Sd Error : app2sd has [%d]error, but return success for uninstallation\n", ret);
439         return APP2EXT_SUCCESS;
440 }
441
442 /*
443 * app2sd_post_app_uninstall_setup
444 * Uninstall Application and free all the allocated resources
445 * Called after dpkg remove, It deallocates dev node and loopback
446 */
447 int app2sd_post_app_uninstall(const char *pkgid)
448 {
449         char buf_dir[FILENAME_MAX] = { 0, };
450         int ret = APP2EXT_SUCCESS;
451         int ret1 = APP2EXT_SUCCESS;
452         /*Validate the function parameter recieved */
453         if (pkgid == NULL) {
454                 app2ext_print
455                     ("App2Sd Error : Invalid function arguments to Post Uninstall\n");
456                 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
457                 goto END;
458         }
459         /*Check whether MMC is present or not */
460         ret = _app2sd_check_mmc_status();
461         if (ret) {
462                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
463                              ret);
464                 ret = APP2EXT_ERROR_MMC_STATUS;
465                 goto END;
466         }
467         /*Unmount the loopback encrypted pseudo device from the application installation path */
468         ret = _app2sd_unmount_app_content(pkgid);
469         if (ret) {
470                 app2ext_print("Unable to unmount the app content %d\n", ret);
471                 ret = APP2EXT_ERROR_UNMOUNT;
472                 goto END;
473         }
474         /*Detach the loopback encryption setup for the application */
475         ret = _app2sd_remove_loopback_encryption_setup(pkgid);
476         if (ret) {
477                 app2ext_print
478                     ("Unable to Detach the loopback encryption setup for the application");
479                 ret = APP2EXT_ERROR_DETACH_LOOPBACK_DEVICE;
480                 goto END;
481         }
482         /*Delete the loopback device from the SD card */
483         ret = _app2sd_delete_loopback_device(pkgid);
484         if (ret) {
485                 app2ext_print
486                     ("App2Sd Error : Unable to delete the loopback device from the SD Card\n");
487                 ret =  APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
488                 goto END;
489         }
490         memset((void *)&buf_dir, '\0', FILENAME_MAX);
491         snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
492         ret1 = _app2sd_delete_directory(buf_dir);
493         if (ret1) {
494                 app2ext_print
495                     ("App2Sd Error : Unable to delete the directory %s\n",
496                      buf_dir);
497         }
498         /*remove encryption password from DB */
499         ret = _app2sd_initialize_db();
500         if (ret) {
501                 app2ext_print("\n app2sd db initialize failed");
502                 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
503                 goto END;
504         }
505         ret = _app2sd_remove_password_from_db(pkgid);
506         if (ret) {
507                 app2ext_print("cannot remove password from db \n");
508                 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
509                 goto END;
510         }
511
512 END:
513         if (ret != APP2EXT_SUCCESS)
514                 app2ext_print("App2Sd Error : app2sd has [%d]error, but return success for uninstallation\n", ret);
515         return APP2EXT_SUCCESS;
516 }
517
518 int app2sd_move_installed_app(const char *pkgid, GList* dir_list,
519                         app2ext_move_type move_type)
520 {
521         int ret = 0;
522         int pkgmgrinfo_ret = 0;
523
524         /*Validate function arguments*/
525         if (pkgid == NULL || dir_list == NULL
526                 || move_type < APP2EXT_MOVE_TO_EXT
527                 || move_type > APP2EXT_MOVE_TO_PHONE) {
528                 app2ext_print("App2Sd Error : Invalid function arguments\n");
529                 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
530                 goto END;
531         }
532
533         /*If  move is completed, then update installed storage to pkgmgr_parser db*/
534         pkgmgrinfo_pkginfo_h info_handle = NULL;
535         pkgmgrinfo_installed_storage storage = PMINFO_INTERNAL_STORAGE;
536         pkgmgrinfo_ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &info_handle);
537         if (pkgmgrinfo_ret < 0) {
538                 app2ext_print("App2Sd Error : pkgmgrinfo_pkginfo_get_pkginfo[%s] fail.. \n", pkgid);
539         }
540         pkgmgrinfo_ret = pkgmgrinfo_pkginfo_get_installed_storage(info_handle, &storage);
541         if (pkgmgrinfo_ret < 0) {
542                 app2ext_print("App2Sd Error : pkgmgrinfo_pkginfo_get_installed_storage[%s] fail.. \n", pkgid);
543         }
544
545         if ((move_type == APP2EXT_MOVE_TO_EXT && storage == PMINFO_EXTERNAL_STORAGE)
546                 || (move_type == APP2EXT_MOVE_TO_PHONE && storage == PMINFO_INTERNAL_STORAGE)) {
547                         ret = APP2EXT_ERROR_PKG_EXISTS;
548                         app2ext_print("App2Sd Error : PKG_EXISTS in [%d]STORAGE\n", storage);
549                         pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
550                         goto END;
551         } else {
552                 app2ext_print("App2Sd Error : okokkokin [%d]STORAGE\n", storage);
553         }
554         pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
555
556         ret = _app2sd_move_app(pkgid, move_type, dir_list);
557         if (ret) {
558                 app2ext_print("App2Sd Error : Unable to move application\n");
559                 goto END;
560         }
561
562         /*If  move is completed, then update installed storage to pkgmgr_parser db*/
563         pkgmgrinfo_pkgdbinfo_h handle = NULL;
564         pkgmgrinfo_ret = pkgmgrinfo_create_pkgdbinfo(pkgid, &handle);
565         if (pkgmgrinfo_ret < 0) {
566                 app2ext_print("App2Sd Error : pkgmgrinfo_create_pkgdbinfo[%s] fail.. \n", pkgid);
567         }
568
569         if (move_type == APP2EXT_MOVE_TO_EXT) {
570                 pkgmgrinfo_ret = pkgmgrinfo_set_installed_storage_to_pkgdbinfo(handle, INSTALL_EXTERNAL);
571                 if (pkgmgrinfo_ret < 0) {
572                         app2ext_print("App2Sd Error : fail to update installed location to db[%s, %s]\n", pkgid, INSTALL_EXTERNAL);
573                 }
574         } else {
575                 pkgmgrinfo_ret = pkgmgrinfo_set_installed_storage_to_pkgdbinfo(handle, INSTALL_INTERNAL);
576                 if (pkgmgrinfo_ret < 0) {
577                         app2ext_print("App2Sd Error : fail to update installed location to db[%s, %s]\n", pkgid, INSTALL_INTERNAL);
578                 }
579         }
580         pkgmgrinfo_ret =pkgmgrinfo_save_pkgdbinfo(handle);
581         if (pkgmgrinfo_ret < 0) {
582                 app2ext_print("pkgmgrinfo_save_pkgdbinfo[%s] failed\n", pkgid);
583         }
584         pkgmgrinfo_ret =pkgmgrinfo_destroy_pkgdbinfo(handle);
585         if (pkgmgrinfo_ret < 0) {
586                 app2ext_print("pkgmgrinfo_destroy_pkgdbinfo failed\n");
587         }
588
589 END:
590
591         vconf_set_int(VCONFKEY_PKGMGR_STATUS, ret);
592
593         return ret;
594 }
595
596 int app2sd_pre_app_upgrade(const char *pkgid, GList* dir_list,
597                         int size)
598 {
599         int ret = APP2EXT_SUCCESS;
600         char app_path[FILENAME_MAX] = { 0, };
601         char *device_node = NULL;
602         unsigned long long curr_size = 0;
603         FILE *fp = NULL;
604
605         /*Validate function arguments*/
606         if (pkgid == NULL || dir_list == NULL || size<=0) {
607                 app2ext_print
608                     ("App2Sd Error : Invalid function arguments \n");
609                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
610         }
611         /*Check whether MMC is present or not */
612         ret = _app2sd_check_mmc_status();
613         if (ret) {
614                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
615                              ret);
616                 return APP2EXT_ERROR_MMC_STATUS;
617         }
618         /*check app entry is there in sd card or not. */
619         snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH,
620                  pkgid);
621         app2ext_print("App2Sd Log : Checking path %s\n", app_path);
622         fp = fopen(app_path, "r+");
623         if (fp == NULL) {
624                 app2ext_print
625                     ("App2SD Error: App Entry is not present in SD Card\n");
626                 return APP2EXT_ERROR_INVALID_PACKAGE;
627         }
628         fclose(fp);
629         /*Get installed app size*/
630         curr_size = _app2sd_calculate_file_size(app_path);
631         curr_size = (curr_size/1024)/1024;
632
633         if (curr_size==0) {
634                 app2ext_print
635                     ("App2SD Error: App Entry is not present in SD Card\n");
636                 return APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE;
637         }
638         if (curr_size<size) {
639                 ret = _app2sd_update_loopback_device_size(pkgid, size, dir_list);
640                 if(APP2EXT_SUCCESS !=ret) {
641                         app2ext_print
642                             ("App2SD Error: _app2sd_update_loopback_device_size() failed\n");
643                         return ret;
644                 }
645         }
646
647         /*Get the associated device node for SD card applicationer */
648         device_node = _app2sd_find_associated_device_node(pkgid);
649         if (NULL == device_node) {
650                 /*Do loopback setup */
651                 device_node = _app2sd_do_loopback_encryption_setup(pkgid);
652                 if (device_node == NULL) {
653                         app2ext_print
654                             ("App2Sd Error : loopback encryption setup failed\n");
655                         return APP2EXT_ERROR_DO_LOSETUP;
656                 }
657                 /*Do  mounting */
658                 ret =
659                     _app2sd_mount_app_content(pkgid, device_node,
660                                         MOUNT_TYPE_RW, NULL,
661                                         APP2SD_PRE_UPGRADE);
662                 if (ret) {
663                         app2ext_print("App2Sd Error : Re-mount failed\n");
664                         if (device_node) {
665                                 free(device_node);
666                                 device_node = NULL;
667                         }
668                         return APP2EXT_ERROR_MOUNT_PATH;
669                 }
670         } else {
671                 /*Do  re-mounting */
672                 ret =
673                     _app2sd_mount_app_content(pkgid, device_node,
674                                               MOUNT_TYPE_RW_REMOUNT, NULL,
675                                               APP2SD_PRE_UPGRADE);
676                 if (ret) {
677                         app2ext_print("App2Sd Error : Re-mount failed\n");
678                         if (device_node) {
679                                 free(device_node);
680                                 device_node = NULL;
681                         }
682                         return APP2EXT_ERROR_MOUNT_PATH;
683                 }
684         }
685
686         if (device_node) {
687                 free(device_node);
688                 device_node = NULL;
689         }
690         return ret;
691 }
692
693
694 int app2sd_post_app_upgrade(const char *pkgid,
695                         app2ext_status install_status)
696 {
697         char *device_name = NULL;
698         int ret = APP2EXT_SUCCESS;
699         /*Validate the function parameter recieved */
700         if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
701                 || install_status > APP2EXT_STATUS_SUCCESS) {
702                 app2ext_print("Invalid func parameters\n");
703                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
704         }
705         /*Check whether MMC is present or not */
706         ret = _app2sd_check_mmc_status();
707         if (ret) {
708                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
709                              ret);
710                 return APP2EXT_ERROR_MMC_STATUS;
711         }
712
713         /*Get the associated device node for SD card applicationer */
714         device_name = _app2sd_find_associated_device_node(pkgid);
715         if (NULL == device_name) {
716                 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
717         }
718         ret = _app2sd_unmount_app_content(pkgid);
719         if (ret) {
720                 if (device_name) {
721                         free(device_name);
722                         device_name = NULL;
723                 }
724                 app2ext_print("Unable to unmount the app content %d\n", ret);
725                 return APP2EXT_ERROR_UNMOUNT;
726         }
727         ret = _app2sd_remove_loopback_encryption_setup(pkgid);
728         if (ret) {
729                 if (device_name) {
730                         free(device_name);
731                         device_name = NULL;
732                 }
733                 app2ext_print
734                     ("Unable to Detach the loopback encryption setup for the application");
735                 return APP2EXT_ERROR_UNMOUNT;
736         }
737         if (device_name) {
738                 free(device_name);
739                 device_name = NULL;
740         }
741         return ret;
742 }
743
744 #if 0
745 /**
746  * Reserved API for forced cleanup
747  *
748  */
749 int app2sd_force_cleanup(const char *pkgid){
750         char *device_name = NULL;
751         char buf_dir[FILENAME_MAX] = { 0, };
752         int ret = APP2EXT_SUCCESS;
753         FILE *fp = NULL;
754
755         /*Validate the function parameter recieved */
756         if (pkgid == NULL) {
757                 app2ext_print("invalid func parameters\n");
758                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
759         }
760         memset((void *)&buf_dir, '\0', FILENAME_MAX);
761         snprintf(buf_dir, FILENAME_MAX, "%s%s", APP2SD_PATH, pkgid);
762         fp = fopen(buf_dir, "r+");
763         if (fp == NULL) {
764                 app2ext_print("\"%s\" not installed on SD Card\n", pkgid);
765                 return APP2EXT_ERROR_INVALID_PACKAGE;
766         }
767         fclose(fp);
768         /*Check whether MMC is present or not */
769         ret = _app2sd_check_mmc_status();
770         if (ret) {
771                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
772                              ret);
773                 return APP2EXT_ERROR_MMC_STATUS;
774         }
775
776         /*Get the associated device node for SD card applicationer */
777         device_name = _app2sd_find_associated_device_node(pkgid);
778         if (NULL != device_name) {
779                 free(device_name);
780                 device_name = NULL;
781                 ret = _app2sd_unmount_app_content(pkgid);
782                 if (ret) {
783                         app2ext_print("Unable to unmount the app content %d\n", ret);
784                         return APP2EXT_ERROR_UNMOUNT;
785                 }
786                 ret = _app2sd_remove_loopback_encryption_setup(pkgid);
787                 if (ret) {
788                         app2ext_print
789                             ("Unable to Detach the loopback encryption setup for the application");
790                         return APP2EXT_ERROR_UNMOUNT;
791                 }
792         }
793
794         memset((void *)&buf_dir, '\0', FILENAME_MAX);
795         snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
796         ret = _app2sd_delete_directory(buf_dir);
797         if (ret) {
798                 app2ext_print
799                     ("App2Sd Error : Unable to delete the directory %s\n",
800                      buf_dir);
801         }
802
803         /*remove passwrd from DB*/
804         ret = _app2sd_initialize_db();
805         if (ret) {
806                 app2ext_print("\n app2sd db initialize failed");
807                 return APP2EXT_ERROR_SQLITE_REGISTRY;
808         }
809         ret = _app2sd_remove_password_from_db(pkgid);
810         if (ret) {
811                 app2ext_print("cannot remove password from db \n");
812                 return APP2EXT_ERROR_SQLITE_REGISTRY;
813         }
814         return ret;
815 }
816 #endif
817
818 /* This is the plug-in load function. The plugin has to bind its functions to function pointers of handle
819         @param[in/out]          st_interface    Specifies the storage interface.
820 */
821 void
822 app2ext_on_load(app2ext_interface *st_interface)
823 {
824         /*Plug-in Binding.*/
825         st_interface->pre_install= app2sd_pre_app_install;
826         st_interface->post_install= app2sd_post_app_install;
827         st_interface->pre_uninstall= app2sd_pre_app_uninstall;
828         st_interface->post_uninstall= app2sd_post_app_uninstall;
829         st_interface->pre_upgrade= app2sd_pre_app_upgrade;
830         st_interface->post_upgrade= app2sd_post_app_upgrade;
831         st_interface->move= app2sd_move_installed_app;
832         st_interface->enable= app2sd_on_demand_setup_init;
833         st_interface->disable= app2sd_on_demand_setup_exit;
834 }
835