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