source: client/engine/File.lib @ 74c04a0

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 74c04a0 was 4edc0b0, checked in by ramon <ramongomez@…>, 15 years ago

Solución ticket:98 en trunk; correcciones varias; engine-1.0: arranque service tracker.

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

  • Property mode set to 100755
File size: 8.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
8#@warning License: GNU GPLv3+
9#*/
10
11
12#/**
13#         ogCopyFile [ str_repo | int_ndisk int_npart ] path_source [ str_repo | int_ndisk int_npart ] path_target
14#@brief   Metafunción para copiar un fichero de sistema OpenGNSys a un directorio OpenGNSys.
15#@see     ogGetPath
16#@warning Deben existir tanto el fichero origen como el directorio destino.
17#@version 0.9 - Pruebas con OpenGNSys.
18#@author  Ramon Gomez, ETSII Universidad de Sevilla
19#@date    2009-10-20
20#*/ ##
21function ogCopyFile ()
22{
23# Variables locales.
24local ARGS SOURCE TARGET
25
26ARGS="$@"
27case "$1" in
28    /*)     # Camino completo.          */ (Comentrio Doxygen)
29        SOURCE=$(ogGetPath "$1")
30        shift ;;
31    [1-9]*) # ndisco npartición.
32        SOURCE=$(ogGetPath "$1" "$2" "$3")
33        shift 3 ;;
34    *)      # Otros: repo, cache, cdrom (no se permiten caminos relativos).
35        SOURCE=$(ogGetPath "$1" "$2")
36        shift 2 ;;
37esac
38# Comprobar fichero origen y directorio destino.
39[ -n "$SOURCE" ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return $?
40TARGET=$(ogGetPath "$@")
41[ -n "$TARGET" ] || ogRaiseError $OG_ERR_NOTFOUND "$@" || return $?
42# Copiar fichero.
43cp -p "$SOURCE" "$TARGET"                    # (definir posible error)
44}
45
46
47#/**
48#         ogDeleteFile [ str_repo | int_ndisk int_npartition ] path_filepath
49#@brief   Metafunción que borra un fichero de un dispositivo.
50#@see     ogGetPath
51#@version 0.9 - Pruebas con OpenGNSys.
52#@author  Ramon Gomez, ETSII Universidad de Sevilla
53#@date    2009-09-29
54#*/ ##
55function ogDeleteFile ()
56{
57# Variables locales.
58local FILE
59# Comprobar que existe el fichero y borrarlo.
60FILE=$(ogGetPath "$@")
61[ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
62rm -f "$FILE" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
63}
64
65
66#/**
67#         ogDeleteTree [ str_repo | int_ndisk int_npartition ] path_dirpath
68#@brief   Metafunción que borra todo un subárbol de directorios de un dispositivo.
69#@see     ogGetPath
70#@version 0.9 - Pruebas con OpenGNSys.
71#@author  Ramon Gomez, ETSII Universidad de Sevilla
72#@date    2009-09-29
73#*/ ##
74function ogDeleteTree ()
75{
76# Variables locales.
77local DIR
78# Comprobar que existe el directorio y borrarlo con su contenido.
79DIR=$(ogGetPath "$@")
80[ -n "$DIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
81rm -fr "$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
82}
83
84
85#/**
86#         ogGetPath [ str_repo | int_ndisk int_npartition ] path_filepath
87#@brief   Inicia el proceso de arranque de un sistema de archivos.
88#@param   path_filepath   camino del fichero (independiente de mayúsculas)
89#@param   str_repo        repositorio de ficheros
90#@param   int_ndisk       nº de orden del disco
91#@param   int_npartition  nº de orden de la partición
92#@return  path_file - camino completo real del fichero.
93#@note    repo = { REPO, CACHE, CDROM }
94#@note    Requisitos: \c grep \c sed
95#@exception OG_ERR_FORMAT    Formato incorrecto.
96#@exception OG_ERR_NOTFOUND  Fichero o dispositivo no encontrado.
97#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
98#@warning En caso de error, sólo devuelve el código y no da mensajes.
99#@todo    Terminar de definir parámetros para acceso a repositorios.
100#@version 0.9 - Pruebas con OpenGNSys.
101#@author  Ramon Gomez, ETSII Universidad de Sevilla
102#@date    2009-09-15
103#*/ ##
104function ogGetPath ()
105{
106# Variables locales.
107local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR
108
109# Si se solicita, mostrar ayuda.
110if [ "$*" == "help" ]; then
111    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
112           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32" \
113           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc/fstab" \
114           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32"
115    return
116fi
117
118# Procesar camino según el número de parámetros.
119case $# in
120    1)  FILE="$1" ;;
121    2)  case "$1" in
122            REPO|repo)
123                FILE="$OGIMG/$2" ;;
124            CACHE|cache)
125                FILE="$(ogMountCache)/$OGIMG/$2" ;;
126            CDROM|cdrom)
127                FILE="$(ogMountCdrom)/$2" ;;
128            *)  ogRaiseError $OG_ERR_FORMAT
129                return $? ;;
130        esac ;;
131    3)  FILE="$(ogMount $1 $2)/$3" ;;
132    *)  ogRaiseError $OG_ERR_FORMAT
133        return $? ;;
134esac
135
136# Eliminar caracteres \c / iniciales, finales y duplicados.
137CURRENTDIR="$PWD"
138        # /* (comentario Doxygen)
139FILE="$(echo $FILE|sed -e 's/\(\/\)*\1/\//g' -e 's/^\///' -e 's/\/$//')"
140PREVFILE=""
141FILEPATH="/"
142while [ "$FILE" != "$PREVFILE" ]; do
143    # Busca el nombre correcto en el directorio actual.
144    cd "$FILEPATH"
145    FILEPATH="${FILEPATH}/$(ls -A | grep -i -m1 "^${FILE%%/*}$")" || return $?
146    PREVFILE="$FILE"
147    FILE="${FILE#*/}"
148done
149        # (comentario Doxygen) */
150# Muestra el camino Linux, quitando el / inicial duplicado.
151[ "$FILEPATH" != "/" ] && echo ${FILEPATH#/}
152cd $CURRENTDIR
153}
154
155
156#/**
157#         ogGetParentPath [ str_repo | int_ndisk int_npartition ] path_filepath
158#@brief   Metafunción que devuelve el camino del directorio padre.
159#@see     ogGetPath
160#@version 0.9 - Pruebas con OpenGNSys.
161#@author  Ramon Gomez, ETSII Universidad de Sevilla
162#@date    2009-09-29
163#*/ ##
164function ogGetParentPath ()
165{
166local PARENT
167if [ "$*" == "help" ]; then
168    ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
169           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS" \
170           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc" \
171           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS"
172    return
173fi
174
175case $# in
176    1)  PARENT=$(dirname "$1") ;;
177    2)  PARENT="$1 $(dirname "/$2")" ;;
178    3)  PARENT="$1 $2 $(dirname "/$3")" ;;
179    *)  ogRaiseError $OG_ERR_FORMAT
180        return $? ;;
181esac
182ogGetPath $PARENT
183}
184
185
186#/**
187#         ogMakeDir [ str_repo | int_ndisk int_npartition ] path_dirpath
188#@brief   Metafunción que crea un subdirectorio vacío en un dispositivo.
189#@see     ogGetParentPath
190#@version 0.9 - Pruebas con OpenGNSys.
191#@author  Ramon Gomez, ETSII Universidad de Sevilla
192#@date    2009-09-29
193#*/ ##
194function ogMakeDir ()
195{
196local PARENT DIR
197PARENT=$(ogGetParentPath "$@") || return $?
198DIR="$(basename "${!#}")"
199mkdir -p "$PARENT/$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
200}
201 
202
203#/**
204#         ogNewPath [ str_repo | int_ndisk int_npartition ] path_filepath
205#@brief   Crea el directorio especificado
206#@param   path_filepath   camino del fichero (independiente de mayúsculas)
207#@param   str_repo        repositorio de ficheros
208#@param   int_ndisk       nº de orden del disco
209#@param   int_npartition  nº de orden de la partición
210#@return  file - camino completo real del directorio creado
211#@note    repo = { REPO, CACHE }
212#@note    Requisitos: \c grep \c sed
213#@exception OG_ERR_FORMAT    Formato incorrecto.
214#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
215#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
216#@warning Primeras pruebas.
217#@todo    Terminar de definir parámetros para acceso a repositorios.
218#@version 0.1 - Primera adaptación para OpenGNSys.
219#@author  obtenido de ogGetPath de Ramon Gomez, ETSII Universidad de Sevilla
220#@date    2009-09-15
221#*/
222function ogNewPath ()
223{
224# Variables locales.
225local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR
226
227#/// Si se solicita, mostrar ayuda.
228if [ "$*" == "help" ]; then
229    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
230           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32" \
231           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc/fstab" \
232           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32"
233    return
234fi
235
236#/// Procesar camino según el número de parámetros.
237case $# in
238     # si 1 parametro directorio-fichero completo.
239    1)  FILE="$1" ;;
240    # Si 2 parametros es REPOSITORIO  DIRECTORIO-FICHERO
241    2)  case "$1" in
242            # consultando servidor repositorio base
243            REPO | $IPREPOMAN )  FILE="$OGIMG/$2" || return $OG_ERR_NOTFOUND ;;
244            # consultando particion auxiliar cache local
245            CACHE | $IP )
246                ogMountCache >> /dev/null &&    FILE="$OGCAC/$OGIMG/$2" || return $OG_ERR_NOTFOUND ;;
247            # conectando con Servidor Axuliar.
248            *)     ogRaiseError $OG_ERR_FORMAT
249                   return $? ;;
250        esac ;;
251    # Si 3 parametros DISK PARTITION DIRECTORIO-FICHERO
252    3)  FILE="$(ogMount $1 $2)/$3" || return $OG_ERR_NOTFOUND ;;
253    *)  return $OG_ERR_FORMAT ;;
254esac
255
256mkdir -p ${FILE}
257echo $FILE
258}
259
Note: See TracBrowser for help on using the repository browser.