Initialize Tizen 2.3
[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.8
19  * @author      Janusz Majnert <j.majnert@samsung.com>
20  * @brief       Implementation of some common utility functions
21  */
22 #include <stddef.h>
23 #include <fts.h>
24 #include <string>
25 #include <algorithm>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <unistd.h>
31 #include <dpl/log/wrt_log.h>
32 #include <dpl/utils/wrt_utility.h>
33
34 void WrtUtilJoinPaths(std::string &joined,
35                       const std::string &parent,
36                       const std::string &child)
37 {
38     size_t parent_len = parent.length();
39     joined = parent;
40     joined += child;
41     //In case someone used windows-style paths
42     std::replace(joined.begin(), joined.end(), '\\', '/');
43
44     if (parent_len != 0 && child.length() != 0) {
45         if (joined[parent_len - 1] != '/' && joined[parent_len] != '/') {
46             joined.insert(parent_len, "/");
47         } else if (joined[parent_len - 1] == '/' && joined[parent_len] ==
48                    '/')
49         {
50             joined.erase(parent_len, 1);
51         }
52     }
53 }
54
55 bool WrtUtilMakeDir(const std::string &newpath, mode_t mode)
56 {
57     size_t pos = 0;
58     int error;
59     char *errstr = NULL;
60     char errbuf[512] = {0,};
61
62     if (newpath.length() == 0) {
63         return false;
64     }
65
66     std::string path = newpath;
67
68     if (*(path.rbegin()) != '/') {
69         path += '/';
70     }
71
72     while ((pos = path.find('/', pos + 1)) != std::string::npos) {
73         if (mkdir(path.substr(0, pos).c_str(), mode) != 0) {
74             error = errno;
75             if (error == EEXIST) {
76                 continue;
77             }
78 #ifdef _GNU_SOURCE
79             errstr = strerror_r(error, errbuf, sizeof(errbuf));
80 #else
81             strerror_r(error, errbuf, sizeof(errbuf));
82             errstr = errbuf;
83 #endif
84             WrtLogW("%s: failed to create directory %s. Error: %s",
85                     __PRETTY_FUNCTION__, path.substr(0, pos).c_str(),errstr);
86             return false;
87         }
88     }
89     return true;
90 }
91
92 bool WrtUtilRemove(const std::string &path)
93 {
94     char *errstr = NULL;
95     char errbuf[512] = {0,};
96     FTS *fts;
97     FTSENT *ftsent;
98     bool rv = true;
99     int error = 0;
100     char * const paths[] = { const_cast<char * const>(path.c_str()), NULL };
101
102     if ((fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR, NULL)) == NULL) {
103         //ERROR
104         error = errno;
105 #ifdef _GNU_SOURCE
106         errstr = strerror_r(error, errbuf, 512);
107 #else
108         strerror_r(error, errbuf, 512);
109         errstr = errbuf;
110 #endif
111         WrtLogW("%s : fts_open failed with error: %s",
112                 __PRETTY_FUNCTION__, errstr);
113         return false;
114     }
115
116     while ((ftsent = fts_read(fts)) != NULL) {
117         switch (ftsent->fts_info) {
118         case FTS_D:
119             //directory in preorder - do nothing
120             break;
121         case FTS_DP:
122             //directory in postorder - remove
123             if (rmdir(ftsent->fts_accpath) != 0) {
124                 error = errno;
125 #ifdef _GNU_SOURCE
126                 errstr = strerror_r(error, errbuf, 512);
127 #else
128                 strerror_r(error, errbuf, 512);
129                 errstr = errbuf;
130 #endif
131                 WrtLogW("%s: rmdir failed with error: %s",
132                         __PRETTY_FUNCTION__, errstr);
133                 rv = false;
134             }
135             break;
136         case FTS_DC:
137         case FTS_F:
138         case FTS_NSOK:
139         case FTS_SL:
140         case FTS_SLNONE:
141         case FTS_DEFAULT:
142             //regular files and other objects that can safely be removed
143             if (unlink(ftsent->fts_accpath) != 0) {
144                 error = errno;
145 #ifdef _GNU_SOURCE
146                 errstr = strerror_r(error, errbuf, 512);
147 #else
148                 strerror_r(error, errbuf, 512);
149                 errstr = errbuf;
150 #endif
151                 WrtLogW("%s: unlink failed with error: %s",
152                         __PRETTY_FUNCTION__, errstr);
153                 rv = false;
154             }
155             break;
156         case FTS_NS:
157 #ifdef _GNU_SOURCE
158             errstr = strerror_r(ftsent->fts_errno, errbuf, 512);
159 #else
160             strerror_r(ftsent->fts_errno, errbuf, 512);
161             errstr = errbuf;
162 #endif
163             WrtLogW("%s: couldn't get stat info for file: %s. The error was: %s",
164                     __PRETTY_FUNCTION__, ftsent->fts_path, errstr);
165             rv = false;
166             break;
167         case FTS_DOT:
168         case FTS_DNR:
169         case FTS_ERR:
170         default:
171 #ifdef _GNU_SOURCE
172             errstr = strerror_r(ftsent->fts_errno, errbuf, 512);
173 #else
174             strerror_r(ftsent->fts_errno, errbuf, 512);
175             errstr = errbuf;
176 #endif
177             WrtLogW("%s: traversal failed with error: %s",
178                     __PRETTY_FUNCTION__, errstr);
179             rv = false;
180             break;
181         }
182     }
183
184     if (fts_close(fts) == -1) {
185         error = errno;
186 #ifdef _GNU_SOURCE
187         errstr = strerror_r(error, errbuf, 512);
188 #else
189         strerror_r(error, errbuf, 512);
190         errstr = errbuf;
191 #endif
192         WrtLogW("%s: fts_close failed with error: %s",
193                 __PRETTY_FUNCTION__, errstr);
194         rv = false;
195     }
196     return rv;
197 }
198
199 bool WrtUtilFileExists(const std::string &path)
200 {
201     struct stat tmp;
202     if (stat(path.c_str(), &tmp) == 0) {
203         return S_ISREG(tmp.st_mode);
204     } else {
205         return false;
206     }
207 }
208
209 bool WrtUtilDirExists(const std::string &path)
210 {
211     struct stat tmp;
212     if (stat(path.c_str(), &tmp) == 0) {
213         return S_ISDIR(tmp.st_mode);
214     } else {
215         return false;
216     }
217 }
218