source: client/engine/File.lib @ 3c2933e

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 3c2933e was f81a4deb, checked in by ramon <ramongomez@…>, 14 years ago

Versión 1.0.2: corregir errata de entrecomillado en función ogGetParentPath (modifica #446).

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

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