2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <sys/statvfs.h>
29 #include "livebox-errno.h"
33 static inline char *check_native_livebox(const char *pkgname)
38 len = strlen(pkgname) * 2;
39 len += strlen("/opt/usr/live/%s/libexec/liblive-%s.so");
41 path = malloc(len + 1);
43 ErrPrint("Heap: %s\n", strerror(errno));
47 snprintf(path, len, "/opt/usr/live/%s/libexec/liblive-%s.so", pkgname, pkgname);
48 if (access(path, F_OK | R_OK) != 0) {
49 ErrPrint("%s is not a valid package (%s)\n", pkgname, strerror(errno));
57 static inline char *check_web_livebox(const char *pkgname)
62 len = strlen(pkgname) * 2;
63 len += strlen("/opt/usr/apps/%s/res/wgt/livebox/index.html");
65 path = malloc(len + 1);
67 ErrPrint("Heap: %s\n", strerror(errno));
71 snprintf(path, len, "/opt/usr/apps/%s/res/wgt/livebox/index.html", pkgname);
72 if (access(path, F_OK | R_OK) != 0) {
73 ErrPrint("%s is not a valid package (%s)\n", pkgname, strerror(errno));
81 int util_validate_livebox_package(const char *pkgname)
86 ErrPrint("Invalid argument\n");
87 return LB_STATUS_ERROR_INVALID;
90 path = check_native_livebox(pkgname);
96 path = check_web_livebox(pkgname);
102 return LB_STATUS_ERROR_INVALID;
105 double util_timestamp(void)
109 gettimeofday(&tv, NULL);
111 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
114 const char *util_basename(const char *name)
117 length = name ? strlen(name) : 0;
121 while (--length > 0 && name[length] != '/');
123 return length <= 0 ? name : (name + length + (name[length] == '/'));
126 unsigned long util_free_space(const char *path)
131 if (statvfs(path, &st) < 0) {
132 ErrPrint("statvfs: %s\n", strerror(errno));
136 space = st.f_bsize * st.f_bfree;
139 * Must have to check the overflow
144 char *util_replace_string(const char *src, const char *pattern, const char *replace)
157 if (!src || !pattern || !replace || !src[0] || !pattern[0]) {
158 ErrPrint("Invalid argument: %s %s %s\n", src, pattern, replace);
162 rlen = strlen(replace);
164 result = malloc(len + 1);
166 ErrPrint("Heap:%s\n", strerror(errno));
173 for (s_idx = 0; src[s_idx]; s_idx++) {
177 len += (rlen > len ? rlen : len);
178 tmp = realloc(result, len + 1);
180 ErrPrint("Heap: %s\n", strerror(errno));
187 if (src[s_idx] == pattern[0]) {
194 len += (rlen > len ? rlen : len);
195 tmp = realloc(result, len + 1);
197 ErrPrint("Heap: %s\n", strerror(errno));
203 result[r_idx++] = src[t_idx++];
205 while (pattern[p_idx]) {
206 if (src[t_idx] == pattern[p_idx]) {
208 if (src[t_idx] == pattern[0]) {
213 len += (rlen > len ? rlen : len);
214 tmp = realloc(result, len + 1);
216 ErrPrint("Heap: %s\n", strerror(errno));
222 result[r_idx++] = src[t_idx];
239 if (pattern[p_idx] == '\0') {
240 if (idx + rlen >= len) {
242 len += (rlen > len ? rlen : len);
243 tmp = realloc(result, len + 1);
245 ErrPrint("Heap: %s\n", strerror(errno));
252 strcpy(result + idx, replace);
253 idx += strlen(replace);
257 s_idx = (n_idx < 0 ? t_idx : n_idx) - 1;
260 result[idx++] = src[s_idx];
274 const char *util_uri_to_path(const char *uri)
278 len = strlen(SCHEMA_FILE);
279 if (strncasecmp(uri, SCHEMA_FILE, len))
285 char *util_conf_get_libexec(const char *pkgname)
290 ErrPrint("Invalid argument\n");
294 path = check_native_livebox(pkgname);
296 path = check_web_livebox(pkgname);
301 char *util_id_to_uri(const char *id)
308 ErrPrint("Heap: %s\n", strerror(errno));
311 } else if (strncmp(id, SCHEMA_FILE, strlen(SCHEMA_FILE))) {
313 len = strlen(SCHEMA_FILE) + strlen(id) + 2;
316 ErrPrint("Heap: %s\n", strerror(errno));
320 snprintf(uri, len, SCHEMA_FILE"%s", id);
324 ErrPrint("Heap: %s\n", strerror(errno));