// // Open Service Platform // Copyright (c) 2012 Samsung Electronics Co., Ltd. // // Licensed under the Apache License, Version 2.0 (the License); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // /** * @file osp-installer-plugin.cpp */ #include #include #include #include #include #include #include #include #include #include #include #include #include "InstallerDefs.h" using namespace Tizen::Base; using namespace Tizen::Io; using namespace Tizen::App; using namespace Tizen::App::Package; #ifdef __cplusplus extern "C" { #endif static const int PKG_SUCCESS = 0; static const int PKG_ERROR = 1; static int pkg_get_installed_size(const String& rootPath); static int pkg_get_data_size(const String& rootPath); static void pkg_plugin_unload(void) { AppLog("[libtpk] pkg_plugin_unload() is called"); } static int pkg_plugin_pkg_is_installed(const char *pkg_name) { int err = PKG_SUCCESS; String packageName(pkg_name); PackageId packageId; bool installed = true; AppLog("[libtpk] pkg_plugin_pkg_is_installed() is called"); AppLog("[libtpk] pkg_name=[%s]", pkg_name); TryCatch(pkg_name, err = PKG_ERROR, "[osp-installer][libtpk] pkg_name is null"); if (packageName.Contains(PACKAGE_NAME_PREFIX_ORG) == true) { String prefixPackage(PACKAGE_NAME_PREFIX_ORG); packageName.SubString(prefixPackage.GetLength(), PACKAGE_ID_LENGTH, packageId); } else { packageName.SubString(0, PACKAGE_ID_LENGTH, packageId); } installed = _PackageManagerImpl::GetInstance()->IsPackageInstalled(packageId); TryCatch(installed == true, err = PKG_ERROR, "[osp-installer][libtpk] IsPackageInstalled() is failed"); AppLog("[libtpk] IsPackageInstalled=[%d]", installed); err = PKG_SUCCESS; CATCH: return err; } static int pkg_plugin_get_installed_pkg_list(const char *category, const char *option, package_manager_pkg_info_t **list, int *count) { AppLog("[libtpk] pkg_plugin_get_installed_pkg_list() is called"); return PKG_ERROR; } static int pkg_plugin_get_pkg_detail_info(const char *pkg_name, package_manager_pkg_detail_info_t *pkg_detail_info) { bool err = PKG_SUCCESS; String packageName(pkg_name); String appId; char* pVersion = null; _PackageInfoImpl pkgInfoImpl; result r = E_SUCCESS; AppLog("[libtpk] pkg_plugin_get_pkg_detail_info() is called"); AppLog("[libtpk] pkg_name=[%s]", pkg_name); TryCatch(pkg_name, err = PKG_ERROR, "[osp-installer][libtpk] pkg_name is null"); memset(pkg_detail_info, 0, sizeof(package_manager_pkg_detail_info_t)); if (packageName.Contains(PACKAGE_NAME_PREFIX_ORG) == true) { String prefixPackage(PACKAGE_NAME_PREFIX_ORG); packageName.SubString(prefixPackage.GetLength(), PACKAGE_ID_LENGTH, appId); } else { packageName.SubString(0, PACKAGE_ID_LENGTH, appId); } r = pkgInfoImpl.Construct(appId); TryCatch(!IsFailed(r), err = PKG_ERROR, "[osp-installer][libtpk] pkgInfoImpl.Construct is failed"); strncpy(pkg_detail_info->pkg_type, "tpk", PKG_TYPE_STRING_LEN_MAX); strncpy(pkg_detail_info->pkg_name, pkg_name, PKG_NAME_STRING_LEN_MAX-1); pVersion = _StringConverter::CopyToCharArrayN(pkgInfoImpl.GetVersion()); TryCatch(pVersion, err = PKG_ERROR, "[osp-installer][libtpk] pVersion is null"); strncpy(pkg_detail_info->version, pVersion, PKG_VERSION_STRING_LEN_MAX-1); delete [] pVersion; pVersion = null; pkg_detail_info->installed_size = pkg_get_installed_size(pkgInfoImpl.GetAppRootPath()); pkg_detail_info->data_size = pkg_get_data_size(pkgInfoImpl.GetAppRootPath()); pkg_detail_info->app_size = pkg_detail_info->installed_size - pkg_detail_info->data_size; err = PKG_SUCCESS; CATCH: delete [] pVersion; return err; } static int pkg_plugin_get_pkg_detail_info_from_package(const char *pkg_path, package_manager_pkg_detail_info_t *pkg_detail_info) { AppLog("[libtpk] pkg_plugin_get_pkg_detail_info_from_package() is called"); return PKG_ERROR; } long long pkg_get_directory_size(const String& rootPath) { long long total = 0; long long ret = 0; int q = 0; int r = 0; DIR *dp = NULL; struct dirent *ep = NULL; struct stat fileinfo; char fileName[FILENAME_MAX] = { 0, }; char *pDirName = null; pDirName = _StringConverter::CopyToCharArrayN(rootPath); TryCatch(pDirName, total = 0, "[osp-installer][libtpk] pDirName is null"); dp = opendir(pDirName); TryCatch(dp, total = 0, "[osp-installer][libtpk] dp is null"); while ((ep = readdir(dp)) != NULL) { if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..")) { continue; } snprintf(fileName, FILENAME_MAX, "%s/%s", pDirName, ep->d_name); if (lstat(fileName, &fileinfo) < 0) { continue; } if (S_ISLNK(fileinfo.st_mode)) { AppLog("[libtpk] SYMLINK=%s", fileName); total += BLOCK_SIZE; continue; } else if (S_ISDIR(fileinfo.st_mode)) { ret = pkg_get_directory_size(fileName); ret += fileinfo.st_size; total += ret; AppLog("[libtpk] DIR=%s, size=%d[%dK],", fileName, (int)ret, (int)(ret/1024)); } else { /*It is a file. Calculate the actual size occupied (in terms of 4096 blocks)*/ q = (fileinfo.st_size / BLOCK_SIZE); r = (fileinfo.st_size % BLOCK_SIZE); if (r) { q++; } total += q * BLOCK_SIZE; if (q) { AppLog("[libtpk] File=%s, size=%d[%dK]", fileName, (q * BLOCK_SIZE), (q * BLOCK_SIZE)/1024); } } } closedir(dp); dp = null; CATCH: delete [] pDirName; return total; } int pkg_get_installed_size(const String& rootPath) { String destPath; long long size = 0; size = pkg_get_directory_size(rootPath); return (int)size; } int pkg_get_data_size(const String& rootPath) { String destPath; long long size = 0; destPath = rootPath + DIR_DATA; size = pkg_get_directory_size(destPath); size += BLOCK_SIZE; return (int)size; } __attribute__ ((visibility("default"))) int pkg_plugin_on_load(pkg_plugin_set *set) { memset(set, 0, sizeof(pkg_plugin_set)); set->plugin_on_unload = pkg_plugin_unload; set->pkg_is_installed = pkg_plugin_pkg_is_installed; set->get_installed_pkg_list = pkg_plugin_get_installed_pkg_list; set->get_pkg_detail_info = pkg_plugin_get_pkg_detail_info; set->get_pkg_detail_info_from_package = pkg_plugin_get_pkg_detail_info_from_package; return PKG_SUCCESS; } #ifdef __cplusplus } #endif