Esempio n. 1
0
    def test_cd(self):
        d = fsobjects.File("DIR", "", fsobjects.DIRECTORY)
        d1 = fsobjects.File("DIR1", "", fsobjects.DIRECTORY)
        d.content = [d1]
        names = ["File1.txt", "File2.txt", "File3.txt"]
        d1.content = list(generate_files_from_names(names))

        db = dirbrowser.DirectoryBrowser(root=d)
        db.change_directory("DIR1")

        self.assertEqual(db.current, d1)
Esempio n. 2
0
    def test_get_absolute_path(self):
        root = fsobjects.File("root", "root", fsobjects.DIRECTORY)
        self.assertEqual(root.get_absolute_path(), "root")

        folder1 = fsobjects.File("Folder1", "Folder1", fsobjects.DIRECTORY)
        folder1.parent = root
        self.assertEqual(folder1.get_absolute_path(), "root/Folder1")

        folder2 = fsobjects.File("Folder2", "Folder2", fsobjects.DIRECTORY)
        folder2.parent = folder1
        self.assertEqual(folder2.get_absolute_path(), "root/Folder1/Folder2")
Esempio n. 3
0
def parse_file_info(entry_parser, long_file_name_buffer=""):
    attributes = entry_parser.parse_int_unsigned(11, 1)
    is_directory = bool(attributes & fsobjects.DIRECTORY)

    name_part = entry_parser.parse_string(0, 8, "cp866", "strict").strip()
    extension_part = entry_parser.parse_string(8, 3, "cp866", "strict").strip()
    short_name = name_part + (
        ('.' + extension_part) if not is_directory else "")

    creation_datetime = parse_creation_datetime(entry_parser)

    try:
        last_access_date = entry_parser.parse_date(18)
    except ValueError:
        last_access_date = None
    try:
        last_modification_datetime = entry_parser.parse_time_date(22)
    except ValueError:
        last_modification_datetime = None

    file_size_bytes = entry_parser.parse_int_unsigned(0 + 28, 4)

    return fsobjects.File(short_name, long_file_name_buffer, attributes,
                          creation_datetime, last_access_date,
                          last_modification_datetime, file_size_bytes)
Esempio n. 4
0
 def get_root_directory(self):
     if self.log_clusters_usage or self.log_clusters_usage_adv:
         for cluster in self._get_cluster_chain(
                 self.root_catalog_first_cluster):
             self.used_clusters[cluster] = True
     root = fsobjects.File("", "", fsobjects.DIRECTORY, None, None, None, 0,
                           self.root_catalog_first_cluster)
     root.content = self._parse_dir_files(
         self.get_data_from_cluster_chain(self.root_catalog_first_cluster),
         root)
     return root
Esempio n. 5
0
    def write_to_image(self,
                       external_path,
                       internal_path,
                       directory=None) -> fsobjects.File:
        """
        Writes file to image, returns File
        """
        path = pathlib.Path(external_path)
        if not path.exists():
            raise FileNotFoundError(str(path) + " not found.")

        if directory is None:
            directory = find_directory(self.get_root_directory(),
                                       internal_path)

        name = ("/" + str(path.absolute()).replace("\\", "/")).split("/")[-1]
        short_name = fsobjects.get_short_name(name, directory=directory)
        attributes = fsobjects.DIRECTORY if path.is_dir() else 0

        creation_datetime, last_access_date, modification_datetime = \
            get_time_stamps(external_path)

        file = fsobjects.File(long_name=name,
                              short_name=short_name,
                              create_datetime=creation_datetime,
                              change_datetime=modification_datetime,
                              last_open_date=last_access_date,
                              attributes=attributes)

        file.parent = directory
        first_cluster, size_bytes = self._write_external_file_content(
            path, file)

        file._start_cluster = first_cluster
        file._size_bytes = size_bytes

        self._append_content_to_dir(directory, file.to_directory_entries())

        return file
Esempio n. 6
0
def generate_files_from_names(names):
    for name in names:
        yield fsobjects.File("", name)
Esempio n. 7
0
 def test_get_dir_content_names_not_a_dir(self):
     file = fsobjects.File("", "File.txt")
     with self.assertRaises(NotADirectoryError):
         list(dirbrowser._get_dir_content_names(file))
Esempio n. 8
0
 def test_get_dir_content_names_empty_dir(self):
     d = fsobjects.File("DIR", "", fsobjects.DIRECTORY)
     d.content = []
     self.assertEqual(list(dirbrowser._get_dir_content_names(d)), [])
Esempio n. 9
0
 def test_get_dir_content_names(self):
     names = ["File1.txt", "File2.txt", "File3.txt"]
     d = fsobjects.File("DIR", "", fsobjects.DIRECTORY)
     d.content = list(generate_files_from_names(names))
     self.assertEqual(list(dirbrowser._get_dir_content_names(d)), names)
Esempio n. 10
0
 def test_size_format_gibibytes(self):
     file = fsobjects.File("file", "file", size_bytes=281382002220)
     self.assertEqual("262.06 GiB (281382002220 bytes)",
                      file.get_size_str())
Esempio n. 11
0
 def test_size_format_mebibytes(self):
     file = fsobjects.File("file", "file", size_bytes=6389353)
     self.assertEqual("6.09 MiB (6389353 bytes)", file.get_size_str())
Esempio n. 12
0
 def test_size_format_kibibytes(self):
     file = fsobjects.File("file", "file", size_bytes=855310)
     self.assertEqual("835.26 KiB (855310 bytes)", file.get_size_str())
Esempio n. 13
0
 def test_size_format_bytes(self):
     file = fsobjects.File("file", "file", size_bytes=5)
     self.assertEqual("5 bytes", file.get_size_str())