source: client/engine/File.lib @ 397751b

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 397751b was e42f34e, checked in by ramon <ramongomez@…>, 16 years ago

Función ogCopyFile.

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

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