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