Advertisement
spikeysnack

carchive

Nov 7th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.40 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # uses  cat  for fast appending of files
  4. #       stat for getting file info
  5. #       xxd  for reading byte offsets
  6. #        dd  for extracting files fril
  7.  
  8. # perhaps stat and dd alone would be more sane
  9.  
  10. # a cat archive is a carchive? why not?
  11.  
  12. carchive ()
  13. {
  14.     let offset=0
  15.  
  16.     STAT="/tmp/stat.tmp"
  17.  
  18.     [ -f "${STAT}" ] && rm -f "${STAT}"
  19.  
  20.  
  21.     CARCHIVE="${1}"
  22.  
  23.     carext="${CARCHIVE##*.}"
  24.  
  25.     [ ! "Xcar" == "X${carext}" ] && echo "first paramater should be the archive name: ( <nams>.car )" && exit 1
  26.  
  27.     shift
  28.  
  29.     for f in "$@"
  30.     do
  31.     ext="${f##*.}"
  32.  
  33.     fname="${f%.*}"
  34.  
  35.     let fsize="$(stat --format='%s' ${f} )"
  36.  
  37.     let offset="$offset + $fsize"
  38.  
  39.     cat "$offset" >> "${STAT}"
  40.  
  41.     stat -t "$f" >> "${STAT}"
  42.  
  43.     cat "$f" >> /tmp/"$CARCHIVE"
  44.     done
  45.  
  46.     statfile_size=$(stat --format="%s" "${STAT}")
  47.  
  48.     cat "${STAT}"
  49.    
  50.     echo -e "statfile size =\t ${statfile_size} bytes"  
  51.  
  52.     cat "${STAT}"  >> "/tmp/${CARCHIVE}"
  53.     echo ${statfile_size} >> "/tmp/${CARCHIVE}"
  54.     cp "/tmp/${CARCHIVE}" .
  55. }
  56.  
  57.  
  58. test_carchive()
  59. {
  60.     # get size of file
  61.     let fsize="$(stat --format="%s" "${1}")"
  62.  
  63.     #go back 2 bytes
  64.     let offset="fsize - 2"
  65.  
  66.     # get the byte size put there
  67.     let stat_size="$(xxd -l 2 -ps -c 2 -seek $offset ${1} )"
  68.  
  69.     # scrape off the stat file
  70.     let startofstat="offset - ${stat_size}"
  71.  
  72.     dd if="${1}" bs=1 skip=${startofstat} count=${stat_size} iflag=skip_bytes,count_bytes of="/tmp/${1}.stat"
  73.  
  74.  
  75.     mapfile -t stat_array < "/tmp/${1}.stat"
  76.  
  77.    
  78.     for line in stat_array
  79.     do
  80.     echo "${line}"
  81.     read_stat "${line}"
  82.    
  83.     done
  84.    
  85.  
  86. }
  87.  
  88. read_stat()
  89. {
  90.     # stat  -t  == --format="%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o"
  91.     # %n name  %s size  %b blocks  %u uid  %g gid ....
  92.  
  93. while IFS=$' \n' read -r -a stat_info
  94. do
  95.     offset="${stat_info[0]}"
  96.     >&2 echo "offset = ${offset}"
  97.     name="${stat_info[1]}"
  98.     >&2 echo "name = ${name}"
  99.     size="${stat_info[2]}"
  100.     >&2 echo "size = ${size}"
  101.  
  102. # the rest
  103.     for s in "${stat_info[@]"
  104.     do
  105.     >&2 echo -e "${s} "
  106.     done
  107.     >&2 echo -e "\n"
  108.  
  109. # output
  110. echo "${name} ${offset} ${size}"
  111. }
  112.  
  113.  
  114.  
  115. uncarchive()
  116. {
  117.     if [ $# -lt 4 ] ;then
  118.     echo "badly formatted unarchive hint stop stop stop"
  119.     break
  120.     else
  121.     dd if="${1}" bs=1 skip="${2}" count="${3}" iflag=skip_bytes,count_bytes of="${4}"
  122.     echo "unarchived ${4}"
  123.     fi
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement