source: client/engine/File.lib @ 5e1020c

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 5e1020c 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
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  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#/**
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#/**
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
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
335#@version 0.9 - Pruebas con OpenGnSys.
336#@author  Ramon Gomez, ETSII Universidad de Sevilla
337#@date    2009-09-29
338#*/ ##
339function ogMakeDir ()
340{
341local PARENT DIR
342PARENT=$(ogGetParentPath "$@") || return $?
343DIR="$(basename "${!#}")"
344mkdir -p "$PARENT/$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
345}
346 
347
348#/**
349#         ogNewPath [ str_repo | int_ndisk int_npartition ] path_filepath
350#@brief   Crea el directorio especificado
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
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.
363#@version 0.1 - Primera adaptaci³n para OpenGNSys.
364#@author  obtenido de ogGetPath de Ramon Gomez, ETSII Universidad de Sevilla
365#@date    2009-09-15
366#*/
367function ogNewPath ()
368{
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.