Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.01 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. make_tar_member() {
  4.     local name=$1
  5.     local content=$2
  6.     local mode=644
  7.     local uid=1000
  8.     local gid=1000
  9.     local size=${#content}
  10.     local mtime=$(date +%s)
  11.     local type=0
  12.     local link=""
  13.     local username="jow"
  14.     local groupname="jow"
  15.  
  16.     printf '%-100s%07d\0%07o\0%07o\0%011o\0%011o\0        %1d%-100sustar  \0%-32s%-32s%183s' \
  17.         "$name" $mode $uid $gid $size $mtime $type \
  18.         "$link" "$username" "$groupname" | tr ' ' '\0' > /tmp/header
  19.  
  20.     local checksum=$((8 * 32))
  21.  
  22.     for byte in $(hexdump -ve '1/1 "%u "' /tmp/header); do
  23.         checksum=$((checksum + $byte))
  24.     done
  25.  
  26.     printf '%06o\0 ' $checksum | dd bs=1 seek=148 count=8 conv=notrunc of=/tmp/header
  27.     cat /tmp/header
  28.     printf "%s" "$content"
  29.  
  30.     local pad=$((512 - (size % 512) ))
  31.     [ $pad -eq 0 ] || dd if=/dev/zero bs=$pad count=1
  32. }
  33.  
  34.  
  35. {
  36.     make_tar_member "example.txt" "Hello there"
  37.     make_tar_member "example-2.txt" "Blah blah"
  38.  
  39.     # alternatively, instead of the dd, run a `tar -c ...` here
  40.     dd if=/dev/zero bs=1024 count=1
  41. } | gzip > /tmp/test.tar.gz
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement