2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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.
24 #include <sys/types.h>
31 #include <livebox-errno.h>
39 HAPI unsigned long util_string_hash(const char *str)
41 unsigned long ret = 0;
44 ret += (unsigned long)(*str++);
50 HAPI double util_timestamp(void)
54 if (gettimeofday(&tv, NULL) < 0) {
55 static unsigned long internal_count = 0;
56 ErrPrint("failed to get time of day: %s\n", strerror(errno));
57 tv.tv_sec = internal_count++;
61 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
64 HAPI int util_check_ext(const char *filename, const char *check_ptr)
68 name_len = strlen(filename);
69 while (--name_len >= 0 && *check_ptr) {
70 if (filename[name_len] != *check_ptr)
71 return LB_STATUS_ERROR_INVALID;
76 return LB_STATUS_SUCCESS;
79 static inline int check_native_livebox(const char *pkgname)
84 len = strlen(pkgname) * 2;
85 len += strlen(ROOT_PATH);
86 len += strlen("%s/libexec/liblive-%s.so");
88 path = malloc(len + 1);
90 ErrPrint("Heap: %s\n", strerror(errno));
91 return LB_STATUS_ERROR_MEMORY;
94 snprintf(path, len, "%s%s/libexec/liblive-%s.so", ROOT_PATH, pkgname, pkgname);
95 if (access(path, F_OK | R_OK) != 0) {
96 ErrPrint("%s is not a valid package\n", pkgname);
98 return LB_STATUS_ERROR_INVALID;
102 return LB_STATUS_SUCCESS;
105 static inline int check_web_livebox(const char *pkgname)
110 len = strlen(pkgname) * 2;
111 len += strlen("/opt/usr/apps/%s/res/wgt/livebox/index.html");
113 path = malloc(len + 1);
115 ErrPrint("Heap: %s\n", strerror(errno));
116 return LB_STATUS_ERROR_MEMORY;
119 snprintf(path, len, "/opt/usr/apps/%s/res/wgt/livebox/index.html", pkgname);
120 if (access(path, F_OK | R_OK) != 0) {
121 ErrPrint("%s is not a valid package\n", pkgname);
123 return LB_STATUS_ERROR_INVALID;
127 return LB_STATUS_SUCCESS;
130 HAPI int util_validate_livebox_package(const char *pkgname)
133 ErrPrint("Invalid argument\n");
134 return LB_STATUS_ERROR_INVALID;
137 if (!check_native_livebox(pkgname) || !check_web_livebox(pkgname))
138 return LB_STATUS_SUCCESS;
140 return LB_STATUS_ERROR_INVALID;
143 HAPI int util_unlink(const char *filename)
150 return LB_STATUS_ERROR_INVALID;
152 desclen = strlen(filename) + 6; /* .desc */
153 descfile = malloc(desclen);
155 ErrPrint("Heap: %s\n", strerror(errno));
156 return LB_STATUS_ERROR_MEMORY;
159 ret = snprintf(descfile, desclen, "%s.desc", filename);
161 ErrPrint("Error: %s\n", strerror(errno));
163 return LB_STATUS_ERROR_FAULT;
166 (void)unlink(descfile);
168 (void)unlink(filename);
170 return LB_STATUS_SUCCESS;
173 HAPI char *util_slavename(void)
175 char slavename[BUFSIZ];
176 static unsigned long idx = 0;
178 snprintf(slavename, sizeof(slavename), "%lu_%lf", idx++, util_timestamp());
179 return strdup(slavename);
182 HAPI const char *util_basename(const char *name)
185 length = name ? strlen(name) : 0;
189 while (--length > 0 && name[length] != '/');
191 return length <= 0 ? name : (name + length + (name[length] == '/'));
195 * Return size of stroage in MegaBytes unit.
197 HAPI unsigned long long util_free_space(const char *path)
200 unsigned long long space;
202 if (statfs(path, &st) < 0) {
203 ErrPrint("statvfs: %s\n", strerror(errno));
207 space = (unsigned long long)st.f_bsize * (unsigned long long)st.f_bavail;
208 DbgPrint("Available size: %llu, f_bsize: %lu, f_bavail: %lu\n", space, st.f_bsize, st.f_bavail);
211 * Must have to check the overflow
216 static inline char *extend_heap(char *buffer, int *sz, int incsz)
221 tmp = realloc(buffer, *sz);
223 ErrPrint("Heap: %s\n", strerror(errno));
231 HAPI char *util_replace_string(const char *src, const char *pattern, const char *replace)
246 if (!src || !pattern)
249 out_sz = strlen(src);
252 ErrPrint("Heap: %s\n", strerror(errno));
257 for (state = STATE_START, ptr = src; state != STATE_END; ptr++) {
262 } else if (!isblank(*ptr)) {
270 } else if (*ptr == *pattern) {
277 if (out_idx == out_sz) {
278 tmp = extend_heap(ret, &out_sz, strlen(replace) + 1);
290 * If there is no space for copying the replacement,
291 * Extend size of the return buffer.
293 if (out_sz - out_idx < strlen(replace) + 1) {
294 tmp = extend_heap(ret, &out_sz, strlen(replace) + 1);
302 strcpy(ret + out_idx, replace);
303 out_idx += strlen(replace);
307 } else if (*ptr != pattern[idx]) {
310 /* Copy the first matched character */
313 if (out_idx == out_sz) {
314 tmp = extend_heap(ret, &out_sz, strlen(replace) + 1);
337 HAPI const char *util_uri_to_path(const char *uri)
341 len = strlen(SCHEMA_FILE);
342 if (strncasecmp(uri, SCHEMA_FILE, len))
348 HAPI double util_time_delay_for_compensation(double period)
351 unsigned long long curtime;
352 unsigned long long _period;
353 unsigned long long remain;
356 if (period == 0.0f) {
357 DbgPrint("Period is ZERO\n");
361 if (gettimeofday(&tv, NULL) < 0){
362 ErrPrint("gettimeofday: %s\n", strerror(errno));
366 curtime = (unsigned long long)tv.tv_sec * 1000000llu + (unsigned long long)tv.tv_usec;
367 _period = (unsigned long long)(period * (double)1000000);
368 if (_period == 0llu) {
369 ErrPrint("%lf <> %llu\n", period, _period);
373 remain = curtime % _period;
375 ret = (double)remain / (double)1000000;
379 HAPI void *util_timer_add(double interval, Eina_Bool (*cb)(void *data), void *data)
384 timer = ecore_timer_add(interval, cb, data);
388 delay = util_time_delay_for_compensation(interval) - interval;
389 ecore_timer_delay(timer, delay);
390 DbgPrint("Compensate timer: %lf\n", delay);
395 HAPI void util_timer_interval_set(void *timer, double interval)
398 ecore_timer_interval_set(timer, interval);
400 delay = util_time_delay_for_compensation(interval) - interval;
401 ecore_timer_delay(timer, delay);
404 HAPI char *util_get_file_kept_in_safe(const char *id)
411 path = util_uri_to_path(id);
413 ErrPrint("Invalid URI(%s)\n", id);
420 if (OVERWRITE_CONTENT)
426 while (base_idx > 0 && path[base_idx] != '/') base_idx--;
427 base_idx += (path[base_idx] == '/');
429 new_path = malloc(len + 10);
431 ErrPrint("Heap: %s\n", strerror(errno));
435 strncpy(new_path, path, base_idx);
436 snprintf(new_path + base_idx, len + 10 - base_idx, "reader/%s", path + base_idx);
440 HAPI int util_unlink_files(const char *folder)
444 struct dirent *entry;
448 if (lstat(folder, &info) < 0) {
449 ErrPrint("Error: %s\n", strerror(errno));
450 return LB_STATUS_ERROR_IO;
453 if (!S_ISDIR(info.st_mode)) {
454 ErrPrint("Error: %s is not a folder", folder);
455 return LB_STATUS_ERROR_INVALID;
458 handle = opendir(folder);
460 ErrPrint("Error: %s\n", strerror(errno));
461 return LB_STATUS_ERROR_IO;
464 while ((entry = readdir(handle))) {
465 if (!strcmp(entry->d_name, "."))
468 if (!strcmp(entry->d_name, ".."))
471 len = strlen(folder) + strlen(entry->d_name) + 3;
472 abspath = calloc(1, len);
474 ErrPrint("Heap: %s\n", strerror(errno));
477 snprintf(abspath, len - 1, "%s/%s", folder, entry->d_name);
479 if (unlink(abspath) < 0)
480 DbgPrint("unlink: %s\n", strerror(errno));
486 return LB_STATUS_SUCCESS;