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 | #*/ ## |
---|
21 | function ogCopyFile () |
---|
22 | { |
---|
23 | # Variables locales. |
---|
24 | local ARGS SOURCE TARGET |
---|
25 | |
---|
26 | ARGS="$@" |
---|
27 | case "$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 ;; |
---|
37 | esac |
---|
38 | # Comprobar fichero origen y directorio destino. |
---|
39 | [ -n "$SOURCE" ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return $? |
---|
40 | TARGET=$(ogGetPath "$@") |
---|
41 | [ -n "$TARGET" ] || ogRaiseError $OG_ERR_NOTFOUND "$@" || return $? |
---|
42 | # Copiar fichero. |
---|
43 | cp -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 | #*/ ## |
---|
55 | function ogDeleteFile () |
---|
56 | { |
---|
57 | # Variables locales. |
---|
58 | local FILE |
---|
59 | # Comprobar que existe el fichero y borrarlo. |
---|
60 | FILE=$(ogGetPath "$@") |
---|
61 | [ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $? |
---|
62 | rm -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 | #*/ ## |
---|
74 | function ogDeleteTree () |
---|
75 | { |
---|
76 | # Variables locales. |
---|
77 | local DIR |
---|
78 | # Comprobar que existe el directorio y borrarlo con su contenido. |
---|
79 | DIR=$(ogGetPath "$@") |
---|
80 | [ -n "$DIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $? |
---|
81 | rm -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 | #*/ ## |
---|
104 | function ogGetPath () |
---|
105 | { |
---|
106 | # Variables locales. |
---|
107 | local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR |
---|
108 | |
---|
109 | # Si se solicita, mostrar ayuda. |
---|
110 | if [ "$*" == "help" ]; then |
---|
111 | ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \ |
---|
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 |
---|
116 | fi |
---|
117 | |
---|
118 | # Procesar camino según el número de parámetros. |
---|
119 | case $# in |
---|
120 | 1) FILE="$1" ;; |
---|
121 | 2) case "$1" in |
---|
122 | REPO|repo) |
---|
123 | FILE="$OGIMG/$2" ;; |
---|
124 | CACHE|cache) |
---|
125 | FILE="$(ogMountCache)/$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 $? ;; |
---|
134 | esac |
---|
135 | |
---|
136 | # Eliminar caracteres \c / iniciales, finales y duplicados. |
---|
137 | CURRENTDIR="$PWD" |
---|
138 | # /* (comentario Doxygen) |
---|
139 | FILE="$(echo $FILE|sed -e 's/\(\/\)*\1/\//g' -e 's/^\///' -e 's/\/$//')" |
---|
140 | PREVFILE="" |
---|
141 | FILEPATH="/" |
---|
142 | while [ "$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#*/}" |
---|
148 | done |
---|
149 | # (comentario Doxygen) */ |
---|
150 | # Muestra el camino Linux, quitando el / inicial duplicado. |
---|
151 | [ "$FILEPATH" != "/" ] && echo ${FILEPATH#/} |
---|
152 | cd $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 | #*/ ## |
---|
164 | function ogGetParentPath () |
---|
165 | { |
---|
166 | local PARENT |
---|
167 | if [ "$*" == "help" ]; then |
---|
168 | ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \ |
---|
169 | "$FUNCNAME \"/mnt/sda1/windows/system32\" ==> /mnt/sda1/WINDOWS" \ |
---|
170 | "$FUNCNAME REPO /etc/fstab ==> /opt/opengnsys/images/etc" \ |
---|
171 | "$FUNCNAME 1 1 \"/windows/system32\" ==> /mnt/sda1/WINDOWS" |
---|
172 | return |
---|
173 | fi |
---|
174 | |
---|
175 | case $# in |
---|
176 | 1) PARENT=$(dirname "$1") ;; |
---|
177 | 2) PARENT="$1 $(dirname "/$2")" ;; |
---|
178 | 3) PARENT="$1 $2 $(dirname "/$3")" ;; |
---|
179 | *) ogRaiseError $OG_ERR_FORMAT |
---|
180 | return $? ;; |
---|
181 | esac |
---|
182 | ogGetPath $PARENT |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | #/** |
---|
187 | # ogMakeDir [ str_repo | int_ndisk int_npartition ] path_dirpath |
---|
188 | #@brief Metafunción que crea un subdirectorio vacío en un dispositivo. |
---|
189 | #@see ogGetParentPath |
---|
190 | #@version 0.9 - Pruebas con OpenGNSys. |
---|
191 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
192 | #@date 2009-09-29 |
---|
193 | #*/ ## |
---|
194 | function ogMakeDir () |
---|
195 | { |
---|
196 | local PARENT DIR |
---|
197 | PARENT=$(ogGetParentPath "$@") || return $? |
---|
198 | DIR="$(basename "${!#}")" |
---|
199 | mkdir -p "$PARENT/$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $? |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | #/** |
---|
204 | # ogNewPath [ str_repo | int_ndisk int_npartition ] path_filepath |
---|
205 | #@brief Crea el directorio especificado |
---|
206 | #@param path_filepath camino del fichero (independiente de mayúsculas) |
---|
207 | #@param str_repo repositorio de ficheros |
---|
208 | #@param int_ndisk nº de orden del disco |
---|
209 | #@param int_npartition nº de orden de la partición |
---|
210 | #@return file - camino completo real del directorio creado |
---|
211 | #@note repo = { REPO, CACHE } |
---|
212 | #@note Requisitos: \c grep \c sed |
---|
213 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
214 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
215 | #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar. |
---|
216 | #@warning Primeras pruebas. |
---|
217 | #@todo Terminar de definir parámetros para acceso a repositorios. |
---|
218 | #@version 0.1 - Primera adaptación para OpenGNSys. |
---|
219 | #@author obtenido de ogGetPath de Ramon Gomez, ETSII Universidad de Sevilla |
---|
220 | #@date 2009-09-15 |
---|
221 | #*/ |
---|
222 | function ogNewPath () |
---|
223 | { |
---|
224 | # Variables locales. |
---|
225 | local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR |
---|
226 | |
---|
227 | #/// Si se solicita, mostrar ayuda. |
---|
228 | if [ "$*" == "help" ]; then |
---|
229 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
230 | "$FUNCNAME \"/mnt/sda1/windows/system32\" ==> /mnt/sda1/WINDOWS/System32" \ |
---|
231 | "$FUNCNAME REPO /etc/fstab ==> /opt/opengnsys/images/etc/fstab" \ |
---|
232 | "$FUNCNAME 1 1 \"/windows/system32\" ==> /mnt/sda1/WINDOWS/System32" |
---|
233 | return |
---|
234 | fi |
---|
235 | |
---|
236 | #/// Procesar camino según el número de parámetros. |
---|
237 | case $# in |
---|
238 | # si 1 parametro directorio-fichero completo. |
---|
239 | 1) FILE="$1" ;; |
---|
240 | # Si 2 parametros es REPOSITORIO DIRECTORIO-FICHERO |
---|
241 | 2) case "$1" in |
---|
242 | # consultando servidor repositorio base |
---|
243 | REPO | $IPREPOMAN ) FILE="$OGIMG/$2" || return $OG_ERR_NOTFOUND ;; |
---|
244 | # consultando particion auxiliar cache local |
---|
245 | CACHE | $IP ) |
---|
246 | ogMountCache >> /dev/null && FILE="$OGCAC/$OGIMG/$2" || return $OG_ERR_NOTFOUND ;; |
---|
247 | # conectando con Servidor Axuliar. |
---|
248 | *) ogRaiseError $OG_ERR_FORMAT |
---|
249 | return $? ;; |
---|
250 | esac ;; |
---|
251 | # Si 3 parametros DISK PARTITION DIRECTORIO-FICHERO |
---|
252 | 3) FILE="$(ogMount $1 $2)/$3" || return $OG_ERR_NOTFOUND ;; |
---|
253 | *) return $OG_ERR_FORMAT ;; |
---|
254 | esac |
---|
255 | |
---|
256 | mkdir -p ${FILE} |
---|
257 | echo $FILE |
---|
258 | } |
---|
259 | |
---|