├── LICENSE ├── selfextracttar └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 izabera 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /selfextracttar: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | if [[ $1 = -h || $1 = --help || -z $1 ]]; then 3 | echo "usage: $0 files..." 4 | exit 5 | fi 6 | 7 | script=' 8 | dd < "${BASH_SOURCE-$0}" skip=3 bs=512 2>/dev/null | tar x 9 | exit 10 | ' 11 | 12 | # create a fake tar archive that just happens to be a valid shell script 13 | # https://en.wikipedia.org/wiki/Tar_(computing) 14 | { 15 | #Field Offset Field Size Field 16 | printf %-100s $'#!/bin/sh\n#' # 0 100 File name 17 | printf %-8s 0 # 100 8 File mode 18 | printf %-8s 0 # 108 8 Owner's numeric user ID 19 | printf %-8s 0 # 116 8 Group's numeric user ID 20 | printf %-12o "${#script}" # 124 12 File size in bytes (octal base) 21 | printf %-12s 0 # 136 12 Last modification time in numeric Unix time format (octal) 22 | printf %-8s ' ' # 148 8 Checksum for header record 23 | # K == "this is a long linkname for the next entry" 24 | printf %-1s K # 156 1 Link indicator (file type) 25 | printf %-100s # 157 100 Name of linked file 26 | printf %-255s 27 | 28 | } > archive.sh 29 | 30 | # checksum == sum (as unsigned ints) of all the bytes in the header 31 | checksum=$( 32 | od -An -tu1 -v archive.sh | 33 | tr -s ' \n' + | 34 | sed 's/^+//;s/+$/\n/' | 35 | bc 36 | ) 37 | 38 | # overwrite the bytes in the right position 39 | printf '%-8o' "$checksum" | 40 | dd seek=148 bs=1 count=8 conv=notrunc of=archive.sh 2>/dev/null 41 | 42 | 43 | 44 | 45 | 46 | printf %-512s "$script" >>archive.sh 47 | chmod +x archive.sh 48 | 49 | 50 | 51 | # add a second entry with an empty filename 52 | checksum=$( { 53 | printf \\0%99s 54 | printf %-8s 0 55 | printf %-8s 0 56 | printf %-8s 0 57 | printf %-12o 0 58 | printf %-12s 0 59 | printf %-8s ' ' 60 | printf %-1s V 61 | printf %-100s 62 | printf %-255s 63 | } | od -An -tu1 -v | tr -s ' \n' + | sed 's/^+//;s/+$/\n/' | bc) 64 | { 65 | printf \\0%99s 66 | printf %-8s 0 67 | printf %-8s 0 68 | printf %-8s 0 69 | printf %-12o 0 70 | printf %-12s 0 71 | printf %-8o "$checksum" 72 | # V == "this is a volume header" (thus, this entry is ignored while extracting) 73 | printf %-1s V 74 | printf %-100s 75 | printf %-255s 76 | } >>archive.sh 77 | 78 | 79 | # now we can append our files normally 80 | tar rf archive.sh "$@" 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | self extracting tar archives 2 | ============================ 3 | 4 | 5 | usage 6 | ----- 7 | 8 | ``` 9 | ./selfextracttar file1 file2 file3... 10 | ``` 11 | 12 | it will generate an executable file called `archive.sh`, which can be extracted by either 13 | 14 | - running it (e.g. `./archive.sh`), or 15 | - with tar (e.g. `tar xf archive.sh`) 16 | 17 | 18 | 19 | how it works 20 | ------------ 21 | 22 | the tar format is just a list of {header+data} blocks. 23 | the header is 512 bytes long, the data is always padded to the next 512 bytes boundary. 24 | 25 | to embed an extracting script in a tar we abuse two facts: 26 | 27 | - the first field in the header is the filename of the current file, and 28 | - posix shells parse files line by line 29 | 30 | we start by putting our shebang in the filename field of the first file. then we add __a newline and a `#`__, to make the following data (until the next newline) a comment according to a posix shell. 31 | 32 | we must also make sure that no `\0` appears anywhere in the script, as some shells will choke on those and stop parsing. this means that our shell script must call `exit` to stop processing the rest of the archive. 33 | 34 | the tar format is quite laxly defined, and most implementation will try to be permissive. the standard requires some fields to be `\0`-terminated, but apparently no real world tar enforces this. 35 | 36 | the script we embed is trivial, just skip the first file in the archive with `dd` and pipe the rest to `tar x` 37 | 38 | --- 39 | 40 | now we have a bash script that can extract itself, and that technically is a valid tar archive. `tar t` will show that the first file is called `#!/bin/sh`, which means a file called `sh` in the directory `#!/bin`. `tar f` would happily extract such file and create the needed directories. 41 | 42 | but hey, a modern tar will probably handle a lot more file types than the old primitive implementations in ancient unix. maybe there's a way to mark a file as deleted? 43 | 44 | turns out, there isn't. gnu tar implements deletion by rewriting the archive without that file. but... we don't actually need to mark it as deleted, just a way to avoid extracting it. 45 | 46 | this is way easier to do, as most modern implementations share a few common extensions. the following code is from gnu tar's `tar.h`: 47 | 48 | ``` 49 | /* This file is a tape/volume header. Ignore it on extraction. */ 50 | #define GNUTYPE_VOLHDR 'V' 51 | ``` 52 | 53 | by setting the type field to `'V'` in the header, we create a tar file with a file that will be skipped when extracting. sadly, `tar t` will still list it as the first file in the archive. 54 | 55 | another type that will cause the current file to be skipped is `'K'`, which means that the current file's content are the link name of the next file, which is too long to fit in the next file's link name field. this will get skipped when listing with `tar t`. 56 | 57 | the first file will be type `'K'`, then the next file will be type `'V'`. by combining the two, we achieve the holy grail!... sort of 58 | 59 | `tar t` will still try to list the first file, so we give it an empty file name. this causes `tar t` to print a leading empty line, and`tar tv` to list the first file as a volume header, but this is about the best one can do. for most purposes, this file will only appear to contain the actual content we archived. 60 | --------------------------------------------------------------------------------