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>
30 #include <dpl/log/log.h>
31 #include <dpl/utils/wrt_utility.h>
33 void WrtUtilJoinPaths(std::string &joined,
34 const std::string &parent,
35 const std::string &child)
37 size_t parent_len = parent.length();
40 //In case someone used windows-style paths
41 std::replace(joined.begin(), joined.end(), '\\', '/');
43 if (parent_len != 0 && child.length() != 0) {
44 if (joined[parent_len - 1] != '/' && joined[parent_len] != '/') {
45 joined.insert(parent_len, "/");
46 } else if (joined[parent_len - 1] == '/' && joined[parent_len] ==
49 joined.erase(parent_len, 1);
54 bool WrtUtilMakeDir(const std::string &newpath, mode_t mode)
59 if (newpath.length() == 0) {
63 std::string path = newpath;
65 if (*(path.rbegin()) != '/') {
69 while ((pos = path.find('/', pos + 1)) != std::string::npos) {
70 if (mkdir(path.substr(0, pos).c_str(), mode) != 0) {
72 if (error == EEXIST) {
75 LogWarning(__PRETTY_FUNCTION__ << ": failed to create directory "
76 << path.substr(0, pos)
85 bool WrtUtilRemove(const std::string &path)
91 char * const paths[] = { const_cast<char * const>(path.c_str()), NULL };
93 if ((fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR, NULL)) == NULL) {
96 LogWarning(__PRETTY_FUNCTION__ << ": fts_open failed with error: "
101 while ((ftsent = fts_read(fts)) != NULL) {
102 switch (ftsent->fts_info) {
104 //directory in preorder - do nothing
107 //directory in postorder - remove
108 if (rmdir(ftsent->fts_accpath) != 0) {
110 LogWarning(__PRETTY_FUNCTION__
111 << ": rmdir failed with error: "
122 //regular files and other objects that can safely be removed
123 if (unlink(ftsent->fts_accpath) != 0) {
125 LogWarning(__PRETTY_FUNCTION__
126 << ": unlink failed with error: "
132 LogWarning(__PRETTY_FUNCTION__
133 << ": couldn't get stat info for file: "
135 << ". The error was: "
136 << strerror(ftsent->fts_errno));
143 LogWarning(__PRETTY_FUNCTION__
144 << ": traversal failed with error: "
145 << strerror(ftsent->fts_errno));
151 if (fts_close(fts) == -1) {
153 LogWarning(__PRETTY_FUNCTION__ << ": fts_close failed with error: "
160 bool WrtUtilFileExists(const std::string &path)
163 if (stat(path.c_str(), &tmp) == 0) {
164 return S_ISREG(tmp.st_mode);
170 bool WrtUtilDirExists(const std::string &path)
173 if (stat(path.c_str(), &tmp) == 0) {
174 return S_ISDIR(tmp.st_mode);