Use regex matching instead of shell-style matching for ddusb
This commit is contained in:
parent
366601d014
commit
a671d8d1a9
@ -1,4 +1,3 @@
|
|||||||
# File for excluding block devices from being written to
|
# File for excluding block devices from being written to
|
||||||
# Write one device per line, shell-style globs are accepted i.e. /dev/sda*
|
# One rule per line, regular expressions are accepted i.e. /dev/sda[0-9]?
|
||||||
# Brace expansion is not supported
|
|
||||||
# Lines beginning with "#" and ";" are ignored
|
# Lines beginning with "#" and ";" are ignored
|
||||||
|
38
ddusb.py
38
ddusb.py
@ -2,7 +2,6 @@
|
|||||||
"""Wrapper script for using dd to write to a USB drive."""
|
"""Wrapper script for using dd to write to a USB drive."""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import glob
|
|
||||||
import pathlib
|
import pathlib
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
@ -33,23 +32,6 @@ def read_exclude_file(exclude_file):
|
|||||||
return lines
|
return lines
|
||||||
|
|
||||||
|
|
||||||
def expand_globs(*globs):
|
|
||||||
"""For each glob given, expand these globs and return a list
|
|
||||||
containing each glob expanded.
|
|
||||||
|
|
||||||
:param globs: patterns to expand
|
|
||||||
:type globs: str
|
|
||||||
:returns: all expanded globs aggregated together
|
|
||||||
:rtype: list
|
|
||||||
"""
|
|
||||||
expanded_globs = []
|
|
||||||
|
|
||||||
for line in globs:
|
|
||||||
expanded_globs.extend(glob.glob(line, recursive=True))
|
|
||||||
|
|
||||||
return expanded_globs
|
|
||||||
|
|
||||||
|
|
||||||
# ========== Main Script ==========
|
# ========== Main Script ==========
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("-b", "--bs", default=512, help="block size", metavar="bs")
|
parser.add_argument("-b", "--bs", default=512, help="block size", metavar="bs")
|
||||||
@ -59,23 +41,23 @@ args = parser.parse_args()
|
|||||||
|
|
||||||
block_size = args.bs
|
block_size = args.bs
|
||||||
input_file = args.input_file
|
input_file = args.input_file
|
||||||
block_device = args.output_file
|
block_path = args.output_file
|
||||||
|
|
||||||
# Ensure that block_device is really a block device
|
# Ensure that block_path is really a block device
|
||||||
if not pathlib.Path(block_device).is_block_device():
|
if not pathlib.Path(block_path).is_block_device():
|
||||||
print(f'Error: "{block_device}" is not a block device')
|
print(f'Error: "{block_path}" is not a block device')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
# Check if block_device is excluded
|
# Check if block_path is excluded
|
||||||
exclude_patterns = read_exclude_file(EXCLUDE_FILE)
|
exclude_patterns = read_exclude_file(EXCLUDE_FILE)
|
||||||
device_blacklist = expand_globs(*exclude_patterns)
|
|
||||||
|
|
||||||
if block_device in device_blacklist:
|
for pattern in exclude_patterns:
|
||||||
print(f'Error: "{block_device}" is blacklisted from running dd')
|
if re.fullmatch(pattern, block_path):
|
||||||
|
print(f'Error: "{block_path}" is blacklisted from running dd')
|
||||||
exit(2)
|
exit(2)
|
||||||
|
|
||||||
print(f"Input file: {input_file}")
|
print(f"Input file: {input_file}")
|
||||||
print(f"Block device: {block_device}")
|
print(f"Block device: {block_path}")
|
||||||
print(f"Block size: {block_size}")
|
print(f"Block size: {block_size}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -83,7 +65,7 @@ try:
|
|||||||
[
|
[
|
||||||
"dd",
|
"dd",
|
||||||
f"if={input_file}",
|
f"if={input_file}",
|
||||||
f"of={block_device}",
|
f"of={block_path}",
|
||||||
f"bs={block_size}",
|
f"bs={block_size}",
|
||||||
"status=progress",
|
"status=progress",
|
||||||
],
|
],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user