Refactor format() (#2512)

This commit is contained in:
codefiles 2024-05-18 18:03:11 -04:00 committed by GitHub
parent 15ee84b7c9
commit 09fdb9e80c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 32 deletions

View File

@ -198,49 +198,34 @@ class DeviceHandler(object):
path: Path, path: Path,
additional_parted_options: List[str] = [] additional_parted_options: List[str] = []
): ):
mkfs_type = fs_type.value
options = [] options = []
command = ''
match fs_type: match fs_type:
case FilesystemType.Btrfs: case FilesystemType.Btrfs | FilesystemType.F2fs | FilesystemType.Xfs:
options += ['-f'] # Force overwrite
command += 'mkfs.btrfs' options.append('-f')
case FilesystemType.Fat16: case FilesystemType.Ext2 | FilesystemType.Ext3 | FilesystemType.Ext4:
options += ['-F16'] # Force create
command += 'mkfs.fat' options.append('-F')
case FilesystemType.Fat32: case FilesystemType.Fat16 | FilesystemType.Fat32:
options += ['-F32'] mkfs_type = 'fat'
command += 'mkfs.fat' # Set FAT size
case FilesystemType.Ext2: options.extend(('-F', fs_type.value.removeprefix(mkfs_type)))
options += ['-F']
command += 'mkfs.ext2'
case FilesystemType.Ext3:
options += ['-F']
command += 'mkfs.ext3'
case FilesystemType.Ext4:
options += ['-F']
command += 'mkfs.ext4'
case FilesystemType.Xfs:
options += ['-f']
command += 'mkfs.xfs'
case FilesystemType.F2fs:
options += ['-f']
command += 'mkfs.f2fs'
case FilesystemType.Ntfs: case FilesystemType.Ntfs:
options += ['-f', '-Q'] # Skip zeroing and bad sector check
command += 'mkfs.ntfs' options.append('--fast')
case FilesystemType.Reiserfs: case FilesystemType.Reiserfs:
command += 'mkfs.reiserfs' pass
case _: case _:
raise UnknownFilesystemFormat(f'Filetype "{fs_type.value}" is not supported') raise UnknownFilesystemFormat(f'Filetype "{fs_type.value}" is not supported')
options += additional_parted_options cmd = [f'mkfs.{mkfs_type}', *options, *additional_parted_options, str(path)]
options_str = ' '.join(options)
debug(f'Formatting filesystem: /usr/bin/{command} {options_str} {path}') debug('Formatting filesystem:', ' '.join(cmd))
try: try:
SysCommand(f"/usr/bin/{command} {options_str} {path}") SysCommand(cmd)
except SysCallError as err: except SysCallError as err:
msg = f'Could not format {path} with {fs_type.value}: {err.message}' msg = f'Could not format {path} with {fs_type.value}: {err.message}'
error(msg) error(msg)