2 * Copyright 2012 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://www.tizenopensource.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>
24 #include <sys/types.h>
37 HAPI unsigned long util_string_hash(const char *str)
39 unsigned long ret = 0;
42 ret += (unsigned long)(*str++);
48 HAPI double util_timestamp(void)
52 if (gettimeofday(&tv, NULL) < 0) {
53 static unsigned long internal_count = 0;
54 ErrPrint("failed to get time of day: %s\n", strerror(errno));
55 tv.tv_sec = internal_count++;
59 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
62 HAPI int util_check_ext(const char *filename, const char *check_ptr)
66 name_len = strlen(filename);
67 while (--name_len >= 0 && *check_ptr) {
68 if (filename[name_len] != *check_ptr)
77 static inline int check_native_livebox(const char *pkgname)
82 len = strlen(pkgname) * 2;
83 len += strlen(ROOT_PATH);
84 len += strlen("%s/libexec/liblive-%s.so");
86 path = malloc(len + 1);
88 ErrPrint("Heap: %s\n", strerror(errno));
92 snprintf(path, len, "%s%s/libexec/liblive-%s.so", ROOT_PATH, pkgname, pkgname);
93 if (access(path, F_OK | R_OK) != 0) {
94 ErrPrint("%s is not a valid package\n", pkgname);
103 static inline int check_web_livebox(const char *pkgname)
108 len = strlen(pkgname) * 2;
109 len += strlen("/opt/usr/apps/%s/res/wgt/livebox/index.html");
111 path = malloc(len + 1);
113 ErrPrint("Heap: %s\n", strerror(errno));
117 snprintf(path, len, "/opt/usr/apps/%s/res/wgt/livebox/index.html", pkgname);
118 if (access(path, F_OK | R_OK) != 0) {
119 ErrPrint("%s is not a valid package\n", pkgname);
128 HAPI int util_validate_livebox_package(const char *pkgname)
131 ErrPrint("Invalid argument\n");
135 if (!check_native_livebox(pkgname) || !check_web_livebox(pkgname))
141 HAPI int util_unlink(const char *filename)
147 desclen = strlen(filename) + 6; /* .desc */
148 descfile = malloc(desclen);
150 ErrPrint("Heap: %s\n", strerror(errno));
154 ret = snprintf(descfile, desclen, "%s.desc", filename);
156 ErrPrint("Error: %s\n", strerror(errno));
161 (void)unlink(descfile);
163 (void)unlink(filename);
168 HAPI char *util_slavename(void)
170 char slavename[BUFSIZ];
171 static unsigned long idx = 0;
173 snprintf(slavename, sizeof(slavename), "%lu_%lf", idx++, util_timestamp());
174 return strdup(slavename);
177 HAPI const char *util_basename(const char *name)
180 length = name ? strlen(name) : 0;
184 while (--length > 0 && name[length] != '/');
186 return length <= 0 ? name : (name + length + (name[length] == '/'));
189 HAPI unsigned long util_free_space(const char *path)
194 if (statvfs(path, &st) < 0) {
195 ErrPrint("statvfs: %s\n", strerror(errno));
199 space = st.f_bsize * st.f_bfree;
202 * Must have to check the overflow
207 static inline char *extend_heap(char *buffer, int *sz, int incsz)
212 tmp = realloc(buffer, *sz);
214 ErrPrint("Heap: %s\n", strerror(errno));
221 HAPI char *util_replace_string(const char *src, const char *pattern, const char *replace)
226 int src_rollback_idx;
227 int ret_rollback_idx;
236 ret = malloc(bufsz + 1);
238 ErrPrint("Heap: %s\n", strerror(errno));
245 for (src_idx = 0; src[src_idx]; src_idx++) {
246 if (!pattern[pattern_idx]) {
247 while (replace[target_idx]) {
248 ret[ret_idx] = replace[target_idx];
252 if (ret_idx >= bufsz) {
255 tmp = extend_heap(ret, &bufsz, incsz);
257 ErrPrint("Heap: %s\n", strerror(errno));
269 } else if (src[src_idx] == pattern[pattern_idx]) {
270 if (pattern_idx == 0) {
271 src_rollback_idx = src_idx;
272 ret_rollback_idx = ret_idx;
276 if (replace[target_idx]) {
277 ret[ret_idx] = replace[target_idx];
280 if (ret_idx >= bufsz) {
283 tmp = extend_heap(ret, &bufsz, incsz);
285 ErrPrint("Heap: %s\n", strerror(errno));
295 } else if (pattern_idx > 0) {
296 src_idx = src_rollback_idx;
297 ret_idx = ret_rollback_idx;
301 ret[ret_idx] = src[src_idx];
303 if (ret_idx >= bufsz) {
306 tmp = extend_heap(ret, &bufsz, incsz);
308 ErrPrint("Heap: %s\n", strerror(errno));
324 HAPI const char *util_uri_to_path(const char *uri)
328 len = strlen(SCHEMA_FILE);
329 if (strncasecmp(uri, SCHEMA_FILE, len))
335 static inline void compensate_timer(Ecore_Timer *timer)
338 struct timeval compensator;
342 if (ecore_timer_interval_get(timer) <= 1.0f) {
343 DbgPrint("Doesn't need to sync the timer to start from ZERO sec\n");
347 if (gettimeofday(&tv, NULL) < 0) {
348 ErrPrint("Error: %s\n", strerror(errno));
352 compensator.tv_sec = tv.tv_sec % 60;
353 if (compensator.tv_sec == 0)
354 compensator.tv_sec = 59;
356 delay = 60.0f - ((double)compensator.tv_sec + ((double)tv.tv_usec / 1000000.0f));
357 pending = ecore_timer_pending_get(timer);
358 ecore_timer_delay(timer, delay - pending);
359 DbgPrint("COMPENSATED: %lf\n", delay);
362 HAPI void *util_timer_add(double interval, Eina_Bool (*cb)(void *data), void *data)
366 timer = ecore_timer_add(interval, cb, data);
370 compensate_timer(timer);
374 HAPI void util_timer_interval_set(void *timer, double interval)
376 ecore_timer_interval_set(timer, interval);
377 compensate_timer(timer);
380 HAPI char *util_get_file_kept_in_safe(const char *id)
387 path = util_uri_to_path(id);
389 ErrPrint("Invalid URI(%s)\n", id);
396 if (OVERWRITE_CONTENT)
402 while (base_idx > 0 && path[base_idx] != '/') base_idx--;
403 base_idx += (path[base_idx] == '/');
405 new_path = malloc(len + 10);
407 ErrPrint("Heap: %s\n", strerror(errno));
411 strncpy(new_path, path, base_idx);
412 snprintf(new_path + base_idx, len + 10 - base_idx, "reader/%s", path + base_idx);
416 HAPI int util_unlink_files(const char *folder)
420 struct dirent *entry;
424 if (lstat(folder, &info) < 0) {
425 ErrPrint("Error: %s\n", strerror(errno));
429 if (!S_ISDIR(info.st_mode)) {
430 ErrPrint("Error: %s is not a folder", folder);
434 handle = opendir(folder);
436 ErrPrint("Error: %s\n", strerror(errno));
440 while ((entry = readdir(handle))) {
441 if (!strcmp(entry->d_name, "."))
444 if (!strcmp(entry->d_name, ".."))
447 len = strlen(folder) + strlen(entry->d_name) + 3;
448 abspath = calloc(1, len);
450 ErrPrint("Heap: %s\n", strerror(errno));
453 snprintf(abspath, len - 1, "%s/%s", folder, entry->d_name);
455 if (unlink(abspath) < 0)
456 DbgPrint("unlink: %s - %s\n", abspath, strerror(errno));