source: client/engine/File.lib @ e9a1f41

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

version 1.0.1: adaptar funciones ogIs... a valores de la API 1.0: 0=true, 1=false (cerrar #396).

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

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