Spring cleaning
[platform/core/test/security-tests.git] / src / cynara-tests / common / cynara_test_file_operations.cpp
index 1f7a5da..cf5b4c1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2020 Samsung Electronics Co., Ltd. All rights reserved
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
  *    you may not use this file except in compliance with the License.
@@ -35,7 +35,9 @@
 namespace FileOperations
 {
 
-static int removeFile(const char *fpath, const struct stat * /*sb*/,
+namespace {
+
+int removeFile(const char *fpath, const struct stat * /*sb*/,
                           int tflag, struct FTW * /*ftwbuf*/)
 {
     if (tflag == FTW_F)
@@ -45,18 +47,18 @@ static int removeFile(const char *fpath, const struct stat * /*sb*/,
     return 0;
 }
 
-bool dirExists(const std::string &directory)
+void syncElem(const std::string &filename, int flags, mode_t mode)
 {
-    struct stat st;
-    int ret = stat(directory.c_str(), &st);
-    if (ret == -1 && errno == ENOENT) {
-        return false;
-    } else if (ret == -1) {
-        RUNNER_ASSERT_ERRNO_MSG(false, "Cannot stat " << directory
-                                          << " not due to its nonexistence");
-    }
-    RUNNER_ASSERT_MSG(st.st_mode & S_IFDIR, directory << " is not a directory");
-    return true;
+    int fileFd = TEMP_FAILURE_RETRY(open(filename.c_str(), flags, mode));
+    RUNNER_ASSERT_ERRNO_MSG(fileFd != -1, "open failed name=" << filename);
+    FdUniquePtr fdPtr(&fileFd);
+
+    int ret = fsync(fileFd);
+    RUNNER_ASSERT_ERRNO_MSG(ret != -1, "fsync failed name=" << filename);
+}
+
+void syncDir(const std::string &dirname, mode_t mode = S_IRUSR | S_IWUSR) {
+    syncElem(dirname, O_DIRECTORY, mode);
 }
 
 void copyCynaraFile(const std::string &src, const std::string &dst)
@@ -100,6 +102,22 @@ void copyCynaraFile(const std::string &src, const std::string &dst)
     RUNNER_ASSERT_ERRNO_MSG(ret != -1, "fsync failed");
 }
 
+} // namespace
+
+bool dirExists(const std::string &directory)
+{
+    struct stat st;
+    int ret = stat(directory.c_str(), &st);
+    if (ret == -1 && errno == ENOENT) {
+        return false;
+    } else if (ret == -1) {
+        RUNNER_ASSERT_ERRNO_MSG(false, "Cannot stat " << directory
+                                          << " not due to its nonexistence");
+    }
+    RUNNER_ASSERT_MSG(st.st_mode & S_IFDIR, directory << " is not a directory");
+    return true;
+}
+
 void copyCynaraFiles(const std::string &source, const std::string &destination)
 {
     DIR *dirPtr = nullptr;
@@ -121,20 +139,6 @@ void copyCynaraFiles(const std::string &source, const std::string &destination)
     syncDir(destination);
 }
 
-void syncElem(const std::string &filename, int flags, mode_t mode)
-{
-    int fileFd = TEMP_FAILURE_RETRY(open(filename.c_str(), flags, mode));
-    RUNNER_ASSERT_ERRNO_MSG(fileFd != -1, "open failed name=" << filename);
-    FdUniquePtr fdPtr(&fileFd);
-
-    int ret = fsync(fileFd);
-    RUNNER_ASSERT_ERRNO_MSG(ret != -1, "fsync failed name=" << filename);
-}
-
-void syncDir(const std::string &dirname, mode_t mode) {
-    syncElem(dirname, O_DIRECTORY, mode);
-}
-
 void makeDir(const std::string &directory)
 {
     RUNNER_ASSERT_ERRNO_MSG(!mkdir(directory.c_str(), S_IRWXU | S_IRWXG | S_IRWXO),