Add multi-user support
[platform/core/base/rpm-installer.git] / backend-lib / src / librpm.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 #include <stdio.h>
26 #include <string.h>
27 #include <sys/time.h>
28 /*rpm specific headers*/
29 #include <rpmlib.h>
30 #include <header.h>
31 #include <rpmts.h>
32 #include <rpmdb.h>
33 #include <vconf.h>
34
35 /* For multi-user support */
36 #include <tzplatform_config.h>
37
38 #include "librpminternals.h"
39
40 #define BASEDIR                                         tzplatform_getenv(TZ_SYS_SHARE)
41 #define USER_APP_FOLDER                         tzplatform_getenv(TZ_USER_APP)
42 #define BUFFSIZE                                                256
43
44 void pkg_native_plugin_on_unload(void)
45 {
46         return;
47 }
48
49 int pkg_plugin_app_is_installed(const char *pkgid)
50 {
51         _librpm_print(DEBUG_INFO,
52                       "pkg_plugin_app_is_installed() is called\n");
53         /* Check for valid arguments */
54         if (pkgid == NULL) {
55                 _librpm_print(DEBUG_ERR,
56                               "[pkg_plugin_get_app_detail_info_from_package] "
57                               "args supplied is NULL\n");
58                 return LIBRPM_ERROR;
59         }
60         int ret = -1;
61         ret = _librpm_app_is_installed(pkgid);
62         if (ret == -1) {
63                 _librpm_print(DEBUG_ERR, "_librpm_app_is_installed() failed\n");
64                 return LIBRPM_ERROR;
65         }
66         /*1 for installed, 0 for not installed*/
67         if (ret == 1)
68                 return LIBRPM_SUCCESS;
69         else
70                 return LIBRPM_ERROR;
71 }
72
73 int pkg_plugin_get_installed_apps_list(const char *category,
74                                        const char *option,
75                                        package_manager_pkg_info_t **list,
76                                        int *count)
77 {
78         return LIBRPM_SUCCESS;
79 }
80
81 int pkg_plugin_get_app_detail_info(const char *pkgid,
82                                    package_manager_pkg_detail_info_t
83                                    *pkg_detail_info)
84 {
85         _librpm_print(DEBUG_INFO,
86                       "pkg_plugin_get_app_detail_info() is called\n");
87         /* Check for valid arguments */
88         if (pkgid == NULL || pkg_detail_info == NULL) {
89                 _librpm_print(DEBUG_ERR,
90                               "[pkg_plugin_get_app_detail_info_from_package] "
91                               "args supplied is NULL\n");
92                 return LIBRPM_ERROR;
93         }
94         char dirname[BUFFSIZE] = { '\0' };
95         int ret = 0;
96         long long data_size = 0;
97         char buff[256] = {'\0'};
98         time_t install_time = 0;
99
100         /* pkgtype is by default rpm */
101         strncpy(pkg_detail_info->pkg_type, "rpm", sizeof(pkg_detail_info->pkg_type));
102
103         /* Get the installed package info from rpm db */
104         ret = _librpm_get_installed_package_info(pkgid, pkg_detail_info);
105         if (ret) {
106                 return LIBRPM_ERROR;
107         }
108
109         /*get data_size*/
110         snprintf(dirname, BUFFSIZE-1, "%s/%s/data", USER_APP_FOLDER, pkgid);
111         data_size = _librpm_calculate_dir_size(dirname);
112         if (data_size < 0) {
113                 _librpm_print(DEBUG_ERR,
114                                 "Calculate dir size failed\n");
115                 pkg_detail_info->data_size = 0 ;
116         }
117         else {
118                 data_size += BLOCK_SIZE; /* the function does not adds 4096
119                                         bytes for the directory size itself*/
120                 pkg_detail_info->data_size = data_size/1024 ;
121         }
122
123         /* Min Platform Version */
124         pkg_detail_info->min_platform_version[0] = '\0';
125
126         /* Optional ID*/
127         pkg_detail_info->optional_id[0] = '\0';
128
129         /* Total Installed Size*/
130         pkg_detail_info->installed_size = pkg_detail_info->app_size +
131                                                 pkg_detail_info->data_size;
132         /* Installed Time*/
133         snprintf(buff, 256, "db/app-info/%s/installed-time", pkgid);
134         ret = vconf_get_int(buff, (int *)&install_time);
135         if (ret) {
136                 _librpm_print(DEBUG_ERR, "get installed time failed\n");
137                 pkg_detail_info->installed_time = 0;
138         }
139         else
140                 pkg_detail_info->installed_time = install_time;
141
142
143         return LIBRPM_SUCCESS;
144 }
145
146 int pkg_plugin_get_app_detail_info_from_package(const char *pkg_path,
147                                         package_manager_pkg_detail_info_t
148                                         *pkg_detail_info)
149 {
150         _librpm_print(DEBUG_INFO,
151                       "pkg_plugin_get_app_detail_info_from_package() is called\n");
152
153         /* Check for valid arguments */
154         if (pkg_path == NULL || pkg_detail_info == NULL) {
155                 _librpm_print(DEBUG_ERR,
156                               "[pkg_plugin_get_app_detail_info_from_package]"
157                               "args supplied is NULL\n");
158                 return LIBRPM_ERROR;
159         }
160
161         int ret = 0;
162         long long data_size = 0;
163         char *str = NULL;
164         char dirname[BUFFSIZE] = { '\0' };
165         char buff[256] = {'\0'};
166         time_t install_time = 0;
167
168         /* populate pkg type */
169         str = strrchr(pkg_path, 46);    /* 46 is ASCII for . */
170         strncpy(pkg_detail_info->pkg_type, (str + 1), strlen(str + 1));
171
172         /* populate rpm header specific info (name, version, description, size)*/
173         ret = _librpm_get_package_header_info(pkg_path, pkg_detail_info);
174         if (ret) {
175                 return LIBRPM_ERROR;
176         }
177
178         /*get data_size. If pkg is not installed it will be 0*/
179         snprintf(dirname, BUFFSIZE-1, "%s/%s/data",
180                                 USER_APP_FOLDER, pkg_detail_info->pkgid);
181
182         data_size = _librpm_calculate_dir_size(dirname);
183         if (data_size < 0) {
184                 _librpm_print(DEBUG_ERR,
185                                 "Calculate dir size failed\n");
186                 pkg_detail_info->data_size = 0 ;
187         }
188         else {
189                 data_size += BLOCK_SIZE; /* the function does not adds 4096
190                                         bytes for the directory size itself*/
191
192                 pkg_detail_info->data_size = data_size/1024 ;
193         }
194
195         /* Min Platform Version */
196         pkg_detail_info->min_platform_version[0] = '\0';
197
198         /* Optional ID*/
199         pkg_detail_info->optional_id[0] = '\0';
200
201         /* Total Installed Size*/
202         pkg_detail_info->installed_size = pkg_detail_info->app_size +
203                                                                         pkg_detail_info->data_size;
204
205         /* Installed Time */
206         snprintf(buff, 256, "db/app-info/%s/installed-time", pkg_detail_info->pkgid);
207         ret = vconf_get_int(buff, (int *)&install_time);
208         if (ret) {
209                 _librpm_print(DEBUG_ERR, "get installed time failed\n");
210                 pkg_detail_info->installed_time = 0;
211         }
212         else
213                 pkg_detail_info->installed_time = install_time;
214
215
216         return LIBRPM_SUCCESS;
217 }
218
219 API int pkg_plugin_on_load(pkg_plugin_set *set)
220 {
221         static int initialized = 0;
222         rpmRC rc;
223         if (set == NULL) {
224                 return LIBRPM_ERROR;
225         }
226
227         memset(set, 0x00, sizeof(pkg_plugin_set));
228         if (!initialized) {
229                 rc = rpmReadConfigFiles(NULL, NULL);
230                 if (rc == RPMRC_OK)
231                         initialized = 1;
232                 else {
233                         _librpm_print(DEBUG_ERR, "Unable to read RPM configuration.\n");
234                         initialized = 0;
235                         return LIBRPM_ERROR;
236                 }
237         }
238
239         set->plugin_on_unload = pkg_native_plugin_on_unload;
240         set->pkg_is_installed = pkg_plugin_app_is_installed;
241         set->get_installed_pkg_list = pkg_plugin_get_installed_apps_list;
242         set->get_pkg_detail_info = pkg_plugin_get_app_detail_info;
243         set->get_pkg_detail_info_from_package =
244             pkg_plugin_get_app_detail_info_from_package;
245
246         return LIBRPM_SUCCESS;
247 }