[4c63eb9] | 1 | #!/bin/bash |
---|
| 2 | #/** |
---|
| 3 | #@file File.lib |
---|
| 4 | #@brief Librería o clase File |
---|
| 5 | #@class File |
---|
| 6 | #@brief Funciones para gestión de archivos y directorios. |
---|
| 7 | #@version 0.9 |
---|
| 8 | #@warning License: GNU GPLv3+ |
---|
| 9 | #*/ |
---|
| 10 | |
---|
| 11 | |
---|
[c40a6b4] | 12 | ##### PRUEBAS |
---|
| 13 | # ogGetPath [ str_repo | int_ndisk int_npart ] path_source [ str_repo | int_ndisk int_npart ] path_target |
---|
| 14 | function ogCopyFile () { |
---|
| 15 | local SOURCE TARGET |
---|
| 16 | case "$1" in |
---|
| 17 | /*) SOURCE=$(ogGetFile "$1") # */ (necesario Doxygen) |
---|
| 18 | shift ;; |
---|
| 19 | [1-9]*) SOURCE=$(ogGetFile "$1" "$2" "$3") |
---|
| 20 | shift 3 ;; |
---|
| 21 | *) SOURCE=$(ogGetFile "$1" "$2") |
---|
| 22 | shift 2 ;; |
---|
| 23 | esac |
---|
| 24 | |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | |
---|
[4c63eb9] | 28 | #/** |
---|
[1d531f9] | 29 | # ogGetPath [ str_repo | int_ndisk int_npartition ] path_filepath |
---|
[4c63eb9] | 30 | #@brief Inicia el proceso de arranque de un sistema de archivos. |
---|
[1d531f9] | 31 | #@arg \c filepath camino del fichero (independiente de mayúsculas) |
---|
| 32 | #@arg \c repo repositorio de ficheros |
---|
[4c63eb9] | 33 | #@arg \c ndisk nº de orden del disco |
---|
| 34 | #@arg \c npartition nº de orden de la partición |
---|
| 35 | #@return path_file - camino completo real del fichero. |
---|
[ee4a96e] | 36 | #@note repo = { REPO, CACHE | CDROM } |
---|
[1d531f9] | 37 | #@note Requisitos: \c grep \c sed |
---|
[4c63eb9] | 38 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
[ebf06c7] | 39 | #@exception OG_ERR_NOTFOUND Fichero o dispositivo no encontrado. |
---|
[4c63eb9] | 40 | #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. |
---|
[cfeabbf] | 41 | #@warning En caso de error, sólo devuelve el código y no da mensajes. |
---|
[4c63eb9] | 42 | #@todo Terminar de definir parámetros para acceso a repositorios. |
---|
[23c6c40] | 43 | #@version 0.9 - Pruebas con OpenGNSys. |
---|
[4c63eb9] | 44 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 45 | #@date 2009-09-15 |
---|
| 46 | #*/ |
---|
| 47 | function ogGetPath () { |
---|
| 48 | |
---|
| 49 | # Variables locales. |
---|
| 50 | local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR |
---|
| 51 | |
---|
| 52 | #/// Si se solicita, mostrar ayuda. |
---|
| 53 | if [ "$*" == "help" ]; then |
---|
| 54 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
[d071d9b] | 55 | "$FUNCNAME \"/mnt/sda1/windows/system32\" ==> /mnt/sda1/WINDOWS/System32" \ |
---|
| 56 | "$FUNCNAME REPO /etc/fstab ==> /opt/opengnsys/images/etc/fstab" \ |
---|
[4c63eb9] | 57 | "$FUNCNAME 1 1 \"/windows/system32\" ==> /mnt/sda1/WINDOWS/System32" |
---|
| 58 | return |
---|
| 59 | fi |
---|
| 60 | |
---|
| 61 | #/// Procesar camino según el número de parámetros. |
---|
| 62 | case $# in |
---|
| 63 | 1) FILE="$1" ;; |
---|
| 64 | 2) case "$1" in |
---|
[ee4a96e] | 65 | REPO|repo) |
---|
[39277f4] | 66 | FILE="$OGIMG/$2" ;; |
---|
[ee4a96e] | 67 | CACHE|cache) |
---|
| 68 | FILE="$OGCAC/$OGIMG/$2" ;; |
---|
| 69 | CDROM|cdrom) |
---|
| 70 | FILE="$(ogMount cdrom)/$2" ;; |
---|
[39277f4] | 71 | *) ogRaiseError $OG_ERR_FORMAT |
---|
| 72 | return $? ;; |
---|
[a2336d6] | 73 | esac ;; |
---|
[ebf06c7] | 74 | 3) FILE="$(ogMount $1 $2)/$3" ;; |
---|
| 75 | *) ogRaiseError $OG_ERR_FORMAT |
---|
[c40a6b4] | 76 | return $? ;; |
---|
[4c63eb9] | 77 | esac |
---|
| 78 | |
---|
| 79 | #/// Eliminar caracteres \c / iniciales, finales y duplicados. |
---|
| 80 | CURRENTDIR="$PWD" |
---|
| 81 | FILE="$(echo $FILE|sed -e 's/\(\/\)*\1/\//g' -e 's/^\///' -e 's/\/$//')" |
---|
| 82 | PREVFILE="" |
---|
| 83 | FILEPATH="/" |
---|
| 84 | while [ "$FILE" != "$PREVFILE" ]; do |
---|
| 85 | #/// Busca el nombre correcto en el directorio actual. |
---|
| 86 | cd "$FILEPATH" |
---|
[f8f4dfa] | 87 | FILEPATH="${FILEPATH}/$(ls -A | grep -i -m1 "^${FILE%%/*}$")" || return $? |
---|
[4c63eb9] | 88 | PREVFILE="$FILE" |
---|
| 89 | FILE="${FILE#*/}" |
---|
| 90 | done |
---|
| 91 | |
---|
| 92 | #/// Muestra el camino Linux, quitando el "/" inicial duplicado. |
---|
| 93 | [ "$FILEPATH" != "/" ] && echo ${FILEPATH#/} |
---|
| 94 | cd $CURRENTDIR |
---|
| 95 | } |
---|
| 96 | |
---|
[a2336d6] | 97 | |
---|
[23c6c40] | 98 | #/** |
---|
| 99 | # ogGetParentPath [ str_repo | int_ndisk int_npartition ] path_filepath |
---|
| 100 | #@brief Metafunción que devuelve el camino del directorio padre. |
---|
| 101 | #@see ogGetPath |
---|
| 102 | #@version 0.9 - Pruebas con OpenGNSys. |
---|
| 103 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 104 | #@date 2009-09-29 |
---|
| 105 | #*/ |
---|
[ebf06c7] | 106 | function ogGetParentPath () { |
---|
| 107 | local PARENT |
---|
| 108 | case $# in |
---|
[23c6c40] | 109 | 1) PARENT="$(dirname "$1")" ;; |
---|
| 110 | 2) PARENT="$1 $(dirname "$2")" ;; |
---|
| 111 | 3) PARENT="$1 $2 $(dirname "$3")" ;; |
---|
[ebf06c7] | 112 | *) ogRaiseError $OG_ERR_FORMAT |
---|
[cfeabbf] | 113 | return $? ;; |
---|
[ebf06c7] | 114 | esac |
---|
| 115 | ogGetPath $PARENT |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | |
---|
[23c6c40] | 119 | #/** |
---|
| 120 | # ogDeleteFile [ str_repo | int_ndisk int_npartition ] path_filepath |
---|
| 121 | #@brief Metafunción que borra un fichero de un dispositivo. |
---|
| 122 | #@see ogGetPath |
---|
| 123 | #@version 0.9 - Pruebas con OpenGNSys. |
---|
| 124 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 125 | #@date 2009-09-29 |
---|
| 126 | #*/ |
---|
[ebf06c7] | 127 | function ogDeleteFile () { |
---|
| 128 | local FILE |
---|
| 129 | FILE=$(ogGetPath "$@") || return $? |
---|
| 130 | [ -z "$FILE" ] && ogRaiseError $OG_ERR_NOTFOUND "$*" && reutrn $? |
---|
[23c6c40] | 131 | rm -f "$FILE" || ogRaiseError $OG_ERR_NOTFOUND "$*" || reutrn $? |
---|
[ebf06c7] | 132 | } |
---|
| 133 | |
---|
[23c6c40] | 134 | |
---|
| 135 | #/** |
---|
| 136 | # ogDeleteTree [ str_repo | int_ndisk int_npartition ] path_dirpath |
---|
| 137 | #@brief Metafunción que borra todo un subárbol de directorios de un dispositivo. |
---|
| 138 | #@see ogGetPath |
---|
| 139 | #@version 0.9 - Pruebas con OpenGNSys. |
---|
| 140 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 141 | #@date 2009-09-29 |
---|
| 142 | #*/ |
---|
[ebf06c7] | 143 | # Borrar subárbol de directorio. |
---|
| 144 | function ogDeleteTree () { |
---|
[23c6c40] | 145 | local DIR |
---|
[ebf06c7] | 146 | DIR=$(ogGetPath "$@") || return $? |
---|
| 147 | [ -z "$DIR" ] && ogRaiseError $OG_ERR_NOTFOUND "$*" && reutrn $? |
---|
[23c6c40] | 148 | rm -fr "$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || reutrn $? |
---|
[ebf06c7] | 149 | } |
---|
| 150 | |
---|
[23c6c40] | 151 | |
---|
| 152 | #/** |
---|
| 153 | # ogMakeDir [ str_repo | int_ndisk int_npartition ] path_dirpath |
---|
| 154 | #@brief Metafunción que crea un subdirectorio vacío en un dispositivo. |
---|
| 155 | #@see ogGetParentPath |
---|
| 156 | #@version 0.9 - Pruebas con OpenGNSys. |
---|
| 157 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 158 | #@date 2009-09-29 |
---|
| 159 | #*/ |
---|
[ebf06c7] | 160 | function ogMakeDir () { |
---|
| 161 | local PARENT DIR |
---|
| 162 | PARENT=$(ogGetParentPath "$@") || return $? |
---|
[23c6c40] | 163 | DIR="$(basename "${!#}")" |
---|
| 164 | echo mkdir -p "$PARENT/$DIR" |
---|
[ebf06c7] | 165 | mkdir -p "$PARENT/$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || reutrn $? |
---|
| 166 | } |
---|
| 167 | |
---|
[a2336d6] | 168 | |
---|
| 169 | #/** |
---|
| 170 | # ogNewPath [ str_repo | int_ndisk int_npartition ] path_filepath |
---|
| 171 | #@brief Crea el directorio especificado |
---|
| 172 | #@arg \c filepath camino del fichero (independiente de mayúsculas) |
---|
| 173 | #@arg \c repo repositorio de ficheros |
---|
| 174 | #@arg \c ndisk nº de orden del disco |
---|
| 175 | #@arg \c npartition nº de orden de la partición |
---|
| 176 | #@return file - camino completo real del directorio creado |
---|
| 177 | #@note repo = { REPO, CACHE } |
---|
| 178 | #@note Requisitos: \c grep \c sed |
---|
| 179 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
| 180 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
| 181 | #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. |
---|
| 182 | #@warning Primeras pruebas. |
---|
| 183 | #@todo Terminar de definir parámetros para acceso a repositorios. |
---|
| 184 | #@version 0.1 - Primera adaptación para OpenGNSys. |
---|
| 185 | #@author obtenido de ogGetPath de Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 186 | #@date 2009-09-15 |
---|
| 187 | #*/ |
---|
| 188 | function ogNewPath () { |
---|
| 189 | |
---|
| 190 | # Variables locales. |
---|
| 191 | local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR |
---|
| 192 | |
---|
| 193 | #/// Si se solicita, mostrar ayuda. |
---|
| 194 | if [ "$*" == "help" ]; then |
---|
| 195 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
| 196 | "$FUNCNAME \"/mnt/sda1/windows/system32\" ==> /mnt/sda1/WINDOWS/System32" \ |
---|
| 197 | "$FUNCNAME REPO /etc/fstab ==> /opt/opengnsys/images/etc/fstab" \ |
---|
| 198 | "$FUNCNAME 1 1 \"/windows/system32\" ==> /mnt/sda1/WINDOWS/System32" |
---|
| 199 | return |
---|
| 200 | fi |
---|
| 201 | |
---|
| 202 | #/// Procesar camino según el número de parámetros. |
---|
| 203 | case $# in |
---|
| 204 | # si 1 parametro directorio-fichero completo. |
---|
| 205 | 1) FILE="$1" ;; |
---|
| 206 | # Si 2 parametros es REPOSITORIO DIRECTORIO-FICHERO |
---|
| 207 | 2) case "$1" in |
---|
| 208 | # consultando servidor repositorio base |
---|
| 209 | REPO | $IPREPOMAN ) FILE="$OGIMG/$2" || return $OG_ERR_NOTFOUND ;; |
---|
| 210 | # consultando particion auxiliar cache local |
---|
| 211 | CACHE | $IP ) |
---|
| 212 | ogMountCache >> /dev/null && FILE="$OGCAC/$OGIMG/$2" || return $OG_ERR_NOTFOUND ;; |
---|
| 213 | # conectando con Servidor Axuliar. |
---|
| 214 | *) ogRaiseError $OG_ERR_FORMAT |
---|
| 215 | return $? ;; |
---|
| 216 | esac ;; |
---|
| 217 | # Si 3 parametros DISK PARTITION DIRECTORIO-FICHERO |
---|
| 218 | 3) FILE="$(ogMount $1 $2)/$3" || return $OG_ERR_NOTFOUND ;; |
---|
| 219 | *) return $OG_ERR_FORMAT ;; |
---|
| 220 | esac |
---|
| 221 | |
---|
| 222 | mkdir -p ${FILE} |
---|
| 223 | echo $FILE |
---|
| 224 | } |
---|
| 225 | |
---|