source: client/engine/File.lib @ 12e8e382

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 12e8e382 was 12e8e382, checked in by ramon <ramongomez@…>, 13 years ago

Versión 1.0.4, #489: Integrar ticket:489 en rama de desarrollo.

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

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