a62a7d03bfb4b46c5c2a68111f1b4aa74a19932b
[platform/framework/web/livebox-viewer.git] / src / util.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
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
7  *
8  * http://www.tizenopensource.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
24 #include <dlog.h>
25
26 #include "debug.h"
27 #include "util.h"
28
29 int errno;
30
31 int util_check_extension(const char *filename, const char *check_ptr)
32 {
33         int name_len;
34
35         name_len = strlen(filename);
36         while (--name_len >= 0 && *check_ptr) {
37                 if (filename[name_len] != *check_ptr)
38                         return -EINVAL;
39
40                 check_ptr ++;
41         }
42
43         return 0;
44 }
45
46 double util_timestamp(void)
47 {
48         struct timeval tv;
49
50         gettimeofday(&tv, NULL);
51
52         return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
53 }
54
55 const char *util_basename(const char *name)
56 {
57         int length;
58         length = name ? strlen(name) : 0;
59         if (!length)
60                 return ".";
61
62         while (--length > 0 && name[length] != '/');
63
64         return length <= 0 ? name : name + length + (name[length] == '/');
65 }
66
67 static inline int check_native_livebox(const char *pkgname)
68 {
69         int len;
70         char *path;
71
72         len = strlen(pkgname) * 2;
73         len += strlen("/opt/usr/live/%s/libexec/liblive-%s.so");
74
75         path = malloc(len + 1);
76         if (!path) {
77                 ErrPrint("Heap: %s\n", strerror(errno));
78                 return -ENOMEM;
79         }
80
81         snprintf(path, len, "/opt/usr/live/%s/libexec/liblive-%s.so", pkgname, pkgname);
82         if (access(path, F_OK | R_OK) != 0) {
83                 ErrPrint("%s is not a valid package\n", pkgname);
84                 free(path);
85                 return -EINVAL;
86         }
87
88         free(path);
89         return 0;
90 }
91
92 static inline int check_web_livebox(const char *pkgname)
93 {
94         int len;
95         char *path;
96
97         len = strlen(pkgname) * 2;
98         len += strlen("/opt/usr/apps/%s/res/wgt/livebox/index.html");
99
100         path = malloc(len + 1);
101         if (!path) {
102                 ErrPrint("Heap: %s\n", strerror(errno));
103                 return -ENOMEM;
104         }
105
106         snprintf(path, len, "/opt/usr/apps/%s/res/wgt/livebox/index.html", pkgname);
107         if (access(path, F_OK | R_OK) != 0) {
108                 ErrPrint("%s is not a valid package\n", pkgname);
109                 free(path);
110                 return -EINVAL;
111         }
112
113         free(path);
114         return 0;
115 }
116
117 int util_validate_livebox_package(const char *pkgname)
118 {
119         if (!pkgname) {
120                 ErrPrint("Invalid argument\n");
121                 return -EINVAL;
122         }
123
124         if (!check_native_livebox(pkgname) || !check_web_livebox(pkgname))
125                 return 0;
126
127         return -EINVAL;
128 }
129
130 const char *util_uri_to_path(const char *uri)
131 {
132         int len;
133
134         len = strlen(SCHEMA_FILE);
135         if (strncasecmp(uri, SCHEMA_FILE, len))
136                 return NULL;
137
138         return uri + len;
139 }
140
141 /* End of a file */