source: client/engine/File.lib @ 562547a

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 562547a was d3669a2, checked in by ramon <ramongomez@…>, 15 years ago

Corregir erratas; {{{ogReduceFs aplica mínimo + 10%: Close #191

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

  • Property mode set to 100755
File size: 13.6 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 $?
[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" \
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.
[985bef0]167#@version 0.1 -  Integracion para Opengnsys  -  HIDRA: CaminoWindows.sh; EAC: GetPath(), FormatSintaxSpacePath(), FormatSintaxBackSlashPath (),  en FileSystem.lib
168#@author Ramon Gomez, ETSII Universidad de Sevilla
169#@Date    2008/10/10
170#@author  Antonio J. Doblas Viso. Universidad de Malaga
171#@date    2008/10/27
[9eefff4]172#@version 0.9 - Pruebas con OpenGnSys.
[4c63eb9]173#@author  Ramon Gomez, ETSII Universidad de Sevilla
174#@date    2009-09-15
[1e7eaab]175#*/ ##
[42669ebf]176function ogGetPath ()
177{
[4c63eb9]178# Variables locales.
179local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR
180
[1e7eaab]181# Si se solicita, mostrar ayuda.
[4c63eb9]182if [ "$*" == "help" ]; then
[4edc0b0]183    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
[d071d9b]184           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32" \
185           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc/fstab" \
[4c63eb9]186           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32"
187    return
188fi
189
[1e7eaab]190# Procesar camino según el número de parámetros.
[4c63eb9]191case $# in
192    1)  FILE="$1" ;;
193    2)  case "$1" in
[ee4a96e]194            REPO|repo)
[39277f4]195                FILE="$OGIMG/$2" ;;
[ee4a96e]196            CACHE|cache)
[4edc0b0]197                FILE="$(ogMountCache)/$OGIMG/$2" ;;
[ee4a96e]198            CDROM|cdrom)
[3543b3e]199                FILE="$(ogMountCdrom)/$2" ;;
[39277f4]200            *)  ogRaiseError $OG_ERR_FORMAT
201                return $? ;;
[a2336d6]202        esac ;;
[ebf06c7]203    3)  FILE="$(ogMount $1 $2)/$3" ;;
204    *)  ogRaiseError $OG_ERR_FORMAT
[c40a6b4]205        return $? ;;
[4c63eb9]206esac
207
[1e7eaab]208# Eliminar caracteres \c / iniciales, finales y duplicados.
[4c63eb9]209CURRENTDIR="$PWD"
[1e7eaab]210        # /* (comentario Doxygen)
[4c63eb9]211FILE="$(echo $FILE|sed -e 's/\(\/\)*\1/\//g' -e 's/^\///' -e 's/\/$//')"
212PREVFILE=""
213FILEPATH="/"
214while [ "$FILE" != "$PREVFILE" ]; do
[1e7eaab]215    # Busca el nombre correcto en el directorio actual.
[4c63eb9]216    cd "$FILEPATH"
[1e7eaab]217    FILEPATH="${FILEPATH}/$(ls -A | grep -i -m1 "^${FILE%%/*}$")" || return $?
[4c63eb9]218    PREVFILE="$FILE"
219    FILE="${FILE#*/}"
220done
[1e7eaab]221        # (comentario Doxygen) */
222# Muestra el camino Linux, quitando el / inicial duplicado.
[4c63eb9]223[ "$FILEPATH" != "/" ] && echo ${FILEPATH#/}
224cd $CURRENTDIR
225}
226
[a2336d6]227
[23c6c40]228#/**
229#         ogGetParentPath [ str_repo | int_ndisk int_npartition ] path_filepath
230#@brief   Metafunción que devuelve el camino del directorio padre.
231#@see     ogGetPath
[9eefff4]232#@version 0.9 - Pruebas con OpenGnSys.
[23c6c40]233#@author  Ramon Gomez, ETSII Universidad de Sevilla
234#@date    2009-09-29
[1e7eaab]235#*/ ##
[42669ebf]236function ogGetParentPath ()
237{
[ebf06c7]238local PARENT
[4edc0b0]239if [ "$*" == "help" ]; then
240    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
241           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS" \
242           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc" \
243           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS"
244    return
245fi
246
[ebf06c7]247case $# in
[42669ebf]248    1)  PARENT=$(dirname "$1") ;;
[a3fb8b2]249    2)  PARENT="$1 $(dirname "/$2")" ;;
250    3)  PARENT="$1 $2 $(dirname "/$3")" ;;
[ebf06c7]251    *)  ogRaiseError $OG_ERR_FORMAT
[cfeabbf]252        return $? ;;
[ebf06c7]253esac
254ogGetPath $PARENT
255}
256
257
[23c6c40]258#/**
[9eefff4]259#         ogIsNewerFile [ str_repo | int_ndisk int_npart ] path_source [ str_repo | int_ndisk int_npart ] path_target
260#@brief   Metafunción que indica se un fichero es más nuevo que otro.
261#@see     ogGetPath
262#@return  int_code      Código de salida
263#@warning Deben existir tanto el fichero origen como el destino.
264#@version 0.9.2 - Primera versión para OpenGnSys.
265#@author  Ramon Gomez, ETSII Universidad de Sevilla
266#@date    2010-07-24
267#*/ ##
268function ogIsNewerFile ()
269{
270# Variables locales.
271local ARGS SOURCE TARGET
272# Si se solicita, mostrar ayuda.
273if [ "$*" == "help" ]; then
274    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_source [ str_repo | int_ndisk int_npartition ] path_target" \
275           "if $FUNCNAME REPO ubuntu.img CACHE ubuntu.img; then ...  fi"
276    return
277fi
278
279ARGS="$@"
280case "$1" in
281    /*)     # Camino completo.          */ (Comentrio Doxygen)
282        SOURCE=$(ogGetPath "$1")
283        shift ;;
284    [1-9]*) # ndisco npartición.
285        SOURCE=$(ogGetPath "$1" "$2" "$3")
286        shift 3 ;;
287    *)      # Otros: repo, cache, cdrom (no se permiten caminos relativos).
288        SOURCE=$(ogGetPath "$1" "$2")
289        shift 2 ;;
290esac
291# Comprobar que existen los ficheros origen y destino.
292[ -n "$SOURCE" ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return $?
293TARGET=$(ogGetPath "$@")
294[ -n "$TARGET" ] || ogRaiseError $OG_ERR_NOTFOUND "$@" || return $?
295# Devolver si el primer fichero se ha modificado después que el segundo.
296test "$SOURCE" -nt "$TARGET"
297}
298
299
300#/**
[95e48e1]301#         ogMakeChecksumFile [ str_repo | int_ndisk int_npart ] path_filepath
302#@brief   Metafunción que guarda el valor de comprobación de un fichero.
303#@see     ogCalculateChecksum
304#@warning Genera un fichero con extensión ".sum".
305#@version 0.9.2 - Primera versión para OpenGnSys.
306#@author  Ramon Gomez, ETSII Universidad de Sevilla
307#@date    2010-07-24
308#*/ ##
309function ogMakeChecksumFile ()
310{
311# Variables locales.
312local FILE DATA
313if [ "$*" == "help" ]; then
314    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
315           "$FUNCNAME REPO ubuntu.img"
316    return
317fi
318
319# Comprobar que existe el fichero y guardar su checksum.
320FILE=$(ogGetPath "$@")
321[ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
322ogCalculateChecksum "$FILE" > "$FILE.sum"
323}
324
325
326#/**
[23c6c40]327#         ogMakeDir [ str_repo | int_ndisk int_npartition ] path_dirpath
328#@brief   Metafunción que crea un subdirectorio vacío en un dispositivo.
329#@see     ogGetParentPath
[985bef0]330#@version 0.1 -  Integracion para Opengnsys  -   HIDRA: CrearDirectorio.sh, EAC: MkdirPath() en FileSystem.lib
331#@author Ramon Gomez, ETSII Universidad de Sevilla
332#@Date    2008/10/10
333#@author  Antonio J. Doblas Viso. Universidad de Malaga
334#@date    2008/10/27
[9eefff4]335#@version 0.9 - Pruebas con OpenGnSys.
[23c6c40]336#@author  Ramon Gomez, ETSII Universidad de Sevilla
337#@date    2009-09-29
[1e7eaab]338#*/ ##
[42669ebf]339function ogMakeDir ()
340{
[ebf06c7]341local PARENT DIR
342PARENT=$(ogGetParentPath "$@") || return $?
[23c6c40]343DIR="$(basename "${!#}")"
[a3fb8b2]344mkdir -p "$PARENT/$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
[ebf06c7]345}
346 
[a2336d6]347
348#/**
349#         ogNewPath [ str_repo | int_ndisk int_npartition ] path_filepath
350#@brief   Crea el directorio especificado
[42669ebf]351#@param   path_filepath   camino del fichero (independiente de mayúsculas)
352#@param   str_repo        repositorio de ficheros
353#@param   int_ndisk       nº de orden del disco
354#@param   int_npartition  nº de orden de la partición
[a2336d6]355#@return  file - camino completo real del directorio creado
356#@note    repo = { REPO, CACHE }
357#@note    Requisitos: \c grep \c sed
358#@exception OG_ERR_FORMAT    Formato incorrecto.
359#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
360#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
361#@warning Primeras pruebas.
362#@todo    Terminar de definir parámetros para acceso a repositorios.
[9eefff4]363#@version 0.1 - Primera adaptaci³n para OpenGNSys.
[a2336d6]364#@author  obtenido de ogGetPath de Ramon Gomez, ETSII Universidad de Sevilla
365#@date    2009-09-15
366#*/
[42669ebf]367function ogNewPath ()
368{
[a2336d6]369# Variables locales.
370local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR
371
372#/// Si se solicita, mostrar ayuda.
373if [ "$*" == "help" ]; then
374    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
375           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32" \
376           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc/fstab" \
377           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32"
378    return
379fi
380
381#/// Procesar camino según el número de parámetros.
382case $# in
383     # si 1 parametro directorio-fichero completo.
384    1)  FILE="$1" ;;
385    # Si 2 parametros es REPOSITORIO  DIRECTORIO-FICHERO
386    2)  case "$1" in
387            # consultando servidor repositorio base
388            REPO | $IPREPOMAN )  FILE="$OGIMG/$2" || return $OG_ERR_NOTFOUND ;;
389            # consultando particion auxiliar cache local
390            CACHE | $IP )
391                ogMountCache >> /dev/null &&    FILE="$OGCAC/$OGIMG/$2" || return $OG_ERR_NOTFOUND ;;
392            # conectando con Servidor Axuliar.
393            *)     ogRaiseError $OG_ERR_FORMAT
394                   return $? ;;
395        esac ;;
396    # Si 3 parametros DISK PARTITION DIRECTORIO-FICHERO
397    3)  FILE="$(ogMount $1 $2)/$3" || return $OG_ERR_NOTFOUND ;;
398    *)  return $OG_ERR_FORMAT ;;
399esac
400
401mkdir -p ${FILE}
402echo $FILE
403}
404
Note: See TracBrowser for help on using the repository browser.