Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ### HOW TO USE
- # 1)
- # set the $blocksize, $digits and $prefix variables.
- # defaults are:
- # blocksize=$((64*1024))
- # digits=20
- # prefix=FILE.part.
- #
- # 2)
- # run/syntax:
- # $ Chunkmd5sum FILE
- #
- # 3)
- # check/read the FILE.parts.md5 file for a list of partial md5sums:
- # $ more FILE.parts.md5
- #
- # 4)
- # do something useful with this information. :-)
- #
- # limitations:
- # FILE must be a fixed size regular file that can be "stat"'ed
- # in order to get it's filesize.
- # Chunkmd5sum currently does not support reading from stdin/streams.
- function Split {
- split -a ${digits:-20} -d -b "$blocksize" "$infile" "${prefix:-$infile.part.}"
- }
- function Countblocks {
- NR=0
- stat "$infile" |
- while read -t 60 line; do
- ((NR += 1))
- if (( NR == 2 )); then
- line="${line##*Size: }"
- size="${line%% *}"
- if (( size >= 0 )) && (( blocksize >= 0 )); then
- blocks=$(( size/blocksize ))
- rest=$(( size - (blocks*blocksize) ))
- if (( blocks >= 0 )) && (( rest >= 0 )); then
- echo "$blocks $rest"
- fi
- else
- return 1
- fi
- break
- fi
- if (( NR > 2 )); then
- # emergency handbrake
- return 1
- fi
- done
- }
- function Createfifos {
- noblocks=$(Countblocks)
- if [[ -z "$noblocks" ]]; then
- return 1
- fi
- blocks=${noblocks%% *}
- rest=${noblocks##* }
- if (( rest > 0 )); then
- (( blocks += 1 ))
- fi
- partno=0
- while (( partno < blocks )); do
- printf "%s%0${digits:-20}u\n" "${prefix:-$infile.part.}" "$partno"
- (( partno += 1 ))
- done | tee /tmp/fifos-$$.list | xargs -n 32 mkfifo
- }
- function Removefifos {
- xargs -a /tmp/fifos-$$.list -n 32 rm &&
- rm /tmp/fifos-$$.list
- }
- function Checksum {
- md5sum "${prefix:-$infile.part.}"* > "${prefix:-$infile.parts.}"md5
- }
- function Chunkmd5sum {
- infile="${infile:-$1}"
- blocksize=${blocksize:-$((64*1024))}
- digits=${digits:-20}
- if [[ -f "$infile" ]] && [[ -r "$infile" ]]; then
- Createfifos || return 1
- (Checksum &)
- Split &&
- Removefifos
- else
- return 1
- fi
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement