Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pyfatfs/FSInfo.py: 59%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

22 statements  

1# -*- coding: utf-8 -*- 

2 

3"""FSInfo structure handling for FAT32 filesystems.""" 

4 

5import struct 

6from collections import OrderedDict 

7 

8 

9class FSInfo(OrderedDict): 

10 """Class for FAT32 FSInfo sector implementation.""" 

11 

12 #: Magic value at the start of an FSInfo struct 

13 LEAD_SIG_MAGIC = 0x41615252 

14 #: Magic value in the middle of an FSInfo struct 

15 STRUCT_SIG_MAGIC = 0x61417272 

16 #: Magic value at the end of an FSInfo struct 

17 TRAIL_SIG_MAGIC = 0xAA550000 

18 

19 #: FSI header layout in struct formatted string 

20 HEADER_LAYOUT = "<L480xLLL12xL" 

21 #: FSI header fields 

22 HEADER_VARS = ["FSI_LeasSig", 

23 "FSI_StrucSig", 

24 "FSI_Free_Count", 

25 "FSI_Nxt_Free", 

26 "FSI_TrailSig"] 

27 

28 def __init__(self, free_count=None, next_free=None): 

29 """Initialize an empty Boot sector header.""" 

30 super().__init__() 

31 self.update({"FSI_LeasSig": FSInfo.LEAD_SIG_MAGIC, 

32 "FSI_StrucSig": FSInfo.STRUCT_SIG_MAGIC, 

33 "FSI_Free_Count": free_count, 

34 "FSI_Nxt_Free": next_free, 

35 "FSI_TrailSig": FSInfo.TRAIL_SIG_MAGIC}) 

36 

37 def __bytes__(self): 

38 """Serialize header data back to bytes.""" 

39 return struct.pack(self.HEADER_LAYOUT, *self.values()) 

40 

41 def __len__(self): 

42 """Return struct size.""" 

43 return struct.calcsize(self.HEADER_LAYOUT) 

44 

45 def parse_header(self, data: bytes) -> "FSInfo": 

46 """Deserialize FSInfo binary data into FSInfo class instance. 

47 

48 :param data: `bytes`: 512 bytes of binary data to be deserialized 

49 """ 

50 if len(data) < len(self): 

51 raise ValueError(f"Invalid FSInfo sector data supplied " 

52 f"for {type(self)}. len(data)={len(data)}, " 

53 f"len(header)={len(self)}") 

54 

55 fsinfo = struct.unpack(self.HEADER_LAYOUT, data[:len(self)]) 

56 self.update(dict(zip(self.HEADER_VARS, fsinfo)))