From fe506eb6d29b5d0d135d6eb6b012e30ed8106640 Mon Sep 17 00:00:00 2001
From: Justus Mueller <justus.mueller@fau.de>
Date: Thu, 13 Jun 2024 20:38:41 +0200
Subject: [PATCH] fix: Revert to rolling pattern

---
 src/drive_handle.py     | 35 ++++++++++-------------------------
 src/tests/slack_test.py |  8 +++-----
 2 files changed, 13 insertions(+), 30 deletions(-)

diff --git a/src/drive_handle.py b/src/drive_handle.py
index 13d6637..371cade 100644
--- a/src/drive_handle.py
+++ b/src/drive_handle.py
@@ -143,7 +143,7 @@ class DriveHandle:
             return True
         return False
 
-    def write_pattern(self, symbol: chr, path: str) -> None:
+    def write_pattern(self, path: str) -> None:
         """
         Writes the pattern file to the disk
 
@@ -151,33 +151,18 @@ class DriveHandle:
             symbol (chr): The symbol the pattern file shall be filled with
             path (str): The path to the pattern file
         """
-        print(colored(f"[DriveHandle] Writing a file ({path}) with 0x{symbol.encode().hex()} pattern to fill the disk.", "light_grey"))
+        print(colored(f"[DriveHandle] Writing a file ({path}) with a rolling pattern to fill the disk.", "light_grey"))
+        pattern_start_symbol : chr = 'A'
+        iteration : int = 0
+        (_, _, cluster_size) = self.get_allocation_unit_sizes()
         progressbar = Spinner('[DriveHandle] Writing the pattern file')
-
-        write_string = str(symbol) * 1024 * 1024 * 5  # 5 MiB
-        file_descriptor = open(path, "w")
-        while True:
-            try:
-                file_descriptor.write(write_string)
-                file_descriptor.flush()
+        with open(path, "wb") as file_descriptor:
+            while os.path.getsize(path)+cluster_size < self.get_free_drive_space():
+                current_pattern_symbol : chr = chr(ord(pattern_start_symbol)+iteration)
+                file_descriptor.write(current_pattern_symbol.encode() * cluster_size)
+                iteration = (iteration + 1) % 26
                 progressbar.next()
-            except OSError as error:
-                if error.errno == errno.ENOSPC:
-                    if len(write_string) > 1:
-                        write_string = write_string[:(len(write_string) // 2)]
-                        if (self.get_free_drive_space() == 0 and len(write_string) <= 1):
-                            break
-                    else:
-                        break
-                else:
-                    raise
-
         progressbar.finish()
-        try:
-            file_descriptor.close()
-        except OSError as error:
-            if error.errno != errno.ENOSPC:
-                raise
         print(colored("[DriveHandle] Completed writing the pattern file.", "light_grey"))
 
     def create_overwrite_file(self, symbol: chr, path: str) -> None:
diff --git a/src/tests/slack_test.py b/src/tests/slack_test.py
index 94b6337..d0c2e16 100644
--- a/src/tests/slack_test.py
+++ b/src/tests/slack_test.py
@@ -31,10 +31,9 @@ class SlackTest(DecisionTest):
 
         # create pattern
         pattern_file_path: str = f'{self.drive_handle.get_path()}transsd-slacktest-pattern'
-        self.drive_handle.write_pattern('A', pattern_file_path)
-        self.drive_handle.flush_cache()
+        self.drive_handle.write_pattern(pattern_file_path)
         print(colored("[SlackTest] Waiting 120s for the OS to flush the write cache.", "light_grey"))
-        time.sleep(120)
+
         pattern_file_clusters: list = []
         if self._check_cluster_overlap:
             pattern_file_clusters = self.drive_handle.get_clusters_for_file(pattern_file_path)
@@ -43,9 +42,8 @@ class SlackTest(DecisionTest):
 
         # delete file
         self.drive_handle.delete_file(pattern_file_path)
-        self.drive_handle.flush_cache()
         print("[SlackTest] Waiting 120s for OS to clean up the disk...")
-        time.sleep(120)
+        time.sleep(60)
         print(colored("[SlackTest] File should be removed from disk without remainder.", "light_grey"))
 
         # create overwriting file
-- 
GitLab