source: client/engine/File.lib @ 3fef8e1

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since 3fef8e1 was a3fb8b2, checked in by ramon <ramongomez@…>, 15 years ago

Solucionado problema con función ogGetParentPath, errata en instalador y script createImage para Consola Web.

git-svn-id: https://opengnsys.es/svn/trunk@582 a21b9725-9963-47de-94b9-378ad31fedc9

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