1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
/*
* Copyright 2013 Samsung Electronics Co., Ltd
*
* Licensed under the Flora License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <dlog.h>
#include <Eina.h>
#include <Ecore.h>
#include <livebox-errno.h>
#include "util.h"
#include "debug.h"
#include "conf.h"
int errno;
HAPI unsigned long util_string_hash(const char *str)
{
unsigned long ret = 0;
while (*str)
ret += (unsigned long)(*str++);
ret %= 371773;
return ret;
}
HAPI double util_timestamp(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) {
static unsigned long internal_count = 0;
ErrPrint("failed to get time of day: %s\n", strerror(errno));
tv.tv_sec = internal_count++;
tv.tv_usec = 0;
}
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
}
HAPI int util_check_ext(const char *filename, const char *check_ptr)
{
int name_len;
name_len = strlen(filename);
while (--name_len >= 0 && *check_ptr) {
if (filename[name_len] != *check_ptr)
return LB_STATUS_ERROR_INVALID;
check_ptr ++;
}
return LB_STATUS_SUCCESS;
}
static inline int check_native_livebox(const char *pkgname)
{
int len;
char *path;
len = strlen(pkgname) * 2;
len += strlen(ROOT_PATH);
len += strlen("%s/libexec/liblive-%s.so");
path = malloc(len + 1);
if (!path) {
ErrPrint("Heap: %s\n", strerror(errno));
return LB_STATUS_ERROR_MEMORY;
}
snprintf(path, len, "%s%s/libexec/liblive-%s.so", ROOT_PATH, pkgname, pkgname);
if (access(path, F_OK | R_OK) != 0) {
ErrPrint("%s is not a valid package\n", pkgname);
DbgFree(path);
return LB_STATUS_ERROR_INVALID;
}
DbgFree(path);
return LB_STATUS_SUCCESS;
}
static inline int check_web_livebox(const char *pkgname)
{
int len;
char *path;
len = strlen(pkgname) * 2;
len += strlen("/opt/usr/apps/%s/res/wgt/livebox/index.html");
path = malloc(len + 1);
if (!path) {
ErrPrint("Heap: %s\n", strerror(errno));
return LB_STATUS_ERROR_MEMORY;
}
snprintf(path, len, "/opt/usr/apps/%s/res/wgt/livebox/index.html", pkgname);
if (access(path, F_OK | R_OK) != 0) {
ErrPrint("%s is not a valid package\n", pkgname);
DbgFree(path);
return LB_STATUS_ERROR_INVALID;
}
DbgFree(path);
return LB_STATUS_SUCCESS;
}
HAPI int util_validate_livebox_package(const char *pkgname)
{
if (!pkgname) {
ErrPrint("Invalid argument\n");
return LB_STATUS_ERROR_INVALID;
}
if (!check_native_livebox(pkgname) || !check_web_livebox(pkgname))
return LB_STATUS_SUCCESS;
return LB_STATUS_ERROR_INVALID;
}
HAPI int util_unlink(const char *filename)
{
char *descfile;
int desclen;
int ret;
if (!filename)
return LB_STATUS_ERROR_INVALID;
desclen = strlen(filename) + 6; /* .desc */
descfile = malloc(desclen);
if (!descfile) {
ErrPrint("Heap: %s\n", strerror(errno));
return LB_STATUS_ERROR_MEMORY;
}
ret = snprintf(descfile, desclen, "%s.desc", filename);
if (ret < 0) {
ErrPrint("Error: %s\n", strerror(errno));
DbgFree(descfile);
return LB_STATUS_ERROR_FAULT;
}
(void)unlink(descfile);
DbgFree(descfile);
(void)unlink(filename);
return LB_STATUS_SUCCESS;
}
HAPI char *util_slavename(void)
{
char slavename[BUFSIZ];
static unsigned long idx = 0;
snprintf(slavename, sizeof(slavename), "%lu_%lf", idx++, util_timestamp());
return strdup(slavename);
}
HAPI const char *util_basename(const char *name)
{
int length;
length = name ? strlen(name) : 0;
if (!length)
return ".";
while (--length > 0 && name[length] != '/');
return length <= 0 ? name : (name + length + (name[length] == '/'));
}
HAPI unsigned long util_free_space(const char *path)
{
struct statvfs st;
unsigned long space;
if (statvfs(path, &st) < 0) {
ErrPrint("statvfs: %s\n", strerror(errno));
return 0lu;
}
space = st.f_bsize * st.f_bfree;
DbgPrint("Available size: %lu, f_bsize: %lu, f_bfree: %lu\n", space, st.f_bsize, st.f_bfree);
/*!
* \note
* Must have to check the overflow
*/
return space;
}
static inline char *extend_heap(char *buffer, int *sz, int incsz)
{
char *tmp;
*sz += incsz;
tmp = realloc(buffer, *sz);
if (!tmp) {
ErrPrint("Heap: %s\n", strerror(errno));
return NULL;
}
return tmp;
}
HAPI char *util_replace_string(const char *src, const char *pattern, const char *replace)
{
const char *ptr;
char *tmp;
char *ret = NULL;
int idx;
int out_idx;
int out_sz;
enum {
STATE_START,
STATE_FIND,
STATE_CHECK,
STATE_END,
} state;
if (!src || !pattern)
return NULL;
out_sz = strlen(src);
ret = strdup(src);
if (!ret) {
printf("Heap: %s\n", strerror(errno));
return NULL;
}
out_idx = 0;
for (state = STATE_START, ptr = src; state != STATE_END; ptr++) {
switch (state) {
case STATE_START:
if (*ptr == '\0') {
state = STATE_END;
} else if (!isblank(*ptr)) {
state = STATE_FIND;
ptr--;
}
break;
case STATE_FIND:
if (*ptr == '\0') {
state = STATE_END;
} else if (*ptr == *pattern) {
state = STATE_CHECK;
ptr--;
idx = 0;
} else {
ret[out_idx] = *ptr;
out_idx++;
if (out_idx == out_sz) {
tmp = extend_heap(ret, &out_sz, strlen(replace) + 1);
if (!tmp) {
free(ret);
return NULL;
}
ret = tmp;
}
}
break;
case STATE_CHECK:
if (!pattern[idx]) {
/*!
* If there is no space for copying the replacement,
* Extend size of the return buffer.
*/
if (out_sz - out_idx < strlen(replace) + 1) {
tmp = extend_heap(ret, &out_sz, strlen(replace) + 1);
if (!tmp) {
free(ret);
return NULL;
}
ret = tmp;
}
strcpy(ret + out_idx, replace);
out_idx += strlen(replace);
state = STATE_FIND;
ptr--;
} else if (*ptr != pattern[idx]) {
ptr -= idx;
/* Copy the first matched character */
ret[out_idx] = *ptr;
out_idx++;
if (out_idx == out_sz) {
tmp = extend_heap(ret, &out_sz, strlen(replace) + 1);
if (!tmp) {
free(ret);
return NULL;
}
ret = tmp;
}
state = STATE_FIND;
} else {
idx++;
}
break;
default:
break;
}
}
return ret;
}
HAPI const char *util_uri_to_path(const char *uri)
{
int len;
len = strlen(SCHEMA_FILE);
if (strncasecmp(uri, SCHEMA_FILE, len))
return NULL;
return uri + len;
}
HAPI double util_time_delay_for_compensation(double period)
{
struct timeval tv;
unsigned long long curtime;
unsigned long long _period;
unsigned long long remain;
unsigned int sec;
unsigned int usec;
double ret;
gettimeofday(&tv, NULL);
curtime = (unsigned long long)tv.tv_sec * 1000000llu + (unsigned long long)tv.tv_usec;
sec = (unsigned int)period;
usec = (period - sec) * 1000000;
_period = (unsigned long long)sec * 1000000llu + usec;
remain = curtime % _period;
sec = (unsigned int)(remain / 1000000llu);
usec = (unsigned int)(remain % 1000000llu);
ret = (double)sec + (double)usec / 1000000.0f;
return period - ret;
}
HAPI void *util_timer_add(double interval, Eina_Bool (*cb)(void *data), void *data)
{
Ecore_Timer *timer;
double delay;
timer = ecore_timer_add(interval, cb, data);
if (!timer)
return NULL;
delay = util_time_delay_for_compensation(interval) - interval;
ecore_timer_delay(timer, delay);
DbgPrint("Compensate timer: %lf\n", delay);
return timer;
}
HAPI void util_timer_interval_set(void *timer, double interval)
{
double delay;
ecore_timer_interval_set(timer, interval);
delay = util_time_delay_for_compensation(interval) - interval;
ecore_timer_delay(timer, delay);
}
HAPI char *util_get_file_kept_in_safe(const char *id)
{
const char *path;
char *new_path;
int len;
int base_idx;
path = util_uri_to_path(id);
if (!path) {
ErrPrint("Invalid URI(%s)\n", id);
return NULL;
}
/*!
* TODO: Remove me
*/
if (OVERWRITE_CONTENT)
return strdup(path);
len = strlen(path);
base_idx = len - 1;
while (base_idx > 0 && path[base_idx] != '/') base_idx--;
base_idx += (path[base_idx] == '/');
new_path = malloc(len + 10);
if (!new_path) {
ErrPrint("Heap: %s\n", strerror(errno));
return NULL;
}
strncpy(new_path, path, base_idx);
snprintf(new_path + base_idx, len + 10 - base_idx, "reader/%s", path + base_idx);
return new_path;
}
HAPI int util_unlink_files(const char *folder)
{
struct stat info;
DIR *handle;
struct dirent *entry;
char *abspath;
int len;
if (lstat(folder, &info) < 0) {
ErrPrint("Error: %s\n", strerror(errno));
return LB_STATUS_ERROR_IO;
}
if (!S_ISDIR(info.st_mode)) {
ErrPrint("Error: %s is not a folder", folder);
return LB_STATUS_ERROR_INVALID;
}
handle = opendir(folder);
if (!handle) {
ErrPrint("Error: %s\n", strerror(errno));
return LB_STATUS_ERROR_IO;
}
while ((entry = readdir(handle))) {
if (!strcmp(entry->d_name, "."))
continue;
if (!strcmp(entry->d_name, ".."))
continue;
len = strlen(folder) + strlen(entry->d_name) + 3;
abspath = calloc(1, len);
if (!abspath) {
ErrPrint("Heap: %s\n", strerror(errno));
continue;
}
snprintf(abspath, len - 1, "%s/%s", folder, entry->d_name);
if (unlink(abspath) < 0)
DbgPrint("unlink: %s - %s\n", abspath, strerror(errno));
free(abspath);
}
closedir(handle);
return LB_STATUS_SUCCESS;
}
/* End of a file */
|