upload tizen1.0 source
[pkgs/n/native-installer.git] / backend-lib / src / libdeb.c
1 /*
2  * rpm-installer
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>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23
24
25
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/time.h>
30 #include "libdebinternals.h"
31
32 #define EXTRACT_MENIFEST                        "/usr/bin/extract_manifest.sh"
33 #define FIND_PACKAGE                            "/usr/bin/find_package.sh"
34 #define MANIFEST_FILE_NAME                      "control"
35 #define BASEDIR                                         "/opt/share"
36 #define THIRD_PARTY_DESKTOP_FILE_PATH \
37         BASEDIR"/install-info/application"
38 #define INHOUSE_DESKTOP_FILE_PATH \
39         BASEDIR"/applications"
40 #define BUFFSIZE                                                256
41
42 void pkg_native_plugin_on_unload(void)
43 {
44         return;
45 }
46
47 int pkg_plugin_app_is_installed(const char *pkg_name)
48 {
49         FILE *fp = NULL;
50         char *lineptr = NULL;
51         size_t len = 512;
52         ssize_t read;
53         char buff[512] = { 0, };
54         /* Check for valid arguments */
55         if (pkg_name == NULL) {
56                 _libdeb_print
57                     (DEBUG_ERR,
58                      "[pkg_plugin_app_is_installed] pkg_name is NULL\n");
59                 return LIBDEB_ERROR;
60         }
61         fp = fopen(PKGMETAINFO, "r");
62         if (fp == NULL) {
63                 _libdeb_print(DEBUG_ERR,
64                               "failed to open file(%s)\n", PKGMETAINFO);
65                 return LIBDEB_ERROR;
66         }
67         snprintf(buff, 512, "Package: %s", pkg_name);
68         /*allocate memory for lineptr. If it is less
69            getline will realloc it */
70         lineptr = (char *)malloc(sizeof(char)*512);
71         if(lineptr == NULL) {
72                 _libdeb_print(DEBUG_ERR,
73                               "malloc failed\n");
74                 fclose(fp);
75                 return LIBDEB_ERROR;
76         }
77         memset(lineptr, 0x00, sizeof(char)*512);
78         clearerr(fp);
79         do {
80                 read = getline(&lineptr, &len, fp);
81                 if (read == -1) {
82                         if (feof(fp)) {
83                                 _libdeb_print(DEBUG_ERR, "EOF reached" 
84                                 "Package not installed\n");
85                         }
86                         else     {
87                                 _libdeb_print(DEBUG_ERR, "getline failed\n");
88                         }
89                         fclose(fp);
90                         return LIBDEB_ERROR;
91                 }
92                 if (lineptr && (strncmp(lineptr, buff, strlen(buff)) == 0) &&
93                             (strlen(lineptr) == strlen(buff) + 1)) {
94                                 _libdeb_print
95                                         (DEBUG_INFO,
96                                              "[pkg_plugin_app_is_installed] "
97                                              "package is installed\n");
98                         fclose(fp);     
99                         return LIBDEB_SUCCESS;
100                         } else {
101                                 if (lineptr)
102                                         memset(lineptr, 0x00, len);
103                                 continue;
104                         }
105         } while (lineptr != NULL);
106         fclose(fp);
107         _libdeb_print(DEBUG_INFO,"[pkg_plugin_app_is_installed] "
108                         "package is not installed\n");
109         return LIBDEB_ERROR;
110
111 }
112
113 int pkg_plugin_get_installed_apps_list(const char *category,
114                                        const char *option,
115                                        package_manager_pkg_info_t **list,
116                                        int *count)
117 {
118         return LIBDEB_SUCCESS;
119 }
120
121 int pkg_plugin_get_app_detail_info(const char *pkg_name,
122                                    package_manager_pkg_detail_info_t
123                                    *pkg_detail_info)
124 {
125         _libdeb_print(DEBUG_INFO,
126                       "pkg_plugin_get_app_detail_info() is called\n");
127         /* Check for valid arguments */
128         if (pkg_name == NULL || pkg_detail_info == NULL) {
129                 _libdeb_print(DEBUG_ERR,
130                               "[pkg_plugin_get_app_detail_info_from_package] "
131                               "args supplied is NULL\n");
132                 return LIBDEB_ERROR;
133         }
134         char dirname[BUFFSIZE] = { '\0' };
135         int ret = 0;
136         long long data_size = 0;
137         char buff[256] = {'\0'};
138         time_t install_time = 0;
139         
140         /*As Installed Size field is optional, it may not be present in control file.
141         So Initialize it to 0. If present it will be populated with app_size*/
142         pkg_detail_info->app_size = 0;
143                 
144         /* pkgtype is by default deb */
145         strncpy(pkg_detail_info->pkg_type, "deb", sizeof(pkg_detail_info->pkg_type));
146
147         /* Populate the structure now */
148
149         /*get data_size*/
150         snprintf(dirname, BUFFSIZE-1, "/opt/apps/%s/data", pkg_name);
151         data_size = _libdeb_calculate_dir_size(dirname);
152         if (data_size < 0) {
153                 _libdeb_print(DEBUG_ERR,
154                                 "Calculate dir size failed\n");
155                 pkg_detail_info->data_size = 0 ;
156         }
157         else {
158                 data_size += BLOCK_SIZE; /* the function does not adds 4096 
159                                         bytes for the directory size itself*/
160                 pkg_detail_info->data_size = data_size/1024 ;
161         }
162         ret = _libdeb_populate_control_info(pkg_name, pkg_detail_info,
163                                             PKGMETAINFO);
164         if (ret) {
165                 _libdeb_print(DEBUG_ERR,
166                               "[pkg_plugin_get_app_detail_info] "
167                               "_libdeb_populate_control_info returns %d\n",
168                               ret);
169                 return LIBDEB_ERROR;
170         }
171
172         /* Min Platform Version */
173         pkg_detail_info->min_platform_version[0] = '\0';
174
175         /* Optional ID*/
176         pkg_detail_info->optional_id[0] = '\0';
177
178         /* Total Installed Size*/
179         pkg_detail_info->installed_size = pkg_detail_info->app_size + 
180                                                                         pkg_detail_info->data_size;
181         /* Installed Time*/
182         snprintf(buff, 256, "db/app-info/%s/installed-time", pkg_name);
183         ret = vconf_get_int(buff, (int *)&install_time);
184         if (ret) {
185                 _libdeb_print(DEBUG_ERR, "get installed time failed\n");
186                 pkg_detail_info->installed_time = 0;
187         }
188         else
189                 pkg_detail_info->installed_time = install_time;
190
191         
192         return LIBDEB_SUCCESS;
193 }
194
195 int pkg_plugin_get_app_detail_info_from_package(const char *pkg_path,
196                                         package_manager_pkg_detail_info_t
197                                         *pkg_detail_info)
198 {
199         _libdeb_print(DEBUG_INFO,
200                       "pkg_plugin_get_app_detail_info_from_package() is called\n");
201         
202         /* Check for valid arguments */
203         if (pkg_path == NULL || pkg_detail_info == NULL) {
204                 _libdeb_print(DEBUG_ERR,
205                               "[pkg_plugin_get_app_detail_info_from_package]"
206                               "args supplied is NULL\n");
207                 return LIBDEB_ERROR;
208         }
209
210         const char *argv[] = { EXTRACT_MENIFEST, pkg_path, NULL };
211         int ret = 0;
212         long long data_size = 0;
213         char *pkg_filename = NULL;
214         char *str = NULL;
215         char controlfilepath[BUFFSIZE] = { '\0' };
216         char dirname[BUFFSIZE] = { '\0' };
217         char buff[256] = {'\0'};
218         time_t install_time = 0;
219         
220         /* populate pkg type */
221         str = strrchr(pkg_path, 46);    /* 46 is ASCII for . */
222         strncpy(pkg_detail_info->pkg_type, (str + 1), strlen(str + 1));
223
224         /* extract the control file from the deb package */
225         ret = _libdeb_xsystem(argv);
226         if (ret != 0) {
227                 _libdeb_print(DEBUG_ERR,
228                               "[pkg_plugin_get_app_detail_info_from_package] "
229                               "xsystem returns %d\n", ret);
230                 return LIBDEB_ERROR;
231         }
232
233         /* Extract file name from file path */
234         pkg_filename = _libdeb_get_filename(pkg_path);
235         if (!pkg_filename) {
236                 _libdeb_print(DEBUG_ERR,
237                               "[pkg_plugin_get_app_detail_info_from_package] "
238                               " _libdeb_get_filename returns %s\n",
239                               pkg_filename);
240                 return LIBDEB_ERROR;
241         }
242         snprintf(controlfilepath, BUFFSIZE - 1, "/var/pkgmgr/%s/metainfo/%s",
243                 pkg_filename, MANIFEST_FILE_NAME);
244         if (pkg_filename) {
245                 free(pkg_filename);
246                 pkg_filename = NULL;
247         }
248         ret =
249             _libdeb_populate_control_info(NULL, pkg_detail_info,
250                                           controlfilepath);
251         if (ret) {
252                 _libdeb_print(DEBUG_ERR,
253                               "[pkg_plugin_get_app_detail_info_from_package] "
254                               " _libdeb_populate_control_info returns %d\n",
255                               ret);
256                 return LIBDEB_ERROR;
257         }
258
259         
260         /*get data_size. If pkg is not installed it will be 0*/
261         snprintf(dirname, BUFFSIZE-1, "/opt/apps/%s/data", 
262                                 pkg_detail_info->pkg_name);
263         
264         data_size = _libdeb_calculate_dir_size(dirname);
265         if (data_size < 0) {
266                 _libdeb_print(DEBUG_ERR,
267                                 "Calculate dir size failed\n");
268                 pkg_detail_info->data_size = 0 ;
269         }
270         else {
271                 data_size += BLOCK_SIZE; /* the function does not adds 4096 
272                                         bytes for the directory size itself*/
273
274                 pkg_detail_info->data_size = data_size/1024 ;
275         }
276
277         /* Min Platform Version */
278         pkg_detail_info->min_platform_version[0] = '\0';
279
280         /* Optional ID*/
281         pkg_detail_info->optional_id[0] = '\0';
282
283         /* Total Installed Size*/
284         pkg_detail_info->installed_size = pkg_detail_info->app_size + 
285                                                                         pkg_detail_info->data_size;
286
287         /* Installed Time */
288         snprintf(buff, 256, "db/app-info/%s/installed-time", pkg_detail_info->pkg_name);
289         ret = vconf_get_int(buff, (int *)&install_time);
290         if (ret) {
291                 _libdeb_print(DEBUG_ERR, "get installed time failed\n");
292                 pkg_detail_info->installed_time = 0;
293         }
294         else
295                 pkg_detail_info->installed_time = install_time;
296         
297
298         return LIBDEB_SUCCESS;
299 }
300
301 API int pkg_plugin_on_load(pkg_plugin_set *set)
302 {
303         if (set == NULL) {
304                 return LIBDEB_ERROR;
305         }
306
307         memset(set, 0x00, sizeof(pkg_plugin_set));
308
309         set->plugin_on_unload = pkg_native_plugin_on_unload;
310         set->pkg_is_installed = pkg_plugin_app_is_installed;
311         set->get_installed_pkg_list = pkg_plugin_get_installed_apps_list;
312         set->get_pkg_detail_info = pkg_plugin_get_app_detail_info;
313         set->get_pkg_detail_info_from_package =
314             pkg_plugin_get_app_detail_info_from_package;
315
316         return LIBDEB_SUCCESS;
317 }