OpenGnsys  1.1.1
File.lib
Ir a la documentación de este archivo.
1 #!/bin/bash
2 #
12 # ##
28 {
29 # Variables locales.
30 local FILE
31 if [ "$*" == "help" ]; then
32  ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
33  "$FUNCNAME REPO ubuntu.img ==> ef899299caf8b517ce36f1157a93d8bf"
34  return
35 fi
36 
37 # Comprobar que existe el fichero y devolver sus datos.
38 FILE=$(ogGetPath "$@")
39 [ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
40 tail -c1M "$FILE" | md5sum -b 2>&1 | cut -f1 -d" "
41 }
42 
43 
44 # ##
54 {
55 # Variables locales.
56 local ARGS SOURCE TARGET
57 if [ "$*" == "help" ]; then
58  ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
59  "if $FUNCNAME REPO ubuntu.img CACHE ubuntu.img; then ...; fi"
60  return
61 fi
62 
63 ARGS="$@"
64 case "$1" in
65  /*) # Camino completo. */ (Comentrio Doxygen)
66  SOURCE=$(ogGetPath "$1")
67  shift ;;
68  [1-9]*) # ndisco npartición.
69  SOURCE=$(ogGetPath "$1" "$2" "$3")
70  shift 3 ;;
71  *) # Otros: repo, cache, cdrom (no se permiten caminos relativos).
72  SOURCE=$(ogGetPath "$1" "$2")
73  shift 2 ;;
74 esac
75 TARGET=$(ogGetPath "$@")
76 
77 # Comparar los ficheros de checksum.
78 test "$(cat "$SOURCE.sum" 2>/dev/null)" == "$(cat "$TARGET.sum" 2>/dev/null)"
79 }
80 
81 
82 # ##
95 {
96 # Variables locales.
97 local FILE
98 if [ "$*" == "help" ]; then
99  ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
100  "$FUNCNAME REPO ubuntu.img ==> ef899299caf8b517ce36f1157a93d8bf"
101  return
102 fi
103 
104 # Comprobar que existe el fichero y devolver sus datos.
105 FILE=$(ogGetPath "$@")
106 [ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
107 #ADV
108 md5sum "$FILE" -b 2>&1 | cut -f1 -d" "
109 # tail -c1M "$FILE" | md5sum -b 2>&1 | cut -f1 -d" "
110 }
111 
112 
113 
114 
115 # ##
128 function ogCopyFile ()
129 {
130 # Variables locales.
131 local ARGS SOURCE TARGET
132 if [ "$*" == "help" ]; then
133  ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_source [ str_repo | int_ndisk int_npartition ] path_target" \
134  "$FUNCNAME REPO newfile.txt 1 2 /tmp/newfile.txt"
135  return
136 fi
137 
138 ARGS="$@"
139 case "$1" in
140  /*) # Camino completo. */ (Comentrio Doxygen)
141  SOURCE="$(ogGetPath "$1")"
142  shift ;;
143  [1-9]*) # ndisco npartición.
144  SOURCE="$(ogGetPath "$1" "$2" "$3")"
145  shift 3 ;;
146  *) # Otros: repo, cache, cdrom (no se permiten caminos relativos).
147  SOURCE="$(ogGetPath "$1" "$2")"
148  shift 2 ;;
149 esac
150 # Comprobar fichero origen y directorio destino.
151 [ -n "$SOURCE" ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return $?
152 TARGET="$(ogGetPath "$@")"
153 [ -n "$TARGET" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
154 # Copiar fichero (para evitar problemas de comunicaciones las copias se hacen con rsync en vez de cp).
155 rsync --progress --inplace -avh "$SOURCE" "$TARGET"
156 }
157 
158 
159 # ##
167 function ogDeleteFile ()
168 {
169 # Variables locales.
170 local FILE
171 if [ "$*" == "help" ]; then
172  ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_file" \
173  "$FUNCNAME 1 2 /tmp/newfile.txt"
174  return
175 fi
176 
177 # Comprobar que existe el fichero y borrarlo.
178 FILE="$(ogGetPath "$@")"
179 [ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
180 rm -f "$FILE" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
181 }
182 
183 
184 # ##
192 function ogDeleteTree ()
193 {
194 # Variables locales.
195 local DIR
196 if [ "$*" == "help" ]; then
197  ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_dir" \
198  "$FUNCNAME 1 2 /tmp/newdir"
199  return
200 fi
201 
202 # Comprobar que existe el directorio y borrarlo con su contenido.
203 DIR="$(ogGetPath "$@")"
204 [ -n "$DIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
205 rm -fr "$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
206 }
207 
208 
209 # ##
236 
237 function ogGetPath ()
238 {
239 # Variables locales.
240 local MNTDIR FILE PREVFILE FILEPATH CURRENTDIR
241 
242 # Si se solicita, mostrar ayuda.
243 if [ "$*" == "help" ]; then
244  ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
245  "$FUNCNAME \"/mnt/sda1/windows/system32\" ==> /mnt/sda1/WINDOWS/System32" \
246  "$FUNCNAME REPO /etc/fstab ==> /opt/opengnsys/images/etc/fstab" \
247  "$FUNCNAME 1 1 \"/windows/system32\" ==> /mnt/sda1/WINDOWS/System32"
248  return
249 fi
250 
251 # Procesar camino según el número de parámetros.
252 case $# in
253  1) FILE="$1" ;;
254  2) case "${1^^}" in
255  REPO)
256  FILE="$OGIMG/$2" ;;
257  CACHE)
258  MNTDIR="$(ogMountCache)" || return $?
259  FILE="$MNTDIR/$OGIMG/$2" ;;
260  CDROM)
261  MNTDIR="$(ogMountCdrom)" || return $?
262  FILE="$MNTDIR/$2" ;;
263  *) ogRaiseError $OG_ERR_FORMAT
264  return $? ;;
265  esac ;;
266  3) MNTDIR="$(ogMount $1 $2)" || return $?
267  FILE="$MNTDIR/$3" ;;
268  *) ogRaiseError $OG_ERR_FORMAT
269  return $? ;;
270 esac
271 
272 # Eliminar caracteres \c / duplicados y finales.
273 
274 FILE="$(echo $FILE|sed -e 's/\(\/\)*\1/\//g; s/\/$//')"
275 # Comprobar si existe el fichero para reducir tiempos.
276 if [ -e "$FILE" ]; then
277  FILEPATH="$FILE"
278 else
279  # Buscar el nombre correcto en cada subdirectorio del camino.
280  FILEPATH="/"
281 
282  while [ "$FILE" != "$PREVFILE" ]; do
283  FILEPATH="$(ls -d "${FILEPATH%/}/${FILE%%/*}" 2>/dev/null || find "$FILEPATH" -maxdepth 1 -iname "${FILE%%/*}" -print 2>/dev/null)" #*/ (Comentario Doxygen)
284  PREVFILE="$FILE"
285  FILE="${FILE#*/}"
286  done
287 
288 fi
289 [ -n "$FILEPATH" ] && echo "$FILEPATH"
290 return 0
291 }
292 
293 
294 # ##
302 
303 function ogGetParentPath ()
304 {
305 local PARENT
306 if [ "$*" == "help" ]; then
307  ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
308  "$FUNCNAME \"/mnt/sda1/windows/system32\" ==> /mnt/sda1/WINDOWS" \
309  "$FUNCNAME REPO /etc/fstab ==> /opt/opengnsys/images/etc" \
310  "$FUNCNAME 1 1 \"/windows/system32\" ==> /mnt/sda1/WINDOWS"
311  return
312 fi
313 
314 case $# in
315  1) PARENT="$(dirname "$1")" ;;
316  2) PARENT="$1 $(dirname "/$2")" ;;
317  3) PARENT="$1 $2 $(dirname "/$3")" ;;
318  *) ogRaiseError $OG_ERR_FORMAT
319  return $? ;;
320 esac
321 ogGetPath $PARENT
322 }
323 
324 
325 # ##
338 function ogIsNewerFile ()
339 {
340 # Variables locales.
341 local ARGS SOURCE TARGET
342 # Si se solicita, mostrar ayuda.
343 if [ "$*" == "help" ]; then
344  ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_source [ str_repo | int_ndisk int_npartition ] path_target" \
345  "if $FUNCNAME REPO ubuntu.img CACHE ubuntu.img; then ... fi"
346  return
347 fi
348 
349 ARGS="$@"
350 case "$1" in
351  /*) # Camino completo. */ (Comentrio Doxygen)
352  SOURCE="$(ogGetPath "$1")"
353  shift ;;
354  [1-9]*) # ndisco npartición.
355  SOURCE="$(ogGetPath "$1" "$2" "$3")"
356  shift 3 ;;
357  *) # Otros: repo, cache, cdrom (no se permiten caminos relativos).
358  SOURCE="$(ogGetPath "$1" "$2")"
359  shift 2 ;;
360 esac
361 # Comprobar que existen los ficheros origen y destino.
362 [ -n "$SOURCE" ] || ogRaiseError $OG_ERR_NOTFOUND "${ARGS% $*}" || return 1
363 TARGET=$(ogGetPath "$@")
364 [ -n "$TARGET" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return 1
365 # Devolver si el primer fichero se ha modificado después que el segundo.
366 test "$SOURCE" -nt "$TARGET"
367 }
368 
369 
370 # ##
379 function ogMakeChecksumFile ()
380 {
381 # Variables locales.
382 local FILE
383 if [ "$*" == "help" ]; then
384  ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath" \
385  "$FUNCNAME REPO ubuntu.img"
386  return
387 fi
388 
389 # Comprobar que existe el fichero y guardar su checksum.
390 FILE="$(ogGetPath "$@")"
391 [ -n "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
392 ogCalculateChecksum "$FILE" > "$FILE.sum"
393 }
394 
395 
396 # ##
409 function ogMakeDir ()
410 {
411 local PARENT DIR
412 if [ "$*" == "help" ]; then
413  ogHelp "$FUNCNAME" "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_dir" \
414  "$FUNCNAME 1 2 /tmp/newdir"
415  return
416 fi
417 
418 PARENT="$(ogGetParentPath "$@")" || return $?
419 DIR="$(basename "${!#}")"
420 mkdir -p "$PARENT/$DIR" || ogRaiseError $OG_ERR_NOTFOUND "$*" || return $?
421 }
422 
function ogCopyFile()
Metafunción para copiar un fichero de sistema OpenGnSys a un directorio. #.
Definition: File.lib:128
function ogDeleteTree()
Metafunción que borra todo un subárbol de directorios de un dispositivo. #.
Definition: File.lib:192
function ogRaiseError()
Devuelve el mensaje y el código de error correspondiente. #.
Definition: System.lib:188
function ogHelp()
Muestra mensaje de ayuda para una función determinda. #.
Definition: System.lib:153
function ogGetPath()
Inicia el proceso de arranque de un sistema de archivos. #.
Definition: File.lib:237
function ogCalculateChecksum()
Devuelve la suma de comprobación (checksum) de un fichero. #.
Definition: File.lib:27
function ogDeleteFile()
Metafunción que borra un fichero de un dispositivo. #.
Definition: File.lib:167
function ogCompareChecksumFiles()
Metafunción que compara las sumas de comprobación almacenadas de 2 ficheros. #.
Definition: File.lib:53
function ogCalculateFullChecksum()
Devuelve la suma COMPLETA de comprobación (checksum) de un fichero. #.
Definition: File.lib:94
function rsync()
Función para utilizar la versión de rsync situada en $OPENGNSYS/bin en vez de la del sistema operativ...
Definition: Rsync.lib:10