Prevent desynchronization in "battery remove test".
authorBartlomiej Grzelewski <b.grzelewski@samsung.com>
Thu, 11 Dec 2014 14:38:08 +0000 (15:38 +0100)
committerMaciej J. Karpiuk <m.karpiuk2@samsung.com>
Tue, 17 Feb 2015 10:20:23 +0000 (11:20 +0100)
This commit force to save encrypted data as soon as possible.
There is still small time window then desynhronization may happen.
Desynchronization may occure if you take out battery exectly after the
data was saved by ckm and the confirmation was not send to
security-server yet.

Change-Id: Ib4d4f0299001d9c71b13acdcfa136298d942ab6c

src/manager/dpl/core/include/dpl/fstream_accessors.h
src/manager/service/file-system.cpp

index 1f6b7a3..b1aa17c 100644 (file)
  *  See the License for the specific language governing permissions and
  *  limitations under the License
  *
- * @file        fstream-helper.h
+ * @file        fstream-accessors.h
  * @author      Marek Smolinski (m.smolinski@samsung.com)
  * @version     1.0
- * @brief       This file is the implementation file of fstream-helper
+ * @brief       This file is the implementation file of fstream-accessors
  *
  */
 
 #ifndef CENT_KEY_FSTREAM_ACCESSORS_H
 #define CENT_KEY_FSTREAM_ACCESSORS_H
 
-namespace DPL {
+namespace CKM {
 
 /*
  * Bypass lack of public member function to get file
@@ -43,6 +43,6 @@ public:
     }
 };
 
-} // namespace DPL
+} // namespace CKM
 
 #endif // CENT_KEY_FSTREAM_ACCESSORS_H
index 0c59753..cefea59 100644 (file)
@@ -33,6 +33,7 @@
 #include <stdexcept>
 
 #include <dpl/errno_string.h>
+#include <dpl/fstream_accessors.h>
 #include <dpl/log/log.h>
 
 #include <file-system.h>
@@ -104,8 +105,15 @@ RawBuffer FileSystem::getDBDEK() const
 }
 
 bool FileSystem::saveFile(const std::string &path, const RawBuffer &buffer) const {
-    std::ofstream os(path, std::ios::out | std::ofstream::binary);
+    std::ofstream os(path, std::ios::out | std::ofstream::binary | std::ofstream::trunc);
     std::copy(buffer.begin(), buffer.end(), std::ostreambuf_iterator<char>(os));
+    if (os.fail())
+        return false;
+
+    // Prevent desynchronization in batter remove test.
+    os.flush();
+    fsync(FstreamAccessors<std::ofstream>::GetFd(os)); // flush kernel space buffer
+    os.close();
     return !os.fail();
 }