Advertisement
PastersGonnaPaste

per-chunk md5sum

Nov 28th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.28 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ### HOW TO USE
  4. # 1)
  5. # set the $blocksize, $digits and $prefix variables.
  6. # defaults are:
  7. # blocksize=$((64*1024))
  8. # digits=20
  9. # prefix=FILE.part.
  10. #
  11. # 2)
  12. # run/syntax:
  13. # $ Chunkmd5sum FILE
  14. #
  15. # 3)
  16. # check/read the FILE.parts.md5 file for a list of partial md5sums:
  17. # $ more FILE.parts.md5
  18. #
  19. # 4)
  20. # do something useful with this information. :-)
  21. #
  22. # limitations:
  23. # FILE must be a fixed size regular file that can be "stat"'ed
  24. # in order to get it's filesize.
  25. # Chunkmd5sum currently does not support reading from stdin/streams.
  26.  
  27. function Split {
  28.     split -a ${digits:-20} -d -b "$blocksize" "$infile" "${prefix:-$infile.part.}"
  29. }
  30.  
  31. function Countblocks {
  32.     NR=0
  33.     stat "$infile" |
  34.     while read -t 60 line; do
  35.         ((NR += 1))
  36.         if (( NR == 2 )); then
  37.             line="${line##*Size: }"
  38.             size="${line%% *}"
  39.             if (( size >= 0 )) && (( blocksize >= 0 )); then
  40.                 blocks=$(( size/blocksize ))
  41.                 rest=$(( size - (blocks*blocksize) ))
  42.                 if (( blocks >= 0 )) && (( rest >= 0 )); then
  43.                     echo "$blocks $rest"
  44.                 fi
  45.             else
  46.                 return 1
  47.             fi
  48.             break
  49.         fi
  50.         if (( NR > 2 )); then
  51.             # emergency handbrake
  52.             return 1
  53.         fi
  54.     done
  55. }
  56.  
  57. function Createfifos {
  58.     noblocks=$(Countblocks)
  59.     if [[ -z "$noblocks" ]]; then
  60.         return 1
  61.     fi
  62.     blocks=${noblocks%% *}
  63.     rest=${noblocks##* }
  64.     if (( rest > 0 )); then
  65.         (( blocks += 1 ))
  66.     fi
  67.     partno=0
  68.     while (( partno < blocks )); do
  69.         printf "%s%0${digits:-20}u\n" "${prefix:-$infile.part.}" "$partno"
  70.         (( partno += 1 ))
  71.     done | tee /tmp/fifos-$$.list | xargs -n 32 mkfifo
  72. }
  73.  
  74. function Removefifos {
  75.     xargs -a /tmp/fifos-$$.list -n 32 rm &&
  76.     rm /tmp/fifos-$$.list
  77. }
  78.  
  79. function Checksum {
  80.     md5sum "${prefix:-$infile.part.}"* > "${prefix:-$infile.parts.}"md5
  81. }
  82.  
  83. function Chunkmd5sum {
  84.     infile="${infile:-$1}"
  85.     blocksize=${blocksize:-$((64*1024))}
  86.     digits=${digits:-20}
  87.     if [[ -f "$infile" ]] && [[ -r "$infile" ]]; then
  88.         Createfifos || return 1
  89.         (Checksum &)
  90.         Split &&
  91.         Removefifos    
  92.     else
  93.         return 1
  94.     fi
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement