File: emulate-gnu-tar

package info (click to toggle)
librep 0.92.5-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 4,116 kB
  • ctags: 2,915
  • sloc: ansic: 31,305; lisp: 11,265; sh: 10,704; makefile: 462; sed: 93
file content (172 lines) | stat: -rwxr-xr-x 2,503 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/sh

# emulate-gnu-tar -- emulate the options of GNU tar that librep uses
# in its tar-file handling code.

# $Id$

compression_mode=""
command=""
tarfile=""
to_stdout=""

version="1.0"

original_directory=`pwd`

usage () {
  cat <<EOF
usage: emulate-gnu-tar [OPTIONS..] COMMAND

Supported options include:

	--compress
	--gzip
	--bzip2
	--xz
	--lzma

Supported commands include:

	--version
	--list --file TARFILE --verbose
	--extract --file TARFILE -C DIR
	--extract --file TARFILE --to-stdout FILE

EOF
}

absolutify () {
  case "$1" in
    /*)
      echo $1
      ;;
    *)
      echo "$original_directory/$1"
      ;;
  esac
}

while [ x"$1" != x ]; do
  case $1 in
    --version)
      cat <<EOF
tar (GNU tar) $version
This isn't really GNU tar. It's just a wrapper script used by librep to
make proprietary tars look somewhat like GNU tar. Don't use it.
EOF
      exit 0
      ;;
    
    --compress)
      compression_mode=compress
      ;;

    --gzip)
      compression_mode=gzip
      ;;

    --bzip2)
      compression_mode=bzip2
      ;;

    --xz)
      compression_mode=xz
      ;;

    --lzma)
      compression_mode=lzma
      ;;

    --file)
      tarfile=`absolutify "$2"`
      shift
      ;;

    --verbose)
      ;;

    -C)
      cd "$2"
      shift
      ;;

    --to-stdout)
      to_stdout=$2
      shift
      ;;

    --extract)
      command=extract
      ;;

    --list)
      command=list
      ;;

    *)
      echo "unknown option: $1" >&2
      exit 1
      ;;
  esac
  shift
done

if [ "x$command" = x ]; then
  usage
  exit 1
fi

case "$compression_mode" in
  gzip)
    input="gzip -d -c \"$tarfile\" |"
    ;;

  compress)
    input="compress -d -c \"$tarfile\" |"
    ;;

  bzip2)
    input="bzip2 -d -c \"$tarfile\" |"
    ;;

  xz)
    input="xz -d -c \"$tarfile\" |"
    ;;

  lzma)
    input="lzma -d -c \"$tarfile\" |"
    ;;

  *)
    input="cat \"$tarfile\" |"
    ;;
esac

case "$command" in
  list)
    eval "$input tar tvf -"
    ;;

  extract)
    if [ "x$to_stdout" = "x" ]; then
      eval "$input tar xf -"
      exit $?
    else
      # Extract the file to a temporary directory, then cat it..
      tmpdir="/tmp/rep-emulate-gnu-tar.$$.output"
      mkdir "$tmpdir" || exit $?
      cd "$tmpdir"
      eval "$input tar xf - $to_stdout" || ( rm -rf $tmpdir && exit $? )
      cat "$to_stdout"
      cd "$original_directory"
      rm -rf "$tmpdir"
      exit 0
    fi
    ;;

  *)
    echo "Unimplemented command: $command"
    exit 1
    ;;
esac