f7ecd7d71e9e076be460816e6986a66c4010762e
[framework/web/wrt-commons.git] / modules / utils / src / wrt_utility.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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  * @file       wrt_utility.cpp
18  * @version    0.6
19  * @author Wei Dong(d.wei@samsung.com)
20  * @author Ma Quan(jason.ma@samsung.com)
21  * @brief  This file implemented some common functions for widget manager
22  */
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <stdarg.h>
33 #include <errno.h>
34 #include <libgen.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <dpl/log/log.h>
38 #include <wrt_utility.h>
39
40 using namespace std;
41
42 bool _WrtUtilSetAbsolutePath(char* absolutePath,
43         const char* parentPath,
44         const char* fileName)
45 {
46     int len;
47     if (NULL == absolutePath || NULL == parentPath || NULL == fileName) {
48         return false;
49     }
50     len = strlen(parentPath);
51     if (len > 0) {
52         // not check the valid of parentPath and fileName.
53         if (parentPath[len - 1] == '/') {
54             snprintf(absolutePath,
55                      MAX_WIDGET_PATH_LENGTH,
56                      "%s%s",
57                      parentPath,
58                      fileName);
59         } else {
60             snprintf(absolutePath,
61                      MAX_WIDGET_PATH_LENGTH,
62                      "%s/%s",
63                      parentPath,
64                      fileName);
65         }
66     } else {
67         LogDebug("The parent path is null");
68         return false;
69     }
70
71     //some widget use Windows notation. We need to change '\' to '/'
72     for (int i = 0; absolutePath[i] != 0; ++i) {
73         if (absolutePath[i] == '\\') {
74             absolutePath[i] = '/';
75         }
76     }
77
78     return true;
79 }
80
81 // KW bool _WrtUtilConvertStrToBool(char* value, bool *result)
82 // KW {
83 // KW     bool ret = false;
84 // KW     if (NULL == value || NULL == result)
85 // KW     {
86 // KW         return ret;
87 // KW     }
88 // KW
89 // KW     char* source = value;
90 // KW     char* changed = (char*)malloc(strlen(value) + 1);
91 // KW     if (NULL == changed)
92 // KW     {
93 // KW         return ret;
94 // KW     }
95 // KW     memset(changed, 0, strlen(value) + 1);
96 // KW
97 // KW     char* cur = changed;
98 // KW     while(*source)
99 // KW     {
100 // KW         *cur++ = tolower(*source++);
101 // KW     }
102 // KW     if (!strcmp(changed,"false") || !strcmp(changed,"0"))
103 // KW     {
104 // KW         *result = false;
105 // KW         ret = true;
106 // KW     }
107 // KW     else if(!strcmp(changed,"true") || !strcmp(changed,"1"))
108 // KW     {
109 // KW         *result = true;
110 // KW         ret = true;
111 // KW     }
112 // KW     free(changed);
113 // KW
114 // KW     return ret;
115 // KW }
116
117 void _WrtUtilGetDirAndFileName(const char* fullPath,
118         char** dirName,
119         char** fileName)
120 {
121     int length = 0;
122     int index = 0;
123     if (NULL == fullPath || (NULL == dirName && NULL == fileName)) {
124         return;
125     }
126
127     length = strlen(fullPath);
128     for (index = length - 1; index >= 0; index--) {
129         if ('/' == fullPath[index]) {
130             if (index == length - 1) {
131                 LogDebug(" Warning: The end of directroy is '/'! ");
132                 if (dirName) {
133                     *dirName = (char*)malloc(sizeof(char) * (length + 1));
134                     if (*dirName != NULL) {
135                         memset(*dirName, 0, sizeof(char) * (length + 1));
136                         strncpy(*dirName, fullPath, length);
137                     }
138                 }
139                 return;
140             }
141             break;
142         }
143     }
144     if (index >= 0) {
145         if (dirName) {
146             int dirName_len = index + 2;
147
148             *dirName = (char*)malloc(sizeof(char) * dirName_len);
149             if (*dirName != NULL) {
150                 memset(*dirName, 0, sizeof(char) * dirName_len);
151                 strncpy(*dirName, fullPath, dirName_len - 1);
152             }
153         }
154
155         if (fileName) {
156             int fileName_len = length - index;
157
158             *fileName = (char*)malloc(sizeof(char) * fileName_len);
159             if (*fileName != NULL) {
160                 memset(*fileName, 0, sizeof(char) * fileName_len);
161                 strncpy(*fileName, &fullPath[index + 1], fileName_len - 1);
162             }
163         }
164     } else {
165         if (fileName) {
166             *fileName = (char*)malloc(sizeof(char) * (length + 1));
167             if (*fileName != NULL) {
168                 memset(*fileName, 0, sizeof(char) * (length + 1));
169                 strncpy(*fileName, fullPath, length);
170             }
171         }
172     }
173 }
174
175 // KW bool _WrtUtilStringCmp(const char* srcStr, const char* destStr)
176 // KW {
177 // KW     bool ret = false;
178 // KW     char* strString = NULL;
179 // KW     char* destString = NULL;
180 // KW
181 // KW     if (NULL == srcStr || NULL == destStr )
182 // KW     {
183 // KW         return ret;
184 // KW     }
185 // KW
186 // KW     _WrtUtilStringToLower(srcStr, &strString);
187 // KW     _WrtUtilStringToLower(destStr,&destString);
188 // KW
189 // KW     if(!strcmp(strString, destString))
190 // KW     {
191 // KW         ret = true;
192 // KW     }
193 // KW
194 // KW     free(strString);
195 // KW     free(destString);
196 // KW
197 // KW     return ret;
198 // KW }
199
200 // check it deeply later.
201 bool _WrtMakeDir (const char *path,
202         long mode,
203         int flags)
204 {
205     if (NULL == path) {
206         return false;
207     }
208
209     const int defaultMode = 0777;
210     if (!(flags & WRT_FILEUTILS_RECUR)) {
211         if (mkdir(path, defaultMode) < 0) {
212             LogDebug("Failed to make dir " << path);
213             return false;
214         }
215         if (mode != -1 && chmod(path, mode) < 0) {
216             LogDebug("Failed to chmod");
217             remove(path);
218             return false;
219         }
220     } else {
221         struct stat st;
222         if (stat(path, &st) < 0 && errno == ENOENT) {
223             bool ret;
224             char *buf = NULL;
225             char *parent = NULL;
226             //            mode_t mask;
227
228             //            mask = umask (0);
229             //            umask (mask);
230
231             buf = strdup(path);
232             parent = dirname(buf);
233             //ret = _WrtMakeDir(parent, (defaultMode & ~mask) | 0300, WRT_FILEUTILS_RECUR);
234             ret = _WrtMakeDir(parent, (defaultMode) | 0300, WRT_FILEUTILS_RECUR);
235             free(buf);
236
237             if ((!ret) || (!_WrtMakeDir(path, mode, 0))) {
238                 LogDebug("Failed to _WrtMakeDir");
239                 return false;
240             }
241         }
242     }
243
244     return true;
245 }
246
247 bool _WrtUtilChangeDir(const char* path)
248 {
249     if (NULL == path) {
250         return false;
251     }
252     if (-1 == chdir(path)) {
253         if (ENOENT == errno) {
254             if (!_WrtMakeDir(path, 0664, WRT_FILEUTILS_RECUR)) {
255                 return false;
256             }
257             if (-1 == chdir(path)) {
258                 remove(path);
259                 return false;
260             }
261         } else {
262             return false;
263         }
264     }
265
266     return true;
267 }
268
269 bool _WrtUtilRemoveDir(const char* path)
270 {
271     DIR* dir = NULL;
272     struct dirent* ptr = NULL;
273     char childPath[MAX_WIDGET_PATH_LENGTH + 1] = { 0 };
274     if (path == NULL) {
275         LogWarning("Path is null");
276         return false;
277     }
278     dir = opendir(path);
279     if (NULL != dir) {
280         while ((ptr = readdir(dir)) != NULL) {
281             if ((!strcmp(ptr->d_name, ".")) || (!strcmp(ptr->d_name, ".."))) {
282                 continue;
283             }
284             int len = strlen(path);
285             if (path[len - 1] == '/') {
286                 snprintf(childPath,
287                          MAX_WIDGET_PATH_LENGTH,
288                          "%s%s",
289                          path,
290                          ptr->d_name);
291             } else {
292                 snprintf(childPath,
293                          MAX_WIDGET_PATH_LENGTH,
294                          "%s/%s",
295                          path,
296                          ptr->d_name);
297             }
298             if (ptr->d_type == DT_DIR) {
299                 if (!_WrtUtilRemoveDir(childPath)) {
300                     closedir(dir);
301                     return false;
302                 }
303             } else {
304                 if (unlink(childPath) != 0) {
305                     closedir(dir);
306                     LogWarning("Failed to remove file " << childPath);
307                     return false;
308                 }
309             }
310         }
311         closedir(dir);
312     } else if (errno == ENOTDIR) {
313         if (unlink(path) != 0) {
314             LogWarning("Failed to remove file " << path);
315             return false;
316         }
317         return true;
318     } else if (errno == ENOENT) { //not exist
319         LogWarning("Cannot remove non existent directory " << path);
320         return true;
321     } else {
322         LogWarning("Can't remove directory " << path);
323         return false;
324     }
325     if (rmdir(path) != 0) {
326         LogWarning("Removing directory failed :" << path << " errno: " << errno);
327         return false;
328     }
329
330     return true;
331 }
332
333 bool
334 _WrtUtilStringToLower(const char* str,
335         char** lowerStr)
336 {
337     if (!str || !lowerStr) {
338         return true;
339     }
340
341     char* cur = NULL;
342     int length = strlen(str);
343
344     *lowerStr = (char*)malloc(length + 1);
345
346     if (!(*lowerStr)) {
347         return false;
348     }
349
350     memset(*lowerStr, 0, length + 1);
351     strncpy(*lowerStr, str, length);
352
353     cur = *lowerStr;
354
355     while (*str != '\0') {
356         *cur++ = tolower(*str++);
357         //cur++;
358     }
359
360     return true;
361 }
362