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
26 #include <sys/types.h>
29 #include <dpl/log/log.h>
30 #include <dpl/utils/wrt_utility.h>
32 void WrtUtilJoinPaths(std::string &joined, const std::string &parent, const std::string &child)
34 size_t parent_len = parent.length();;
37 //In case someone used windows-style paths
38 std::replace(joined.begin(), joined.end(), '\\', '/');
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);
48 bool WrtUtilMakeDir(const std::string &newpath, mode_t mode)
53 if (newpath.length() == 0) return false;
55 std::string path=newpath;
57 if (*(path.rbegin()) != '/') path += '/';
59 while ((pos = path.find('/', pos+1)) != std::string::npos) {
60 if (mkdir(path.substr(0, pos).c_str(), mode) != 0) {
62 if (error == EEXIST) continue;
63 LogWarning(__PRETTY_FUNCTION__ << ": failed to create directory "
73 bool WrtUtilRemove(const std::string &path)
79 char * const paths[] = {const_cast<char * const>(path.c_str()), NULL};
81 if ((fts = fts_open(paths, FTS_PHYSICAL|FTS_NOCHDIR, NULL)) == NULL) {
84 LogWarning(__PRETTY_FUNCTION__ << ": fts_open failed with error: "
89 while ((ftsent = fts_read(fts)) != NULL) {
90 switch (ftsent->fts_info) {
92 //directory in preorder - do nothing
95 //directory in postorder - remove
96 if (rmdir(ftsent->fts_accpath) != 0) {
98 LogWarning(__PRETTY_FUNCTION__
99 << ": rmdir failed with error: "
110 //regular files and other objects that can safely be removed
111 if (unlink(ftsent->fts_accpath) != 0) {
113 LogWarning(__PRETTY_FUNCTION__
114 << ": unlink failed with error: "
120 LogWarning(__PRETTY_FUNCTION__
121 << ": couldn't get stat info for file: "
123 << ". The error was: "
124 << strerror(ftsent->fts_errno));
131 LogWarning(__PRETTY_FUNCTION__
132 << ": traversal failed with error: "
133 << strerror(ftsent->fts_errno));
139 if (fts_close(fts) == -1) {
141 LogWarning(__PRETTY_FUNCTION__ << ": fts_close failed with error: "
148 void WrtUtilStringToLower(std::string &out, const std::string &in)
151 for (std::string::const_iterator it=in.begin(); it<in.end(); ++it) {
152 out += static_cast<char>(tolower(*it));
156 bool WrtUtilFileExists(const std::string &path)
159 if (stat(path.c_str(),&tmp) == 0) {
160 return S_ISREG(tmp.st_mode);
166 bool WrtUtilDirExists(const std::string &path)
169 if (stat(path.c_str(),&tmp) == 0) {
170 return S_ISDIR(tmp.st_mode);