source: client/engine/File.lib @ d725a41

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 d725a41 was 42669ebf, checked in by ramon <ramongomez@…>, 15 years ago

Funciones adaptadas a plantilla Doxygen; nueva función ogFsToId.

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

  • Property mode set to 100755
File size: 8.2 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 int_ndisk int_npartition" \
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="$OGCAC/$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
167case $# in
168    1)  PARENT=$(dirname "$1") ;;
169    2)  PARENT="$1 $(dirname "/$2")" ;;
170    3)  PARENT="$1 $2 $(dirname "/$3")" ;;
171    *)  ogRaiseError $OG_ERR_FORMAT
172        return $? ;;
173esac
174ogGetPath $PARENT
175}
176
177
178#/**
179#         ogMakeDir [ str_repo | int_ndisk int_npartition ] path_dirpath
180#@brief   Metafunción que crea un subdirectorio vacío en un dispositivo.
181#@see     ogGetParentPath
182#@version 0.9 - Pruebas con OpenGNSys.
183#@author  Ramon Gomez, ETSII Universidad de Sevilla
184#@date    2009-09-29
185#*/ ##
186function ogMakeDir ()
187{
188local PARENT DIR
189PARENT=$(ogGetParentPath "$@") || return $?
190DIR="$(basename "${!#}")"
191mkdir -p "$PARENT/$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
192}
193 
194
195#/**
196#         ogNewPath [ str_repo | int_ndisk int_npartition ] path_filepath
197#@brief   Crea el directorio especificado
198#@param   path_filepath   camino del fichero (independiente de mayúsculas)
199#@param   str_repo        repositorio de ficheros
200#@param   int_ndisk       nº de orden del disco
201#@param   int_npartition  nº de orden de la partición
202#@return  file - camino completo real del directorio creado
203#@note    repo = { REPO, CACHE }
204#@note    Requisitos: \c grep \c sed
205#@exception OG_ERR_FORMAT    Formato incorrecto.
206#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
207#@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar.
208#@warning Primeras pruebas.
209#@todo    Terminar de definir parámetros para acceso a repositorios.
210#@version 0.1 - Primera adaptación para OpenGNSys.
211#@author  obtenido de ogGetPath de Ramon Gomez, ETSII Universidad de Sevilla
212#@date    2009-09-15
213#*/
214function ogNewPath ()
215{
216# Variables locales.
217local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR
218
219#/// Si se solicita, mostrar ayuda.
220if [ "$*" == "help" ]; then
221    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
222           "$FUNCNAME \"/mnt/sda1/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32" \
223           "$FUNCNAME REPO /etc/fstab  ==>  /opt/opengnsys/images/etc/fstab" \
224           "$FUNCNAME 1 1 \"/windows/system32\"  ==>  /mnt/sda1/WINDOWS/System32"
225    return
226fi
227
228#/// Procesar camino según el número de parámetros.
229case $# in
230     # si 1 parametro directorio-fichero completo.
231    1)  FILE="$1" ;;
232    # Si 2 parametros es REPOSITORIO  DIRECTORIO-FICHERO
233    2)  case "$1" in
234            # consultando servidor repositorio base
235            REPO | $IPREPOMAN )  FILE="$OGIMG/$2" || return $OG_ERR_NOTFOUND ;;
236            # consultando particion auxiliar cache local
237            CACHE | $IP )
238                ogMountCache >> /dev/null &&    FILE="$OGCAC/$OGIMG/$2" || return $OG_ERR_NOTFOUND ;;
239            # conectando con Servidor Axuliar.
240            *)     ogRaiseError $OG_ERR_FORMAT
241                   return $? ;;
242        esac ;;
243    # Si 3 parametros DISK PARTITION DIRECTORIO-FICHERO
244    3)  FILE="$(ogMount $1 $2)/$3" || return $OG_ERR_NOTFOUND ;;
245    *)  return $OG_ERR_FORMAT ;;
246esac
247
248mkdir -p ${FILE}
249echo $FILE
250}
251
Note: See TracBrowser for help on using the repository browser.