Remove hardcoded path for multiuser support
[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 #include <time.h>
24
25 #include <dlog.h>
26 #include <livebox-errno.h> /* For error code */
27 #include <tzplatform_config.h>
28
29 #include "debug.h"
30 #include "util.h"
31
32 int errno;
33 #if defined(_USE_ECORE_TIME_GET)
34 static struct {
35         clockid_t type;
36 } s_info = {
37         .type = CLOCK_MONOTONIC,
38 };
39 #endif
40
41 int util_check_extension(const char *filename, const char *check_ptr)
42 {
43         int name_len;
44
45         name_len = strlen(filename);
46         while (--name_len >= 0 && *check_ptr) {
47                 if (filename[name_len] != *check_ptr) {
48                         return LB_STATUS_ERROR_INVALID;
49                 }
50
51                 check_ptr ++;
52         }
53
54         return 0;
55 }
56
57 double util_timestamp(void)
58 {
59 #if defined(_USE_ECORE_TIME_GET)
60         struct timespec ts;
61
62         do {
63                 if (clock_gettime(s_info.type, &ts) == 0) {
64                         return ts.tv_sec + ts.tv_nsec / 1000000000.0f;
65                 }
66
67                 ErrPrint("%d: %s\n", s_info.type, strerror(errno));
68                 if (s_info.type == CLOCK_MONOTONIC) {
69                         s_info.type = CLOCK_REALTIME;
70                 } else if (s_info.type == CLOCK_REALTIME) {
71                         struct timeval tv;
72                         if (gettimeofday(&tv, NULL) < 0) {
73                                 ErrPrint("gettimeofday: %s\n", strerror(errno));
74                                 break;
75                         }
76
77                         return tv.tv_sec + tv.tv_usec / 1000000.0f;
78                 }
79         } while (1);
80
81         return 0.0f;
82 #else
83         struct timeval tv;
84
85         if (gettimeofday(&tv, NULL) < 0) {
86                 ErrPrint("gettimeofday: %s\n", strerror(errno));
87                 tv.tv_sec = 0;
88                 tv.tv_usec = 0;
89         }
90
91         return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
92 #endif
93 }
94
95 const char *util_basename(const char *name)
96 {
97         int length;
98         length = name ? strlen(name) : 0;
99         if (!length) {
100                 return ".";
101         }
102
103         while (--length > 0 && name[length] != '/');
104
105         return length <= 0 ? name : name + length + (name[length] == '/');
106 }
107
108 static inline int check_native_livebox(const char *pkgname)
109 {
110         int len;
111         char *path;
112
113         /* get lenght of path */
114         len = snprintf(NULL, 0, "%s/%s/libexec/liblive-%s.so",tzplatform_getenv(TZ_USER_LIVE),pkgname, pkgname);
115
116         path = malloc(len + 1);
117         if (!path) {
118                 ErrPrint("Heap: %s\n", strerror(errno));
119                 return LB_STATUS_ERROR_MEMORY;
120         }
121
122         snprintf(path, len, "%s/%s/libexec/liblive-%s.so",tzplatform_getenv(TZ_USER_LIVE),pkgname, pkgname);
123         if (access(path, F_OK | R_OK) != 0) {
124                 ErrPrint("%s is not a valid package\n", pkgname);
125                 free(path);
126                 return LB_STATUS_ERROR_INVALID;
127         }
128
129         free(path);
130         return 0;
131 }
132
133 static inline int check_web_livebox(const char *pkgname)
134 {
135         int len;
136         char *path;
137
138         /* get lenght of path*/
139         len = snprintf(NULL, 0, "%s/%s/res/wgt/livebox/index.html",tzplatform_getenv(TZ_USER_APP),pkgname);
140
141         path = malloc(len + 1);
142         if (!path) {
143                 ErrPrint("Heap: %s\n", strerror(errno));
144                 return LB_STATUS_ERROR_MEMORY;
145         }
146
147         snprintf(path, len, "%s/%s/res/wgt/livebox/index.html",tzplatform_getenv(TZ_USER_APP) ,pkgname);
148         if (access(path, F_OK | R_OK) != 0) {
149                 ErrPrint("%s is not a valid package\n", pkgname);
150                 free(path);
151                 return LB_STATUS_ERROR_INVALID;
152         }
153
154         free(path);
155         return 0;
156 }
157
158 int util_validate_livebox_package(const char *pkgname)
159 {
160         if (!pkgname) {
161                 ErrPrint("Invalid argument\n");
162                 return LB_STATUS_ERROR_INVALID;
163         }
164
165         if (!check_native_livebox(pkgname) || !check_web_livebox(pkgname)) {
166                 return 0;
167         }
168
169         return LB_STATUS_ERROR_INVALID;
170 }
171
172 const char *util_uri_to_path(const char *uri)
173 {
174         int len;
175
176         len = strlen(SCHEMA_FILE);
177         if (strncasecmp(uri, SCHEMA_FILE, len)) {
178                 return NULL;
179         }
180
181         return uri + len;
182 }
183
184 int util_unlink(const char *filename)
185 {
186         char *descfile;
187         int desclen;
188         int ret;
189
190         if (!filename) {
191                 return LB_STATUS_ERROR_INVALID;
192         }
193
194         desclen = strlen(filename) + 6; /* .desc */
195         descfile = malloc(desclen);
196         if (!descfile) {
197                 ErrPrint("Heap: %s\n", strerror(errno));
198                 return LB_STATUS_ERROR_MEMORY;
199         }
200
201         ret = snprintf(descfile, desclen, "%s.desc", filename);
202         if (ret < 0) {
203                 ErrPrint("Error: %s\n", strerror(errno));
204                 free(descfile);
205                 return LB_STATUS_ERROR_FAULT;
206         }
207
208         (void)unlink(descfile);
209         free(descfile);
210         (void)unlink(filename);
211
212         return LB_STATUS_SUCCESS;
213 }
214
215 /* End of a file */