a2f1ef69b570b43bbdd0fa29bddb860293b7278b

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

fix(extract): correctly extract rpm files on other directories

Diff

 1diff --git a/plugins/extract/extract.plugin.zsh b/plugins/extract/extract.plugin.zsh
 2index ac181fa78b2cd0ce5c0403a72b7887f2885b48df..12a1ec9489d6af1dfed2a051edd1e87c8a06e973 100644
 3--- a/plugins/extract/extract.plugin.zsh
 4+++ b/plugins/extract/extract.plugin.zsh
 5@@ -20,6 +20,7 @@ extract() {
 6 		shift
 7 	fi
 8 
 9+	local pwd="$PWD"
10 	while (( $# > 0 )); do
11 		if [[ ! -f "$1" ]]; then
12 			echo "extract: '$1' is not a valid file" >&2
13@@ -29,6 +30,7 @@ extract() {
14 
15 		success=0
16 		extract_dir="${1:t:r}"
17+		local full_path="${1:A}"
18 		case "${1:l}" in
19 			(*.tar.gz|*.tgz) (( $+commands[pigz] )) && { pigz -dc "$1" | tar xv } || tar zxvf "$1" ;;
20 			(*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$1" ;;
21@@ -57,17 +59,16 @@ extract() {
22 			(*.z) uncompress "$1" ;;
23 			(*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$1" -d "$extract_dir" ;;
24 			(*.rar) unrar x -ad "$1" ;;
25-			(*.rpm) mkdir "$extract_dir" && cd "$extract_dir" && rpm2cpio "../$1" | cpio --quiet -id && cd .. ;;
26+			(*.rpm)
27+				command mkdir -p "$extract_dir" && builtin cd -q "$extract_dir" \
28+				&& rpm2cpio "$full_path" | cpio --quiet -id ;;
29 			(*.7z) 7za x "$1" ;;
30 			(*.deb)
31-				local pwd="$PWD" file="${1:A}"
32-				mkdir -p "$extract_dir/control" "$extract_dir/data"
33-				builtin cd -q "$extract_dir"; ar vx "$file" > /dev/null
34+				command mkdir -p "$extract_dir/control" "$extract_dir/data"
35+				builtin cd -q "$extract_dir"; ar vx "$full_path" > /dev/null
36 				builtin cd -q control; extract ../control.tar.*
37 				builtin cd -q ../data; extract ../data.tar.*
38-				builtin cd -q ..; command rm *.tar.* debian-binary
39-				builtin cd -q "$pwd"
40-			;;
41+				builtin cd -q ..; command rm *.tar.* debian-binary ;;
42 			(*.zst) unzstd "$1" ;;
43 			(*.cab) cabextract -d "$extract_dir" "$1" ;;
44 			(*.cpio) cpio -idmvF "$1" ;;
45@@ -77,8 +78,11 @@ extract() {
46 			;;
47 		esac
48 
49-		(( success = $success > 0 ? $success : $? ))
50-		(( $success == 0 )) && (( $remove_archive == 0 )) && rm "$1"
51+		(( success = success > 0 ? success : $? ))
52+		(( success == 0 && remove_archive == 0 )) && rm "$full_path"
53 		shift
54+
55+		# Go back to original working directory in case we ran cd previously
56+		builtin cd -q "$pwd"
57 	done
58 }