From 7790ea0c680a9f951a86066e5940ec16b2333c9a Mon Sep 17 00:00:00 2001
From: Christian Brabandt <cb@256bit.org>
Date: Tue, 20 Aug 2024 22:41:52 +0200
Subject: [PATCH] patch 9.1.0686: zip-plugin has problems with special
 characters

Problem:  zip-plugin has problems with special characters
          (user202729)
Solution: escape '*?[\' on Unix and handle those chars
          a bit differently on MS-Windows, add a test, check
          before overwriting files

runtime(zip): small fixes for zip plugin

This does the following:
- verify the unzip plugin is executable when loading the autoload plugin
- handle extracting file names with '[*?\' in its name correctly by
  escaping those characters for the unzip command (and handle those
  characters a bit differently on MS-Windows, since the quoting is different)
- verify, that the extract plugin is not overwriting a file (could cause
  a hang, because unzip asking for confirmation)
- add a test zip file which contains those special file names

fixes: #15505
closes: #15519

Signed-off-by: Christian Brabandt <cb@256bit.org>
---
 runtime/autoload/zip.vim | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/runtime/autoload/zip.vim b/runtime/autoload/zip.vim
index 31fb32779f86d8..a7a7e579a2f319 100644
--- a/runtime/autoload/zip.vim
+++ b/runtime/autoload/zip.vim
@@ -1,12 +1,13 @@
 " zip.vim: Handles browsing zipfiles
 "            AUTOLOAD PORTION
-" Date:		Aug 05, 2024
+" Date:		Aug 18, 2024
 " Version:	33
 " Maintainer:	Charles E Campbell <NcampObell@SdrPchip.AorgM-NOSPAM>
 " Last Change:
 " 2024 Jul 23 by Vim Project: fix 'x' command
 " 2024 Aug 04 by Vim Project: escape '[' in name of file to be extracted
 " 2024 Aug 05 by Vim Project: workaround for the FreeBSD's unzip
+" 2024 Aug 18 by Vim Project: correctly handle special globbing chars
 " License:	Vim License  (see vim's :help license)
 " Copyright:    Copyright (C) 2005-2019 Charles E. Campbell {{{1
 "               Permission is hereby granted to use and distribute this code,
@@ -61,6 +62,11 @@ if !exists("g:zip_extractcmd")
  let g:zip_extractcmd= g:zip_unzipcmd
 endif
 
+" sanity checks
+ if !executable(g:zip_unzipcmd)
+  echohl Error | echo "***error*** (zip#Browse) unzip not available on your system" | echohl None
+  finish
+ endif
 if !dist#vim#IsSafeExecutable('zip', g:zip_unzipcmd)
  echoerr "Warning: NOT executing " .. g:zip_unzipcmd .. " from current directory!"
  finish
@@ -228,7 +234,7 @@ fun! zip#Read(fname,mode)
    let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
    let fname   = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
   endif
-  let fname   = substitute(fname, '[', '[[]', 'g')
+  let fname    = fname->substitute('[', '[[]', 'g')->escape('?*\\')
 "  call Decho("zipfile<".zipfile.">")
 "  call Decho("fname  <".fname.">")
   " sanity check
@@ -403,9 +409,24 @@ fun! zip#Extract()
 "   call Dret("zip#Extract")
    return
   endif
+  if filereadable(fname)
+    echohl Error | echo "***error*** (zip#Extract) <".fname."> already exists in directory, not overwriting!" | echohl None
+    return
+   endif
+   let target = fname->substitute('\[', '[[]', 'g')
+   if &shell =~ 'cmd' && (has("win32") || has("win64"))
+     let target = target
+ 		\ ->substitute('[?*]', '[&]', 'g')
+ 		\ ->substitute('[\\]', '?', 'g')
+ 		\ ->shellescape()
+     " there cannot be a file name with '\' in its name, unzip replaces it by _
+     let fname = fname->substitute('[\\?*]', '_', 'g')
+   else
+     let target = target->escape('*?\\')->shellescape()
+   endif
 
   " extract the file mentioned under the cursor
-  call system($"{g:zip_extractcmd} {shellescape(b:zipfile)} {shellescape(fname)}")
+  call system($"{g:zip_extractcmd} -o {shellescape(b:zipfile)} {target}")
 "  call Decho("zipfile<".b:zipfile.">")
   if v:shell_error != 0
    echohl Error | echo "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!" | echohl NONE
-- 
2.43.0

