Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2024
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. print_padding() {
  4.     [ $1 -eq 0 ] || dd if=/dev/zero bs=$1 count=1 2>/dev/null
  5. }
  6.  
  7. print_tar_header() {
  8.     local checksum=$1; shift
  9.  
  10.     while [ -n "$1" ]; do
  11.         local width=$1; shift
  12.         local format=$1; shift
  13.         local value=$1; shift
  14.  
  15.         # replace checksum (the only field with width 7)
  16.         if [ $width = 7 ]; then
  17.             format='%06o'
  18.             value=$checksum
  19.         fi
  20.  
  21.         local str="$(printf "$format" "$value")"
  22.  
  23.         printf "%.${width}s" "$str"
  24.         [ ${#str} -ge $width ] || print_padding $((width - (${#str} % width)))
  25.     done
  26.  
  27.     return $checksum
  28. }
  29.  
  30. checksum_tar_header() {
  31.     local checksum=0
  32.  
  33.     while [ -n "$1" ]; do
  34.         local width=$1; shift
  35.         local format=$1; shift
  36.         local value=$1; shift
  37.  
  38.         for byte in $(printf "$format" "$value" | hexdump -n $width -ve '1/1 "%u "'); do
  39.             checksum=$((checksum + $byte))
  40.         done
  41.     done
  42.  
  43.     return $checksum
  44. }
  45.  
  46. make_tar_header() {
  47.     checksum_tar_header "$@"
  48.     print_tar_header $? "$@"
  49. }
  50.  
  51. make_tar_member() {
  52.     local name=$1
  53.     local content=$2
  54.     local mode=644
  55.     local uid=1000
  56.     local gid=1000
  57.     local size=${#content}
  58.     local mtime=$(date +%s -r "/tmp/t/$name")
  59.     local type=0
  60.     local link=""
  61.     local username="jow"
  62.     local groupname="jow"
  63.  
  64.     # member header
  65.     make_tar_header \
  66.         100 "%.100s" "$name"      \
  67.           8   "%07d"  $mode       \
  68.           8   "%07o"  $uid        \
  69.           8   "%07o"  $gid        \
  70.          12  "%011o"  $size       \
  71.          12  "%011o"  $mtime      \
  72.           7    "%7s"  ""          \
  73.           1    "%1s"  ""          \
  74.           1     "%d"  $type       \
  75.         100 "%.100s" "$link"      \
  76.           8   "%-7s" "ustar"      \
  77.          32  "%.32s" "$username"  \
  78.          32  "%.32s" "$groupname"
  79.  
  80.     # header padding
  81.     print_padding 183
  82.  
  83.     # content data
  84.     printf "%s" "$content"
  85.  
  86.     # content padding
  87.     print_padding $((512 - (size % 512)))
  88. }
  89.  
  90.  
  91. {
  92.     make_tar_member "example.txt" "Hello there"
  93.     make_tar_member "example-2.txt" "Blah blah"
  94.  
  95.     # alternatively, instead of the dd, run a `tar -c ...` here
  96.     print_padding 1024
  97. } | gzip > /tmp/test.tar.gz
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement