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