* limitations under the License.
*/
/**
- * @file wrt_utility.cpp
- * @version 0.6
- * @author Wei Dong(d.wei@samsung.com)
- * @author Ma Quan(jason.ma@samsung.com)
- * @brief This file implemented some common functions for widget manager
+ * @file wrt_utility.cpp
+ * @version 0.8
+ * @author Janusz Majnert <j.majnert@samsung.com>
+ * @brief Implementation of some common utility functions
*/
-
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-#include <dirent.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <stdarg.h>
-#include <errno.h>
-#include <libgen.h>
-#include <sys/types.h>
+#include <stddef.h>
+#include <fts.h>
+#include <string>
#include <sys/stat.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <ctype.h>
#include <dpl/log/log.h>
-#include <wrt_utility.h>
+#include <dpl/utils/wrt_utility.h>
-using namespace std;
-
-bool _WrtUtilSetAbsolutePath(char* absolutePath,
- const char* parentPath,
- const char* fileName)
+void WrtUtilJoinPaths(std::string &joined, const std::string &parent, const std::string &child)
{
- int len;
- if (NULL == absolutePath || NULL == parentPath || NULL == fileName) {
- return false;
+ size_t parent_len = parent.length();;
+ joined=parent;
+ joined+=child;
+ //In case someone used windows-style paths
+ std::replace(joined.begin(), joined.end(), '\\', '/');
+
+ if (parent_len != 0 && child.length() != 0) {
+ if (joined[parent_len-1] != '/' && joined[parent_len] != '/')
+ joined.insert(parent_len, "/");
+ else if (joined[parent_len-1] == '/' && joined[parent_len] == '/')
+ joined.erase(parent_len, 1);
}
- len = strlen(parentPath);
- if (len > 0) {
- // not check the valid of parentPath and fileName.
- if (parentPath[len - 1] == '/') {
- snprintf(absolutePath,
- MAX_WIDGET_PATH_LENGTH,
- "%s%s",
- parentPath,
- fileName);
- } else {
- snprintf(absolutePath,
- MAX_WIDGET_PATH_LENGTH,
- "%s/%s",
- parentPath,
- fileName);
- }
- } else {
- LogDebug("The parent path is null");
- return false;
- }
-
- //some widget use Windows notation. We need to change '\' to '/'
- for (int i = 0; absolutePath[i] != 0; ++i) {
- if (absolutePath[i] == '\\') {
- absolutePath[i] = '/';
- }
- }
-
- return true;
}
-// KW bool _WrtUtilConvertStrToBool(char* value, bool *result)
-// KW {
-// KW bool ret = false;
-// KW if (NULL == value || NULL == result)
-// KW {
-// KW return ret;
-// KW }
-// KW
-// KW char* source = value;
-// KW char* changed = (char*)malloc(strlen(value) + 1);
-// KW if (NULL == changed)
-// KW {
-// KW return ret;
-// KW }
-// KW memset(changed, 0, strlen(value) + 1);
-// KW
-// KW char* cur = changed;
-// KW while(*source)
-// KW {
-// KW *cur++ = tolower(*source++);
-// KW }
-// KW if (!strcmp(changed,"false") || !strcmp(changed,"0"))
-// KW {
-// KW *result = false;
-// KW ret = true;
-// KW }
-// KW else if(!strcmp(changed,"true") || !strcmp(changed,"1"))
-// KW {
-// KW *result = true;
-// KW ret = true;
-// KW }
-// KW free(changed);
-// KW
-// KW return ret;
-// KW }
-
-void _WrtUtilGetDirAndFileName(const char* fullPath,
- char** dirName,
- char** fileName)
+bool WrtUtilMakeDir(const std::string &newpath, mode_t mode)
{
- int length = 0;
- int index = 0;
- if (NULL == fullPath || (NULL == dirName && NULL == fileName)) {
- return;
- }
-
- length = strlen(fullPath);
- for (index = length - 1; index >= 0; index--) {
- if ('/' == fullPath[index]) {
- if (index == length - 1) {
- LogDebug(" Warning: The end of directroy is '/'! ");
- if (dirName) {
- *dirName = (char*)malloc(sizeof(char) * (length + 1));
- if (*dirName != NULL) {
- memset(*dirName, 0, sizeof(char) * (length + 1));
- strncpy(*dirName, fullPath, length);
- }
- }
- return;
- }
- break;
- }
- }
- if (index >= 0) {
- if (dirName) {
- int dirName_len = index + 2;
-
- *dirName = (char*)malloc(sizeof(char) * dirName_len);
- if (*dirName != NULL) {
- memset(*dirName, 0, sizeof(char) * dirName_len);
- strncpy(*dirName, fullPath, dirName_len - 1);
- }
- }
-
- if (fileName) {
- int fileName_len = length - index;
+ size_t pos = 0;
+ int error;
- *fileName = (char*)malloc(sizeof(char) * fileName_len);
- if (*fileName != NULL) {
- memset(*fileName, 0, sizeof(char) * fileName_len);
- strncpy(*fileName, &fullPath[index + 1], fileName_len - 1);
- }
- }
- } else {
- if (fileName) {
- *fileName = (char*)malloc(sizeof(char) * (length + 1));
- if (*fileName != NULL) {
- memset(*fileName, 0, sizeof(char) * (length + 1));
- strncpy(*fileName, fullPath, length);
- }
- }
- }
-}
+ if (newpath.length() == 0) return false;
-// KW bool _WrtUtilStringCmp(const char* srcStr, const char* destStr)
-// KW {
-// KW bool ret = false;
-// KW char* strString = NULL;
-// KW char* destString = NULL;
-// KW
-// KW if (NULL == srcStr || NULL == destStr )
-// KW {
-// KW return ret;
-// KW }
-// KW
-// KW _WrtUtilStringToLower(srcStr, &strString);
-// KW _WrtUtilStringToLower(destStr,&destString);
-// KW
-// KW if(!strcmp(strString, destString))
-// KW {
-// KW ret = true;
-// KW }
-// KW
-// KW free(strString);
-// KW free(destString);
-// KW
-// KW return ret;
-// KW }
+ std::string path=newpath;
-// check it deeply later.
-bool _WrtMakeDir (const char *path,
- long mode,
- int flags)
-{
- if (NULL == path) {
- return false;
- }
+ if (*(path.rbegin()) != '/') path += '/';
- const int defaultMode = 0777;
- if (!(flags & WRT_FILEUTILS_RECUR)) {
- if (mkdir(path, defaultMode) < 0) {
- LogDebug("Failed to make dir " << path);
+ while ((pos = path.find('/', pos+1)) != std::string::npos) {
+ if (mkdir(path.substr(0, pos).c_str(), mode) != 0) {
+ error=errno;
+ if (error == EEXIST) continue;
+ LogWarning(__PRETTY_FUNCTION__ << ": failed to create directory "
+ << path.substr(0,pos)
+ << ". Error: "
+ << strerror(error));
return false;
}
- if (mode != -1 && chmod(path, mode) < 0) {
- LogDebug("Failed to chmod");
- remove(path);
- return false;
- }
- } else {
- struct stat st;
- if (stat(path, &st) < 0 && errno == ENOENT) {
- bool ret;
- char *buf = NULL;
- char *parent = NULL;
- // mode_t mask;
-
- // mask = umask (0);
- // umask (mask);
-
- buf = strdup(path);
- parent = dirname(buf);
- //ret = _WrtMakeDir(parent, (defaultMode & ~mask) | 0300, WRT_FILEUTILS_RECUR);
- ret = _WrtMakeDir(parent, (defaultMode) | 0300, WRT_FILEUTILS_RECUR);
- free(buf);
-
- if ((!ret) || (!_WrtMakeDir(path, mode, 0))) {
- LogDebug("Failed to _WrtMakeDir");
- return false;
- }
- }
}
-
return true;
}
-bool _WrtUtilChangeDir(const char* path)
+bool WrtUtilRemove(const std::string &path)
{
- if (NULL == path) {
+ FTS *fts;
+ FTSENT *ftsent;
+ bool rv = true;
+ int error = 0;
+ char * const paths[] = {const_cast<char * const>(path.c_str()), NULL};
+
+ if ((fts = fts_open(paths, FTS_PHYSICAL|FTS_NOCHDIR, NULL)) == NULL) {
+ //ERROR
+ error = errno;
+ LogWarning(__PRETTY_FUNCTION__ << ": fts_open failed with error: "
+ << strerror(error));
return false;
}
- if (-1 == chdir(path)) {
- if (ENOENT == errno) {
- if (!_WrtMakeDir(path, 0664, WRT_FILEUTILS_RECUR)) {
- return false;
- }
- if (-1 == chdir(path)) {
- remove(path);
- return false;
- }
- } else {
- return false;
+
+ while ((ftsent = fts_read(fts)) != NULL) {
+ switch (ftsent->fts_info) {
+ case FTS_D:
+ //directory in preorder - do nothing
+ break;
+ case FTS_DP:
+ //directory in postorder - remove
+ if (rmdir(ftsent->fts_accpath) != 0) {
+ error = errno;
+ LogWarning(__PRETTY_FUNCTION__
+ << ": rmdir failed with error: "
+ << strerror(error));
+ rv = false;
+ }
+ break;
+ case FTS_DC:
+ case FTS_F:
+ case FTS_NSOK:
+ case FTS_SL:
+ case FTS_SLNONE:
+ case FTS_DEFAULT:
+ //regular files and other objects that can safely be removed
+ if (unlink(ftsent->fts_accpath) != 0) {
+ error = errno;
+ LogWarning(__PRETTY_FUNCTION__
+ << ": unlink failed with error: "
+ << strerror(error));
+ rv = false;
+ }
+ break;
+ case FTS_NS:
+ LogWarning(__PRETTY_FUNCTION__
+ << ": couldn't get stat info for file: "
+ << ftsent->fts_path
+ << ". The error was: "
+ << strerror(ftsent->fts_errno));
+ rv = false;
+ break;
+ case FTS_DOT:
+ case FTS_DNR:
+ case FTS_ERR:
+ default:
+ LogWarning(__PRETTY_FUNCTION__
+ << ": traversal failed with error: "
+ << strerror(ftsent->fts_errno));
+ rv = false;
+ break;
}
}
- return true;
+ if (fts_close(fts) == -1) {
+ error = errno;
+ LogWarning(__PRETTY_FUNCTION__ << ": fts_close failed with error: "
+ << strerror(error));
+ rv = false;
+ }
+ return rv;
}
-bool _WrtUtilRemoveDir(const char* path)
+bool WrtUtilFileExists(const std::string &path)
{
- DIR* dir = NULL;
- struct dirent* ptr = NULL;
- char childPath[MAX_WIDGET_PATH_LENGTH + 1] = { 0 };
- if (path == NULL) {
- LogWarning("Path is null");
- return false;
- }
- dir = opendir(path);
- if (NULL != dir) {
- while ((ptr = readdir(dir)) != NULL) {
- if ((!strcmp(ptr->d_name, ".")) || (!strcmp(ptr->d_name, ".."))) {
- continue;
- }
- int len = strlen(path);
- if (path[len - 1] == '/') {
- snprintf(childPath,
- MAX_WIDGET_PATH_LENGTH,
- "%s%s",
- path,
- ptr->d_name);
- } else {
- snprintf(childPath,
- MAX_WIDGET_PATH_LENGTH,
- "%s/%s",
- path,
- ptr->d_name);
- }
- if (ptr->d_type == DT_DIR) {
- if (!_WrtUtilRemoveDir(childPath)) {
- closedir(dir);
- return false;
- }
- } else {
- if (unlink(childPath) != 0) {
- closedir(dir);
- LogWarning("Failed to remove file " << childPath);
- return false;
- }
- }
- }
- closedir(dir);
- } else if (errno == ENOTDIR) {
- if (unlink(path) != 0) {
- LogWarning("Failed to remove file " << path);
- return false;
- }
- return true;
- } else if (errno == ENOENT) { //not exist
- LogWarning("Cannot remove non existent directory " << path);
- return true;
+ struct stat tmp;
+ if (stat(path.c_str(),&tmp) == 0) {
+ return S_ISREG(tmp.st_mode);
} else {
- LogWarning("Can't remove directory " << path);
return false;
}
- if (rmdir(path) != 0) {
- LogWarning("Removing directory failed :" << path << " errno: " << errno);
- return false;
- }
-
- return true;
}
-bool
-_WrtUtilStringToLower(const char* str,
- char** lowerStr)
+bool WrtUtilDirExists(const std::string &path)
{
- if (!str || !lowerStr) {
- return true;
- }
-
- char* cur = NULL;
- int length = strlen(str);
-
- *lowerStr = (char*)malloc(length + 1);
-
- if (!(*lowerStr)) {
+ struct stat tmp;
+ if (stat(path.c_str(),&tmp) == 0) {
+ return S_ISDIR(tmp.st_mode);
+ } else {
return false;
}
-
- memset(*lowerStr, 0, length + 1);
- strncpy(*lowerStr, str, length);
-
- cur = *lowerStr;
-
- while (*str != '\0') {
- *cur++ = tolower(*str++);
- //cur++;
- }
-
- return true;
}