#1947: clean up existing work and clear up some itesm left in TODO

This commit is contained in:
Czar.Echavez
2023-10-18 13:21:05 +01:00
parent 4ee2235dd1
commit 0edb9b46a7
3 changed files with 28 additions and 47 deletions

View File

@@ -110,11 +110,9 @@ Clone & Install PrimAITE for Development
To be able to extend PrimAITE further, or to build wheels manually before install, clone the repository to a location To be able to extend PrimAITE further, or to build wheels manually before install, clone the repository to a location
of your choice: of your choice:
.. TODO:: Add repo path once we know what it is
.. code-block:: bash .. code-block:: bash
git clone <repo path> git clone https://github.com/Autonomous-Resilient-Cyber-Defence/PrimAITE
cd primaite cd primaite
Create and activate your Python virtual environment (venv) Create and activate your Python virtual environment (venv)

View File

@@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
import copy
import hashlib import hashlib
import json import json
import math import math
@@ -358,8 +359,6 @@ class FileSystem(SimComponent):
return return
folder = self._folders_by_name.get(folder_name) folder = self._folders_by_name.get(folder_name)
if folder: if folder:
for file in folder.files.values():
self.delete_file(folder_name=folder_name, file_name=file.name)
# add to deleted list # add to deleted list
folder.delete() folder.delete()
self.deleted_folders[folder.uuid] = folder self.deleted_folders[folder.uuid] = folder
@@ -489,7 +488,7 @@ class FileSystem(SimComponent):
new_file.sim_path.parent.mkdir(exist_ok=True) new_file.sim_path.parent.mkdir(exist_ok=True)
shutil.copy2(file.sim_path, new_file.sim_path) shutil.copy2(file.sim_path, new_file.sim_path)
def restore_file(self, folder_id: str, file_id: str) -> bool: def restore_file(self, folder_id: str, file_id: str):
""" """
Restore a file. Restore a file.
@@ -501,11 +500,7 @@ class FileSystem(SimComponent):
:param: folder_id: id of the file to restore :param: folder_id: id of the file to restore
:type: folder_id: str :type: folder_id: str
""" """
folder = self.get_folder_by_id(folder_uuid=folder_id, show_deleted=True) pass
if folder:
file = folder.get_file_by_id(file_uuid=file_id, show_deleted=True)
return file.restore()
def get_folder(self, folder_name: str) -> Optional[Folder]: def get_folder(self, folder_name: str) -> Optional[Folder]:
""" """
@@ -648,6 +643,16 @@ class Folder(FileSystemItemABC):
else: else:
_LOGGER.debug(f"File with UUID {file.uuid} was not found.") _LOGGER.debug(f"File with UUID {file.uuid} was not found.")
def restore_file(self, file: Optional[File]):
"""
Restores a file.
The method can take a File object or a file id.
:param file: The file to remove
"""
pass
def quarantine(self): def quarantine(self):
"""Quarantines the File System Folder.""" """Quarantines the File System Folder."""
if self.status != FileSystemItemStatus.QUARANTINED: if self.status != FileSystemItemStatus.QUARANTINED:
@@ -726,9 +731,17 @@ class Folder(FileSystemItemABC):
"""TODO.""" """TODO."""
pass pass
def delete(self) -> bool: def delete(self):
"""TODO.""" """Deletes the files within the folder and then deletes the folder."""
super().delete() super().delete()
# iterate through the files in the folder
files = copy.copy(self.files)
for file_id in files:
file = self.get_file_by_id(file_uuid=file_id)
file.delete()
self.remove_file(file)
self.fs.sys_log.info(f"Deleted folder {self.name} (id: {self.uuid})") self.fs.sys_log.info(f"Deleted folder {self.name} (id: {self.uuid})")
def corrupt(self) -> bool: def corrupt(self) -> bool:
@@ -742,7 +755,7 @@ class Folder(FileSystemItemABC):
file = self.get_file_by_id(file_uuid=file_id) file = self.get_file_by_id(file_uuid=file_id)
corrupted = file.corrupt() corrupted = file.corrupt()
# set file status to good if corrupt # set file status to corrupt if good
if self.status == FileSystemItemStatus.GOOD: if self.status == FileSystemItemStatus.GOOD:
self.status = FileSystemItemStatus.CORRUPTED self.status = FileSystemItemStatus.CORRUPTED
corrupted = True corrupted = True
@@ -842,7 +855,7 @@ class File(FileSystemItemABC):
return state return state
def scan(self) -> None: def scan(self) -> None:
"""TODO.""" """Updates the visible statuses of the file."""
super().scan() super().scan()
path = self.folder.name + "/" + self.name path = self.folder.name + "/" + self.name
@@ -882,7 +895,7 @@ class File(FileSystemItemABC):
return True return True
def delete(self) -> None: def delete(self) -> None:
"""TODO.""" """Deletes the file."""
super().delete() super().delete()
path = self.folder.name + "/" + self.name path = self.folder.name + "/" + self.name
@@ -902,14 +915,7 @@ class File(FileSystemItemABC):
def restore(self) -> None: def restore(self) -> None:
"""Restore a corrupted File by setting the status to FileSystemItemStatus.GOOD.""" """Restore a corrupted File by setting the status to FileSystemItemStatus.GOOD."""
super().restore() pass
# set file status to good if deleted or corrupt
if self.status in [FileSystemItemStatus.CORRUPTED, FileSystemItemStatus.DELETED]:
self.status = FileSystemItemStatus.GOOD
path = self.folder.name + "/" + self.name
self.folder.fs.sys_log.info(f"Repaired file {self.sim_path if self.sim_path else path}")
def corrupt(self) -> bool: def corrupt(self) -> bool:
"""Corrupt a File by setting the status to FileSystemItemStatus.CORRUPTED.""" """Corrupt a File by setting the status to FileSystemItemStatus.CORRUPTED."""

View File

@@ -217,29 +217,6 @@ def test_folder_scan(file_system):
assert file.visible_status == FileSystemItemStatus.CORRUPTED assert file.visible_status == FileSystemItemStatus.CORRUPTED
def test_file_delete_restore(file_system):
"""Test the ability to delete and restore a file."""
folder: Folder = file_system.create_folder(folder_name="test_folder")
file: File = file_system.create_file(file_name="test_file.txt", folder_name="test_folder")
assert file.status == FileSystemItemStatus.GOOD
assert file.visible_status == FileSystemItemStatus.GOOD
file_system.delete_file(folder_name=folder.name, file_name=file.name)
assert folder.get_file(file_name=file.name) is None
assert folder.get_file_by_id(file_uuid=file.uuid, show_deleted=True).status == FileSystemItemStatus.DELETED
file_system.restore_file(folder_id=folder.uuid, file_id=file.uuid)
assert file.status == FileSystemItemStatus.GOOD
assert folder.get_file(file_name=file.name) is not None
def test_folder_delete_restore(file_system):
pass
def test_simulated_file_check_hash(file_system): def test_simulated_file_check_hash(file_system):
file: File = file_system.create_file(file_name="test_file.txt", folder_name="test_folder") file: File = file_system.create_file(file_name="test_file.txt", folder_name="test_folder")