Add synchronization after file operations 71/36971/2
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Tue, 17 Mar 2015 14:27:16 +0000 (15:27 +0100)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Tue, 17 Mar 2015 14:44:09 +0000 (15:44 +0100)
After creating, copying and removing files program should
wait on disc synchronization, otherwise test can fail, e.g.
when we swap database files and start cynara, which loads
old not-yet-synchronized version of db.

Change-Id: Ie5c26da859e4e2a88311bb68ed7a0138b98e87d7

src/cynara-tests/common/cynara_test_file_operations.cpp
src/cynara-tests/common/cynara_test_file_operations.h

index 0c27a26..1f7a5da 100644 (file)
@@ -95,6 +95,9 @@ void copyCynaraFile(const std::string &src, const std::string &dst)
 
     ret = sendfile(outFd, inFd, 0, statSrc.st_size);
     RUNNER_ASSERT_ERRNO_MSG(ret != -1, "sendfile failed");
+
+    ret = fsync(outFd);
+    RUNNER_ASSERT_ERRNO_MSG(ret != -1, "fsync failed");
 }
 
 void copyCynaraFiles(const std::string &source, const std::string &destination)
@@ -114,12 +117,30 @@ void copyCynaraFiles(const std::string &source, const std::string &destination)
         std::string tempSrc = source + "/" + direntPtr->d_name;
         copyCynaraFile(tempSrc, tempDest);
     }
+
+    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),
                                "Unable to make " << directory << " test directory");
+
+    syncDir(directory);
 }
 
 void removeDirFiles(const std::string &dir)
@@ -127,6 +148,8 @@ void removeDirFiles(const std::string &dir)
     int ret = nftw(dir.c_str(), removeFile, 2, FTW_DEPTH | FTW_PHYS);
     if (ret == -1)
         RUNNER_ASSERT_ERRNO_MSG(errno == ENOENT, "nftw failed");
+    else
+        syncDir(dir);
 }
 
 void removeDirIfExists(const std::string &dir)
index fae2e09..d3acdb8 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef CYNARA_TEST_FILE_OPERATIONS_H
 #define CYNARA_TEST_FILE_OPERATIONS_H
 
+#include <fcntl.h>
 #include <string>
 
 namespace FileOperations
@@ -25,6 +26,8 @@ namespace FileOperations
 bool dirExists(const std::string &directory);
 void copyCynaraFile(const std::string &src, const std::string &dst);
 void copyCynaraFiles(const std::string &source, const std::string &destination);
+void syncElem(const std::string &filename, int flags = O_RDONLY, mode_t mode = S_IRUSR | S_IWUSR);
+void syncDir(const std::string &dirname, mode_t mode = S_IRUSR | S_IWUSR);
 void makeDir(const std::string &directory);
 void removeDirFiles(const std::string &dir);
 void removeDirIfExists(const std::string &dir);