source: client/engine/File.lib @ d372e6e

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 d372e6e 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
Line 
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.
7#@version 0.9.2
8#@warning License: GNU GPLv3+
9#*/
10
11
12#/**
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#@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
26#*/ ##
27function ogCalculateChecksum ()
28{
29# Variables locales.
30local FILE
31if [ "$*" == "help" ]; then
32    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
33           "$FUNCNAME REPO ubuntu.img  ==>  ef899299caf8b517ce36f1157a93d8bf"
34    return
35fi
36
37# Comprobar que existe el fichero y devolver sus datos.
38FILE=$(ogGetPath "$@")
39[ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
40tail -c1M "$FILE" | md5sum -b 2>&1 | cut -f1 -d" "
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" \
59           "if $FUNCNAME REPO ubuntu.img CACHE ubuntu.img; then ...;  fi"
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#/**
83#         ogCopyFile [ str_repo | int_ndisk int_npart ] path_source [ str_repo | int_ndisk int_npart ] path_target
84#@brief   Metafunción para copiar un fichero de sistema OpenGnSys a un directorio.
85#@see     ogGetPath
86#@warning Deben existir tanto el fichero origen como el directorio destino.
87#@version 0.9 - Pruebas con OpenGnSys.
88#@author  Ramon Gomez, ETSII Universidad de Sevilla
89#@date    2009-10-20
90#*/ ##
91function ogCopyFile ()
92{
93# Variables locales.
94local ARGS SOURCE TARGET
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
100
101ARGS="$@"
102case "$1" in
103    /*)     # Camino completo.          */ (Comentrio Doxygen)
104        SOURCE="$(ogGetPath "$1")"
105        shift ;;
106    [1-9]*) # ndisco npartición.
107        SOURCE="$(ogGetPath "$1" "$2" "$3")"
108        shift 3 ;;
109    *)      # Otros: repo, cache, cdrom (no se permiten caminos relativos).
110        SOURCE="$(ogGetPath "$1" "$2")"
111        shift 2 ;;
112esac
113# Comprobar fichero origen y directorio destino.
114[ -n "$SOURCE" ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return $?
115TARGET="$(ogGetPath "$@")"
116[ -n "$TARGET" ] || ogRaiseError $OG_ERR_NOTFOUND "$@" || return $?
117# Copiar fichero.
118cp -a "$SOURCE" "$TARGET"                    # (definir posible error)
119}
120
121
122#/**
123#         ogDeleteFile [ str_repo | int_ndisk int_npartition ] path_filepath
124#@brief   Metafunción que borra un fichero de un dispositivo.
125#@see     ogGetPath
126#@version 0.9 - Pruebas con OpenGnSys.
127#@author  Ramon Gomez, ETSII Universidad de Sevilla
128#@date    2009-09-29
129#*/ ##
130function ogDeleteFile ()
131{
132# Variables locales.
133local FILE
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
140# Comprobar que existe el fichero y borrarlo.
141FILE="$(ogGetPath "$@")"
142[ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
143rm -f "$FILE" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
144}
145
146
147#/**
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
151#@version 0.9 - Pruebas con OpenGnSys.
152#@author  Ramon Gomez, ETSII Universidad de Sevilla
153#@date    2009-09-29
154#*/ ##
155function ogDeleteTree ()
156{
157# Variables locales.
158local DIR
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
165# Comprobar que existe el directorio y borrarlo con su contenido.
166DIR="$(ogGetPath "$@")"
167[ -n "$DIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
168rm -fr "$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
169}
170
171
172#/**
173#         ogGetPath [ str_repo | int_ndisk int_npartition ] path_filepath
174#@brief   Inicia el proceso de arranque de un sistema de archivos.
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
179#@return  path_file - camino completo real del fichero.
180#@note    repo = { REPO, CACHE, CDROM }
181#@note    Requisitos: \c grep \c sed
182#@exception OG_ERR_FORMAT    Formato incorrecto.
183#@exception OG_ERR_NOTFOUND  Fichero o dispositivo no encontrado.
184#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
185#@warning En caso de error, sólo devuelve el código y no da mensajes.
186#@todo    Terminar de definir parámetros para acceso a repositorios.
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
192#@version 0.9 - Pruebas con OpenGnSys.
193#@author  Ramon Gomez, ETSII Universidad de Sevilla
194#@date    2009-09-15
195#*/ ##
196function ogGetPath ()
197{
198# Variables locales.
199local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR
200
201# Si se solicita, mostrar ayuda.
202if [ "$*" == "help" ]; then
203    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
204           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32" \
205           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc/fstab" \
206           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32"
207    return
208fi
209
210# Procesar camino según el número de parámetros.
211case $# in
212    1)  FILE="$1" ;;
213    2)  case "$1" in
214            REPO|repo)
215                FILE="$OGIMG/$2" ;;
216            CACHE|cache)
217                FILE="$(ogMountCache)/$OGIMG/$2" ;;
218            CDROM|cdrom)
219                FILE="$(ogMountCdrom)/$2" ;;
220            *)  ogRaiseError $OG_ERR_FORMAT
221                return $? ;;
222        esac ;;
223    3)  FILE="$(ogMount $1 $2)/$3" ;;
224    *)  ogRaiseError $OG_ERR_FORMAT
225        return $? ;;
226esac
227
228# Eliminar caracteres \c / iniciales, finales y duplicados.
229CURRENTDIR="$PWD"
230        # /* (comentario Doxygen)
231FILE="$(echo $FILE|sed -e 's/\(\/\)*\1/\//g' -e 's/^\///' -e 's/\/$//')"
232PREVFILE=""
233FILEPATH="/"
234while [ "$FILE" != "$PREVFILE" ]; do
235    # Busca el nombre correcto en el directorio actual.
236    cd "$FILEPATH"
237    FILEPATH="${FILEPATH}/$(ls -A | grep -i -m1 "^${FILE%%/*}$")" || return $?
238    PREVFILE="$FILE"
239    FILE="${FILE#*/}"
240done
241        # (comentario Doxygen) */
242# Muestra el camino Linux, quitando el / inicial duplicado.
243[ "$FILEPATH" != "/" ] && echo ${FILEPATH#/}
244cd $CURRENTDIR
245}
246
247
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
252#@version 0.9 - Pruebas con OpenGnSys.
253#@author  Ramon Gomez, ETSII Universidad de Sevilla
254#@date    2009-09-29
255#*/ ##
256function ogGetParentPath ()
257{
258local PARENT
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
267case $# in
268    1)  PARENT="$(dirname "$1")" ;;
269    2)  PARENT="$1 $(dirname "/$2")" ;;
270    3)  PARENT="$1 $2 $(dirname "/$3")" ;;
271    *)  ogRaiseError $OG_ERR_FORMAT
272        return $? ;;
273esac
274ogGetPath $PARENT
275}
276
277
278#/**
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
282#@return  Código de salida: 0 - nuevo, 1 - antiguo o error
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
287#@version 1.0.1 - Devolver falso en caso de error.
288#@author  Ramon Gomez, ETSII Universidad de Sevilla
289#@date    2011-05-18
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)
305        SOURCE="$(ogGetPath "$1")"
306        shift ;;
307    [1-9]*) # ndisco npartición.
308        SOURCE="$(ogGetPath "$1" "$2" "$3")"
309        shift 3 ;;
310    *)      # Otros: repo, cache, cdrom (no se permiten caminos relativos).
311        SOURCE="$(ogGetPath "$1" "$2")"
312        shift 2 ;;
313esac
314# Comprobar que existen los ficheros origen y destino.
315[ -n "$SOURCE" ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return 1
316TARGET=$(ogGetPath "$@")
317[ -n "$TARGET" ] || ogRaiseError $OG_ERR_NOTFOUND "$@" || return 1
318# Devolver si el primer fichero se ha modificado después que el segundo.
319test "$SOURCE" -nt "$TARGET"
320}
321
322
323#/**
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.
335local FILE
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.
343FILE="$(ogGetPath "$@")"
344[ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
345ogCalculateChecksum "$FILE" > "$FILE.sum"
346}
347
348
349#/**
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
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
358#@version 0.9 - Pruebas con OpenGnSys.
359#@author  Ramon Gomez, ETSII Universidad de Sevilla
360#@date    2009-09-29
361#*/ ##
362function ogMakeDir ()
363{
364local PARENT DIR
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
371PARENT="$(ogGetParentPath "$@")" || return $?
372DIR="$(basename "${!#}")"
373mkdir -p "$PARENT/$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
374}
375 
376
377#/**
378#         ogNewPath [ str_repo | int_ndisk int_npartition ] path_filepath
379#@brief   Crea el directorio especificado
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
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.
392#@version 0.1 - Primera adaptaci³n para OpenGNSys.
393#@author  obtenido de ogGetPath de Ramon Gomez, ETSII Universidad de Sevilla
394#@date    2009-09-15
395#*/
396function ogNewPath ()
397{
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.