PyFat

FAT and BPB parsing for files.

class pyfatfs.PyFat.PyFat(encoding: str = 'ibm437', offset: int = 0, lazy_load: bool = True)

Bases: object

PyFAT base class, parses generic filesystem information.

Set up PyFat class instance.

Parameters:
  • encoding (str) – Define encoding to use for filenames

  • offset (int) – Offset of the FAT partition in the given file

FAT12_CLUSTER_VALUES = {'BAD_CLUSTER': 4087, 'END_OF_CLUSTER_MAX': 4095, 'END_OF_CLUSTER_MIN': 4088, 'FREE_CLUSTER': 0, 'MAX_DATA_CLUSTER': 4079, 'MIN_DATA_CLUSTER': 2}

Possible cluster values for FAT12 partitions

FAT12_SPECIAL_EOC = 4080
FAT16_CLEAN_SHUTDOWN_BIT_MASK = 32768

FAT16 bit mask for clean shutdown bit

FAT16_CLUSTER_VALUES = {'BAD_CLUSTER': 65527, 'END_OF_CLUSTER_MAX': 65535, 'END_OF_CLUSTER_MIN': 65528, 'FREE_CLUSTER': 0, 'MAX_DATA_CLUSTER': 65519, 'MIN_DATA_CLUSTER': 2}

Possible cluster values for FAT16 partitions

FAT16_DRIVE_ERROR_BIT_MASK = 16384

FAT16 bit mask for volume error bit

FAT32_CLEAN_SHUTDOWN_BIT_MASK = 134217728

FAT32 bit mask for clean shutdown bit

FAT32_CLUSTER_VALUES = {'BAD_CLUSTER': 268435447, 'END_OF_CLUSTER_MAX': 268435455, 'END_OF_CLUSTER_MIN': 268435448, 'FREE_CLUSTER': 0, 'MAX_DATA_CLUSTER': 268435439, 'MIN_DATA_CLUSTER': 2}

Possible cluster values for FAT32 partitions

FAT32_DRIVE_ERROR_BIT_MASK = 67108864

FAT32 bit mask for volume error bit

FAT_CLUSTER_VALUES = {12: {'BAD_CLUSTER': 4087, 'END_OF_CLUSTER_MAX': 4095, 'END_OF_CLUSTER_MIN': 4088, 'FREE_CLUSTER': 0, 'MAX_DATA_CLUSTER': 4079, 'MIN_DATA_CLUSTER': 2}, 16: {'BAD_CLUSTER': 65527, 'END_OF_CLUSTER_MAX': 65535, 'END_OF_CLUSTER_MIN': 65528, 'FREE_CLUSTER': 0, 'MAX_DATA_CLUSTER': 65519, 'MIN_DATA_CLUSTER': 2}, 32: {'BAD_CLUSTER': 268435447, 'END_OF_CLUSTER_MAX': 268435455, 'END_OF_CLUSTER_MIN': 268435448, 'FREE_CLUSTER': 0, 'MAX_DATA_CLUSTER': 268435439, 'MIN_DATA_CLUSTER': 2}}

Maps fat_type to possible cluster values

FAT_DIRTY_BIT_MASK = 1

Dirty bit in FAT header

FAT_TYPE_FAT12 = 12

Used as fat_type if FAT12 fs has been detected

FAT_TYPE_FAT16 = 16

Used as fat_type if FAT16 fs has been detected

FAT_TYPE_FAT32 = 32

Used as fat_type if FAT32 fs has been detected

FAT_TYPE_UNKNOWN = 0

Used as fat_type if unable to detect FAT type

FS_TYPES = {0: b'FAT     ', 12: b'FAT12   ', 16: b'FAT16   ', 32: b'FAT32   '}

Maps fat_type to BS_FilSysType from FS header information

allocate_bytes(**kwargs)
calc_num_clusters(size: int = 0) int

Calculate the number of required clusters.

Parameters:

sizeint: required bytes to allocate

Returns:

Number of required clusters

close(**kwargs)
flush_fat(**kwargs)
free_cluster_chain(**kwargs)
get_cluster_chain(**kwargs)
get_data_cluster_address(cluster: int) int

Get offset of given cluster in bytes.

Parameters:

cluster – Cluster number as int

Returns:

Bytes address location of cluster

get_fs_location(**kwargs)
mkfs(filename: str, fat_type: 12 | 16 | 32, size: int = None, sector_size: int = 512, number_of_fats: int = 2, label: str = 'NO NAME', volume_id: int = None, media_type: int = 248)

Create a new FAT filesystem.

Parameters:
  • filenamestr: Name of file to create filesystem in

  • fat_typeFAT_TYPE_FAT{12,16,32}: FAT type

  • sizeint: Size of new filesystem in bytes

  • sector_sizeint: Size of a sector in bytes

  • number_of_fatsint: Number of FATs on the disk

  • labelstr: Volume label

  • volume_idbytes: Volume id (4 bytes)

  • media_typeint: Media type (0xF{0,8-F})

open(filename: str | PathLike, read_only: bool = False)

Open filesystem for usage with PyFat.

Parameters:
  • filenamestr: Name of file to open for usage with PyFat.

  • read_onlybool: Force read-only mode of filesystem.

static open_fs(filename: str, offset: int = 0, encoding='ibm437')

Context manager for direct use of PyFAT.

parse_dir_entries_in_address(address: int = 0, max_address: int = 0, tmp_lfn_entry: FATLongDirectoryEntry = None)

Parse directory entries in address range.

parse_dir_entries_in_cluster_chain(cluster) list

Parse directory entries while following given cluster chain.

parse_header()

Parse BPB & FAT headers in opened file.

parse_lfn_entry(lfn_entry: FATLongDirectoryEntry = None, address: int = 0)

Parse LFN entry at given address.

parse_root_dir()

Parse root directory entry.

read_cluster_contents(**kwargs)
set_fp(fp: BytesIO | IOBase)

Open a filesystem from a valid file pointer.

This allows using in-memory filesystems (e.g., BytesIO).

Parameters:

fpFileIO: Valid FileIO object

update_directory_entry(**kwargs)
write_data_to_cluster(**kwargs)