source: client/engine/File.lib @ 71d382b

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 71d382b was 95e48e1, checked in by ramon <ramongomez@…>, 15 years ago

Funciones para calcular y tratar checksum de ficheros.

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

  • Property mode set to 100755
File size: 13.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.
[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" \
30           "$FUNCNAME REPO ubuntu.img   ==>   ef899299caf8b517ce36f1157a93d8bf"
31    return
32fi
33
34# Comprobar que existe el fichero y devolver sus datos.
35FILE=$(ogGetPath "$@")
36[ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
37md5sum "$FILE" 2>&1 | cut -f1-2 -d" "
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" \
56           "if $FUNCNAME REPO ubuntu.img CACHE ubuntu.img; then ... fi"
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
[55ad138c]92
[e42f34e]93ARGS="$@"
[c40a6b4]94case "$1" in
[1e7eaab]95    /*)     # Camino completo.          */ (Comentrio Doxygen)
[e42f34e]96        SOURCE=$(ogGetPath "$1")
97        shift ;;
98    [1-9]*) # ndisco npartición.
99        SOURCE=$(ogGetPath "$1" "$2" "$3")
100        shift 3 ;;
101    *)      # Otros: repo, cache, cdrom (no se permiten caminos relativos).
102        SOURCE=$(ogGetPath "$1" "$2")
103        shift 2 ;;
[c40a6b4]104esac
[1e7eaab]105# Comprobar fichero origen y directorio destino.
[e42f34e]106[ -n "$SOURCE" ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return $?
107TARGET=$(ogGetPath "$@")
108[ -n "$TARGET" ] || ogRaiseError $OG_ERR_NOTFOUND "$@" || return $?
[1e7eaab]109# Copiar fichero.
[55ad138c]110cp -p "$SOURCE" "$TARGET"                    # (definir posible error)
[c40a6b4]111}
112
113
[4c63eb9]114#/**
[c7d9af7]115#         ogDeleteFile [ str_repo | int_ndisk int_npartition ] path_filepath
116#@brief   Metafunción que borra un fichero de un dispositivo.
117#@see     ogGetPath
[9eefff4]118#@version 0.9 - Pruebas con OpenGnSys.
[c7d9af7]119#@author  Ramon Gomez, ETSII Universidad de Sevilla
120#@date    2009-09-29
[1e7eaab]121#*/ ##
[42669ebf]122function ogDeleteFile ()
123{
[c7d9af7]124# Variables locales.
125local FILE
[1e7eaab]126# Comprobar que existe el fichero y borrarlo.
[c7d9af7]127FILE=$(ogGetPath "$@")
[a3fb8b2]128[ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
129rm -f "$FILE" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
[c7d9af7]130}
131
132
133#/**
[3543b3e]134#         ogDeleteTree [ str_repo | int_ndisk int_npartition ] path_dirpath
135#@brief   Metafunción que borra todo un subárbol de directorios de un dispositivo.
136#@see     ogGetPath
[9eefff4]137#@version 0.9 - Pruebas con OpenGnSys.
[3543b3e]138#@author  Ramon Gomez, ETSII Universidad de Sevilla
139#@date    2009-09-29
[1e7eaab]140#*/ ##
[42669ebf]141function ogDeleteTree ()
142{
[c7d9af7]143# Variables locales.
[3543b3e]144local DIR
[1e7eaab]145# Comprobar que existe el directorio y borrarlo con su contenido.
[c7d9af7]146DIR=$(ogGetPath "$@")
[a3fb8b2]147[ -n "$DIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
148rm -fr "$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
[3543b3e]149}
150
151
152#/**
[1d531f9]153#         ogGetPath [ str_repo | int_ndisk int_npartition ] path_filepath
[4c63eb9]154#@brief   Inicia el proceso de arranque de un sistema de archivos.
[42669ebf]155#@param   path_filepath   camino del fichero (independiente de mayúsculas)
156#@param   str_repo        repositorio de ficheros
157#@param   int_ndisk       nº de orden del disco
158#@param   int_npartition  nº de orden de la partición
[4c63eb9]159#@return  path_file - camino completo real del fichero.
[55ad138c]160#@note    repo = { REPO, CACHE, CDROM }
[1d531f9]161#@note    Requisitos: \c grep \c sed
[4c63eb9]162#@exception OG_ERR_FORMAT    Formato incorrecto.
[ebf06c7]163#@exception OG_ERR_NOTFOUND  Fichero o dispositivo no encontrado.
[4c63eb9]164#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
[cfeabbf]165#@warning En caso de error, sólo devuelve el código y no da mensajes.
[4c63eb9]166#@todo    Terminar de definir parámetros para acceso a repositorios.
[9eefff4]167#@version 0.9 - Pruebas con OpenGnSys.
[4c63eb9]168#@author  Ramon Gomez, ETSII Universidad de Sevilla
169#@date    2009-09-15
[1e7eaab]170#*/ ##
[42669ebf]171function ogGetPath ()
172{
[4c63eb9]173# Variables locales.
174local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR
175
[1e7eaab]176# Si se solicita, mostrar ayuda.
[4c63eb9]177if [ "$*" == "help" ]; then
[4edc0b0]178    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
[d071d9b]179           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32" \
180           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc/fstab" \
[4c63eb9]181           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32"
182    return
183fi
184
[1e7eaab]185# Procesar camino según el número de parámetros.
[4c63eb9]186case $# in
187    1)  FILE="$1" ;;
188    2)  case "$1" in
[ee4a96e]189            REPO|repo)
[39277f4]190                FILE="$OGIMG/$2" ;;
[ee4a96e]191            CACHE|cache)
[4edc0b0]192                FILE="$(ogMountCache)/$OGIMG/$2" ;;
[ee4a96e]193            CDROM|cdrom)
[3543b3e]194                FILE="$(ogMountCdrom)/$2" ;;
[39277f4]195            *)  ogRaiseError $OG_ERR_FORMAT
196                return $? ;;
[a2336d6]197        esac ;;
[ebf06c7]198    3)  FILE="$(ogMount $1 $2)/$3" ;;
199    *)  ogRaiseError $OG_ERR_FORMAT
[c40a6b4]200        return $? ;;
[4c63eb9]201esac
202
[1e7eaab]203# Eliminar caracteres \c / iniciales, finales y duplicados.
[4c63eb9]204CURRENTDIR="$PWD"
[1e7eaab]205        # /* (comentario Doxygen)
[4c63eb9]206FILE="$(echo $FILE|sed -e 's/\(\/\)*\1/\//g' -e 's/^\///' -e 's/\/$//')"
207PREVFILE=""
208FILEPATH="/"
209while [ "$FILE" != "$PREVFILE" ]; do
[1e7eaab]210    # Busca el nombre correcto en el directorio actual.
[4c63eb9]211    cd "$FILEPATH"
[1e7eaab]212    FILEPATH="${FILEPATH}/$(ls -A | grep -i -m1 "^${FILE%%/*}$")" || return $?
[4c63eb9]213    PREVFILE="$FILE"
214    FILE="${FILE#*/}"
215done
[1e7eaab]216        # (comentario Doxygen) */
217# Muestra el camino Linux, quitando el / inicial duplicado.
[4c63eb9]218[ "$FILEPATH" != "/" ] && echo ${FILEPATH#/}
219cd $CURRENTDIR
220}
221
[a2336d6]222
[23c6c40]223#/**
224#         ogGetParentPath [ str_repo | int_ndisk int_npartition ] path_filepath
225#@brief   Metafunción que devuelve el camino del directorio padre.
226#@see     ogGetPath
[9eefff4]227#@version 0.9 - Pruebas con OpenGnSys.
[23c6c40]228#@author  Ramon Gomez, ETSII Universidad de Sevilla
229#@date    2009-09-29
[1e7eaab]230#*/ ##
[42669ebf]231function ogGetParentPath ()
232{
[ebf06c7]233local PARENT
[4edc0b0]234if [ "$*" == "help" ]; then
235    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
236           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS" \
237           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc" \
238           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS"
239    return
240fi
241
[ebf06c7]242case $# in
[42669ebf]243    1)  PARENT=$(dirname "$1") ;;
[a3fb8b2]244    2)  PARENT="$1 $(dirname "/$2")" ;;
245    3)  PARENT="$1 $2 $(dirname "/$3")" ;;
[ebf06c7]246    *)  ogRaiseError $OG_ERR_FORMAT
[cfeabbf]247        return $? ;;
[ebf06c7]248esac
249ogGetPath $PARENT
250}
251
252
[23c6c40]253#/**
[9eefff4]254#         ogIsNewerFile [ str_repo | int_ndisk int_npart ] path_source [ str_repo | int_ndisk int_npart ] path_target
255#@brief   Metafunción que indica se un fichero es más nuevo que otro.
256#@see     ogGetPath
257#@return  int_code      Código de salida
258#@warning Deben existir tanto el fichero origen como el destino.
259#@version 0.9.2 - Primera versión para OpenGnSys.
260#@author  Ramon Gomez, ETSII Universidad de Sevilla
261#@date    2010-07-24
262#*/ ##
263function ogIsNewerFile ()
264{
265# Variables locales.
266local ARGS SOURCE TARGET
267# Si se solicita, mostrar ayuda.
268if [ "$*" == "help" ]; then
269    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_source [ str_repo | int_ndisk int_npartition ] path_target" \
270           "if $FUNCNAME REPO ubuntu.img CACHE ubuntu.img; then ...  fi"
271    return
272fi
273
274ARGS="$@"
275case "$1" in
276    /*)     # Camino completo.          */ (Comentrio Doxygen)
277        SOURCE=$(ogGetPath "$1")
278        shift ;;
279    [1-9]*) # ndisco npartición.
280        SOURCE=$(ogGetPath "$1" "$2" "$3")
281        shift 3 ;;
282    *)      # Otros: repo, cache, cdrom (no se permiten caminos relativos).
283        SOURCE=$(ogGetPath "$1" "$2")
284        shift 2 ;;
285esac
286# Comprobar que existen los ficheros origen y destino.
287[ -n "$SOURCE" ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return $?
288TARGET=$(ogGetPath "$@")
289[ -n "$TARGET" ] || ogRaiseError $OG_ERR_NOTFOUND "$@" || return $?
290# Devolver si el primer fichero se ha modificado después que el segundo.
291test "$SOURCE" -nt "$TARGET"
292}
293
294
295#/**
[95e48e1]296#         ogMakeChecksumFile [ str_repo | int_ndisk int_npart ] path_filepath
297#@brief   Metafunción que guarda el valor de comprobación de un fichero.
298#@see     ogCalculateChecksum
299#@warning Genera un fichero con extensión ".sum".
300#@version 0.9.2 - Primera versión para OpenGnSys.
301#@author  Ramon Gomez, ETSII Universidad de Sevilla
302#@date    2010-07-24
303#*/ ##
304function ogMakeChecksumFile ()
305{
306# Variables locales.
307local FILE DATA
308if [ "$*" == "help" ]; then
309    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
310           "$FUNCNAME REPO ubuntu.img"
311    return
312fi
313
314# Comprobar que existe el fichero y guardar su checksum.
315FILE=$(ogGetPath "$@")
316[ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
317ogCalculateChecksum "$FILE" > "$FILE.sum"
318}
319
320
321#/**
[23c6c40]322#         ogMakeDir [ str_repo | int_ndisk int_npartition ] path_dirpath
323#@brief   Metafunción que crea un subdirectorio vacío en un dispositivo.
324#@see     ogGetParentPath
[9eefff4]325#@version 0.9 - Pruebas con OpenGnSys.
[23c6c40]326#@author  Ramon Gomez, ETSII Universidad de Sevilla
327#@date    2009-09-29
[1e7eaab]328#*/ ##
[42669ebf]329function ogMakeDir ()
330{
[ebf06c7]331local PARENT DIR
332PARENT=$(ogGetParentPath "$@") || return $?
[23c6c40]333DIR="$(basename "${!#}")"
[a3fb8b2]334mkdir -p "$PARENT/$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
[ebf06c7]335}
336 
[a2336d6]337
338#/**
339#         ogNewPath [ str_repo | int_ndisk int_npartition ] path_filepath
340#@brief   Crea el directorio especificado
[42669ebf]341#@param   path_filepath   camino del fichero (independiente de mayúsculas)
342#@param   str_repo        repositorio de ficheros
343#@param   int_ndisk       nº de orden del disco
344#@param   int_npartition  nº de orden de la partición
[a2336d6]345#@return  file - camino completo real del directorio creado
346#@note    repo = { REPO, CACHE }
347#@note    Requisitos: \c grep \c sed
348#@exception OG_ERR_FORMAT    Formato incorrecto.
349#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
350#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
351#@warning Primeras pruebas.
352#@todo    Terminar de definir parámetros para acceso a repositorios.
[9eefff4]353#@version 0.1 - Primera adaptaci³n para OpenGNSys.
[a2336d6]354#@author  obtenido de ogGetPath de Ramon Gomez, ETSII Universidad de Sevilla
355#@date    2009-09-15
356#*/
[42669ebf]357function ogNewPath ()
358{
[a2336d6]359# Variables locales.
360local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR
361
362#/// Si se solicita, mostrar ayuda.
363if [ "$*" == "help" ]; then
364    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
365           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32" \
366           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc/fstab" \
367           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32"
368    return
369fi
370
371#/// Procesar camino según el número de parámetros.
372case $# in
373     # si 1 parametro directorio-fichero completo.
374    1)  FILE="$1" ;;
375    # Si 2 parametros es REPOSITORIO  DIRECTORIO-FICHERO
376    2)  case "$1" in
377            # consultando servidor repositorio base
378            REPO | $IPREPOMAN )  FILE="$OGIMG/$2" || return $OG_ERR_NOTFOUND ;;
379            # consultando particion auxiliar cache local
380            CACHE | $IP )
381                ogMountCache >> /dev/null &&    FILE="$OGCAC/$OGIMG/$2" || return $OG_ERR_NOTFOUND ;;
382            # conectando con Servidor Axuliar.
383            *)     ogRaiseError $OG_ERR_FORMAT
384                   return $? ;;
385        esac ;;
386    # Si 3 parametros DISK PARTITION DIRECTORIO-FICHERO
387    3)  FILE="$(ogMount $1 $2)/$3" || return $OG_ERR_NOTFOUND ;;
388    *)  return $OG_ERR_FORMAT ;;
389esac
390
391mkdir -p ${FILE}
392echo $FILE
393}
394
Note: See TracBrowser for help on using the repository browser.