source: client/engine/File.lib @ dce072b

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

NTFS: formatear; FAT 12/16/32: comprobar y formatear; retoques en scripts.

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