source: client/engine/File.lib @ d01525b

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 d01525b was 62d6dd5, checked in by adv <adv@…>, 7 years ago

#853 Adapting File.lib library for doxygen

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