opengnsys/client/engine/File.lib

235 lines
7.7 KiB
Bash

#!/bin/bash
#/**
#@file File.lib
#@brief Librería o clase File
#@class File
#@brief Funciones para gestión de archivos y directorios.
#@version 0.9
#@warning License: GNU GPLv3+
#*/
##### PRUEBAS
# ogCopyFile [ str_repo | int_ndisk int_npart ] path_source [ str_repo | int_ndisk int_npart ] path_target
function ogCopyFile () {
local ARGS SOURCE TARGET
ARGS="$@"
case "$1" in
/*) # Camino completo. */ (necesario Doxygen)
SOURCE=$(ogGetPath "$1")
shift ;;
[1-9]*) # ndisco npartición.
SOURCE=$(ogGetPath "$1" "$2" "$3")
shift 3 ;;
*) # Otros: repo, cache, cdrom (no se permiten caminos relativos).
SOURCE=$(ogGetPath "$1" "$2")
shift 2 ;;
esac
#/// Comprobar fichero origen y directorio destino.
[ -n "$SOURCE" ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return $?
TARGET=$(ogGetPath "$@")
[ -n "$TARGET" ] || ogRaiseError $OG_ERR_NOTFOUND "$@" || return $?
#/// Copiar fichero.
cp "$SOURCE" "$TARGET" # (definir posible error)
}
#/**
# ogDeleteTree [ str_repo | int_ndisk int_npartition ] path_dirpath
#@brief Metafunción que borra todo un subárbol de directorios de un dispositivo.
#@see ogGetPath
#@version 0.9 - Pruebas con OpenGNSys.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2009-09-29
#*/
# Borrar subárbol de directorio.
function ogDeleteTree () {
local DIR
DIR=$(ogGetPath "$@") || return $?
[ -z "$DIR" ] && ogRaiseError $OG_ERR_NOTFOUND "$*" && reutrn $?
rm -fr "$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || reutrn $?
}
#/**
# ogGetPath [ str_repo | int_ndisk int_npartition ] path_filepath
#@brief Inicia el proceso de arranque de un sistema de archivos.
#@arg \c filepath camino del fichero (independiente de mayúsculas)
#@arg \c repo repositorio de ficheros
#@arg \c ndisk nº de orden del disco
#@arg \c npartition nº de orden de la partición
#@return path_file - camino completo real del fichero.
#@note repo = { REPO, CACHE | CDROM }
#@note Requisitos: \c grep \c sed
#@exception OG_ERR_FORMAT Formato incorrecto.
#@exception OG_ERR_NOTFOUND Fichero o dispositivo no encontrado.
#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
#@warning En caso de error, sólo devuelve el código y no da mensajes.
#@todo Terminar de definir parámetros para acceso a repositorios.
#@version 0.9 - Pruebas con OpenGNSys.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2009-09-15
#*/
function ogGetPath () {
# Variables locales.
local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR
#/// Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
"$FUNCNAME \"/mnt/sda1/windows/system32\" ==> /mnt/sda1/WINDOWS/System32" \
"$FUNCNAME REPO /etc/fstab ==> /opt/opengnsys/images/etc/fstab" \
"$FUNCNAME 1 1 \"/windows/system32\" ==> /mnt/sda1/WINDOWS/System32"
return
fi
#/// Procesar camino según el número de parámetros.
case $# in
1) FILE="$1" ;;
2) case "$1" in
REPO|repo)
FILE="$OGIMG/$2" ;;
CACHE|cache)
FILE="$OGCAC/$OGIMG/$2" ;;
CDROM|cdrom)
FILE="$(ogMountCdrom)/$2" ;;
*) ogRaiseError $OG_ERR_FORMAT
return $? ;;
esac ;;
3) FILE="$(ogMount $1 $2)/$3" ;;
*) ogRaiseError $OG_ERR_FORMAT
return $? ;;
esac
#/// Eliminar caracteres \c / iniciales, finales y duplicados.
CURRENTDIR="$PWD"
FILE="$(echo $FILE|sed -e 's/\(\/\)*\1/\//g' -e 's/^\///' -e 's/\/$//')"
PREVFILE=""
FILEPATH="/"
while [ "$FILE" != "$PREVFILE" ]; do
#/// Busca el nombre correcto en el directorio actual.
cd "$FILEPATH"
FILEPATH="${FILEPATH}/$(ls -A | grep -i -m1 "^${FILE%%/*}$")" || return $?
PREVFILE="$FILE"
FILE="${FILE#*/}"
done
# */ (necesario Doxygen)
#/// Muestra el camino Linux, quitando el "/" inicial duplicado.
[ "$FILEPATH" != "/" ] && echo ${FILEPATH#/}
cd $CURRENTDIR
}
#/**
# ogGetParentPath [ str_repo | int_ndisk int_npartition ] path_filepath
#@brief Metafunción que devuelve el camino del directorio padre.
#@see ogGetPath
#@version 0.9 - Pruebas con OpenGNSys.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2009-09-29
#*/
function ogGetParentPath () {
local PARENT
case $# in
1) PARENT="$(dirname "$1")" ;;
2) PARENT="$1 $(dirname "$2")" ;;
3) PARENT="$1 $2 $(dirname "$3")" ;;
*) ogRaiseError $OG_ERR_FORMAT
return $? ;;
esac
ogGetPath $PARENT
}
#/**
# ogDeleteFile [ str_repo | int_ndisk int_npartition ] path_filepath
#@brief Metafunción que borra un fichero de un dispositivo.
#@see ogGetPath
#@version 0.9 - Pruebas con OpenGNSys.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2009-09-29
#*/
function ogDeleteFile () {
local FILE
FILE=$(ogGetPath "$@") || return $?
[ -z "$FILE" ] && ogRaiseError $OG_ERR_NOTFOUND "$*" && reutrn $?
rm -f "$FILE" || ogRaiseError $OG_ERR_NOTFOUND "$*" || reutrn $?
}
#/**
# ogMakeDir [ str_repo | int_ndisk int_npartition ] path_dirpath
#@brief Metafunción que crea un subdirectorio vacío en un dispositivo.
#@see ogGetParentPath
#@version 0.9 - Pruebas con OpenGNSys.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2009-09-29
#*/
function ogMakeDir () {
local PARENT DIR
PARENT=$(ogGetParentPath "$@") || return $?
DIR="$(basename "${!#}")"
echo mkdir -p "$PARENT/$DIR"
mkdir -p "$PARENT/$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || reutrn $?
}
#/**
# ogNewPath [ str_repo | int_ndisk int_npartition ] path_filepath
#@brief Crea el directorio especificado
#@arg \c filepath camino del fichero (independiente de mayúsculas)
#@arg \c repo repositorio de ficheros
#@arg \c ndisk nº de orden del disco
#@arg \c npartition nº de orden de la partición
#@return file - camino completo real del directorio creado
#@note repo = { REPO, CACHE }
#@note Requisitos: \c grep \c sed
#@exception OG_ERR_FORMAT Formato incorrecto.
#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
#@warning Primeras pruebas.
#@todo Terminar de definir parámetros para acceso a repositorios.
#@version 0.1 - Primera adaptación para OpenGNSys.
#@author obtenido de ogGetPath de Ramon Gomez, ETSII Universidad de Sevilla
#@date 2009-09-15
#*/
function ogNewPath () {
# Variables locales.
local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR
#/// Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
"$FUNCNAME \"/mnt/sda1/windows/system32\" ==> /mnt/sda1/WINDOWS/System32" \
"$FUNCNAME REPO /etc/fstab ==> /opt/opengnsys/images/etc/fstab" \
"$FUNCNAME 1 1 \"/windows/system32\" ==> /mnt/sda1/WINDOWS/System32"
return
fi
#/// Procesar camino según el número de parámetros.
case $# in
# si 1 parametro directorio-fichero completo.
1) FILE="$1" ;;
# Si 2 parametros es REPOSITORIO DIRECTORIO-FICHERO
2) case "$1" in
# consultando servidor repositorio base
REPO | $IPREPOMAN ) FILE="$OGIMG/$2" || return $OG_ERR_NOTFOUND ;;
# consultando particion auxiliar cache local
CACHE | $IP )
ogMountCache >> /dev/null && FILE="$OGCAC/$OGIMG/$2" || return $OG_ERR_NOTFOUND ;;
# conectando con Servidor Axuliar.
*) ogRaiseError $OG_ERR_FORMAT
return $? ;;
esac ;;
# Si 3 parametros DISK PARTITION DIRECTORIO-FICHERO
3) FILE="$(ogMount $1 $2)/$3" || return $OG_ERR_NOTFOUND ;;
*) return $OG_ERR_FORMAT ;;
esac
mkdir -p ${FILE}
echo $FILE
}