3 # public domain by Nathan Wagner <nw@hydaspes.if.org>
5 # Assume N bytes total.
6 # Assume L is the lowest number of files allowable.
7 # H is the highest number of files allowable.
8 # F is the actual number of files used
9 # B is the minimum bytes per file
10 # R is the remaining bytes if all files are of size B
11 # K is the maximum number of files allowed to be one byte larger than the
14 # So, you need to determine if there is some L <= F <= H such that R <= K.
16 # For a given candidate F:
19 # if R <= K then the candidate F is allowable, F files will be used,
20 # R of them will be of size B+1 and F-R of them will be of size B.
31 while getopts l:h:k:n:ev flag ; do
39 ?) printf "Usage: evensplit [-lhknev] <file>\n" 1>&2; exit 2 ;;
42 shift $(($OPTIND - 1))
46 N=$(stat -c '%s' $INPUT)
47 : ${L:=$((N/65536))} # default to at least 65536 bytes per split
48 if [ $L -lt 1 ]; then L=1; fi
50 : ${H:=$N} # use one byte per file if needed
51 if [ $H -lt $L ]; then H=$L; fi
53 : ${K:=$((H-1))} # by default K can by any number of the files
57 if [ $VERBOSE -ne 0 ]; then
58 echo '#' F files: $L '<= F <=' $H, at most $K files one byte larger 1>&2
61 for F in $(seq $L $H); do
64 if [ $R -le $K ]; then
65 if [ $VERBOSE -ne 0 ]; then
66 printf "# Usable: %u files, size %u" $((F-R)) $B 1>&2
68 printf ", %u size %u", $R $((B+1)) 1>&2
77 echo "no usable splits found" 1>&2;
81 DD="dd if=$INPUT count=1"
83 # ok, at this point we want F-R files of size $B
84 for ext in $(seq -w 1 $F); do
85 x=$(echo $ext | sed -e 's/^0\+//')
86 if [ $x -gt $((F-R)) ]; then
87 ${ECHO} $DD of=${INPUT}.$ext bs=$((B+1)) skip=$(((F-R) * B)) iflag=skip_bytes
89 ${ECHO} $DD of=${INPUT}.$ext bs=$B skip=$((x-1))