1
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-12-19 02:41:57 +00:00

fix(extract): fix conflict if compressed file has a folder of the same name

This change fixes the case where the compressed file (e.g. tools.tgz) only
contains a folder with the same name (e.g. tools) in its root folder.

tools.tgz:
|- tools
   |- fileA.txt
   |- fileB.txt
   \- fileC.txt

In that case, the "smart" folder creation mechanism will extract the files in
a folder "tools", and this extraction folder will contain a single folder with
the same name.

Before this fix, the tool would try to move out the inside folder to the parent
one, but there would already be a folder named "tools", so it would generate a
conflict.

This change first renames the inside folder to a random string, and only then
it is moved outside and the old extraction folder is deleted.
This commit is contained in:
Marc Cornellà 2023-04-06 21:06:16 +02:00
parent cf0c800492
commit 49d34d00cd
No known key found for this signature in database
GPG key ID: 0314585E776A9C1B

View file

@ -98,8 +98,22 @@ EOF
local -a content
content=("${extract_dir}"/*(DNY2))
if [[ ${#content} -eq 1 && -d "${content[1]}" ]]; then
command mv -f "${content[1]}" .
command rmdir "$extract_dir"
# The extracted folder (${content[1]}) may have the same name as $extract_dir
# If so, we need to rename it to avoid conflicts in a 3-step process
#
# 1. Move and rename the extracted folder to a temporary random name
# 2. Delete the empty folder
# 3. Rename the extracted folder to the original name
if [[ "${content[1]:t}" == "$extract_dir" ]]; then
# =(:) gives /tmp/zsh<random>, with :t it gives zsh<random>
local tmp_dir==(:); tmp_dir="${tmp_dir:t}"
command mv -f "${content[1]}" "$tmp_dir" \
&& command rmdir "$extract_dir" \
&& command mv -f "$tmp_dir" "$extract_dir"
else
command mv -f "${content[1]}" . \
&& command rmdir "$extract_dir"
fi
elif [[ ${#content} -eq 0 ]]; then
command rmdir "$extract_dir"
fi