Update License
[apps/livebox/livebox-service.git] / src / util.c
1 /*
2  * Copyright 2013  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://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 <sys/time.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <sys/statvfs.h>
24
25 #include <dlog.h>
26
27 #include "util.h"
28 #include "debug.h"
29 #include "livebox-errno.h"
30
31 int errno;
32
33 static inline char *check_native_livebox(const char *pkgname)
34 {
35         int len;
36         char *path;
37
38         len = strlen(pkgname) * 2;
39         len += strlen("/opt/usr/live/%s/libexec/liblive-%s.so");
40
41         path = malloc(len + 1);
42         if (!path) {
43                 ErrPrint("Heap: %s\n", strerror(errno));
44                 return NULL;
45         }
46
47         snprintf(path, len, "/opt/usr/live/%s/libexec/liblive-%s.so", pkgname, pkgname);
48         if (access(path, F_OK | R_OK) != 0) {
49                 ErrPrint("%s is not a valid package (%s)\n", pkgname, strerror(errno));
50                 free(path);
51                 return NULL;
52         }
53
54         return path;
55 }
56
57 static inline char *check_web_livebox(const char *pkgname)
58 {
59         int len;
60         char *path;
61
62         len = strlen(pkgname) * 2;
63         len += strlen("/opt/usr/apps/%s/res/wgt/livebox/index.html");
64
65         path = malloc(len + 1);
66         if (!path) {
67                 ErrPrint("Heap: %s\n", strerror(errno));
68                 return NULL;
69         }
70
71         snprintf(path, len, "/opt/usr/apps/%s/res/wgt/livebox/index.html", pkgname);
72         if (access(path, F_OK | R_OK) != 0) {
73                 ErrPrint("%s is not a valid package (%s)\n", pkgname, strerror(errno));
74                 free(path);
75                 return NULL;
76         }
77
78         return path;
79 }
80
81 int util_validate_livebox_package(const char *pkgname)
82 {
83         char *path;
84
85         if (!pkgname) {
86                 ErrPrint("Invalid argument\n");
87                 return LB_STATUS_ERROR_INVALID;
88         }
89
90         path = check_native_livebox(pkgname);
91         if (path) {
92                 free(path);
93                 return 0;
94         }
95
96         path = check_web_livebox(pkgname);
97         if (path) {
98                 free(path);
99                 return 0;
100         }
101
102         return LB_STATUS_ERROR_INVALID;
103 }
104
105 double util_timestamp(void)
106 {
107         struct timeval tv;
108
109         gettimeofday(&tv, NULL);
110
111         return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
112 }
113
114 const char *util_basename(const char *name)
115 {
116         int length;
117         length = name ? strlen(name) : 0;
118         if (!length)
119                 return ".";
120
121         while (--length > 0 && name[length] != '/');
122
123         return length <= 0 ? name : (name + length + (name[length] == '/'));
124 }
125
126 unsigned long util_free_space(const char *path)
127 {
128         struct statvfs st;
129         unsigned long space;
130
131         if (statvfs(path, &st) < 0) {
132                 ErrPrint("statvfs: %s\n", strerror(errno));
133                 return 0lu;
134         }
135
136         space = st.f_bsize * st.f_bfree;
137         /*!
138          * \note
139          * Must have to check the overflow
140          */
141         return space;
142 }
143
144 char *util_replace_string(const char *src, const char *pattern, const char *replace)
145 {
146         int s_idx;
147         int p_idx;
148         int n_idx;
149         int t_idx;
150         int r_idx;
151         int idx;
152         char *result;
153         int len;
154         int rlen;
155         int matched;
156
157         if (!src || !pattern || !replace || !src[0] || !pattern[0]) {
158                 ErrPrint("Invalid argument: %s %s %s\n", src, pattern, replace);
159                 return NULL;
160         }
161
162         rlen = strlen(replace);
163         len = strlen(src);
164         result = malloc(len + 1);
165         if (!result) {
166                 ErrPrint("Heap:%s\n", strerror(errno));
167                 return NULL;
168         }
169
170         r_idx = 0;
171         idx = 0;
172         matched = 0;
173         for (s_idx = 0; src[s_idx]; s_idx++) {
174                 if (idx == len) {
175                         char *tmp;
176
177                         len += (rlen > len ? rlen : len);
178                         tmp = realloc(result, len + 1);
179                         if (!tmp) {
180                                 ErrPrint("Heap: %s\n", strerror(errno));
181                                 free(result);
182                                 return NULL;
183                         }
184                         result = tmp;
185                 }
186
187                 if (src[s_idx] == pattern[0]) {
188                         n_idx = -1;
189                         t_idx = s_idx;
190                         r_idx = idx;
191
192                         if (r_idx == len) {
193                                 char *tmp;
194                                 len += (rlen > len ? rlen : len);
195                                 tmp = realloc(result, len + 1);
196                                 if (!tmp) {
197                                         ErrPrint("Heap: %s\n", strerror(errno));
198                                         free(result);
199                                         return NULL;
200                                 }
201                                 result = tmp;
202                         }
203                         result[r_idx++] = src[t_idx++];
204                         p_idx = 1;
205                         while (pattern[p_idx]) {
206                                 if (src[t_idx] == pattern[p_idx]) {
207                                         if (n_idx < 0) {
208                                                 if (src[t_idx] == pattern[0]) {
209                                                         n_idx = t_idx;
210                                                 } else {
211                                                         if (r_idx == len) {
212                                                                 char *tmp;
213                                                                 len += (rlen > len ? rlen : len);
214                                                                 tmp = realloc(result, len + 1);
215                                                                 if (!tmp) {
216                                                                         ErrPrint("Heap: %s\n", strerror(errno));
217                                                                         free(result);
218                                                                         return NULL;
219                                                                 }
220                                                                 result = tmp;
221                                                         }
222                                                         result[r_idx++] = src[t_idx];
223                                                 }
224                                         }
225
226                                         p_idx++;
227                                         t_idx++;
228                                         continue;
229                                 }
230
231                                 if (n_idx < 0)
232                                         s_idx = t_idx;
233                                 else
234                                         s_idx = n_idx;
235
236                                 break;
237                         }
238
239                         if (pattern[p_idx] == '\0') {
240                                 if (idx + rlen >= len) {
241                                         char *tmp;
242                                         len += (rlen > len ? rlen : len);
243                                         tmp = realloc(result, len + 1);
244                                         if (!tmp) {
245                                                 ErrPrint("Heap: %s\n", strerror(errno));
246                                                 free(result);
247                                                 return NULL;
248                                         }
249                                         result = tmp;
250                                         matched++;
251                                 }
252                                 strcpy(result + idx, replace);
253                                 idx += strlen(replace);
254                                 s_idx = t_idx - 1;
255                         } else {
256                                 idx = r_idx;
257                                 s_idx = (n_idx < 0 ? t_idx : n_idx) - 1;
258                         }
259                 } else {
260                         result[idx++] = src[s_idx];
261                 }
262         }
263
264         result[idx] = '\0';
265
266         if (!matched) {
267                 free(result);
268                 result = NULL;
269         }
270
271         return result;
272 }
273
274 const char *util_uri_to_path(const char *uri)
275 {
276         int len;
277
278         len = strlen(SCHEMA_FILE);
279         if (strncasecmp(uri, SCHEMA_FILE, len))
280                 return NULL;
281
282         return uri + len;
283 }
284
285 char *util_conf_get_libexec(const char *pkgname)
286 {
287         char *path;
288
289         if (!pkgname) {
290                 ErrPrint("Invalid argument\n");
291                 return NULL;
292         }
293
294         path = check_native_livebox(pkgname);
295         if (!path)
296                 path = check_web_livebox(pkgname);
297
298         return path;
299 }
300
301 char *util_id_to_uri(const char *id)
302 {
303         char *uri;
304
305         if (!id) {
306                 uri = strdup("");
307                 if (!uri) {
308                         ErrPrint("Heap: %s\n", strerror(errno));
309                         return NULL;
310                 }
311         } else if (strncmp(id, SCHEMA_FILE, strlen(SCHEMA_FILE))) {
312                 int len;
313                 len = strlen(SCHEMA_FILE) + strlen(id) + 2;
314                 uri = malloc(len);
315                 if (!uri) {
316                         ErrPrint("Heap: %s\n", strerror(errno));
317                         return NULL;
318                 }
319
320                 snprintf(uri, len, SCHEMA_FILE"%s", id);
321         } else {
322                 uri = strdup(id);
323                 if (!uri) {
324                         ErrPrint("Heap: %s\n", strerror(errno));
325                         return NULL;
326                 }
327         }
328
329         return uri;
330 }
331
332 /* End of a file */