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.
25 #include <livebox-errno.h> /* For error code */
32 int util_check_extension(const char *filename, const char *check_ptr)
36 name_len = strlen(filename);
37 while (--name_len >= 0 && *check_ptr) {
38 if (filename[name_len] != *check_ptr)
39 return LB_STATUS_ERROR_INVALID;
47 double util_timestamp(void)
51 gettimeofday(&tv, NULL);
53 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
56 const char *util_basename(const char *name)
59 length = name ? strlen(name) : 0;
63 while (--length > 0 && name[length] != '/');
65 return length <= 0 ? name : name + length + (name[length] == '/');
68 static inline int check_native_livebox(const char *pkgname)
73 len = strlen(pkgname) * 2;
74 len += strlen("/opt/usr/live/%s/libexec/liblive-%s.so");
76 path = malloc(len + 1);
78 ErrPrint("Heap: %s\n", strerror(errno));
79 return LB_STATUS_ERROR_MEMORY;
82 snprintf(path, len, "/opt/usr/live/%s/libexec/liblive-%s.so", pkgname, pkgname);
83 if (access(path, F_OK | R_OK) != 0) {
84 ErrPrint("%s is not a valid package\n", pkgname);
86 return LB_STATUS_ERROR_INVALID;
93 static inline int check_web_livebox(const char *pkgname)
98 len = strlen(pkgname) * 2;
99 len += strlen("/opt/usr/apps/%s/res/wgt/livebox/index.html");
101 path = malloc(len + 1);
103 ErrPrint("Heap: %s\n", strerror(errno));
104 return LB_STATUS_ERROR_MEMORY;
107 snprintf(path, len, "/opt/usr/apps/%s/res/wgt/livebox/index.html", pkgname);
108 if (access(path, F_OK | R_OK) != 0) {
109 ErrPrint("%s is not a valid package\n", pkgname);
111 return LB_STATUS_ERROR_INVALID;
118 int util_validate_livebox_package(const char *pkgname)
121 ErrPrint("Invalid argument\n");
122 return LB_STATUS_ERROR_INVALID;
125 if (!check_native_livebox(pkgname) || !check_web_livebox(pkgname))
128 return LB_STATUS_ERROR_INVALID;
131 const char *util_uri_to_path(const char *uri)
135 len = strlen(SCHEMA_FILE);
136 if (strncasecmp(uri, SCHEMA_FILE, len))
142 int util_unlink(const char *filename)
149 return LB_STATUS_ERROR_INVALID;
151 desclen = strlen(filename) + 6; /* .desc */
152 descfile = malloc(desclen);
154 ErrPrint("Heap: %s\n", strerror(errno));
155 return LB_STATUS_ERROR_MEMORY;
158 ret = snprintf(descfile, desclen, "%s.desc", filename);
160 ErrPrint("Error: %s\n", strerror(errno));
162 return LB_STATUS_ERROR_FAULT;
165 (void)unlink(descfile);
167 (void)unlink(filename);
169 return LB_STATUS_SUCCESS;