mirror of https://git.48k.eu/ogclient
fs: improve value parsing from command output
value extraction did not have error checking and was handled in a one-liner. The actual implementation expands the parsing logic and moves it into a function.master
parent
4109bb6ecc
commit
bcbbe26573
|
|
@ -237,11 +237,28 @@ def _reduce_ntfsresize(partdev):
|
||||||
# The first split operation leaves the wanted data at the second element of
|
# The first split operation leaves the wanted data at the second element of
|
||||||
# the split ([1]). Finally do a second split with ' ' to get the data but
|
# the split ([1]). Finally do a second split with ' ' to get the data but
|
||||||
# nothing else following it.
|
# nothing else following it.
|
||||||
#
|
def parse_ntfsresize(output_data, pattern):
|
||||||
# In addition, increase by 10%+1K the reported shrink location on which the
|
data_split = output_data.split(pattern)
|
||||||
# filesystem can be resized according to ntfsresize.
|
# If we fail to match pattern in the split then data_split will contain [output_data]
|
||||||
size = int(out_info.split('device size: ')[1].split(' ')[0])
|
if len(data_split) == 1:
|
||||||
new_size = int(int(out_info.split('resize at ')[1].split(' ')[0])*1.1+1024)
|
logging.error(f'nfsresize: failed to find: "{pattern}"')
|
||||||
|
raise ValueError(f'nfsresize: failed to find: "{pattern}"')
|
||||||
|
value_str = data_split[1].split(' ')[0]
|
||||||
|
|
||||||
|
if not value_str.isdigit() or value_str.startswith('-'):
|
||||||
|
logging.error(f'nfsresize: failed to parse numeric value at "{pattern}"')
|
||||||
|
raise ValueError(f'nfsresize: failed to parse numeric value at "{pattern}"')
|
||||||
|
return int(value_str)
|
||||||
|
|
||||||
|
try:
|
||||||
|
size = parse_ntfsresize(out_info, 'device size: ')
|
||||||
|
new_size = parse_ntfsresize(out_info, 'resize at ')
|
||||||
|
# Increase by 10%+1K the indicated reduction by which the file system
|
||||||
|
# can be resized according to ntfsresize.
|
||||||
|
new_size = int(new_size * 1.1 + 1024)
|
||||||
|
except ValueError:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
# Run ntfsresize with -n to to probe for the smallest size, this loop is
|
# Run ntfsresize with -n to to probe for the smallest size, this loop is
|
||||||
# intentional. Acumulate size until ntfsresize in dry-run mode fails, then
|
# intentional. Acumulate size until ntfsresize in dry-run mode fails, then
|
||||||
|
|
@ -262,8 +279,13 @@ def _reduce_ntfsresize(partdev):
|
||||||
if out_resize_dryrun.find('Needed relocations : ') == -1:
|
if out_resize_dryrun.find('Needed relocations : ') == -1:
|
||||||
break
|
break
|
||||||
|
|
||||||
extra_size = int(out_resize_dryrun.split('Needed relocations : ')[1].split(' ')[0])*1.1+1024
|
try:
|
||||||
new_size += int(extra_size)
|
extra_size = parse_ntfsresize(out_resize_dryrun, 'Needed relocations : ')
|
||||||
|
# Add size padding
|
||||||
|
extra_size = int(extra_size * 1.1 + 1024)
|
||||||
|
new_size += extra_size
|
||||||
|
except ValueError:
|
||||||
|
return -1
|
||||||
|
|
||||||
if new_size < size:
|
if new_size < size:
|
||||||
cmd_resize = shlex.split(f'ntfsresize -fs {new_size:.0f} {partdev}')
|
cmd_resize = shlex.split(f'ntfsresize -fs {new_size:.0f} {partdev}')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue