From 042a368f7b2244ad4b727ac2f46f5c112b197b2a Mon Sep 17 00:00:00 2001
From: Justus Mueller <justus.mueller@fau.de>
Date: Sat, 1 Jun 2024 15:33:38 +0200
Subject: [PATCH] feat: Add write cache flush

---
 hash_longterm.py        | 3 +++
 src/drive_handle.py     | 4 ++++
 src/tests/hash_test.py  | 4 +++-
 src/tests/slack_test.py | 2 ++
 4 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/hash_longterm.py b/hash_longterm.py
index 1322991..68f7560 100644
--- a/hash_longterm.py
+++ b/hash_longterm.py
@@ -70,11 +70,14 @@ if __name__ == "__main__":
     print(colored(log("[Hash-Longtermstudy] Starting the hash run!"), "light_blue"))
     drive_handle.copy_demo_files()
     print(colored(log("[Hash-Longtermstudy] Waiting 120s for the OS to flush the write buffer."), "cyan"))
+    drive_handle.flush_cache()
     time.sleep(120)
     # create hash
     print(colored(log(f"[Hash-Longtermstudy] The hash prior to the deletion is {drive_handle.generate_hash()}"), "light_blue"))
     # delete data
     drive_handle.remove_demo_files()
+    # flush the cache
+    drive_handle.flush_cache()
     # set drive to read only
     drive_handle.set_read_only(True)
     # repeadetly create the hash
diff --git a/src/drive_handle.py b/src/drive_handle.py
index a1df2c2..a0a6be7 100644
--- a/src/drive_handle.py
+++ b/src/drive_handle.py
@@ -173,3 +173,7 @@ class DriveHandle:
         print(colored(f"[DriveHandle] Setting drive {self._drive_letter}'s read-only state to {read_only}.", "light_grey"))
         powershell_bool: str = "$true" if read_only else "$false"
         subprocess.check_output(['powershell.exe', f'Get-Disk -Number (Get-Partition -DriveLetter {self._drive_letter}).DiskNumber | Set-Disk -IsReadOnly {powershell_bool}'])
+
+    def flush_cache(self) -> None:
+        print(colored("[DriveHandle] Flushing drive's write cache.", "light_grey"))
+        subprocess.check_output(['powershell.exe', f'Write-VolumeCache -DriveLetter {self._drive_letter}'])
diff --git a/src/tests/hash_test.py b/src/tests/hash_test.py
index 772b4ca..e4f6f01 100644
--- a/src/tests/hash_test.py
+++ b/src/tests/hash_test.py
@@ -19,11 +19,13 @@ class HashTest(DecisionTest):
         print(colored("[HashTest] Running the hash test!", 'light_blue'))
         print(colored("[HashTest] Copying the demo files.", 'light_grey'))
         self._drive_handle.copy_demo_files()
-        print(colored("[HashTest] Waiting 120s for the OS to flush the write buffer.", "light_grey"))
+        print(colored("[HashTest] Waiting 120s for the OS to flush the write cache.", "light_grey"))
+        self._drive_handle.flush_cache()
         time.sleep(120)
         print(colored(f"[HashTest] The current hash of the drive is equal to {self._drive_handle.generate_hash()}", "yellow"))
         print(colored("[HashTest] Deleting the demo files.", 'light_grey'))
         self._drive_handle.remove_demo_files()
+        self._drive_handle.flush_cache()
         self._drive_handle.set_read_only(True)
         detected_wear_levelling: bool = False
         prior_hash: str = None
diff --git a/src/tests/slack_test.py b/src/tests/slack_test.py
index d65e625..805f03b 100644
--- a/src/tests/slack_test.py
+++ b/src/tests/slack_test.py
@@ -23,6 +23,7 @@ class SlackTest(DecisionTest):
         # create rolling pattern
         pattern_file_path: str = f'{self._drive_handle.get_path()}transsd-slacktest-pattern'
         self._drive_handle.write_pattern(pattern_file_path)
+        self._drive_handle.flush_cache()
         pattern_file_clusters: list = []
         if self._check_cluster_overlap:
             pattern_file_clusters = self._drive_handle.get_clusters_for_file(pattern_file_path)
@@ -32,6 +33,7 @@ class SlackTest(DecisionTest):
         # delete file
         self._drive_handle.delete_file(pattern_file_path)
         print("[SlackTest] Waiting 120s for OS to clean up the disk...")
+        self._drive_handle.flush_cache()
         time.sleep(120)
         print(colored("[SlackTest] File should be removed from disk without remainder.", "light_grey"))
 
-- 
GitLab