#!/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+ #*/ #/** # ogCopyFile [ str_repo | int_ndisk int_npart ] path_source [ str_repo | int_ndisk int_npart ] path_target #@brief Metafunción para copiar un fichero de sistema OpenGNSys a un directorio OpenGNSys. #@see ogGetPath #@warning Deben existir tanto el fichero origen como el directorio destino. #@version 0.9 - Pruebas con OpenGNSys. #@author Ramon Gomez, ETSII Universidad de Sevilla #@date 2009-10-20 #*/ ## function ogCopyFile () { # Variables locales. local ARGS SOURCE TARGET ARGS="$@" case "$1" in /*) # Camino completo. */ (Comentrio 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 -p "$SOURCE" "$TARGET" # (definir posible error) } #/** # 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 () { # Variables locales. local FILE # Comprobar que existe el fichero y borrarlo. FILE=$(ogGetPath "$@") [ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $? rm -f "$FILE" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $? } #/** # 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 #*/ ## function ogDeleteTree () { # Variables locales. local DIR # Comprobar que existe el directorio y borrarlo con su contenido. DIR=$(ogGetPath "$@") [ -n "$DIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $? rm -fr "$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $? } #/** # ogGetPath [ str_repo | int_ndisk int_npartition ] path_filepath #@brief Inicia el proceso de arranque de un sistema de archivos. #@param path_filepath camino del fichero (independiente de mayúsculas) #@param str_repo repositorio de ficheros #@param int_ndisk nº de orden del disco #@param int_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 [ str_repo | int_ndisk int_npartition ] path_filepath" \ "$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="$(ogMountCache)/$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" # /* (comentario Doxygen) 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 # (comentario 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 if [ "$*" == "help" ]; then ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \ "$FUNCNAME \"/mnt/sda1/windows/system32\" ==> /mnt/sda1/WINDOWS" \ "$FUNCNAME REPO /etc/fstab ==> /opt/opengnsys/images/etc" \ "$FUNCNAME 1 1 \"/windows/system32\" ==> /mnt/sda1/WINDOWS" return fi 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 } #/** # 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 "${!#}")" mkdir -p "$PARENT/$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $? } #/** # ogNewPath [ str_repo | int_ndisk int_npartition ] path_filepath #@brief Crea el directorio especificado #@param path_filepath camino del fichero (independiente de mayúsculas) #@param str_repo repositorio de ficheros #@param int_ndisk nº de orden del disco #@param int_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 }