2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file wrt_utility.cpp
19 * @author Janusz Majnert <j.majnert@samsung.com>
20 * @brief Implementation of some common utility functions
27 #include <sys/types.h>
31 #include <dpl/log/wrt_log.h>
32 #include <dpl/utils/wrt_utility.h>
34 void WrtUtilJoinPaths(std::string &joined,
35 const std::string &parent,
36 const std::string &child)
38 size_t parent_len = parent.length();
41 //In case someone used windows-style paths
42 std::replace(joined.begin(), joined.end(), '\\', '/');
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] ==
50 joined.erase(parent_len, 1);
55 bool WrtUtilMakeDir(const std::string &newpath, mode_t mode)
60 char errbuf[512] = {0,};
62 if (newpath.length() == 0) {
66 std::string path = newpath;
68 if (*(path.rbegin()) != '/') {
72 while ((pos = path.find('/', pos + 1)) != std::string::npos) {
73 if (mkdir(path.substr(0, pos).c_str(), mode) != 0) {
75 if (error == EEXIST) {
79 errstr = strerror_r(error, errbuf, sizeof(errbuf));
81 strerror_r(error, errbuf, sizeof(errbuf));
84 WrtLogW("%s: failed to create directory %s. Error: %s",
85 __PRETTY_FUNCTION__, path.substr(0, pos).c_str(),errstr);
92 bool WrtUtilRemove(const std::string &path)
95 char errbuf[512] = {0,};
100 char * const paths[] = { const_cast<char * const>(path.c_str()), NULL };
102 if ((fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR, NULL)) == NULL) {
106 errstr = strerror_r(error, errbuf, 512);
108 strerror_r(error, errbuf, 512);
111 WrtLogW("%s : fts_open failed with error: %s",
112 __PRETTY_FUNCTION__, errstr);
116 while ((ftsent = fts_read(fts)) != NULL) {
117 switch (ftsent->fts_info) {
119 //directory in preorder - do nothing
122 //directory in postorder - remove
123 if (rmdir(ftsent->fts_accpath) != 0) {
126 errstr = strerror_r(error, errbuf, 512);
128 strerror_r(error, errbuf, 512);
131 WrtLogW("%s: rmdir failed with error: %s",
132 __PRETTY_FUNCTION__, errstr);
142 //regular files and other objects that can safely be removed
143 if (unlink(ftsent->fts_accpath) != 0) {
146 errstr = strerror_r(error, errbuf, 512);
148 strerror_r(error, errbuf, 512);
151 WrtLogW("%s: unlink failed with error: %s",
152 __PRETTY_FUNCTION__, errstr);
158 errstr = strerror_r(ftsent->fts_errno, errbuf, 512);
160 strerror_r(ftsent->fts_errno, errbuf, 512);
163 WrtLogW("%s: couldn't get stat info for file: %s. The error was: %s",
164 __PRETTY_FUNCTION__, ftsent->fts_path, errstr);
172 errstr = strerror_r(ftsent->fts_errno, errbuf, 512);
174 strerror_r(ftsent->fts_errno, errbuf, 512);
177 WrtLogW("%s: traversal failed with error: %s",
178 __PRETTY_FUNCTION__, errstr);
184 if (fts_close(fts) == -1) {
187 errstr = strerror_r(error, errbuf, 512);
189 strerror_r(error, errbuf, 512);
192 WrtLogW("%s: fts_close failed with error: %s",
193 __PRETTY_FUNCTION__, errstr);
199 bool WrtUtilFileExists(const std::string &path)
202 if (stat(path.c_str(), &tmp) == 0) {
203 return S_ISREG(tmp.st_mode);
209 bool WrtUtilDirExists(const std::string &path)
212 if (stat(path.c_str(), &tmp) == 0) {
213 return S_ISDIR(tmp.st_mode);