refs #1415 add ogGetCacheSpace, fix a bug

code-review
Natalia Serrano 2025-01-30 17:05:21 +01:00
parent 25901173b9
commit 1c6b5ace16
2 changed files with 59 additions and 36 deletions

View File

@ -193,46 +193,51 @@ def ogGetCacheSize():
#@note El espacio disponible es el que hay entre el límite superior de la partición 3 del disco 1 y el final de dicho disco, y no puede ser superior a la mitad de dicho disco.
#*/ ##
def ogGetCacheSpace():
"""
Obtiene el espacio libre en la partición de caché en kilobytes.
:return: Espacio libre en kilobytes.
:raises RuntimeError: Si ocurre un error al obtener el espacio libre.
"""
# Si se solicita, mostrar ayuda.
if len(sys.argv) > 1 and sys.argv[1] == "help":
ogHelp("ogGetCacheSpace", "help", "ogGetCacheSpace")
# Error si no se encuentra partición de caché.
cachepart = ogFindCache()
if cachepart is None:
raise RuntimeError(MSG_NOCACHE)
if not cachepart:
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, ogGlobals.lang.MSG_NOCACHE)
return None
cachepart = cachepart.split()
# Obtener el tamaño del disco y el número de sectores.
disk = ogDiskToDev(cachepart)
try:
result = subprocess.run(["sfdisk", "-g", disk], capture_output=True, text=True, check=True)
output = result.stdout
sectors_per_cylinder = int(output.splitlines()[1].split()[1])
total_sectors = int(output.splitlines()[1].split()[0]) * sectors_per_cylinder - 1
except subprocess.CalledProcessError as e:
raise RuntimeError(f"Error al obtener el espacio libre: {e}")
disk = DiskLib.ogDiskToDev (cachepart[0])
if not disk:
return None
# Obtener el último sector de la partición 3.
try:
result = subprocess.run(["sfdisk", "-uS", "-l", disk], capture_output=True, text=True, check=True)
output = result.stdout
end_sector_part3 = int([line.split()[2] for line in output.splitlines() if cachepart + "3" in line][0])
except subprocess.CalledProcessError as e:
raise RuntimeError(f"Error al obtener el espacio libre: {e}")
sectors = 0
disk_bn = os.path.basename (disk)
with open ('/proc/partitions', 'r') as fd:
proc_parts = fd.read()
for l in proc_parts.splitlines():
items = l.split()
if len(items) < 4: continue
if items[3] == disk_bn:
sectors = 2 * int (items[2])
if not sectors: return None
# Calcular el espacio libre en kilobytes.
if end_sector_part3 > total_sectors // 2:
free_space_kb = (total_sectors - end_sector_part3) // 2
sfdisk_out = subprocess.run (['sfdisk', '-g', disk], capture_output=True, text=True).stdout
cyls = int (sfdisk_out.split()[1])
sectors = sectors/cyls * cyls - 1
## the original code has a hard dependency on the existence of a third partition
## if the disk has sda1, sda2 and sda4, the code fails.
## this is an improved version
endpart3 = 0
for trypart in [3, 2, 1]:
sfdisk_out = subprocess.run (['sfdisk', '-uS', '-l', disk], capture_output=True, text=True).stdout
for l in sfdisk_out.splitlines():
items = l.split()
if len(items) < 6: continue
if f'{disk}{trypart}' == items[0]:
endpart3 = int (items[2])
break
if endpart3: break
if not endpart3: return None
# Mostrar espacio libre en KB (1 KB = 2 sectores)
if endpart3 > sectors // 2:
return (sectors - endpart3) // 2
else:
free_space_kb = total_sectors // 4
return free_space_kb
return sectors // 4
#/**
@ -243,7 +248,12 @@ def ogGetCacheSpace():
#@warning Salidas de errores no determinada
#*/ ##
def ogMountCache():
c = ogFindCache().split()
c = ogFindCache()
if not c:
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, ogGlobals.lang.MSG_NOCACHE)
return None
c = c.split()
m = FileSystemLib.ogMountFs (c[0], c[1])
if not m:
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, ogGlobals.lang.MSG_NOCACHE)

View File

@ -0,0 +1,13 @@
#!/usr/bin/python3
import sys
import argparse
from SystemLib import ogHelp
from CacheLib import ogGetCacheSpace
ret = ogGetCacheSpace()
if ret is not None:
if ret == True: sys.exit (0)
elif ret == False: sys.exit (1)
else: print (ret)