dissect.fat 3.12.dev1__tar.gz → 3.12.dev3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. dissect_fat-3.12.dev3/.git-blame-ignore-revs +6 -0
  2. {dissect_fat-3.12.dev1/dissect.fat.egg-info → dissect_fat-3.12.dev3}/PKG-INFO +1 -1
  3. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/dissect/fat/c_exfat.py +2 -0
  4. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/dissect/fat/c_fat.py +2 -0
  5. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/dissect/fat/exfat.py +28 -26
  6. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/dissect/fat/fat.py +68 -63
  7. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3/dissect.fat.egg-info}/PKG-INFO +1 -1
  8. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/dissect.fat.egg-info/SOURCES.txt +1 -0
  9. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/pyproject.toml +48 -5
  10. dissect_fat-3.12.dev3/tests/conftest.py +43 -0
  11. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/tests/test_exceptions.py +3 -1
  12. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/tests/test_exfat.py +6 -2
  13. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/tests/test_fat.py +8 -4
  14. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/tox.ini +4 -10
  15. dissect_fat-3.12.dev1/tests/conftest.py +0 -42
  16. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/COPYRIGHT +0 -0
  17. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/LICENSE +0 -0
  18. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/MANIFEST.in +0 -0
  19. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/README.md +0 -0
  20. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/dissect/fat/__init__.py +1 -1
  21. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/dissect/fat/exceptions.py +0 -0
  22. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/dissect.fat.egg-info/dependency_links.txt +0 -0
  23. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/dissect.fat.egg-info/requires.txt +0 -0
  24. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/dissect.fat.egg-info/top_level.txt +0 -0
  25. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/setup.cfg +0 -0
  26. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/tests/__init__.py +0 -0
  27. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/tests/data/exfat.bin +0 -0
  28. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/tests/data/exfat4m.bin +0 -0
  29. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/tests/data/fat12.bin +0 -0
  30. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/tests/data/fat16.bin +0 -0
  31. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/tests/data/fat32.bin +0 -0
  32. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/tests/docs/Makefile +0 -0
  33. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/tests/docs/conf.py +0 -0
  34. {dissect_fat-3.12.dev1 → dissect_fat-3.12.dev3}/tests/docs/index.rst +0 -0
@@ -0,0 +1,6 @@
1
+ # Formatting commits. You can ignore them during git-blame with `--ignore-rev` or `--ignore-revs-file`.
2
+ #
3
+ # $ git config --add 'blame.ignoreRevsFile' '.git-blame-ignore-revs'
4
+ #
5
+ # Change linter to Ruff (#33)
6
+ 2ac481b6f2102904cb3dbe8cd23736403cb6d6fe
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: dissect.fat
3
- Version: 3.12.dev1
3
+ Version: 3.12.dev3
4
4
  Summary: A Dissect module implementing parsers for the FAT and exFAT file systems, commonly used on flash memory based storage devices and UEFI partitions
5
5
  Author-email: Dissect Team <dissect@fox-it.com>
6
6
  License: Affero General Public License v3
@@ -1,3 +1,5 @@
1
+ from __future__ import annotations
2
+
1
3
  from dissect.cstruct import cstruct
2
4
 
3
5
  exfat_def = """
@@ -1,3 +1,5 @@
1
+ from __future__ import annotations
2
+
1
3
  from dissect.cstruct import cstruct
2
4
 
3
5
  # https://ogris.de/fatrepair/fat.c
@@ -1,9 +1,12 @@
1
+ from __future__ import annotations
2
+
1
3
  import logging
2
4
  import os
3
5
  import struct
4
6
  from collections import OrderedDict
5
7
  from itertools import groupby
6
8
  from operator import itemgetter
9
+ from typing import BinaryIO
7
10
 
8
11
  from dissect.util.stream import RangeStream, RunlistStream
9
12
 
@@ -27,7 +30,7 @@ log.setLevel(os.getenv("DISSECT_LOG_EXFAT", "CRITICAL"))
27
30
 
28
31
 
29
32
  class ExFAT:
30
- def __init__(self, fh):
33
+ def __init__(self, fh: BinaryIO):
31
34
  self.filesystem = fh
32
35
  fh.seek(0)
33
36
 
@@ -56,7 +59,7 @@ class ExFAT:
56
59
  self.upcase_table = self.root_directory.upcase_entry
57
60
  self.volume_label = self.root_directory.volume_entry.volume_label.strip("\x00")
58
61
 
59
- def cluster_to_sector(self, cluster):
62
+ def cluster_to_sector(self, cluster: int) -> int | None:
60
63
  """
61
64
  Returns the clusters' corresponding sector address
62
65
 
@@ -70,7 +73,7 @@ class ExFAT:
70
73
  sector = ((cluster - 2) * (2**self.vbr.sectors_per_cluster_exp)) + self.cluster_heap_sector
71
74
  return sector if sector > 0 else None
72
75
 
73
- def sector_to_cluster(self, sector):
76
+ def sector_to_cluster(self, sector: int) -> int | None:
74
77
  """
75
78
  Returns the sectors' corresponding cluster address
76
79
 
@@ -84,7 +87,9 @@ class ExFAT:
84
87
  cluster = ((sector - self.cluster_to_sector(2)) // self.sector_size) + 2
85
88
  return cluster if cluster >= 2 else None
86
89
 
87
- def runlist(self, starting_cluster, not_fragmented=True, size=None):
90
+ def runlist(
91
+ self, starting_cluster: int, not_fragmented: bool = True, size: int | None = None
92
+ ) -> list[tuple[int, int]]:
88
93
  """
89
94
  Creates a RunlistStream compatible runlist from exFAT FAT structures, in sectors.
90
95
 
@@ -120,7 +125,7 @@ class ExFAT:
120
125
 
121
126
  return runlist
122
127
 
123
- def get_cluster_chain(self, starting_cluster):
128
+ def get_cluster_chain(self, starting_cluster: int) -> list[int]:
124
129
  """
125
130
  Reads the on disk FAT to construct the cluster chain
126
131
 
@@ -136,17 +141,17 @@ class ExFAT:
136
141
 
137
142
  if starting_cluster < 2:
138
143
  return chain
139
- else:
140
- while next_ != EOC:
141
- self.fat.seek(starting_cluster * FAT_ENTRY_SIZE)
142
- next_ = struct.unpack("<L", self.fat.read(FAT_ENTRY_SIZE))[0]
143
- chain.append(starting_cluster)
144
- starting_cluster = next_
145
144
 
146
- return chain
145
+ while next_ != EOC:
146
+ self.fat.seek(starting_cluster * FAT_ENTRY_SIZE)
147
+ next_ = struct.unpack("<L", self.fat.read(FAT_ENTRY_SIZE))[0]
148
+ chain.append(starting_cluster)
149
+ starting_cluster = next_
150
+
151
+ return chain
147
152
 
148
153
  @staticmethod
149
- def _utc_timezone(timezone):
154
+ def _utc_timezone(timezone: int) -> dict[str, str | int]:
150
155
  """
151
156
  Converts a Microsoft exFAT timezone byte to its UTC timezone equivalent
152
157
 
@@ -173,12 +178,12 @@ class ExFAT:
173
178
  utc_name = f"UTC{hours:+03}:{minutes:02}"
174
179
 
175
180
  return {"name": utc_name, "offset": utc_minute_offset}
176
- else:
177
- return {"name": "localtime", "offset": 0}
181
+
182
+ return {"name": "localtime", "offset": 0}
178
183
 
179
184
 
180
185
  class RootDirectory:
181
- def __init__(self, fh, location, exfat):
186
+ def __init__(self, fh: BinaryIO, location: int, exfat: ExFAT):
182
187
  self.exfat = exfat
183
188
  self.location = location
184
189
  self.size = 0
@@ -189,7 +194,7 @@ class RootDirectory:
189
194
  self.dict = OrderedDict()
190
195
  self._parse_root_dir(fh)
191
196
 
192
- def _parse_root_dir(self, fh):
197
+ def _parse_root_dir(self, fh: BinaryIO) -> None:
193
198
  """
194
199
  Parses the passed fh to construct the Root directory object"""
195
200
 
@@ -206,7 +211,7 @@ class RootDirectory:
206
211
  self.root_dir = RunlistStream(fh, runlist, self.size, self.exfat.sector_size)
207
212
  self.dict = self._create_root_dir(self.root_dir)
208
213
 
209
- def _parse_subdir(self, entry):
214
+ def _parse_subdir(self, entry: c_exfat.FILE) -> OrderedDict:
210
215
  """
211
216
  Parses the given sub directory file directory entry for containing files
212
217
 
@@ -225,7 +230,7 @@ class RootDirectory:
225
230
  return self._parse_file_entries(fh)
226
231
 
227
232
  @staticmethod
228
- def _construct_filename(fn_entries, is_dir=False):
233
+ def _construct_filename(fn_entries: list[c_exfat.FILENAME_DIRECTORY_ENTRY], is_dir: bool = False) -> str:
229
234
  """
