Initialize Tizen 2.3
[apps/livebox/livebox-viewer.git] / src / util.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <sys/time.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <time.h>
24
25 #include <dlog.h>
26 #include <livebox-errno.h> /* For error code */
27
28 #include "debug.h"
29 #include "util.h"
30
31 int errno;
32 #if defined(_USE_ECORE_TIME_GET)
33 static struct {
34         clockid_t type;
35 } s_info = {
36         .type = CLOCK_MONOTONIC,
37 };
38 #endif
39
40 int util_check_extension(const char *filename, const char *check_ptr)
41 {
42         int name_len;
43
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;
48                 }
49
50                 check_ptr ++;
51         }
52
53         return 0;
54 }
55
56 double util_timestamp(void)
57 {
58 #if defined(_USE_ECORE_TIME_GET)
59         struct timespec ts;
60
61         do {
62                 if (clock_gettime(s_info.type, &ts) == 0) {
63                         return ts.tv_sec + ts.tv_nsec / 1000000000.0f;
64                 }
65
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) {
70                         struct timeval tv;
71                         if (gettimeofday(&tv, NULL) < 0) {
72                                 ErrPrint("gettimeofday: %s\n", strerror(errno));
73                                 break;
74                         }
75
76                         return tv.tv_sec + tv.tv_usec / 1000000.0f;
77                 }
78         } while (1);
79
80         return 0.0f;
81 #else
82         struct timeval tv;
83
84         if (gettimeofday(&tv, NULL) < 0) {
85                 ErrPrint("gettimeofday: %s\n", strerror(errno));
86                 tv.tv_sec = 0;
87                 tv.tv_usec = 0;
88         }
89
90         return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
91 #endif
92 }
93
94 const char *util_basename(const char *name)
95 {
96         int length;
97         length = name ? strlen(name) : 0;
98         if (!length) {
99                 return ".";
100         }
101
102         while (--length > 0 && name[length] != '/');
103
104         return length <= 0 ? name : name + length + (name[length] == '/');
105 }
106
107 static inline int check_native_livebox(const char *pkgname)
108 {
109         int len;
110         char *path;
111
112         len = strlen(pkgname) * 2;
113         len += strlen("/opt/usr/live/%s/libexec/liblive-%s.so");
114
115         path = malloc(len + 1);
116         if (!path) {
117                 ErrPrint("Heap: %s\n", strerror(errno));
118                 return LB_STATUS_ERROR_MEMORY;
119         }
120
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);
124                 free(path);
125                 return LB_STATUS_ERROR_INVALID;
126         }
127
128         free(path);
129         return 0;
130 }
131
132 static inline int check_web_livebox(const char *pkgname)
133 {
134         int len;
135         char *path;
136
137         len = strlen(pkgname) * 2;
138         len += strlen("/opt/usr/apps/%s/res/wgt/livebox/index.html");
139
140         path = malloc(len + 1);
141         if (!path) {
142                 ErrPrint("Heap: %s\n", strerror(errno));
143                 return LB_STATUS_ERROR_MEMORY;
144         }
145
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);
149                 free(path);
150                 return LB_STATUS_ERROR_INVALID;
151         }
152
153         free(path);
154         return 0;
155 }
156
157 int util_validate_livebox_package(const char *pkgname)
158 {
159         if (!pkgname) {
160                 ErrPrint("Invalid argument\n");
161                 return LB_STATUS_ERROR_INVALID;
162         }
163
164         if (!check_native_livebox(pkgname) || !check_web_livebox(pkgname)) {
165                 return 0;
166         }
167
168         return LB_STATUS_ERROR_INVALID;
169 }
170
171 const char *util_uri_to_path(const char *uri)
172 {
173         int len;
174
175         len = strlen(SCHEMA_FILE);
176         if (strncasecmp(uri, SCHEMA_FILE, len)) {
177                 return NULL;
178         }
179
180         return uri + len;
181 }
182
183 int util_unlink(const char *filename)
184 {
185         char *descfile;
186         int desclen;
187         int ret;
188
189         if (!filename) {
190                 return LB_STATUS_ERROR_INVALID;
191         }
192
193         desclen = strlen(filename) + 6; /* .desc */
194         descfile = malloc(desclen);
195         if (!descfile) {
196                 ErrPrint("Heap: %s\n", strerror(errno));
197                 return LB_STATUS_ERROR_MEMORY;
198         }
199
200         ret = snprintf(descfile, desclen, "%s.desc", filename);
201         if (ret < 0) {
202                 ErrPrint("Error: %s\n", strerror(errno));
203                 free(descfile);
204                 return LB_STATUS_ERROR_FAULT;
205         }
206
207         (void)unlink(descfile);
208         free(descfile);
209         (void)unlink(filename);
210
211         return LB_STATUS_SUCCESS;
212 }
213
214 /* End of a file */