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.
33 #include <sys/types.h>
37 #include "libdebinternals.h"
39 /* This is backend lib's filter string for dlogutil*/
40 #define LOCAL_LOG_TAG "libdeb"
43 #define NATIVE_INSTALLER_LIBDEB_LOG_FILE "/tmp/libdeb"
47 void _libdeb_print_msg(int type, int exetype, char *format, ...)
49 char buffer[FILENAME_MAX] = { 0 };
50 char tbuffer[FILENAME_MAX] = { 0 };
54 va_start(args, format);
55 nbuffer = vsnprintf(tbuffer, FILENAME_MAX, format, args);
60 LOG(LOG_ERROR, LOCAL_LOG_TAG, tbuffer);
63 LOG(LOG_WARN, LOCAL_LOG_TAG, tbuffer);
66 LOG(LOG_DEBUG, LOCAL_LOG_TAG, tbuffer);
74 if (DEBUG_ERR == (logging & type)) {
75 nbuffer = snprintf(buffer, FILENAME_MAX, "ERROR:%s", tbuffer);
76 vfprintf(stderr, format, args);
77 } else if (DEBUG_INFO == (logging & type)) {
78 nbuffer = snprintf(buffer, FILENAME_MAX, "INFO:%s", tbuffer);
79 vfprintf(stdout, format, args);
80 } else if (DEBUG_RESULT == (logging & type)) {
81 nbuffer = snprintf(buffer, FILENAME_MAX, "RESULT:%s", tbuffer);
82 vfprintf(stdout, format, args);
89 fwrite(buffer, sizeof(char), strlen(buffer), logfile);
90 #endif /*LOG_IN_FILE */
93 int _libdeb_xsystem(const char *argv[])
100 perror("fork failed");
104 execvp(argv[0], (char *const *)argv);
111 _libdeb_print(DEBUG_INFO, "parent\n");
112 if (waitpid(pid, &status, 0) == -1) {
113 perror("waitpid failed");
117 if (WIFSIGNALED(status)) {
122 if (!WIFEXITED(status)) {
123 /* shouldn't happen */
124 perror("should not happen");
128 return WEXITSTATUS(status);
131 char *_libdeb_substring(const char *str, size_t begin, size_t len)
133 if (str == 0 || strlen(str) == 0 || strlen(str) < begin ||
134 strlen(str) < (begin + len))
135 return LIBDEB_SUCCESS;
136 return strndup(str + begin, len);
139 char *_libdeb_get_filename(const char *packagefilepath)
142 regmatch_t mached[5];
144 char *package_file_name = NULL;
145 if (packagefilepath == NULL) {
146 _libdeb_print(DEBUG_ERR,
147 "[_libdeb_get_filename] invalid arguments\n");
152 regcomp(&re, "[/]*([.a-z_A-Z0-9-]+\\.([aA][pP][kK]|[dD][eE][bB]))",
155 _libdeb_print(DEBUG_ERR,
156 "[_libdeb_get_filename] regcomp failed(%d)\n",
160 ret = regexec(&re, packagefilepath, 5, mached, 0);
162 _libdeb_print(DEBUG_ERR,
163 "[_libdeb_get_filename] regexec failed(%s)(%d)\n",
164 packagefilepath, ret);
169 package_file_name = _libdeb_substring(packagefilepath, mached[1].rm_so,
173 _libdeb_print(DEBUG_INFO,
174 "[_libdeb_get_filename][%s] -> [%s] \n", packagefilepath,
177 return package_file_name;
180 int _libdeb_fill_pkginfo_from_control_file(package_manager_pkg_detail_info_t
181 *pkg_detail_info, char *line)
184 regmatch_t mached[5];
188 ret = regcomp(&re, "[Pp]ackage[\\ ]*:[\\ ]*([.A-Z0-9a-z_\\-]+)",
193 "[_libdeb_fill_pkginfo_from_control_file]"
194 " regcomp failed(%d)", ret);
197 ret = regexec(&re, line, 5, mached, 0);
201 retstr = _libdeb_substring(line, mached[1].rm_so,
202 mached[1].rm_eo - mached[1].rm_so);
205 "[_libdeb_fill_pkginfo_from_control_file] PackageName=%s\n",
207 strncpy(pkg_detail_info->pkg_name, retstr,
208 sizeof(pkg_detail_info->pkg_name) - 1);
211 return LIBDEB_SUCCESS;
215 "Version[\\ ]*:[\\ ]*([0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3})",
220 "[_libdeb_fill_pkginfo_from_control_file] "
221 "regcomp failed(%d)", ret);
224 ret = regexec(&re, line, 5, mached, 0);
228 retstr = _libdeb_substring(line, mached[1].rm_so,
229 mached[1].rm_eo - mached[1].rm_so);
232 "[_libdeb_fill_pkginfo_from_control_file] Version=%s\n",
234 strncpy(pkg_detail_info->version, retstr,
235 sizeof(pkg_detail_info->version) - 1);
238 return LIBDEB_SUCCESS;
242 ret = regcomp(&re, "[Dd]escription[\\ ]*:[\\ ]*([.A-Z0-9a-z_\\-]+)",
247 "[_libdeb_fill_pkginfo_from_control_file]"
248 " regcomp failed(%d)", ret);
251 ret = regexec(&re, line, 5, mached, 0);
255 retstr = _libdeb_substring(line, mached[1].rm_so,
256 mached[1].rm_eo - mached[1].rm_so);
259 "[_libdeb_fill_pkginfo_from_control_file] Description=%s\n",
261 strncpy(pkg_detail_info->pkg_description, retstr,
262 sizeof(pkg_detail_info->pkg_description) - 1);
265 return LIBDEB_SUCCESS;
270 "[Ii]nstalled-[Ss]ize[\\ ]*:[\\ ]*([.A-Z0-9a-z_\\-]+)",
275 "[_libdeb_fill_pkginfo_from_control_file] "
276 "regcomp failed(%d)", ret);
279 ret = regexec(&re, line, 5, mached, 0);
283 retstr = _libdeb_substring(line, mached[1].rm_so,
284 mached[1].rm_eo - mached[1].rm_so);
287 "[_libdeb_fill_pkginfo_from_control_file] "
288 "Installed Size=%s\n", retstr);
289 pkg_detail_info->app_size = atoi(retstr);
292 return LIBDEB_SUCCESS;
295 return LIBDEB_SUCCESS;
299 int _libdeb_populate_control_info(const char *pkgname,
300 package_manager_pkg_detail_info_t
301 *pkg_detail_info, char *controlfilepath)
304 char *lineptr = NULL;
307 char buff[512] = { 0, };
310 if (strncmp(controlfilepath, PKGMETAINFO, strlen(PKGMETAINFO)))
311 flag = 1; /*directly read from control file of pkg */
313 flag = 0; /* read from dpkg available file */
314 _libdeb_print(DEBUG_INFO, "flag value is %d\n", flag);
315 fp = fopen(controlfilepath, "r");
317 _libdeb_print(DEBUG_ERR,
318 "failed to open file(%s)\n", controlfilepath);
322 snprintf(buff, 512, "Package: %s", pkgname);
323 /*allocate memory for lineptr. If it is less
324 getline will realloc it */
325 lineptr = (char *)malloc(sizeof(char) * 512);
326 if (UNLIKELY(lineptr == NULL)) {
327 _libdeb_print(DEBUG_ERR,
332 memset(lineptr, 0x00, sizeof(char) * 512);
335 memset(lineptr, 0x00, sizeof(char) * 512);
336 read = getline(&lineptr, &len, fp);
342 return LIBDEB_SUCCESS;
344 _libdeb_print(DEBUG_ERR, "getline failed\n");
349 /*got the entry in available file. Now read till
350 next empty line and break */
351 /*10 is ASCII of newline \n */
355 _libdeb_fill_pkginfo_from_control_file
356 (pkg_detail_info, lineptr);
362 if ((strncmp(lineptr, buff, strlen(buff)) == 0) &&
363 (strlen(lineptr) == strlen(buff) + 1)) {
365 /*Parse the fields now */
367 _libdeb_fill_pkginfo_from_control_file
368 (pkg_detail_info, lineptr);
378 } while (lineptr != NULL);
385 return LIBDEB_SUCCESS;
390 long long _libdeb_calculate_dir_size(char *dirname)
394 int q = 0; /*quotient*/
395 int r = 0; /*remainder*/
397 struct dirent *ep = NULL;
398 struct stat fileinfo;
399 char abs_filename[FILENAME_MAX] = { 0, };
400 if (dirname == NULL) {
401 _libdeb_print(DEBUG_ERR,
405 dp = opendir(dirname);
407 while ((ep = readdir(dp)) != NULL) {
408 if (!strcmp(ep->d_name, ".") ||
409 !strcmp(ep->d_name, "..")) {
412 snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname,
414 if (stat(abs_filename, &fileinfo) < 0)
415 perror(abs_filename);
417 if (S_ISDIR(fileinfo.st_mode)) {
418 total += fileinfo.st_size;
419 if (strcmp(ep->d_name, ".")
420 && strcmp(ep->d_name, "..")) {
421 ret = _libdeb_calculate_dir_size
426 /*It is a file. Calculate the actual
427 size occupied (in terms of 4096 blocks)*/
428 q = (fileinfo.st_size / BLOCK_SIZE);
429 r = (fileinfo.st_size % BLOCK_SIZE);
433 total += q * BLOCK_SIZE;
439 _libdeb_print(DEBUG_ERR,
440 "Couldn't open the directory\n");