230
235
  Assembles the filename from given file name directory entries
231
236
 
@@ -247,7 +252,7 @@ class RootDirectory:
247
252
 
248
253
  return filename if not is_dir else filename + "/"
249
254
 
250
- def _parse_file_entries(self, fh):
255
+ def _parse_file_entries(self, fh: BinaryIO) -> OrderedDict:
251
256
  """
252
257
  Finds and parses file entries in a given file handle (file like object)
253
258
 
@@ -271,10 +276,7 @@ class RootDirectory:
271
276
  fnentry_count = metadata.subentry_count - 1
272
277
 
273
278
  stream = c_exfat.STREAM_DIRECTORY_ENTRY(fh.read(DIR_ENTRY_SIZE))
274
- fn_entries = []
275
-
276
- for _ in range(fnentry_count):
277
- fn_entries.append(c_exfat.FILENAME_DIRECTORY_ENTRY(fh.read(DIR_ENTRY_SIZE)))
279
+ fn_entries = [c_exfat.FILENAME_DIRECTORY_ENTRY(fh.read(DIR_ENTRY_SIZE)) for _ in range(fnentry_count)]
278
280
 
279
281
  file_ = c_exfat.FILE(metadata=metadata, stream=stream, fn_entries=fn_entries)
280
282
  if file_.metadata.attributes.directory:
@@ -294,7 +296,7 @@ class RootDirectory:
294
296
 
295
297
  return entries
296
298
 
297
- def _non_file_entries(self, entry):
299
+ def _non_file_entries(self, entry: c_exfat.FILE_DIRECTORY_ENTRY) -> None:
298
300
  if entry.entry_type == VOLUME_LABEL_ENTRY or entry.entry_type == NO_VOLUME_LABEL_ENTRY:
299
301
  self.volume_entry = c_exfat.VOLUME_DIRECTORY_ENTRY(entry.dumps())
300
302
  elif entry.entry_type == BITMAP_ENTRY:
@@ -302,7 +304,7 @@ class RootDirectory:
302
304
  elif entry.entry_type == UPCASE_TABLE_ENTRY:
303
305
  self.upcase_entry = c_exfat.UPCASE_DIRECTORY_ENTRY(entry.dumps())
304
306
 
305
- def _create_root_dir(self, root_dir):
307
+ def _create_root_dir(self, root_dir: BinaryIO) -> OrderedDict:
306
308
  """
307
309
  Since exFAT does not have a dedicated root directory entry
308
310
  one has to be constructed form available parameters during filesystem parsing.
@@ -2,11 +2,13 @@
2
2
  # - https://ogris.de/fatrepair/fat.c
3
3
  # - https://github.com/nathanhi/pyfatfs
4
4
  # - https://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/fatgen103.doc
5
+ from __future__ import annotations
5
6
 
6
7
  import datetime
7
8
  import struct
8
9
  from functools import lru_cache, reduce
9
10
  from operator import itemgetter
11
+ from typing import TYPE_CHECKING, BinaryIO
10
12
 
11
13
  from dissect.util.stream import RangeStream, RunlistStream
12
14
  from dissect.util.ts import dostimestamp
@@ -34,9 +36,12 @@ from dissect.fat.exceptions import (
34
36
  NotADirectoryError,
35
37
  )
36
38
 
39
+ if TYPE_CHECKING:
40
+ from collections.abc import Iterator
41
+
37
42
 
38
43
  class FATFS:
39
- def __init__(self, fh, encoding="ibm437"):
44
+ def __init__(self, fh: BinaryIO, encoding: str = "ibm437"):
40
45
  self.fh = fh
41
46
  self.encoding = encoding
42
47
 
@@ -89,8 +94,8 @@ class FATFS:
89
94
 
90
95
  self.root = RootDirectory(self)
91
96
 
92
- def get(self, path, dirent=None):
93
- dirent = self.root if not dirent else dirent
97
+ def get(self, path: str, dirent: DirectoryEntry | RootDirectory | None = None) -> DirectoryEntry | RootDirectory:
98
+ dirent = dirent if dirent else self.root
94
99
 
95
100
  # Programmatically we will often use the `/` separator, so replace it with the native path separator of FAT
96
101
  # `/` is an illegal character in FAT filenames, so it's safe to replace
@@ -113,7 +118,7 @@ class FATFS:
113
118
  return dirent
114
119
 
115
120
 
116
- def validate_bpb(bpb):
121
+ def validate_bpb(bpb: c_fat.Bpb | bytes) -> None:
117
122
  if isinstance(bpb, bytes):
