fix. Удаление файла при возврате пространства

This commit is contained in:
Iurii 2024-04-01 21:51:13 +03:00
parent 6e80f9fba2
commit ef254ecb12
1 changed files with 10 additions and 6 deletions

View File

@ -16,19 +16,23 @@ def set_utilization(fs, utilization_percent=0.6, baloon_filename='baloon', hyste
hysteresis = int(du.total * hysteresis_percent)
if hysteresis < BS: hysteresis = BS
to_be_wasted = int(((du.used+du.free) * utilization_percent) - ((du.used+du.free) - du.free))
baloon_filename = os.path.join(fs, baloon_filename)
if not quiet:
print(f'hysteresis: {hysteresis}bytes')
print(f'total: {round(du.total/ 2**30, 3)}GB, used: {round(du.used/ 2**30, 3)}GB, free: {round(du.free/ 2**30, 3)}GB usage: {round(du.used * 100 / (du.used+du.free), 3)}%')
print(f'to_be_wasted: {round(to_be_wasted/ 2**30, 3)}GB ')
if to_be_wasted < -hysteresis:
to_be_trimmed = abs(to_be_wasted)
baloon_size = os.path.getsize(os.path.join(fs, baloon_filename))
baloon_size = os.path.getsize(baloon_filename)
if baloon_size > to_be_trimmed:
os.truncate(os.path.join(fs, baloon_filename), baloon_size - to_be_trimmed)
os.truncate(baloon_filename, baloon_size - to_be_trimmed)
if not quiet: print(f'trimmed {to_be_trimmed} Bytes')
elif not quiet: print(f'can\'t trim {baloon_size} < {to_be_trimmed}Bytes')
else:
if os.path.exists(baloon_filename):
os.remove(baloon_filename)
if not quiet and to_be_trimmed > baloon_size: print(f'can\'t trim {to_be_trimmed - baloon_size} Bytes')
elif to_be_wasted > hysteresis:
with open(os.path.join(fs, baloon_filename) , 'ab') as f:
with open(baloon_filename , 'ab') as f:
last = 0
writed = to_be_wasted
while to_be_wasted > 0: