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