118
123
  bpb = c_fat.Bpb(bpb[: len(c_fat.Bpb)])
119
124
 
@@ -146,7 +151,7 @@ def validate_bpb(bpb):
146
151
 
147
152
 
148
153
  class FAT:
149
- def __init__(self, fh, fattype):
154
+ def __init__(self, fh: BinaryIO, fattype: Fattype):
150
155
  self.fh = fh
151
156
 
152
157
  if fattype == Fattype.FAT12:
@@ -162,7 +167,7 @@ class FAT:
162
167
 
163
168
  self.get = lru_cache(4096)(self.get)
164
169
 
165
- def get(self, cluster):
170
+ def get(self, cluster: int) -> int | None:
166
171
  if cluster >= self.entry_count:
167
172
  raise ValueError(f"Cluster exceeds FAT entry count: {cluster} >= {self.entry_count}")
168
173
 
@@ -171,20 +176,21 @@ class FAT:
171
176
  self.fh.seek(offset_in_fat)
172
177
  value = struct.unpack("<H", self.fh.read(2))[0]
173
178
 
174
- if cluster & 1:
175
- return value >> 4
176
- else:
177
- return value & 0x0FFF
178
- elif self.bits_per_entry == 16:
179
+ return value >> 4 if cluster & 1 else value & 0x0FFF
180
+
181
+ if self.bits_per_entry == 16:
179
182
  offset_in_fat = cluster * 2
180
183
  self.fh.seek(offset_in_fat)
181
184
  return struct.unpack("<H", self.fh.read(2))[0]
182
- elif self.bits_per_entry == 32:
185
+
186
+ if self.bits_per_entry == 32:
183
187
  offset_in_fat = cluster * 4
184
188
  self.fh.seek(offset_in_fat)
185
189
  return struct.unpack("<I", self.fh.read(4))[0] & 0x0FFFFFFF # FAT32 clusters are 28 bits
186
190
 
187
- def chain(self, cluster):
191
+ raise ValueError("Unsupported FAT type")
192
+
193
+ def chain(self, cluster: int) -> Iterator[int]:
188
194
  bits = self.bits_per_entry
189
195
  while True:
190
196
  value = self.get(cluster)
@@ -209,7 +215,7 @@ class FAT:
209
215
 
210
216
  cluster = value
211
217
 
