4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
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>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 /* System Include files */
32 #include <sys/types.h>
42 /* SLP include files */
43 #include "nativeinstaller.h"
44 #include "native_installer_util.h"
47 #define EXTRACT_MENIFEST "/usr/bin/extract_manifest.sh"
48 #define FIND_PACKAGE "/usr/bin/find_package.sh"
49 #define MANIFEST_FILE_NAME "control"
51 typedef struct _pkgfile_info {
54 char pkg_manifest_filepath[256];
55 char extracted_pkg_location[256];
60 extern char *gptrpkgname;
61 extern int do_upgrade;
63 static int __pkgmgr_get_filename(char *package_filepath,
64 pkgfile_info *ppkg_fileinfo);
65 static int __recursive_delete_dir(char *dirname);
67 pkginfo *_pkgmgr_get_installed_pkg_info(char *pkgname)
69 char pkg_manifest_filepath[256];
72 const char *argv[] = { FIND_PACKAGE, pkgname, NULL };
78 d_msg_backend(DEBUG_INFO,
79 "[_pkgmgr_get_installed_pkg_info] _xsystem returns %d\n",
82 d_msg_backend(DEBUG_INFO,
83 "[_pkgmgr_get_installed_pkg_info] "
84 "Package Not installed \n");
85 } else if (err == 2) {
86 d_msg_backend(DEBUG_INFO,
87 "[_pkgmgr_get_installed_pkg_info] "
88 "package already install\n");
89 snprintf(pkg_manifest_filepath, 255, "/var/pkgmgr/%s/pkginfo",
91 d_msg_backend(DEBUG_INFO, "manifest=%s\n",
92 pkg_manifest_filepath);
94 _pkgmgr_txt_parser_read_manifest(
95 (char *)(pkg_manifest_filepath));
98 d_msg_backend(DEBUG_ERR,
99 "Failed to parse Manifest file \n");
104 d_msg_backend(DEBUG_ERR,
105 "[_pkgmgr_get_installed_pkg_info] "
106 "_xsystem returns error = %d\n",
113 static int __recursive_delete_dir(char *dirname)
117 char abs_filename[FILENAME_MAX];
118 struct stat stFileInfo;
119 dp = opendir(dirname);
121 while (ep = readdir(dp)) {
122 snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname,
124 if (lstat(abs_filename, &stFileInfo) < 0)
125 perror(abs_filename);
126 if (S_ISDIR(stFileInfo.st_mode)) {
127 if (strcmp(ep->d_name, ".") &&
128 strcmp(ep->d_name, "..")) {
129 __recursive_delete_dir(abs_filename);
130 remove(abs_filename);
133 remove(abs_filename);
138 d_msg_backend(DEBUG_ERR,"Couldn't open the directory\n");
139 return NATIVEINSTALLER_ERR_CLEAR_DATA_FAILED;
142 return NATIVEINSTALLER_SUCCESS;
145 static int __pkgmgr_get_filename(char *package_filepath,
146 pkgfile_info *ppkg_fileinfo)
149 regmatch_t mached[5];
153 regcomp(&re, "[/]*([.a-z_A-Z0-9-]+\\.([aA][pP][kK]|[dD][eE][bB]))",
156 d_msg_backend(DEBUG_ERR,
157 "[__pkgmgr_get_filename]regcomp failed(%d)\n",
161 ret = regexec(&re, package_filepath, 5, mached, 0);
163 d_msg_backend(DEBUG_ERR,
164 "[__pkgmgr_get_filename]regexec failed(%s)(%d)\n",
165 package_filepath, ret);
169 /*Use simple logic to extract filename
170 as it may contain any character */
171 ptr = strrchr(package_filepath, 47); /* 47 is ASCII for / */
175 ppkg_fileinfo->ppkg_filename = strdup(ptr + 1);
176 ppkg_fileinfo->ppkgtype =
177 _substring(package_filepath, mached[2].rm_so,
178 mached[2].rm_eo - mached[2].rm_so);
180 d_msg_backend(DEBUG_INFO,
181 "[getPackageFileName][%s] -> [%s]; pkgtype = %s \n",
182 package_filepath, ppkg_fileinfo->ppkg_filename,
183 ppkg_fileinfo->ppkgtype);
188 pkginfo *_pkgmgr_get_pkg_info(char *pkg_filepath, int *errno,
189 bool *alreay_installed)
191 pkgfile_info pkg_fileinfo;
192 pkginfo *info = NULL;
193 pkginfo *tmpinfo = NULL;
195 const char *argv[] = { EXTRACT_MENIFEST, pkg_filepath, NULL };
197 if (pkg_filepath == NULL || errno == NULL || alreay_installed == NULL)
201 err = _xsystem(argv);
202 d_msg_backend(DEBUG_INFO, "[_pkgmgr_get_pkg_info] "
203 "_xsystem returns %d\n", err);
205 d_msg_backend(DEBUG_ERR,
206 "[_pkgmgr_get_pkg_info] _xsystem returns %d\n",
209 /*E_FILE_NOT_FOUND=6 */
211 /*E_NOT_VALID_ARCHIVE=8 */
215 *errno = NATIVEINSTALLER_ERR_PKG_NOT_FOUND;
218 *errno = NATIVEINSTALLER_ERR_NO_MANIFEST;
221 *errno = NATIVEINSTALLER_ERR_NO_DEB_FILE;
224 *errno = NATIVEINSTALLER_ERR_UNKNOWN;
230 err = __pkgmgr_get_filename(pkg_filepath, &pkg_fileinfo);
232 *errno = NATIVEINSTALLER_ERR_UNKNOWN;
236 if (pkg_fileinfo.ppkgtype == NULL) {
237 d_msg_backend(DEBUG_ERR, "Package type is returned NULL \n");
241 if (strcmp(pkg_fileinfo.ppkgtype, PKGTYPE) != 0) {
242 d_msg_backend(DEBUG_ERR,
243 "Package file is not a debian file \n");
244 *errno = NATIVEINSTALLER_ERR_NO_DEB_FILE;
247 snprintf(pkg_fileinfo.extracted_pkg_location, 255, "/var/pkgmgr/%s",
248 pkg_fileinfo.ppkg_filename);
250 snprintf(pkg_fileinfo.pkg_manifest_filepath, 255,
251 "/var/pkgmgr/%s/metainfo/%s",
252 pkg_fileinfo.ppkg_filename, MANIFEST_FILE_NAME);
253 d_msg_backend(DEBUG_INFO, "manifest=%s\n",
254 pkg_fileinfo.pkg_manifest_filepath);
255 info = _pkgmgr_txt_parser_read_manifest(
256 (char *)(pkg_fileinfo.pkg_manifest_filepath));
259 d_msg_backend(DEBUG_ERR, "Failed to parse Manifest file \n");
260 *errno = NATIVEINSTALLER_ERR_UNKNOWN;
263 unlink(pkg_fileinfo.pkg_manifest_filepath);
264 if (pkg_fileinfo.ppkg_filename) {
265 free(pkg_fileinfo.ppkg_filename);
266 pkg_fileinfo.ppkg_filename = NULL;
268 if (pkg_fileinfo.ppkgtype) {
269 free(pkg_fileinfo.ppkgtype);
270 pkg_fileinfo.ppkgtype = NULL;
273 /* check is it already installed or not */
274 tmpinfo = _pkgmgr_get_installed_pkg_info(info->package_name);
275 if (tmpinfo != NULL) {
276 /*already installed */
277 *alreay_installed = true;
285 int _pkgmgr_package_install(char *pkgfilepath, bool forceinstall,
286 char *installoptions)
289 pkginfo *info = NULL;
290 pkginfo *tmpinfo = NULL;
291 bool alreadyinstalled = false;
292 if (forceinstall == true && installoptions == NULL)
293 return NATIVEINSTALLER_ERR_WRONG_PARAM;
295 info = _pkgmgr_get_pkg_info(pkgfilepath, &err, &alreadyinstalled);
296 if (err != 0 || info == NULL) {
302 gptrpkgname = strdup(info->package_name);
303 _broadcast_status_notification(info->package_name, "start", "install");
304 _broadcast_status_notification(info->package_name,
305 "command", "Install");
308 if (alreadyinstalled == true && forceinstall != true) {
309 tmpinfo = _pkgmgr_get_installed_pkg_info(info->package_name);
311 return NATIVEINSTALLER_ERR_PACKAGE_NOT_INSTALLED;
313 d_msg_backend(DEBUG_ERR,
314 "installed version is %s and to be "
315 "installed version is %s\n",
316 tmpinfo->version, info->version);
317 if (strcmp(info->version, tmpinfo->version) == 0) {
318 _set_backend_state_info(GOT_PACKAGE_INFO_SUCCESSFULLY);
319 err = _pkgmgr_dpkg_upgrade_pkg(pkgfilepath,
322 d_msg_backend(DEBUG_ERR,
323 "upgrade complete with error(%d)\n",
335 _set_backend_state_info(DPKG_REQUEST_COMPLETED);
344 return NATIVEINSTALLER_SUCCESS;
345 } else if (strcmp(info->version, tmpinfo->version) > 0) {
347 _set_backend_state_info(GOT_PACKAGE_INFO_SUCCESSFULLY);
348 err = _pkgmgr_dpkg_upgrade_pkg(
349 pkgfilepath, installoptions);
351 d_msg_backend(DEBUG_ERR,
352 "upgrade complete with error(%d)\n",
364 _set_backend_state_info(DPKG_REQUEST_COMPLETED);
373 return NATIVEINSTALLER_SUCCESS;
375 } else if (strcmp(info->version, tmpinfo->version) < 0) {
376 /*show popup and confirm from user if non quiet mode
377 else continue downgrade*/
381 switch (do_upgrade) {
383 _set_backend_state_info(DPKG_REQUEST_PENDING);
392 return NATIVEINSTALLER_ERR_NEED_USER_CONFIRMATION;
403 _set_backend_state_info(DPKG_REQUEST_COMPLETED);
404 return NATIVEINSTALLER_PACKAGE_NOT_UPGRADED;
406 /*continue with downgrade */
407 _set_backend_state_info
408 (GOT_PACKAGE_INFO_SUCCESSFULLY);
410 _pkgmgr_dpkg_upgrade_pkg(pkgfilepath,
413 d_msg_backend(DEBUG_ERR,
414 "upgrade complete with error(%d)\n",
426 _set_backend_state_info(DPKG_REQUEST_COMPLETED);
435 return NATIVEINSTALLER_SUCCESS;
443 _set_backend_state_info(GOT_PACKAGE_INFO_SUCCESSFULLY);
444 err = _pkgmgr_dpkg_install_pkg(pkgfilepath, installoptions);
446 d_msg_backend(DEBUG_ERR,
447 "install complete with error(%d)\n", err);
455 _set_backend_state_info(DPKG_REQUEST_COMPLETED);
460 return NATIVEINSTALLER_SUCCESS;
466 return NATIVEINSTALLER_SUCCESS;
469 int _pkgmgr_package_uninstall(char *pkgname)
473 pkginfo *tmppkginfo = _pkgmgr_get_installed_pkg_info(pkgname);
474 if (tmppkginfo == NULL)
475 return NATIVEINSTALLER_ERR_PACKAGE_NOT_INSTALLED;
479 gptrpkgname = strdup(pkgname);
480 _broadcast_status_notification(pkgname, "start", "uninstall");
481 _broadcast_status_notification(pkgname, "command", "Uninstall");
483 _set_backend_state_info(GOT_PACKAGE_INFO_SUCCESSFULLY);
484 /* call remove package using package installer module */
485 ret = _pkgmgr_dpkg_uninstall_pkg(pkgname);
487 _set_backend_state_info(DPKG_REQUEST_COMPLETED);
492 int _pkgmgr_clear_private_data(char *pkgname)
495 return NATIVEINSTALLER_ERR_WRONG_PARAM;
496 char dir_path[256] = {'\0'};
498 snprintf(dir_path, 255, "/opt/apps/%s/data/", pkgname);
499 ret = __recursive_delete_dir(dir_path);