cdafb4f65908f053d2fe2dc2ada2da3b017e7e69
[platform/framework/web/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
24 #include <dlog.h>
25 #include <livebox-errno.h> /* For error code */
26
27 #include "debug.h"
28 #include "util.h"
29
30 int errno;
31
32 int util_check_extension(const char *filename, const char *check_ptr)
33 {
34         int name_len;
35
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;
40                 }
41
42                 check_ptr ++;
43         }
44
45         return 0;
46 }
47
48 double util_timestamp(void)
49 {
50         struct timeval tv;
51
52         gettimeofday(&tv, NULL);
53
54         return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
55 }
56
57 const char *util_basename(const char *name)
58 {
59         int length;
60         length = name ? strlen(name) : 0;
61         if (!length) {
62                 return ".";
63         }
64
65         while (--length > 0 && name[length] != '/');
66
67         return length <= 0 ? name : name + length + (name[length] == '/');
68 }
69
70 static inline int check_native_livebox(const char *pkgname)
71 {
72         int len;
73         char *path;
74
75         len = strlen(pkgname) * 2;
76         len += strlen("/opt/usr/live/%s/libexec/liblive-%s.so");
77
78         path = malloc(len + 1);
79         if (!path) {
80                 ErrPrint("Heap: %s\n", strerror(errno));
81                 return LB_STATUS_ERROR_MEMORY;
82         }
83
84         snprintf(path, len, "/opt/usr/live/%s/libexec/liblive-%s.so", pkgname, pkgname);
85         if (access(path, F_OK | R_OK) != 0) {
86                 ErrPrint("%s is not a valid package\n", pkgname);
87                 free(path);
88                 return LB_STATUS_ERROR_INVALID;
89         }
90
91         free(path);
92         return 0;
93 }
94
95 static inline int check_web_livebox(const char *pkgname)
96 {
97         int len;
98         char *path;
99
100         len = strlen(pkgname) * 2;
101         len += strlen("/opt/usr/apps/%s/res/wgt/livebox/index.html");
102
103         path = malloc(len + 1);
104         if (!path) {
105                 ErrPrint("Heap: %s\n", strerror(errno));
106                 return LB_STATUS_ERROR_MEMORY;
107         }
108
109         snprintf(path, len, "/opt/usr/apps/%s/res/wgt/livebox/index.html", pkgname);
110         if (access(path, F_OK | R_OK) != 0) {
111                 ErrPrint("%s is not a valid package\n", pkgname);
112                 free(path);
113                 return LB_STATUS_ERROR_INVALID;
114         }
115
116         free(path);
117         return 0;
118 }
119
120 int util_validate_livebox_package(const char *pkgname)
121 {
122         if (!pkgname) {
123                 ErrPrint("Invalid argument\n");
124                 return LB_STATUS_ERROR_INVALID;
125         }
126
127         if (!check_native_livebox(pkgname) || !check_web_livebox(pkgname)) {
128                 return 0;
129         }
130
131         return LB_STATUS_ERROR_INVALID;
132 }
133
134 const char *util_uri_to_path(const char *uri)
135 {
136         int len;
137
138         len = strlen(SCHEMA_FILE);
139         if (strncasecmp(uri, SCHEMA_FILE, len)) {
140                 return NULL;
141         }
142
143         return uri + len;
144 }
145
146 int util_unlink(const char *filename)
147 {
148         char *descfile;
149         int desclen;
150         int ret;
151
152         if (!filename) {
153                 return LB_STATUS_ERROR_INVALID;
154         }
155
156         desclen = strlen(filename) + 6; /* .desc */
157         descfile = malloc(desclen);
158         if (!descfile) {
159                 ErrPrint("Heap: %s\n", strerror(errno));
160                 return LB_STATUS_ERROR_MEMORY;
161         }
162
163         ret = snprintf(descfile, desclen, "%s.desc", filename);
164         if (ret < 0) {
165                 ErrPrint("Error: %s\n", strerror(errno));
166                 free(descfile);
167                 return LB_STATUS_ERROR_FAULT;
168         }
169
170         (void)unlink(descfile);
171         free(descfile);
172         (void)unlink(filename);
173
174         return LB_STATUS_SUCCESS;
175 }
176
177 /* End of a file */