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