#!/bin/bash if test "$UID" != "0" then echo "$0 error: \$UID is not 0; you should not" echo be running $0 unless you are user root. exit 1 fi OVERWRITE=no if test "$1" = "--overwrite" then OVERWRITE=yes shift fi if test "$1" = "" then echo $0 error: name of tar file not given exit 1 fi if ! test -e $1 then echo $0 error: no file named $1 exit 1 fi # use tar to get a list of all files in the archive, and reformat the list # with sed set -e # Exit immediately if a command exits with a non-zero status LIST1=`tar --list --file=$1 | sed -n -e 's|/$||' -e 1h -e '2,$H' -e '$g' -e '$s/\n/ /g' -e '$p' ` set +e if test "$LIST1" = "" then echo $0 error: no files found in $1 exit 1 fi LIST2= if test "$OVERWRITE" = "yes" then LIST2="$LIST1" else # check each file in LIST1; if the file does NOT exist, add the file # name to LIST2 for A in $LIST1 do if ! test -e $A then LIST2="$LIST2 $A" fi done if test "$LIST2" = "" then echo $0 error: all files in $1 already exist exit 1 fi fi # unarchive all files in LIST2; then make root the owner of all # files in LIST2 #tar --extract --file=$1 $LIST2 #chown 0:0 $LIST2 # no, that does not work because when tar unarchives a directory, # it unarchives every file in the directory #cpio --extract --make-directories --preserve-modification-time \ # --no-preserve-owner --file=$1 $LIST2 # that works #if ! tar --extract --no-recursion --file=$1 $LIST2 #then $EXIT_CODE=$? # echo "$0 error: the following command:" # echo " tar --extract --no-recursion --file=$1 $LIST2" # echo "returned an exit code of $EXIT_CODE" # exit 1 #fi # no, that does not work because --no-recursion is ignored; probably # --no-recursion is only for creating archives. So what happens is you # unarchive a directory, tar unarchives all files in the directory, then you # try to unarchive the first file in the directory, and displays an error # message saying it cannot find the file in the directory. tar has already # unarchived the file; tar cannot find the file because tar has moved past # the file in reading the archive. if test "$OVERWRITE" = "no" then TAR_OPTIONS='--keep-old-files' fi if ! tar --extract $TAR_OPTIONS --file=$1 then $EXIT_CODE=$? echo "$0 error: the following command:" echo " tar --extract $TAR_OPTIONS --file=$1" echo "returned an exit code of $EXIT_CODE" exit 1 fi if ! chown 0:0 $LIST2 then $EXIT_CODE=$? echo "$0 error: the following command:" echo " chown 0:0 $LIST2" echo "returned an exit code of $EXIT_CODE" exit 1 fi exit 0 This shell script is for user root to use tar to unarchive programs. When tar is run by user root, tar assumes it is doing backups, and files which are unarchived will have the UID and GID from when the files were archived, so that files which are restored will have the same owners and groups as when the files were backed up. But if user root is installing a program which was downloaded from the internet, then user root probably does not want the files to have the same owners and groups as when the files were archived; user root probably wants to be the owner of the files. tar will not make user root the owner of the files, unless root was the owner of the files when the archive was made. So user root may have to change the ownership of the files after the files are unarchived. Usually the newly unarchived files will be in a seperate directory from other files, so you can easily change the ownership of the newly unarchived files with a command like 'chown --recursive 0:0 directory_name'; but sometimes the newly unarchived files will be mixed in with other files, and changing the ownership of all the newly unarchived files without changing the other files is difficult. For that, you need tar_unarchive_root. Suppose you have a tar archive named foo.tar. Change to the directory where you want to unarchive foo.tar, and enter the command 'tar_unarchive_root foo.tar'. Note that tar_unarchive_root skips any file if there is already a file with the same name. If you want to overwrite the files which already exist, use the option --overwrite. You can modify tar_unarchive_root to use cpio or afio instead of tar. You can use something like tar_unarchive_root to add a no overwrite option to cpio. Kenneth Howlett av556@detroit.freenet.org