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