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, const std::string &parent, const std::string &child)
35 size_t parent_len = parent.length();;
38 //In case someone used windows-style paths
39 std::replace(joined.begin(), joined.end(), '\\', '/');
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);
49 bool WrtUtilMakeDir(const std::string &newpath, mode_t mode)
54 if (newpath.length() == 0) return false;
56 std::string path=newpath;
58 if (*(path.rbegin()) != '/') path += '/';
60 while ((pos = path.find('/', pos+1)) != std::string::npos) {
61 if (mkdir(path.substr(0, pos).c_str(), mode) != 0) {
63 if (error == EEXIST) continue;
64 LogWarning(__PRETTY_FUNCTION__ << ": failed to create directory "
74 bool WrtUtilRemove(const std::string &path)
80 char * const paths[] = {const_cast<char * const>(path.c_str()), NULL};
82 if ((fts = fts_open(paths, FTS_PHYSICAL|FTS_NOCHDIR, NULL)) == NULL) {
85 LogWarning(__PRETTY_FUNCTION__ << ": fts_open failed with error: "
90 while ((ftsent = fts_read(fts)) != NULL) {
91 switch (ftsent->fts_info) {
93 //directory in preorder - do nothing
96 //directory in postorder - remove
97 if (rmdir(ftsent->fts_accpath) != 0) {
99 LogWarning(__PRETTY_FUNCTION__
100 << ": rmdir failed with error: "
111 //regular files and other objects that can safely be removed
112 if (unlink(ftsent->fts_accpath) != 0) {
114 LogWarning(__PRETTY_FUNCTION__
115 << ": unlink failed with error: "
121 LogWarning(__PRETTY_FUNCTION__
122 << ": couldn't get stat info for file: "
124 << ". The error was: "
125 << strerror(ftsent->fts_errno));
132 LogWarning(__PRETTY_FUNCTION__
133 << ": traversal failed with error: "
134 << strerror(ftsent->fts_errno));
140 if (fts_close(fts) == -1) {
142 LogWarning(__PRETTY_FUNCTION__ << ": fts_close failed with error: "
149 bool WrtUtilFileExists(const std::string &path)
152 if (stat(path.c_str(),&tmp) == 0) {
153 return S_ISREG(tmp.st_mode);
159 bool WrtUtilDirExists(const std::string &path)
162 if (stat(path.c_str(),&tmp) == 0) {
163 return S_ISDIR(tmp.st_mode);