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.
26 #include <livebox-errno.h> /* For error code */
32 #if defined(_USE_ECORE_TIME_GET)
36 .type = CLOCK_MONOTONIC,
40 int util_check_extension(const char *filename, const char *check_ptr)
44 name_len = strlen(filename);
45 while (--name_len >= 0 && *check_ptr) {
46 if (filename[name_len] != *check_ptr) {
47 return LB_STATUS_ERROR_INVALID;
56 double util_timestamp(void)
58 #if defined(_USE_ECORE_TIME_GET)
62 if (clock_gettime(s_info.type, &ts) == 0) {
63 return ts.tv_sec + ts.tv_nsec / 1000000000.0f;
66 ErrPrint("%d: %s\n", s_info.type, strerror(errno));
67 if (s_info.type == CLOCK_MONOTONIC) {
68 s_info.type = CLOCK_REALTIME;
69 } else if (s_info.type == CLOCK_REALTIME) {
71 if (gettimeofday(&tv, NULL) < 0) {
72 ErrPrint("gettimeofday: %s\n", strerror(errno));
76 return tv.tv_sec + tv.tv_usec / 1000000.0f;
84 if (gettimeofday(&tv, NULL) < 0) {
85 ErrPrint("gettimeofday: %s\n", strerror(errno));
90 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
94 const char *util_basename(const char *name)
97 length = name ? strlen(name) : 0;
102 while (--length > 0 && name[length] != '/');
104 return length <= 0 ? name : name + length + (name[length] == '/');
107 static inline int check_native_livebox(const char *pkgname)
112 len = strlen(pkgname) * 2;
113 len += strlen("/opt/usr/live/%s/libexec/liblive-%s.so");
115 path = malloc(len + 1);
117 ErrPrint("Heap: %s\n", strerror(errno));
118 return LB_STATUS_ERROR_MEMORY;
121 snprintf(path, len, "/opt/usr/live/%s/libexec/liblive-%s.so", pkgname, pkgname);
122 if (access(path, F_OK | R_OK) != 0) {
123 ErrPrint("%s is not a valid package\n", pkgname);
125 return LB_STATUS_ERROR_INVALID;
132 static inline int check_web_livebox(const char *pkgname)
137 len = strlen(pkgname) * 2;
138 len += strlen("/opt/usr/apps/%s/res/wgt/livebox/index.html");
140 path = malloc(len + 1);
142 ErrPrint("Heap: %s\n", strerror(errno));
143 return LB_STATUS_ERROR_MEMORY;
146 snprintf(path, len, "/opt/usr/apps/%s/res/wgt/livebox/index.html", pkgname);
147 if (access(path, F_OK | R_OK) != 0) {
148 ErrPrint("%s is not a valid package\n", pkgname);
150 return LB_STATUS_ERROR_INVALID;
157 int util_validate_livebox_package(const char *pkgname)
160 ErrPrint("Invalid argument\n");
161 return LB_STATUS_ERROR_INVALID;
164 if (!check_native_livebox(pkgname) || !check_web_livebox(pkgname)) {
168 return LB_STATUS_ERROR_INVALID;
171 const char *util_uri_to_path(const char *uri)
175 len = strlen(SCHEMA_FILE);
176 if (strncasecmp(uri, SCHEMA_FILE, len)) {
183 int util_unlink(const char *filename)
190 return LB_STATUS_ERROR_INVALID;
193 desclen = strlen(filename) + 6; /* .desc */
194 descfile = malloc(desclen);
196 ErrPrint("Heap: %s\n", strerror(errno));
197 return LB_STATUS_ERROR_MEMORY;
200 ret = snprintf(descfile, desclen, "%s.desc", filename);
202 ErrPrint("Error: %s\n", strerror(errno));
204 return LB_STATUS_ERROR_FAULT;
207 (void)unlink(descfile);
209 (void)unlink(filename);
211 return LB_STATUS_SUCCESS;