Runtime: fsync() files when transfering them, to prebent data loss when poweroff soon after programming. Fixed variable naming as a side effect.
authorEdouard Tisserant
Mon, 01 Jun 2020 14:47:31 +0200
changeset 2651 231f8bd2cd7d
parent 2646 d692e01d94f4
child 2652 269149e78302
Runtime: fsync() files when transfering them, to prebent data loss when poweroff soon after programming. Fixed variable naming as a side effect.
runtime/PLCObject.py
--- a/runtime/PLCObject.py	Thu May 28 11:38:09 2020 +0200
+++ b/runtime/PLCObject.py	Mon Jun 01 14:47:31 2020 +0200
@@ -520,7 +520,7 @@
     @RunInMain
     def SeedBlob(self, seed):
         blob = (mkstemp(dir=self.tmpdir) + (hashlib.new('md5'),))
-        _fobj, _path, md5sum = blob
+        _fd, _path, md5sum = blob
         md5sum.update(seed)
         newBlobID = md5sum.digest()
         self.blobs[newBlobID] = blob
@@ -533,17 +533,17 @@
         if blob is None:
             return None
 
-        fobj, _path, md5sum = blob
+        fd, _path, md5sum = blob
         md5sum.update(data)
         newBlobID = md5sum.digest()
-        os.write(fobj, data)
+        os.write(fd, data)
         self.blobs[newBlobID] = blob
         return newBlobID
 
     @RunInMain
     def PurgeBlobs(self):
-        for fobj, _path, _md5sum in self.blobs.values():
-            os.close(fobj)
+        for fd, _path, _md5sum in self.blobs.values():
+            os.close(fd)
         self._init_blobs()
 
     def _BlobAsFile(self, blobID, newpath):
@@ -552,8 +552,11 @@
         if blob is None:
             raise Exception(_("Missing data to create file: {}").format(newpath))
 
-        fobj, path, _md5sum = blob
-        os.close(fobj)
+        fd, path, _md5sum = blob
+        fobj = os.fdopen(fd)
+        fobj.flush()
+        os.fsync(fd)
+        fobj.close()
         shutil.move(path, newpath)
 
     def _extra_files_log_path(self):