source: client/engine/File.lib @ 0d997c6

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 0d997c6 was 55ad138c, checked in by ramon <ramongomez@…>, 16 years ago

Corrección ticket:73; metafunciones ogListPrimaryPartitions y ogListLogicalPartitions.

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

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