source: client/engine/File.lib @ 499a7ec

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 499a7ec was d0d4a3d, checked in by ramon <ramongomez@…>, 11 years ago

#616: Limpiar código eliminando antigua función ogNewPath,

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

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