Tizen 2.0 Release
[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 <sys/stat.h>
26 #include <sys/types.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <dpl/log/log.h>
30 #include <dpl/utils/wrt_utility.h>
31
32 void WrtUtilJoinPaths(std::string &joined, const std::string &parent, const std::string &child)
33 {
34     size_t parent_len = parent.length();;
35     joined=parent;
36     joined+=child;
37     //In case someone used windows-style paths
38     std::replace(joined.begin(), joined.end(), '\\', '/');
39
40     if (parent_len != 0 && child.length() != 0) {
41         if (joined[parent_len-1] != '/' && joined[parent_len] != '/')
42             joined.insert(parent_len, "/");
43         else if (joined[parent_len-1] == '/' && joined[parent_len] == '/')
44             joined.erase(parent_len, 1);
45     }
46 }
47
48 bool WrtUtilMakeDir(const std::string &newpath, mode_t mode)
49 {
50     size_t pos = 0;
51     int error;
52
53     if (newpath.length() == 0) return false;
54
55     std::string path=newpath;
56
57     if (*(path.rbegin()) != '/') path += '/';
58
59     while ((pos = path.find('/', pos+1)) != std::string::npos) {
60         if (mkdir(path.substr(0, pos).c_str(), mode) != 0) {
61             error=errno;
62             if (error == EEXIST) continue;
63             LogWarning(__PRETTY_FUNCTION__ << ": failed to create directory "
64                         << path.substr(0,pos)
65                         << ". Error: "
66                         << strerror(error));
67             return false;
68         }
69     }
70     return true;
71 }
72
73 bool WrtUtilRemove(const std::string &path)
74 {
75     FTS *fts;
76     FTSENT *ftsent;
77     bool rv = true;
78     int error = 0;
79     char * const paths[] = {const_cast<char * const>(path.c_str()), NULL};
80
81     if ((fts = fts_open(paths, FTS_PHYSICAL|FTS_NOCHDIR, NULL)) == NULL) {
82         //ERROR
83         error = errno;
84         LogWarning(__PRETTY_FUNCTION__ << ": fts_open failed with error: "
85                     << strerror(error));
86         return false;
87     }
88
89     while ((ftsent = fts_read(fts)) != NULL) {
90         switch (ftsent->fts_info) {
91             case FTS_D:
92                 //directory in preorder - do nothing
93                 break;
94             case FTS_DP:
95                 //directory in postorder - remove
96                 if (rmdir(ftsent->fts_accpath) != 0) {
97                     error = errno;
98                     LogWarning(__PRETTY_FUNCTION__
99                                 << ": rmdir failed with error: "
100                                 << strerror(error));
101                     rv = false;
102                 }
103                 break;
104             case FTS_DC:
105             case FTS_F:
106             case FTS_NSOK:
107             case FTS_SL:
108             case FTS_SLNONE:
109             case FTS_DEFAULT:
110                 //regular files and other objects that can safely be removed
111                 if (unlink(ftsent->fts_accpath) != 0) {
112                     error = errno;
113                     LogWarning(__PRETTY_FUNCTION__
114                                 << ": unlink failed with error: "
115                                 << strerror(error));
116                     rv = false;
117                 }
118                 break;
119             case FTS_NS:
120                 LogWarning(__PRETTY_FUNCTION__
121                             << ": couldn't get stat info for file: "
122                             << ftsent->fts_path
123                             << ". The error was: "
124                             << strerror(ftsent->fts_errno));
125                 rv = false;
126                 break;
127             case FTS_DOT:
128             case FTS_DNR:
129             case FTS_ERR:
130             default:
131                 LogWarning(__PRETTY_FUNCTION__
132                             << ": traversal failed with error: "
133                             << strerror(ftsent->fts_errno));
134                 rv = false;
135                 break;
136         }
137     }
138
139     if (fts_close(fts) == -1) {
140         error = errno;
141         LogWarning(__PRETTY_FUNCTION__ << ": fts_close failed with error: "
142                     << strerror(error));
143         rv = false;
144     }
145     return rv;
146 }
147
148 bool WrtUtilFileExists(const std::string &path)
149 {
150     struct stat tmp;
151     if (stat(path.c_str(),&tmp) == 0) {
152         return S_ISREG(tmp.st_mode);
153     } else {
154         return false;
155     }
156 }
157
158 bool WrtUtilDirExists(const std::string &path)
159 {
160     struct stat tmp;
161     if (stat(path.c_str(),&tmp) == 0) {
162         return S_ISDIR(tmp.st_mode);
163     } else {
164         return false;
165     }
166 }
167