diff --git a/client/lib/engine/bin/FileSystemLib.py b/client/lib/engine/bin/FileSystemLib.py index 9120a5e..8eb19e1 100755 --- a/client/lib/engine/bin/FileSystemLib.py +++ b/client/lib/engine/bin/FileSystemLib.py @@ -30,116 +30,62 @@ import FileSystemLib #@warning No se comprueban sistemas de archivos montados o bloqueados. #@todo Definir salidas. #*/ ## -def ogCheckFs(int_ndisk, int_nfilesys): +def ogCheckFs (disk, par): + PART = DiskLib.ogDiskToDev (disk, par) + if not PART: return - if len(sys.argv) != 3: - SystemLib.ogRaiseError( - "session", - ogGlobals.OG_ERR_FORMAT, - "Not enough arguments" - ) - return - - # Obtener partición. - PART = DiskLib.ogDiskToDev(int_ndisk, int_nfilesys) - if not PART: - return - - TYPE = ogGetFsType(int_ndisk, int_nfilesys) - if TYPE == "EXT[234]" or TYPE == "CACHE": - PROG = "e2fsck" - PARAMS = "-y" - CODES = [1, 2] - elif TYPE == "BTRFS": - PROG = "btrfsck" - CODES = [1] - elif TYPE == "REISERFS": - PROG = "fsck.reiserfs" - PARAMS = "<<<\"Yes\"" - CODES = [1, 2] - elif TYPE == "REISER4": - PROG = "fsck.reiser4" - PARAMS = "-ay" - elif TYPE == "JFS": - PROG = "fsck.jfs" - CODES = [1, 2] - elif TYPE == "XFS": - PROG = "xfs_repair" - elif TYPE == "F2FS": - PROG = "fsck.f2fs" - elif TYPE == "NTFS": - PROG = "ntfsfix" - elif TYPE == "EXFAT": - PROG = "fsck.exfat" - elif TYPE == "FAT32": - PROG = "dosfsck" - PARAMS = "-a" - CODES = [1] - elif TYPE == "FAT16": - PROG = "dosfsck" - PARAMS = "-a" - CODES = [1] - elif TYPE == "FAT12": - PROG = "dosfsck" - PARAMS = "-a" - CODES = [1] - elif TYPE == "HFS": - PROG = "fsck.hfs" - PARAMS = "-f" - elif TYPE == "HFSPLUS": - PROG = "fsck.hfs" - PARAMS = "-f" - elif TYPE == "UFS": - PROG = "fsck.ufs" - elif TYPE == "ZFS": - PROG = "fsck.zfs" + data = { + 'EXT2': { 'prog': 'e2fsck', 'params': '-y', 'codes': (1, 2), }, + 'EXT3': { 'prog': 'e2fsck', 'params': '-y', 'codes': (1, 2), }, + 'EXT4': { 'prog': 'e2fsck', 'params': '-y', 'codes': (1, 2), }, + 'CACHE': { 'prog': 'e2fsck', 'params': '-y', 'codes': (1, 2), }, + 'BTRFS': { 'prog': 'btrfsck', 'codes': (1), }, + 'REISERFS': { 'prog': 'fsck.reiserfs', 'codes': (1, 2), 'input': 'Yes' }, + 'REISER4': { 'prog': 'fsck.reiser4', 'params': '-ay', }, + 'JFS': { 'prog': 'fsck.jfs', 'codes': (1, 2), }, + 'XFS': { 'prog': 'xfs_repair', }, + 'F2FS': { 'prog': 'fsck.f2fs', }, + 'NTFS': { 'prog': 'ntfsfix', }, + 'EXFAT': { 'prog': 'fsck.exfat', }, + 'FAT32': { 'prog': 'dosfsck', 'params': '-a', 'codes': (1), }, + 'FAT16': { 'prog': 'dosfsck', 'params': '-a', 'codes': (1), }, + 'FAT12': { 'prog': 'dosfsck', 'params': '-a', 'codes': (1), }, + 'HFS': { 'prog': 'fsck.hfs', 'params': '-f', }, + 'HFSPLUS': { 'prog': 'fsck.hfs', 'params': '-f', }, + 'UFS': { 'prog': 'fsck.ufs', }, + 'ZFS': { 'prog': 'fsck.zfs', }, + } + type = ogGetFsType (disk, par) + if type in data: + prog = data[type]['prog'] + params = data[type]['params'] if 'params' in data[type] else '' + codes = data[type]['codes'] if 'codes' in data[type] else [] + input = data[type]['input'] if 'input' in data[type] else None else: - SystemLib.ogRaiseError( - "session", - ogGlobals.OG_ERR_PARTITION, - f"{int_ndisk}, {int_nfilesys}" - ) - return + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, f'{disk}, {par}, {type}') + return None - # Error si el sistema de archivos esta montado o bloqueado. - ogUnmount(int_ndisk, int_nfilesys) - if ogIsMounted(int_ndisk, int_nfilesys): - SystemLib.ogRaiseError( - "session", - ogGlobals.OG_ERR_PARTITION, - f"{int_ndisk} {int_nfilesys}" - ) - return - if ogIsLocked(int_ndisk, int_nfilesys): - SystemLib.ogRaiseError( - "session", - ogGlobals.OG_ERR_LOCKED, - f"{int_ndisk} {int_nfilesys}" - ) - return + ogUnmount (disk, par) + if ogIsMounted (disk, par): + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, f'{disk} {par}') + return None + if ogIsLocked (disk, par): + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_LOCKED, f'{disk} {par}') + return None - # Comprobar en modo uso exclusivo. - ogLock(int_ndisk, int_nfilesys) - try: - result = subprocess.run([PROG, PARAMS, PART], capture_output=True) - ERRCODE = result.returncode - except FileNotFoundError: - SystemLib.ogRaiseError( - "session", - ogGlobals.OG_ERR_NOTEXEC, - PROG - ) - ERRCODE = ogGlobals.OG_ERR_NOTEXEC - except: - SystemLib.ogRaiseError( - "session", - ogGlobals.OG_ERR_PARTITION, - f"{int_ndisk} {int_nfilesys}" - ) - ERRCODE = ogGlobals.OG_ERR_PARTITION + ogLock (disk, par) + rc = subprocess.run ([prog] + params.split() + [PART], input=input, text=True).returncode + if 0 == rc or rc in codes: + errcode = 0 + elif 127 == rc: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTEXEC, prog) + errcode = ogGlobals.OG_ERR_NOTEXEC + else: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, f'{disk} {par}') + errcode = ogGlobals.OG_ERR_PARTITION - ogUnlock(int_ndisk, int_nfilesys) - return ERRCODE + ogUnlock (disk, par) + return errcode #/** diff --git a/client/shared/functions/ogCheckFs b/client/shared/functions/ogCheckFs new file mode 100755 index 0000000..7eade6a --- /dev/null +++ b/client/shared/functions/ogCheckFs @@ -0,0 +1,23 @@ +#!/usr/bin/python3 + +import sys +import argparse +from SystemLib import ogHelp +from FileSystemLib import ogCheckFs + +parser = argparse.ArgumentParser (add_help=False) +parser.add_argument ('disk') +parser.add_argument ('par') + +if 2 == len (sys.argv) and 'help' == sys.argv[1]: + #parser.print_help() sale en inglés aunque la locale indique otra cosa + ogHelp ('ogCheckFs', 'ogCheckFs int_ndisk int_nfilesys', ['ogCheckFs 1 1']) + sys.exit (0) + +args = parser.parse_args() +ret = ogCheckFs (args.disk, args.par) + +if ret is not None: + if ret == True: sys.exit (0) + elif ret == False: sys.exit (1) + else: print (ret)