212
- def runlist(self, cluster):
218
+ def runlist(self, cluster: int) -> Iterator[tuple[int, int]]:
213
219
  """Create a runlist from a cluster chain.
214
220
 
215
221
  First two clusters are reserved, so substract those.
@@ -232,7 +238,7 @@ class FAT:
232
238
 
233
239
 
234
240
  class DirectoryEntry:
235
- def __init__(self, fs, fh, parent=None):
241
+ def __init__(self, fs: FATFS, fh: BinaryIO, parent: DirectoryEntry | RootDirectory | None = None):
236
242
  self.fs = fs
237
243
  self.parent = parent
238
244
 
@@ -267,7 +273,7 @@ class DirectoryEntry:
267
273
  self.dirent = c_fat.Dirent(buf)
268
274
 
269
275
  self.ldirents.sort(key=lambda e: e.LDIR_Ord & 0x3F)
270
- name_map = map(lambda e: e.LDIR_Name1 + e.LDIR_Name2 + e.LDIR_Name3, self.ldirents)
276
+ name_map = (e.LDIR_Name1 + e.LDIR_Name2 + e.LDIR_Name3 for e in self.ldirents)
271
277
  name_reduce = bytes(reduce(lambda a, b: a + b, name_map))
272
278
  self.name = c_fat.wchar[None](name_reduce + b"\x00\x00")
273
279
  else:
@@ -280,69 +286,68 @@ class DirectoryEntry:
280
286
  base = dir_name[:8].decode(self.fs.encoding).rstrip("\x00").rstrip()
281
287
  ext = dir_name[8:].decode(self.fs.encoding).rstrip("\x00").rstrip()
282
288
 
283
- self.short_name = ".".join([base, ext]) if ext else base
289
+ self.short_name = f"{base}.{ext}" if ext else base
284
290
  if not self.name:
285
291
  self.name = self.short_name
286
292
 
287
293
  self._runlist = None
288
294
  self._entries = None
289
295
 
290
- def __repr__(self):
296
+ def __repr__(self) -> str:
291
297
  return f"<DirectoryEntry name={self.name}>"
292
298
 
293
299
  @property
294
- def path(self):
300
+ def path(self) -> str:
295
301
  return "\\".join([self.parent.path if self.parent else "", self.name]).lstrip("\\")
296
302
 
297
303
  @property
298
- def size(self):
304
+ def size(self) -> size:
299
305
  if self.is_directory():
300
306
  return sum(map(itemgetter(1), self.dataruns())) * self.fs.cluster_size
301
- else:
302
- return self.dirent.DIR_FileSize
307
+ return self.dirent.DIR_FileSize
303
308
 
304
309
  @property
305
- def cluster(self):
310
+ def cluster(self) -> int:
306
311
  return (self.dirent.DIR_FstClusHI << 16) | self.dirent.DIR_FstClusLO
307
312
 
308
313
  @property
309
- def ctime(self):
314
+ def ctime(self) -> datetime.datetime:
310
315
  if self.dirent.DIR_CrtDate and self.dirent.DIR_CrtTime:
311
316
  return dostimestamp(
312
317
  (self.dirent.DIR_CrtDate << 16) | self.dirent.DIR_CrtTime,
313
318
  self.dirent.DIR_CrtTimeTenth,
314
319
  )
315
- return datetime.datetime(1980, 1, 1)
320
+ return datetime.datetime(1980, 1, 1) # noqa: DTZ001
316
321
 
317
322
  @property
318
- def atime(self):
323
+ def atime(self) -> datetime.datetime:
319
324
  if self.dirent.DIR_LstAccDate:
320
325
  return dostimestamp(self.dirent.DIR_LstAccDate << 16)
321
- return datetime.datetime(1980, 1, 1)
326
+ return datetime.datetime(1980, 1, 1) # noqa: DTZ001
322
327
 
323
328
  @property
324
- def mtime(self):
329
+ def mtime(self) -> datetime.datetime:
325
330
  return dostimestamp((self.dirent.DIR_WrtDate << 16) | self.dirent.DIR_WrtTime)
326
331
 
327
- def is_readonly(self):
332
+ def is_readonly(self) -> bool:
328
333
  return bool(self.dirent.DIR_Attr & c_fat.ATTR_READ_ONLY)
329
334
 
330
- def is_hidden(self):
335
+ def is_hidden(self) -> bool:
331
336
  return bool(self.dirent.DIR_Attr & c_fat.ATTR_HIDDEN)
332
337
 
333
- def is_system(self):
338
+ def is_system(self) -> bool:
334
339
  return bool(self.dirent.DIR_Attr & c_fat.ATTR_SYSTEM)
335
340
 
336
- def is_volume_id(self):
341
+ def is_volume_id(self) -> bool:
337
342
  return bool(self.dirent.DIR_Attr & c_fat.ATTR_VOLUME_ID)
338
343
 
339
- def is_directory(self):
344
+ def is_directory(self) -> bool:
340
345
  return bool(self.dirent.DIR_Attr & c_fat.ATTR_DIRECTORY)
341
346
 
342
- def is_archive(self):
347
+ def is_archive(self) -> bool:
343
348
  return bool(self.dirent.DIR_Attr & c_fat.ATTR_ARCHIVE)
344
349
 
345
- def iterdir(self):
350
+ def iterdir(self) -> Iterator[DirectoryEntry]:
346
351
  if not self.is_directory():
347
352
  raise NotADirectoryError(self.name)
348
353
 
@@ -356,17 +361,17 @@ class DirectoryEntry:
356
361
  else:
357
362
  yield from self._entries
358
363
 
359
- def dataruns(self):
364
+ def dataruns(self) -> list[tuple[int, int]]:
360
365
  if self._runlist is None:
361
366
  self._runlist = [] if self.cluster == FREE_CLUSTER else list(self.fs.fat.runlist(self.cluster))
362
367
  return self._runlist
363
368
 
364
- def open(self):
369
+ def open(self) -> RunlistStream:
365
370
  return RunlistStream(self.fs.data_stream, self.dataruns(), self.size, self.fs.cluster_size)
366
371
 
367
372
 
368
373
  class RootDirectory(DirectoryEntry):
369
- def __init__(self, fs):
374
+ def __init__(self, fs: FATFS):
370
375
  self.fs = fs
371
376
  self.name = "\\"
372
377
  self.short_name = self.name
@@ -375,53 +380,52 @@ class RootDirectory(DirectoryEntry):
375
380
  self._entries = None
376
381
 
377
382
  @property
378
- def path(self):
383
+ def path(self) -> str:
379
384
  return ""
380
385
 
381
386
  @property
382
- def size(self):
387
+ def size(self) -> int:
383
388
  if self.fs.type in (Fattype.FAT12, Fattype.FAT16):
384
389
  return self.fs.bpb.BPB_RootEntCnt * 32
385
- else:
386
- return sum(map(itemgetter(1), self.dataruns())) * self.fs.cluster_size
390
+ return sum(map(itemgetter(1), self.dataruns())) * self.fs.cluster_size
387
391
 
388
392
  @property
389
- def cluster(self):
393
+ def cluster(self) -> int | None:
390
394
  if self.fs.type == Fattype.FAT32:
391
395
  return self.fs.bpb_ext.BPB_RootClus
392
396
  return None
393
397
 
394
398
  @property
395
- def ctime(self):
396
- return datetime.datetime(1980, 1, 1)
399
+ def ctime(self) -> datetime.datetime:
400
+ return datetime.datetime(1980, 1, 1) # noqa: DTZ001
397
401
 
398
402
  @property
399
- def atime(self):
400
- return datetime.datetime(1980, 1, 1)
403
+ def atime(self) -> datetime.datetime:
404
+ return datetime.datetime(1980, 1, 1) # noqa: DTZ001
401
405
 
402
406
  @property
403
- def mtime(self):
404
- return datetime.datetime(1980, 1, 1)
407
+ def mtime(self) -> datetime.datetime:
408
+ return datetime.datetime(1980, 1, 1) # noqa: DTZ001
405
409
 
406
- def is_readonly(self):
410
+ def is_readonly(self) -> bool:
407
411
  return False
408
412
 
409
- def is_hidden(self):
413
+ def is_hidden(self) -> bool:
410
414
  return False
411
415
 
412
- def is_system(self):
416
+ def is_system(self) -> bool:
413
417
  return False
414
418
 
415
- def is_volume_id(self):
419
+ def is_volume_id(self) -> bool:
416
420
  return False
417
421
 
418
- def is_directory(self):
422
+ def is_directory(self) -> bool:
419
423
  return True
420
424
 
421
- def is_archive(self):
425
+ def is_archive(self) -> bool:
422
426
  return False
423
427
 
424
- def iterdir(self):
428
+ def iterdir(self) -> Iterator[DirectoryEntry]:
425
429
  if not self._entries:
426
430
  entries = []
427
431
  for entry in _iter_dirent(self.fs, self.open(), self):
@@ -432,29 +436,30 @@ class RootDirectory(DirectoryEntry):
432
436
  else:
433
437
  yield from self._entries
434
438
 
435
- def dataruns(self):
439
+ def dataruns(self) -> list[tuple[int, int]]:
436
440
  if self._runlist is None:
437
441
  self._runlist = [] if self.cluster == FREE_CLUSTER else list(self.fs.fat.runlist(self.cluster))
438
442
  return self._runlist
439
443
 
440
- def open(self):
444
+ def open(self) -> RangeStream | RunlistStream:
441
445
  if self.fs.type in (Fattype.FAT12, Fattype.FAT16):
442
446
  root_dir_sector = self.fs.bpb.BPB_RsvdSecCnt + (self.fs.fat_size * self.fs.bpb.BPB_NumFATs)
443
447
  offset = root_dir_sector * self.fs.sector_size
444
448
  return RangeStream(self.fs.fh, offset, self.size)
445
- else:
446
- return RunlistStream(self.fs.data_stream, self.dataruns(), self.size, self.fs.cluster_size)
449
+ return RunlistStream(self.fs.data_stream, self.dataruns(), self.size, self.fs.cluster_size)
447
450
 
448
451
 
449
- def mask(v, bits):
452
+ def mask(v: int, bits: int) -> int:
450
453
  return v & ((1 << bits) - 1)
451
454
 
452
455
 
453
- def _iter_dirent(fs, fh, parent=None):
456
+ def _iter_dirent(
457
+ fs: FATFS, fh: BinaryIO, parent: DirectoryEntry | RootDirectory | None = None
458
+ ) -> Iterator[DirectoryEntry]:
454
459
  while True:
455
460
  try:
456
461
  yield DirectoryEntry(fs, fh, parent)
457
- except EmptyDirectoryError:
462
+ except EmptyDirectoryError: # noqa: PERF203
458
463
  continue
459
464
  except LastEmptyDirectoryError:
460
465
  break
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: dissect.fat
3
- Version: 3.12.dev1
3
+ Version: 3.12.dev3
4
4
  Summary: A Dissect module implementing parsers for the FAT and exFAT file systems, commonly used on flash memory based storage devices and UEFI partitions
5
5
  Author-email: Dissect Team <dissect@fox-it.com>
6
6
  License: Affero General Public License v3
@@ -1,3 +1,4 @@
1
+ .git-blame-ignore-revs
1
2
  COPYRIGHT
2
3
  LICENSE
3
4
  MANIFEST.in
@@ -41,13 +41,56 @@ dev = [
41
41
  "dissect.util>=3.0.dev,<4.0.dev",
42
42
  ]
43
43
 
44
- [tool.black]
44
+ [tool.ruff]
45
45
  line-length = 120
46
+ required-version = ">=0.9.0"
46
47
 
47
- [tool.isort]
48
- profile = "black"
49
- known_first_party = ["dissect.fat"]
50
- known_third_party = ["dissect"]
48
+ [tool.ruff.format]
49
+ docstring-code-format = true
50
+
51
+ [tool.ruff.lint]
52
+ select = [
53
+ "F",
54
+ "E",
55
+ "W",
56
+ "I",
57
+ "UP",
58
+ "YTT",
59
+ "ANN",
60
+ "B",
61
+ "C4",
62
+ "DTZ",
63
+ "T10",
64
+ "FA",
65
+ "ISC",
66
+ "G",
67
+ "INP",
68
+ "PIE",
69
+ "PYI",
70
+ "PT",
71
+ "Q",
72
+ "RSE",
73
+ "RET",
74
+ "SLOT",
75
+ "SIM",
76
+ "TID",
77
+ "TCH",
78
+ "PTH",
79
+ "PLC",
80
+ "TRY",
81
+ "FLY",
82
+ "PERF",
83
+ "FURB",
84
+ "RUF",
85
+ ]
86
+ ignore = ["E203", "B904", "UP024", "ANN002", "ANN003", "ANN204", "ANN401", "SIM105", "TRY003"]
87
+
88
+ [tool.ruff.lint.per-file-ignores]
89
+ "tests/docs/**" = ["INP001"]
90
+
91
+ [tool.ruff.lint.isort]
92
+ known-first-party = ["dissect.fat"]
93
+ known-third-party = ["dissect"]
51
94
 
52
95
  [tool.setuptools]
53
96
  license-files = ["LICENSE", "COPYRIGHT"]
@@ -0,0 +1,43 @@
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+ from typing import TYPE_CHECKING, BinaryIO
5
+
6
+ import pytest
7
+
8
+ if TYPE_CHECKING:
9
+ from collections.abc import Iterator
10
+
11
+
12
+ def absolute_path(filename: str) -> Path:
13
+ return Path(__file__).parent / filename
14
+
15
+
16
+ def open_file(name: str, mode: str = "rb") -> Iterator[BinaryIO]:
17
+ with absolute_path(name).open(mode) as f:
18
+ yield f
19
+
20
+
21
+ @pytest.fixture
22
+ def exfat_simple() -> Iterator[BinaryIO]:
23
+ yield from open_file("data/exfat.bin")
24
+
25
+
26
+ @pytest.fixture
27
+ def exfat_4m() -> Iterator[BinaryIO]:
28
+ yield from open_file("data/exfat4m.bin")
29
+
30
+
31
+ @pytest.fixture
32
+ def fat12() -> Iterator[BinaryIO]:
33
+ yield from open_file("data/fat12.bin")
34
+
35
+
36
+ @pytest.fixture
37
+ def fat16() -> Iterator[BinaryIO]:
38
+ yield from open_file("data/fat16.bin")
39
+
40
+
41
+ @pytest.fixture
42
+ def fat32() -> Iterator[BinaryIO]:
43
+ yield from open_file("data/fat32.bin")
@@ -1,10 +1,12 @@
1
+ from __future__ import annotations
2
+
1
3
  import pytest
2
4
 
3
5
  from dissect.fat import exceptions
4
6
 
5
7
 
6
8
  @pytest.mark.parametrize(
7
- "exc, std",
9
+ ("exc", "std"),
8
10
  [
9
11
  (exceptions.FileNotFoundError, FileNotFoundError),
10
12
  (exceptions.IsADirectoryError, IsADirectoryError),
@@ -1,7 +1,11 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import BinaryIO
4
+
1
5
  from dissect.fat import exfat
2
6
 
3
7
 
4
- def test_exfat(exfat_simple):
8
+ def test_exfat(exfat_simple: BinaryIO) -> None:
5
9
  e = exfat.ExFAT(exfat_simple)
6
10
 
7
11
  assert e.volume_label == "THESIS"
@@ -41,7 +45,7 @@ def test_exfat(exfat_simple):
41
45
  assert sysvol.stream.data_length == 512
42
46
 
43
47
 
44
- def test_exfat_4m(exfat_4m):
48
+ def test_exfat_4m(exfat_4m: BinaryIO) -> None:
45
49
  e = exfat.ExFAT(exfat_4m)
46
50
 
47
51
  assert e.volume_label == ""
@@ -1,7 +1,11 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import BinaryIO
4
+
1
5
  from dissect.fat import fat
2
6
 
3
7
 
4
- def test_fat12(fat12):
8
+ def test_fat12(fat12: BinaryIO) -> None:
5
9
  volume_label = "VOLLAB1"
6
10
 
7
11
  fs = fat.FATFS(fat12)
@@ -15,7 +19,7 @@ def test_fat12(fat12):
15
19
  verify_fs_content(fs, volume_label)
16
20
 
17
21
 
18
- def test_fat16(fat16):
22
+ def test_fat16(fat16: BinaryIO) -> None:
19
23
  volume_label = "LABFAT16"
20
24
 
21
25
  fs = fat.FATFS(fat16)
@@ -29,7 +33,7 @@ def test_fat16(fat16):
29
33
  verify_fs_content(fs, volume_label)
30
34
 
31
35
 
32
- def test_fat32(fat32):
36
+ def test_fat32(fat32: BinaryIO) -> None:
33
37
  volume_label = "LABFAT32"
34
38
 
35
39
  fs = fat.FATFS(fat32)
@@ -43,7 +47,7 @@ def test_fat32(fat32):
43
47
  verify_fs_content(fs, volume_label)
44
48
 
45
49
 
46
- def verify_fs_content(fs, volume_label):
50
+ def verify_fs_content(fs: fat.FATFS, volume_label: str) -> None:
47
51
  entries_map = {e.name: e for e in fs.root.iterdir()}
48
52
 
49
53
  assert set(entries_map.keys()) == {volume_label, "file1.txt", "file2.txt", "subdir1"}
@@ -32,23 +32,17 @@ commands =
32
32
  [testenv:fix]
33
33
  package = skip
34
34
  deps =
35
- black==23.1.0
36
- isort==5.11.4
35
+ ruff==0.9.2
37
36
  commands =
38
- black dissect tests
39
- isort dissect tests
37
+ ruff format dissect tests
40
38
 
41
39
  [testenv:lint]
42
40
  package = skip
43
41
  deps =
44
- black==23.1.0
45
- flake8
46
- flake8-black
47
- flake8-isort
48
- isort==5.11.4
42
+ ruff==0.9.2
49
43
  vermin
50
44
  commands =
51
- flake8 dissect tests
45
+ ruff check dissect tests
52
46
  vermin -t=3.9- --no-tips --lint dissect tests
53
47
 
54
48
  [flake8]
@@ -1,42 +0,0 @@
1
- import os
2
-
3
- import pytest
4
-
5
-
6
- def absolute_path(filename):
7
- return os.path.join(os.path.dirname(__file__), filename)
8
-
9
-
10
- @pytest.fixture
11
- def exfat_simple():
12
- name = "data/exfat.bin"
13
- with open(absolute_path(name), "rb") as f:
14
- yield f
15
-
16
-
17
- @pytest.fixture
18
- def exfat_4m():
19
- name = "data/exfat4m.bin"
20
- with open(absolute_path(name), "rb") as f:
21
- yield f
22
-
23
-
24
- @pytest.fixture
25
- def fat12():
26
- name = "data/fat12.bin"
27
- with open(absolute_path(name), "rb") as f:
28
- yield f
29
-
30
-
31
- @pytest.fixture
32
- def fat16():
33
- name = "data/fat16.bin"
34
- with open(absolute_path(name), "rb") as f:
35
- yield f
36
-
37
-
38
- @pytest.fixture
39
- def fat32():
40
- name = "data/fat32.bin"
41
- with open(absolute_path(name), "rb") as f:
42
- yield f
File without changes
@@ -14,11 +14,11 @@ from dissect.fat.exfat import ExFAT
14
14
  from dissect.fat.fat import FATFS
15
15
 
16
16
  __all__ = [
17
- "ExFAT",
18
17
  "FATFS",
19
18
  "BadClusterError",
20
19
  "EmptyDirectoryError",
21
20
  "Error",
21
+ "ExFAT",
22
22
  "FileNotFoundError",
23
23
  "FreeClusterError",
24
24
  "InvalidBPB",