source: client/engine/File.lib @ 53fba30

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 53fba30 was f81a4deb, checked in by ramon <ramongomez@…>, 14 years ago

Versión 1.0.2: corregir errata de entrecomillado en función ogGetParentPath (modifica #446).

git-svn-id: https://opengnsys.es/svn/branches/version1.0@2349 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 14.5 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.
[9eefff4]7#@version 0.9.2
[4c63eb9]8#@warning License: GNU GPLv3+
9#*/
10
11
[55ad138c]12#/**
[95e48e1]13#         ogCalculateChecksum [ str_repo | int_ndisk int_npart ] path_filepath
14#@brief   Devuelve la suma de comprobación (checksum) de un fichero.
15#@param   path_filepath     camino del fichero (independiente de mayúsculas)
16#@param   str_repo          repositorio de ficheros
17#@param   int_ndisk         nº de orden del disco
18#@param   int_npartition    nº de orden de la partición
19#@return  hex_checksum      Checksum del fichero
20#@version 0.9.2 - Primera versión para OpenGnSys.
21#@author  Ramon Gomez, ETSII Universidad de Sevilla
22#@date    2010-07-24
23#*/ ##
24function ogCalculateChecksum ()
25{
26# Variables locales.
27local FILE
28if [ "$*" == "help" ]; then
29    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
[cbbb046]30           "$FUNCNAME REPO ubuntu.img  ==>  ef899299caf8b517ce36f1157a93d8bf"
[95e48e1]31    return
32fi
33
34# Comprobar que existe el fichero y devolver sus datos.
35FILE=$(ogGetPath "$@")
36[ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
[d3669a2]37md5sum "$FILE" 2>&1 | cut -f1 -d" "
[95e48e1]38}
39
40
41#/**
42#         ogCompareChecksumFiles [ str_repo | int_ndisk int_npart ] path_source [ str_repo | int_ndisk int_npart ] path_target
43#@brief   Metafunción que compara las sumas de comprobación almacenadas de 2 ficheros.
44#@return  bool_compare   Valor de comparación.
45#@warning No es necesario especificar la extensión ".sum".
46#@version 0.9.2 - Primera versión para OpenGnSys.
47#@author  Ramon Gomez, ETSII Universidad de Sevilla
48#@date    2010-07-24
49#*/ ##
50function ogCompareChecksumFiles ()
51{
52# Variables locales.
53local ARGS SOURCE TARGET
54if [ "$*" == "help" ]; then
55    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
[cbbb046]56           "if $FUNCNAME REPO ubuntu.img CACHE ubuntu.img; then ...;  fi"
[95e48e1]57    return
58fi
59
60ARGS="$@"
61case "$1" in
62    /*)     # Camino completo.          */ (Comentrio Doxygen)
63        SOURCE=$(ogGetPath "$1")
64        shift ;;
65    [1-9]*) # ndisco npartición.
66        SOURCE=$(ogGetPath "$1" "$2" "$3")
67        shift 3 ;;
68    *)      # Otros: repo, cache, cdrom (no se permiten caminos relativos).
69        SOURCE=$(ogGetPath "$1" "$2")
70        shift 2 ;;
71esac
72TARGET=$(ogGetPath "$@")
73
74# Comparar los ficheros de checksum.
75test "$(cat "$SOURCE.sum" 2>/dev/null)" == "$(cat "$TARGET.sum" 2>/dev/null)"
76}
77
78
79#/**
[e42f34e]80#         ogCopyFile [ str_repo | int_ndisk int_npart ] path_source [ str_repo | int_ndisk int_npart ] path_target
[9eefff4]81#@brief   Metafunción para copiar un fichero de sistema OpenGnSys a un directorio.
[55ad138c]82#@see     ogGetPath
83#@warning Deben existir tanto el fichero origen como el directorio destino.
[9eefff4]84#@version 0.9 - Pruebas con OpenGnSys.
[55ad138c]85#@author  Ramon Gomez, ETSII Universidad de Sevilla
86#@date    2009-10-20
[1e7eaab]87#*/ ##
[42669ebf]88function ogCopyFile ()
89{
[55ad138c]90# Variables locales.
[e42f34e]91local ARGS SOURCE TARGET
[cbbb046]92if [ "$*" == "help" ]; then
93    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_source [ str_repo | int_ndisk int_npartition ] path_target" \
94           "$FUNCNAME REPO newfile.txt 1 2 /tmp/newfile.txt"
95    return
96fi
[55ad138c]97
[e42f34e]98ARGS="$@"
[c40a6b4]99case "$1" in
[1e7eaab]100    /*)     # Camino completo.          */ (Comentrio Doxygen)
[3915005]101        SOURCE="$(ogGetPath "$1")"
[e42f34e]102        shift ;;
103    [1-9]*) # ndisco npartición.
[3915005]104        SOURCE="$(ogGetPath "$1" "$2" "$3")"
[e42f34e]105        shift 3 ;;
106    *)      # Otros: repo, cache, cdrom (no se permiten caminos relativos).
[3915005]107        SOURCE="$(ogGetPath "$1" "$2")"
[e42f34e]108        shift 2 ;;
[c40a6b4]109esac
[1e7eaab]110# Comprobar fichero origen y directorio destino.
[e42f34e]111[ -n "$SOURCE" ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return $?
[3915005]112TARGET="$(ogGetPath "$@")"
[e42f34e]113[ -n "$TARGET" ] || ogRaiseError $OG_ERR_NOTFOUND "$@" || return $?
[1e7eaab]114# Copiar fichero.
[01a9904]115cp -a "$SOURCE" "$TARGET"                    # (definir posible error)
[c40a6b4]116}
117
118
[4c63eb9]119#/**
[c7d9af7]120#         ogDeleteFile [ str_repo | int_ndisk int_npartition ] path_filepath
121#@brief   Metafunción que borra un fichero de un dispositivo.
122#@see     ogGetPath
[9eefff4]123#@version 0.9 - Pruebas con OpenGnSys.
[c7d9af7]124#@author  Ramon Gomez, ETSII Universidad de Sevilla
125#@date    2009-09-29
[1e7eaab]126#*/ ##
[42669ebf]127function ogDeleteFile ()
128{
[c7d9af7]129# Variables locales.
130local FILE
[cbbb046]131if [ "$*" == "help" ]; then
132    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_file" \
133           "$FUNCNAME 1 2 /tmp/newfile.txt"
134    return
135fi
136
[1e7eaab]137# Comprobar que existe el fichero y borrarlo.
[3915005]138FILE="$(ogGetPath "$@")"
[a3fb8b2]139[ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
140rm -f "$FILE" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
[c7d9af7]141}
142
143
144#/**
[3543b3e]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
[9eefff4]148#@version 0.9 - Pruebas con OpenGnSys.
[3543b3e]149#@author  Ramon Gomez, ETSII Universidad de Sevilla
150#@date    2009-09-29
[1e7eaab]151#*/ ##
[42669ebf]152function ogDeleteTree ()
153{
[c7d9af7]154# Variables locales.
[3543b3e]155local DIR
[cbbb046]156if [ "$*" == "help" ]; then
157    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_dir" \
158           "$FUNCNAME 1 2 /tmp/newdir"
159    return
160fi
161
[1e7eaab]162# Comprobar que existe el directorio y borrarlo con su contenido.
[3915005]163DIR="$(ogGetPath "$@")"
[a3fb8b2]164[ -n "$DIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
165rm -fr "$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
[3543b3e]166}
167
168
169#/**
[1d531f9]170#         ogGetPath [ str_repo | int_ndisk int_npartition ] path_filepath
[4c63eb9]171#@brief   Inicia el proceso de arranque de un sistema de archivos.
[42669ebf]172#@param   path_filepath   camino del fichero (independiente de mayúsculas)
173#@param   str_repo        repositorio de ficheros
174#@param   int_ndisk       nº de orden del disco
175#@param   int_npartition  nº de orden de la partición
[4c63eb9]176#@return  path_file - camino completo real del fichero.
[55ad138c]177#@note    repo = { REPO, CACHE, CDROM }
[1d531f9]178#@note    Requisitos: \c grep \c sed
[4c63eb9]179#@exception OG_ERR_FORMAT    Formato incorrecto.
[ebf06c7]180#@exception OG_ERR_NOTFOUND  Fichero o dispositivo no encontrado.
[4c63eb9]181#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
[cfeabbf]182#@warning En caso de error, sólo devuelve el código y no da mensajes.
[4c63eb9]183#@todo    Terminar de definir parámetros para acceso a repositorios.
[985bef0]184#@version 0.1 -  Integracion para Opengnsys  -  HIDRA: CaminoWindows.sh; EAC: GetPath(), FormatSintaxSpacePath(), FormatSintaxBackSlashPath (),  en FileSystem.lib
185#@author Ramon Gomez, ETSII Universidad de Sevilla
186#@Date    2008/10/10
187#@author  Antonio J. Doblas Viso. Universidad de Malaga
188#@date    2008/10/27
[9eefff4]189#@version 0.9 - Pruebas con OpenGnSys.
[4c63eb9]190#@author  Ramon Gomez, ETSII Universidad de Sevilla
191#@date    2009-09-15
[1e7eaab]192#*/ ##
[42669ebf]193function ogGetPath ()
194{
[4c63eb9]195# Variables locales.
196local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR
197
[1e7eaab]198# Si se solicita, mostrar ayuda.
[4c63eb9]199if [ "$*" == "help" ]; then
[4edc0b0]200    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
[d071d9b]201           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32" \
202           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc/fstab" \
[4c63eb9]203           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32"
204    return
205fi
206
[1e7eaab]207# Procesar camino según el número de parámetros.
[4c63eb9]208case $# in
209    1)  FILE="$1" ;;
210    2)  case "$1" in
[ee4a96e]211            REPO|repo)
[39277f4]212                FILE="$OGIMG/$2" ;;
[ee4a96e]213            CACHE|cache)
[4edc0b0]214                FILE="$(ogMountCache)/$OGIMG/$2" ;;
[ee4a96e]215            CDROM|cdrom)
[3543b3e]216                FILE="$(ogMountCdrom)/$2" ;;
[39277f4]217            *)  ogRaiseError $OG_ERR_FORMAT
218                return $? ;;
[a2336d6]219        esac ;;
[ebf06c7]220    3)  FILE="$(ogMount $1 $2)/$3" ;;
221    *)  ogRaiseError $OG_ERR_FORMAT
[c40a6b4]222        return $? ;;
[4c63eb9]223esac
224
[1e7eaab]225# Eliminar caracteres \c / iniciales, finales y duplicados.
[4c63eb9]226CURRENTDIR="$PWD"
[1e7eaab]227        # /* (comentario Doxygen)
[4c63eb9]228FILE="$(echo $FILE|sed -e 's/\(\/\)*\1/\//g' -e 's/^\///' -e 's/\/$//')"
229PREVFILE=""
230FILEPATH="/"
231while [ "$FILE" != "$PREVFILE" ]; do
[1e7eaab]232    # Busca el nombre correcto en el directorio actual.
[4c63eb9]233    cd "$FILEPATH"
[1e7eaab]234    FILEPATH="${FILEPATH}/$(ls -A | grep -i -m1 "^${FILE%%/*}$")" || return $?
[4c63eb9]235    PREVFILE="$FILE"
236    FILE="${FILE#*/}"
237done
[1e7eaab]238        # (comentario Doxygen) */
239# Muestra el camino Linux, quitando el / inicial duplicado.
[4c63eb9]240[ "$FILEPATH" != "/" ] && echo ${FILEPATH#/}
241cd $CURRENTDIR
242}
243
[a2336d6]244
[23c6c40]245#/**
246#         ogGetParentPath [ str_repo | int_ndisk int_npartition ] path_filepath
247#@brief   Metafunción que devuelve el camino del directorio padre.
248#@see     ogGetPath
[9eefff4]249#@version 0.9 - Pruebas con OpenGnSys.
[23c6c40]250#@author  Ramon Gomez, ETSII Universidad de Sevilla
251#@date    2009-09-29
[1e7eaab]252#*/ ##
[42669ebf]253function ogGetParentPath ()
254{
[ebf06c7]255local PARENT
[4edc0b0]256if [ "$*" == "help" ]; then
257    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
258           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS" \
259           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc" \
260           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS"
261    return
262fi
263
[ebf06c7]264case $# in
[3915005]265    1)  PARENT="$(dirname "$1")" ;;
[a3fb8b2]266    2)  PARENT="$1 $(dirname "/$2")" ;;
267    3)  PARENT="$1 $2 $(dirname "/$3")" ;;
[ebf06c7]268    *)  ogRaiseError $OG_ERR_FORMAT
[cfeabbf]269        return $? ;;
[ebf06c7]270esac
[f81a4deb]271ogGetPath $PARENT
[ebf06c7]272}
273
274
[23c6c40]275#/**
[9eefff4]276#         ogIsNewerFile [ str_repo | int_ndisk int_npart ] path_source [ str_repo | int_ndisk int_npart ] path_target
277#@brief   Metafunción que indica se un fichero es más nuevo que otro.
278#@see     ogGetPath
[7685100]279#@return  Código de salida: 0 - nuevo, 1 - antiguo o error
[9eefff4]280#@warning Deben existir tanto el fichero origen como el destino.
281#@version 0.9.2 - Primera versión para OpenGnSys.
282#@author  Ramon Gomez, ETSII Universidad de Sevilla
283#@date    2010-07-24
[7685100]284#@version 1.0.1 - Devolver falso en caso de error.
285#@author  Ramon Gomez, ETSII Universidad de Sevilla
286#@date    2011-05-18
[9eefff4]287#*/ ##
288function ogIsNewerFile ()
289{
290# Variables locales.
291local ARGS SOURCE TARGET
292# Si se solicita, mostrar ayuda.
293if [ "$*" == "help" ]; then
294    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_source [ str_repo | int_ndisk int_npartition ] path_target" \
295           "if $FUNCNAME REPO ubuntu.img CACHE ubuntu.img; then ...  fi"
296    return
297fi
298
299ARGS="$@"
300case "$1" in
301    /*)     # Camino completo.          */ (Comentrio Doxygen)
[3915005]302        SOURCE="$(ogGetPath "$1")"
[9eefff4]303        shift ;;
304    [1-9]*) # ndisco npartición.
[3915005]305        SOURCE="$(ogGetPath "$1" "$2" "$3")"
[9eefff4]306        shift 3 ;;
307    *)      # Otros: repo, cache, cdrom (no se permiten caminos relativos).
[3915005]308        SOURCE="$(ogGetPath "$1" "$2")"
[9eefff4]309        shift 2 ;;
310esac
311# Comprobar que existen los ficheros origen y destino.
[7685100]312[ -n "$SOURCE" ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return 1
[9eefff4]313TARGET=$(ogGetPath "$@")
[7685100]314[ -n "$TARGET" ] || ogRaiseError $OG_ERR_NOTFOUND "$@" || return 1
[9eefff4]315# Devolver si el primer fichero se ha modificado después que el segundo.
316test "$SOURCE" -nt "$TARGET"
317}
318
319
320#/**
[95e48e1]321#         ogMakeChecksumFile [ str_repo | int_ndisk int_npart ] path_filepath
322#@brief   Metafunción que guarda el valor de comprobación de un fichero.
323#@see     ogCalculateChecksum
324#@warning Genera un fichero con extensión ".sum".
325#@version 0.9.2 - Primera versión para OpenGnSys.
326#@author  Ramon Gomez, ETSII Universidad de Sevilla
327#@date    2010-07-24
328#*/ ##
329function ogMakeChecksumFile ()
330{
331# Variables locales.
[3915005]332local FILE
[95e48e1]333if [ "$*" == "help" ]; then
334    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
335           "$FUNCNAME REPO ubuntu.img"
336    return
337fi
338
339# Comprobar que existe el fichero y guardar su checksum.
[3915005]340FILE="$(ogGetPath "$@")"
[95e48e1]341[ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
342ogCalculateChecksum "$FILE" > "$FILE.sum"
343}
344
345
346#/**
[23c6c40]347#         ogMakeDir [ str_repo | int_ndisk int_npartition ] path_dirpath
348#@brief   Metafunción que crea un subdirectorio vacío en un dispositivo.
349#@see     ogGetParentPath
[985bef0]350#@version 0.1 -  Integracion para Opengnsys  -   HIDRA: CrearDirectorio.sh, EAC: MkdirPath() en FileSystem.lib
351#@author Ramon Gomez, ETSII Universidad de Sevilla
352#@Date    2008/10/10
353#@author  Antonio J. Doblas Viso. Universidad de Malaga
354#@date    2008/10/27
[9eefff4]355#@version 0.9 - Pruebas con OpenGnSys.
[23c6c40]356#@author  Ramon Gomez, ETSII Universidad de Sevilla
357#@date    2009-09-29
[1e7eaab]358#*/ ##
[42669ebf]359function ogMakeDir ()
360{
[ebf06c7]361local PARENT DIR
[cbbb046]362if [ "$*" == "help" ]; then
363    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_dir" \
364           "$FUNCNAME 1 2 /tmp/newdir"
365    return
366fi
367
[3915005]368PARENT="$(ogGetParentPath "$@")" || return $?
[23c6c40]369DIR="$(basename "${!#}")"
[a3fb8b2]370mkdir -p "$PARENT/$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
[ebf06c7]371}
372 
[a2336d6]373
374#/**
375#         ogNewPath [ str_repo | int_ndisk int_npartition ] path_filepath
376#@brief   Crea el directorio especificado
[42669ebf]377#@param   path_filepath   camino del fichero (independiente de mayúsculas)
378#@param   str_repo        repositorio de ficheros
379#@param   int_ndisk       nº de orden del disco
380#@param   int_npartition  nº de orden de la partición
[a2336d6]381#@return  file - camino completo real del directorio creado
382#@note    repo = { REPO, CACHE }
383#@note    Requisitos: \c grep \c sed
384#@exception OG_ERR_FORMAT    Formato incorrecto.
385#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
386#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
387#@warning Primeras pruebas.
388#@todo    Terminar de definir parámetros para acceso a repositorios.
[9eefff4]389#@version 0.1 - Primera adaptaci³n para OpenGNSys.
[a2336d6]390#@author  obtenido de ogGetPath de Ramon Gomez, ETSII Universidad de Sevilla
391#@date    2009-09-15
392#*/
[42669ebf]393function ogNewPath ()
394{
[a2336d6]395# Variables locales.
396local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR
397
398#/// Si se solicita, mostrar ayuda.
399if [ "$*" == "help" ]; then
400    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
401           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32" \
402           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc/fstab" \
403           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32"
404    return
405fi
406
407#/// Procesar camino según el número de parámetros.
408case $# in
409     # si 1 parametro directorio-fichero completo.
410    1)  FILE="$1" ;;
411    # Si 2 parametros es REPOSITORIO  DIRECTORIO-FICHERO
412    2)  case "$1" in
413            # consultando servidor repositorio base
414            REPO | $IPREPOMAN )  FILE="$OGIMG/$2" || return $OG_ERR_NOTFOUND ;;
415            # consultando particion auxiliar cache local
416            CACHE | $IP )
417                ogMountCache >> /dev/null &&    FILE="$OGCAC/$OGIMG/$2" || return $OG_ERR_NOTFOUND ;;
418            # conectando con Servidor Axuliar.
419            *)     ogRaiseError $OG_ERR_FORMAT
420                   return $? ;;
421        esac ;;
422    # Si 3 parametros DISK PARTITION DIRECTORIO-FICHERO
423    3)  FILE="$(ogMount $1 $2)/$3" || return $OG_ERR_NOTFOUND ;;
424    *)  return $OG_ERR_FORMAT ;;
425esac
426
427mkdir -p ${FILE}
428echo $FILE
429}
430
Note: See TracBrowser for help on using the repository browser.